Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmPreferences: Double clicking an account opens the account settings
[xestiaab/.git] / source / frmPreferences.cpp
1 // frmPreferences.cpp - Preferences form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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"
22 #include "frmMain.h"
23 #include "common/preferences.h"
24 #include "common/dirs.h"
25 #include "bitmaps.h"
26 #include "bitmaps/preferences.h"
28 #include "import/import.h"
29 #include "export/export.h"
31 #include <wx/wx.h>
32 #include <wx/mstream.h>
33 #include <wx/dialog.h>
34 #include <wx/msgdlg.h>
35 #include <wx/fileconf.h>
37 frmPreferences::frmPreferences( wxWindow* parent )
38 :
39 frmPreferencesADT( parent )
40 {
42         // Setup the preferences window.
43         
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));
47         wxMemoryInputStream windowposstream(icons_windowpos_png, sizeof(icons_windowpos_png));
48         wxMemoryInputStream hideaddressbooksstream(icons_hideaddressbooks_png, sizeof(icons_hideaddressbooks_png));
50         wxImage icons_windowpos_png(windowposstream, wxBITMAP_TYPE_PNG);
51         wxBitmap WindowPosition(icons_windowpos_png, -1);
53         wxImage icons_hideaddressbooks_png(hideaddressbooksstream, wxBITMAP_TYPE_PNG);
54         wxBitmap HideAddressBooks(icons_hideaddressbooks_png, -1);
55         
56         bmpWindowPosition->SetBitmap(WindowPosition);
57         bmpLocalAddressBooks->SetBitmap(HideAddressBooks);
58         
59         wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
60         wxBitmap AccInet(icons_accinet_png, -1);
61         wxIcon wxIAccInet;
62         wxIAccInet.CopyFromBitmap(AccInet);
64         wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
65         wxBitmap AccNIcon(icons_acclocal_png, -1);
66         wxIcon wxIAccNIcon;
67         wxIAccNIcon.CopyFromBitmap(AccNIcon);
69         wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
70         wxBitmap AccUIcon(icons_accunsupported_png, -1);
71         wxIcon wxIAccUIcon;
72         wxIAccUIcon.CopyFromBitmap(AccUIcon);
73         
74         AccountID = AccImgList->Add(wxIAccNIcon);
75         AccountNetID = AccImgList->Add(wxIAccInet);
76         AccountUnsupportedID = AccImgList->Add(wxIAccUIcon);
78         NbtPreferences->RemovePage(1);
80 }
82 frmPreferences::~frmPreferences(){
84         // Destory the preferences window.
85         
86         delete AccImgList;
87         AccImgList = NULL;
89 }
91 void frmPreferences::EnableABButtons( wxListEvent& event )
92 {
93         
94         // Enable the account buttons.
95         
96         btnAccountModify->Enable(TRUE);
97         btnAccountDelete->Enable(TRUE);
98         
99 }
101 void frmPreferences::DisableABButtons( wxListEvent& event )
103         
104         // Disable the account buttons.
105         
106         btnAccountModify->Enable(FALSE);
107         btnAccountDelete->Enable(FALSE);
108         
111 void frmPreferences::AddABAccount(wxCommandEvent& event)
114         // Add new account.
115         
116         bool ReloadAccountsData = FALSE;
118         frmNewAccount *frameNewAccount = new frmNewAccount(this);
119         frameNewAccount->SetupPointers(&ReloadAccountsData);
120         frameNewAccount->ShowModal();
121         delete frameNewAccount;
122         frameNewAccount = NULL;
124         if (ReloadAccountsData == TRUE){
126                 // Reload the accounts as a change has been made within
127                 // the application.
129                 ReloadAccounts();
130                 *ReloadAccountConfig = TRUE;
132         }
136 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
139         // Get the settings for the account, setup the form for
140         // editing the account and then display the window
141         // with the settings.
142     
143         long lstAccountsIndex = -1;
144         wxString AccFilename = GetAccountsFile();
145         wxString AccName;
146         wxString AccType;
147     
148         // Check if an account has been selected before continuing.
149         
150         lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
151                 wxLIST_NEXT_ALL,
152                 wxLIST_STATE_SELECTED);
153         
154         if (lstAccountsIndex == -1){
155                 
156                 // No account has been selected so exit this subroutine.
157                 
158                 return;
159                 
160         }
161         
162         wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
163         
164         // Check that the account type is a supported account type.
165         
166         AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
167         
168         if (AccType != "CardDAV" && AccType != "carddav" &&
169                 AccType != "Local" && AccType != "local"){
170         
171                 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
172                 return;
173                         
174         }
175         
176         // Get the account name.
177     
178         AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
179     
180         frmEditAccount *frameEditAccount = new frmEditAccount ( this );
181         frameEditAccount->LoadPointers(cfgfile);
182         frameEditAccount->LoadSettings(AccName);
183         frameEditAccount->ShowModal();
185         bool DialogResult = frameEditAccount->GetDialogResult();
187         delete frameEditAccount;
188         frameEditAccount = NULL;
189         delete cfgfile;
190         cfgfile = NULL;
192         if (DialogResult == false){
193                 return;
194         }
196         // Reload the account list in the preferences window.
198         ReloadAccounts();
199         *ReloadAccountConfig = TRUE;
200         btnAccountModify->Enable(FALSE);
201         btnAccountDelete->Enable(FALSE);
202     
205 void frmPreferences::ModifyABAccount( wxMouseEvent& event )
207         
208         wxCommandEvent NullEvent;
209         ModifyABAccount(NullEvent);
210         
213 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
215         // Display a confirmation dialog to confirm deletion.
216     
217         long lstAccountsIndex = -1;
218         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     
220         if (dlgdel.ShowModal() == wxID_YES){
221                 
222                 // Remove the selected item from the accounts list
223                 // and mark in the accounts list as deleted (Don't write to
224                 // accounts file).
225         
226                 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
227                         wxLIST_NEXT_ALL,
228                         wxLIST_STATE_SELECTED);
229         
230                 bool ContinueAcc = TRUE;
231                 wxString AccountDir;
232                 wxString AccountType;
233                 wxString AccountDirFull;
234                 wxString AccountDirDelFull;
235                 wxString AccountName;
236                 wxString AccName;
237                 long itemindex = 0;
238         
239                 wxString AccFilename = GetAccountsFile();
240     
241                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
242         
243                 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
244         
245                 // Get the account directory name and account type..
246         
247                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
248         
249                 while (ContinueAcc){
251                         if (AccountName == AccName){
253                                 cfgfile->SetPath(AccountName);
254                         
255                                 cfgfile->Read("accountdir", &AccountDir);
256                                 cfgfile->Read("type", &AccountType);
257                 
258                                 break;
260                         }
261                 
262                         cfgfile->SetPath(wxT("/"));
263                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
264         
265                 }
266         
267                 AccountDirFull.Append(AccountDir);
268                 AccountDirFull.Append(wxT("."));
269         
270                 if (AccountType == wxT("CardDAV")){
271         
272                         AccountDirFull.Append(wxT("local"));
273         
274                 } else if (AccountType == wxT("Local")){
275         
276                         AccountDirFull.Append(wxT("carddav"));
277         
278                 } else {
279                         
280                         AccountDirFull.Append(AccountType.Lower());
281                         
282                 }
283         
284                 lstAccounts->DeleteItem(lstAccountsIndex);
285         
286                 // Delete the directory that contains the account information.
287         
288                 if (!AccountDirFull.IsEmpty()){
289         
290                         AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
291                         AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
292                         AccountDirDelFull.Append(AccountDirFull);
293         
294                         wxRmDir(AccountDirDelFull);
295         
296                 }
297         
298                 // Delete the account from the configuration file.
299         
300                 cfgfile->SetPath(wxT("/"));
301                 cfgfile->DeleteGroup(AccountName);
302                 cfgfile->Flush();
303         
304                 // Set flag for reloading accounts on window closure.
305     
306                 *ReloadAccountConfig = TRUE;
307     
308         }
310         btnAccountModify->Enable(FALSE);
311         btnAccountDelete->Enable(FALSE);
312         
315 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
318         // Setup the preferences filename string. Default is the
319         // *nix systems one (/home/$USER/.xestiaab/preferences)
320     
321         preffilename = GetUserPrefDir();
323         preferences = new XABPreferences(preffilename);
324   
325         // Setup the General Tab.
326     
327         bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
328         bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
329     
330         if (SaveWindowPos == TRUE){
331     
332                 chkSaveWindowPosition->SetValue(TRUE);
333     
334         }
335     
336         if (HideLocalABs == TRUE){
337     
338                 chkHideLocal->SetValue(TRUE);
339     
340         }
341     
342         // Setup the Themes tab.
344         wxListItem themecol0;
345     
346         themecol0.SetId(0);
347         themecol0.SetWidth(250);
348         themecol0.SetText(_("Theme"));
349         lstThemes->InsertColumn(0,themecol0);
350     
351         // Setup the Accounts tab.
352     
353         ReloadAccounts();   
354     
357 void frmPreferences::ReloadAccounts(){
359         // Reload the accounts in the accounts list.
360         
361         if (FirstLoad == FALSE){
362                 delete preferences;
363                 preferences = NULL;
364                 preferences = new XABPreferences(preffilename);    
365         } else {
366                 FirstLoad = FALSE;
367         }
369         wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
371         lstAccounts->ClearAll();
373         accountscol0.SetId(0);
374         accountscol0.SetWidth(24);
375         lstAccounts->InsertColumn(0,accountscol0);
376         lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
378         accountscol1.SetId(1);
379         accountscol1.SetText(_("Name"));
380         accountscol1.SetWidth(224);    
381         lstAccounts->InsertColumn(1,accountscol1);   
383         accountscol2.SetId(2);
384         accountscol2.SetText(_("Type"));
385         accountscol2.SetWidth(96);
386         lstAccounts->InsertColumn(2,accountscol2);
387     
388         wxString AccType;
389     
390         for (int i = 0; i < preferences->accounts.GetCount() ; i++){
392                 wxListItem col0;
393                 col0.SetId(i);
394                 
395                 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
396         
397                         col0.SetImage(AccountNetID);
398         
399                 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
400         
401                         col0.SetImage(AccountID);
402         
403                 } else {
405                         col0.SetImage(AccountUnsupportedID);
406                         
407                 }
408         
409                 long itemindex = lstAccounts->InsertItem( col0 );
410         
411                 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
412                 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
413         
414         }
415     
418 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
420         // Setup the pointers for the preferences form.
421         
422         ReloadAccountConfig = ReloadAccountInc;
426 void frmPreferences::SavePreferences(wxCommandEvent &event){
428         // Load up the preferences file.
430         wxString SetFilename = GetSettingsFile();
432         wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
434         // Update the settings file.
435                 
436         cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
437         cfgfile->DeleteEntry(wxT("WindowPositionX"));
438         cfgfile->DeleteEntry(wxT("WindowPositionY"));
439         cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
440         cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
441         
442         if (chkSaveWindowPosition->GetValue() == TRUE){
443         
444                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
445         
446                 // Get parent window details.
447                 
448                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
450                 // Get main window position data and save it.
451         
452                 wxRect frmMainPos = frmMainPtr->GetRect();
453         
454                 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
455                 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
456                 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
457                 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
459         } else {
460         
461                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
462         
463         }
464         
465         cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
466         
467         if (chkHideLocal->GetValue() == TRUE){
469                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
470                 *ReloadAccountConfig = TRUE;
471         
472         } else {
473         
474                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
475                 *ReloadAccountConfig = TRUE;
476         
477         }
479         delete cfgfile;
480         cfgfile = NULL;
482         this->Close();
486 void frmPreferences::CloseWindow(wxCommandEvent &event){
488         // Close the preferences window.
489         
490         this->Close();
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy