Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmPreferences: Added icons
[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         wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
149         
150         // Get the account name.
151     
152         lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
153                 wxLIST_NEXT_ALL,
154                 wxLIST_STATE_SELECTED);
155         
156         // Check that the account type is a supported account type.
157         
158         AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
159         
160         if (AccType != "CardDAV" && AccType != "carddav" &&
161                 AccType != "Local" && AccType != "local"){
162         
163                 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
164                 return;
165                         
166         }
167     
168         AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
169     
170         frmEditAccount *frameEditAccount = new frmEditAccount ( this );
171         frameEditAccount->LoadPointers(cfgfile);
172         frameEditAccount->LoadSettings(AccName);
173         frameEditAccount->ShowModal();
175         bool DialogResult = frameEditAccount->GetDialogResult();
177         delete frameEditAccount;
178         frameEditAccount = NULL;
179         delete cfgfile;
180         cfgfile = NULL;
182         if (DialogResult == false){
183                 return;
184         }
186         // Reload the account list in the preferences window.
188         ReloadAccounts();
189         *ReloadAccountConfig = TRUE;
190         btnAccountModify->Enable(FALSE);
191         btnAccountDelete->Enable(FALSE);
192     
195 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
197         // Display a confirmation dialog to confirm deletion.
198     
199         long lstAccountsIndex = -1;
200         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);
201     
202         if (dlgdel.ShowModal() == wxID_YES){
203                 
204                 // Remove the selected item from the accounts list
205                 // and mark in the accounts list as deleted (Don't write to
206                 // accounts file).
207         
208                 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
209                         wxLIST_NEXT_ALL,
210                         wxLIST_STATE_SELECTED);
211         
212                 bool ContinueAcc = TRUE;
213                 wxString AccountDir;
214                 wxString AccountType;
215                 wxString AccountDirFull;
216                 wxString AccountDirDelFull;
217                 wxString AccountName;
218                 wxString AccName;
219                 long itemindex = 0;
220         
221                 wxString AccFilename = GetAccountsFile();
222     
223                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
224         
225                 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
226         
227                 // Get the account directory name and account type..
228         
229                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
230         
231                 while (ContinueAcc){
233                         if (AccountName == AccName){
235                                 cfgfile->SetPath(AccountName);
236                         
237                                 cfgfile->Read("accountdir", &AccountDir);
238                                 cfgfile->Read("type", &AccountType);
239                 
240                                 break;
242                         }
243                 
244                         cfgfile->SetPath(wxT("/"));
245                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
246         
247                 }
248         
249                 AccountDirFull.Append(AccountDir);
250                 AccountDirFull.Append(wxT("."));
251         
252                 if (AccountType == wxT("CardDAV")){
253         
254                         AccountDirFull.Append(wxT("local"));
255         
256                 } else if (AccountType == wxT("Local")){
257         
258                         AccountDirFull.Append(wxT("carddav"));
259         
260                 } else {
261                         
262                         AccountDirFull.Append(AccountType.Lower());
263                         
264                 }
265         
266                 lstAccounts->DeleteItem(lstAccountsIndex);
267         
268                 // Delete the directory that contains the account information.
269         
270                 if (!AccountDirFull.IsEmpty()){
271         
272                         AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
273                         AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
274                         AccountDirDelFull.Append(AccountDirFull);
275         
276                         wxRmDir(AccountDirDelFull);
277         
278                 }
279         
280                 // Delete the account from the configuration file.
281         
282                 cfgfile->SetPath(wxT("/"));
283                 cfgfile->DeleteGroup(AccountName);
284                 cfgfile->Flush();
285         
286                 // Set flag for reloading accounts on window closure.
287     
288                 *ReloadAccountConfig = TRUE;
289     
290         }
292         btnAccountModify->Enable(FALSE);
293         btnAccountDelete->Enable(FALSE);
294         
297 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
300         // Setup the preferences filename string. Default is the
301         // *nix systems one (/home/$USER/.xestiaab/preferences)
302     
303         preffilename = GetUserPrefDir();
305         preferences = new XABPreferences(preffilename);
306   
307         // Setup the General Tab.
308     
309         bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
310         bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
311     
312         if (SaveWindowPos == TRUE){
313     
314                 chkSaveWindowPosition->SetValue(TRUE);
315     
316         }
317     
318         if (HideLocalABs == TRUE){
319     
320                 chkHideLocal->SetValue(TRUE);
321     
322         }
323     
324         // Setup the Themes tab.
326         wxListItem themecol0;
327     
328         themecol0.SetId(0);
329         themecol0.SetWidth(250);
330         themecol0.SetText(_("Theme"));
331         lstThemes->InsertColumn(0,themecol0);
332     
333         // Setup the Accounts tab.
334     
335         ReloadAccounts();   
336     
339 void frmPreferences::ReloadAccounts(){
341         // Reload the accounts in the accounts list.
342         
343         if (FirstLoad == FALSE){
344                 delete preferences;
345                 preferences = NULL;
346                 preferences = new XABPreferences(preffilename);    
347         } else {
348                 FirstLoad = FALSE;
349         }
351         wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
353         lstAccounts->ClearAll();
355         accountscol0.SetId(0);
356         accountscol0.SetWidth(24);
357         lstAccounts->InsertColumn(0,accountscol0);
358         lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
360         accountscol1.SetId(1);
361         accountscol1.SetText(_("Name"));
362         accountscol1.SetWidth(224);    
363         lstAccounts->InsertColumn(1,accountscol1);   
365         accountscol2.SetId(2);
366         accountscol2.SetText(_("Type"));
367         accountscol2.SetWidth(96);
368         lstAccounts->InsertColumn(2,accountscol2);
369     
370         wxString AccType;
371     
372         for (int i = 0; i < preferences->accounts.GetCount() ; i++){
374                 wxListItem col0;
375                 col0.SetId(i);
376                 
377                 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
378         
379                         col0.SetImage(AccountNetID);
380         
381                 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
382         
383                         col0.SetImage(AccountID);
384         
385                 } else {
387                         col0.SetImage(AccountUnsupportedID);
388                         
389                 }
390         
391                 long itemindex = lstAccounts->InsertItem( col0 );
392         
393                 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
394                 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
395         
396         }
397     
400 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
402         // Setup the pointers for the preferences form.
403         
404         ReloadAccountConfig = ReloadAccountInc;
408 void frmPreferences::SavePreferences(wxCommandEvent &event){
410         // Load up the preferences file.
412         wxString SetFilename = GetSettingsFile();
414         wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
416         // Update the settings file.
417                 
418         cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
419         cfgfile->DeleteEntry(wxT("WindowPositionX"));
420         cfgfile->DeleteEntry(wxT("WindowPositionY"));
421         cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
422         cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
423         
424         if (chkSaveWindowPosition->GetValue() == TRUE){
425         
426                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
427         
428                 // Get parent window details.
429                 
430                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
432                 // Get main window position data and save it.
433         
434                 wxRect frmMainPos = frmMainPtr->GetRect();
435         
436                 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
437                 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
438                 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
439                 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
441         } else {
442         
443                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
444         
445         }
446         
447         cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
448         
449         if (chkHideLocal->GetValue() == TRUE){
451                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
452                 *ReloadAccountConfig = TRUE;
453         
454         } else {
455         
456                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
457                 *ReloadAccountConfig = TRUE;
458         
459         }
461         delete cfgfile;
462         cfgfile = NULL;
464         this->Close();
468 void frmPreferences::CloseWindow(wxCommandEvent &event){
470         // Close the preferences window.
471         
472         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