Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmEventEditor: Added form for editing events
[xestiacalendar/.git] / source / forms / eventeditor / frmEventEditor.cpp
1 #include "frmEventEditor.h"
3 using namespace std;
5 frmEventEditor::frmEventEditor( wxWindow* parent )
6 :
7 frmEventEditorADT( parent )
8 {
10 }
12 void frmEventEditor::SetupForm(CalendarDataStorage *dataStorage, XCALPreferences *preferences){
13         
14         this->preferences = preferences;
15         this->dataStorage = dataStorage;
16         
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.
19         
20         CDSAccountList accountList = dataStorage->GetAccountList();
21         
22         for (std::vector<CDSGetAccountInfo>::iterator accountSeek = accountList.accountList.begin();
23                 accountSeek != accountList.accountList.end(); accountSeek++){
24                 
25                 CDSCalendarList calendarList = dataStorage->GetCalendarList((*accountSeek).accountID);
26         
27                 for (std::vector<int>::iterator calendarSeek = calendarList.calendarList.begin();
28                         calendarSeek != calendarList.calendarList.end(); calendarSeek++){
29         
30                         string combinedName = "";
31                         
32                         combinedName = (*accountSeek).accountName;
33                         combinedName += " : ";
34                                 
35                         CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar((*calendarSeek));
36                                 
37                         combinedName += calendarInfo.calendarName;
38                         cmbCalendar->Append((wxString)combinedName);
39                         calendarIDList.push_back(calendarInfo.calendarID);
40                                 
41                 }
42                 
43         }
44         
45         // Check the edit mode and get the event data.
46         
47         if (editMode == true){
48                 
49                 CDSGetCalendarEntryInfo eventInfo = dataStorage->GetEvent(eventID);
50                 
51                 // Load the calendar info.
52                 
53                 cmbCalendar->Show(false);
54                 lblCalendar->Show(false);
55                 
56                 szrDetails->Layout();
57                 szrList->Layout();              
58                 
59                 // Load the data into the form.
60                 
61                 txtEventName->SetValue(wxString(eventInfo.entryName));
62                 txtEventDescription->SetValue(wxString(eventInfo.entryDescription));
63                 
64                 // Load the start and end dates.
65                 
66                 wxDateTime startDate;
67                 startDate.Set(eventInfo.entryStartDay, (wxDateTime::Month)(eventInfo.entryStartMonth -1), eventInfo.entryStartYear, 0, 0, 0);
68                 dapStartDate->SetValue(startDate);
69                 
70                 wxDateTime endDate;
71                 endDate.Set(eventInfo.entryEndDay, (wxDateTime::Month)(eventInfo.entryEndMonth -1), eventInfo.entryEndYear, 0, 0, 0);
72                 dapEndDate->SetValue(endDate);
73                 
74                 // Load the start and end times.
75                 
76                 string startTime;
77                 string endTime;
78                 
79                 stringstream stringData;
80                 
81                 stringData << setw(2) << setfill('0') << eventInfo.entryStartHour;
82                 stringData << ":";
83                 stringData << setw(2) << setfill('0') << eventInfo.entryStartMinute;
84                 
85                 txtStartTime->SetValue((wxString)stringData.str());
86                 stringData.str("");
87                 
88                 stringData << setw(2) << setfill('0') << eventInfo.entryEndHour;
89                 stringData << ":";
90                 stringData << setw(2) << setfill('0') << eventInfo.entryEndMinute;              
91                 
92                 txtEndTime->SetValue((wxString)stringData.str());
93                 
94                 // Set the filename.
95                 
96                 eventFilePath = eventInfo.entryFilename;
97                 
98                 // Load the required data.
99                 
100                 eventData.LoadFile(eventFilePath);
101                 
102                 // TODO: Set the duration data.
103                 
104                 return;
105         }
106         
107         // Setup the date and time with some default values (today's date and time).
108         
109         SetDefaultDateTime();
113 void frmEventEditor::SetEditMode(bool editMode){
114         
115         this->editMode = editMode;
116         
119 void frmEventEditor::SaveContact(wxCommandEvent &event){
120         
121         SaveContact();
122         
125 void frmEventEditor::SaveNewContact(wxCommandEvent &event){
126         
127         SaveContact();
128         
129         // Reset the form for a new entry.
130         
131         cmbCalendar->SetSelection(-1);
133         editMode = false;
134         eventData.Clear();
135                 
136         SetDefaultDateTime();
137         
138         txtEventName->Clear();
139         txtEventDescription->Clear();
140         
141         cmbCalendar->Show(true);
142         lblCalendar->Show(true);
143         szrDetails->Layout();
144         szrList->Layout();
145         
147         
150 void frmEventEditor::SaveContact(){
152         // Verify that a calendar has been selected.
153         
154         if (cmbCalendar->GetSelection() == -1 && editMode == false){
155                 
156                 wxMessageBox("Please select a calendar for this entry.", "No calendar selected", wxOK);
157                 return;
158                 
159         }
160         
161         // Do verification of entry name (shouldn't be blank).
162         
163         if (txtEventName->GetValue().IsEmpty()){
164                 
165                 wxMessageBox("The event name cannot be left empty.", "Event name is empty", wxOK);
166                 return; 
167                 
168         }
169         
170         // Do verification of start time.
171         
172         bool timeValid = false;
173         
174         // TODO: Do verification of end time.
175         
176         timeValid = false;
177         
178         // TODO: Do verification of duration.
179         
180         
181         
182         // TODO: Do verification that end date is later than
183         // the start date.
184         
185         
186         
187         bool durationValid = false;
188         
189         // Set the data into the calendar event object.
190                 
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("");
196                 
197         stringstream stringData;
198                 
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();
202         stringData << "T";
203         stringData << txtStartTime->GetValue().ToStdString().substr(0, 2).c_str();
204         stringData << txtStartTime->GetValue().ToStdString().substr(3, 2).c_str();
205         stringData << "00Z";
207         eventData.DateTimeStartData = stringData.str();
208         eventData.DateTimeStampData = stringData.str();
209                 
210         stringData.str("");
211                 
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();
215         stringData << "T";
216         stringData << txtEndTime->GetValue().ToStdString().substr(0, 2).c_str();
217         stringData << txtEndTime->GetValue().ToStdString().substr(3, 2).c_str();
218         stringData << "00Z";    
220         eventData.DateTimeEndData = stringData.str();
221         
222         // TODO: Implement Duration.
223         
224         if (editMode == false){
225                 
226                 // This is a new event so create a new UUID.
227                 
228                 string NewUUID = GenerateUUID();
229                 
230                 // Setup the calendar directory path.
231                 
232                 CDSGetCalendarInfo calendarInfo = dataStorage->GetCalendar(calendarIDList[cmbCalendar->GetSelection()]);
233                 CDSGetAccountInfo accountInfo = dataStorage->GetAccount(calendarInfo.accountName);
234                 
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;
247                 eventFile += ".ics";
249                 eventData.UniqueID = NewUUID;
251                 // Write the file.
253                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFile);
254                 
255                 // Add the event to the calendar data storage.
256                 
257                 CDSAddEntryResult addEventResult = dataStorage->AddEvent(calendarIDList[cmbCalendar->GetSelection()], eventFile);
258                 
259                 // Post an event to show the new entry in
260                 // the main window.
261                 
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;
272                 
273                 wxCommandEvent addEvent(XCMAIN_ADDEVENT);
274                 addEvent.SetClientData(eventInfo);
275                 addEvent.SetId(ID_ADDENTRY);
276                 wxPostEvent(this->GetParent(), addEvent);
277                 
278                 eventFilePath = eventFile;
279                 editMode = true;
280                 
281                 eventID = addEventResult.calendarEntryID;
282                 
283                 // Hide the calendar selection controls.
284                 
285                 cmbCalendar->Show(false);
286                 lblCalendar->Show(false);
287                 
288                 szrDetails->Layout();
289                 szrList->Layout();
290                 
291         } else {
292                 
293                 // Setup the calendar directory path.
295                 CDSGetCalendarEntryInfo eventDataInfo = dataStorage->GetEvent(eventID);
296                 
297                 // Write the file.
298                 
299                 CalendarObjectSaveResult saveResult = eventData.SaveFile(eventFilePath);
300                 
301                 // Update the data in the calendar data storage.
302                 
303                 CDSEditEntryResult editEventResult = dataStorage->UpdateEvent(eventID, eventFilePath);
304                 
305                 // Post an event to update the new entry in
306                 // the main window.
307                 
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;
318                 
319                 wxCommandEvent updateEvent(XCMAIN_UPDATEEVENT);
320                 updateEvent.SetClientData(eventInfo);
321                 updateEvent.SetId(ID_UPDATEENTRY);
322                 wxPostEvent(this->GetParent(), updateEvent);
323                 
324         }
325         
328 void frmEventEditor::CloseWindow(wxCommandEvent &event){
329         
330         this->Close();
331         
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.
341     
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.
345     
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
351         
352                 static int s_eventType = 0;
353                 static wxWindowID s_id = 0;
354         
355                 if (s_id != event.GetId() && s_eventType != event.GetEventType()){
356                         
357                         s_eventType = event.GetEventType();
358                         s_id = event.GetId();
359             
360                         wxWindow* focusWin = wxFindFocusDescendant(this);
361                         if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event)){
362                                 //s_command = NULL;
363                                 s_eventType = 0;
364                                 s_id = 0;
365                                 return true;
366                         }
367                         s_eventType = 0;
368                         s_id = 0;
369                         
370                 } else {
371                 
372                         return false;
373                         
374                 }
375         }
376     
377         return wxFrame::ProcessEvent(event);
378         
381 void frmEventEditor::CutText(wxCommandEvent &event){
382         
385 void frmEventEditor::CopyText(wxCommandEvent &event){
386         
389 void frmEventEditor::PasteText(wxCommandEvent &event){
390         
393 void frmEventEditor::SetEventID(int eventID){
394         
395         this->eventID = eventID;
396         
399 void frmEventEditor::SetDefaultDateTime(){
400         
401         wxDateTime DTNow = wxDateTime::Now();
402         
403         wxTimeSpan oneHour;
404         
405         string formattedTime = "";
406         
407         dapStartDate->SetValue(DTNow);
408         
409         if (DTNow.GetHour() < 10){
410                 formattedTime += "0";
411                 formattedTime += to_string(DTNow.GetHour());
412         } else {
413                 formattedTime += to_string(DTNow.GetHour());
414         }
415         
416         formattedTime += ":";
417         
418         if (DTNow.GetMinute() < 10){
419                 formattedTime += "0";
420                 formattedTime += to_string(DTNow.GetMinute());          
421         } else {
422                 formattedTime += to_string(DTNow.GetMinute());
423         }
424         
425         txtStartTime->SetValue((wxString)formattedTime);
426         
427         dapEndDate->SetValue(DTNow.Add(oneHour));
429         formattedTime.clear();
430         
431         if (DTNow.Add(oneHour.Hour()).GetHour() < 10){
432                 formattedTime += "0";
433                 formattedTime += to_string(DTNow.GetHour());
434         } else {
435                 formattedTime += to_string(DTNow.GetHour());
436         }
437         
438         formattedTime += ":";
439         
440         if (DTNow.GetMinute() < 10){
441                 formattedTime += "0";
442                 formattedTime += to_string(DTNow.GetMinute());
443         } else {
444                 formattedTime += to_string(DTNow.GetMinute());
445         }
446         
447         txtEndTime->SetValue((wxString)formattedTime);
448         
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