3 frmMain::frmMain( wxWindow* parent )
8 // Setup the default settings if they don't
11 // Setup the preferences.
12 wxString prefDirectory = GetUserPrefDir();
13 preferences = new XCALPreferences(prefDirectory);
17 wxString fullPrefPath;
19 fullPrefPath.Append(prefDirectory);
20 fullPrefPath.Append(wxT("settings"));
22 wxFileConfig *settingfile = new wxFileConfig("", "", fullPrefPath);
25 settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
27 if (ValueInc == wxT("true")){
29 wxRect windowPosition;
31 long posX, posY, posH, posW = 0;
33 bool posXValid, posYValid, posHValid, posWValid = false;
35 posXValid = settingfile->Read(wxT("WindowPositionX"), &posX);
36 posYValid = settingfile->Read(wxT("WindowPositionY"), &posY);
37 posHValid = settingfile->Read(wxT("WindowPositionHeight"), &posH);
38 posWValid = settingfile->Read(wxT("WindowPositionWidth"), &posW);
40 if (posXValid == true && posYValid == true && posHValid == true && posWValid == true){
42 this->SetSize(posX, posY, posH, posW);
46 this->SetSize(-1, -1, 800, 600);
52 // Load the account data.
56 // Setup the form control.
58 mainCalendarCtrl = new XCCalendarCtrl(this, &calendarData);
59 szrMain->Add(mainCalendarCtrl, 1, wxEXPAND, 5);
62 Connect(ID_PROCESSCALENDAR, XCMAIN_PROCESSCALENDAR, wxCommandEventHandler(frmMain::ProcessCalendar));
63 Connect(ID_EDITCALENDAR, XCMAIN_EDITCALENDAR, wxCommandEventHandler(frmMain::EditCalendar));
64 Connect(ID_DELETECALENDAR, XCMAIN_DELETECALENDAR, wxCommandEventHandler(frmMain::DeleteCalendar));
65 Connect(ID_DELETEEVENT, XCMAIN_DELETEEVENT, wxCommandEventHandler(frmMain::DeleteEvent));
66 Connect(ID_ADDENTRY, XCMAIN_ADDEVENT, wxCommandEventHandler(frmMain::AddEvent));
67 Connect(ID_UPDATEENTRY, XCMAIN_UPDATEEVENT, wxCommandEventHandler(frmMain::UpdateEvent));
68 Connect(ID_EDITEVENT, XCMAIN_EDITEVENT, wxCommandEventHandler(frmMain::EditEvent));
72 void frmMain::QuitApp( wxCloseEvent& event )
75 // Run the QuitApp function.
81 void frmMain::QuitApp( wxCommandEvent& event )
84 // Run the QuitApp function.
90 void frmMain::QuitApp()
93 // Function to run when quitting.
95 //Go through the windows and close each one (be it search
96 //or contact editor). Abort if user wants to cancel.
98 // Close the contact editor windows.
100 // Close the contact windows.
102 // Close the search windows.
104 // Write out the ETag databases.
106 // Save Preferences: Save the window position if that option is enabled.
108 wxString SetFilename = GetUserPrefDir();
110 #if defined(__HAIKU__)
114 #elif defined(__WIN32__)
116 SetFilename.Append(wxT("settings"));
122 SetFilename.Append(wxT("settings"));
126 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
128 bool SaveWindowPos = false;
129 wxString SaveWindowInc;
130 cfgfile->Read(wxT("SaveWindowPosition"), &SaveWindowInc);
132 if (SaveWindowInc == wxT("true")){
134 SaveWindowPos = true;
138 if (SaveWindowPos == true){
140 wxRect frmMainPos = this->GetRect();
142 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
143 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
144 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
145 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
152 //Everything closed... exit.
160 void frmMain::LoadAccountData(){
162 // Get the list of accounts and put into the calendar data storage.
164 int accountCount = preferences->accounts.GetCount();
166 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
168 CDSAccountResult addResult = calendarData.AddAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), accountSeek);
172 // Get the list of calendars and put them into the calendar data storage.
174 for (int accountSeek = 0; accountSeek < accountCount; accountSeek++){
176 CDSGetAccountInfo accountInfo = calendarData.GetAccount(string(preferences->accounts.GetAccountName(accountSeek).mb_str()));
180 string calendarListFilename = string(GetUserDir().mb_str());
181 calendarListFilename += "accounts/";
182 calendarListFilename += string(preferences->accounts.GetAccountDirectory(accountSeek).mb_str());
183 calendarListFilename += ".";
184 calendarListFilename += string(preferences->accounts.GetAccountType(accountSeek).mb_str());
186 // Get the list of calendars.
188 XCAccountCalendarList calendarList(calendarListFilename);
190 // Add the calendar and set the calendar ID for it.
192 for (int calendarSeek = 0; calendarSeek < calendarList.calendarShortName.size(); calendarSeek++){
196 CDSCalendarResult calendarAddResult = calendarData.AddCalendar(accountInfo.accountID,
197 calendarList.calendarName[calendarSeek],
198 calendarList.calendarShortName[calendarSeek],
199 calendarList.calendarColour[calendarSeek],
200 calendarList.calendarDescription[calendarSeek]);
202 // Set the calendar ID.
204 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(string(preferences->accounts.GetAccountName(accountSeek).mb_str()), calendarList.calendarShortName[calendarSeek]);
205 calendarList.calendarStorageID[calendarSeek] = calendarInfo.calendarID;
207 // Find the entries and load each entry.
209 string calendarListDirectory = calendarListFilename;
210 calendarListDirectory += "/";
211 calendarListDirectory += calendarList.calendarShortName[calendarSeek];
212 calendarListDirectory += "/";
214 wxDir entryListDirectory(calendarListDirectory);
215 wxString entryListFilename;
217 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*.ics", wxDIR_NO_FOLLOW|wxDIR_FILES);
219 while (continueProcessing){
221 string entryListFullFilename;
222 entryListFullFilename += calendarListDirectory;
223 entryListFullFilename += string(entryListFilename.mb_str());
225 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
226 CDSAddEntryResult addEventResult = calendarData.AddEvent(calendarInfo.calendarID, entryListFullFilename);
236 void frmMain::ShowPreferencesWindow( wxCommandEvent& event )
239 // Open the preferences window.
241 reloadAccounts = FALSE;
243 frmPreferences *framePreferences = new frmPreferences ( this );
244 framePreferences->SetupPointers(&reloadAccounts);
245 framePreferences->ShowModal();
246 delete framePreferences;
247 framePreferences = NULL;
249 // Reload the preferences.
251 if (reloadAccounts == true){
253 // Reload the accounts as a change has been made within
256 wxString prefDirectory = GetUserPrefDir();
257 XCALPreferences *oldPreferences = preferences;
258 preferences = new XCALPreferences(prefDirectory);
260 delete oldPreferences;
261 oldPreferences = nullptr;
263 // Clear all of the data from the calendar data storage.
265 calendarData.Clear();
268 // Politely ask the calendar control to reload it's view.
270 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
271 updateGrid.SetId(ID_CHANGEGRID);
272 wxPostEvent(mainCalendarCtrl, updateGrid);
278 void frmMain::ShowAboutWindow( wxCommandEvent& event )
281 // Show the about window.
283 frmAbout *frameAbout = new frmAbout ( this );
284 frameAbout->SetupAboutWindow();
285 frameAbout->ShowModal();
291 void frmMain::ShowUpdateWindow( wxCommandEvent& event )
294 frmUpdate *frameUpdate = new frmUpdate ( this );
295 frameUpdate->FetchData();
296 frameUpdate->ShowModal();
302 void frmMain::OpenNewAccountDialog( wxCommandEvent& event )
305 // Open the new account dialog.
307 reloadAccounts = false;
309 frmNewAccount *frameNewAccount = new frmNewAccount ( this );
310 frameNewAccount->SetupPointers(&reloadAccounts, &calendarData);
311 frameNewAccount->ShowModal();
312 delete frameNewAccount;
313 frameNewAccount = NULL;
315 // Reload the preferences.
317 if (reloadAccounts == true){
319 // Reload the accounts as a change has been made within
322 wxString prefDirectory = GetUserPrefDir();
323 XCALPreferences *oldPreferences = preferences;
324 preferences = new XCALPreferences(prefDirectory);
326 delete oldPreferences;
327 oldPreferences = nullptr;
329 // Clear all of the data from the calendar data storage.
331 calendarData.Clear();
334 // Politely ask the calendar control to reload it's view.
336 wxCommandEvent updateGrid(XCCALENDARCTRL_CHANGEGRID);
337 updateGrid.SetId(ID_CHANGEGRID);
338 wxPostEvent(mainCalendarCtrl, updateGrid);
344 void frmMain::CreateNewCalendar( wxCommandEvent& event )
347 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
348 frameNewCalendar->SetMode(false);
349 frameNewCalendar->SetupAccounts(preferences);
350 frameNewCalendar->ShowModal();
351 delete frameNewCalendar;
352 frameNewCalendar = nullptr;
356 void frmMain::EditCalendar( wxCommandEvent& event )
359 // Get the calendar data.
361 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(event.GetInt());
363 frmCalendarEditor *frameNewCalendar = new frmCalendarEditor ( this );
364 frameNewCalendar->SetMode(true);
365 frameNewCalendar->SetupAccounts(preferences);
366 frameNewCalendar->SetData(event.GetInt(), calendarInfo.accountName, calendarInfo.calendarName, calendarInfo.calendarDescription, calendarInfo.calendarColour);
367 frameNewCalendar->ShowModal();
368 delete frameNewCalendar;
369 frameNewCalendar = nullptr;
373 void frmMain::DeleteCalendar( wxCommandEvent& event )
376 CalendarProperties *calendarEventInfo = (CalendarProperties*)event.GetClientData();
378 // Get the calendar data.
380 CDSGetCalendarInfo calendarInfo = calendarData.GetCalendar(calendarEventInfo->calendarID);
382 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){
386 // Go through the grid and delete each calendar entry widget that
387 // is associated with the calendar.
389 wxCommandEvent deleteCalendar(XCCALENDARCTRL_DELETECALENDARENTRIES);
390 deleteCalendar.SetId(ID_DELETECALENDARENTRIES);
391 deleteCalendar.SetInt(calendarInfo.calendarID);
392 wxPostEvent(mainCalendarCtrl, deleteCalendar);
394 // Get the account configuration file and delete the calendar information.
396 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo.accountName);
398 string accountDirectoryPath = string(GetUserDir().mb_str());
399 accountDirectoryPath += "accounts/";
400 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarEventInfo->accountPreferencesID).mb_str());
401 accountDirectoryPath += ".";
402 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarEventInfo->accountPreferencesID).mb_str());
403 accountDirectoryPath += "/";
405 string calendarListFilenameFull = accountDirectoryPath;
406 calendarListFilenameFull += "calendarlist.db";
408 string calendarDirectoryPath = accountDirectoryPath;
409 calendarDirectoryPath += calendarInfo.calendarTextID;
411 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
413 //calendarListFile->SetPath(wxString(calendarInfo.calendarTextID));
414 calendarListFile->DeleteGroup(wxString(calendarInfo.calendarTextID));
416 // Delete the calendar directory.
418 wxDir entryListDirectory((wxString)calendarDirectoryPath.c_str());
419 wxString entryListFilename;
421 bool continueProcessing = entryListDirectory.GetFirst(&entryListFilename, "*", wxDIR_NO_FOLLOW|wxDIR_FILES);
423 while (continueProcessing){
425 string entryListFullFilename;
426 entryListFullFilename += calendarDirectoryPath;
427 entryListFullFilename += "/";
428 entryListFullFilename += string(entryListFilename.mb_str());
430 wxRemoveFile(wxString(entryListFullFilename.c_str()));
432 continueProcessing = entryListDirectory.GetNext(&entryListFilename);
436 wxRmdir(calendarDirectoryPath);
438 // Delete the calendar from the calendar data storage.
440 calendarData.DeleteCalendar(calendarEventInfo->calendarID);
442 delete calendarListFile;
443 calendarListFile = nullptr;
445 delete calendarEventInfo;
446 calendarEventInfo = nullptr;
450 void frmMain::CreateNewEvent( wxCommandEvent& event ){
452 frmEventEditor *frameNewEvent = new frmEventEditor ( this );
453 frameNewEvent->SetupForm(&calendarData, preferences);
454 frameNewEvent->Show();
458 void frmMain::EditEvent( wxCommandEvent& event ){
460 frmEventEditor *frameEditEvent = new frmEventEditor ( this );
461 frameEditEvent->SetEventID(event.GetInt());
462 frameEditEvent->SetEditMode(true);
463 frameEditEvent->SetupForm(&calendarData, preferences);
464 frameEditEvent->Show();
468 void frmMain::DeleteEvent( wxCommandEvent& event ){
470 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
472 // Get the event information.
474 CDSGetCalendarEntryInfo eventDeleteData = calendarData.GetEvent(eventInfo->eventID);
475 CDSGetCalendarInfo calendarDeleteData = calendarData.GetCalendar(eventInfo->calendarID);
477 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){
481 // Go through the grid and delete the entry from the GUI.
483 wxCommandEvent deleteEvent(XCCALENDARCTRL_DELETEENTRY);
484 deleteEvent.SetId(ID_DELETEENTRY);
485 deleteEvent.SetInt(eventInfo->eventID);
486 wxPostEvent(mainCalendarCtrl, deleteEvent);
488 // Get the filename and delete the entry.
490 wxRemoveFile(wxString(eventDeleteData.entryFilename.c_str()));
492 // Delete the entry from the calendar data storage.
494 calendarData.DeleteEvent(eventInfo->eventID);
501 void frmMain::AddEvent( wxCommandEvent& event )
504 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
506 wxCommandEvent addEvent(XCCALENDARCTRL_ADDENTRY);
507 addEvent.SetId(ID_ADDENTRY);
508 addEvent.SetClientData(eventInfo);
509 wxPostEvent(mainCalendarCtrl, addEvent);
513 void frmMain::UpdateEvent( wxCommandEvent& event )
516 EventProperties *eventInfo = (EventProperties*)event.GetClientData();
518 wxCommandEvent updateEvent(XCCALENDARCTRL_UPDATEENTRY);
519 updateEvent.SetId(ID_UPDATEENTRY);
520 updateEvent.SetClientData(eventInfo);
521 wxPostEvent(mainCalendarCtrl, updateEvent);
525 void frmMain::ProcessCalendar( wxCommandEvent& event )
528 CalendarProperties *calendarInfo = (CalendarProperties*)event.GetClientData();
530 // Get the account name.
532 CDSGetAccountInfo accountInfo = calendarData.GetAccount(calendarInfo->accountName);
534 // Build the account directory path.
536 string accountDirectoryPath = string(GetUserDir().mb_str());
537 accountDirectoryPath += "accounts/";
538 accountDirectoryPath += string(preferences->accounts.GetAccountDirectory(calendarInfo->accountPreferencesID).mb_str());
539 accountDirectoryPath += ".";
540 accountDirectoryPath += string(preferences->accounts.GetAccountType(calendarInfo->accountPreferencesID).mb_str());
541 accountDirectoryPath += "/";
543 // Generate a UUID for the new calendar.
545 string calendarPath = accountDirectoryPath;
547 string calendarUUID = "";
549 if (calendarInfo->editMode == false){
551 calendarUUID = GenerateUUID();
552 calendarPath += calendarUUID;
556 calendarUUID = calendarData.GetCalendar(calendarInfo->calendarID).calendarTextID;
557 calendarPath += calendarPath;
563 // Open the account's calendar file and add to the list of calendars.
565 string calendarListFilenameFull = accountDirectoryPath;
566 calendarListFilenameFull += "calendarlist.db";
568 wxFileConfig *calendarListFile = new wxFileConfig("", "", wxString(calendarListFilenameFull));
570 wxString calendarDescription = wxString(calendarInfo->calendarDescription);
572 calendarDescription.Replace("\n", "\\n", true);
574 // Create the directory for the calendar entries if
575 // not editing a calendar.
577 calendarListFile->SetPath(wxString(calendarUUID));
578 calendarListFile->Write(wxT("name"), wxString(calendarInfo->calendarName));
579 calendarListFile->Write(wxT("description"), calendarDescription);
580 calendarListFile->Write(wxT("colour"), wxString(calendarInfo->calendarColour));
582 if (calendarInfo->editMode == false){
584 wxMkDir(wxString(calendarPath), wxS_DIR_DEFAULT);
585 calendarData.AddCalendar(accountInfo.accountID, calendarInfo->calendarName, calendarUUID, calendarInfo->calendarColour, calendarInfo->calendarDescription);
589 calendarData.UpdateCalendar(calendarInfo->calendarID, calendarInfo->calendarName, calendarInfo->calendarColour, calendarInfo->calendarDescription);
591 // Post event to update the colour in the calendar entries.
593 updateColourData.calendarID = calendarInfo->calendarID;
594 updateColourData.newColour.red = calendarInfo->calendarColour.red;
595 updateColourData.newColour.green = calendarInfo->calendarColour.green;
596 updateColourData.newColour.blue = calendarInfo->calendarColour.blue;
597 updateColourData.newColour.alpha = calendarInfo->calendarColour.alpha;
599 wxCommandEvent updateColour(XCCALENDARCTRL_UPDATECALENDARCOLOUR);
600 updateColour.SetClientData(&updateColourData);
601 updateColour.SetId(ID_UPDATECOLOUR);
602 wxPostEvent(mainCalendarCtrl, updateColour);
606 // Add the calendar to the calendar data storage.
608 delete calendarListFile;
609 calendarListFile = nullptr;
612 calendarInfo = nullptr;
616 void frmMain::ShowHelp( wxCommandEvent& event )
619 wxMessageBox(_("The help documentation will be implemented in a future version of Xestia Calendar."), _("Unimplemented"));