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));
47 wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
48 wxBitmap AccInet(icons_accinet_png, -1);
50 wxIAccInet.CopyFromBitmap(AccInet);
52 wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
53 wxBitmap AccNIcon(icons_acclocal_png, -1);
55 wxIAccNIcon.CopyFromBitmap(AccNIcon);
57 AccountID = AccImgList->Add(wxIAccNIcon);
58 AccountNetID = AccImgList->Add(wxIAccInet);
60 NbtPreferences->RemovePage(1);
64 frmPreferences::~frmPreferences(){
66 // Destory the preferences window.
73 void frmPreferences::EnableABButtons( wxListEvent& event )
76 // Enable the account buttons.
78 btnAccountModify->Enable(TRUE);
79 btnAccountDelete->Enable(TRUE);
83 void frmPreferences::DisableABButtons( wxListEvent& event )
86 // Disable the account buttons.
88 btnAccountModify->Enable(FALSE);
89 btnAccountDelete->Enable(FALSE);
93 void frmPreferences::AddABAccount(wxCommandEvent& event)
98 bool ReloadAccountsData = FALSE;
100 frmNewAccount *frameNewAccount = new frmNewAccount(this);
101 frameNewAccount->SetupPointers(&ReloadAccountsData);
102 frameNewAccount->ShowModal();
103 delete frameNewAccount;
104 frameNewAccount = NULL;
106 if (ReloadAccountsData == TRUE){
108 // Reload the accounts as a change has been made within
112 *ReloadAccountConfig = TRUE;
118 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
121 // Get the settings for the account, setup the form for
122 // editing the account and then display the window
123 // with the settings.
125 long lstAccountsIndex = -1;
126 wxString AccFilename = GetAccountsFile();
129 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
131 // Get the account name.
133 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
135 wxLIST_STATE_SELECTED);
137 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
139 frmEditAccount *frameEditAccount = new frmEditAccount ( this );
140 frameEditAccount->LoadPointers(cfgfile);
141 frameEditAccount->LoadSettings(AccName);
142 frameEditAccount->ShowModal();
144 bool DialogResult = frameEditAccount->GetDialogResult();
146 delete frameEditAccount;
147 frameEditAccount = NULL;
151 if (DialogResult == false){
155 // Reload the account list in the preferences window.
158 *ReloadAccountConfig = TRUE;
159 btnAccountModify->Enable(FALSE);
160 btnAccountDelete->Enable(FALSE);
164 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
166 // Display a confirmation dialog to confirm deletion.
168 long lstAccountsIndex = -1;
169 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);
171 if (dlgdel.ShowModal() == wxID_YES){
173 // Remove the selected item from the accounts list
174 // and mark in the accounts list as deleted (Don't write to
177 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
179 wxLIST_STATE_SELECTED);
181 bool ContinueAcc = TRUE;
183 wxString AccountType;
184 wxString AccountDirFull;
185 wxString AccountDirDelFull;
186 wxString AccountName;
190 wxString AccFilename = GetAccountsFile();
192 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
194 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
196 // Get the account directory name and account type..
198 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
202 if (AccountName == AccName){
204 cfgfile->SetPath(AccountName);
206 cfgfile->Read("accountdir", &AccountDir);
207 cfgfile->Read("type", &AccountType);
213 cfgfile->SetPath(wxT("/"));
214 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
218 AccountDirFull.Append(AccountDir);
219 AccountDirFull.Append(wxT("."));
221 if (AccountType == wxT("CardDAV")){
223 AccountDirFull.Append(wxT("local"));
225 } else if (AccountType == wxT("Local")){
227 AccountDirFull.Append(wxT("carddav"));
231 lstAccounts->DeleteItem(lstAccountsIndex);
233 // Delete the directory that contains the account information.
235 if (!AccountDirFull.IsEmpty()){
237 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
238 AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
239 AccountDirDelFull.Append(AccountDirFull);
241 wxRmDir(AccountDirDelFull);
245 // Delete the account from the configuration file.
247 cfgfile->SetPath(wxT("/"));
248 cfgfile->DeleteGroup(AccountName);
251 // Set flag for reloading accounts on window closure.
253 *ReloadAccountConfig = TRUE;
259 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
262 // Setup the preferences filename string. Default is the
263 // *nix systems one (/home/$USER/.xestiaab/preferences)
265 preffilename = GetUserPrefDir();
267 preferences = new XABPreferences(preffilename);
269 // Setup the General Tab.
271 bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
272 bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
274 if (SaveWindowPos == TRUE){
276 chkSaveWindowPosition->SetValue(TRUE);
280 if (HideLocalABs == TRUE){
282 chkHideLocal->SetValue(TRUE);
286 // Setup the Themes tab.
288 wxListItem themecol0;
291 themecol0.SetWidth(250);
292 themecol0.SetText(_("Theme"));
293 lstThemes->InsertColumn(0,themecol0);
295 // Setup the Accounts tab.
301 void frmPreferences::ReloadAccounts(){
303 // Reload the accounts in the accounts list.
305 if (FirstLoad == FALSE){
308 preferences = new XABPreferences(preffilename);
313 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
315 lstAccounts->ClearAll();
317 accountscol0.SetId(0);
318 accountscol0.SetWidth(24);
319 lstAccounts->InsertColumn(0,accountscol0);
320 lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
322 accountscol1.SetId(1);
323 accountscol1.SetText(_("Name"));
324 accountscol1.SetWidth(224);
325 lstAccounts->InsertColumn(1,accountscol1);
327 accountscol2.SetId(2);
328 accountscol2.SetText(_("Type"));
329 accountscol2.SetWidth(96);
330 lstAccounts->InsertColumn(2,accountscol2);
334 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
339 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
341 col0.SetImage(AccountNetID);
343 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
345 col0.SetImage(AccountID);
349 long itemindex = lstAccounts->InsertItem( col0 );
351 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
352 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
358 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
360 // Setup the pointers for the preferences form.
362 ReloadAccountConfig = ReloadAccountInc;
366 void frmPreferences::SavePreferences(wxCommandEvent &event){
368 // Load up the preferences file.
370 wxString SetFilename = GetSettingsFile();
372 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
374 // Update the settings file.
376 cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
377 cfgfile->DeleteEntry(wxT("WindowPositionX"));
378 cfgfile->DeleteEntry(wxT("WindowPositionY"));
379 cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
380 cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
382 if (chkSaveWindowPosition->GetValue() == TRUE){
384 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
386 // Get parent window details.
388 frmMain *frmMainPtr = (frmMain*)this->GetParent();
390 // Get main window position data and save it.
392 wxRect frmMainPos = frmMainPtr->GetRect();
394 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
395 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
396 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
397 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
401 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
405 cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
407 if (chkHideLocal->GetValue() == TRUE){
409 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
410 *ReloadAccountConfig = TRUE;
414 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
415 *ReloadAccountConfig = TRUE;
426 void frmPreferences::CloseWindow(wxCommandEvent &event){
428 // Close the preferences window.