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 // Check if an account has been selected before continuing.
150 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
152 wxLIST_STATE_SELECTED);
154 if (lstAccountsIndex == -1){
156 // No account has been selected so exit this subroutine.
162 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
164 // Check that the account type is a supported account type.
166 AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
168 if (AccType != "CardDAV" && AccType != "carddav" &&
169 AccType != "Local" && AccType != "local"){
171 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
176 // Get the account name.
178 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
180 frmEditAccount *frameEditAccount = new frmEditAccount ( this );
181 frameEditAccount->LoadPointers(cfgfile);
182 frameEditAccount->LoadSettings(AccName);
183 frameEditAccount->ShowModal();
185 bool DialogResult = frameEditAccount->GetDialogResult();
187 delete frameEditAccount;
188 frameEditAccount = NULL;
192 if (DialogResult == false){
196 // Reload the account list in the preferences window.
199 *ReloadAccountConfig = TRUE;
200 btnAccountModify->Enable(FALSE);
201 btnAccountDelete->Enable(FALSE);
205 void frmPreferences::ModifyABAccount( wxMouseEvent& event )
208 wxCommandEvent NullEvent;
209 ModifyABAccount(NullEvent);
213 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
215 // Display a confirmation dialog to confirm deletion.
217 long lstAccountsIndex = -1;
218 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);
220 if (dlgdel.ShowModal() == wxID_YES){
222 // Remove the selected item from the accounts list
223 // and mark in the accounts list as deleted (Don't write to
226 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
228 wxLIST_STATE_SELECTED);
230 bool ContinueAcc = TRUE;
232 wxString AccountType;
233 wxString AccountDirFull;
234 wxString AccountDirDelFull;
235 wxString AccountName;
239 wxString AccFilename = GetAccountsFile();
241 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
243 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
245 // Get the account directory name and account type..
247 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
251 if (AccountName == AccName){
253 cfgfile->SetPath(AccountName);
255 cfgfile->Read("accountdir", &AccountDir);
256 cfgfile->Read("type", &AccountType);
262 cfgfile->SetPath(wxT("/"));
263 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
267 AccountDirFull.Append(AccountDir);
268 AccountDirFull.Append(wxT("."));
270 if (AccountType == wxT("CardDAV")){
272 AccountDirFull.Append(wxT("local"));
274 } else if (AccountType == wxT("Local")){
276 AccountDirFull.Append(wxT("carddav"));
280 AccountDirFull.Append(AccountType.Lower());
284 lstAccounts->DeleteItem(lstAccountsIndex);
286 // Delete the directory that contains the account information.
288 if (!AccountDirFull.IsEmpty()){
290 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
291 AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
292 AccountDirDelFull.Append(AccountDirFull);
294 wxRmDir(AccountDirDelFull);
298 // Delete the account from the configuration file.
300 cfgfile->SetPath(wxT("/"));
301 cfgfile->DeleteGroup(AccountName);
304 // Set flag for reloading accounts on window closure.
306 *ReloadAccountConfig = TRUE;
310 btnAccountModify->Enable(FALSE);
311 btnAccountDelete->Enable(FALSE);
315 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
318 // Setup the preferences filename string. Default is the
319 // *nix systems one (/home/$USER/.xestiaab/preferences)
321 preffilename = GetUserPrefDir();
323 preferences = new XABPreferences(preffilename);
325 // Setup the General Tab.
327 bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
328 bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
330 if (SaveWindowPos == TRUE){
332 chkSaveWindowPosition->SetValue(TRUE);
336 if (HideLocalABs == TRUE){
338 chkHideLocal->SetValue(TRUE);
342 // Setup the Themes tab.
344 wxListItem themecol0;
347 themecol0.SetWidth(250);
348 themecol0.SetText(_("Theme"));
349 lstThemes->InsertColumn(0,themecol0);
351 // Setup the Accounts tab.
357 void frmPreferences::ReloadAccounts(){
359 // Reload the accounts in the accounts list.
361 if (FirstLoad == FALSE){
364 preferences = new XABPreferences(preffilename);
369 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
371 lstAccounts->ClearAll();
373 accountscol0.SetId(0);
374 accountscol0.SetWidth(24);
375 lstAccounts->InsertColumn(0,accountscol0);
376 lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
378 accountscol1.SetId(1);
379 accountscol1.SetText(_("Name"));
380 accountscol1.SetWidth(224);
381 lstAccounts->InsertColumn(1,accountscol1);
383 accountscol2.SetId(2);
384 accountscol2.SetText(_("Type"));
385 accountscol2.SetWidth(96);
386 lstAccounts->InsertColumn(2,accountscol2);
390 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
395 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
397 col0.SetImage(AccountNetID);
399 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
401 col0.SetImage(AccountID);
405 col0.SetImage(AccountUnsupportedID);
409 long itemindex = lstAccounts->InsertItem( col0 );
411 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
412 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
418 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
420 // Setup the pointers for the preferences form.
422 ReloadAccountConfig = ReloadAccountInc;
426 void frmPreferences::SavePreferences(wxCommandEvent &event){
428 // Load up the preferences file.
430 wxString SetFilename = GetSettingsFile();
432 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
434 // Update the settings file.
436 cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
437 cfgfile->DeleteEntry(wxT("WindowPositionX"));
438 cfgfile->DeleteEntry(wxT("WindowPositionY"));
439 cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
440 cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
442 if (chkSaveWindowPosition->GetValue() == TRUE){
444 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
446 // Get parent window details.
448 frmMain *frmMainPtr = (frmMain*)this->GetParent();
450 // Get main window position data and save it.
452 wxRect frmMainPos = frmMainPtr->GetRect();
454 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
455 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
456 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
457 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
461 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
465 cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
467 if (chkHideLocal->GetValue() == TRUE){
469 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
470 *ReloadAccountConfig = TRUE;
474 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
475 *ReloadAccountConfig = TRUE;
486 void frmPreferences::CloseWindow(wxCommandEvent &event){
488 // Close the preferences window.