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);
44 NbtPreferences->ChangeSelection(0);
48 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
51 // Setup the preferences filename string. Default is the
52 // *nix systems one (/home/$USER/.xestiacal/preferences)
54 prefFilename = GetUserPrefDir();
56 preferences = new XCALPreferences(prefFilename);
58 // Setup the General Tab.
60 bool saveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
61 bool hideLocalABs = preferences->GetBoolData(wxT("HideLocalCalendars"));
63 if (saveWindowPos == TRUE){
65 chkSaveWindowPosition->SetValue(TRUE);
69 if (hideLocalABs == TRUE){
71 chkHideLocal->SetValue(TRUE);
75 // Setup the Themes tab.
80 themecol0.SetWidth(250);
81 themecol0.SetText(_("Theme"));
82 lstThemes->InsertColumn(0,themecol0);
84 // Setup the Accounts tab.
90 frmPreferences::~frmPreferences(){
92 // Destory the preferences window.
99 void frmPreferences::ReloadAccounts(){
101 // Reload the accounts in the accounts list.
103 if (firstLoad == FALSE){
106 preferences = new XCALPreferences(prefFilename);
111 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
113 lstAccounts->ClearAll();
115 accountscol0.SetId(0);
116 accountscol0.SetWidth(24);
117 lstAccounts->InsertColumn(0,accountscol0);
118 lstAccounts->SetImageList(accImgList, wxIMAGE_LIST_SMALL);
120 accountscol1.SetId(1);
121 accountscol1.SetText(_("Name"));
122 accountscol1.SetWidth(224);
123 lstAccounts->InsertColumn(1,accountscol1);
125 accountscol2.SetId(2);
126 accountscol2.SetText(_("Type"));
127 accountscol2.SetWidth(96);
128 lstAccounts->InsertColumn(2,accountscol2);
132 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
137 /* if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
139 col0.SetImage(AccountNetID);
143 if (preferences->accounts.GetAccountType(i) == wxT("Local")){
145 col0.SetImage(accountID);
149 col0.SetImage(accountUnsupportedID);
153 long itemindex = lstAccounts->InsertItem( col0 );
155 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
156 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
157 lstAccounts->SetItemData(itemindex, i);
163 void frmPreferences::DisableButtons( wxListEvent& event )
166 // Disable the account buttons.
168 btnAccountModify->Enable(FALSE);
169 btnAccountDelete->Enable(FALSE);
173 void frmPreferences::EnableButtons( wxListEvent& event )
176 // Enable the account buttons.
178 btnAccountModify->Enable(TRUE);
179 btnAccountDelete->Enable(TRUE);
183 void frmPreferences::AddAccount( wxCommandEvent& event )
185 // TODO: Implement AddABAccount
188 void frmPreferences::ModifyAccount( wxCommandEvent& event )
191 // Get the settings for the account, setup the form for
192 // editing the account and then display the window
193 // with the settings.
195 long lstAccountsIndex = -1;
196 wxString accFilename = GetAccountsFile();
200 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
202 // Get the account name.
204 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
206 wxLIST_STATE_SELECTED);
208 // Check that the account type is a supported account type.
210 accType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
212 if (accType != "CardDAV" && accType != "carddav" &&
213 accType != "Local" && accType != "local"){
215 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
220 accName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
222 frmEditAccount *frameEditAccount = new frmEditAccount ( this );
223 frameEditAccount->LoadPointers(cfgFile);
224 frameEditAccount->LoadSettings(accName);
225 frameEditAccount->ShowModal();
227 bool dialogResult = frameEditAccount->GetDialogResult();
229 delete frameEditAccount;
230 frameEditAccount = NULL;
234 if (dialogResult == false){
238 // Reload the account list in the preferences window.
241 *reloadAccountConfig = TRUE;
242 btnAccountModify->Enable(FALSE);
243 btnAccountDelete->Enable(FALSE);
247 void frmPreferences::DeleteAccount( wxCommandEvent& event )
250 // Display a confirmation dialog to confirm deletion.
252 long lstAccountsIndex = -1;
253 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);
255 if (dlgdel.ShowModal() == wxID_YES){
257 // Remove the selected item from the accounts list
258 // and mark in the accounts list as deleted (Don't write to
261 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
263 wxLIST_STATE_SELECTED);
265 bool continueAcc = TRUE;
267 wxString accountType;
268 wxString accountDirFull;
269 wxString accountDirDelFull;
270 wxString accountName;
274 wxString accFilename = GetAccountsFile();
276 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
278 accName = preferences->accounts.GetAccountName((int)lstAccounts->GetItemData(lstAccountsIndex));
280 // Get the account directory name and account type..
282 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
286 if (accountName == accName){
288 cfgFile->SetPath(accountName);
290 cfgFile->Read("accountdir", &accountDir);
291 cfgFile->Read("type", &accountType);
297 cfgFile->SetPath(wxT("/"));
298 continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
302 accountDirFull.Append(accountDir);
303 accountDirFull.Append(wxT("."));
305 /*if (AccountType == wxT("CalDAV")){
307 AccountDirFull.Append(wxT("local"));
311 if (accountType == wxT("Local")){
313 accountDirFull.Append(wxT("Local"));
317 AccountDirFull.Append(AccountType.Lower());
321 lstAccounts->DeleteItem(lstAccountsIndex);
323 // Delete the directory that contains the account information.
325 if (!accountDirFull.IsEmpty()){
327 accountDirDelFull.Append(GetAccountDir(accountDirFull, false));
329 DeleteDirectory(accountDirDelFull);
333 // Delete the account from the configuration file.
335 cfgFile->SetPath(wxT("/"));
336 cfgFile->DeleteGroup(accountName);
339 // Set flag for reloading accounts on window closure.
341 *reloadAccountConfig = TRUE;
345 btnAccountModify->Enable(FALSE);
346 btnAccountDelete->Enable(FALSE);
350 void frmPreferences::SavePreferences( wxCommandEvent& event )
353 // Load up the preferences file.
355 wxString setFilename = GetSettingsFile();
357 wxFileConfig *cfgFile = new wxFileConfig("", "", setFilename);
359 // Update the settings file.
361 cfgFile->DeleteEntry(wxT("SaveWindowPosition"));
362 cfgFile->DeleteEntry(wxT("WindowPositionX"));
363 cfgFile->DeleteEntry(wxT("WindowPositionY"));
364 cfgFile->DeleteEntry(wxT("WindowPositionHeight"));
365 cfgFile->DeleteEntry(wxT("WindowPositionWidth"));
367 if (chkSaveWindowPosition->GetValue() == TRUE){
369 cfgFile->Write(wxT("SaveWindowPosition"), wxT("true"));
371 // Get parent window details.
373 frmMain *frmMainPtr = (frmMain*)this->GetParent();
375 // Get main window position data and save it.
377 wxRect frmMainPos = frmMainPtr->GetRect();
379 cfgFile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
380 cfgFile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
381 cfgFile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
382 cfgFile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
386 cfgFile->Write(wxT("SaveWindowPosition"), wxT("false"));
390 cfgFile->DeleteEntry(wxT("HideLocalCalendars"));
392 if (chkHideLocal->GetValue() == TRUE){
394 cfgFile->Write(wxT("HideLocalCalendars"), wxT("true"));
395 *reloadAccountConfig = TRUE;
399 cfgFile->Write(wxT("HideLocalCalendars"), wxT("false"));
400 *reloadAccountConfig = TRUE;
411 void frmPreferences::CloseWindow( wxCommandEvent& event )
414 // Close the preferences window.
420 void frmPreferences::SetupPointers(bool *reloadAccountInc){
422 // Setup the pointers for the preferences form.
424 reloadAccountConfig = reloadAccountInc;
428 void frmPreferences::DeleteDirectory(wxString directoryPath)
431 // Open the directory and look for files and directories
434 wxDir directoryHandle(directoryPath);
436 if (!directoryHandle.IsOpened())
441 wxString directoryFilename;
443 bool continueProcessing = directoryHandle.GetFirst(&directoryFilename, wxEmptyString, wxDIR_FILES|wxDIR_DIRS|wxDIR_HIDDEN);
444 while (continueProcessing)
446 wxString directoryFilenameFull = directoryPath + "/" + directoryFilename;
447 if (wxDir::Exists(directoryFilenameFull))
449 DeleteDirectory(directoryFilenameFull);
450 continueProcessing = directoryHandle.GetNext(&directoryFilename);
453 wxRemoveFile(directoryFilenameFull);
454 continueProcessing = directoryHandle.GetNext(&directoryFilename);
456 directoryHandle.Close();
458 wxRmDir(directoryPath);