Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmEventEditor: Fixed issue where calendar event description wasn’t being implemented...
[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                 // Set the calendar combination box to the name.
74                 
75                 string combinedName = dataStorage->GetCalendar(calendarID).accountName;
76                 combinedName += " : ";
77                 combinedName += dataStorage->GetCalendar(eventInfo.calendarID).calendarName;
78                 
79                 // Load the calendar info.
80                 
81                 cmbCalendar->SetSelection(cmbCalendar->FindString(combinedName));
82                 cmbCalendar->Show(false);
83                 lblCalendar->Show(false);
84                 
85                 szrDetails->Layout();
86                 szrList->Layout();      
87                 
88                 // Load the data into the form.
89                 
90                 txtEventName->SetValue(wxString(eventInfo.entryName));
91                 txtEventDescription->SetValue(wxString(eventInfo.entryDescription));
92                 
93                 // Load the start and end dates.
94                 
95                 wxDateTime startDate;
96                 startDate.Set(eventInfo.entryStartDay, (wxDateTime::Month)(eventInfo.entryStartMonth -1), eventInfo.entryStartYear, 0, 0, 0);
97                 dapStartDate->SetValue(startDate);
98                 
99                 wxDateTime endDate;
100                 endDate.Set(eventInfo.entryEndDay, (wxDateTime::Month)(eventInfo.entryEndMonth -1), eventInfo.entryEndYear, 0, 0, 0);
101                 dapEndDate->SetValue(endDate);
102                 
103                 // Load the start and end times.
104                 
105                 string startTime;
106                 string endTime;
107                 
108                 stringstream stringData;
109                 
110                 stringData << setw(2) << setfill('0') << eventInfo.entryStartHour;
111                 stringData << ":";
112                 stringData << setw(2) << setfill('0') << eventInfo.entryStartMinute;
113                 
114                 txtStartTime->SetValue((wxString)stringData.str());
115                 stringData.str("");
116                 
117                 stringData << setw(2) << setfill('0') << eventInfo.entryEndHour;
118                 stringData << ":";
119                 stringData << setw(2) << setfill('0') << eventInfo.entryEndMinute;              
120                 
121                 txtEndTime->SetValue((wxString)stringData.str());
122                 
123                 // Set the filename.
124                 
125                 eventFilePath = eventInfo.entryFilename;
126                 
127                 // Load the required data.
128                 
129                 eventData.LoadFile(eventFilePath);
130                 
131                 // TODO: Set the duration data.
133         } else {
134         
135                 // Setup the date and time with some default values (today's date and time).
137                 SetDefaultDateTime();
138                 
139         }
141         // Enable window updating and update window name.
143         UpdateWindowName();
144         enableUpdates = true;
145         
148 void frmEventEditor::SetEditMode(bool editMode){
149         
150         this->editMode = editMode;
151         
154 void frmEventEditor::SaveContact(wxCommandEvent &event){
155         
156         SaveContact();
157         
160 void frmEventEditor::SaveNewContact(wxCommandEvent &event){
161         
162         if (!SaveContact())
163         {
164                 return;
165         }
166         
167         // Reset the form for a new entry.
168         
169         cmbCalendar->SetSelection(-1);
171         editMode = false;
172         eventData.Clear();
173                 
174         SetDefaultDateTime();
175         
176         txtEventName->Clear();
177         txtEventDescription->Clear();
178         
179         cmbCalendar->Show(true);
180         lblCalendar->Show(true);
181         szrDetails->Layout();
182         szrList->Layout();
183         
186 bool frmEventEditor::SaveContact(){
188         // Verify that a calendar has been selected.
189         
190         if (cmbCalendar->GetSelection() == -1 && editMode == false){
191                 
192                 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
193                 return false;
194                 
195         }
196         
197         // Do verification of entry name (shouldn't be blank).
198         
199         if (txtEventName->GetValue().IsEmpty()){
200                 
201                 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
202                 return false;
203                 
204         }
205         
206         // Do verification of start time.
207         
208         bool timeValid = false;
209         
210         // TODO: Do verification of end time.
211         
212         timeValid = false;
213         
214         // TODO: Do verification of duration.
215         
216         
217         
218         // TODO: Do verification that end date is later than
219         // the start date.
220         
221         
222         
223         bool durationValid = false;
224         
225         // Set the data into the calendar event object.
226                 
227         eventData.summaryData = txtEventName->GetValue().ToStdString();
228     
229     if (eventData.descriptionList.size() > 0)
230     {
231         eventData.descriptionList[0] = txtEventDescription->GetValue().ToStdString();
232     }
233     else
234     {
235         eventData.descriptionList.push_back(txtEventDescription->GetValue().ToStdString());
236         eventData.descriptionListAltRep.push_back("");
237         eventData.descriptionListLanguage.push_back("");
238         eventData.descriptionListTokens.push_back("");
239     }
240     
241         eventData.descriptionList.push_back(txtEventDescription->GetValue().ToStdString());
242         eventData.descriptionListAltRep.push_back("");
243         eventData.descriptionListLanguage.push_back("");
244         eventData.descriptionListTokens.push_back("");
245                 
246         stringstream stringData;
247                 
248         stringData << setw(4) << setfill('0') << dapStartDate->GetValue().GetYear();
249         stringData << setw(2) << setfill('0') << (dapStartDate->GetValue().GetMonth() + 1);
250         stringData << setw(2) << setfill('0') << dapStartDate->GetValue().GetDay();
251         stringData << "T";
252         stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
253         stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
254         stringData << "00Z";
256         eventData.dateTimeStartData = stringData.str();
257         eventData.dateTimeStampData = stringData.str();
258                 
259         stringData.str("");
260                 
261         stringData << setw(4) << setfill('0') << dapEndDate->GetValue().GetYear();
262         stringData << setw(2) << setfill('0') << (dapEndDate->GetValue().GetMonth() + 1);
263         stringData << setw(2) << setfill('0') << dapEndDate->GetValue().GetDay();
264         stringData << "T";
265         stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
266         stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
267         stringData << "00Z";    
269         eventData.dateTimeEndData = stringData.str();
270         
271         // TODO: Implement Duration.
272         
273         if (editMode == false){
274                 
275                 // This is a new event so create a new UUID.
276                 
277                 string NewUUID = GenerateUUID();
278                 
279                 // Setup the calendar directory path.
280                 
281                 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
282                 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
283                 
284                 string calendarDirectory = GetUserDir().ToStdString();
285                 calendarDirectory += "accounts";
286                 calendarDirectory += "/";
287                 calendarDirectory += preferences->accounts.GetAccountDirectory(accountInfo.accountPreferencesID).ToStdString();
288                 calendarDirectory += ".";
289                 calendarDirectory += preferences->accounts.GetAccountType(accountInfo.accountPreferencesID).ToStdString();
290                 calendarDirectory += "/";
291                 calendarDirectory += calendarInfo.calendarTextID;
292                 calendarDirectory += "/";
294                 string eventFile = calendarDirectory;
295                 eventFile += NewUUID;
296                 eventFile += ".ics";
298                 eventData.uniqueID = NewUUID;
300                 // Write the file.
302                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
303                 
304                 // Add the event to the calendar data storage.
305                 
306                 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
307                 
308                 // Post an event to show the new entry in
309                 // the main window.
310                 
311                 EventProperties *eventInfo = new EventProperties;
312                 eventInfo->eventName = txtEventName->GetValue().ToStdString();
313         eventInfo->eventDescipriton = txtEventDescription->GetValue().ToStdString();
314                 eventInfo->calendarID = calendarIDList[cmbCalendar->GetSelection()];
315                 eventInfo->eventID = addEventResult.calendarEntryID;
316                 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
317                 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
318                 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
319                 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
320                 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
321                 eventInfo->eventSecond = 0;
322                 
323                 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
324                 addEvent.SetClientData(eventInfo);
325                 addEvent.SetId(ID_ADDENTRY);
326                 wxPostEvent(this->GetParent(), addEvent);
327                 
328                 eventFilePath = eventFile;
329                 editMode = true;
330                 calendarID = eventInfo->calendarID;
331                 
332                 eventID = addEventResult.calendarEntryID;
333                 
334                 // Hide the calendar selection controls.
335                 
336                 cmbCalendar->Show(false);
337                 lblCalendar->Show(false);
338                 
339                 szrDetails->Layout();
340                 szrList->Layout();
341                 
342         } else {
343                 
344                 // Setup the calendar directory path.
346                 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
347                 
348                 // Write the file.
349                 
350                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
351                 
352                 // Update the data in the calendar data storage.
353                 
354                 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
355                 
356                 // Post an event to update the new entry in
357                 // the main window.
358                 
359                 EventProperties *eventInfo = new EventProperties;
360                 eventInfo->eventName = txtEventName->GetValue().ToStdString();
361         eventInfo->eventDescipriton = txtEventDescription->GetValue().ToStdString();
362                 eventInfo->calendarID = calendarID;
363                 eventInfo->eventID = eventID;
364                 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
365                 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
366                 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
367                 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
368                 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
369                 eventInfo->eventSecond = 0;
370                 
371                 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
372                 updateEvent.SetClientData(eventInfo);
373                 updateEvent.SetId(ID_UPDATEENTRY);
374                 wxPostEvent(this->GetParent(), updateEvent);
375                 
376         }
377         
378         return true;
379         
382 void frmEventEditor::CloseWindow(wxCommandEvent &event)
384         
385         this->Close();
386         
389 void frmEventEditor::CloseWindow(wxCloseEvent &event)
392         WindowData *deleteWindowData = new WindowData;
393     
394         deleteWindowData->DataType = 1;
395         deleteWindowData->WindowPointer = (void*)this;
396         deleteWindowData->WindowID = windowID;
397         
398         // Delete the window from the window list.
399         
400         wxCommandEvent deleteEvent(XCMAIN_DELETEWINDOWINFO);
401         deleteEvent.SetId(ID_DELETEWINDOW);
402         deleteEvent.SetClientData(deleteWindowData);
403         wxPostEvent(this->GetParent(), deleteEvent);
404         
405         this->Destroy();
406         
409 bool frmEventEditor::ProcessEvent(wxEvent& event)
412         // Process the cut/copy/paste events.
414         // This section has been taken from the wxWidgets sample code of richtext.cpp
415         // so that simple Cut/Copy/Paste code can be made.
416     
417         // As this code comes from the samples of the wxWidgets library, this is licenced
418         // under the wxWindows Library Licence and is compatable with the LGPL and is
419         // compatable with Xestia Address Book's licence.
420     
421         if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent))){
422                 // Problem: we can get infinite recursion because the events
423                 // climb back up to this frame, and repeat.
424                 // Assume that command events don't cause another command event
425                 // to be called, so we can rely on inCommand not being overwritten
426         
427                 static int s_eventType = 0;
428                 static wxWindowID s_id = 0;
429         
430                 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
431                         
432                         s_eventType = event.GetEventType();
433                         s_id = event.GetId();
434             
435                         wxWindow* focusWin = wxFindFocusDescendant(this);
436                         if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
437                                 //s_command = NULL;
438                                 s_eventType = 0;
439                                 s_id = 0;
440                                 return true;
441                         }
442                         s_eventType = 0;
443                         s_id = 0;
444                         
445                 } else {
446                 
447                         return false;
448                         
449                 }
450         }
451     
452         return wxFrame::ProcessEvent(event);
453         
456 void frmEventEditor::CutText(wxCommandEvent &event){
457         
460 void frmEventEditor::CopyText(wxCommandEvent &event){
461         
464 void frmEventEditor::PasteText(wxCommandEvent &event){
465         
468 void frmEventEditor::SetEventID(int eventID){
469         
470         this->eventID = eventID;
471         
474 void frmEventEditor::SetDefaultDateTime(){
475         
476         wxDateTime DTNow = wxDateTime::Now();
477         
478         wxTimeSpan oneHour;
479         
480         string formattedTime = "";
481         
482         dapStartDate->SetValue(DTNow);
483         
484         if (DTNow.GetHour() < 10){
485                 formattedTime += "0";
486                 formattedTime += to_string(DTNow.GetHour());
487         } else {
488                 formattedTime += to_string(DTNow.GetHour());
489         }
490         
491         formattedTime += ":";
492         
493         if (DTNow.GetMinute() < 10){
494                 formattedTime += "0";
495                 formattedTime += to_string(DTNow.GetMinute());          
496         } else {
497                 formattedTime += to_string(DTNow.GetMinute());
498         }
499         
500         txtStartTime->SetValue((wxString)formattedTime);
501         
502         dapEndDate->SetValue(DTNow.Add(oneHour));
504         formattedTime.clear();
505         
506         if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
507                 formattedTime += "0";
508                 formattedTime += to_string(DTNow.GetHour());
509         } else {
510                 formattedTime += to_string(DTNow.GetHour());
511         }
512         
513         formattedTime += ":";
514         
515         if (DTNow.GetMinute() < 10){
516                 formattedTime += "0";
517                 formattedTime += to_string(DTNow.GetMinute());
518         } else {
519                 formattedTime += to_string(DTNow.GetMinute());
520         }
521         
522         txtEndTime->SetValue((wxString)formattedTime);
523         
526 void frmEventEditor::SetWindowMenuItemID(int windowID)
528         
529         this->windowID = windowID;
530         
533 void frmEventEditor::ProcessCalendarControl(wxCommandEvent &event)
535         
536         UpdateWindowName();
537         
540 void frmEventEditor::ProcessEventName(wxCommandEvent &event)
542         
543         UpdateWindowName();
544         
547 void frmEventEditor::UpdateWindowName()
549         
550         // Generate the window title.
551         
552         string windowTitle;
553         
554         if (cmbCalendar->GetSelection() == -1)
555         {
556                 windowTitle += "(calendar not selected)";
557         }
558         else
559         {
560                 windowTitle += cmbCalendar->GetStringSelection().ToStdString();
561         }
562         
563         if (txtEventName->IsEmpty())
564         {
565                 windowTitle += " - ";
566                 windowTitle += "(unamed event)";
567         }
568         else
569         {
570                 windowTitle += " - ";
571                 windowTitle += txtEventName->GetValue().ToStdString();
572         }
573         
574         SetTitle(windowTitle);
576         // Check if post window title updating is enabled before
577         // going any further.
578         
579         if (enableUpdates == false)
580         {
581                 return;
582         }
583         
584         WindowData *updateWindowData = new WindowData;
586         updateWindowData->DataType = 1;
587         updateWindowData->WindowPointer = (void*)this;
588         updateWindowData->WindowID = windowID;
589         
590         // Delete the window from the window list.
591         
592         wxCommandEvent updateEvent(XCMAIN_UPDATEWINDOWINFO);
593         updateEvent.SetId(ID_UPDATEWINDOW);
594         updateEvent.SetClientData(updateWindowData);
595         wxPostEvent(this->GetParent(), updateEvent);
596         
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