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 wxMemoryInputStream astream(icons_accinet_png, sizeof(icons_accinet_png));
43 wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png));
45 wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
46 wxBitmap AccInet(icons_accinet_png, -1);
48 wxIAccInet.CopyFromBitmap(AccInet);
50 wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
51 wxBitmap AccNIcon(icons_acclocal_png, -1);
53 wxIAccNIcon.CopyFromBitmap(AccNIcon);
55 AccountID = AccImgList->Add(wxIAccNIcon);
56 AccountNetID = AccImgList->Add(wxIAccInet);
58 NbtPreferences->RemovePage(1);
62 frmPreferences::~frmPreferences(){
69 void frmPreferences::EnableABButtons( wxListEvent& event )
71 btnAccountModify->Enable(TRUE);
72 btnAccountDelete->Enable(TRUE);
75 void frmPreferences::DisableABButtons( wxListEvent& event )
77 btnAccountModify->Enable(FALSE);
78 btnAccountDelete->Enable(FALSE);
81 void frmPreferences::AddABAccount(wxCommandEvent& event)
84 bool ReloadAccountsData = FALSE;
86 frmNewAccount *frameNewAccount = new frmNewAccount(this);
87 frameNewAccount->SetupPointers(&ReloadAccountsData);
88 frameNewAccount->ShowModal();
89 delete frameNewAccount;
90 frameNewAccount = NULL;
92 if (ReloadAccountsData == TRUE){
94 // Reload the accounts as a change has been made within
98 *ReloadAccountConfig = TRUE;
104 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
107 // Get the settings for the account, setup the form for
108 // editing the account and then display the window
109 // with the settings.
111 long lstAccountsIndex = -1;
112 wxString AccFilename = GetAccountsFile();
115 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
117 // Get the account name.
119 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
121 wxLIST_STATE_SELECTED);
123 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
125 frmEditAccount *frameEditAccount = new frmEditAccount ( this );
126 frameEditAccount->LoadPointers(cfgfile);
127 frameEditAccount->LoadSettings(AccName);
128 frameEditAccount->ShowModal();
130 bool DialogResult = frameEditAccount->GetDialogResult();
132 delete frameEditAccount;
133 frameEditAccount = NULL;
137 if (DialogResult == false){
141 // Reload the account list in the preferences window.
144 *ReloadAccountConfig = TRUE;
145 btnAccountModify->Enable(FALSE);
146 btnAccountDelete->Enable(FALSE);
150 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
152 // Display a confirmation dialog to confirm deletion.
154 long lstAccountsIndex = -1;
155 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);
157 if (dlgdel.ShowModal() == wxID_YES){
158 // Remove the selected item from the accounts list
159 // and mark in the accounts list as deleted (Don't write to
162 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
164 wxLIST_STATE_SELECTED);
166 bool ContinueAcc = TRUE;
168 wxString AccountType;
169 wxString AccountDirFull;
170 wxString AccountDirDelFull;
171 wxString AccountName;
175 wxString AccFilename = GetAccountsFile();
177 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
179 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
181 // Get the account directory name and account type..
183 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
187 if (AccountName == AccName){
189 cfgfile->SetPath(AccountName);
191 cfgfile->Read("accountdir", &AccountDir);
192 cfgfile->Read("type", &AccountType);
198 cfgfile->SetPath(wxT("/"));
199 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
203 AccountDirFull.Append(AccountDir);
204 AccountDirFull.Append(wxT("."));
206 if (AccountType == wxT("CardDAV")){
208 AccountDirFull.Append(wxT("local"));
210 } else if (AccountType == wxT("Local")){
212 AccountDirFull.Append(wxT("carddav"));
216 lstAccounts->DeleteItem(lstAccountsIndex);
218 // Delete the directory that contains the account information.
220 if (!AccountDirFull.IsEmpty()){
222 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
223 AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
224 AccountDirDelFull.Append(AccountDirFull);
226 wxRmDir(AccountDirDelFull);
230 // Delete the account from the configuration file.
232 cfgfile->SetPath(wxT("/"));
233 cfgfile->DeleteGroup(AccountName);
236 // Set flag for reloading accounts on window closure.
238 *ReloadAccountConfig = TRUE;
244 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
247 // Setup the preferences filename string. Default is the
248 // *nix systems one (/home/$USER/.xestiaab/preferences)
250 preffilename = GetUserPrefDir();
252 preferences = new XABPreferences(preffilename);
254 // Setup the General Tab.
256 bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
257 bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
259 if (SaveWindowPos == TRUE){
261 chkSaveWindowPosition->SetValue(TRUE);
265 if (HideLocalABs == TRUE){
267 chkHideLocal->SetValue(TRUE);
271 // Setup the Themes tab.
273 wxListItem themecol0;
276 themecol0.SetWidth(250);
277 themecol0.SetText(_("Theme"));
278 lstThemes->InsertColumn(0,themecol0);
280 // Setup the Accounts tab.
286 void frmPreferences::ReloadAccounts(){
288 if (FirstLoad == FALSE){
291 preferences = new XABPreferences(preffilename);
296 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
298 lstAccounts->ClearAll();
300 accountscol0.SetId(0);
301 accountscol0.SetWidth(24);
302 lstAccounts->InsertColumn(0,accountscol0);
303 lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
305 accountscol1.SetId(1);
306 accountscol1.SetText(_("Name"));
307 accountscol1.SetWidth(224);
308 lstAccounts->InsertColumn(1,accountscol1);
310 accountscol2.SetId(2);
311 accountscol2.SetText(_("Type"));
312 accountscol2.SetWidth(96);
313 lstAccounts->InsertColumn(2,accountscol2);
315 /*accountscol1.SetId(1);
316 accountscol1.SetText(_(""));
317 accountscol1.SetWidth(32);
318 lstAccounts->InsertColumn(1,accountscol1);
320 accountscol2.SetId(2);
321 accountscol2.SetText(_("Name"));
322 accountscol2.SetWidth(224);
323 lstAccounts->InsertColumn(2,accountscol2);
325 accountscol3.SetId(3);
326 accountscol3.SetText(_("Type"));
327 accountscol3.SetWidth(96);
328 lstAccounts->InsertColumn(3,accountscol3);*/
332 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
336 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
338 col0.SetImage(AccountNetID);
340 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
342 col0.SetImage(AccountID);
346 //col0.SetText( wxString::Format(wxT("%i"),i) );
347 //col0.SetText(preferences.accounts.GetAccountName(i));
349 long itemindex = lstAccounts->InsertItem( col0 );
351 //AccType = preferences.accounts.GetAccountType(i);
354 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
355 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
356 //lstAccounts->SetItem(itemindex, 3, AccType);
362 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
364 ReloadAccountConfig = ReloadAccountInc;
368 void frmPreferences::SavePreferences(wxCommandEvent &event){
370 // Load up the preferences file.
372 wxString SetFilename = GetSettingsFile();
374 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
376 // Update the settings file.
378 cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
379 cfgfile->DeleteEntry(wxT("WindowPositionX"));
380 cfgfile->DeleteEntry(wxT("WindowPositionY"));
381 cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
382 cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
384 if (chkSaveWindowPosition->GetValue() == TRUE){
386 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
388 // Get parent window details.
390 frmMain *frmMainPtr = (frmMain*)this->GetParent();
392 // Get main window position data and save it.
394 wxRect frmMainPos = frmMainPtr->GetRect();
396 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
397 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
398 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
399 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
403 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
407 cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
409 if (chkHideLocal->GetValue() == TRUE){
411 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
412 *ReloadAccountConfig = TRUE;
416 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
417 *ReloadAccountConfig = TRUE;
428 void frmPreferences::CloseWindow(wxCommandEvent &event){