Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmEventEditor: Set calendar ID when editing event
[xestiacalendar/.git] / source / forms / eventeditor / frmEventEditor.cpp
1 // frmEventEditor.cpp - frmEventEditor form functions.
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Calendar is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "frmEventEditor.h"
21 using namespace std;
23 frmEventEditor::frmEventEditor( wxWindow* parent )
24 :
25 frmEventEditorADT( parent )
26 {
28 }
30 void frmEventEditor::SetupForm(CalendarDataStorage *dataStorage, XCALPreferences *preferences){
31         
32         this->preferences = preferences;
33         this->dataStorage = dataStorage;
34         
35         // Go thorugh the accounts and get the list of calendars associated
36         // with the account and add them to the list of available calendars.
37         
38         CDSAccountList accountList = dataStorage->GetAccountList();
39         
40         for (std::vector<CDSGetAccountInfo>::iterator accountSeek = accountList.accountList.begin();
41                 accountSeek != accountList.accountList.end(); accountSeek++){
42                 
43                 CDSCalendarList calendarList = dataStorage->GetCalendarList((*accountSeek).accountID);
44         
45                 for (std::vector<int>::iterator calendarSeek = calendarList.calendarList.begin();
46                         calendarSeek != calendarList.calendarList.end(); calendarSeek++){
47         
48                         string combinedName = "";
49                         
50                         combinedName = (*accountSeek).accountName;
51                         combinedName += " : ";
52                                 
53                         CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar((*calendarSeek));
54                                 
55                         combinedName += calendarInfo.calendarName;
56                         cmbCalendar->Append((wxString)combinedName);
57                         calendarIDList.push_back(calendarInfo.calendarID);
58                                 
59                 }
60                 
61         }
62         
63         // Check the edit mode and get the event data.
64         
65         if (editMode == true){
66                 
67                 CDSGetCalendarEntryInfo eventInfo = dataStorage->GetEvent(eventID);
68                 
69                 // Set the calendar ID.
70                 
71                 calendarID = eventInfo.calendarID;
72                 
73                 // Load the calendar info.
74                 
75                 cmbCalendar->Show(false);
76                 lblCalendar->Show(false);
77                 
78                 szrDetails->Layout();
79                 szrList->Layout();      
80                 
81                 // Load the data into the form.
82                 
83                 txtEventName->SetValue(wxString(eventInfo.entryName));
84                 txtEventDescription->SetValue(wxString(eventInfo.entryDescription));
85                 
86                 // Load the start and end dates.
87                 
88                 wxDateTime startDate;
89                 startDate.Set(eventInfo.entryStartDay, (wxDateTime::Month)(eventInfo.entryStartMonth -1), eventInfo.entryStartYear, 0, 0, 0);
90                 dapStartDate->SetValue(startDate);
91                 
92                 wxDateTime endDate;
93                 endDate.Set(eventInfo.entryEndDay, (wxDateTime::Month)(eventInfo.entryEndMonth -1), eventInfo.entryEndYear, 0, 0, 0);
94                 dapEndDate->SetValue(endDate);
95                 
96                 // Load the start and end times.
97                 
98                 string startTime;
99                 string endTime;
100                 
101                 stringstream stringData;
102                 
103                 stringData << setw(2) << setfill('0') << eventInfo.entryStartHour;
104                 stringData << ":";
105                 stringData << setw(2) << setfill('0') << eventInfo.entryStartMinute;
106                 
107                 txtStartTime->SetValue((wxString)stringData.str());
108                 stringData.str("");
109                 
110                 stringData << setw(2) << setfill('0') << eventInfo.entryEndHour;
111                 stringData << ":";
112                 stringData << setw(2) << setfill('0') << eventInfo.entryEndMinute;              
113                 
114                 txtEndTime->SetValue((wxString)stringData.str());
115                 
116                 // Set the filename.
117                 
118                 eventFilePath = eventInfo.entryFilename;
119                 
120                 // Load the required data.
121                 
122                 eventData.LoadFile(eventFilePath);
123                 
124                 // TODO: Set the duration data.
125                 
126                 return;
127         }
128         
129         // Setup the date and time with some default values (today's date and time).
130         
131         SetDefaultDateTime();
135 void frmEventEditor::SetEditMode(bool editMode){
136         
137         this->editMode = editMode;
138         
141 void frmEventEditor::SaveContact(wxCommandEvent &event){
142         
143         SaveContact();
144         
147 void frmEventEditor::SaveNewContact(wxCommandEvent &event){
148         
149         SaveContact();
150         
151         // Reset the form for a new entry.
152         
153         cmbCalendar->SetSelection(-1);
155         editMode = false;
156         eventData.Clear();
157                 
158         SetDefaultDateTime();
159         
160         txtEventName->Clear();
161         txtEventDescription->Clear();
162         
163         cmbCalendar->Show(true);
164         lblCalendar->Show(true);
165         szrDetails->Layout();
166         szrList->Layout();
167         
169         
172 void frmEventEditor::SaveContact(){
174         // Verify that a calendar has been selected.
175         
176         if (cmbCalendar->GetSelection() == -1 && editMode == false){
177                 
178                 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
179                 return;
180                 
181         }
182         
183         // Do verification of entry name (shouldn't be blank).
184         
185         if (txtEventName->GetValue().IsEmpty()){
186                 
187                 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
188                 return; 
189                 
190         }
191         
192         // Do verification of start time.
193         
194         bool timeValid = false;
195         
196         // TODO: Do verification of end time.
197         
198         timeValid = false;
199         
200         // TODO: Do verification of duration.
201         
202         
203         
204         // TODO: Do verification that end date is later than
205         // the start date.
206         
207         
208         
209         bool durationValid = false;
210         
211         // Set the data into the calendar event object.
212                 
213         eventData.summaryData = txtEventName->GetValue().ToStdString();
214         eventData.descriptionList.push_back(txtEventDescription->GetValue().ToStdString());
215         eventData.descriptionListAltRep.push_back("");
216         eventData.descriptionListLanguage.push_back("");
217         eventData.descriptionListTokens.push_back("");
218                 
219         stringstream stringData;
220                 
221         stringData << setw(4) << setfill('0') << dapStartDate->GetValue().GetYear();
222         stringData << setw(2) << setfill('0') << (dapStartDate->GetValue().GetMonth() + 1);
223         stringData << setw(2) << setfill('0') << dapStartDate->GetValue().GetDay();
224         stringData << "T";
225         stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
226         stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
227         stringData << "00Z";
229         eventData.dateTimeStartData = stringData.str();
230         eventData.dateTimeStampData = stringData.str();
231                 
232         stringData.str("");
233                 
234         stringData << setw(4) << setfill('0') << dapEndDate->GetValue().GetYear();
235         stringData << setw(2) << setfill('0') << (dapEndDate->GetValue().GetMonth() + 1);
236         stringData << setw(2) << setfill('0') << dapEndDate->GetValue().GetDay();
237         stringData << "T";
238         stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
239         stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
240         stringData << "00Z";    
242         eventData.dateTimeEndData = stringData.str();
243         
244         // TODO: Implement Duration.
245         
246         if (editMode == false){
247                 
248                 // This is a new event so create a new UUID.
249                 
250                 string NewUUID = GenerateUUID();
251                 
252                 // Setup the calendar directory path.
253                 
254                 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
255                 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
256                 
257                 string calendarDirectory = GetUserDir().ToStdString();
258                 calendarDirectory += "accounts";
259                 calendarDirectory += "/";
260                 calendarDirectory += preferences->accounts.GetAccountDirectory(accountInfo.accountPreferencesID).ToStdString();
261                 calendarDirectory += ".";
262                 calendarDirectory += preferences->accounts.GetAccountType(accountInfo.accountPreferencesID).ToStdString();
263                 calendarDirectory += "/";
264                 calendarDirectory += calendarInfo.calendarTextID;
265                 calendarDirectory += "/";
267                 string eventFile = calendarDirectory;
268                 eventFile += NewUUID;
269                 eventFile += ".ics";
271                 eventData.uniqueID = NewUUID;
273                 // Write the file.
275                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
276                 
277                 // Add the event to the calendar data storage.
278                 
279                 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
280                 
281                 // Post an event to show the new entry in
282                 // the main window.
283                 
284                 EventProperties *eventInfo = new EventProperties;
285                 eventInfo->eventName = txtEventName->GetValue().ToStdString();
286                 eventInfo->calendarID = calendarIDList[cmbCalendar->GetSelection()];
287                 eventInfo->eventID = addEventResult.calendarEntryID;
288                 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
289                 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
290                 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
291                 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
292                 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
293                 eventInfo->eventSecond = 0;
294                 
295                 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
296                 addEvent.SetClientData(eventInfo);
297                 addEvent.SetId(ID_ADDENTRY);
298                 wxPostEvent(this->GetParent(), addEvent);
299                 
300                 eventFilePath = eventFile;
301                 editMode = true;
302                 calendarID = eventInfo->calendarID;
303                 
304                 eventID = addEventResult.calendarEntryID;
305                 
306                 // Hide the calendar selection controls.
307                 
308                 cmbCalendar->Show(false);
309                 lblCalendar->Show(false);
310                 
311                 szrDetails->Layout();
312                 szrList->Layout();
313                 
314         } else {
315                 
316                 // Setup the calendar directory path.
318                 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
319                 
320                 // Write the file.
321                 
322                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
323                 
324                 // Update the data in the calendar data storage.
325                 
326                 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
327                 
328                 // Post an event to update the new entry in
329                 // the main window.
330                 
331                 EventProperties *eventInfo = new EventProperties;
332                 eventInfo->eventName = txtEventName->GetValue().ToStdString();
333                 eventInfo->calendarID = calendarID;
334                 eventInfo->eventID = eventID;
335                 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
336                 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
337                 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
338                 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
339                 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
340                 eventInfo->eventSecond = 0;
341                 
342                 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
343                 updateEvent.SetClientData(eventInfo);
344                 updateEvent.SetId(ID_UPDATEENTRY);
345                 wxPostEvent(this->GetParent(), updateEvent);
346                 
347         }
348         
351 void frmEventEditor::CloseWindow(wxCommandEvent &event){
352         
353         this->Close();
354         
357 bool frmEventEditor::ProcessEvent(wxEvent& event)
360         // Process the cut/copy/paste events.
362         // This section has been taken from the wxWidgets sample code of richtext.cpp
363         // so that simple Cut/Copy/Paste code can be made.
364     
365         // As this code comes from the samples of the wxWidgets library, this is licenced
366         // under the wxWindows Library Licence and is compatable with the LGPL and is
367         // compatable with Xestia Address Book's licence.
368     
369         if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent))){
370                 // Problem: we can get infinite recursion because the events
371                 // climb back up to this frame, and repeat.
372                 // Assume that command events don't cause another command event
373                 // to be called, so we can rely on inCommand not being overwritten
374         
375                 static int s_eventType = 0;
376                 static wxWindowID s_id = 0;
377         
378                 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
379                         
380                         s_eventType = event.GetEventType();
381                         s_id = event.GetId();
382             
383                         wxWindow* focusWin = wxFindFocusDescendant(this);
384                         if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
385                                 //s_command = NULL;
386                                 s_eventType = 0;
387                                 s_id = 0;
388                                 return true;
389                         }
390                         s_eventType = 0;
391                         s_id = 0;
392                         
393                 } else {
394                 
395                         return false;
396                         
397                 }
398         }
399     
400         return wxFrame::ProcessEvent(event);
401         
404 void frmEventEditor::CutText(wxCommandEvent &event){
405         
408 void frmEventEditor::CopyText(wxCommandEvent &event){
409         
412 void frmEventEditor::PasteText(wxCommandEvent &event){
413         
416 void frmEventEditor::SetEventID(int eventID){
417         
418         this->eventID = eventID;
419         
422 void frmEventEditor::SetDefaultDateTime(){
423         
424         wxDateTime DTNow = wxDateTime::Now();
425         
426         wxTimeSpan oneHour;
427         
428         string formattedTime = "";
429         
430         dapStartDate->SetValue(DTNow);
431         
432         if (DTNow.GetHour() < 10){
433                 formattedTime += "0";
434                 formattedTime += to_string(DTNow.GetHour());
435         } else {
436                 formattedTime += to_string(DTNow.GetHour());
437         }
438         
439         formattedTime += ":";
440         
441         if (DTNow.GetMinute() < 10){
442                 formattedTime += "0";
443                 formattedTime += to_string(DTNow.GetMinute());          
444         } else {
445                 formattedTime += to_string(DTNow.GetMinute());
446         }
447         
448         txtStartTime->SetValue((wxString)formattedTime);
449         
450         dapEndDate->SetValue(DTNow.Add(oneHour));
452         formattedTime.clear();
453         
454         if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
455                 formattedTime += "0";
456                 formattedTime += to_string(DTNow.GetHour());
457         } else {
458                 formattedTime += to_string(DTNow.GetHour());
459         }
460         
461         formattedTime += ":";
462         
463         if (DTNow.GetMinute() < 10){
464                 formattedTime += "0";
465                 formattedTime += to_string(DTNow.GetMinute());
466         } else {
467                 formattedTime += to_string(DTNow.GetMinute());
468         }
469         
470         txtEndTime->SetValue((wxString)formattedTime);
471         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy