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"
27 #include "import/import.h"
28 #include "export/export.h"
31 #include <wx/mstream.h>
32 #include <wx/dialog.h>
33 #include <wx/msgdlg.h>
34 #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));
48 wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
49 wxBitmap AccInet(icons_accinet_png, -1);
51 wxIAccInet.CopyFromBitmap(AccInet);
53 wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
54 wxBitmap AccNIcon(icons_acclocal_png, -1);
56 wxIAccNIcon.CopyFromBitmap(AccNIcon);
58 wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
59 wxBitmap AccUIcon(icons_accunsupported_png, -1);
61 wxIAccUIcon.CopyFromBitmap(AccUIcon);
63 AccountID = AccImgList->Add(wxIAccNIcon);
64 AccountNetID = AccImgList->Add(wxIAccInet);
65 AccountUnsupportedID = AccImgList->Add(wxIAccUIcon);
67 NbtPreferences->RemovePage(1);
71 frmPreferences::~frmPreferences(){
73 // Destory the preferences window.
80 void frmPreferences::EnableABButtons( wxListEvent& event )
83 // Enable the account buttons.
85 btnAccountModify->Enable(TRUE);
86 btnAccountDelete->Enable(TRUE);
90 void frmPreferences::DisableABButtons( wxListEvent& event )
93 // Disable the account buttons.
95 btnAccountModify->Enable(FALSE);
96 btnAccountDelete->Enable(FALSE);
100 void frmPreferences::AddABAccount(wxCommandEvent& event)
105 bool ReloadAccountsData = FALSE;
107 frmNewAccount *frameNewAccount = new frmNewAccount(this);
108 frameNewAccount->SetupPointers(&ReloadAccountsData);
109 frameNewAccount->ShowModal();
110 delete frameNewAccount;
111 frameNewAccount = NULL;
113 if (ReloadAccountsData == TRUE){
115 // Reload the accounts as a change has been made within
119 *ReloadAccountConfig = TRUE;
125 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
128 // Get the settings for the account, setup the form for
129 // editing the account and then display the window
130 // with the settings.
132 long lstAccountsIndex = -1;
133 wxString AccFilename = GetAccountsFile();
137 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
139 // Get the account name.
141 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
143 wxLIST_STATE_SELECTED);
145 // Check that the account type is a supported account type.
147 AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
149 if (AccType != "CardDAV" && AccType != "carddav" &&
150 AccType != "Local" && AccType != "local"){
152 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
157 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
159 frmEditAccount *frameEditAccount = new frmEditAccount ( this );
160 frameEditAccount->LoadPointers(cfgfile);
161 frameEditAccount->LoadSettings(AccName);
162 frameEditAccount->ShowModal();
164 bool DialogResult = frameEditAccount->GetDialogResult();
166 delete frameEditAccount;
167 frameEditAccount = NULL;
171 if (DialogResult == false){
175 // Reload the account list in the preferences window.
178 *ReloadAccountConfig = TRUE;
179 btnAccountModify->Enable(FALSE);
180 btnAccountDelete->Enable(FALSE);
184 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
186 // Display a confirmation dialog to confirm deletion.
188 long lstAccountsIndex = -1;
189 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);
191 if (dlgdel.ShowModal() == wxID_YES){
193 // Remove the selected item from the accounts list
194 // and mark in the accounts list as deleted (Don't write to
197 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
199 wxLIST_STATE_SELECTED);
201 bool ContinueAcc = TRUE;
203 wxString AccountType;
204 wxString AccountDirFull;
205 wxString AccountDirDelFull;
206 wxString AccountName;
210 wxString AccFilename = GetAccountsFile();
212 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
214 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
216 // Get the account directory name and account type..
218 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
222 if (AccountName == AccName){
224 cfgfile->SetPath(AccountName);
226 cfgfile->Read("accountdir", &AccountDir);
227 cfgfile->Read("type", &AccountType);
233 cfgfile->SetPath(wxT("/"));
234 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
238 AccountDirFull.Append(AccountDir);
239 AccountDirFull.Append(wxT("."));
241 if (AccountType == wxT("CardDAV")){
243 AccountDirFull.Append(wxT("local"));
245 } else if (AccountType == wxT("Local")){
247 AccountDirFull.Append(wxT("carddav"));
251 AccountDirFull.Append(AccountType.Lower());
255 lstAccounts->DeleteItem(lstAccountsIndex);
257 // Delete the directory that contains the account information.
259 if (!AccountDirFull.IsEmpty()){
261 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
262 AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
263 AccountDirDelFull.Append(AccountDirFull);
265 wxRmDir(AccountDirDelFull);
269 // Delete the account from the configuration file.
271 cfgfile->SetPath(wxT("/"));
272 cfgfile->DeleteGroup(AccountName);
275 // Set flag for reloading accounts on window closure.
277 *ReloadAccountConfig = TRUE;
281 btnAccountModify->Enable(FALSE);
282 btnAccountDelete->Enable(FALSE);
286 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
289 // Setup the preferences filename string. Default is the
290 // *nix systems one (/home/$USER/.xestiaab/preferences)
292 preffilename = GetUserPrefDir();
294 preferences = new XABPreferences(preffilename);
296 // Setup the General Tab.
298 bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
299 bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
301 if (SaveWindowPos == TRUE){
303 chkSaveWindowPosition->SetValue(TRUE);
307 if (HideLocalABs == TRUE){
309 chkHideLocal->SetValue(TRUE);
313 // Setup the Themes tab.
315 wxListItem themecol0;
318 themecol0.SetWidth(250);
319 themecol0.SetText(_("Theme"));
320 lstThemes->InsertColumn(0,themecol0);
322 // Setup the Accounts tab.
328 void frmPreferences::ReloadAccounts(){
330 // Reload the accounts in the accounts list.
332 if (FirstLoad == FALSE){
335 preferences = new XABPreferences(preffilename);
340 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
342 lstAccounts->ClearAll();
344 accountscol0.SetId(0);
345 accountscol0.SetWidth(24);
346 lstAccounts->InsertColumn(0,accountscol0);
347 lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
349 accountscol1.SetId(1);
350 accountscol1.SetText(_("Name"));
351 accountscol1.SetWidth(224);
352 lstAccounts->InsertColumn(1,accountscol1);
354 accountscol2.SetId(2);
355 accountscol2.SetText(_("Type"));
356 accountscol2.SetWidth(96);
357 lstAccounts->InsertColumn(2,accountscol2);
361 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
366 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
368 col0.SetImage(AccountNetID);
370 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
372 col0.SetImage(AccountID);
376 col0.SetImage(AccountUnsupportedID);
380 long itemindex = lstAccounts->InsertItem( col0 );
382 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
383 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
389 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
391 // Setup the pointers for the preferences form.
393 ReloadAccountConfig = ReloadAccountInc;
397 void frmPreferences::SavePreferences(wxCommandEvent &event){
399 // Load up the preferences file.
401 wxString SetFilename = GetSettingsFile();
403 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
405 // Update the settings file.
407 cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
408 cfgfile->DeleteEntry(wxT("WindowPositionX"));
409 cfgfile->DeleteEntry(wxT("WindowPositionY"));
410 cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
411 cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
413 if (chkSaveWindowPosition->GetValue() == TRUE){
415 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
417 // Get parent window details.
419 frmMain *frmMainPtr = (frmMain*)this->GetParent();
421 // Get main window position data and save it.
423 wxRect frmMainPos = frmMainPtr->GetRect();
425 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
426 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
427 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
428 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
432 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
436 cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
438 if (chkHideLocal->GetValue() == TRUE){
440 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
441 *ReloadAccountConfig = TRUE;
445 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
446 *ReloadAccountConfig = TRUE;
457 void frmPreferences::CloseWindow(wxCommandEvent &event){
459 // Close the preferences window.