+
+ // Bind events to the control.
+
+ Bind(XCMAIN_PROCESSCALENDAR, &frmMain::ProcessCalendar, this, ID_PROCESSCALENDAR);
+ Bind(XCMAIN_EDITCALENDAR, &frmMain::EditCalendar, this, ID_EDITCALENDAR);
+ Bind(XCMAIN_DELETECALENDAR, &frmMain::DeleteCalendar, this, ID_DELETECALENDAR);
+ Bind(XCMAIN_ADDEVENT, &frmMain::AddEvent, this, ID_ADDENTRY);
+ Bind(XCMAIN_UPDATEEVENT, &frmMain::UpdateEvent, this, ID_UPDATEENTRY);
+ Bind(XCMAIN_EDITEVENT, &frmMain::EditEvent, this, ID_EDITEVENT);
+ Bind(XCMAIN_DELETEEVENT, &frmMain::DeleteEvent, this, ID_DELETEEVENT);
+
+ Bind(XCMAIN_ADDWINDOWINFO, &frmMain::WindowAdd, this, ID_ADDWINDOW);
+ Bind(XCMAIN_UPDATEWINDOWINFO, &frmMain::WindowUpdate, this, ID_UPDATEWINDOW);
+ Bind(XCMAIN_DELETEWINDOWINFO, &frmMain::WindowDelete, this, ID_DELETEWINDOW);
+}
+
+void frmMain::QuitApp( wxCloseEvent& event )
+{
+
+ // Run the QuitApp function.
+
+ QuitApp();
+
+}
+
+void frmMain::QuitApp( wxCommandEvent& event )
+{
+
+ // Run the QuitApp function.
+
+ QuitApp();
+
+}
+
+void frmMain::QuitApp()
+{
+
+ // Function to run when quitting.
+
+ //Go through the windows and close each one (be it search
+ //or contact editor). Abort if user wants to cancel.
+
+ // Close the contact editor windows.
+
+ // Close the contact windows.
+
+ // Close the search windows.
+
+ // Write out the ETag databases.
+
+ // Save Preferences: Save the window position if that option is enabled.
+
+ wxString SetFilename = GetUserPrefDir();
+
+#if defined(__HAIKU__)
+
+
+
+#elif defined(__WIN32__)
+
+ SetFilename.Append(wxT("settings"));
+
+#else
+
+ // *nix OSes
+
+ SetFilename.Append(wxT("settings"));
+
+#endif
+
+ wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
+
+ bool SaveWindowPos = false;
+ wxString SaveWindowInc;
+ cfgfile->Read(wxT("SaveWindowPosition"), &SaveWindowInc);
+
+ if (SaveWindowInc == wxT("true")){
+
+ SaveWindowPos = true;
+
+ }
+
+ if (SaveWindowPos == true){
+
+ wxRect frmMainPos = this->GetRect();
+
+ cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
+ cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
+ cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
+ cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
+
+ }
+
+ delete cfgfile;
+ cfgfile = nullptr;
+
+ //Everything closed... exit.
+
+ std::exit(0);
+
+ Close();
+
+}
+
+void frmMain::LoadAccountData(){
+
+ // Get the list of accounts and put into the calendar data storage.
+
+ int accountCount = preferences->accounts.GetCount();
+
+ for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
+
+ CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).ToUTF8()), accountSeek);
+
+ }
+
+ // Get the list of calendars and put them into the calendar data storage.
+
+ for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
+
+ CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).ToUTF8()));
+
+ // Build the path.
+
+ string calendarListFilename = string(GetUserDir().ToUTF8());
+ calendarListFilename += "accounts/";
+ calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).ToUTF8());
+ calendarListFilename += ".";
+ calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).ToUTF8());
+
+ // Get the list of calendars.
+
+ XCAccountCalendarList calendarList(calendarListFilename);
+
+ // Add the calendar and set the calendar ID for it.
+
+ for (int calendarSeek = 0; calendarSeek < calendarList.calendarShortName.size(); calendarSeek++){
+
+ // Add the calendar.
+
+ CDSCalendarResult calendarAddResult = calendarData.AddCalendar(accountInfo.accountID,
+ calendarList.calendarName[calendarSeek],
+ calendarList.calendarShortName[calendarSeek],
+ calendarList.calendarColour[calendarSeek],
+ calendarList.calendarDescription[calendarSeek]);
+
+ // Set the calendar ID.
+
+ CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(string(preferences->accounts.GetAccountName(accountSeek).ToUTF8()), calendarList.calendarShortName[calendarSeek]);
+ calendarList.calendarStorageID[calendarSeek] = calendarInfo.calendarID;
+
+ // Find the entries and load each entry.
+
+ wxString calendarListDirectory = wxString(calendarListFilename.c_str(), wxConvUTF8);
+ calendarListDirectory += "/";
+ calendarListDirectory += wxString(calendarList.calendarShortName[calendarSeek].c_str(), wxConvUTF8);
+ calendarListDirectory += "/";
+
+ wxDir entryListDirectory(calendarListDirectory);
+ wxString entryListFilename;
+
+ bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*.ics", wxDIR_NO_FOLLOW|wxDIR_FILES);
+
+ while (continueProcessing){
+
+ string entryListFullFilename;
+ entryListFullFilename += string(calendarListDirectory.ToUTF8());
+ entryListFullFilename += string(entryListFilename.ToUTF8());
+
+ continueProcessing = entryListDirectory.GetNext(&entryListFilename);
+ CDSAddEntryResult addEventResult = calendarData.AddEvent(calendarInfo.calendarID, entryListFullFilename);
+
+ }
+
+ }
+
+ }
+
+}
+
+void frmMain::ShowPreferencesWindow( wxCommandEvent& event )
+{
+
+ // Close all windows first.
+
+ if (CloseAllWindows() == false)
+ {
+ return;
+ }
+
+ // Open the preferences window.
+
+ reloadAccounts = FALSE;
+
+ frmPreferences *framePreferences = new frmPreferences ( this );
+ framePreferences->SetupPointers(&reloadAccounts);
+ framePreferences->ShowModal();
+ delete framePreferences;
+ framePreferences = NULL;
+
+ // Reload the preferences.
+
+ if (reloadAccounts == true){
+
+ // Reload the accounts as a change has been made within
+ // the application.
+
+ wxString prefDirectory = GetUserPrefDir();
+ XCALPreferences *oldPreferences = preferences;
+ preferences = new XCALPreferences(prefDirectory);
+
+ delete oldPreferences;
+ oldPreferences = nullptr;
+
+ // Clear all of the data from the calendar data storage.
+
+ calendarData.Clear();
+ LoadAccountData();
+
+ // Politely ask the calendar control to reload it's view.
+
+ wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
+ updateGrid.SetId(ID_CHANGEGRID);
+ wxPostEvent(mainCalendarCtrl, updateGrid);
+
+ }
+
+}
+
+void frmMain::ShowAboutWindow( wxCommandEvent& event )
+{
+
+ // Show the about window.
+
+ frmAbout *frameAbout = new frmAbout ( this );
+ frameAbout->SetupAboutWindow();
+ frameAbout->ShowModal();
+ delete frameAbout;
+ frameAbout = NULL;
+
+}
+
+void frmMain::ShowUpdateWindow( wxCommandEvent& event )
+{
+
+ frmUpdate *frameUpdate = new frmUpdate ( this );
+ frameUpdate->FetchData();
+ frameUpdate->ShowModal();
+ delete frameUpdate;
+ frameUpdate = NULL;
+
+}
+
+void frmMain::OpenNewAccountDialog( wxCommandEvent& event )
+{
+
+ // Open the new account dialog.
+
+ reloadAccounts = false;
+
+ frmNewAccount *frameNewAccount = new frmNewAccount ( this );
+ frameNewAccount->SetupPointers(&reloadAccounts, &calendarData);
+ frameNewAccount->ShowModal();
+ delete frameNewAccount;
+ frameNewAccount = NULL;
+
+ // Reload the preferences.
+
+ if (reloadAccounts == true){
+
+ // Reload the accounts as a change has been made within
+ // the application.
+
+ wxString prefDirectory = GetUserPrefDir();
+ XCALPreferences *oldPreferences = preferences;
+ preferences = new XCALPreferences(prefDirectory);
+
+ delete oldPreferences;
+ oldPreferences = nullptr;
+
+ // Clear all of the data from the calendar data storage.
+
+ calendarData.Clear();
+ LoadAccountData();
+
+ // Politely ask the calendar control to reload it's view.
+
+ wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
+ updateGrid.SetId(ID_CHANGEGRID);
+ wxPostEvent(mainCalendarCtrl, updateGrid);
+
+ }
+
+}
+
+void frmMain::CreateNewCalendar( wxCommandEvent& event )
+{
+
+ frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
+ frameNewCalendar->SetMode(false);
+ frameNewCalendar->SetupAccounts(preferences);
+ frameNewCalendar->ShowModal();
+ delete frameNewCalendar;
+ frameNewCalendar = nullptr;
+
+}
+
+void frmMain::EditCalendar( wxCommandEvent& event )
+{
+
+ // Close all windows first.
+
+ if (CloseAllWindows() == false)
+ {
+ return;
+ }
+
+ // Get the calendar data.
+
+ CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(event.GetInt());
+
+ frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
+ frameNewCalendar->SetMode(true);
+ frameNewCalendar->SetupAccounts(preferences);
+ frameNewCalendar->SetData(event.GetInt(), calendarInfo.accountName, calendarInfo.calendarName, calendarInfo.calendarDescription, calendarInfo.calendarColour);
+ frameNewCalendar->ShowModal();
+ delete frameNewCalendar;
+ frameNewCalendar = nullptr;
+
+}
+
+void frmMain::DeleteCalendar( wxCommandEvent& event )
+{
+
+ // Close all windows first.
+
+ if (CloseAllWindows() == false)
+ {
+ return;
+ }
+
+ CalendarProperties *calendarEventInfo = (CalendarProperties*)event.GetClientData();
+
+ // Get the calendar data.
+
+ CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(calendarEventInfo->calendarID);
+
+ 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){
+ return;
+ }
+
+ // Go through the grid and delete each calendar entry widget that
+ // is associated with the calendar.
+
+ wxCommandEvent deleteCalendar(XCCALENDARCTRL_DELETECALENDARENTRIES);
+ deleteCalendar.SetId(ID_DELETECALENDARENTRIES);
+ deleteCalendar.SetInt(calendarInfo.calendarID);
+ wxPostEvent(mainCalendarCtrl, deleteCalendar);
+
+ // Get the account configuration file and delete the calendar information.
+
+ CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo.accountName.c_str());
+
+ string accountDirectoryPath = string(GetUserDir().mb_str());
+ accountDirectoryPath += "accounts/";
+ accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarEventInfo->accountPreferencesID).ToUTF8());
+ accountDirectoryPath += ".";
+ accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarEventInfo->accountPreferencesID).ToUTF8());
+ accountDirectoryPath += "/";
+
+ string calendarListFilenameFull = accountDirectoryPath;
+ calendarListFilenameFull += "calendarlist.db";
+
+ string calendarDirectoryPath = accountDirectoryPath;
+ calendarDirectoryPath += calendarInfo.calendarTextID;
+
+ wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull.c_str(), wxConvUTF8));
+
+ // Delete the calendar directory.
+
+ wxDir entryListDirectory(wxString(calendarDirectoryPath.c_str(), wxConvUTF8));
+ wxString entryListFilename;
+
+ bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*", wxDIR_NO_FOLLOW|wxDIR_FILES);
+
+ while (continueProcessing){
+
+ string entryListFullFilename;
+ entryListFullFilename += wxString(calendarDirectoryPath.c_str(), wxConvUTF8);
+ entryListFullFilename += "/";
+ entryListFullFilename += string(entryListFilename.mb_str());
+
+ wxRemoveFile(wxString(entryListFullFilename.c_str(), wxConvUTF8));
+
+ continueProcessing = entryListDirectory.GetNext(&entryListFilename);
+
+ }
+
+ entryListDirectory.Close();
+
+ // Delete the calendar from the account calendar list.
+
+ calendarListFile->DeleteGroup(wxString(calendarInfo.calendarTextID));
+
+ // Delete the calendar from the calendar data storage.
+
+ calendarData.DeleteCalendar(calendarEventInfo->calendarID);
+
+ // Delete the calendar directory.
+
+ wxRmDir(wxString(calendarDirectoryPath.c_str(), wxConvUTF8));
+
+ delete calendarListFile;
+ calendarListFile = nullptr;
+
+ delete calendarEventInfo;
+ calendarEventInfo = nullptr;
+
+}
+
+void frmMain::CreateNewEvent( wxCommandEvent& event ){
+
+ frmEventEditor *frameNewEvent = new frmEventEditor ( this );
+ frameNewEvent->SetupForm(&calendarData, preferences);
+ frameNewEvent->SetWindowMenuItemID(++WindowMenuItemID);
+
+ // Add the window to the window list.
+
+ WindowData *newWindowData = new WindowData;
+
+ newWindowData->DataType = 1;
+ newWindowData->WindowPointer = (void*)frameNewEvent;
+ newWindowData->WindowID = WindowMenuItemID;
+
+ wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO);
+ addevent.SetId(ID_ADDWINDOW);
+ addevent.SetClientData(newWindowData);
+ wxPostEvent(this, addevent);
+
+ frameNewEvent->Show();