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).ToUTF8()), 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).ToUTF8()));
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 wxString calendarListDirectory = wxString(calendarListFilename.c_str(), wxConvUTF8);
244 calendarListDirectory += "/";
245 calendarListDirectory += wxString(calendarList.calendarShortName[calendarSeek].c_str(), wxConvUTF8);
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 += string(calendarListDirectory.ToUTF8());
257 entryListFullFilename += string(entryListFilename.ToUTF8());
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?", wxString(calendarInfo.calendarName.c_str(), wxConvUTF8), wxString(calendarInfo.accountName.c_str(), wxConvUTF8)), "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.c_str());
453 string accountDirectoryPath = string(GetUserDir().mb_str());
454 accountDirectoryPath += "accounts/";
455 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarEventInfo->accountPreferencesID).ToUTF8());
456 accountDirectoryPath += ".";
457 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarEventInfo->accountPreferencesID).ToUTF8());
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.c_str(), wxConvUTF8));
468 // Delete the calendar directory.
470 wxDir entryListDirectory(wxString(calendarDirectoryPath.c_str(), wxConvUTF8));
471 wxString entryListFilename;
473 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*", wxDIR_NO_FOLLOW|wxDIR_FILES);
475 while (continueProcessing){
477 string entryListFullFilename;
478 entryListFullFilename += wxString(calendarDirectoryPath.c_str(), wxConvUTF8);
479 entryListFullFilename += "/";
480 entryListFullFilename += string(entryListFilename.mb_str());
482 wxRemoveFile(wxString(entryListFullFilename.c_str(), wxConvUTF8));
484 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
488 entryListDirectory.Close();
490 // Delete the calendar from the account calendar list.
492 calendarListFile->DeleteGroup(wxString(calendarInfo.calendarTextID));
494 // Delete the calendar from the calendar data storage.
496 calendarData.DeleteCalendar(calendarEventInfo->calendarID);
498 // Delete the calendar directory.
500 wxRmDir(wxString(calendarDirectoryPath.c_str(), wxConvUTF8));
502 delete calendarListFile;
503 calendarListFile = nullptr;
505 delete calendarEventInfo;
506 calendarEventInfo = nullptr;
510 void frmMain::CreateNewEvent( wxCommandEvent& event ){
512 frmEventEditor *frameNewEvent = new frmEventEditor ( this );
513 frameNewEvent->SetupForm(&calendarData, preferences);
514 frameNewEvent->SetWindowMenuItemID(++WindowMenuItemID);
516 // Add the window to the window list.
518 WindowData *newWindowData = new WindowData;
520 newWindowData->DataType = 1;
521 newWindowData->WindowPointer = (void*)frameNewEvent;
522 newWindowData->WindowID = WindowMenuItemID;
524 wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
525 addevent.SetId(ID_ADDWINDOW);
526 addevent.SetClientData(newWindowData);
527 wxPostEvent(this, addevent);
529 frameNewEvent->Show();
533 void frmMain::EditEvent( wxCommandEvent& event ){
535 frmEventEditor *frameEditEvent = new frmEventEditor ( this );
536 frameEditEvent->SetEventID(event.GetInt());
537 frameEditEvent->SetEditMode(true);
538 frameEditEvent->SetWindowMenuItemID(++WindowMenuItemID);
539 frameEditEvent->SetupForm(&calendarData, preferences);
541 // Add the window to the window list.
543 WindowData *newWindowData = new WindowData;
545 newWindowData->DataType = 1;
546 newWindowData->WindowPointer = (void*)frameEditEvent;
547 newWindowData->WindowID = WindowMenuItemID;
549 wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
550 addevent.SetId(ID_ADDWINDOW);
551 addevent.SetClientData(newWindowData);
552 wxPostEvent(this, addevent);
554 // Setup the form and display it.
556 frameEditEvent->Show();
560 void frmMain::DeleteEvent( wxCommandEvent& event ){
562 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
564 // Get the event information.
566 CDSGetCalendarEntryInfo eventDeleteData = calendarData.GetEvent(eventInfo->eventID);
567 CDSGetCalendarInfo calendarDeleteData = calendarData.GetCalendar(eventInfo->calendarID);
569 if (wxMessageBox(wxString::Format("Are you sure you want to delete the event %s from the %s calendar?", wxString(eventDeleteData.entryName.c_str(), wxConvUTF8), wxString(calendarDeleteData.calendarName.c_str(), wxConvUTF8)), "Delete event", wxYES_NO|wxICON_QUESTION) == wxNO){
573 // Go through the grid and delete the entry from the GUI.
575 wxCommandEvent deleteEvent(XCCALENDARCTRL_DELETEENTRY);
576 deleteEvent.SetId(ID_DELETEENTRY);
577 deleteEvent.SetInt(eventInfo->eventID);
578 wxPostEvent(mainCalendarCtrl, deleteEvent);
580 // Get the filename and delete the entry.
582 wxRemoveFile(wxString(eventDeleteData.entryFilename.c_str(), wxConvUTF8));
584 // Delete the entry from the calendar data storage.
586 calendarData.DeleteEvent(eventInfo->eventID);
593 void frmMain::AddEvent( wxCommandEvent& event )
596 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
598 wxCommandEvent addEvent(XCCALENDARCTRL_ADDENTRY);
599 addEvent.SetId(ID_ADDENTRY);
600 addEvent.SetClientData(eventInfo);
601 wxPostEvent(mainCalendarCtrl, addEvent);
605 void frmMain::UpdateEvent( wxCommandEvent& event )
608 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
610 wxCommandEvent updateEvent(XCCALENDARCTRL_UPDATEENTRY);
611 updateEvent.SetId(ID_UPDATEENTRY);
612 updateEvent.SetClientData(eventInfo);
613 wxPostEvent(mainCalendarCtrl, updateEvent);
617 void frmMain::ProcessCalendar( wxCommandEvent& event )
620 CalendarProperties *calendarInfo = (CalendarProperties*)event.GetClientData();
622 // Get the account name.
624 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo->accountName.c_str());
626 // Build the account directory path.
628 string accountDirectoryPath = string(GetUserDir().mb_str());
629 accountDirectoryPath += "accounts/";
630 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarInfo->accountPreferencesID).ToUTF8());
631 accountDirectoryPath += ".";
632 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarInfo->accountPreferencesID).mb_str());
633 accountDirectoryPath += "/";
635 // Generate a UUID for the new calendar.
637 string calendarPath = accountDirectoryPath;
639 string calendarUUID = "";
641 if (calendarInfo->editMode == false){
643 calendarUUID = GenerateUUID();
644 calendarPath += calendarUUID;
648 calendarUUID = calendarData.GetCalendar(calendarInfo->calendarID).calendarTextID;
649 calendarPath += calendarPath;
655 // Open the account's calendar file and add to the list of calendars.
657 string calendarListFilenameFull = accountDirectoryPath;
658 calendarListFilenameFull += "calendarlist.db";
660 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull.c_str(), wxConvUTF8));
662 wxString calendarDescription = wxString(calendarInfo->calendarDescription.c_str(), wxConvUTF8);
664 calendarDescription.Replace("\n", "\\n", true);
666 // Create the directory for the calendar entries if
667 // not editing a calendar.
669 calendarListFile->SetPath(wxString(calendarUUID));
670 calendarListFile->Write(wxT("name"), wxString(calendarInfo->calendarName.c_str(), wxConvUTF8));
671 calendarListFile->Write(wxT("description"), calendarDescription);
672 calendarListFile->Write(wxT("colour"), wxString(calendarInfo->calendarColour));
674 if (calendarInfo->editMode == false){
676 wxMkDir(wxString(calendarPath.c_str(), wxConvUTF8), wxS_DIR_DEFAULT);
677 calendarData.AddCalendar(accountInfo.accountID, calendarInfo->calendarName, calendarUUID, calendarInfo->calendarColour, calendarInfo->calendarDescription);
681 calendarData.UpdateCalendar(calendarInfo->calendarID, calendarInfo->calendarName, calendarInfo->calendarColour, calendarInfo->calendarDescription);
683 // Post event to update the colour in the calendar entries.
685 updateColourData.calendarID = calendarInfo->calendarID;
686 updateColourData.newColour.red = calendarInfo->calendarColour.red;
687 updateColourData.newColour.green = calendarInfo->calendarColour.green;
688 updateColourData.newColour.blue = calendarInfo->calendarColour.blue;
689 updateColourData.newColour.alpha = calendarInfo->calendarColour.alpha;
691 wxCommandEvent updateColour(XCCALENDARCTRL_UPDATECALENDARCOLOUR);
692 updateColour.SetClientData(&updateColourData);
693 updateColour.SetId(ID_UPDATECOLOUR);
694 wxPostEvent(mainCalendarCtrl, updateColour);
698 // Add the calendar to the calendar data storage.
700 delete calendarListFile;
701 calendarListFile = nullptr;
704 calendarInfo = nullptr;
708 void frmMain::ShowHelp( wxCommandEvent& event )
711 wxMessageBox(_("The help documentation will be implemented in a future version of Xestia Calendar."), _("Unimplemented"));