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 // Load the calendar info.
71 cmbCalendar->Show(false);
72 lblCalendar->Show(false);
77 // Load the data into the form.
79 txtEventName->SetValue(wxString(eventInfo.entryName));
80 txtEventDescription->SetValue(wxString(eventInfo.entryDescription));
82 // Load the start and end dates.
85 startDate.Set(eventInfo.entryStartDay, (wxDateTime::Month)(eventInfo.entryStartMonth -1), eventInfo.entryStartYear, 0, 0, 0);
86 dapStartDate->SetValue(startDate);
89 endDate.Set(eventInfo.entryEndDay, (wxDateTime::Month)(eventInfo.entryEndMonth -1), eventInfo.entryEndYear, 0, 0, 0);
90 dapEndDate->SetValue(endDate);
92 // Load the start and end times.
97 stringstream stringData;
99 stringData << setw(2) << setfill('0') << eventInfo.entryStartHour;
101 stringData << setw(2) << setfill('0') << eventInfo.entryStartMinute;
103 txtStartTime->SetValue((wxString)stringData.str());
106 stringData << setw(2) << setfill('0') << eventInfo.entryEndHour;
108 stringData << setw(2) << setfill('0') << eventInfo.entryEndMinute;
110 txtEndTime->SetValue((wxString)stringData.str());
114 eventFilePath = eventInfo.entryFilename;
116 // Load the required data.
118 eventData.LoadFile(eventFilePath);
120 // TODO: Set the duration data.
125 // Setup the date and time with some default values (today's date and time).
127 SetDefaultDateTime();
131 void frmEventEditor::SetEditMode(bool editMode){
133 this->editMode = editMode;
137 void frmEventEditor::SaveContact(wxCommandEvent &event){
143 void frmEventEditor::SaveNewContact(wxCommandEvent &event){
147 // Reset the form for a new entry.
149 cmbCalendar->SetSelection(-1);
154 SetDefaultDateTime();
156 txtEventName->Clear();
157 txtEventDescription->Clear();
159 cmbCalendar->Show(true);
160 lblCalendar->Show(true);
161 szrDetails->Layout();
168 void frmEventEditor::SaveContact(){
170 // Verify that a calendar has been selected.
172 if (cmbCalendar->GetSelection() == -1 && editMode == false){
174 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
179 // Do verification of entry name (shouldn't be blank).
181 if (txtEventName->GetValue().IsEmpty()){
183 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
188 // Do verification of start time.
190 bool timeValid = false;
192 // TODO: Do verification of end time.
196 // TODO: Do verification of duration.
200 // TODO: Do verification that end date is later than
205 bool durationValid = false;
207 // Set the data into the calendar event object.
209 eventData.summaryData = txtEventName->GetValue().ToStdString();
210 eventData.descriptionList.push_back(txtEventDescription->GetValue().ToStdString());
211 eventData.descriptionListAltRep.push_back("");
212 eventData.descriptionListLanguage.push_back("");
213 eventData.descriptionListTokens.push_back("");
215 stringstream stringData;
217 stringData << setw(4) << setfill('0') << dapStartDate->GetValue().GetYear();
218 stringData << setw(2) << setfill('0') << (dapStartDate->GetValue().GetMonth() + 1);
219 stringData << setw(2) << setfill('0') << dapStartDate->GetValue().GetDay();
221 stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
222 stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
225 eventData.dateTimeStartData = stringData.str();
226 eventData.dateTimeStampData = stringData.str();
230 stringData << setw(4) << setfill('0') << dapEndDate->GetValue().GetYear();
231 stringData << setw(2) << setfill('0') << (dapEndDate->GetValue().GetMonth() + 1);
232 stringData << setw(2) << setfill('0') << dapEndDate->GetValue().GetDay();
234 stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
235 stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
238 eventData.dateTimeEndData = stringData.str();
240 // TODO: Implement Duration.
242 if (editMode == false){
244 // This is a new event so create a new UUID.
246 string NewUUID = GenerateUUID();
248 // Setup the calendar directory path.
250 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
251 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
253 string calendarDirectory = GetUserDir().ToStdString();
254 calendarDirectory += "accounts";
255 calendarDirectory += "/";
256 calendarDirectory += preferences->accounts.GetAccountDirectory(accountInfo.accountPreferencesID).ToStdString();
257 calendarDirectory += ".";
258 calendarDirectory += preferences->accounts.GetAccountType(accountInfo.accountPreferencesID).ToStdString();
259 calendarDirectory += "/";
260 calendarDirectory += calendarInfo.calendarTextID;
261 calendarDirectory += "/";
263 string eventFile = calendarDirectory;
264 eventFile += NewUUID;
267 eventData.uniqueID = NewUUID;
271 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
273 // Add the event to the calendar data storage.
275 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
277 // Post an event to show the new entry in
280 EventProperties *eventInfo = new EventProperties;
281 eventInfo->eventName = txtEventName->GetValue().ToStdString();
282 eventInfo->calendarID = calendarIDList[cmbCalendar->GetSelection()];
283 eventInfo->eventID = addEventResult.calendarEntryID;
284 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
285 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
286 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
287 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
288 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
289 eventInfo->eventSecond = 0;
291 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
292 addEvent.SetClientData(eventInfo);
293 addEvent.SetId(ID_ADDENTRY);
294 wxPostEvent(this->GetParent(), addEvent);
296 eventFilePath = eventFile;
299 eventID = addEventResult.calendarEntryID;
301 // Hide the calendar selection controls.
303 cmbCalendar->Show(false);
304 lblCalendar->Show(false);
306 szrDetails->Layout();
311 // Setup the calendar directory path.
313 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
317 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
319 // Update the data in the calendar data storage.
321 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
323 // Post an event to update the new entry in
326 EventProperties *eventInfo = new EventProperties;
327 eventInfo->eventName = txtEventName->GetValue().ToStdString();
328 eventInfo->calendarID = calendarIDList[cmbCalendar->GetSelection()];
329 eventInfo->eventID = eventID;
330 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
331 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
332 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
333 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
334 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
335 eventInfo->eventSecond = 0;
337 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
338 updateEvent.SetClientData(eventInfo);
339 updateEvent.SetId(ID_UPDATEENTRY);
340 wxPostEvent(this->GetParent(), updateEvent);
346 void frmEventEditor::CloseWindow(wxCommandEvent &event){
352 bool frmEventEditor::ProcessEvent(wxEvent& event)
355 // Process the cut/copy/paste events.
357 // This section has been taken from the wxWidgets sample code of richtext.cpp
358 // so that simple Cut/Copy/Paste code can be made.
360 // As this code comes from the samples of the wxWidgets library, this is licenced
361 // under the wxWindows Library Licence and is compatable with the LGPL and is
362 // compatable with Xestia Address Book's licence.
364 if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent))){
365 // Problem: we can get infinite recursion because the events
366 // climb back up to this frame, and repeat.
367 // Assume that command events don't cause another command event
368 // to be called, so we can rely on inCommand not being overwritten
370 static int s_eventType = 0;
371 static wxWindowID s_id = 0;
373 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
375 s_eventType = event.GetEventType();
376 s_id = event.GetId();
378 wxWindow* focusWin = wxFindFocusDescendant(this);
379 if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
395 return wxFrame::ProcessEvent(event);
399 void frmEventEditor::CutText(wxCommandEvent &event){
403 void frmEventEditor::CopyText(wxCommandEvent &event){
407 void frmEventEditor::PasteText(wxCommandEvent &event){
411 void frmEventEditor::SetEventID(int eventID){
413 this->eventID = eventID;
417 void frmEventEditor::SetDefaultDateTime(){
419 wxDateTime DTNow = wxDateTime::Now();
423 string formattedTime = "";
425 dapStartDate->SetValue(DTNow);
427 if (DTNow.GetHour() < 10){
428 formattedTime += "0";
429 formattedTime += to_string(DTNow.GetHour());
431 formattedTime += to_string(DTNow.GetHour());
434 formattedTime += ":";
436 if (DTNow.GetMinute() < 10){
437 formattedTime += "0";
438 formattedTime += to_string(DTNow.GetMinute());
440 formattedTime += to_string(DTNow.GetMinute());
443 txtStartTime->SetValue((wxString)formattedTime);
445 dapEndDate->SetValue(DTNow.Add(oneHour));
447 formattedTime.clear();
449 if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
450 formattedTime += "0";
451 formattedTime += to_string(DTNow.GetHour());
453 formattedTime += to_string(DTNow.GetHour());
456 formattedTime += ":";
458 if (DTNow.GetMinute() < 10){
459 formattedTime += "0";
460 formattedTime += to_string(DTNow.GetMinute());
462 formattedTime += to_string(DTNow.GetMinute());
465 txtEndTime->SetValue((wxString)formattedTime);