1 // frmMain.h - frmMain form functions
3 // (c) 2016-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar.
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.
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.
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/>
21 wxDEFINE_EVENT(XCMAIN_PROCESSCALENDAR, wxCommandEvent);
22 wxDEFINE_EVENT(XCMAIN_EDITCALENDAR, wxCommandEvent);
23 wxDEFINE_EVENT(XCMAIN_DELETECALENDAR, wxCommandEvent);
24 wxDEFINE_EVENT(XCMAIN_ADDEVENT, wxCommandEvent);
25 wxDEFINE_EVENT(XCMAIN_UPDATEEVENT, wxCommandEvent);
26 wxDEFINE_EVENT(XCMAIN_EDITEVENT, wxCommandEvent);
27 wxDEFINE_EVENT(XCMAIN_DELETEEVENT, wxCommandEvent);
29 wxDEFINE_EVENT(XCMAIN_ADDWINDOWINFO, wxCommandEvent);
30 wxDEFINE_EVENT(XCMAIN_UPDATEWINDOWINFO, wxCommandEvent);
31 wxDEFINE_EVENT(XCMAIN_DELETEWINDOWINFO, wxCommandEvent);
33 frmMain::frmMain( wxWindow* parent )
37 // Setup the default settings if they don't
40 // Setup the preferences.
41 wxString prefDirectory = GetUserPrefDir();
42 preferences = new XCALPreferences(prefDirectory);
46 wxString fullPrefPath;
48 fullPrefPath.Append(prefDirectory);
49 fullPrefPath.Append(wxT("settings"));
51 wxFileConfig *settingfile = new wxFileConfig("", "", fullPrefPath);
54 settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
56 if (ValueInc == wxT("true")){
58 wxRect windowPosition;
60 long posX, posY, posH, posW = 0;
62 bool posXValid, posYValid, posHValid, posWValid = false;
64 posXValid = settingfile->Read(wxT("WindowPositionX"), &posX);
65 posYValid = settingfile->Read(wxT("WindowPositionY"), &posY);
66 posHValid = settingfile->Read(wxT("WindowPositionHeight"), &posH);
67 posWValid = settingfile->Read(wxT("WindowPositionWidth"), &posW);
69 if (posXValid == true && posYValid == true && posHValid == true && posWValid == true){
71 this->SetSize(posX, posY, posH, posW);
75 this->SetSize(-1, -1, 800, 600);
81 // Load the account data.
85 // Setup the form control.
87 mainCalendarCtrl = new XCCalendarCtrl(this, &calendarData);
88 szrMain->Add(mainCalendarCtrl, 1, wxEXPAND, 5);
91 // Bind events to the control.
93 Bind(XCMAIN_PROCESSCALENDAR, &frmMain::ProcessCalendar, this, ID_PROCESSCALENDAR);
94 Bind(XCMAIN_EDITCALENDAR, &frmMain::EditCalendar, this, ID_EDITCALENDAR);
95 Bind(XCMAIN_DELETECALENDAR, &frmMain::DeleteCalendar, this, ID_DELETECALENDAR);
96 Bind(XCMAIN_ADDEVENT, &frmMain::AddEvent, this, ID_ADDENTRY);
97 Bind(XCMAIN_UPDATEEVENT, &frmMain::UpdateEvent, this, ID_UPDATEENTRY);
98 Bind(XCMAIN_EDITEVENT, &frmMain::EditEvent, this, ID_EDITEVENT);
99 Bind(XCMAIN_DELETEEVENT, &frmMain::DeleteEvent, this, ID_DELETEEVENT);
101 Bind(XCMAIN_ADDWINDOWINFO, &frmMain::WindowAdd, this, ID_ADDWINDOW);
102 Bind(XCMAIN_UPDATEWINDOWINFO, &frmMain::WindowUpdate, this, ID_UPDATEWINDOW);
103 Bind(XCMAIN_DELETEWINDOWINFO, &frmMain::WindowDelete, this, ID_DELETEWINDOW);
106 void frmMain::QuitApp( wxCloseEvent& event )
109 // Run the QuitApp function.
115 void frmMain::QuitApp( wxCommandEvent& event )
118 // Run the QuitApp function.
124 void frmMain::QuitApp()
127 // Function to run when quitting.
129 //Go through the windows and close each one (be it search
130 //or contact editor). Abort if user wants to cancel.
132 // Close the contact editor windows.
134 // Close the contact windows.
136 // Close the search windows.
138 // Write out the ETag databases.
140 // Save Preferences: Save the window position if that option is enabled.
142 wxString SetFilename = GetUserPrefDir();
144 #if defined(__HAIKU__)
148 #elif defined(__WIN32__)
150 SetFilename.Append(wxT("settings"));
156 SetFilename.Append(wxT("settings"));
160 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
162 bool SaveWindowPos = false;
163 wxString SaveWindowInc;
164 cfgfile->Read(wxT("SaveWindowPosition"), &SaveWindowInc);
166 if (SaveWindowInc == wxT("true")){
168 SaveWindowPos = true;
172 if (SaveWindowPos == true){
174 wxRect frmMainPos = this->GetRect();
176 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
177 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
178 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
179 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
186 //Everything closed... exit.
194 void frmMain::LoadAccountData(){
196 // Get the list of accounts and put into the calendar data storage.
198 int accountCount = preferences->accounts.GetCount();
200 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
202 CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), accountSeek);
206 // Get the list of calendars and put them into the calendar data storage.
208 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
210 CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()));
214 string calendarListFilename = string(GetUserDir().mb_str());
215 calendarListFilename += "accounts/";
216 calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).mb_str());
217 calendarListFilename += ".";
218 calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).mb_str());
220 // Get the list of calendars.
222 XCAccountCalendarList calendarList(calendarListFilename);
224 // Add the calendar and set the calendar ID for it.
226 for (int calendarSeek = 0; calendarSeek < calendarList.calendarShortName.size(); calendarSeek++){
230 CDSCalendarResult calendarAddResult = calendarData.AddCalendar(accountInfo.accountID,
231 calendarList.calendarName[calendarSeek],
232 calendarList.calendarShortName[calendarSeek],
233 calendarList.calendarColour[calendarSeek],
234 calendarList.calendarDescription[calendarSeek]);
236 // Set the calendar ID.
238 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), calendarList.calendarShortName[calendarSeek]);
239 calendarList.calendarStorageID[calendarSeek] = calendarInfo.calendarID;
241 // Find the entries and load each entry.
243 string calendarListDirectory = calendarListFilename;
244 calendarListDirectory += "/";
245 calendarListDirectory += calendarList.calendarShortName[calendarSeek];
246 calendarListDirectory += "/";
248 wxDir entryListDirectory(calendarListDirectory);
249 wxString entryListFilename;
251 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*.ics", wxDIR_NO_FOLLOW|wxDIR_FILES);
253 while (continueProcessing){
255 string entryListFullFilename;
256 entryListFullFilename += calendarListDirectory;
257 entryListFullFilename += string(entryListFilename.mb_str());
259 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
260 CDSAddEntryResult addEventResult = calendarData.AddEvent(calendarInfo.calendarID, entryListFullFilename);
270 void frmMain::ShowPreferencesWindow( wxCommandEvent& event )
273 // Close all windows first.
275 if (CloseAllWindows() == false)
280 // Open the preferences window.
282 reloadAccounts = FALSE;
284 frmPreferences *framePreferences = new frmPreferences ( this );
285 framePreferences->SetupPointers(&reloadAccounts);
286 framePreferences->ShowModal();
287 delete framePreferences;
288 framePreferences = NULL;
290 // Reload the preferences.
292 if (reloadAccounts == true){
294 // Reload the accounts as a change has been made within
297 wxString prefDirectory = GetUserPrefDir();
298 XCALPreferences *oldPreferences = preferences;
299 preferences = new XCALPreferences(prefDirectory);
301 delete oldPreferences;
302 oldPreferences = nullptr;
304 // Clear all of the data from the calendar data storage.
306 calendarData.Clear();
309 // Politely ask the calendar control to reload it's view.
311 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
312 updateGrid.SetId(ID_CHANGEGRID);
313 wxPostEvent(mainCalendarCtrl, updateGrid);
319 void frmMain::ShowAboutWindow( wxCommandEvent& event )
322 // Show the about window.
324 frmAbout *frameAbout = new frmAbout ( this );
325 frameAbout->SetupAboutWindow();
326 frameAbout->ShowModal();
332 void frmMain::ShowUpdateWindow( wxCommandEvent& event )
335 frmUpdate *frameUpdate = new frmUpdate ( this );
336 frameUpdate->FetchData();
337 frameUpdate->ShowModal();
343 void frmMain::OpenNewAccountDialog( wxCommandEvent& event )
346 // Open the new account dialog.
348 reloadAccounts = false;
350 frmNewAccount *frameNewAccount = new frmNewAccount ( this );
351 frameNewAccount->SetupPointers(&reloadAccounts, &calendarData);
352 frameNewAccount->ShowModal();
353 delete frameNewAccount;
354 frameNewAccount = NULL;
356 // Reload the preferences.
358 if (reloadAccounts == true){
360 // Reload the accounts as a change has been made within
363 wxString prefDirectory = GetUserPrefDir();
364 XCALPreferences *oldPreferences = preferences;
365 preferences = new XCALPreferences(prefDirectory);
367 delete oldPreferences;
368 oldPreferences = nullptr;
370 // Clear all of the data from the calendar data storage.
372 calendarData.Clear();
375 // Politely ask the calendar control to reload it's view.
377 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
378 updateGrid.SetId(ID_CHANGEGRID);
379 wxPostEvent(mainCalendarCtrl, updateGrid);
385 void frmMain::CreateNewCalendar( wxCommandEvent& event )
388 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
389 frameNewCalendar->SetMode(false);
390 frameNewCalendar->SetupAccounts(preferences);
391 frameNewCalendar->ShowModal();
392 delete frameNewCalendar;
393 frameNewCalendar = nullptr;
397 void frmMain::EditCalendar( wxCommandEvent& event )
400 // Close all windows first.
402 if (CloseAllWindows() == false)
407 // Get the calendar data.
409 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(event.GetInt());
411 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
412 frameNewCalendar->SetMode(true);
413 frameNewCalendar->SetupAccounts(preferences);
414 frameNewCalendar->SetData(event.GetInt(), calendarInfo.accountName, calendarInfo.calendarName, calendarInfo.calendarDescription, calendarInfo.calendarColour);
415 frameNewCalendar->ShowModal();
416 delete frameNewCalendar;
417 frameNewCalendar = nullptr;
421 void frmMain::DeleteCalendar( wxCommandEvent& event )
424 // Close all windows first.
426 if (CloseAllWindows() == false)
431 CalendarProperties *calendarEventInfo = (CalendarProperties*)event.GetClientData();
433 // Get the calendar data.
435 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(calendarEventInfo->calendarID);
437 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){
441 // Go through the grid and delete each calendar entry widget that
442 // is associated with the calendar.
444 wxCommandEvent deleteCalendar(XCCALENDARCTRL_DELETECALENDARENTRIES);
445 deleteCalendar.SetId(ID_DELETECALENDARENTRIES);
446 deleteCalendar.SetInt(calendarInfo.calendarID);
447 wxPostEvent(mainCalendarCtrl, deleteCalendar);
449 // Get the account configuration file and delete the calendar information.
451 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo.accountName);
453 string accountDirectoryPath = string(GetUserDir().mb_str());
454 accountDirectoryPath += "accounts/";
455 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarEventInfo->accountPreferencesID).mb_str());
456 accountDirectoryPath += ".";
457 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarEventInfo->accountPreferencesID).mb_str());
458 accountDirectoryPath += "/";
460 string calendarListFilenameFull = accountDirectoryPath;
461 calendarListFilenameFull += "calendarlist.db";
463 string calendarDirectoryPath = accountDirectoryPath;
464 calendarDirectoryPath += calendarInfo.calendarTextID;
466 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
468 //calendarListFile->SetPath(wxString(calendarInfo.calendarTextID));
469 calendarListFile->DeleteGroup(wxString(calendarInfo.calendarTextID));
471 // Delete the calendar directory.
473 wxDir entryListDirectory((wxString)calendarDirectoryPath.c_str());
474 wxString entryListFilename;
476 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*", wxDIR_NO_FOLLOW|wxDIR_FILES);
478 while (continueProcessing){
480 string entryListFullFilename;
481 entryListFullFilename += calendarDirectoryPath;
482 entryListFullFilename += "/";
483 entryListFullFilename += string(entryListFilename.mb_str());
485 wxRemoveFile(wxString(entryListFullFilename.c_str()));
487 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
491 wxRmdir(calendarDirectoryPath);
493 // Delete the calendar from the calendar data storage.
495 calendarData.DeleteCalendar(calendarEventInfo->calendarID);
497 delete calendarListFile;
498 calendarListFile = nullptr;
500 delete calendarEventInfo;
501 calendarEventInfo = nullptr;
505 void frmMain::CreateNewEvent( wxCommandEvent& event ){
507 frmEventEditor *frameNewEvent = new frmEventEditor ( this );
508 frameNewEvent->SetupForm(&calendarData, preferences);
509 frameNewEvent->SetWindowMenuItemID(++WindowMenuItemID);
511 // Add the window to the window list.
513 WindowData *newWindowData = new WindowData;
515 newWindowData->DataType = 1;
516 newWindowData->WindowPointer = (void*)frameNewEvent;
517 newWindowData->WindowID = WindowMenuItemID;
519 wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
520 addevent.SetId(ID_ADDWINDOW);
521 addevent.SetClientData(newWindowData);
522 wxPostEvent(this, addevent);
524 frameNewEvent->Show();
528 void frmMain::EditEvent( wxCommandEvent& event ){
530 frmEventEditor *frameEditEvent = new frmEventEditor ( this );
531 frameEditEvent->SetEventID(event.GetInt());
532 frameEditEvent->SetEditMode(true);
533 frameEditEvent->SetWindowMenuItemID(++WindowMenuItemID);
534 frameEditEvent->SetupForm(&calendarData, preferences);
536 // Add the window to the window list.
538 WindowData *newWindowData = new WindowData;
540 newWindowData->DataType = 1;
541 newWindowData->WindowPointer = (void*)frameEditEvent;
542 newWindowData->WindowID = WindowMenuItemID;
544 wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
545 addevent.SetId(ID_ADDWINDOW);
546 addevent.SetClientData(newWindowData);
547 wxPostEvent(this, addevent);
549 // Setup the form and display it.
551 frameEditEvent->Show();
555 void frmMain::DeleteEvent( wxCommandEvent& event ){
557 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
559 // Get the event information.
561 CDSGetCalendarEntryInfo eventDeleteData = calendarData.GetEvent(eventInfo->eventID);
562 CDSGetCalendarInfo calendarDeleteData = calendarData.GetCalendar(eventInfo->calendarID);
564 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){
568 // Go through the grid and delete the entry from the GUI.
570 wxCommandEvent deleteEvent(XCCALENDARCTRL_DELETEENTRY);
571 deleteEvent.SetId(ID_DELETEENTRY);
572 deleteEvent.SetInt(eventInfo->eventID);
573 wxPostEvent(mainCalendarCtrl, deleteEvent);
575 // Get the filename and delete the entry.
577 wxRemoveFile(wxString(eventDeleteData.entryFilename.c_str()));
579 // Delete the entry from the calendar data storage.
581 calendarData.DeleteEvent(eventInfo->eventID);
588 void frmMain::AddEvent( wxCommandEvent& event )
591 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
593 wxCommandEvent addEvent(XCCALENDARCTRL_ADDENTRY);
594 addEvent.SetId(ID_ADDENTRY);
595 addEvent.SetClientData(eventInfo);
596 wxPostEvent(mainCalendarCtrl, addEvent);
600 void frmMain::UpdateEvent( wxCommandEvent& event )
603 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
605 wxCommandEvent updateEvent(XCCALENDARCTRL_UPDATEENTRY);
606 updateEvent.SetId(ID_UPDATEENTRY);
607 updateEvent.SetClientData(eventInfo);
608 wxPostEvent(mainCalendarCtrl, updateEvent);
612 void frmMain::ProcessCalendar( wxCommandEvent& event )
615 CalendarProperties *calendarInfo = (CalendarProperties*)event.GetClientData();
617 // Get the account name.
619 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo->accountName);
621 // Build the account directory path.
623 string accountDirectoryPath = string(GetUserDir().mb_str());
624 accountDirectoryPath += "accounts/";
625 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarInfo->accountPreferencesID).mb_str());
626 accountDirectoryPath += ".";
627 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarInfo->accountPreferencesID).mb_str());
628 accountDirectoryPath += "/";
630 // Generate a UUID for the new calendar.
632 string calendarPath = accountDirectoryPath;
634 string calendarUUID = "";
636 if (calendarInfo->editMode == false){
638 calendarUUID = GenerateUUID();
639 calendarPath += calendarUUID;
643 calendarUUID = calendarData.GetCalendar(calendarInfo->calendarID).calendarTextID;
644 calendarPath += calendarPath;
650 // Open the account's calendar file and add to the list of calendars.
652 string calendarListFilenameFull = accountDirectoryPath;
653 calendarListFilenameFull += "calendarlist.db";
655 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
657 wxString calendarDescription = wxString(calendarInfo->calendarDescription);
659 calendarDescription.Replace("\n", "\\n", true);
661 // Create the directory for the calendar entries if
662 // not editing a calendar.
664 calendarListFile->SetPath(wxString(calendarUUID));
665 calendarListFile->Write(wxT("name"), wxString(calendarInfo->calendarName));
666 calendarListFile->Write(wxT("description"), calendarDescription);
667 calendarListFile->Write(wxT("colour"), wxString(calendarInfo->calendarColour));
669 if (calendarInfo->editMode == false){
671 wxMkDir(wxString(calendarPath), wxS_DIR_DEFAULT);
672 calendarData.AddCalendar(accountInfo.accountID, calendarInfo->calendarName, calendarUUID, calendarInfo->calendarColour, calendarInfo->calendarDescription);
676 calendarData.UpdateCalendar(calendarInfo->calendarID, calendarInfo->calendarName, calendarInfo->calendarColour, calendarInfo->calendarDescription);
678 // Post event to update the colour in the calendar entries.
680 updateColourData.calendarID = calendarInfo->calendarID;
681 updateColourData.newColour.red = calendarInfo->calendarColour.red;
682 updateColourData.newColour.green = calendarInfo->calendarColour.green;
683 updateColourData.newColour.blue = calendarInfo->calendarColour.blue;
684 updateColourData.newColour.alpha = calendarInfo->calendarColour.alpha;
686 wxCommandEvent updateColour(XCCALENDARCTRL_UPDATECALENDARCOLOUR);
687 updateColour.SetClientData(&updateColourData);
688 updateColour.SetId(ID_UPDATECOLOUR);
689 wxPostEvent(mainCalendarCtrl, updateColour);
693 // Add the calendar to the calendar data storage.
695 delete calendarListFile;
696 calendarListFile = nullptr;
699 calendarInfo = nullptr;
703 void frmMain::ShowHelp( wxCommandEvent& event )
706 wxMessageBox(_("The help documentation will be implemented in a future version of Xestia Calendar."), _("Unimplemented"));