1 // frmEventEditor.cpp - frmEventEditor form functions.
3 // (c) 2016-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar.
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.
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.
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"
23 frmEventEditor::frmEventEditor( wxWindow* parent )
25 frmEventEditorADT( parent )
30 void frmEventEditor::SetupForm(CalendarDataStorage *dataStorage, XCALPreferences *preferences){
32 this->preferences = preferences;
33 this->dataStorage = dataStorage;
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.
38 CDSAccountList accountList = dataStorage->GetAccountList();
40 for (std::vector<CDSGetAccountInfo>::iterator accountSeek = accountList.accountList.begin();
41 accountSeek != accountList.accountList.end(); accountSeek++){
43 CDSCalendarList calendarList = dataStorage->GetCalendarList((*accountSeek).accountID);
45 for (std::vector<int>::iterator calendarSeek = calendarList.calendarList.begin();
46 calendarSeek != calendarList.calendarList.end(); calendarSeek++){
48 string combinedName = "";
50 combinedName = (*accountSeek).accountName;
51 combinedName += " : ";
53 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar((*calendarSeek));
55 combinedName += calendarInfo.calendarName;
56 cmbCalendar->Append((wxString)combinedName);
57 calendarIDList.push_back(calendarInfo.calendarID);
63 // Check the edit mode and get the event data.
65 if (editMode == true){
67 CDSGetCalendarEntryInfo eventInfo = dataStorage->GetEvent(eventID);
69 // Set the calendar ID.
71 calendarID = eventInfo.calendarID;
73 // Set the calendar combination box to the name.
75 string combinedName = dataStorage->GetCalendar(calendarID).accountName;
76 combinedName += " : ";
77 combinedName += dataStorage->GetCalendar(eventInfo.calendarID).calendarName;
79 // Load the calendar info.
81 cmbCalendar->SetSelection(cmbCalendar->FindString(combinedName));
82 cmbCalendar->Show(false);
83 lblCalendar->Show(false);
88 // Load the data into the form.
90 txtEventName->SetValue(wxString(eventInfo.entryName));
91 txtEventDescription->SetValue(wxString(eventInfo.entryDescription));
93 // Load the start and end dates.
96 startDate.Set(eventInfo.entryStartDay, (wxDateTime::Month)(eventInfo.entryStartMonth -1), eventInfo.entryStartYear, 0, 0, 0);
97 dapStartDate->SetValue(startDate);
100 endDate.Set(eventInfo.entryEndDay, (wxDateTime::Month)(eventInfo.entryEndMonth -1), eventInfo.entryEndYear, 0, 0, 0);
101 dapEndDate->SetValue(endDate);
103 // Load the start and end times.
108 stringstream stringData;
110 stringData << setw(2) << setfill('0') << eventInfo.entryStartHour;
112 stringData << setw(2) << setfill('0') << eventInfo.entryStartMinute;
114 txtStartTime->SetValue((wxString)stringData.str());
117 stringData << setw(2) << setfill('0') << eventInfo.entryEndHour;
119 stringData << setw(2) << setfill('0') << eventInfo.entryEndMinute;
121 txtEndTime->SetValue((wxString)stringData.str());
125 eventFilePath = eventInfo.entryFilename;
127 // Load the required data.
129 eventData.LoadFile(eventFilePath);
131 // TODO: Set the duration data.
135 // Setup the date and time with some default values (today's date and time).
137 SetDefaultDateTime();
141 // Enable window updating and update window name.
144 enableUpdates = true;
148 void frmEventEditor::SetEditMode(bool editMode){
150 this->editMode = editMode;
154 void frmEventEditor::SaveContact(wxCommandEvent &event){
160 void frmEventEditor::SaveNewContact(wxCommandEvent &event){
167 // Reset the form for a new entry.
169 cmbCalendar->SetSelection(-1);
174 SetDefaultDateTime();
176 txtEventName->Clear();
177 txtEventDescription->Clear();
179 cmbCalendar->Show(true);
180 lblCalendar->Show(true);
181 szrDetails->Layout();
186 bool frmEventEditor::SaveContact(){
188 // Verify that a calendar has been selected.
190 if (cmbCalendar->GetSelection() == -1 && editMode == false){
192 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
197 // Do verification of entry name (shouldn't be blank).
199 if (txtEventName->GetValue().IsEmpty()){
201 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
206 // Do verification of start time.
208 bool timeValid = false;
210 // TODO: Do verification of end time.
214 // TODO: Do verification of duration.
218 // TODO: Do verification that end date is later than
223 bool durationValid = false;
225 // Set the data into the calendar event object.
227 eventData.summaryData = txtEventName->GetValue().ToStdString();
228 eventData.descriptionList.push_back(txtEventDescription->GetValue().ToStdString());
229 eventData.descriptionListAltRep.push_back("");
230 eventData.descriptionListLanguage.push_back("");
231 eventData.descriptionListTokens.push_back("");
233 stringstream stringData;
235 stringData << setw(4) << setfill('0') << dapStartDate->GetValue().GetYear();
236 stringData << setw(2) << setfill('0') << (dapStartDate->GetValue().GetMonth() + 1);
237 stringData << setw(2) << setfill('0') << dapStartDate->GetValue().GetDay();
239 stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
240 stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
243 eventData.dateTimeStartData = stringData.str();
244 eventData.dateTimeStampData = stringData.str();
248 stringData << setw(4) << setfill('0') << dapEndDate->GetValue().GetYear();
249 stringData << setw(2) << setfill('0') << (dapEndDate->GetValue().GetMonth() + 1);
250 stringData << setw(2) << setfill('0') << dapEndDate->GetValue().GetDay();
252 stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
253 stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
256 eventData.dateTimeEndData = stringData.str();
258 // TODO: Implement Duration.
260 if (editMode == false){
262 // This is a new event so create a new UUID.
264 string NewUUID = GenerateUUID();
266 // Setup the calendar directory path.
268 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
269 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
271 string calendarDirectory = GetUserDir().ToStdString();
272 calendarDirectory += "accounts";
273 calendarDirectory += "/";
274 calendarDirectory += preferences->accounts.GetAccountDirectory(accountInfo.accountPreferencesID).ToStdString();
275 calendarDirectory += ".";
276 calendarDirectory += preferences->accounts.GetAccountType(accountInfo.accountPreferencesID).ToStdString();
277 calendarDirectory += "/";
278 calendarDirectory += calendarInfo.calendarTextID;
279 calendarDirectory += "/";
281 string eventFile = calendarDirectory;
282 eventFile += NewUUID;
285 eventData.uniqueID = NewUUID;
289 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
291 // Add the event to the calendar data storage.
293 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
295 // Post an event to show the new entry in
298 EventProperties *eventInfo = new EventProperties;
299 eventInfo->eventName = txtEventName->GetValue().ToStdString();
300 eventInfo->calendarID = calendarIDList[cmbCalendar->GetSelection()];
301 eventInfo->eventID = addEventResult.calendarEntryID;
302 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
303 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
304 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
305 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
306 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
307 eventInfo->eventSecond = 0;
309 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
310 addEvent.SetClientData(eventInfo);
311 addEvent.SetId(ID_ADDENTRY);
312 wxPostEvent(this->GetParent(), addEvent);
314 eventFilePath = eventFile;
316 calendarID = eventInfo->calendarID;
318 eventID = addEventResult.calendarEntryID;
320 // Hide the calendar selection controls.
322 cmbCalendar->Show(false);
323 lblCalendar->Show(false);
325 szrDetails->Layout();
330 // Setup the calendar directory path.
332 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
336 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
338 // Update the data in the calendar data storage.
340 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
342 // Post an event to update the new entry in
345 EventProperties *eventInfo = new EventProperties;
346 eventInfo->eventName = txtEventName->GetValue().ToStdString();
347 eventInfo->calendarID = calendarID;
348 eventInfo->eventID = eventID;
349 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
350 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
351 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
352 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
353 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
354 eventInfo->eventSecond = 0;
356 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
357 updateEvent.SetClientData(eventInfo);
358 updateEvent.SetId(ID_UPDATEENTRY);
359 wxPostEvent(this->GetParent(), updateEvent);
367 void frmEventEditor::CloseWindow(wxCommandEvent &event)
374 void frmEventEditor::CloseWindow(wxCloseEvent &event)
377 WindowData *deleteWindowData = new WindowData;
379 deleteWindowData->DataType = 1;
380 deleteWindowData->WindowPointer = (void*)this;
381 deleteWindowData->WindowID = windowID;
383 // Delete the window from the window list.
385 wxCommandEvent deleteEvent(XCMAIN_DELETEWINDOWINFO);
386 deleteEvent.SetId(ID_DELETEWINDOW);
387 deleteEvent.SetClientData(deleteWindowData);
388 wxPostEvent(this->GetParent(), deleteEvent);
394 bool frmEventEditor::ProcessEvent(wxEvent& event)
397 // Process the cut/copy/paste events.
399 // This section has been taken from the wxWidgets sample code of richtext.cpp
400 // so that simple Cut/Copy/Paste code can be made.
402 // As this code comes from the samples of the wxWidgets library, this is licenced
403 // under the wxWindows Library Licence and is compatable with the LGPL and is
404 // compatable with Xestia Address Book's licence.
406 if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent))){
407 // Problem: we can get infinite recursion because the events
408 // climb back up to this frame, and repeat.
409 // Assume that command events don't cause another command event
410 // to be called, so we can rely on inCommand not being overwritten
412 static int s_eventType = 0;
413 static wxWindowID s_id = 0;
415 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
417 s_eventType = event.GetEventType();
418 s_id = event.GetId();
420 wxWindow* focusWin = wxFindFocusDescendant(this);
421 if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
437 return wxFrame::ProcessEvent(event);
441 void frmEventEditor::CutText(wxCommandEvent &event){
445 void frmEventEditor::CopyText(wxCommandEvent &event){
449 void frmEventEditor::PasteText(wxCommandEvent &event){
453 void frmEventEditor::SetEventID(int eventID){
455 this->eventID = eventID;
459 void frmEventEditor::SetDefaultDateTime(){
461 wxDateTime DTNow = wxDateTime::Now();
465 string formattedTime = "";
467 dapStartDate->SetValue(DTNow);
469 if (DTNow.GetHour() < 10){
470 formattedTime += "0";
471 formattedTime += to_string(DTNow.GetHour());
473 formattedTime += to_string(DTNow.GetHour());
476 formattedTime += ":";
478 if (DTNow.GetMinute() < 10){
479 formattedTime += "0";
480 formattedTime += to_string(DTNow.GetMinute());
482 formattedTime += to_string(DTNow.GetMinute());
485 txtStartTime->SetValue((wxString)formattedTime);
487 dapEndDate->SetValue(DTNow.Add(oneHour));
489 formattedTime.clear();
491 if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
492 formattedTime += "0";
493 formattedTime += to_string(DTNow.GetHour());
495 formattedTime += to_string(DTNow.GetHour());
498 formattedTime += ":";
500 if (DTNow.GetMinute() < 10){
501 formattedTime += "0";
502 formattedTime += to_string(DTNow.GetMinute());
504 formattedTime += to_string(DTNow.GetMinute());
507 txtEndTime->SetValue((wxString)formattedTime);
511 void frmEventEditor::SetWindowMenuItemID(int windowID)
514 this->windowID = windowID;
518 void frmEventEditor::ProcessCalendarControl(wxCommandEvent &event)
525 void frmEventEditor::ProcessEventName(wxCommandEvent &event)
532 void frmEventEditor::UpdateWindowName()
535 // Generate the window title.
539 if (cmbCalendar->GetSelection() == -1)
541 windowTitle += "(calendar not selected)";
545 windowTitle += cmbCalendar->GetStringSelection().ToStdString();
548 if (txtEventName->IsEmpty())
550 windowTitle += " - ";
551 windowTitle += "(unamed event)";
555 windowTitle += " - ";
556 windowTitle += txtEventName->GetValue().ToStdString();
559 SetTitle(windowTitle);
561 // Check if post window title updating is enabled before
562 // going any further.
564 if (enableUpdates == false)
569 WindowData *updateWindowData = new WindowData;
571 updateWindowData->DataType = 1;
572 updateWindowData->WindowPointer = (void*)this;
573 updateWindowData->WindowID = windowID;
575 // Delete the window from the window list.
577 wxCommandEvent updateEvent(XCMAIN_UPDATEWINDOWINFO);
578 updateEvent.SetId(ID_UPDATEWINDOW);
579 updateEvent.SetClientData(updateWindowData);
580 wxPostEvent(this->GetParent(), updateEvent);