X-Git-Url: http://Server1/repobrowser/?p=xestiacalendar%2F.git;a=blobdiff_plain;f=source%2Fforms%2Fmain%2FfrmMain.cpp;h=f7e2c1fa85426040f0cca258ee8439833c7c5e05;hp=c46a8ae915544b35963f7a513ee7d456fb0c907e;hb=49789773e2797383c866dca470fb99e80cb49e41;hpb=285af7a588967bcc5c4565125c37f5f296040556 diff --git a/source/forms/main/frmMain.cpp b/source/forms/main/frmMain.cpp index c46a8ae..f7e2c1f 100644 --- a/source/forms/main/frmMain.cpp +++ b/source/forms/main/frmMain.cpp @@ -1,12 +1,39 @@ +// 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 ) { - - mainCalendarCtrl = new XCCalendarCtrl(this, &calendarData); - // Setup the default settings if they don't // exist. @@ -14,9 +41,6 @@ frmMainADT( parent ) wxString prefDirectory = GetUserPrefDir(); preferences = new XCALPreferences(prefDirectory); - szrMain->Add(mainCalendarCtrl, 1, wxEXPAND, 5); - szrMain->Layout(); - // Load the settings. wxString fullPrefPath; @@ -53,9 +77,30 @@ frmMainADT( parent ) } } + + // 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 ) @@ -154,7 +199,7 @@ void frmMain::LoadAccountData(){ for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){ - CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str())); + CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).ToUTF8()), accountSeek); } @@ -162,15 +207,15 @@ void frmMain::LoadAccountData(){ for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){ - CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str())); + CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).ToUTF8())); // Build the path. - string calendarListFilename = string(GetUserDir().mb_str()); + string calendarListFilename = string(GetUserDir().ToUTF8()); calendarListFilename += "accounts/"; - calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).mb_str()); + calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).ToUTF8()); calendarListFilename += "."; - calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).mb_str()); + calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).ToUTF8()); // Get the list of calendars. @@ -185,15 +230,484 @@ void frmMain::LoadAccountData(){ CDSCalendarResult calendarAddResult = calendarData.AddCalendar(accountInfo.accountID, calendarList.calendarName[calendarSeek], calendarList.calendarShortName[calendarSeek], - calendarList.calendarColour[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]); + 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(); + +} + +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?", wxString(eventDeleteData.entryName.c_str(), wxConvUTF8), wxString(calendarDeleteData.calendarName.c_str(), wxConvUTF8)), "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(), wxConvUTF8)); + + // 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).ToUTF8()); + 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")); + } \ No newline at end of file