1 #include "frmEventEditor.h"
5 frmEventEditor::frmEventEditor( wxWindow* parent )
7 frmEventEditorADT( parent )
12 void frmEventEditor::SetupForm(CalendarDataStorage *dataStorage, XCALPreferences *preferences){
14 this->preferences = preferences;
15 this->dataStorage = dataStorage;
17 // Go thorugh the accounts and get the list of calendars associated
18 // with the account and add them to the list of available calendars.
20 CDSAccountList accountList = dataStorage->GetAccountList();
22 for (std::vector<CDSGetAccountInfo>::iterator accountSeek = accountList.accountList.begin();
23 accountSeek != accountList.accountList.end(); accountSeek++){
25 CDSCalendarList calendarList = dataStorage->GetCalendarList((*accountSeek).accountID);
27 for (std::vector<int>::iterator calendarSeek = calendarList.calendarList.begin();
28 calendarSeek != calendarList.calendarList.end(); calendarSeek++){
30 string combinedName = "";
32 combinedName = (*accountSeek).accountName;
33 combinedName += " : ";
35 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar((*calendarSeek));
37 combinedName += calendarInfo.calendarName;
38 cmbCalendar->Append((wxString)combinedName);
39 calendarIDList.push_back(calendarInfo.calendarID);
45 // Check the edit mode and get the event data.
47 if (editMode == true){
49 CDSGetCalendarEntryInfo eventInfo = dataStorage->GetEvent(eventID);
51 // Load the calendar info.
53 cmbCalendar->Show(false);
54 lblCalendar->Show(false);
59 // Load the data into the form.
61 txtEventName->SetValue(wxString(eventInfo.entryName));
62 txtEventDescription->SetValue(wxString(eventInfo.entryDescription));
64 // Load the start and end dates.
67 startDate.Set(eventInfo.entryStartDay, (wxDateTime::Month)(eventInfo.entryStartMonth -1), eventInfo.entryStartYear, 0, 0, 0);
68 dapStartDate->SetValue(startDate);
71 endDate.Set(eventInfo.entryEndDay, (wxDateTime::Month)(eventInfo.entryEndMonth -1), eventInfo.entryEndYear, 0, 0, 0);
72 dapEndDate->SetValue(endDate);
74 // Load the start and end times.
79 stringstream stringData;
81 stringData << setw(2) << setfill('0') << eventInfo.entryStartHour;
83 stringData << setw(2) << setfill('0') << eventInfo.entryStartMinute;
85 txtStartTime->SetValue((wxString)stringData.str());
88 stringData << setw(2) << setfill('0') << eventInfo.entryEndHour;
90 stringData << setw(2) << setfill('0') << eventInfo.entryEndMinute;
92 txtEndTime->SetValue((wxString)stringData.str());
96 eventFilePath = eventInfo.entryFilename;
98 // Load the required data.
100 eventData.LoadFile(eventFilePath);
102 // TODO: Set the duration data.
107 // Setup the date and time with some default values (today's date and time).
109 SetDefaultDateTime();
113 void frmEventEditor::SetEditMode(bool editMode){
115 this->editMode = editMode;
119 void frmEventEditor::SaveContact(wxCommandEvent &event){
125 void frmEventEditor::SaveNewContact(wxCommandEvent &event){
129 // Reset the form for a new entry.
131 cmbCalendar->SetSelection(-1);
136 SetDefaultDateTime();
138 txtEventName->Clear();
139 txtEventDescription->Clear();
141 cmbCalendar->Show(true);
142 lblCalendar->Show(true);
143 szrDetails->Layout();
150 void frmEventEditor::SaveContact(){
152 // Verify that a calendar has been selected.
154 if (cmbCalendar->GetSelection() == -1 && editMode == false){
156 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
161 // Do verification of entry name (shouldn't be blank).
163 if (txtEventName->GetValue().IsEmpty()){
165 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
170 // Do verification of start time.
172 bool timeValid = false;
174 // TODO: Do verification of end time.
178 // TODO: Do verification of duration.
182 // TODO: Do verification that end date is later than
187 bool durationValid = false;
189 // Set the data into the calendar event object.
191 eventData.SummaryData = txtEventName->GetValue().ToStdString();
192 eventData.DescriptionList.push_back(txtEventDescription->GetValue().ToStdString());
193 eventData.DescriptionListAltRep.push_back("");
194 eventData.DescriptionListLanguage.push_back("");
195 eventData.DescriptionListTokens.push_back("");
197 stringstream stringData;
199 stringData << setw(4) << setfill('0') << dapStartDate->GetValue().GetYear();
200 stringData << setw(2) << setfill('0') << (dapStartDate->GetValue().GetMonth() + 1);
201 stringData << setw(2) << setfill('0') << dapStartDate->GetValue().GetDay();
203 stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
204 stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
207 eventData.DateTimeStartData = stringData.str();
208 eventData.DateTimeStampData = stringData.str();
212 stringData << setw(4) << setfill('0') << dapEndDate->GetValue().GetYear();
213 stringData << setw(2) << setfill('0') << (dapEndDate->GetValue().GetMonth() + 1);
214 stringData << setw(2) << setfill('0') << dapEndDate->GetValue().GetDay();
216 stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
217 stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
220 eventData.DateTimeEndData = stringData.str();
222 // TODO: Implement Duration.
224 if (editMode == false){
226 // This is a new event so create a new UUID.
228 string NewUUID = GenerateUUID();
230 // Setup the calendar directory path.
232 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
233 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
235 string calendarDirectory = GetUserDir().ToStdString();
236 calendarDirectory += "accounts";
237 calendarDirectory += "/";
238 calendarDirectory += preferences->accounts.GetAccountDirectory(accountInfo.accountPreferencesID).ToStdString();
239 calendarDirectory += ".";
240 calendarDirectory += preferences->accounts.GetAccountType(accountInfo.accountPreferencesID).ToStdString();
241 calendarDirectory += "/";
242 calendarDirectory += calendarInfo.calendarTextID;
243 calendarDirectory += "/";
245 string eventFile = calendarDirectory;
246 eventFile += NewUUID;
249 eventData.UniqueID = NewUUID;
253 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
255 // Add the event to the calendar data storage.
257 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
259 // Post an event to show the new entry in
262 EventProperties *eventInfo = new EventProperties;
263 eventInfo->eventName = txtEventName->GetValue().ToStdString();
264 eventInfo->calendarID = calendarIDList[cmbCalendar->GetSelection()];
265 eventInfo->eventID = addEventResult.calendarEntryID;
266 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
267 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
268 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
269 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
270 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
271 eventInfo->eventSecond = 0;
273 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
274 addEvent.SetClientData(eventInfo);
275 addEvent.SetId(ID_ADDENTRY);
276 wxPostEvent(this->GetParent(), addEvent);
278 eventFilePath = eventFile;
281 eventID = addEventResult.calendarEntryID;
283 // Hide the calendar selection controls.
285 cmbCalendar->Show(false);
286 lblCalendar->Show(false);
288 szrDetails->Layout();
293 // Setup the calendar directory path.
295 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
299 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
301 // Update the data in the calendar data storage.
303 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
305 // Post an event to update the new entry in
308 EventProperties *eventInfo = new EventProperties;
309 eventInfo->eventName = txtEventName->GetValue().ToStdString();
310 eventInfo->calendarID = calendarIDList[cmbCalendar->GetSelection()];
311 eventInfo->eventID = eventID;
312 eventInfo->eventYear = dapStartDate->GetValue().GetYear();
313 eventInfo->eventMonth = dapStartDate->GetValue().GetMonth();
314 eventInfo->eventDay = dapStartDate->GetValue().GetDay();
315 eventInfo->eventHour = atoi(txtStartTime->GetValue().ToStdString().substr(0, 2).c_str());
316 eventInfo->eventMinute = atoi(txtStartTime->GetValue().ToStdString().substr(3, 2).c_str());
317 eventInfo->eventSecond = 0;
319 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
320 updateEvent.SetClientData(eventInfo);
321 updateEvent.SetId(ID_UPDATEENTRY);
322 wxPostEvent(this->GetParent(), updateEvent);
328 void frmEventEditor::CloseWindow(wxCommandEvent &event){
334 bool frmEventEditor::ProcessEvent(wxEvent& event)
337 // Process the cut/copy/paste events.
339 // This section has been taken from the wxWidgets sample code of richtext.cpp
340 // so that simple Cut/Copy/Paste code can be made.
342 // As this code comes from the samples of the wxWidgets library, this is licenced
343 // under the wxWindows Library Licence and is compatable with the LGPL and is
344 // compatable with Xestia Address Book's licence.
346 if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent))){
347 // Problem: we can get infinite recursion because the events
348 // climb back up to this frame, and repeat.
349 // Assume that command events don't cause another command event
350 // to be called, so we can rely on inCommand not being overwritten
352 static int s_eventType = 0;
353 static wxWindowID s_id = 0;
355 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
357 s_eventType = event.GetEventType();
358 s_id = event.GetId();
360 wxWindow* focusWin = wxFindFocusDescendant(this);
361 if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
377 return wxFrame::ProcessEvent(event);
381 void frmEventEditor::CutText(wxCommandEvent &event){
385 void frmEventEditor::CopyText(wxCommandEvent &event){
389 void frmEventEditor::PasteText(wxCommandEvent &event){
393 void frmEventEditor::SetEventID(int eventID){
395 this->eventID = eventID;
399 void frmEventEditor::SetDefaultDateTime(){
401 wxDateTime DTNow = wxDateTime::Now();
405 string formattedTime = "";
407 dapStartDate->SetValue(DTNow);
409 if (DTNow.GetHour() < 10){
410 formattedTime += "0";
411 formattedTime += to_string(DTNow.GetHour());
413 formattedTime += to_string(DTNow.GetHour());
416 formattedTime += ":";
418 if (DTNow.GetMinute() < 10){
419 formattedTime += "0";
420 formattedTime += to_string(DTNow.GetMinute());
422 formattedTime += to_string(DTNow.GetMinute());
425 txtStartTime->SetValue((wxString)formattedTime);
427 dapEndDate->SetValue(DTNow.Add(oneHour));
429 formattedTime.clear();
431 if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
432 formattedTime += "0";
433 formattedTime += to_string(DTNow.GetHour());
435 formattedTime += to_string(DTNow.GetHour());
438 formattedTime += ":";
440 if (DTNow.GetMinute() < 10){
441 formattedTime += "0";
442 formattedTime += to_string(DTNow.GetMinute());
444 formattedTime += to_string(DTNow.GetMinute());
447 txtEndTime->SetValue((wxString)formattedTime);