Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
languages: wo and xh combined due to missing comma
[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     
329         if (SaveWindowPos == TRUE){
330     
331                 chkSaveWindowPosition->SetValue(TRUE);
332     
333         }
334     
335         if (HideLocalABs == TRUE){
336     
337                 chkHideLocal->SetValue(TRUE);
338     
339         }
340     
341         // Setup the Themes tab.
343         wxListItem themecol0;
344     
345         themecol0.SetId(0);
346         themecol0.SetWidth(250);
347         themecol0.SetText(_("Theme"));
348         lstThemes->InsertColumn(0,themecol0);
349     
350         // Setup the Accounts tab.
351     
352         ReloadAccounts();   
353     
356 void frmPreferences::ReloadAccounts(){
358         // Reload the accounts in the accounts list.
359         
360         if (FirstLoad == FALSE){
361                 delete preferences;
362                 preferences = NULL;
363                 preferences = new XABPreferences(preffilename);    
364         } else {
365                 FirstLoad = FALSE;
366         }
368         wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
370         lstAccounts->ClearAll();
372         accountscol0.SetId(0);
373         accountscol0.SetWidth(24);
374         lstAccounts->InsertColumn(0,accountscol0);
375         lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
377         accountscol1.SetId(1);
378         accountscol1.SetText(_("Name"));
379         accountscol1.SetWidth(224);    
380         lstAccounts->InsertColumn(1,accountscol1);   
382         accountscol2.SetId(2);
383         accountscol2.SetText(_("Type"));
384         accountscol2.SetWidth(96);
385         lstAccounts->InsertColumn(2,accountscol2);
386     
387         wxString AccType;
388     
389         for (int i = 0; i < preferences->accounts.GetCount() ; i++){
391                 wxListItem col0;
392                 col0.SetId(i);
393                 
394                 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
395         
396                         col0.SetImage(AccountNetID);
397         
398                 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
399         
400                         col0.SetImage(AccountID);
401         
402                 } else {
404                         col0.SetImage(AccountUnsupportedID);
405                         
406                 }
407         
408                 long itemindex = lstAccounts->InsertItem( col0 );
409         
410                 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
411                 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
412         
413         }
414     
417 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
419         // Setup the pointers for the preferences form.
420         
421         ReloadAccountConfig = ReloadAccountInc;
425 void frmPreferences::SavePreferences(wxCommandEvent &event){
427         // Load up the preferences file.
429         wxString SetFilename = GetSettingsFile();
431         wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
433         // Update the settings file.
434                 
435         cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
436         cfgfile->DeleteEntry(wxT("WindowPositionX"));
437         cfgfile->DeleteEntry(wxT("WindowPositionY"));
438         cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
439         cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
440         
441         if (chkSaveWindowPosition->GetValue() == TRUE){
442         
443                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
444         
445                 // Get parent window details.
446                 
447                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
449                 // Get main window position data and save it.
450         
451                 wxRect frmMainPos = frmMainPtr->GetRect();
452         
453                 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
454                 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
455                 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
456                 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
458         } else {
459         
460                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
461         
462         }
463         
464         cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
465         
466         if (chkHideLocal->GetValue() == TRUE){
468                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
469                 *ReloadAccountConfig = TRUE;
470         
471         } else {
472         
473                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
474                 *ReloadAccountConfig = TRUE;
475         
476         }
478         delete cfgfile;
479         cfgfile = NULL;
481         this->Close();
485 void frmPreferences::CloseWindow(wxCommandEvent &event){
487         // Close the preferences window.
488         
489         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