1 // frmPreferences.cpp - frmPreferences form functions
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/>
19 #include "frmPreferences.h"
21 frmPreferences::frmPreferences( wxWindow* parent )
23 frmPreferencesADT( parent )
26 wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png));
27 wxMemoryInputStream cstream(icons_accunsupported_png, sizeof(icons_accunsupported_png));
29 wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
30 wxBitmap accNIcon(icons_acclocal_png, -1);
32 wxIAccNIcon.CopyFromBitmap(accNIcon);
34 wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
35 wxBitmap accUIcon(icons_accunsupported_png, -1);
37 wxIAccUIcon.CopyFromBitmap(accUIcon);
39 accountID = accImgList->Add(wxIAccNIcon);
40 accountUnsupportedID = accImgList->Add(wxIAccUIcon);
42 NbtPreferences->RemovePage(1);
43 btnAccountAdd->Show(false);
47 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
50 // Setup the preferences filename string. Default is the
51 // *nix systems one (/home/$USER/.xestiacal/preferences)
53 prefFilename = GetUserPrefDir();
55 preferences = new XCALPreferences(prefFilename);
57 // Setup the General Tab.
59 bool saveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
60 bool hideLocalABs = preferences->GetBoolData(wxT("HideLocalCalendars"));
62 if (saveWindowPos == TRUE){
64 chkSaveWindowPosition->SetValue(TRUE);
68 if (hideLocalABs == TRUE){
70 chkHideLocal->SetValue(TRUE);
74 // Setup the Themes tab.
79 themecol0.SetWidth(250);
80 themecol0.SetText(_("Theme"));
81 lstThemes->InsertColumn(0,themecol0);
83 // Setup the Accounts tab.
89 frmPreferences::~frmPreferences(){
91 // Destory the preferences window.
98 void frmPreferences::ReloadAccounts(){
100 // Reload the accounts in the accounts list.
102 if (firstLoad == FALSE){
105 preferences = new XCALPreferences(prefFilename);
110 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
112 lstAccounts->ClearAll();
114 accountscol0.SetId(0);
115 accountscol0.SetWidth(24);
116 lstAccounts->InsertColumn(0,accountscol0);
117 lstAccounts->SetImageList(accImgList, wxIMAGE_LIST_SMALL);
119 accountscol1.SetId(1);
120 accountscol1.SetText(_("Name"));
121 accountscol1.SetWidth(224);
122 lstAccounts->InsertColumn(1,accountscol1);
124 accountscol2.SetId(2);
125 accountscol2.SetText(_("Type"));
126 accountscol2.SetWidth(96);
127 lstAccounts->InsertColumn(2,accountscol2);
131 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
136 /* if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
138 col0.SetImage(AccountNetID);
142 if (preferences->accounts.GetAccountType(i) == wxT("Local")){
144 col0.SetImage(accountID);
148 col0.SetImage(accountUnsupportedID);
152 long itemindex = lstAccounts->InsertItem( col0 );
154 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
155 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
156 lstAccounts->SetItemData(itemindex, i);
162 void frmPreferences::DisableButtons( wxListEvent& event )
165 // Disable the account buttons.
167 btnAccountModify->Enable(FALSE);
168 btnAccountDelete->Enable(FALSE);
172 void frmPreferences::EnableButtons( wxListEvent& event )
175 // Enable the account buttons.
177 btnAccountModify->Enable(TRUE);
178 btnAccountDelete->Enable(TRUE);
182 void frmPreferences::AddAccount( wxCommandEvent& event )
184 // TODO: Implement AddABAccount
187 void frmPreferences::ModifyAccount( wxCommandEvent& event )
190 // Get the settings for the account, setup the form for
191 // editing the account and then display the window
192 // with the settings.
194 long lstAccountsIndex = -1;
195 wxString accFilename = GetAccountsFile();
199 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
201 // Get the account name.
203 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
205 wxLIST_STATE_SELECTED);
207 // Check that the account type is a supported account type.
209 accType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
211 if (accType != "CardDAV" && accType != "carddav" &&
212 accType != "Local" && accType != "local"){
214 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
219 accName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
221 frmEditAccount *frameEditAccount = new frmEditAccount ( this );
222 frameEditAccount->LoadPointers(cfgFile);
223 frameEditAccount->LoadSettings(accName);
224 frameEditAccount->ShowModal();
226 bool dialogResult = frameEditAccount->GetDialogResult();
228 delete frameEditAccount;
229 frameEditAccount = NULL;
233 if (dialogResult == false){
237 // Reload the account list in the preferences window.
240 *reloadAccountConfig = TRUE;
241 btnAccountModify->Enable(FALSE);
242 btnAccountDelete->Enable(FALSE);
246 void frmPreferences::DeleteAccount( wxCommandEvent& event )
249 // Display a confirmation dialog to confirm deletion.
251 long lstAccountsIndex = -1;
252 wxMessageDialog dlgdel(this, wxT("Are you sure you want to delete this account?\r\n\r\nAll data that is stored locally will be removed."), wxT("Delete account"), wxYES_NO | wxICON_EXCLAMATION);
254 if (dlgdel.ShowModal() == wxID_YES){
256 // Remove the selected item from the accounts list
257 // and mark in the accounts list as deleted (Don't write to
260 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
262 wxLIST_STATE_SELECTED);
264 bool continueAcc = TRUE;
266 wxString accountType;
267 wxString accountDirFull;
268 wxString accountDirDelFull;
269 wxString accountName;
273 wxString accFilename = GetAccountsFile();
275 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
277 accName = preferences->accounts.GetAccountName((int)lstAccounts->GetItemData(lstAccountsIndex));
279 // Get the account directory name and account type..
281 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
285 if (accountName == accName){
287 cfgFile->SetPath(accountName);
289 cfgFile->Read("accountdir", &accountDir);
290 cfgFile->Read("type", &accountType);
296 cfgFile->SetPath(wxT("/"));
297 continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
301 accountDirFull.Append(accountDir);
302 accountDirFull.Append(wxT("."));
304 /*if (AccountType == wxT("CalDAV")){
306 AccountDirFull.Append(wxT("local"));
310 if (accountType == wxT("Local")){
312 accountDirFull.Append(wxT("Local"));
316 AccountDirFull.Append(AccountType.Lower());
320 lstAccounts->DeleteItem(lstAccountsIndex);
322 // Delete the directory that contains the account information.
324 if (!accountDirFull.IsEmpty()){
326 accountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
327 accountDirDelFull.Append(wxT("/.xestiacal/accounts/"));
328 accountDirDelFull.Append(accountDirFull);
330 wxRmDir(accountDirDelFull);
334 // Delete the account from the configuration file.
336 cfgFile->SetPath(wxT("/"));
337 cfgFile->DeleteGroup(accountName);
340 // Set flag for reloading accounts on window closure.
342 *reloadAccountConfig = TRUE;
346 btnAccountModify->Enable(FALSE);
347 btnAccountDelete->Enable(FALSE);
351 void frmPreferences::SavePreferences( wxCommandEvent& event )
354 // Load up the preferences file.
356 wxString setFilename = GetSettingsFile();
358 wxFileConfig *cfgFile = new wxFileConfig("", "", setFilename);
360 // Update the settings file.
362 cfgFile->DeleteEntry(wxT("SaveWindowPosition"));
363 cfgFile->DeleteEntry(wxT("WindowPositionX"));
364 cfgFile->DeleteEntry(wxT("WindowPositionY"));
365 cfgFile->DeleteEntry(wxT("WindowPositionHeight"));
366 cfgFile->DeleteEntry(wxT("WindowPositionWidth"));
368 if (chkSaveWindowPosition->GetValue() == TRUE){
370 cfgFile->Write(wxT("SaveWindowPosition"), wxT("true"));
372 // Get parent window details.
374 frmMain *frmMainPtr = (frmMain*)this->GetParent();
376 // Get main window position data and save it.
378 wxRect frmMainPos = frmMainPtr->GetRect();
380 cfgFile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
381 cfgFile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
382 cfgFile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
383 cfgFile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
387 cfgFile->Write(wxT("SaveWindowPosition"), wxT("false"));
391 cfgFile->DeleteEntry(wxT("HideLocalCalendars"));
393 if (chkHideLocal->GetValue() == TRUE){
395 cfgFile->Write(wxT("HideLocalCalendars"), wxT("true"));
396 *reloadAccountConfig = TRUE;
400 cfgFile->Write(wxT("HideLocalCalendars"), wxT("false"));
401 *reloadAccountConfig = TRUE;
412 void frmPreferences::CloseWindow( wxCommandEvent& event )
415 // Close the preferences window.
421 void frmPreferences::SetupPointers(bool *reloadAccountInc){
423 // Setup the pointers for the preferences form.
425 reloadAccountConfig = reloadAccountInc;