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>
36 frmPreferences::frmPreferences( wxWindow* parent )
38 frmPreferencesADT( parent )
41 // Setup the preferences window.
43 wxMemoryInputStream astream(icons_accinet_png, sizeof(icons_accinet_png));
44 wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png));
45 wxMemoryInputStream cstream(icons_accunsupported_png, sizeof(icons_accunsupported_png));
46 wxMemoryInputStream windowposstream(icons_windowpos_png, sizeof(icons_windowpos_png));
47 wxMemoryInputStream hideaddressbooksstream(icons_hideaddressbooks_png, sizeof(icons_hideaddressbooks_png));
49 wxImage icons_windowpos_png(windowposstream, wxBITMAP_TYPE_PNG);
50 wxBitmap WindowPosition(icons_windowpos_png, -1);
52 wxImage icons_hideaddressbooks_png(hideaddressbooksstream, wxBITMAP_TYPE_PNG);
53 wxBitmap HideAddressBooks(icons_hideaddressbooks_png, -1);
55 bmpWindowPosition->SetBitmap(WindowPosition);
56 bmpLocalAddressBooks->SetBitmap(HideAddressBooks);
58 wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
59 wxBitmap AccInet(icons_accinet_png, -1);
61 wxIAccInet.CopyFromBitmap(AccInet);
63 wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
64 wxBitmap AccNIcon(icons_acclocal_png, -1);
66 wxIAccNIcon.CopyFromBitmap(AccNIcon);
68 wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
69 wxBitmap AccUIcon(icons_accunsupported_png, -1);
71 wxIAccUIcon.CopyFromBitmap(AccUIcon);
73 AccountID = AccImgList->Add(wxIAccNIcon);
74 AccountNetID = AccImgList->Add(wxIAccInet);
75 AccountUnsupportedID = AccImgList->Add(wxIAccUIcon);
77 NbtPreferences->RemovePage(1);
81 frmPreferences::~frmPreferences(){
83 // Destory the preferences window.
90 void frmPreferences::EnableABButtons( wxListEvent& event )
93 // Enable the account buttons.
95 btnAccountModify->Enable(TRUE);
96 btnAccountDelete->Enable(TRUE);
100 void frmPreferences::DisableABButtons( wxListEvent& event )
103 // Disable the account buttons.
105 btnAccountModify->Enable(FALSE);
106 btnAccountDelete->Enable(FALSE);
110 void frmPreferences::AddABAccount(wxCommandEvent& event)
115 bool ReloadAccountsData = FALSE;
117 frmNewAccount *frameNewAccount = new frmNewAccount(this);
118 frameNewAccount->SetupPointers(&ReloadAccountsData);
119 frameNewAccount->ShowModal();
120 delete frameNewAccount;
121 frameNewAccount = NULL;
123 if (ReloadAccountsData == TRUE){
125 // Reload the accounts as a change has been made within
129 *ReloadAccountConfig = TRUE;
135 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
138 // Get the settings for the account, setup the form for
139 // editing the account and then display the window
140 // with the settings.
142 long lstAccountsIndex = -1;
143 wxString AccFilename = GetAccountsFile();
147 // Check if an account has been selected before continuing.
149 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
151 wxLIST_STATE_SELECTED);
153 if (lstAccountsIndex == -1){
155 // No account has been selected so exit this subroutine.
161 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
163 // Check that the account type is a supported account type.
165 AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
167 if (AccType != "CardDAV" && AccType != "carddav" &&
168 AccType != "Local" && AccType != "local"){
170 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
175 // Get the account name.
177 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
179 frmEditAccount *frameEditAccount = new frmEditAccount ( this );
180 frameEditAccount->LoadPointers(cfgfile);
181 frameEditAccount->LoadSettings(AccName);
182 frameEditAccount->ShowModal();
184 bool DialogResult = frameEditAccount->GetDialogResult();
186 delete frameEditAccount;
187 frameEditAccount = NULL;
191 if (DialogResult == false){
195 // Reload the account list in the preferences window.
198 *ReloadAccountConfig = TRUE;
199 btnAccountModify->Enable(FALSE);
200 btnAccountDelete->Enable(FALSE);
204 void frmPreferences::ModifyABAccount( wxMouseEvent& event )
207 wxCommandEvent NullEvent;
208 ModifyABAccount(NullEvent);
212 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
214 // Display a confirmation dialog to confirm deletion.
216 long lstAccountsIndex = -1;
217 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);
219 if (dlgdel.ShowModal() == wxID_YES){
221 // Remove the selected item from the accounts list
222 // and mark in the accounts list as deleted (Don't write to
225 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
227 wxLIST_STATE_SELECTED);
229 bool ContinueAcc = TRUE;
231 wxString AccountType;
232 wxString AccountDirFull;
233 wxString AccountDirDelFull;
234 wxString AccountName;
238 wxString AccFilename = GetAccountsFile();
240 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
242 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
244 // Get the account directory name and account type..
246 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
250 if (AccountName == AccName){
252 cfgfile->SetPath(AccountName);
254 cfgfile->Read("accountdir", &AccountDir);
255 cfgfile->Read("type", &AccountType);
261 cfgfile->SetPath(wxT("/"));
262 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
266 AccountDirFull.Append(AccountDir);
267 AccountDirFull.Append(wxT("."));
269 if (AccountType == wxT("CardDAV")){
271 AccountDirFull.Append(wxT("local"));
273 } else if (AccountType == wxT("Local")){
275 AccountDirFull.Append(wxT("carddav"));
279 AccountDirFull.Append(AccountType.Lower());
283 lstAccounts->DeleteItem(lstAccountsIndex);
285 // Delete the directory that contains the account information.
287 if (!AccountDirFull.IsEmpty()){
289 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
290 AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
291 AccountDirDelFull.Append(AccountDirFull);
293 wxRmDir(AccountDirDelFull);
297 // Delete the account from the configuration file.
299 cfgfile->SetPath(wxT("/"));
300 cfgfile->DeleteGroup(AccountName);
303 // Set flag for reloading accounts on window closure.
305 *ReloadAccountConfig = TRUE;
309 btnAccountModify->Enable(FALSE);
310 btnAccountDelete->Enable(FALSE);
314 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
317 // Setup the preferences filename string. Default is the
318 // *nix systems one (/home/$USER/.xestiaab/preferences)
320 preffilename = GetUserPrefDir();
322 preferences = new XABPreferences(preffilename);
324 // Setup the General Tab.
326 bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
327 bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
329 if (SaveWindowPos == TRUE){
331 chkSaveWindowPosition->SetValue(TRUE);
335 if (HideLocalABs == TRUE){
337 chkHideLocal->SetValue(TRUE);
341 // Setup the Themes tab.
343 wxListItem themecol0;
346 themecol0.SetWidth(250);
347 themecol0.SetText(_("Theme"));
348 lstThemes->InsertColumn(0,themecol0);
350 // Setup the Accounts tab.
356 void frmPreferences::ReloadAccounts(){
358 // Reload the accounts in the accounts list.
360 if (FirstLoad == FALSE){
363 preferences = new XABPreferences(preffilename);
368 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
370 lstAccounts->ClearAll();
372 accountscol0.SetId(0);
373 accountscol0.SetWidth(24);
374 lstAccounts->InsertColumn(0,accountscol0);
375 lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
377 accountscol1.SetId(1);
378 accountscol1.SetText(_("Name"));
379 accountscol1.SetWidth(224);
380 lstAccounts->InsertColumn(1,accountscol1);
382 accountscol2.SetId(2);
383 accountscol2.SetText(_("Type"));
384 accountscol2.SetWidth(96);
385 lstAccounts->InsertColumn(2,accountscol2);
389 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
394 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
396 col0.SetImage(AccountNetID);
398 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
400 col0.SetImage(AccountID);
404 col0.SetImage(AccountUnsupportedID);
408 long itemindex = lstAccounts->InsertItem( col0 );
410 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
411 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
417 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
419 // Setup the pointers for the preferences form.
421 ReloadAccountConfig = ReloadAccountInc;
425 void frmPreferences::SavePreferences(wxCommandEvent &event){
427 // Load up the preferences file.
429 wxString SetFilename = GetSettingsFile();
431 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
433 // Update the settings file.
435 cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
436 cfgfile->DeleteEntry(wxT("WindowPositionX"));
437 cfgfile->DeleteEntry(wxT("WindowPositionY"));
438 cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
439 cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
441 if (chkSaveWindowPosition->GetValue() == TRUE){
443 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
445 // Get parent window details.
447 frmMain *frmMainPtr = (frmMain*)this->GetParent();
449 // Get main window position data and save it.
451 wxRect frmMainPos = frmMainPtr->GetRect();
453 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
454 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
455 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
456 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
460 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
464 cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
466 if (chkHideLocal->GetValue() == TRUE){
468 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
469 *ReloadAccountConfig = TRUE;
473 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
474 *ReloadAccountConfig = TRUE;
485 void frmPreferences::CloseWindow(wxCommandEvent &event){
487 // Close the preferences window.