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 frmMain::frmMain( wxWindow* parent )
26 // Setup the default settings if they don't
29 // Setup the preferences.
30 wxString prefDirectory = GetUserPrefDir();
31 preferences = new XCALPreferences(prefDirectory);
35 wxString fullPrefPath;
37 fullPrefPath.Append(prefDirectory);
38 fullPrefPath.Append(wxT("settings"));
40 wxFileConfig *settingfile = new wxFileConfig("", "", fullPrefPath);
43 settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
45 if (ValueInc == wxT("true")){
47 wxRect windowPosition;
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);
64 this->SetSize(-1, -1, 800, 600);
70 // Load the account data.
74 // Setup the form control.
76 mainCalendarCtrl = new XCCalendarCtrl(this, &calendarData);
77 szrMain->Add(mainCalendarCtrl, 1, wxEXPAND, 5);
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));
88 Connect(ID_ADDWINDOW, XCMAIN_ADDWINDOWINFO, wxCommandEventHandler(frmMain::WindowAdd));
89 Connect(ID_UPDATEWINDOW, XCMAIN_UPDATEWINDOWINFO, wxCommandEventHandler(frmMain::WindowUpdate));
90 Connect(ID_DELETEWINDOW, XCMAIN_DELETEWINDOWINFO, wxCommandEventHandler(frmMain::WindowDelete));
94 void frmMain::QuitApp( wxCloseEvent& event )
97 // Run the QuitApp function.
103 void frmMain::QuitApp( wxCommandEvent& event )
106 // Run the QuitApp function.
112 void frmMain::QuitApp()
115 // Function to run when quitting.
117 //Go through the windows and close each one (be it search
118 //or contact editor). Abort if user wants to cancel.
120 // Close the contact editor windows.
122 // Close the contact windows.
124 // Close the search windows.
126 // Write out the ETag databases.
128 // Save Preferences: Save the window position if that option is enabled.
130 wxString SetFilename = GetUserPrefDir();
132 #if defined(__HAIKU__)
136 #elif defined(__WIN32__)
138 SetFilename.Append(wxT("settings"));
144 SetFilename.Append(wxT("settings"));
148 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
150 bool SaveWindowPos = false;
151 wxString SaveWindowInc;
152 cfgfile->Read(wxT("SaveWindowPosition"), &SaveWindowInc);
154 if (SaveWindowInc == wxT("true")){
156 SaveWindowPos = true;
160 if (SaveWindowPos == true){
162 wxRect frmMainPos = this->GetRect();
164 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
165 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
166 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
167 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
174 //Everything closed... exit.
182 void frmMain::LoadAccountData(){
184 // Get the list of accounts and put into the calendar data storage.
186 int accountCount = preferences->accounts.GetCount();
188 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
190 CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), accountSeek);
194 // Get the list of calendars and put them into the calendar data storage.
196 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
198 CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()));
202 string calendarListFilename = string(GetUserDir().mb_str());
203 calendarListFilename += "accounts/";
204 calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).mb_str());
205 calendarListFilename += ".";
206 calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).mb_str());
208 // Get the list of calendars.
210 XCAccountCalendarList calendarList(calendarListFilename);
212 // Add the calendar and set the calendar ID for it.
214 for (int calendarSeek = 0; calendarSeek < calendarList.calendarShortName.size(); calendarSeek++){
218 CDSCalendarResult calendarAddResult = calendarData.AddCalendar(accountInfo.accountID,
219 calendarList.calendarName[calendarSeek],
220 calendarList.calendarShortName[calendarSeek],
221 calendarList.calendarColour[calendarSeek],
222 calendarList.calendarDescription[calendarSeek]);
224 // Set the calendar ID.
226 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), calendarList.calendarShortName[calendarSeek]);
227 calendarList.calendarStorageID[calendarSeek] = calendarInfo.calendarID;
229 // Find the entries and load each entry.
231 string calendarListDirectory = calendarListFilename;
232 calendarListDirectory += "/";
233 calendarListDirectory += calendarList.calendarShortName[calendarSeek];
234 calendarListDirectory += "/";
236 wxDir entryListDirectory(calendarListDirectory);
237 wxString entryListFilename;
239 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*.ics", wxDIR_NO_FOLLOW|wxDIR_FILES);
241 while (continueProcessing){
243 string entryListFullFilename;
244 entryListFullFilename += calendarListDirectory;
245 entryListFullFilename += string(entryListFilename.mb_str());
247 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
248 CDSAddEntryResult addEventResult = calendarData.AddEvent(calendarInfo.calendarID, entryListFullFilename);
258 void frmMain::ShowPreferencesWindow( wxCommandEvent& event )
261 // Close all windows first.
263 if (CloseAllWindows() == false)
268 // Open the preferences window.
270 reloadAccounts = FALSE;
272 frmPreferences *framePreferences = new frmPreferences ( this );
273 framePreferences->SetupPointers(&reloadAccounts);
274 framePreferences->ShowModal();
275 delete framePreferences;
276 framePreferences = NULL;
278 // Reload the preferences.
280 if (reloadAccounts == true){
282 // Reload the accounts as a change has been made within
285 wxString prefDirectory = GetUserPrefDir();
286 XCALPreferences *oldPreferences = preferences;
287 preferences = new XCALPreferences(prefDirectory);
289 delete oldPreferences;
290 oldPreferences = nullptr;
292 // Clear all of the data from the calendar data storage.
294 calendarData.Clear();
297 // Politely ask the calendar control to reload it's view.
299 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
300 updateGrid.SetId(ID_CHANGEGRID);
301 wxPostEvent(mainCalendarCtrl, updateGrid);
307 void frmMain::ShowAboutWindow( wxCommandEvent& event )
310 // Show the about window.
312 frmAbout *frameAbout = new frmAbout ( this );
313 frameAbout->SetupAboutWindow();
314 frameAbout->ShowModal();
320 void frmMain::ShowUpdateWindow( wxCommandEvent& event )
323 frmUpdate *frameUpdate = new frmUpdate ( this );
324 frameUpdate->FetchData();
325 frameUpdate->ShowModal();
331 void frmMain::OpenNewAccountDialog( wxCommandEvent& event )
334 // Open the new account dialog.
336 reloadAccounts = false;
338 frmNewAccount *frameNewAccount = new frmNewAccount ( this );
339 frameNewAccount->SetupPointers(&reloadAccounts, &calendarData);
340 frameNewAccount->ShowModal();
341 delete frameNewAccount;
342 frameNewAccount = NULL;
344 // Reload the preferences.
346 if (reloadAccounts == true){
348 // Reload the accounts as a change has been made within
351 wxString prefDirectory = GetUserPrefDir();
352 XCALPreferences *oldPreferences = preferences;
353 preferences = new XCALPreferences(prefDirectory);
355 delete oldPreferences;
356 oldPreferences = nullptr;
358 // Clear all of the data from the calendar data storage.
360 calendarData.Clear();
363 // Politely ask the calendar control to reload it's view.
365 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
366 updateGrid.SetId(ID_CHANGEGRID);
367 wxPostEvent(mainCalendarCtrl, updateGrid);
373 void frmMain::CreateNewCalendar( wxCommandEvent& event )
376 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
377 frameNewCalendar->SetMode(false);
378 frameNewCalendar->SetupAccounts(preferences);
379 frameNewCalendar->ShowModal();
380 delete frameNewCalendar;
381 frameNewCalendar = nullptr;
385 void frmMain::EditCalendar( wxCommandEvent& event )
388 // Close all windows first.
390 if (CloseAllWindows() == false)
395 // Get the calendar data.
397 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(event.GetInt());
399 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
400 frameNewCalendar->SetMode(true);
401 frameNewCalendar->SetupAccounts(preferences);
402 frameNewCalendar->SetData(event.GetInt(), calendarInfo.accountName, calendarInfo.calendarName, calendarInfo.calendarDescription, calendarInfo.calendarColour);
403 frameNewCalendar->ShowModal();
404 delete frameNewCalendar;
405 frameNewCalendar = nullptr;
409 void frmMain::DeleteCalendar( wxCommandEvent& event )
412 // Close all windows first.
414 if (CloseAllWindows() == false)
419 CalendarProperties *calendarEventInfo = (CalendarProperties*)event.GetClientData();
421 // Get the calendar data.
423 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(calendarEventInfo->calendarID);
425 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){
429 // Go through the grid and delete each calendar entry widget that
430 // is associated with the calendar.
432 wxCommandEvent deleteCalendar(XCCALENDARCTRL_DELETECALENDARENTRIES);
433 deleteCalendar.SetId(ID_DELETECALENDARENTRIES);
434 deleteCalendar.SetInt(calendarInfo.calendarID);
435 wxPostEvent(mainCalendarCtrl, deleteCalendar);
437 // Get the account configuration file and delete the calendar information.
439 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo.accountName);
441 string accountDirectoryPath = string(GetUserDir().mb_str());
442 accountDirectoryPath += "accounts/";
443 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarEventInfo->accountPreferencesID).mb_str());
444 accountDirectoryPath += ".";
445 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarEventInfo->accountPreferencesID).mb_str());
446 accountDirectoryPath += "/";
448 string calendarListFilenameFull = accountDirectoryPath;
449 calendarListFilenameFull += "calendarlist.db";
451 string calendarDirectoryPath = accountDirectoryPath;
452 calendarDirectoryPath += calendarInfo.calendarTextID;
454 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
456 //calendarListFile->SetPath(wxString(calendarInfo.calendarTextID));
457 calendarListFile->DeleteGroup(wxString(calendarInfo.calendarTextID));
459 // Delete the calendar directory.
461 wxDir entryListDirectory((wxString)calendarDirectoryPath.c_str());
462 wxString entryListFilename;
464 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*", wxDIR_NO_FOLLOW|wxDIR_FILES);
466 while (continueProcessing){
468 string entryListFullFilename;
469 entryListFullFilename += calendarDirectoryPath;
470 entryListFullFilename += "/";
471 entryListFullFilename += string(entryListFilename.mb_str());
473 wxRemoveFile(wxString(entryListFullFilename.c_str()));
475 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
479 wxRmdir(calendarDirectoryPath);
481 // Delete the calendar from the calendar data storage.
483 calendarData.DeleteCalendar(calendarEventInfo->calendarID);
485 delete calendarListFile;
486 calendarListFile = nullptr;
488 delete calendarEventInfo;
489 calendarEventInfo = nullptr;
493 void frmMain::CreateNewEvent( wxCommandEvent& event ){
495 frmEventEditor *frameNewEvent = new frmEventEditor ( this );
496 frameNewEvent->SetupForm(&calendarData, preferences);
497 frameNewEvent->SetWindowMenuItemID(++WindowMenuItemID);
499 // Add the window to the window list.
501 WindowData *newWindowData = new WindowData;
503 newWindowData->DataType = 1;
504 newWindowData->WindowPointer = (void*)frameNewEvent;
505 newWindowData->WindowID = WindowMenuItemID;
507 wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
508 addevent.SetId(ID_ADDWINDOW);
509 addevent.SetClientData(newWindowData);
510 wxPostEvent(this, addevent);
512 frameNewEvent->Show();
516 void frmMain::EditEvent( wxCommandEvent& event ){
518 frmEventEditor *frameEditEvent = new frmEventEditor ( this );
519 frameEditEvent->SetEventID(event.GetInt());
520 frameEditEvent->SetEditMode(true);
521 frameEditEvent->SetWindowMenuItemID(++WindowMenuItemID);
522 frameEditEvent->SetupForm(&calendarData, preferences);
524 // Add the window to the window list.
526 WindowData *newWindowData = new WindowData;
528 newWindowData->DataType = 1;
529 newWindowData->WindowPointer = (void*)frameEditEvent;
530 newWindowData->WindowID = WindowMenuItemID;
532 wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
533 addevent.SetId(ID_ADDWINDOW);
534 addevent.SetClientData(newWindowData);
535 wxPostEvent(this, addevent);
537 // Setup the form and display it.
539 frameEditEvent->Show();
543 void frmMain::DeleteEvent( wxCommandEvent& event ){
545 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
547 // Get the event information.
549 CDSGetCalendarEntryInfo eventDeleteData = calendarData.GetEvent(eventInfo->eventID);
550 CDSGetCalendarInfo calendarDeleteData = calendarData.GetCalendar(eventInfo->calendarID);
552 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){
556 // Go through the grid and delete the entry from the GUI.
558 wxCommandEvent deleteEvent(XCCALENDARCTRL_DELETEENTRY);
559 deleteEvent.SetId(ID_DELETEENTRY);
560 deleteEvent.SetInt(eventInfo->eventID);
561 wxPostEvent(mainCalendarCtrl, deleteEvent);
563 // Get the filename and delete the entry.
565 wxRemoveFile(wxString(eventDeleteData.entryFilename.c_str()));
567 // Delete the entry from the calendar data storage.
569 calendarData.DeleteEvent(eventInfo->eventID);
576 void frmMain::AddEvent( wxCommandEvent& event )
579 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
581 wxCommandEvent addEvent(XCCALENDARCTRL_ADDENTRY);
582 addEvent.SetId(ID_ADDENTRY);
583 addEvent.SetClientData(eventInfo);
584 wxPostEvent(mainCalendarCtrl, addEvent);
588 void frmMain::UpdateEvent( wxCommandEvent& event )
591 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
593 wxCommandEvent updateEvent(XCCALENDARCTRL_UPDATEENTRY);
594 updateEvent.SetId(ID_UPDATEENTRY);
595 updateEvent.SetClientData(eventInfo);
596 wxPostEvent(mainCalendarCtrl, updateEvent);
600 void frmMain::ProcessCalendar( wxCommandEvent& event )
603 CalendarProperties *calendarInfo = (CalendarProperties*)event.GetClientData();
605 // Get the account name.
607 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo->accountName);
609 // Build the account directory path.
611 string accountDirectoryPath = string(GetUserDir().mb_str());
612 accountDirectoryPath += "accounts/";
613 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarInfo->accountPreferencesID).mb_str());
614 accountDirectoryPath += ".";
615 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarInfo->accountPreferencesID).mb_str());
616 accountDirectoryPath += "/";
618 // Generate a UUID for the new calendar.
620 string calendarPath = accountDirectoryPath;
622 string calendarUUID = "";
624 if (calendarInfo->editMode == false){
626 calendarUUID = GenerateUUID();
627 calendarPath += calendarUUID;
631 calendarUUID = calendarData.GetCalendar(calendarInfo->calendarID).calendarTextID;
632 calendarPath += calendarPath;
638 // Open the account's calendar file and add to the list of calendars.
640 string calendarListFilenameFull = accountDirectoryPath;
641 calendarListFilenameFull += "calendarlist.db";
643 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
645 wxString calendarDescription = wxString(calendarInfo->calendarDescription);
647 calendarDescription.Replace("\n", "\\n", true);
649 // Create the directory for the calendar entries if
650 // not editing a calendar.
652 calendarListFile->SetPath(wxString(calendarUUID));
653 calendarListFile->Write(wxT("name"), wxString(calendarInfo->calendarName));
654 calendarListFile->Write(wxT("description"), calendarDescription);
655 calendarListFile->Write(wxT("colour"), wxString(calendarInfo->calendarColour));
657 if (calendarInfo->editMode == false){
659 wxMkDir(wxString(calendarPath), wxS_DIR_DEFAULT);
660 calendarData.AddCalendar(accountInfo.accountID, calendarInfo->calendarName, calendarUUID, calendarInfo->calendarColour, calendarInfo->calendarDescription);
664 calendarData.UpdateCalendar(calendarInfo->calendarID, calendarInfo->calendarName, calendarInfo->calendarColour, calendarInfo->calendarDescription);
666 // Post event to update the colour in the calendar entries.
668 updateColourData.calendarID = calendarInfo->calendarID;
669 updateColourData.newColour.red = calendarInfo->calendarColour.red;
670 updateColourData.newColour.green = calendarInfo->calendarColour.green;
671 updateColourData.newColour.blue = calendarInfo->calendarColour.blue;
672 updateColourData.newColour.alpha = calendarInfo->calendarColour.alpha;
674 wxCommandEvent updateColour(XCCALENDARCTRL_UPDATECALENDARCOLOUR);
675 updateColour.SetClientData(&updateColourData);
676 updateColour.SetId(ID_UPDATECOLOUR);
677 wxPostEvent(mainCalendarCtrl, updateColour);
681 // Add the calendar to the calendar data storage.
683 delete calendarListFile;
684 calendarListFile = nullptr;
687 calendarInfo = nullptr;
691 void frmMain::ShowHelp( wxCommandEvent& event )
694 wxMessageBox(_("The help documentation will be implemented in a future version of Xestia Calendar."), _("Unimplemented"));