1 // frmPreferences.cpp - Preferences form.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book 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 Address Book 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 Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmPreferences.h"
20 #include "frmEditAccount.h"
21 #include "frmNewAccount.h"
23 #include "common/preferences.h"
24 #include "common/dirs.h"
26 #include "bitmaps/preferences.h"
28 #include "import/import.h"
29 #include "export/export.h"
32 #include <wx/mstream.h>
33 #include <wx/dialog.h>
34 #include <wx/msgdlg.h>
35 #include <wx/fileconf.h>
37 frmPreferences::frmPreferences( wxWindow* parent )
39 frmPreferencesADT( parent )
42 // Setup the preferences window.
44 wxMemoryInputStream astream(icons_accinet_png, sizeof(icons_accinet_png));
45 wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png));
46 wxMemoryInputStream cstream(icons_accunsupported_png, sizeof(icons_accunsupported_png));
47 wxMemoryInputStream windowposstream(icons_windowpos_png, sizeof(icons_windowpos_png));
48 wxMemoryInputStream hideaddressbooksstream(icons_hideaddressbooks_png, sizeof(icons_hideaddressbooks_png));
50 wxImage icons_windowpos_png(windowposstream, wxBITMAP_TYPE_PNG);
51 wxBitmap WindowPosition(icons_windowpos_png, -1);
53 wxImage icons_hideaddressbooks_png(hideaddressbooksstream, wxBITMAP_TYPE_PNG);
54 wxBitmap HideAddressBooks(icons_hideaddressbooks_png, -1);
56 bmpWindowPosition->SetBitmap(WindowPosition);
57 bmpLocalAddressBooks->SetBitmap(HideAddressBooks);
59 wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
60 wxBitmap AccInet(icons_accinet_png, -1);
62 wxIAccInet.CopyFromBitmap(AccInet);
64 wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
65 wxBitmap AccNIcon(icons_acclocal_png, -1);
67 wxIAccNIcon.CopyFromBitmap(AccNIcon);
69 wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
70 wxBitmap AccUIcon(icons_accunsupported_png, -1);
72 wxIAccUIcon.CopyFromBitmap(AccUIcon);
74 AccountID = AccImgList->Add(wxIAccNIcon);
75 AccountNetID = AccImgList->Add(wxIAccInet);
76 AccountUnsupportedID = AccImgList->Add(wxIAccUIcon);
78 NbtPreferences->RemovePage(1);
82 frmPreferences::~frmPreferences(){
84 // Destory the preferences window.
91 void frmPreferences::EnableABButtons( wxListEvent& event )
94 // Enable the account buttons.
96 btnAccountModify->Enable(TRUE);
97 btnAccountDelete->Enable(TRUE);
101 void frmPreferences::DisableABButtons( wxListEvent& event )
104 // Disable the account buttons.
106 btnAccountModify->Enable(FALSE);
107 btnAccountDelete->Enable(FALSE);
111 void frmPreferences::AddABAccount(wxCommandEvent& event)
116 bool ReloadAccountsData = FALSE;
118 frmNewAccount *frameNewAccount = new frmNewAccount(this);
119 frameNewAccount->SetupPointers(&ReloadAccountsData);
120 frameNewAccount->ShowModal();
121 delete frameNewAccount;
122 frameNewAccount = NULL;
124 if (ReloadAccountsData == TRUE){
126 // Reload the accounts as a change has been made within
130 *ReloadAccountConfig = TRUE;
136 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
139 // Get the settings for the account, setup the form for
140 // editing the account and then display the window
141 // with the settings.
143 long lstAccountsIndex = -1;
144 wxString AccFilename = GetAccountsFile();
148 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
150 // Get the account name.
152 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
154 wxLIST_STATE_SELECTED);
156 // Check that the account type is a supported account type.
158 AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
160 if (AccType != "CardDAV" && AccType != "carddav" &&
161 AccType != "Local" && AccType != "local"){
163 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
168 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
170 frmEditAccount *frameEditAccount = new frmEditAccount ( this );
171 frameEditAccount->LoadPointers(cfgfile);
172 frameEditAccount->LoadSettings(AccName);
173 frameEditAccount->ShowModal();
175 bool DialogResult = frameEditAccount->GetDialogResult();
177 delete frameEditAccount;
178 frameEditAccount = NULL;
182 if (DialogResult == false){
186 // Reload the account list in the preferences window.
189 *ReloadAccountConfig = TRUE;
190 btnAccountModify->Enable(FALSE);
191 btnAccountDelete->Enable(FALSE);
195 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
197 // Display a confirmation dialog to confirm deletion.
199 long lstAccountsIndex = -1;
200 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);
202 if (dlgdel.ShowModal() == wxID_YES){
204 // Remove the selected item from the accounts list
205 // and mark in the accounts list as deleted (Don't write to
208 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
210 wxLIST_STATE_SELECTED);
212 bool ContinueAcc = TRUE;
214 wxString AccountType;
215 wxString AccountDirFull;
216 wxString AccountDirDelFull;
217 wxString AccountName;
221 wxString AccFilename = GetAccountsFile();
223 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
225 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
227 // Get the account directory name and account type..
229 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
233 if (AccountName == AccName){
235 cfgfile->SetPath(AccountName);
237 cfgfile->Read("accountdir", &AccountDir);
238 cfgfile->Read("type", &AccountType);
244 cfgfile->SetPath(wxT("/"));
245 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
249 AccountDirFull.Append(AccountDir);
250 AccountDirFull.Append(wxT("."));
252 if (AccountType == wxT("CardDAV")){
254 AccountDirFull.Append(wxT("local"));
256 } else if (AccountType == wxT("Local")){
258 AccountDirFull.Append(wxT("carddav"));
262 AccountDirFull.Append(AccountType.Lower());
266 lstAccounts->DeleteItem(lstAccountsIndex);
268 // Delete the directory that contains the account information.
270 if (!AccountDirFull.IsEmpty()){
272 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
273 AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
274 AccountDirDelFull.Append(AccountDirFull);
276 wxRmDir(AccountDirDelFull);
280 // Delete the account from the configuration file.
282 cfgfile->SetPath(wxT("/"));
283 cfgfile->DeleteGroup(AccountName);
286 // Set flag for reloading accounts on window closure.
288 *ReloadAccountConfig = TRUE;
292 btnAccountModify->Enable(FALSE);
293 btnAccountDelete->Enable(FALSE);
297 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
300 // Setup the preferences filename string. Default is the
301 // *nix systems one (/home/$USER/.xestiaab/preferences)
303 preffilename = GetUserPrefDir();
305 preferences = new XABPreferences(preffilename);
307 // Setup the General Tab.
309 bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
310 bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
312 if (SaveWindowPos == TRUE){
314 chkSaveWindowPosition->SetValue(TRUE);
318 if (HideLocalABs == TRUE){
320 chkHideLocal->SetValue(TRUE);
324 // Setup the Themes tab.
326 wxListItem themecol0;
329 themecol0.SetWidth(250);
330 themecol0.SetText(_("Theme"));
331 lstThemes->InsertColumn(0,themecol0);
333 // Setup the Accounts tab.
339 void frmPreferences::ReloadAccounts(){
341 // Reload the accounts in the accounts list.
343 if (FirstLoad == FALSE){
346 preferences = new XABPreferences(preffilename);
351 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
353 lstAccounts->ClearAll();
355 accountscol0.SetId(0);
356 accountscol0.SetWidth(24);
357 lstAccounts->InsertColumn(0,accountscol0);
358 lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
360 accountscol1.SetId(1);
361 accountscol1.SetText(_("Name"));
362 accountscol1.SetWidth(224);
363 lstAccounts->InsertColumn(1,accountscol1);
365 accountscol2.SetId(2);
366 accountscol2.SetText(_("Type"));
367 accountscol2.SetWidth(96);
368 lstAccounts->InsertColumn(2,accountscol2);
372 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
377 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
379 col0.SetImage(AccountNetID);
381 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
383 col0.SetImage(AccountID);
387 col0.SetImage(AccountUnsupportedID);
391 long itemindex = lstAccounts->InsertItem( col0 );
393 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
394 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
400 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
402 // Setup the pointers for the preferences form.
404 ReloadAccountConfig = ReloadAccountInc;
408 void frmPreferences::SavePreferences(wxCommandEvent &event){
410 // Load up the preferences file.
412 wxString SetFilename = GetSettingsFile();
414 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
416 // Update the settings file.
418 cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
419 cfgfile->DeleteEntry(wxT("WindowPositionX"));
420 cfgfile->DeleteEntry(wxT("WindowPositionY"));
421 cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
422 cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
424 if (chkSaveWindowPosition->GetValue() == TRUE){
426 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
428 // Get parent window details.
430 frmMain *frmMainPtr = (frmMain*)this->GetParent();
432 // Get main window position data and save it.
434 wxRect frmMainPos = frmMainPtr->GetRect();
436 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
437 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
438 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
439 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
443 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
447 cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
449 if (chkHideLocal->GetValue() == TRUE){
451 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
452 *ReloadAccountConfig = TRUE;
456 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
457 *ReloadAccountConfig = TRUE;
468 void frmPreferences::CloseWindow(wxCommandEvent &event){
470 // Close the preferences window.