// frmMain.h - frmMain form functions // // (c) 2016-2017 Xestia Software Development. // // This file is part of Xestia Calendar. // // Xestia Calendar is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by the // Free Software Foundation, version 3 of the license. // // Xestia Calendar is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with Xestia Calendar. If not, see #include "frmMain.h" wxDEFINE_EVENT(XCMAIN_PROCESSCALENDAR, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_EDITCALENDAR, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_DELETECALENDAR, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_ADDEVENT, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_UPDATEEVENT, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_EDITEVENT, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_DELETEEVENT, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_ADDWINDOWINFO, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_UPDATEWINDOWINFO, wxCommandEvent); wxDEFINE_EVENT(XCMAIN_DELETEWINDOWINFO, wxCommandEvent); frmMain::frmMain( wxWindow* parent ) : frmMainADT( parent ) { // Setup the default settings if they don't // exist. // Setup the preferences. wxString prefDirectory = GetUserPrefDir(); preferences = new XCALPreferences(prefDirectory); // Load the settings. wxString fullPrefPath; fullPrefPath.Append(prefDirectory); fullPrefPath.Append(wxT("settings")); wxFileConfig *settingfile = new wxFileConfig("", "", fullPrefPath); wxString ValueInc; settingfile->Read(wxT("SaveWindowPosition"), &ValueInc); if (ValueInc == wxT("true")){ wxRect windowPosition; long posX, posY, posH, posW = 0; bool posXValid, posYValid, posHValid, posWValid = false; posXValid = settingfile->Read(wxT("WindowPositionX"), &posX); posYValid = settingfile->Read(wxT("WindowPositionY"), &posY); posHValid = settingfile->Read(wxT("WindowPositionHeight"), &posH); posWValid = settingfile->Read(wxT("WindowPositionWidth"), &posW); if (posXValid == true && posYValid == true && posHValid == true && posWValid == true){ this->SetSize(posX, posY, posH, posW); } else { this->SetSize(-1, -1, 800, 600); } } // Load the account data. LoadAccountData(); // Setup the form control. mainCalendarCtrl = new XCCalendarCtrl(this, &calendarData); szrMain->Add(mainCalendarCtrl, 1, wxEXPAND, 5); szrMain->Layout(); // 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).mb_str()), 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).mb_str())); // Build the path. string calendarListFilename = string(GetUserDir().mb_str()); calendarListFilename += "accounts/"; calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).mb_str()); calendarListFilename += "."; calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).mb_str()); // 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).mb_str()), calendarList.calendarShortName[calendarSeek]); calendarList.calendarStorageID[calendarSeek] = calendarInfo.calendarID; // Find the entries and load each entry. string calendarListDirectory = calendarListFilename; calendarListDirectory += "/"; calendarListDirectory += calendarList.calendarShortName[calendarSeek]; calendarListDirectory += "/"; wxDir entryListDirectory(calendarListDirectory); wxString entryListFilename; bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*.ics", wxDIR_NO_FOLLOW|wxDIR_FILES); while (continueProcessing){ string entryListFullFilename; entryListFullFilename += calendarListDirectory; entryListFullFilename += string(entryListFilename.mb_str()); 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).mb_str()); accountDirectoryPath += "/"; string calendarListFilenameFull = accountDirectoryPath; calendarListFilenameFull += "calendarlist.db"; string calendarDirectoryPath = accountDirectoryPath; calendarDirectoryPath += calendarInfo.calendarTextID; wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull)); // 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(); } void frmMain::EditEvent( wxCommandEvent& event ){ frmEventEditor *frameEditEvent = new frmEventEditor ( this ); frameEditEvent->SetEventID(event.GetInt()); frameEditEvent->SetEditMode(true); frameEditEvent->SetWindowMenuItemID(++WindowMenuItemID); frameEditEvent->SetupForm(&calendarData, preferences); // Add the window to the window list. WindowData *newWindowData = new WindowData; newWindowData->DataType = 1; newWindowData->WindowPointer = (void*)frameEditEvent; newWindowData->WindowID = WindowMenuItemID; wxCommandEvent addevent(XCMAIN_ADDWINDOWINFO); addevent.SetId(ID_ADDWINDOW); addevent.SetClientData(newWindowData); wxPostEvent(this, addevent); // Setup the form and display it. frameEditEvent->Show(); } void frmMain::DeleteEvent( wxCommandEvent& event ){ EventProperties *eventInfo = (EventProperties*)event.GetClientData(); // Get the event information. CDSGetCalendarEntryInfo eventDeleteData = calendarData.GetEvent(eventInfo->eventID); CDSGetCalendarInfo calendarDeleteData = calendarData.GetCalendar(eventInfo->calendarID); 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){ return; } // Go through the grid and delete the entry from the GUI. wxCommandEvent deleteEvent(XCCALENDARCTRL_DELETEENTRY); deleteEvent.SetId(ID_DELETEENTRY); deleteEvent.SetInt(eventInfo->eventID); wxPostEvent(mainCalendarCtrl, deleteEvent); // Get the filename and delete the entry. wxRemoveFile(wxString(eventDeleteData.entryFilename.c_str())); // Delete the entry from the calendar data storage. calendarData.DeleteEvent(eventInfo->eventID); delete eventInfo; eventInfo = nullptr; } void frmMain::AddEvent( wxCommandEvent& event ) { EventProperties *eventInfo = (EventProperties*)event.GetClientData(); wxCommandEvent addEvent(XCCALENDARCTRL_ADDENTRY); addEvent.SetId(ID_ADDENTRY); addEvent.SetClientData(eventInfo); wxPostEvent(mainCalendarCtrl, addEvent); } void frmMain::UpdateEvent( wxCommandEvent& event ) { EventProperties *eventInfo = (EventProperties*)event.GetClientData(); wxCommandEvent updateEvent(XCCALENDARCTRL_UPDATEENTRY); updateEvent.SetId(ID_UPDATEENTRY); updateEvent.SetClientData(eventInfo); wxPostEvent(mainCalendarCtrl, updateEvent); } void frmMain::ProcessCalendar( wxCommandEvent& event ) { CalendarProperties *calendarInfo = (CalendarProperties*)event.GetClientData(); // Get the account name. CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo->accountName.c_str()); // Build the account directory path. string accountDirectoryPath = string(GetUserDir().mb_str()); accountDirectoryPath += "accounts/"; accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarInfo->accountPreferencesID).ToUTF8()); accountDirectoryPath += "."; accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarInfo->accountPreferencesID).mb_str()); accountDirectoryPath += "/"; // Generate a UUID for the new calendar. string calendarPath = accountDirectoryPath; string calendarUUID = ""; if (calendarInfo->editMode == false){ calendarUUID = GenerateUUID(); calendarPath += calendarUUID; } else { calendarUUID = calendarData.GetCalendar(calendarInfo->calendarID).calendarTextID; calendarPath += calendarPath; } calendarPath += "/"; // Open the account's calendar file and add to the list of calendars. string calendarListFilenameFull = accountDirectoryPath; calendarListFilenameFull += "calendarlist.db"; wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull.c_str(), wxConvUTF8)); wxString calendarDescription = wxString(calendarInfo->calendarDescription.c_str(), wxConvUTF8); calendarDescription.Replace("\n", "\\n", true); // Create the directory for the calendar entries if // not editing a calendar. calendarListFile->SetPath(wxString(calendarUUID)); calendarListFile->Write(wxT("name"), wxString(calendarInfo->calendarName.c_str(), wxConvUTF8)); calendarListFile->Write(wxT("description"), calendarDescription); calendarListFile->Write(wxT("colour"), wxString(calendarInfo->calendarColour)); if (calendarInfo->editMode == false){ wxMkDir(wxString(calendarPath.c_str(), wxConvUTF8), wxS_DIR_DEFAULT); calendarData.AddCalendar(accountInfo.accountID, calendarInfo->calendarName, calendarUUID, calendarInfo->calendarColour, calendarInfo->calendarDescription); } else { calendarData.UpdateCalendar(calendarInfo->calendarID, calendarInfo->calendarName, calendarInfo->calendarColour, calendarInfo->calendarDescription); // Post event to update the colour in the calendar entries. updateColourData.calendarID = calendarInfo->calendarID; updateColourData.newColour.red = calendarInfo->calendarColour.red; updateColourData.newColour.green = calendarInfo->calendarColour.green; updateColourData.newColour.blue = calendarInfo->calendarColour.blue; updateColourData.newColour.alpha = calendarInfo->calendarColour.alpha; wxCommandEvent updateColour(XCCALENDARCTRL_UPDATECALENDARCOLOUR); updateColour.SetClientData(&updateColourData); updateColour.SetId(ID_UPDATECOLOUR); wxPostEvent(mainCalendarCtrl, updateColour); } // Add the calendar to the calendar data storage. delete calendarListFile; calendarListFile = nullptr; delete calendarInfo; calendarInfo = nullptr; } void frmMain::ShowHelp( wxCommandEvent& event ) { wxMessageBox(_("The help documentation will be implemented in a future version of Xestia Calendar."), _("Unimplemented")); }