Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmMain: Manage windows from Window menu.
[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         SaveContact();
163         
164         // Reset the form for a new entry.
165         
166         cmbCalendar->SetSelection(-1);
168         editMode = false;
169         eventData.Clear();
170                 
171         SetDefaultDateTime();
172         
173         txtEventName->Clear();
174         txtEventDescription->Clear();
175         
176         cmbCalendar->Show(true);
177         lblCalendar->Show(true);
178         szrDetails->Layout();
179         szrList->Layout();
180         
183 void frmEventEditor::SaveContact(){
185         // Verify that a calendar has been selected.
186         
187         if (cmbCalendar->GetSelection() == -1 && editMode == false){
188                 
189                 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
190                 return;
191                 
192         }
193         
194         // Do verification of entry name (shouldn't be blank).
195         
196         if (txtEventName->GetValue().IsEmpty()){
197                 
198                 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
199                 return; 
200                 
201         }
202         
203         // Do verification of start time.
204         
205         bool timeValid = false;
206         
207         // TODO: Do verification of end time.
208         
209         timeValid = false;
210         
211         // TODO: Do verification of duration.
212         
213         
214         
215         // TODO: Do verification that end date is later than
216         // the start date.
217         
218         
219         
220         bool durationValid = false;
221         
222         // Set the data into the calendar event object.
223                 
224         eventData.summaryData = txtEventName->GetValue().ToStdString();
225         eventData.descriptionList.push_back(txtEventDescription->GetValue().ToStdString());
226         eventData.descriptionListAltRep.push_back("");
227         eventData.descriptionListLanguage.push_back("");
228         eventData.descriptionListTokens.push_back("");
229                 
230         stringstream stringData;
231                 
232         stringData << setw(4) << setfill('0') << dapStartDate->GetValue().GetYear();
233         stringData << setw(2) << setfill('0') << (dapStartDate->GetValue().GetMonth() + 1);
234         stringData << setw(2) << setfill('0') << dapStartDate->GetValue().GetDay();
235         stringData << "T";
236         stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
237         stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
238         stringData << "00Z";
240         eventData.dateTimeStartData = stringData.str();
241         eventData.dateTimeStampData = stringData.str();
242                 
243         stringData.str("");
244                 
245         stringData << setw(4) << setfill('0') << dapEndDate->GetValue().GetYear();
246         stringData << setw(2) << setfill('0') << (dapEndDate->GetValue().GetMonth() + 1);
247         stringData << setw(2) << setfill('0') << dapEndDate->GetValue().GetDay();
248         stringData << "T";
249         stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
250         stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
251         stringData << "00Z";    
253         eventData.dateTimeEndData = stringData.str();
254         
255         // TODO: Implement Duration.
256         
257         if (editMode == false){
258                 
259                 // This is a new event so create a new UUID.
260                 
261                 string NewUUID = GenerateUUID();
262                 
263                 // Setup the calendar directory path.
264                 
265                 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
266                 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
267                 
268                 string calendarDirectory = GetUserDir().ToStdString();
269                 calendarDirectory += "accounts";
270                 calendarDirectory += "/";
271                 calendarDirectory += preferences->accounts.GetAccountDirectory(accountInfo.accountPreferencesID).ToStdString();
272                 calendarDirectory += ".";
273                 calendarDirectory += preferences->accounts.GetAccountType(accountInfo.accountPreferencesID).ToStdString();
274                 calendarDirectory += "/";
275                 calendarDirectory += calendarInfo.calendarTextID;
276                 calendarDirectory += "/";
278                 string eventFile = calendarDirectory;
279                 eventFile += NewUUID;
280                 eventFile += ".ics";
282                 eventData.uniqueID = NewUUID;
284                 // Write the file.
286                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
287                 
288                 // Add the event to the calendar data storage.
289                 
290                 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
291                 
292                 // Post an event to show the new entry in
293                 // the main window.
294                 
295                 EventProperties *eventInfo = new EventProperties;
296                 eventInfo->eventName = txtEventName->GetValue().ToStdString();
297                 eventInfo->calendarID = calendarIDList[cmbCalendar->GetSelection()];
298                 eventInfo->eventID = addEventResult.calendarEntryID;
299                 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
300                 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
301                 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
302                 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
303                 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
304                 eventInfo->eventSecond = 0;
305                 
306                 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
307                 addEvent.SetClientData(eventInfo);
308                 addEvent.SetId(ID_ADDENTRY);
309                 wxPostEvent(this->GetParent(), addEvent);
310                 
311                 eventFilePath = eventFile;
312                 editMode = true;
313                 calendarID = eventInfo->calendarID;
314                 
315                 eventID = addEventResult.calendarEntryID;
316                 
317                 // Hide the calendar selection controls.
318                 
319                 cmbCalendar->Show(false);
320                 lblCalendar->Show(false);
321                 
322                 szrDetails->Layout();
323                 szrList->Layout();
324                 
325         } else {
326                 
327                 // Setup the calendar directory path.
329                 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
330                 
331                 // Write the file.
332                 
333                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
334                 
335                 // Update the data in the calendar data storage.
336                 
337                 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
338                 
339                 // Post an event to update the new entry in
340                 // the main window.
341                 
342                 EventProperties *eventInfo = new EventProperties;
343                 eventInfo->eventName = txtEventName->GetValue().ToStdString();
344                 eventInfo->calendarID = calendarID;
345                 eventInfo->eventID = eventID;
346                 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
347                 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
348                 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
349                 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
350                 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
351                 eventInfo->eventSecond = 0;
352                 
353                 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
354                 updateEvent.SetClientData(eventInfo);
355                 updateEvent.SetId(ID_UPDATEENTRY);
356                 wxPostEvent(this->GetParent(), updateEvent);
357                 
358         }
359         
362 void frmEventEditor::CloseWindow(wxCommandEvent &event)
364         
365         this->Close();
366         
369 void frmEventEditor::CloseWindow(wxCloseEvent &event)
372         WindowData *deleteWindowData = new WindowData;
373     
374         deleteWindowData->DataType = 1;
375         deleteWindowData->WindowPointer = (void*)this;
376         deleteWindowData->WindowID = windowID;
377         
378         // Delete the window from the window list.
379         
380         wxCommandEvent deleteEvent(XCMAIN_DELETEWINDOWINFO);
381         deleteEvent.SetId(ID_DELETEWINDOW);
382         deleteEvent.SetClientData(deleteWindowData);
383         wxPostEvent(this->GetParent(), deleteEvent);
384         
385         this->Destroy();
386         
389 bool frmEventEditor::ProcessEvent(wxEvent& event)
392         // Process the cut/copy/paste events.
394         // This section has been taken from the wxWidgets sample code of richtext.cpp
395         // so that simple Cut/Copy/Paste code can be made.
396     
397         // As this code comes from the samples of the wxWidgets library, this is licenced
398         // under the wxWindows Library Licence and is compatable with the LGPL and is
399         // compatable with Xestia Address Book's licence.
400     
401         if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent))){
402                 // Problem: we can get infinite recursion because the events
403                 // climb back up to this frame, and repeat.
404                 // Assume that command events don't cause another command event
405                 // to be called, so we can rely on inCommand not being overwritten
406         
407                 static int s_eventType = 0;
408                 static wxWindowID s_id = 0;
409         
410                 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
411                         
412                         s_eventType = event.GetEventType();
413                         s_id = event.GetId();
414             
415                         wxWindow* focusWin = wxFindFocusDescendant(this);
416                         if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
417                                 //s_command = NULL;
418                                 s_eventType = 0;
419                                 s_id = 0;
420                                 return true;
421                         }
422                         s_eventType = 0;
423                         s_id = 0;
424                         
425                 } else {
426                 
427                         return false;
428                         
429                 }
430         }
431     
432         return wxFrame::ProcessEvent(event);
433         
436 void frmEventEditor::CutText(wxCommandEvent &event){
437         
440 void frmEventEditor::CopyText(wxCommandEvent &event){
441         
444 void frmEventEditor::PasteText(wxCommandEvent &event){
445         
448 void frmEventEditor::SetEventID(int eventID){
449         
450         this->eventID = eventID;
451         
454 void frmEventEditor::SetDefaultDateTime(){
455         
456         wxDateTime DTNow = wxDateTime::Now();
457         
458         wxTimeSpan oneHour;
459         
460         string formattedTime = "";
461         
462         dapStartDate->SetValue(DTNow);
463         
464         if (DTNow.GetHour() < 10){
465                 formattedTime += "0";
466                 formattedTime += to_string(DTNow.GetHour());
467         } else {
468                 formattedTime += to_string(DTNow.GetHour());
469         }
470         
471         formattedTime += ":";
472         
473         if (DTNow.GetMinute() < 10){
474                 formattedTime += "0";
475                 formattedTime += to_string(DTNow.GetMinute());          
476         } else {
477                 formattedTime += to_string(DTNow.GetMinute());
478         }
479         
480         txtStartTime->SetValue((wxString)formattedTime);
481         
482         dapEndDate->SetValue(DTNow.Add(oneHour));
484         formattedTime.clear();
485         
486         if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
487                 formattedTime += "0";
488                 formattedTime += to_string(DTNow.GetHour());
489         } else {
490                 formattedTime += to_string(DTNow.GetHour());
491         }
492         
493         formattedTime += ":";
494         
495         if (DTNow.GetMinute() < 10){
496                 formattedTime += "0";
497                 formattedTime += to_string(DTNow.GetMinute());
498         } else {
499                 formattedTime += to_string(DTNow.GetMinute());
500         }
501         
502         txtEndTime->SetValue((wxString)formattedTime);
503         
506 void frmEventEditor::SetWindowMenuItemID(int windowID)
508         
509         this->windowID = windowID;
510         
513 void frmEventEditor::ProcessCalendarControl(wxCommandEvent &event)
515         
516         UpdateWindowName();
517         
520 void frmEventEditor::ProcessEventName(wxCommandEvent &event)
522         
523         UpdateWindowName();
524         
527 void frmEventEditor::UpdateWindowName()
529         
530         // Generate the window title.
531         
532         string windowTitle;
533         
534         if (cmbCalendar->GetSelection() == -1)
535         {
536                 windowTitle += "(calendar not selected)";
537         }
538         else
539         {
540                 windowTitle += cmbCalendar->GetStringSelection().ToStdString();
541         }
542         
543         if (txtEventName->IsEmpty())
544         {
545                 windowTitle += " - ";
546                 windowTitle += "(unamed event)";
547         }
548         else
549         {
550                 windowTitle += " - ";
551                 windowTitle += txtEventName->GetValue().ToStdString();
552         }
553         
554         SetTitle(windowTitle);
556         // Check if post window title updating is enabled before
557         // going any further.
558         
559         if (enableUpdates == false)
560         {
561                 return;
562         }
563         
564         WindowData *updateWindowData = new WindowData;
566         updateWindowData->DataType = 1;
567         updateWindowData->WindowPointer = (void*)this;
568         updateWindowData->WindowID = windowID;
569         
570         // Delete the window from the window list.
571         
572         wxCommandEvent updateEvent(XCMAIN_UPDATEWINDOWINFO);
573         updateEvent.SetId(ID_UPDATEWINDOW);
574         updateEvent.SetClientData(updateWindowData);
575         wxPostEvent(this->GetParent(), updateEvent);
576         
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