Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
forms: Forms updated
[xestiacalendar/.git] / source / forms / main / frmMain.cpp
1 #include "frmMain.h"
3 frmMain::frmMain( wxWindow* parent )
4 :
5 frmMainADT( parent )
6 {
7         
8         // Setup the default settings if they don't
9         // exist.
10         
11         // Setup the preferences.
12         wxString prefDirectory = GetUserPrefDir();
13         preferences = new XCALPreferences(prefDirectory);
14         
15         // Load the settings.
16         
17         wxString fullPrefPath;
18         
19         fullPrefPath.Append(prefDirectory);
20         fullPrefPath.Append(wxT("settings"));
22         wxFileConfig *settingfile = new wxFileConfig("", "", fullPrefPath);
23     
24         wxString ValueInc;
25         settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
26     
27         if (ValueInc == wxT("true")){
28                 
29                 wxRect windowPosition;
30         
31                 long posX, posY, posH, posW = 0;
33                 bool posXValid, posYValid, posHValid, posWValid = false;
35                 posXValid = settingfile->Read(wxT("WindowPositionX"), &posX);
36                 posYValid = settingfile->Read(wxT("WindowPositionY"), &posY);
37                 posHValid = settingfile->Read(wxT("WindowPositionHeight"), &posH);
38                 posWValid = settingfile->Read(wxT("WindowPositionWidth"), &posW);
40                 if (posXValid == true && posYValid == true && posHValid == true && posWValid == true){
42                         this->SetSize(posX, posY, posH, posW);
44                 } else {
46                         this->SetSize(-1, -1, 800, 600);
47                         
48                 }
49         
50         }
52         // Load the account data.
53         
54         LoadAccountData();
55         
56         // Setup the form control.
57         
58         mainCalendarCtrl = new XCCalendarCtrl(this, &calendarData);
59         szrMain->Add(mainCalendarCtrl, 1, wxEXPAND, 5);
60         szrMain->Layout();
61         
62         Connect(ID_PROCESSCALENDAR, XCMAIN_PROCESSCALENDAR, wxCommandEventHandler(frmMain::ProcessCalendar));
63         Connect(ID_EDITCALENDAR, XCMAIN_EDITCALENDAR, wxCommandEventHandler(frmMain::EditCalendar));
64         Connect(ID_DELETECALENDAR, XCMAIN_DELETECALENDAR, wxCommandEventHandler(frmMain::DeleteCalendar));
65         Connect(ID_DELETEEVENT, XCMAIN_DELETEEVENT, wxCommandEventHandler(frmMain::DeleteEvent));
66         Connect(ID_ADDENTRY, XCMAIN_ADDEVENT, wxCommandEventHandler(frmMain::AddEvent));
67         Connect(ID_UPDATEENTRY, XCMAIN_UPDATEEVENT, wxCommandEventHandler(frmMain::UpdateEvent));
68         Connect(ID_EDITEVENT, XCMAIN_EDITEVENT, wxCommandEventHandler(frmMain::EditEvent));
69         
70 }
72 void frmMain::QuitApp( wxCloseEvent& event )
73 {
75         // Run the QuitApp function.
77         QuitApp();
79 }
81 void frmMain::QuitApp( wxCommandEvent& event )
82 {
83     
84         // Run the QuitApp function.
85     
86         QuitApp();
87     
88 }
90 void frmMain::QuitApp()
91 {
93         // Function to run when quitting.
95         //Go through the windows and close each one (be it search
96         //or contact editor). Abort if user wants to cancel.
98         // Close the contact editor windows.
100         // Close the contact windows.
102         // Close the search windows.
104         // Write out the ETag databases.
106         // Save Preferences: Save the window position if that option is enabled.
108         wxString SetFilename = GetUserPrefDir();
110 #if defined(__HAIKU__)
114 #elif defined(__WIN32__)
116         SetFilename.Append(wxT("settings"));
118 #else
120         // *nix OSes
122         SetFilename.Append(wxT("settings"));
124 #endif
126         wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
128         bool SaveWindowPos = false;
129         wxString SaveWindowInc;
130         cfgfile->Read(wxT("SaveWindowPosition"), &SaveWindowInc);
132         if (SaveWindowInc == wxT("true")){
133         
134                 SaveWindowPos = true;
135         
136         }
138         if (SaveWindowPos == true){
139         
140                 wxRect frmMainPos = this->GetRect();
141         
142                 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
143                 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
144                 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
145                 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
146         
147         }
149         delete cfgfile;
150         cfgfile = nullptr;
152         //Everything closed... exit.
154         std::exit(0);
156         Close();
160 void frmMain::LoadAccountData(){
161         
162         // Get the list of accounts and put into the calendar data storage.
163         
164         int accountCount = preferences->accounts.GetCount();
165         
166         for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
167                 
168                 CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), accountSeek);
169                 
170         }
171         
172         // Get the list of calendars and put them into the calendar data storage.
173         
174         for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
175         
176                 CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()));
177         
178                 // Build the path.
179                 
180                 string calendarListFilename = string(GetUserDir().mb_str());
181                 calendarListFilename += "accounts/";
182                 calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).mb_str());
183                 calendarListFilename += ".";
184                 calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).mb_str());
185                 
186                 // Get the list of calendars.
187                 
188                 XCAccountCalendarList calendarList(calendarListFilename);
189                 
190                 // Add the calendar and set the calendar ID for it.
191                 
192                 for (int calendarSeek = 0; calendarSeek < calendarList.calendarShortName.size(); calendarSeek++){
193                         
194                         // Add the calendar.
195                         
196                         CDSCalendarResult calendarAddResult = calendarData.AddCalendar(accountInfo.accountID, 
197                                 calendarList.calendarName[calendarSeek], 
198                                 calendarList.calendarShortName[calendarSeek],
199                                 calendarList.calendarColour[calendarSeek],
200                                 calendarList.calendarDescription[calendarSeek]);
201                         
202                         // Set the calendar ID.
203                         
204                         CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), calendarList.calendarShortName[calendarSeek]);
205                         calendarList.calendarStorageID[calendarSeek] = calendarInfo.calendarID;
206                         
207                         // Find the entries and load each entry.
208                         
209                         string calendarListDirectory = calendarListFilename;
210                         calendarListDirectory += "/";
211                         calendarListDirectory += calendarList.calendarShortName[calendarSeek];
212                         calendarListDirectory += "/";
213                         
214                         wxDir entryListDirectory(calendarListDirectory);
215                         wxString entryListFilename;
216                         
217                         bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*.ics", wxDIR_NO_FOLLOW|wxDIR_FILES);
218                         
219                         while (continueProcessing){
220                                 
221                                 string entryListFullFilename;
222                                 entryListFullFilename += calendarListDirectory;
223                                 entryListFullFilename += string(entryListFilename.mb_str());
224                                 
225                                 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
226                                 CDSAddEntryResult addEventResult = calendarData.AddEvent(calendarInfo.calendarID, entryListFullFilename);
227                                 
228                         }                       
229                         
230                 }
231                 
232         }
233         
236 void frmMain::ShowPreferencesWindow( wxCommandEvent& event )
239         // Open the preferences window.
240     
241         reloadAccounts = FALSE;
242     
243         frmPreferences *framePreferences = new frmPreferences ( this );
244         framePreferences->SetupPointers(&reloadAccounts);
245         framePreferences->ShowModal();
246         delete framePreferences;
247         framePreferences = NULL;
248     
249         // Reload the preferences.
250         
251         if (reloadAccounts == true){
252             
253                 // Reload the accounts as a change has been made within
254                 // the application.
255         
256                 wxString prefDirectory = GetUserPrefDir();
257                 XCALPreferences *oldPreferences = preferences;
258                 preferences = new XCALPreferences(prefDirectory);
259                 
260                 delete oldPreferences;
261                 oldPreferences = nullptr;
262                 
263                 // Clear all of the data from the calendar data storage.
264                 
265                 calendarData.Clear();
266                 LoadAccountData();
267                 
268                 // Politely ask the calendar control to reload it's view.
270                 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
271                 updateGrid.SetId(ID_CHANGEGRID);
272                 wxPostEvent(mainCalendarCtrl, updateGrid);
273                 
274         }
275     
278 void frmMain::ShowAboutWindow( wxCommandEvent& event )
281         // Show the about window.
282     
283         frmAbout *frameAbout = new frmAbout ( this );
284         frameAbout->SetupAboutWindow();
285         frameAbout->ShowModal();
286         delete frameAbout;
287         frameAbout = NULL;
288     
291 void frmMain::ShowUpdateWindow( wxCommandEvent& event )
294         frmUpdate *frameUpdate = new frmUpdate ( this );
295         frameUpdate->FetchData();
296         frameUpdate->ShowModal();
297         delete frameUpdate;
298         frameUpdate = NULL;
299         
302 void frmMain::OpenNewAccountDialog( wxCommandEvent& event )
304         
305         // Open the new account dialog.
306     
307         reloadAccounts = false;
308     
309         frmNewAccount *frameNewAccount = new frmNewAccount ( this );
310         frameNewAccount->SetupPointers(&reloadAccounts, &calendarData);
311         frameNewAccount->ShowModal();
312         delete frameNewAccount;
313         frameNewAccount = NULL;
314         
315         // Reload the preferences.
316         
317         if (reloadAccounts == true){
318             
319                 // Reload the accounts as a change has been made within
320                 // the application.
321         
322                 wxString prefDirectory = GetUserPrefDir();
323                 XCALPreferences *oldPreferences = preferences;
324                 preferences = new XCALPreferences(prefDirectory);
325                 
326                 delete oldPreferences;
327                 oldPreferences = nullptr;
328                 
329                 // Clear all of the data from the calendar data storage.
330                 
331                 calendarData.Clear();
332                 LoadAccountData();
333                 
334                 // Politely ask the calendar control to reload it's view.
336                 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
337                 updateGrid.SetId(ID_CHANGEGRID);
338                 wxPostEvent(mainCalendarCtrl, updateGrid);
339                 
340         }
341         
344 void frmMain::CreateNewCalendar( wxCommandEvent& event )
346         
347         frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
348         frameNewCalendar->SetMode(false);
349         frameNewCalendar->SetupAccounts(preferences);
350         frameNewCalendar->ShowModal();
351         delete frameNewCalendar;
352         frameNewCalendar = nullptr;
353         
356 void frmMain::EditCalendar( wxCommandEvent& event )
358         
359         // Get the calendar data.
360         
361         CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(event.GetInt());
362         
363         frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
364         frameNewCalendar->SetMode(true);
365         frameNewCalendar->SetupAccounts(preferences);
366         frameNewCalendar->SetData(event.GetInt(), calendarInfo.accountName, calendarInfo.calendarName, calendarInfo.calendarDescription, calendarInfo.calendarColour);
367         frameNewCalendar->ShowModal();
368         delete frameNewCalendar;
369         frameNewCalendar = nullptr;
370         
373 void frmMain::DeleteCalendar( wxCommandEvent& event )
376         CalendarProperties *calendarEventInfo = (CalendarProperties*)event.GetClientData();
377         
378         // Get the calendar data.
379         
380         CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(calendarEventInfo->calendarID);
381         
382         if (wxMessageBox(wxString::Format("Are you sure you want to delete the calendar %s from the %s account?", calendarInfo.calendarName, calendarInfo.accountName), "Delete calendar", wxYES_NO|wxICON_QUESTION) == wxNO){
383                 return;
384         }
385         
386         // Go through the grid and delete each calendar entry widget that 
387         // is associated with the calendar.
388         
389         wxCommandEvent deleteCalendar(XCCALENDARCTRL_DELETECALENDARENTRIES);
390         deleteCalendar.SetId(ID_DELETECALENDARENTRIES);
391         deleteCalendar.SetInt(calendarInfo.calendarID);
392         wxPostEvent(mainCalendarCtrl, deleteCalendar);
393         
394         // Get the account configuration file and delete the calendar information.
395         
396         CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo.accountName);
397         
398         string accountDirectoryPath = string(GetUserDir().mb_str());    
399         accountDirectoryPath += "accounts/";
400         accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarEventInfo->accountPreferencesID).mb_str());
401         accountDirectoryPath += ".";
402         accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarEventInfo->accountPreferencesID).mb_str());
403         accountDirectoryPath += "/";
404         
405         string calendarListFilenameFull = accountDirectoryPath;
406         calendarListFilenameFull += "calendarlist.db";
407         
408         string calendarDirectoryPath = accountDirectoryPath;
409         calendarDirectoryPath += calendarInfo.calendarTextID;
410         
411         wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
412         
413         //calendarListFile->SetPath(wxString(calendarInfo.calendarTextID));
414         calendarListFile->DeleteGroup(wxString(calendarInfo.calendarTextID));
415         
416         // Delete the calendar directory.
418         wxDir entryListDirectory((wxString)calendarDirectoryPath.c_str());
419         wxString entryListFilename;
420         
421         bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*", wxDIR_NO_FOLLOW|wxDIR_FILES);
422                         
423         while (continueProcessing){
424                                 
425                 string entryListFullFilename;
426                 entryListFullFilename += calendarDirectoryPath;
427                 entryListFullFilename += "/";
428                 entryListFullFilename += string(entryListFilename.mb_str());
429                 
430                 wxRemoveFile(wxString(entryListFullFilename.c_str()));
431                 
432                 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
433                                 
434         }
436         wxRmdir(calendarDirectoryPath);
437         
438         // Delete the calendar from the calendar data storage.
439         
440         calendarData.DeleteCalendar(calendarEventInfo->calendarID);
441         
442         delete calendarListFile;
443         calendarListFile = nullptr;
444         
445         delete calendarEventInfo;
446         calendarEventInfo = nullptr;
447         
450 void frmMain::CreateNewEvent( wxCommandEvent& event ){
451         
452         frmEventEditor *frameNewEvent = new frmEventEditor ( this );
453         frameNewEvent->SetupForm(&calendarData, preferences);
454         frameNewEvent->Show();
455         
458 void frmMain::EditEvent( wxCommandEvent& event ){
459         
460         frmEventEditor *frameEditEvent = new frmEventEditor ( this );
461         frameEditEvent->SetEventID(event.GetInt());
462         frameEditEvent->SetEditMode(true);
463         frameEditEvent->SetupForm(&calendarData, preferences);
464         frameEditEvent->Show();
465         
468 void frmMain::DeleteEvent( wxCommandEvent& event ){
469         
470         EventProperties *eventInfo = (EventProperties*)event.GetClientData();
471         
472         // Get the event information.
473         
474         CDSGetCalendarEntryInfo eventDeleteData = calendarData.GetEvent(eventInfo->eventID);
475         CDSGetCalendarInfo calendarDeleteData = calendarData.GetCalendar(eventInfo->calendarID);
476         
477         if (wxMessageBox(wxString::Format("Are you sure you want to delete the event %s from the %s calendar?", eventDeleteData.entryName, calendarDeleteData.calendarName), "Delete event", wxYES_NO|wxICON_QUESTION) == wxNO){
478                 return;
479         }
480         
481         // Go through the grid and delete the entry from the GUI.
482         
483         wxCommandEvent deleteEvent(XCCALENDARCTRL_DELETEENTRY);
484         deleteEvent.SetId(ID_DELETEENTRY);
485         deleteEvent.SetInt(eventInfo->eventID);
486         wxPostEvent(mainCalendarCtrl, deleteEvent);
487         
488         // Get the filename and delete the entry.
489         
490         wxRemoveFile(wxString(eventDeleteData.entryFilename.c_str()));
491         
492         // Delete the entry from the calendar data storage.
493         
494         calendarData.DeleteEvent(eventInfo->eventID);
495         
496         delete eventInfo;
497         eventInfo = nullptr;
498         
501 void frmMain::AddEvent( wxCommandEvent& event )
503         
504         EventProperties *eventInfo = (EventProperties*)event.GetClientData();
505         
506         wxCommandEvent addEvent(XCCALENDARCTRL_ADDENTRY);
507         addEvent.SetId(ID_ADDENTRY);
508         addEvent.SetClientData(eventInfo);
509         wxPostEvent(mainCalendarCtrl, addEvent);
510         
513 void frmMain::UpdateEvent( wxCommandEvent& event )
515         
516         EventProperties *eventInfo = (EventProperties*)event.GetClientData();
517         
518         wxCommandEvent updateEvent(XCCALENDARCTRL_UPDATEENTRY);
519         updateEvent.SetId(ID_UPDATEENTRY);
520         updateEvent.SetClientData(eventInfo);
521         wxPostEvent(mainCalendarCtrl, updateEvent);
522         
525 void frmMain::ProcessCalendar( wxCommandEvent& event )
527         
528         CalendarProperties *calendarInfo = (CalendarProperties*)event.GetClientData();
529         
530         // Get the account name.
531         
532         CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo->accountName);
533         
534         // Build the account directory path.
535         
536         string accountDirectoryPath = string(GetUserDir().mb_str());
537         accountDirectoryPath += "accounts/";
538         accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarInfo->accountPreferencesID).mb_str());
539         accountDirectoryPath += ".";
540         accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarInfo->accountPreferencesID).mb_str());
541         accountDirectoryPath += "/";
542         
543         // Generate a UUID for the new calendar.
544         
545         string calendarPath = accountDirectoryPath;
547         string calendarUUID = "";
549         if (calendarInfo->editMode == false){
551                 calendarUUID = GenerateUUID();
552                 calendarPath += calendarUUID;
553                 
554         } else {
555         
556                 calendarUUID = calendarData.GetCalendar(calendarInfo->calendarID).calendarTextID;
557                 calendarPath += calendarPath;
558                 
559         }
560         
561         calendarPath += "/";
562         
563         // Open the account's calendar file and add to the list of calendars.
564         
565         string calendarListFilenameFull = accountDirectoryPath;
566         calendarListFilenameFull += "calendarlist.db";
567         
568         wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
569         
570         wxString calendarDescription = wxString(calendarInfo->calendarDescription);
571         
572         calendarDescription.Replace("\n", "\\n", true);
573         
574         // Create the directory for the calendar entries if 
575         // not editing a calendar.
576         
577         calendarListFile->SetPath(wxString(calendarUUID));
578         calendarListFile->Write(wxT("name"), wxString(calendarInfo->calendarName));
579         calendarListFile->Write(wxT("description"), calendarDescription);
580         calendarListFile->Write(wxT("colour"), wxString(calendarInfo->calendarColour));
581         
582         if (calendarInfo->editMode == false){
583         
584                 wxMkDir(wxString(calendarPath), wxS_DIR_DEFAULT);
585                 calendarData.AddCalendar(accountInfo.accountID, calendarInfo->calendarName, calendarUUID, calendarInfo->calendarColour, calendarInfo->calendarDescription);
586                 
587         } else {
588                 
589                 calendarData.UpdateCalendar(calendarInfo->calendarID, calendarInfo->calendarName, calendarInfo->calendarColour, calendarInfo->calendarDescription);
590                 
591                 // Post event to update the colour in the calendar entries.
592                 
593                 updateColourData.calendarID = calendarInfo->calendarID;
594                 updateColourData.newColour.red = calendarInfo->calendarColour.red;
595                 updateColourData.newColour.green = calendarInfo->calendarColour.green;
596                 updateColourData.newColour.blue = calendarInfo->calendarColour.blue;
597                 updateColourData.newColour.alpha = calendarInfo->calendarColour.alpha;
598                 
599                 wxCommandEvent updateColour(XCCALENDARCTRL_UPDATECALENDARCOLOUR);
600                 updateColour.SetClientData(&updateColourData);
601                 updateColour.SetId(ID_UPDATECOLOUR);
602                 wxPostEvent(mainCalendarCtrl, updateColour);
603                 
604         }
605         
606         // Add the calendar to the calendar data storage.
607         
608         delete calendarListFile;
609         calendarListFile = nullptr;
610         
611         delete calendarInfo;
612         calendarInfo = nullptr;
613         
616 void frmMain::ShowHelp( wxCommandEvent& event )
618         
619         wxMessageBox(_("The help documentation will be implemented in a future version of Xestia Calendar."), _("Unimplemented"));
620         
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