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 delete frameEditAccount;
131 frameEditAccount = NULL;
135 // Reload the account list in the preferences window.
138 *ReloadAccountConfig = TRUE;
142 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
144 // Display a confirmation dialog to confirm deletion.
146 long lstAccountsIndex = -1;
147 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);
149 if (dlgdel.ShowModal() == wxID_YES){
150 // Remove the selected item from the accounts list
151 // and mark in the accounts list as deleted (Don't write to
154 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
156 wxLIST_STATE_SELECTED);
158 bool ContinueAcc = TRUE;
160 wxString AccountType;
161 wxString AccountDirFull;
162 wxString AccountDirDelFull;
163 wxString AccountName;
167 wxString AccFilename = GetAccountsFile();
169 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
171 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
173 // Get the account directory name and account type..
175 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
179 if (AccountName == AccName){
181 cfgfile->SetPath(AccountName);
183 cfgfile->Read("accountdir", &AccountDir);
184 cfgfile->Read("type", &AccountType);
190 cfgfile->SetPath(wxT("/"));
191 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
195 AccountDirFull.Append(AccountDir);
196 AccountDirFull.Append(wxT("."));
198 if (AccountType == wxT("CardDAV")){
200 AccountDirFull.Append(wxT("local"));
202 } else if (AccountType == wxT("Local")){
204 AccountDirFull.Append(wxT("carddav"));
208 lstAccounts->DeleteItem(lstAccountsIndex);
210 // Delete the directory that contains the account information.
212 if (!AccountDirFull.IsEmpty()){
214 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
215 AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
216 AccountDirDelFull.Append(AccountDirFull);
218 wxRmDir(AccountDirDelFull);
222 // Delete the account from the configuration file.
224 cfgfile->SetPath(wxT("/"));
225 cfgfile->DeleteGroup(AccountName);
228 // Set flag for reloading accounts on window closure.
230 *ReloadAccountConfig = TRUE;
236 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
239 // Setup the preferences filename string. Default is the
240 // *nix systems one (/home/$USER/.xestiaab/preferences)
242 preffilename = GetUserPrefDir();
244 preferences = new XABPreferences(preffilename);
246 // Setup the General Tab.
248 bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
249 bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
251 if (SaveWindowPos == TRUE){
253 chkSaveWindowPosition->SetValue(TRUE);
257 if (HideLocalABs == TRUE){
259 chkHideLocal->SetValue(TRUE);
263 // Setup the Themes tab.
265 wxListItem themecol0;
268 themecol0.SetWidth(250);
269 themecol0.SetText(_("Theme"));
270 lstThemes->InsertColumn(0,themecol0);
272 // Setup the Accounts tab.
278 void frmPreferences::ReloadAccounts(){
280 if (FirstLoad == FALSE){
283 preferences = new XABPreferences(preffilename);
288 wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
290 lstAccounts->ClearAll();
292 accountscol0.SetId(0);
293 accountscol0.SetWidth(24);
294 lstAccounts->InsertColumn(0,accountscol0);
295 lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
297 accountscol1.SetId(1);
298 accountscol1.SetText(_("Name"));
299 accountscol1.SetWidth(224);
300 lstAccounts->InsertColumn(1,accountscol1);
302 accountscol2.SetId(2);
303 accountscol2.SetText(_("Type"));
304 accountscol2.SetWidth(96);
305 lstAccounts->InsertColumn(2,accountscol2);
307 /*accountscol1.SetId(1);
308 accountscol1.SetText(_(""));
309 accountscol1.SetWidth(32);
310 lstAccounts->InsertColumn(1,accountscol1);
312 accountscol2.SetId(2);
313 accountscol2.SetText(_("Name"));
314 accountscol2.SetWidth(224);
315 lstAccounts->InsertColumn(2,accountscol2);
317 accountscol3.SetId(3);
318 accountscol3.SetText(_("Type"));
319 accountscol3.SetWidth(96);
320 lstAccounts->InsertColumn(3,accountscol3);*/
324 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
328 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
330 col0.SetImage(AccountNetID);
332 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
334 col0.SetImage(AccountID);
338 //col0.SetText( wxString::Format(wxT("%i"),i) );
339 //col0.SetText(preferences.accounts.GetAccountName(i));
341 long itemindex = lstAccounts->InsertItem( col0 );
343 //AccType = preferences.accounts.GetAccountType(i);
346 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
347 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
348 //lstAccounts->SetItem(itemindex, 3, AccType);
354 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
356 ReloadAccountConfig = ReloadAccountInc;
360 void frmPreferences::SavePreferences(wxCommandEvent &event){
362 // Load up the preferences file.
364 wxString SetFilename = GetSettingsFile();
366 wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
368 // Update the settings file.
370 cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
371 cfgfile->DeleteEntry(wxT("WindowPositionX"));
372 cfgfile->DeleteEntry(wxT("WindowPositionY"));
373 cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
374 cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
376 if (chkSaveWindowPosition->GetValue() == TRUE){
378 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
380 // Get parent window details.
382 frmMain *frmMainPtr = (frmMain*)this->GetParent();
384 // Get main window position data and save it.
386 wxRect frmMainPos = frmMainPtr->GetRect();
388 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
389 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
390 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
391 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
395 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
399 cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
401 if (chkHideLocal->GetValue() == TRUE){
403 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
404 *ReloadAccountConfig = TRUE;
408 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
409 *ReloadAccountConfig = TRUE;
420 void frmPreferences::CloseWindow(wxCommandEvent &event){