1 // frmMain.h - frmMain form functions header.
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));
90 void frmMain::QuitApp( wxCloseEvent& event )
93 // Run the QuitApp function.
99 void frmMain::QuitApp( wxCommandEvent& event )
102 // Run the QuitApp function.
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"));
140 SetFilename.Append(wxT("settings"));
144 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
146 bool SaveWindowPos = false;
147 wxString SaveWindowInc;
148 cfgfile->Read(wxT("SaveWindowPosition"), &SaveWindowInc);
150 if (SaveWindowInc == wxT("true")){
152 SaveWindowPos = true;
156 if (SaveWindowPos == true){
158 wxRect frmMainPos = this->GetRect();
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());
170 //Everything closed... exit.
178 void frmMain::LoadAccountData(){
180 // Get the list of accounts and put into the calendar data storage.
182 int accountCount = preferences->accounts.GetCount();
184 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
186 CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), accountSeek);
190 // Get the list of calendars and put them into the calendar data storage.
192 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
194 CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()));
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());
204 // Get the list of calendars.
206 XCAccountCalendarList calendarList(calendarListFilename);
208 // Add the calendar and set the calendar ID for it.
210 for (int calendarSeek = 0; calendarSeek < calendarList.calendarShortName.size(); calendarSeek++){
214 CDSCalendarResult calendarAddResult = calendarData.AddCalendar(accountInfo.accountID,
215 calendarList.calendarName[calendarSeek],
216 calendarList.calendarShortName[calendarSeek],
217 calendarList.calendarColour[calendarSeek],
218 calendarList.calendarDescription[calendarSeek]);
220 // Set the calendar ID.
222 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), calendarList.calendarShortName[calendarSeek]);
223 calendarList.calendarStorageID[calendarSeek] = calendarInfo.calendarID;
225 // Find the entries and load each entry.
227 string calendarListDirectory = calendarListFilename;
228 calendarListDirectory += "/";
229 calendarListDirectory += calendarList.calendarShortName[calendarSeek];
230 calendarListDirectory += "/";
232 wxDir entryListDirectory(calendarListDirectory);
233 wxString entryListFilename;
235 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*.ics", wxDIR_NO_FOLLOW|wxDIR_FILES);
237 while (continueProcessing){
239 string entryListFullFilename;
240 entryListFullFilename += calendarListDirectory;
241 entryListFullFilename += string(entryListFilename.mb_str());
243 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
244 CDSAddEntryResult addEventResult = calendarData.AddEvent(calendarInfo.calendarID, entryListFullFilename);
254 void frmMain::ShowPreferencesWindow( wxCommandEvent& event )
257 // Open the preferences window.
259 reloadAccounts = FALSE;
261 frmPreferences *framePreferences = new frmPreferences ( this );
262 framePreferences->SetupPointers(&reloadAccounts);
263 framePreferences->ShowModal();
264 delete framePreferences;
265 framePreferences = NULL;
267 // Reload the preferences.
269 if (reloadAccounts == true){
271 // Reload the accounts as a change has been made within
274 wxString prefDirectory = GetUserPrefDir();
275 XCALPreferences *oldPreferences = preferences;
276 preferences = new XCALPreferences(prefDirectory);
278 delete oldPreferences;
279 oldPreferences = nullptr;
281 // Clear all of the data from the calendar data storage.
283 calendarData.Clear();
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);
296 void frmMain::ShowAboutWindow( wxCommandEvent& event )
299 // Show the about window.
301 frmAbout *frameAbout = new frmAbout ( this );
302 frameAbout->SetupAboutWindow();
303 frameAbout->ShowModal();
309 void frmMain::ShowUpdateWindow( wxCommandEvent& event )
312 frmUpdate *frameUpdate = new frmUpdate ( this );
313 frameUpdate->FetchData();
314 frameUpdate->ShowModal();
320 void frmMain::OpenNewAccountDialog( wxCommandEvent& event )
323 // Open the new account dialog.
325 reloadAccounts = false;
327 frmNewAccount *frameNewAccount = new frmNewAccount ( this );
328 frameNewAccount->SetupPointers(&reloadAccounts, &calendarData);
329 frameNewAccount->ShowModal();
330 delete frameNewAccount;
331 frameNewAccount = NULL;
333 // Reload the preferences.
335 if (reloadAccounts == true){
337 // Reload the accounts as a change has been made within
340 wxString prefDirectory = GetUserPrefDir();
341 XCALPreferences *oldPreferences = preferences;
342 preferences = new XCALPreferences(prefDirectory);
344 delete oldPreferences;
345 oldPreferences = nullptr;
347 // Clear all of the data from the calendar data storage.
349 calendarData.Clear();
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);
362 void frmMain::CreateNewCalendar( wxCommandEvent& event )
365 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
366 frameNewCalendar->SetMode(false);
367 frameNewCalendar->SetupAccounts(preferences);
368 frameNewCalendar->ShowModal();
369 delete frameNewCalendar;
370 frameNewCalendar = nullptr;
374 void frmMain::EditCalendar( wxCommandEvent& event )
377 // Get the calendar data.
379 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(event.GetInt());
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;
391 void frmMain::DeleteCalendar( wxCommandEvent& event )
394 CalendarProperties *calendarEventInfo = (CalendarProperties*)event.GetClientData();
396 // Get the calendar data.
398 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(calendarEventInfo->calendarID);
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){
404 // Go through the grid and delete each calendar entry widget that
405 // is associated with the calendar.
407 wxCommandEvent deleteCalendar(XCCALENDARCTRL_DELETECALENDARENTRIES);
408 deleteCalendar.SetId(ID_DELETECALENDARENTRIES);
409 deleteCalendar.SetInt(calendarInfo.calendarID);
410 wxPostEvent(mainCalendarCtrl, deleteCalendar);
412 // Get the account configuration file and delete the calendar information.
414 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo.accountName);
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 += "/";
423 string calendarListFilenameFull = accountDirectoryPath;
424 calendarListFilenameFull += "calendarlist.db";
426 string calendarDirectoryPath = accountDirectoryPath;
427 calendarDirectoryPath += calendarInfo.calendarTextID;
429 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
431 //calendarListFile->SetPath(wxString(calendarInfo.calendarTextID));
432 calendarListFile->DeleteGroup(wxString(calendarInfo.calendarTextID));
434 // Delete the calendar directory.
436 wxDir entryListDirectory((wxString)calendarDirectoryPath.c_str());
437 wxString entryListFilename;
439 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*", wxDIR_NO_FOLLOW|wxDIR_FILES);
441 while (continueProcessing){
443 string entryListFullFilename;
444 entryListFullFilename += calendarDirectoryPath;
445 entryListFullFilename += "/";
446 entryListFullFilename += string(entryListFilename.mb_str());
448 wxRemoveFile(wxString(entryListFullFilename.c_str()));
450 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
454 wxRmdir(calendarDirectoryPath);
456 // Delete the calendar from the calendar data storage.
458 calendarData.DeleteCalendar(calendarEventInfo->calendarID);
460 delete calendarListFile;
461 calendarListFile = nullptr;
463 delete calendarEventInfo;
464 calendarEventInfo = nullptr;
468 void frmMain::CreateNewEvent( wxCommandEvent& event ){
470 frmEventEditor *frameNewEvent = new frmEventEditor ( this );
471 frameNewEvent->SetupForm(&calendarData, preferences);
472 frameNewEvent->Show();
476 void frmMain::EditEvent( wxCommandEvent& event ){
478 frmEventEditor *frameEditEvent = new frmEventEditor ( this );
479 frameEditEvent->SetEventID(event.GetInt());
480 frameEditEvent->SetEditMode(true);
481 frameEditEvent->SetupForm(&calendarData, preferences);
482 frameEditEvent->Show();
486 void frmMain::DeleteEvent( wxCommandEvent& event ){
488 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
490 // Get the event information.
492 CDSGetCalendarEntryInfo eventDeleteData = calendarData.GetEvent(eventInfo->eventID);
493 CDSGetCalendarInfo calendarDeleteData = calendarData.GetCalendar(eventInfo->calendarID);
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){
499 // Go through the grid and delete the entry from the GUI.
501 wxCommandEvent deleteEvent(XCCALENDARCTRL_DELETEENTRY);
502 deleteEvent.SetId(ID_DELETEENTRY);
503 deleteEvent.SetInt(eventInfo->eventID);
504 wxPostEvent(mainCalendarCtrl, deleteEvent);
506 // Get the filename and delete the entry.
508 wxRemoveFile(wxString(eventDeleteData.entryFilename.c_str()));
510 // Delete the entry from the calendar data storage.
512 calendarData.DeleteEvent(eventInfo->eventID);
519 void frmMain::AddEvent( wxCommandEvent& event )
522 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
524 wxCommandEvent addEvent(XCCALENDARCTRL_ADDENTRY);
525 addEvent.SetId(ID_ADDENTRY);
526 addEvent.SetClientData(eventInfo);
527 wxPostEvent(mainCalendarCtrl, addEvent);
531 void frmMain::UpdateEvent( wxCommandEvent& event )
534 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
536 wxCommandEvent updateEvent(XCCALENDARCTRL_UPDATEENTRY);
537 updateEvent.SetId(ID_UPDATEENTRY);
538 updateEvent.SetClientData(eventInfo);
539 wxPostEvent(mainCalendarCtrl, updateEvent);
543 void frmMain::ProcessCalendar( wxCommandEvent& event )
546 CalendarProperties *calendarInfo = (CalendarProperties*)event.GetClientData();
548 // Get the account name.
550 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo->accountName);
552 // Build the account directory path.
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 += "/";
561 // Generate a UUID for the new calendar.
563 string calendarPath = accountDirectoryPath;
565 string calendarUUID = "";
567 if (calendarInfo->editMode == false){
569 calendarUUID = GenerateUUID();
570 calendarPath += calendarUUID;
574 calendarUUID = calendarData.GetCalendar(calendarInfo->calendarID).calendarTextID;
575 calendarPath += calendarPath;
581 // Open the account's calendar file and add to the list of calendars.
583 string calendarListFilenameFull = accountDirectoryPath;
584 calendarListFilenameFull += "calendarlist.db";
586 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
588 wxString calendarDescription = wxString(calendarInfo->calendarDescription);
590 calendarDescription.Replace("\n", "\\n", true);
592 // Create the directory for the calendar entries if
593 // not editing a calendar.
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));
600 if (calendarInfo->editMode == false){
602 wxMkDir(wxString(calendarPath), wxS_DIR_DEFAULT);
603 calendarData.AddCalendar(accountInfo.accountID, calendarInfo->calendarName, calendarUUID, calendarInfo->calendarColour, calendarInfo->calendarDescription);
607 calendarData.UpdateCalendar(calendarInfo->calendarID, calendarInfo->calendarName, calendarInfo->calendarColour, calendarInfo->calendarDescription);
609 // Post event to update the colour in the calendar entries.
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;
617 wxCommandEvent updateColour(XCCALENDARCTRL_UPDATECALENDARCOLOUR);
618 updateColour.SetClientData(&updateColourData);
619 updateColour.SetId(ID_UPDATECOLOUR);
620 wxPostEvent(mainCalendarCtrl, updateColour);
624 // Add the calendar to the calendar data storage.
626 delete calendarListFile;
627 calendarListFile = nullptr;
630 calendarInfo = nullptr;
634 void frmMain::ShowHelp( wxCommandEvent& event )
637 wxMessageBox(_("The help documentation will be implemented in a future version of Xestia Calendar."), _("Unimplemented"));