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 // Load the calendar info.
75 cmbCalendar->Show(false);
76 lblCalendar->Show(false);
81 // Load the data into the form.
83 txtEventName->SetValue(wxString(eventInfo.entryName));
84 txtEventDescription->SetValue(wxString(eventInfo.entryDescription));
86 // Load the start and end dates.
89 startDate.Set(eventInfo.entryStartDay, (wxDateTime::Month)(eventInfo.entryStartMonth -1), eventInfo.entryStartYear, 0, 0, 0);
90 dapStartDate->SetValue(startDate);
93 endDate.Set(eventInfo.entryEndDay, (wxDateTime::Month)(eventInfo.entryEndMonth -1), eventInfo.entryEndYear, 0, 0, 0);
94 dapEndDate->SetValue(endDate);
96 // Load the start and end times.
101 stringstream stringData;
103 stringData << setw(2) << setfill('0') << eventInfo.entryStartHour;
105 stringData << setw(2) << setfill('0') << eventInfo.entryStartMinute;
107 txtStartTime->SetValue((wxString)stringData.str());
110 stringData << setw(2) << setfill('0') << eventInfo.entryEndHour;
112 stringData << setw(2) << setfill('0') << eventInfo.entryEndMinute;
114 txtEndTime->SetValue((wxString)stringData.str());
118 eventFilePath = eventInfo.entryFilename;
120 // Load the required data.
122 eventData.LoadFile(eventFilePath);
124 // TODO: Set the duration data.
129 // Setup the date and time with some default values (today's date and time).
131 SetDefaultDateTime();
135 void frmEventEditor::SetEditMode(bool editMode){
137 this->editMode = editMode;
141 void frmEventEditor::SaveContact(wxCommandEvent &event){
147 void frmEventEditor::SaveNewContact(wxCommandEvent &event){
151 // Reset the form for a new entry.
153 cmbCalendar->SetSelection(-1);
158 SetDefaultDateTime();
160 txtEventName->Clear();
161 txtEventDescription->Clear();
163 cmbCalendar->Show(true);
164 lblCalendar->Show(true);
165 szrDetails->Layout();
172 void frmEventEditor::SaveContact(){
174 // Verify that a calendar has been selected.
176 if (cmbCalendar->GetSelection() == -1 && editMode == false){
178 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
183 // Do verification of entry name (shouldn't be blank).
185 if (txtEventName->GetValue().IsEmpty()){
187 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
192 // Do verification of start time.
194 bool timeValid = false;
196 // TODO: Do verification of end time.
200 // TODO: Do verification of duration.
204 // TODO: Do verification that end date is later than
209 bool durationValid = false;
211 // Set the data into the calendar event object.
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("");
219 stringstream stringData;
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();
225 stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
226 stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
229 eventData.dateTimeStartData = stringData.str();
230 eventData.dateTimeStampData = stringData.str();
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();
238 stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
239 stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
242 eventData.dateTimeEndData = stringData.str();
244 // TODO: Implement Duration.
246 if (editMode == false){
248 // This is a new event so create a new UUID.
250 string NewUUID = GenerateUUID();
252 // Setup the calendar directory path.
254 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
255 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
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;
271 eventData.uniqueID = NewUUID;
275 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
277 // Add the event to the calendar data storage.
279 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
281 // Post an event to show the new entry in
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;
295 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
296 addEvent.SetClientData(eventInfo);
297 addEvent.SetId(ID_ADDENTRY);
298 wxPostEvent(this->GetParent(), addEvent);
300 eventFilePath = eventFile;
302 calendarID = eventInfo->calendarID;
304 eventID = addEventResult.calendarEntryID;
306 // Hide the calendar selection controls.
308 cmbCalendar->Show(false);
309 lblCalendar->Show(false);
311 szrDetails->Layout();
316 // Setup the calendar directory path.
318 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
322 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
324 // Update the data in the calendar data storage.
326 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
328 // Post an event to update the new entry in
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;
342 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
343 updateEvent.SetClientData(eventInfo);
344 updateEvent.SetId(ID_UPDATEENTRY);
345 wxPostEvent(this->GetParent(), updateEvent);
351 void frmEventEditor::CloseWindow(wxCommandEvent &event){
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.
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.
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
375 static int s_eventType = 0;
376 static wxWindowID s_id = 0;
378 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
380 s_eventType = event.GetEventType();
381 s_id = event.GetId();
383 wxWindow* focusWin = wxFindFocusDescendant(this);
384 if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
400 return wxFrame::ProcessEvent(event);
404 void frmEventEditor::CutText(wxCommandEvent &event){
408 void frmEventEditor::CopyText(wxCommandEvent &event){
412 void frmEventEditor::PasteText(wxCommandEvent &event){
416 void frmEventEditor::SetEventID(int eventID){
418 this->eventID = eventID;
422 void frmEventEditor::SetDefaultDateTime(){
424 wxDateTime DTNow = wxDateTime::Now();
428 string formattedTime = "";
430 dapStartDate->SetValue(DTNow);
432 if (DTNow.GetHour() < 10){
433 formattedTime += "0";
434 formattedTime += to_string(DTNow.GetHour());
436 formattedTime += to_string(DTNow.GetHour());
439 formattedTime += ":";
441 if (DTNow.GetMinute() < 10){
442 formattedTime += "0";
443 formattedTime += to_string(DTNow.GetMinute());
445 formattedTime += to_string(DTNow.GetMinute());
448 txtStartTime->SetValue((wxString)formattedTime);
450 dapEndDate->SetValue(DTNow.Add(oneHour));
452 formattedTime.clear();
454 if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
455 formattedTime += "0";
456 formattedTime += to_string(DTNow.GetHour());
458 formattedTime += to_string(DTNow.GetHour());
461 formattedTime += ":";
463 if (DTNow.GetMinute() < 10){
464 formattedTime += "0";
465 formattedTime += to_string(DTNow.GetMinute());
467 formattedTime += to_string(DTNow.GetMinute());
470 txtEndTime->SetValue((wxString)formattedTime);