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 icons.
90 // Setup the form control.
92 mainCalendarCtrl = new XCCalendarCtrl(this, &calendarData);
93 szrMain->Add(mainCalendarCtrl, 1, wxEXPAND, 5);
96 // Bind events to the control.
98 Bind(XCMAIN_PROCESSCALENDAR, &frmMain::ProcessCalendar, this, ID_PROCESSCALENDAR);
99 Bind(XCMAIN_EDITCALENDAR, &frmMain::EditCalendar, this, ID_EDITCALENDAR);
100 Bind(XCMAIN_DELETECALENDAR, &frmMain::DeleteCalendar, this, ID_DELETECALENDAR);
101 Bind(XCMAIN_ADDEVENT, &frmMain::AddEvent, this, ID_ADDENTRY);
102 Bind(XCMAIN_UPDATEEVENT, &frmMain::UpdateEvent, this, ID_UPDATEENTRY);
103 Bind(XCMAIN_EDITEVENT, &frmMain::EditEvent, this, ID_EDITEVENT);
104 Bind(XCMAIN_DELETEEVENT, &frmMain::DeleteEvent, this, ID_DELETEEVENT);
106 Bind(XCMAIN_ADDWINDOWINFO, &frmMain::WindowAdd, this, ID_ADDWINDOW);
107 Bind(XCMAIN_UPDATEWINDOWINFO, &frmMain::WindowUpdate, this, ID_UPDATEWINDOW);
108 Bind(XCMAIN_DELETEWINDOWINFO, &frmMain::WindowDelete, this, ID_DELETEWINDOW);
111 void frmMain::QuitApp( wxCloseEvent& event )
114 // Run the QuitApp function.
120 void frmMain::QuitApp( wxCommandEvent& event )
123 // Run the QuitApp function.
129 void frmMain::QuitApp()
132 // Function to run when quitting.
134 //Go through the windows and close each one (be it search
135 //or contact editor). Abort if user wants to cancel.
137 // Close the contact editor windows.
139 // Close the contact windows.
141 // Close the search windows.
143 // Write out the ETag databases.
145 // Save Preferences: Save the window position if that option is enabled.
147 wxString SetFilename = GetUserPrefDir();
149 #if defined(__HAIKU__)
153 #elif defined(__WIN32__)
155 SetFilename.Append(wxT("settings"));
161 SetFilename.Append(wxT("settings"));
165 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
167 bool SaveWindowPos = false;
168 wxString SaveWindowInc;
169 cfgfile->Read(wxT("SaveWindowPosition"), &SaveWindowInc);
171 if (SaveWindowInc == wxT("true")){
173 SaveWindowPos = true;
177 if (SaveWindowPos == true){
179 wxRect frmMainPos = this->GetRect();
181 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
182 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
183 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
184 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
191 //Everything closed... exit.
199 void frmMain::LoadAccountData()
202 // Get the list of accounts and put into the calendar data storage.
204 int accountCount = preferences->accounts.GetCount();
206 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
208 CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), accountSeek);
212 // Get the list of calendars and put them into the calendar data storage.
214 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
216 CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()));
220 string calendarListFilename = string(GetUserDir().mb_str());
221 calendarListFilename += "accounts/";
222 calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).mb_str());
223 calendarListFilename += ".";
224 calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).mb_str());
226 // Get the list of calendars.
228 XCAccountCalendarList calendarList(calendarListFilename);
230 // Add the calendar and set the calendar ID for it.
232 for (int calendarSeek = 0; calendarSeek < calendarList.calendarShortName.size(); calendarSeek++){
236 CDSCalendarResult calendarAddResult = calendarData.AddCalendar(accountInfo.accountID,
237 calendarList.calendarName[calendarSeek],
238 calendarList.calendarShortName[calendarSeek],
239 calendarList.calendarColour[calendarSeek],
240 calendarList.calendarDescription[calendarSeek]);
242 // Set the calendar ID.
244 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), calendarList.calendarShortName[calendarSeek]);
245 calendarList.calendarStorageID[calendarSeek] = calendarInfo.calendarID;
247 // Find the entries and load each entry.
249 string calendarListDirectory = calendarListFilename;
250 calendarListDirectory += "/";
251 calendarListDirectory += calendarList.calendarShortName[calendarSeek];
252 calendarListDirectory += "/";
254 wxDir entryListDirectory(calendarListDirectory);
255 wxString entryListFilename;
257 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*.ics", wxDIR_NO_FOLLOW|wxDIR_FILES);
259 while (continueProcessing){
261 string entryListFullFilename;
262 entryListFullFilename += calendarListDirectory;
263 entryListFullFilename += string(entryListFilename.mb_str());
265 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
266 CDSAddEntryResult addEventResult = calendarData.AddEvent(calendarInfo.calendarID, entryListFullFilename);
276 void frmMain::ShowPreferencesWindow( wxCommandEvent& event )
279 // Close all windows first.
281 if (CloseAllWindows() == false)
286 // Open the preferences window.
288 reloadAccounts = FALSE;
290 frmPreferences *framePreferences = new frmPreferences ( this );
291 framePreferences->SetupPointers(&reloadAccounts);
292 framePreferences->ShowModal();
293 delete framePreferences;
294 framePreferences = NULL;
296 // Reload the preferences.
298 if (reloadAccounts == true){
300 // Reload the accounts as a change has been made within
303 wxString prefDirectory = GetUserPrefDir();
304 XCALPreferences *oldPreferences = preferences;
305 preferences = new XCALPreferences(prefDirectory);
307 delete oldPreferences;
308 oldPreferences = nullptr;
310 // Clear all of the data from the calendar data storage.
312 calendarData.Clear();
315 // Politely ask the calendar control to reload it's view.
317 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
318 updateGrid.SetId(ID_CHANGEGRID);
319 wxPostEvent(mainCalendarCtrl, updateGrid);
325 void frmMain::ShowAboutWindow( wxCommandEvent& event )
328 // Show the about window.
330 frmAbout *frameAbout = new frmAbout ( this );
331 frameAbout->SetupAboutWindow();
332 frameAbout->ShowModal();
338 void frmMain::ShowUpdateWindow( wxCommandEvent& event )
341 frmUpdate *frameUpdate = new frmUpdate ( this );
342 frameUpdate->FetchData();
343 frameUpdate->ShowModal();
349 void frmMain::OpenNewAccountDialog( wxCommandEvent& event )
352 // Open the new account dialog.
354 reloadAccounts = false;
356 frmNewAccount *frameNewAccount = new frmNewAccount ( this );
357 frameNewAccount->SetupPointers(&reloadAccounts, &calendarData);
358 frameNewAccount->ShowModal();
359 delete frameNewAccount;
360 frameNewAccount = NULL;
362 // Reload the preferences.
364 if (reloadAccounts == true){
366 // Reload the accounts as a change has been made within
369 wxString prefDirectory = GetUserPrefDir();
370 XCALPreferences *oldPreferences = preferences;
371 preferences = new XCALPreferences(prefDirectory);
373 delete oldPreferences;
374 oldPreferences = nullptr;
376 // Clear all of the data from the calendar data storage.
378 calendarData.Clear();
381 // Politely ask the calendar control to reload it's view.
383 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
384 updateGrid.SetId(ID_CHANGEGRID);
385 wxPostEvent(mainCalendarCtrl, updateGrid);
391 void frmMain::CreateNewCalendar( wxCommandEvent& event )
394 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
395 frameNewCalendar->SetMode(false);
396 frameNewCalendar->SetupAccounts(preferences);
397 frameNewCalendar->ShowModal();
398 delete frameNewCalendar;
399 frameNewCalendar = nullptr;
403 void frmMain::EditCalendar( wxCommandEvent& event )
406 // Close all windows first.
408 if (CloseAllWindows() == false)
413 // Get the calendar data.
415 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(event.GetInt());
417 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
418 frameNewCalendar->SetMode(true);
419 frameNewCalendar->SetupAccounts(preferences);
420 frameNewCalendar->SetData(event.GetInt(), calendarInfo.accountName, calendarInfo.calendarName, calendarInfo.calendarDescription, calendarInfo.calendarColour);
421 frameNewCalendar->ShowModal();
422 delete frameNewCalendar;
423 frameNewCalendar = nullptr;
427 void frmMain::DeleteCalendar( wxCommandEvent& event )
430 // Close all windows first.
432 if (CloseAllWindows() == false)
437 CalendarProperties *calendarEventInfo = (CalendarProperties*)event.GetClientData();
439 // Get the calendar data.
441 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(calendarEventInfo->calendarID);
443 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){
447 // Go through the grid and delete each calendar entry widget that
448 // is associated with the calendar.
450 wxCommandEvent deleteCalendar(XCCALENDARCTRL_DELETECALENDARENTRIES);
451 deleteCalendar.SetId(ID_DELETECALENDARENTRIES);
452 deleteCalendar.SetInt(calendarInfo.calendarID);
453 wxPostEvent(mainCalendarCtrl, deleteCalendar);
455 // Get the account configuration file and delete the calendar information.
457 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo.accountName);
459 string accountDirectoryPath = string(GetUserDir().mb_str());
460 accountDirectoryPath += "accounts/";
461 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarEventInfo->accountPreferencesID).mb_str());
462 accountDirectoryPath += ".";
463 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarEventInfo->accountPreferencesID).mb_str());
464 accountDirectoryPath += "/";
466 string calendarListFilenameFull = accountDirectoryPath;
467 calendarListFilenameFull += "calendarlist.db";
469 string calendarDirectoryPath = accountDirectoryPath;
470 calendarDirectoryPath += calendarInfo.calendarTextID;
472 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
474 //calendarListFile->SetPath(wxString(calendarInfo.calendarTextID));
475 calendarListFile->DeleteGroup(wxString(calendarInfo.calendarTextID));
477 // Delete the calendar directory.
479 wxDir entryListDirectory((wxString)calendarDirectoryPath.c_str());
480 wxString entryListFilename;
482 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*", wxDIR_NO_FOLLOW|wxDIR_FILES);
484 while (continueProcessing){
486 string entryListFullFilename;
487 entryListFullFilename += calendarDirectoryPath;
488 entryListFullFilename += "/";
489 entryListFullFilename += string(entryListFilename.mb_str());
491 wxRemoveFile(wxString(entryListFullFilename.c_str()));
493 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
497 wxRmdir(calendarDirectoryPath);
499 // Delete the calendar from the calendar data storage.
501 calendarData.DeleteCalendar(calendarEventInfo->calendarID);
503 delete calendarListFile;
504 calendarListFile = nullptr;
506 delete calendarEventInfo;
507 calendarEventInfo = nullptr;
511 void frmMain::CreateNewEvent( wxCommandEvent& event )
514 frmEventEditor *frameNewEvent = new frmEventEditor ( this );
515 frameNewEvent->SetupForm(&calendarData, preferences);
516 frameNewEvent->SetWindowMenuItemID(++WindowMenuItemID);
518 // Add the window to the window list.
520 WindowData *newWindowData = new WindowData;
522 newWindowData->DataType = 1;
523 newWindowData->WindowPointer = (void*)frameNewEvent;
524 newWindowData->WindowID = WindowMenuItemID;
526 wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
527 addevent.SetId(ID_ADDWINDOW);
528 addevent.SetClientData(newWindowData);
529 wxPostEvent(this, addevent);
531 frameNewEvent->Show();
535 void frmMain::EditEvent( wxCommandEvent& event )
538 frmEventEditor *frameEditEvent = new frmEventEditor ( this );
539 frameEditEvent->SetEventID(event.GetInt());
540 frameEditEvent->SetEditMode(true);
541 frameEditEvent->SetWindowMenuItemID(++WindowMenuItemID);
542 frameEditEvent->SetupForm(&calendarData, preferences);
544 // Add the window to the window list.
546 WindowData *newWindowData = new WindowData;
548 newWindowData->DataType = 1;
549 newWindowData->WindowPointer = (void*)frameEditEvent;
550 newWindowData->WindowID = WindowMenuItemID;
552 wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
553 addevent.SetId(ID_ADDWINDOW);
554 addevent.SetClientData(newWindowData);
555 wxPostEvent(this, addevent);
557 // Setup the form and display it.
559 frameEditEvent->Show();
563 void frmMain::DeleteEvent( wxCommandEvent& event )
566 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
568 // Get the event information.
570 CDSGetCalendarEntryInfo eventDeleteData = calendarData.GetEvent(eventInfo->eventID);
571 CDSGetCalendarInfo calendarDeleteData = calendarData.GetCalendar(eventInfo->calendarID);
573 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){
577 // Go through the grid and delete the entry from the GUI.
579 wxCommandEvent deleteEvent(XCCALENDARCTRL_DELETEENTRY);
580 deleteEvent.SetId(ID_DELETEENTRY);
581 deleteEvent.SetInt(eventInfo->eventID);
582 wxPostEvent(mainCalendarCtrl, deleteEvent);
584 // Get the filename and delete the entry.
586 wxRemoveFile(wxString(eventDeleteData.entryFilename.c_str()));
588 // Delete the entry from the calendar data storage.
590 calendarData.DeleteEvent(eventInfo->eventID);
597 void frmMain::AddEvent( wxCommandEvent& event )
600 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
602 wxCommandEvent addEvent(XCCALENDARCTRL_ADDENTRY);
603 addEvent.SetId(ID_ADDENTRY);
604 addEvent.SetClientData(eventInfo);
605 wxPostEvent(mainCalendarCtrl, addEvent);
609 void frmMain::UpdateEvent( wxCommandEvent& event )
612 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
614 wxCommandEvent updateEvent(XCCALENDARCTRL_UPDATEENTRY);
615 updateEvent.SetId(ID_UPDATEENTRY);
616 updateEvent.SetClientData(eventInfo);
617 wxPostEvent(mainCalendarCtrl, updateEvent);
621 void frmMain::ProcessCalendar( wxCommandEvent& event )
624 CalendarProperties *calendarInfo = (CalendarProperties*)event.GetClientData();
626 // Get the account name.
628 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo->accountName);
630 // Build the account directory path.
632 string accountDirectoryPath = string(GetUserDir().mb_str());
633 accountDirectoryPath += "accounts/";
634 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarInfo->accountPreferencesID).mb_str());
635 accountDirectoryPath += ".";
636 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarInfo->accountPreferencesID).mb_str());
637 accountDirectoryPath += "/";
639 // Generate a UUID for the new calendar.
641 string calendarPath = accountDirectoryPath;
643 string calendarUUID = "";
645 if (calendarInfo->editMode == false){
647 calendarUUID = GenerateUUID();
648 calendarPath += calendarUUID;
652 calendarUUID = calendarData.GetCalendar(calendarInfo->calendarID).calendarTextID;
653 calendarPath += calendarPath;
659 // Open the account's calendar file and add to the list of calendars.
661 string calendarListFilenameFull = accountDirectoryPath;
662 calendarListFilenameFull += "calendarlist.db";
664 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
666 wxString calendarDescription = wxString(calendarInfo->calendarDescription);
668 calendarDescription.Replace("\n", "\\n", true);
670 // Create the directory for the calendar entries if
671 // not editing a calendar.
673 calendarListFile->SetPath(wxString(calendarUUID));
674 calendarListFile->Write(wxT("name"), wxString(calendarInfo->calendarName));
675 calendarListFile->Write(wxT("description"), calendarDescription);
676 calendarListFile->Write(wxT("colour"), wxString(calendarInfo->calendarColour));
678 if (calendarInfo->editMode == false){
680 wxMkDir(wxString(calendarPath), wxS_DIR_DEFAULT);
681 calendarData.AddCalendar(accountInfo.accountID, calendarInfo->calendarName, calendarUUID, calendarInfo->calendarColour, calendarInfo->calendarDescription);
685 calendarData.UpdateCalendar(calendarInfo->calendarID, calendarInfo->calendarName, calendarInfo->calendarColour, calendarInfo->calendarDescription);
687 // Post event to update the colour in the calendar entries.
689 updateColourData.calendarID = calendarInfo->calendarID;
690 updateColourData.newColour.red = calendarInfo->calendarColour.red;
691 updateColourData.newColour.green = calendarInfo->calendarColour.green;
692 updateColourData.newColour.blue = calendarInfo->calendarColour.blue;
693 updateColourData.newColour.alpha = calendarInfo->calendarColour.alpha;
695 wxCommandEvent updateColour(XCCALENDARCTRL_UPDATECALENDARCOLOUR);
696 updateColour.SetClientData(&updateColourData);
697 updateColour.SetId(ID_UPDATECOLOUR);
698 wxPostEvent(mainCalendarCtrl, updateColour);
702 // Add the calendar to the calendar data storage.
704 delete calendarListFile;
705 calendarListFile = nullptr;
708 calendarInfo = nullptr;
712 void frmMain::ShowHelp( wxCommandEvent& event )
715 wxMessageBox(_("The help documentation will be implemented in a future version of Xestia Calendar."), _("Unimplemented"));
719 void frmMain::ShowActivityManager( wxCommandEvent &event )
722 activityManager->Show();