Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Capitalise Bitmaps and Tools directories
[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"
27 #include "import/import.h"
28 #include "export/export.h"
30 #include <wx/wx.h>
31 #include <wx/mstream.h>
32 #include <wx/dialog.h>
33 #include <wx/msgdlg.h>
34 #include <wx/fileconf.h>
36 frmPreferences::frmPreferences( wxWindow* parent )
37 :
38 frmPreferencesADT( parent )
39 {
41         // Setup the preferences window.
42         
43         wxMemoryInputStream astream(icons_accinet_png, sizeof(icons_accinet_png));
44         wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png));
45         wxMemoryInputStream cstream(icons_accunsupported_png, sizeof(icons_accunsupported_png));
46         wxMemoryInputStream windowposstream(icons_windowpos_png, sizeof(icons_windowpos_png));
47         wxMemoryInputStream hideaddressbooksstream(icons_hideaddressbooks_png, sizeof(icons_hideaddressbooks_png));
49         wxImage icons_windowpos_png(windowposstream, wxBITMAP_TYPE_PNG);
50         wxBitmap WindowPosition(icons_windowpos_png, -1);
52         wxImage icons_hideaddressbooks_png(hideaddressbooksstream, wxBITMAP_TYPE_PNG);
53         wxBitmap HideAddressBooks(icons_hideaddressbooks_png, -1);
54         
55         bmpWindowPosition->SetBitmap(WindowPosition);
56         bmpLocalAddressBooks->SetBitmap(HideAddressBooks);
57         
58         wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
59         wxBitmap AccInet(icons_accinet_png, -1);
60         wxIcon wxIAccInet;
61         wxIAccInet.CopyFromBitmap(AccInet);
63         wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
64         wxBitmap AccNIcon(icons_acclocal_png, -1);
65         wxIcon wxIAccNIcon;
66         wxIAccNIcon.CopyFromBitmap(AccNIcon);
68         wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
69         wxBitmap AccUIcon(icons_accunsupported_png, -1);
70         wxIcon wxIAccUIcon;
71         wxIAccUIcon.CopyFromBitmap(AccUIcon);
72         
73         AccountID = AccImgList->Add(wxIAccNIcon);
74         AccountNetID = AccImgList->Add(wxIAccInet);
75         AccountUnsupportedID = AccImgList->Add(wxIAccUIcon);
77         NbtPreferences->RemovePage(1);
79 }
81 frmPreferences::~frmPreferences(){
83         // Destory the preferences window.
84         
85         delete AccImgList;
86         AccImgList = NULL;
88 }
90 void frmPreferences::EnableABButtons( wxListEvent& event )
91 {
92         
93         // Enable the account buttons.
94         
95         btnAccountModify->Enable(TRUE);
96         btnAccountDelete->Enable(TRUE);
97         
98 }
100 void frmPreferences::DisableABButtons( wxListEvent& event )
102         
103         // Disable the account buttons.
104         
105         btnAccountModify->Enable(FALSE);
106         btnAccountDelete->Enable(FALSE);
107         
110 void frmPreferences::AddABAccount(wxCommandEvent& event)
113         // Add new account.
114         
115         bool ReloadAccountsData = FALSE;
117         frmNewAccount *frameNewAccount = new frmNewAccount(this);
118         frameNewAccount->SetupPointers(&ReloadAccountsData);
119         frameNewAccount->ShowModal();
120         delete frameNewAccount;
121         frameNewAccount = NULL;
123         if (ReloadAccountsData == TRUE){
125                 // Reload the accounts as a change has been made within
126                 // the application.
128                 ReloadAccounts();
129                 *ReloadAccountConfig = TRUE;
131         }
135 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
138         // Get the settings for the account, setup the form for
139         // editing the account and then display the window
140         // with the settings.
141     
142         long lstAccountsIndex = -1;
143         wxString AccFilename = GetAccountsFile();
144         wxString AccName;
145         wxString AccType;
146     
147         // Check if an account has been selected before continuing.
148         
149         lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
150                 wxLIST_NEXT_ALL,
151                 wxLIST_STATE_SELECTED);
152         
153         if (lstAccountsIndex == -1){
154                 
155                 // No account has been selected so exit this subroutine.
156                 
157                 return;
158                 
159         }
160         
161         wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
162         
163         // Check that the account type is a supported account type.
164         
165         AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
166         
167         if (AccType != "CardDAV" && AccType != "carddav" &&
168                 AccType != "Local" && AccType != "local"){
169         
170                 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
171                 return;
172                         
173         }
174         
175         // Get the account name.
176     
177         AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
178     
179         frmEditAccount *frameEditAccount = new frmEditAccount ( this );
180         frameEditAccount->LoadPointers(cfgfile);
181         frameEditAccount->LoadSettings(AccName);
182         frameEditAccount->ShowModal();
184         bool DialogResult = frameEditAccount->GetDialogResult();
186         delete frameEditAccount;
187         frameEditAccount = NULL;
188         delete cfgfile;
189         cfgfile = NULL;
191         if (DialogResult == false){
192                 return;
193         }
195         // Reload the account list in the preferences window.
197         ReloadAccounts();
198         *ReloadAccountConfig = TRUE;
199         btnAccountModify->Enable(FALSE);
200         btnAccountDelete->Enable(FALSE);
201     
204 void frmPreferences::ModifyABAccount( wxMouseEvent& event )
206         
207         wxCommandEvent NullEvent;
208         ModifyABAccount(NullEvent);
209         
212 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
214         // Display a confirmation dialog to confirm deletion.
215     
216         long lstAccountsIndex = -1;
217         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);
218     
219         if (dlgdel.ShowModal() == wxID_YES){
220                 
221                 // Remove the selected item from the accounts list
222                 // and mark in the accounts list as deleted (Don't write to
223                 // accounts file).
224         
225                 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
226                         wxLIST_NEXT_ALL,
227                         wxLIST_STATE_SELECTED);
228         
229                 bool ContinueAcc = TRUE;
230                 wxString AccountDir;
231                 wxString AccountType;
232                 wxString AccountDirFull;
233                 wxString AccountDirDelFull;
234                 wxString AccountName;
235                 wxString AccName;
236                 long itemindex = 0;
237         
238                 wxString AccFilename = GetAccountsFile();
239     
240                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
241         
242                 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
243         
244                 // Get the account directory name and account type..
245         
246                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
247         
248                 while (ContinueAcc){
250                         if (AccountName == AccName){
252                                 cfgfile->SetPath(AccountName);
253                         
254                                 cfgfile->Read("accountdir", &AccountDir);
255                                 cfgfile->Read("type", &AccountType);
256                 
257                                 break;
259                         }
260                 
261                         cfgfile->SetPath(wxT("/"));
262                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
263         
264                 }
265         
266                 AccountDirFull.Append(AccountDir);
267                 AccountDirFull.Append(wxT("."));
268         
269                 if (AccountType == wxT("CardDAV")){
270         
271                         AccountDirFull.Append(wxT("local"));
272         
273                 } else if (AccountType == wxT("Local")){
274         
275                         AccountDirFull.Append(wxT("carddav"));
276         
277                 } else {
278                         
279                         AccountDirFull.Append(AccountType.Lower());
280                         
281                 }
282         
283                 lstAccounts->DeleteItem(lstAccountsIndex);
284         
285                 // Delete the directory that contains the account information.
286         
287                 if (!AccountDirFull.IsEmpty()){
288         
289                         AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
290                         AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
291                         AccountDirDelFull.Append(AccountDirFull);
292         
293                         wxRmDir(AccountDirDelFull);
294         
295                 }
296         
297                 // Delete the account from the configuration file.
298         
299                 cfgfile->SetPath(wxT("/"));
300                 cfgfile->DeleteGroup(AccountName);
301                 cfgfile->Flush();
302         
303                 // Set flag for reloading accounts on window closure.
304     
305                 *ReloadAccountConfig = TRUE;
306     
307         }
309         btnAccountModify->Enable(FALSE);
310         btnAccountDelete->Enable(FALSE);
311         
314 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
317         // Setup the preferences filename string. Default is the
318         // *nix systems one (/home/$USER/.xestiaab/preferences)
319     
320         preffilename = GetUserPrefDir();
322         preferences = new XABPreferences(preffilename);
323   
324         // Setup the General Tab.
325     
326         bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
327         bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
328         bool UseBackgroundContactColour = preferences->GetBoolData(wxT("UseBackgroundContactColour"));
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         if (UseBackgroundContactColour == true){
343                 
344                 chkUseBackgroundColour->SetValue(true);
345                 clpContactBackgroundColour->SetColour(preferences->GetBackgroundContactColourData());
346                 
347         } else {
348                 
349                 clpContactBackgroundColour->Enable(false);
350                 
351         }
352         
353         // Setup the Themes tab.
355         wxListItem themecol0;
356     
357         themecol0.SetId(0);
358         themecol0.SetWidth(250);
359         themecol0.SetText(_("Theme"));
360         lstThemes->InsertColumn(0,themecol0);
361     
362         // Setup the Accounts tab.
363     
364         ReloadAccounts();   
365     
368 void frmPreferences::ReloadAccounts(){
370         // Reload the accounts in the accounts list.
371         
372         if (FirstLoad == FALSE){
373                 delete preferences;
374                 preferences = NULL;
375                 preferences = new XABPreferences(preffilename);    
376         } else {
377                 FirstLoad = FALSE;
378         }
380         wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
382         lstAccounts->ClearAll();
384         accountscol0.SetId(0);
385         accountscol0.SetWidth(24);
386         lstAccounts->InsertColumn(0,accountscol0);
387         lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
389         accountscol1.SetId(1);
390         accountscol1.SetText(_("Name"));
391         accountscol1.SetWidth(224);    
392         lstAccounts->InsertColumn(1,accountscol1);   
394         accountscol2.SetId(2);
395         accountscol2.SetText(_("Type"));
396         accountscol2.SetWidth(96);
397         lstAccounts->InsertColumn(2,accountscol2);
398     
399         wxString AccType;
400     
401         for (int i = 0; i < preferences->accounts.GetCount() ; i++){
403                 wxListItem col0;
404                 col0.SetId(i);
405                 
406                 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
407         
408                         col0.SetImage(AccountNetID);
409         
410                 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
411         
412                         col0.SetImage(AccountID);
413         
414                 } else {
416                         col0.SetImage(AccountUnsupportedID);
417                         
418                 }
419         
420                 long itemindex = lstAccounts->InsertItem( col0 );
421         
422                 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
423                 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
424         
425         }
426     
429 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
431         // Setup the pointers for the preferences form.
432         
433         ReloadAccountConfig = ReloadAccountInc;
437 void frmPreferences::SavePreferences(wxCommandEvent &event){
439         // Load up the preferences file.
441         wxString SetFilename = GetSettingsFile();
443         wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
445         // Update the settings file.
446                 
447         cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
448         cfgfile->DeleteEntry(wxT("WindowPositionX"));
449         cfgfile->DeleteEntry(wxT("WindowPositionY"));
450         cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
451         cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
452         
453         if (chkSaveWindowPosition->GetValue() == TRUE){
454         
455                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
456         
457                 // Get parent window details.
458                 
459                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
461                 // Get main window position data and save it.
462         
463                 wxRect frmMainPos = frmMainPtr->GetRect();
464         
465                 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
466                 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
467                 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
468                 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
470         } else {
471         
472                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
473         
474         }
475         
476         cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
477         
478         if (chkHideLocal->GetValue() == TRUE){
480                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
481                 *ReloadAccountConfig = TRUE;
482         
483         } else {
484         
485                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
486                 *ReloadAccountConfig = TRUE;
487         
488         }
489         
490         if (chkUseBackgroundColour->GetValue() == TRUE){
491                 
492                 cfgfile->Write(wxT("UseBackgroundContactColour"), wxT("true"));
493                 cfgfile->Write(wxT("BackgroundContactColour"), clpContactBackgroundColour->GetColour().GetAsString(wxC2S_HTML_SYNTAX));
494                 *ReloadAccountConfig = TRUE;
495                 
496         } else {
498                 cfgfile->Write(wxT("UseBackgroundContactColour"), wxT("false"));
499                 *ReloadAccountConfig = TRUE;
500                 
501         }
503         delete cfgfile;
504         cfgfile = NULL;
506         this->Close();
510 void frmPreferences::EnableBackgroundColourPicker(wxCommandEvent &event){
511         
512         if (chkUseBackgroundColour->GetValue() == true){
513                 clpContactBackgroundColour->Enable(true);
514         } else {
515                 clpContactBackgroundColour->Enable(false);
516         }
517         
520 void frmPreferences::CloseWindow(wxCommandEvent &event){
522         // Close the preferences window.
523         
524         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