Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
camelCase: Converted remaining code that was missed
[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                 // Load the calendar info.
70                 
71                 cmbCalendar->Show(false);
72                 lblCalendar->Show(false);
73                 
74                 szrDetails->Layout();
75                 szrList->Layout();              
76                 
77                 // Load the data into the form.
78                 
79                 txtEventName->SetValue(wxString(eventInfo.entryName));
80                 txtEventDescription->SetValue(wxString(eventInfo.entryDescription));
81                 
82                 // Load the start and end dates.
83                 
84                 wxDateTime startDate;
85                 startDate.Set(eventInfo.entryStartDay, (wxDateTime::Month)(eventInfo.entryStartMonth -1), eventInfo.entryStartYear, 0, 0, 0);
86                 dapStartDate->SetValue(startDate);
87                 
88                 wxDateTime endDate;
89                 endDate.Set(eventInfo.entryEndDay, (wxDateTime::Month)(eventInfo.entryEndMonth -1), eventInfo.entryEndYear, 0, 0, 0);
90                 dapEndDate->SetValue(endDate);
91                 
92                 // Load the start and end times.
93                 
94                 string startTime;
95                 string endTime;
96                 
97                 stringstream stringData;
98                 
99                 stringData << setw(2) << setfill('0') << eventInfo.entryStartHour;
100                 stringData << ":";
101                 stringData << setw(2) << setfill('0') << eventInfo.entryStartMinute;
102                 
103                 txtStartTime->SetValue((wxString)stringData.str());
104                 stringData.str("");
105                 
106                 stringData << setw(2) << setfill('0') << eventInfo.entryEndHour;
107                 stringData << ":";
108                 stringData << setw(2) << setfill('0') << eventInfo.entryEndMinute;              
109                 
110                 txtEndTime->SetValue((wxString)stringData.str());
111                 
112                 // Set the filename.
113                 
114                 eventFilePath = eventInfo.entryFilename;
115                 
116                 // Load the required data.
117                 
118                 eventData.LoadFile(eventFilePath);
119                 
120                 // TODO: Set the duration data.
121                 
122                 return;
123         }
124         
125         // Setup the date and time with some default values (today's date and time).
126         
127         SetDefaultDateTime();
131 void frmEventEditor::SetEditMode(bool editMode){
132         
133         this->editMode = editMode;
134         
137 void frmEventEditor::SaveContact(wxCommandEvent &event){
138         
139         SaveContact();
140         
143 void frmEventEditor::SaveNewContact(wxCommandEvent &event){
144         
145         SaveContact();
146         
147         // Reset the form for a new entry.
148         
149         cmbCalendar->SetSelection(-1);
151         editMode = false;
152         eventData.Clear();
153                 
154         SetDefaultDateTime();
155         
156         txtEventName->Clear();
157         txtEventDescription->Clear();
158         
159         cmbCalendar->Show(true);
160         lblCalendar->Show(true);
161         szrDetails->Layout();
162         szrList->Layout();
163         
165         
168 void frmEventEditor::SaveContact(){
170         // Verify that a calendar has been selected.
171         
172         if (cmbCalendar->GetSelection() == -1 && editMode == false){
173                 
174                 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
175                 return;
176                 
177         }
178         
179         // Do verification of entry name (shouldn't be blank).
180         
181         if (txtEventName->GetValue().IsEmpty()){
182                 
183                 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
184                 return; 
185                 
186         }
187         
188         // Do verification of start time.
189         
190         bool timeValid = false;
191         
192         // TODO: Do verification of end time.
193         
194         timeValid = false;
195         
196         // TODO: Do verification of duration.
197         
198         
199         
200         // TODO: Do verification that end date is later than
201         // the start date.
202         
203         
204         
205         bool durationValid = false;
206         
207         // Set the data into the calendar event object.
208                 
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("");
214                 
215         stringstream stringData;
216                 
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();
220         stringData << "T";
221         stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
222         stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
223         stringData << "00Z";
225         eventData.dateTimeStartData = stringData.str();
226         eventData.dateTimeStampData = stringData.str();
227                 
228         stringData.str("");
229                 
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();
233         stringData << "T";
234         stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
235         stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
236         stringData << "00Z";    
238         eventData.dateTimeEndData = stringData.str();
239         
240         // TODO: Implement Duration.
241         
242         if (editMode == false){
243                 
244                 // This is a new event so create a new UUID.
245                 
246                 string NewUUID = GenerateUUID();
247                 
248                 // Setup the calendar directory path.
249                 
250                 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
251                 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
252                 
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;
265                 eventFile += ".ics";
267                 eventData.uniqueID = NewUUID;
269                 // Write the file.
271                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
272                 
273                 // Add the event to the calendar data storage.
274                 
275                 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
276                 
277                 // Post an event to show the new entry in
278                 // the main window.
279                 
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;
290                 
291                 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
292                 addEvent.SetClientData(eventInfo);
293                 addEvent.SetId(ID_ADDENTRY);
294                 wxPostEvent(this->GetParent(), addEvent);
295                 
296                 eventFilePath = eventFile;
297                 editMode = true;
298                 
299                 eventID = addEventResult.calendarEntryID;
300                 
301                 // Hide the calendar selection controls.
302                 
303                 cmbCalendar->Show(false);
304                 lblCalendar->Show(false);
305                 
306                 szrDetails->Layout();
307                 szrList->Layout();
308                 
309         } else {
310                 
311                 // Setup the calendar directory path.
313                 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
314                 
315                 // Write the file.
316                 
317                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
318                 
319                 // Update the data in the calendar data storage.
320                 
321                 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
322                 
323                 // Post an event to update the new entry in
324                 // the main window.
325                 
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;
336                 
337                 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
338                 updateEvent.SetClientData(eventInfo);
339                 updateEvent.SetId(ID_UPDATEENTRY);
340                 wxPostEvent(this->GetParent(), updateEvent);
341                 
342         }
343         
346 void frmEventEditor::CloseWindow(wxCommandEvent &event){
347         
348         this->Close();
349         
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.
359     
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.
363     
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
369         
370                 static int s_eventType = 0;
371                 static wxWindowID s_id = 0;
372         
373                 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
374                         
375                         s_eventType = event.GetEventType();
376                         s_id = event.GetId();
377             
378                         wxWindow* focusWin = wxFindFocusDescendant(this);
379                         if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
380                                 //s_command = NULL;
381                                 s_eventType = 0;
382                                 s_id = 0;
383                                 return true;
384                         }
385                         s_eventType = 0;
386                         s_id = 0;
387                         
388                 } else {
389                 
390                         return false;
391                         
392                 }
393         }
394     
395         return wxFrame::ProcessEvent(event);
396         
399 void frmEventEditor::CutText(wxCommandEvent &event){
400         
403 void frmEventEditor::CopyText(wxCommandEvent &event){
404         
407 void frmEventEditor::PasteText(wxCommandEvent &event){
408         
411 void frmEventEditor::SetEventID(int eventID){
412         
413         this->eventID = eventID;
414         
417 void frmEventEditor::SetDefaultDateTime(){
418         
419         wxDateTime DTNow = wxDateTime::Now();
420         
421         wxTimeSpan oneHour;
422         
423         string formattedTime = "";
424         
425         dapStartDate->SetValue(DTNow);
426         
427         if (DTNow.GetHour() < 10){
428                 formattedTime += "0";
429                 formattedTime += to_string(DTNow.GetHour());
430         } else {
431                 formattedTime += to_string(DTNow.GetHour());
432         }
433         
434         formattedTime += ":";
435         
436         if (DTNow.GetMinute() < 10){
437                 formattedTime += "0";
438                 formattedTime += to_string(DTNow.GetMinute());          
439         } else {
440                 formattedTime += to_string(DTNow.GetMinute());
441         }
442         
443         txtStartTime->SetValue((wxString)formattedTime);
444         
445         dapEndDate->SetValue(DTNow.Add(oneHour));
447         formattedTime.clear();
448         
449         if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
450                 formattedTime += "0";
451                 formattedTime += to_string(DTNow.GetHour());
452         } else {
453                 formattedTime += to_string(DTNow.GetHour());
454         }
455         
456         formattedTime += ":";
457         
458         if (DTNow.GetMinute() < 10){
459                 formattedTime += "0";
460                 formattedTime += to_string(DTNow.GetMinute());
461         } else {
462                 formattedTime += to_string(DTNow.GetMinute());
463         }
464         
465         txtEndTime->SetValue((wxString)formattedTime);
466         
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