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