Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Updated Makefile.in with new files for compiling
[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>
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         
48         wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
49         wxBitmap AccInet(icons_accinet_png, -1);
50         wxIcon wxIAccInet;
51         wxIAccInet.CopyFromBitmap(AccInet);
53         wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
54         wxBitmap AccNIcon(icons_acclocal_png, -1);
55         wxIcon wxIAccNIcon;
56         wxIAccNIcon.CopyFromBitmap(AccNIcon);
58         wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
59         wxBitmap AccUIcon(icons_accunsupported_png, -1);
60         wxIcon wxIAccUIcon;
61         wxIAccUIcon.CopyFromBitmap(AccUIcon);
62         
63         AccountID = AccImgList->Add(wxIAccNIcon);
64         AccountNetID = AccImgList->Add(wxIAccInet);
65         AccountUnsupportedID = AccImgList->Add(wxIAccUIcon);
67         NbtPreferences->RemovePage(1);
69 }
71 frmPreferences::~frmPreferences(){
73         // Destory the preferences window.
74         
75         delete AccImgList;
76         AccImgList = NULL;
78 }
80 void frmPreferences::EnableABButtons( wxListEvent& event )
81 {
82         
83         // Enable the account buttons.
84         
85         btnAccountModify->Enable(TRUE);
86         btnAccountDelete->Enable(TRUE);
87         
88 }
90 void frmPreferences::DisableABButtons( wxListEvent& event )
91 {
92         
93         // Disable the account buttons.
94         
95         btnAccountModify->Enable(FALSE);
96         btnAccountDelete->Enable(FALSE);
97         
98 }
100 void frmPreferences::AddABAccount(wxCommandEvent& event)
103         // Add new account.
104         
105         bool ReloadAccountsData = FALSE;
107         frmNewAccount *frameNewAccount = new frmNewAccount(this);
108         frameNewAccount->SetupPointers(&ReloadAccountsData);
109         frameNewAccount->ShowModal();
110         delete frameNewAccount;
111         frameNewAccount = NULL;
113         if (ReloadAccountsData == TRUE){
115                 // Reload the accounts as a change has been made within
116                 // the application.
118                 ReloadAccounts();
119                 *ReloadAccountConfig = TRUE;
121         }
125 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
128         // Get the settings for the account, setup the form for
129         // editing the account and then display the window
130         // with the settings.
131     
132         long lstAccountsIndex = -1;
133         wxString AccFilename = GetAccountsFile();
134         wxString AccName;
135         wxString AccType;
136     
137         wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
138         
139         // Get the account name.
140     
141         lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
142                 wxLIST_NEXT_ALL,
143                 wxLIST_STATE_SELECTED);
144         
145         // Check that the account type is a supported account type.
146         
147         AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
148         
149         if (AccType != "CardDAV" && AccType != "carddav" &&
150                 AccType != "Local" && AccType != "local"){
151         
152                 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
153                 return;
154                         
155         }
156     
157         AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
158     
159         frmEditAccount *frameEditAccount = new frmEditAccount ( this );
160         frameEditAccount->LoadPointers(cfgfile);
161         frameEditAccount->LoadSettings(AccName);
162         frameEditAccount->ShowModal();
164         bool DialogResult = frameEditAccount->GetDialogResult();
166         delete frameEditAccount;
167         frameEditAccount = NULL;
168         delete cfgfile;
169         cfgfile = NULL;
171         if (DialogResult == false){
172                 return;
173         }
175         // Reload the account list in the preferences window.
177         ReloadAccounts();
178         *ReloadAccountConfig = TRUE;
179         btnAccountModify->Enable(FALSE);
180         btnAccountDelete->Enable(FALSE);
181     
184 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
186         // Display a confirmation dialog to confirm deletion.
187     
188         long lstAccountsIndex = -1;
189         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);
190     
191         if (dlgdel.ShowModal() == wxID_YES){
192                 
193                 // Remove the selected item from the accounts list
194                 // and mark in the accounts list as deleted (Don't write to
195                 // accounts file).
196         
197                 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
198                         wxLIST_NEXT_ALL,
199                         wxLIST_STATE_SELECTED);
200         
201                 bool ContinueAcc = TRUE;
202                 wxString AccountDir;
203                 wxString AccountType;
204                 wxString AccountDirFull;
205                 wxString AccountDirDelFull;
206                 wxString AccountName;
207                 wxString AccName;
208                 long itemindex = 0;
209         
210                 wxString AccFilename = GetAccountsFile();
211     
212                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
213         
214                 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
215         
216                 // Get the account directory name and account type..
217         
218                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
219         
220                 while (ContinueAcc){
222                         if (AccountName == AccName){
224                                 cfgfile->SetPath(AccountName);
225                         
226                                 cfgfile->Read("accountdir", &AccountDir);
227                                 cfgfile->Read("type", &AccountType);
228                 
229                                 break;
231                         }
232                 
233                         cfgfile->SetPath(wxT("/"));
234                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
235         
236                 }
237         
238                 AccountDirFull.Append(AccountDir);
239                 AccountDirFull.Append(wxT("."));
240         
241                 if (AccountType == wxT("CardDAV")){
242         
243                         AccountDirFull.Append(wxT("local"));
244         
245                 } else if (AccountType == wxT("Local")){
246         
247                         AccountDirFull.Append(wxT("carddav"));
248         
249                 } else {
250                         
251                         AccountDirFull.Append(AccountType.Lower());
252                         
253                 }
254         
255                 lstAccounts->DeleteItem(lstAccountsIndex);
256         
257                 // Delete the directory that contains the account information.
258         
259                 if (!AccountDirFull.IsEmpty()){
260         
261                         AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
262                         AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
263                         AccountDirDelFull.Append(AccountDirFull);
264         
265                         wxRmDir(AccountDirDelFull);
266         
267                 }
268         
269                 // Delete the account from the configuration file.
270         
271                 cfgfile->SetPath(wxT("/"));
272                 cfgfile->DeleteGroup(AccountName);
273                 cfgfile->Flush();
274         
275                 // Set flag for reloading accounts on window closure.
276     
277                 *ReloadAccountConfig = TRUE;
278     
279         }
281         btnAccountModify->Enable(FALSE);
282         btnAccountDelete->Enable(FALSE);
283         
286 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
289         // Setup the preferences filename string. Default is the
290         // *nix systems one (/home/$USER/.xestiaab/preferences)
291     
292         preffilename = GetUserPrefDir();
294         preferences = new XABPreferences(preffilename);
295   
296         // Setup the General Tab.
297     
298         bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
299         bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
300     
301         if (SaveWindowPos == TRUE){
302     
303                 chkSaveWindowPosition->SetValue(TRUE);
304     
305         }
306     
307         if (HideLocalABs == TRUE){
308     
309                 chkHideLocal->SetValue(TRUE);
310     
311         }
312     
313         // Setup the Themes tab.
315         wxListItem themecol0;
316     
317         themecol0.SetId(0);
318         themecol0.SetWidth(250);
319         themecol0.SetText(_("Theme"));
320         lstThemes->InsertColumn(0,themecol0);
321     
322         // Setup the Accounts tab.
323     
324         ReloadAccounts();   
325     
328 void frmPreferences::ReloadAccounts(){
330         // Reload the accounts in the accounts list.
331         
332         if (FirstLoad == FALSE){
333                 delete preferences;
334                 preferences = NULL;
335                 preferences = new XABPreferences(preffilename);    
336         } else {
337                 FirstLoad = FALSE;
338         }
340         wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
342         lstAccounts->ClearAll();
344         accountscol0.SetId(0);
345         accountscol0.SetWidth(24);
346         lstAccounts->InsertColumn(0,accountscol0);
347         lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
349         accountscol1.SetId(1);
350         accountscol1.SetText(_("Name"));
351         accountscol1.SetWidth(224);    
352         lstAccounts->InsertColumn(1,accountscol1);   
354         accountscol2.SetId(2);
355         accountscol2.SetText(_("Type"));
356         accountscol2.SetWidth(96);
357         lstAccounts->InsertColumn(2,accountscol2);
358     
359         wxString AccType;
360     
361         for (int i = 0; i < preferences->accounts.GetCount() ; i++){
363                 wxListItem col0;
364                 col0.SetId(i);
365                 
366                 if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
367         
368                         col0.SetImage(AccountNetID);
369         
370                 } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
371         
372                         col0.SetImage(AccountID);
373         
374                 } else {
376                         col0.SetImage(AccountUnsupportedID);
377                         
378                 }
379         
380                 long itemindex = lstAccounts->InsertItem( col0 );
381         
382                 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
383                 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
384         
385         }
386     
389 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
391         // Setup the pointers for the preferences form.
392         
393         ReloadAccountConfig = ReloadAccountInc;
397 void frmPreferences::SavePreferences(wxCommandEvent &event){
399         // Load up the preferences file.
401         wxString SetFilename = GetSettingsFile();
403         wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
405         // Update the settings file.
406                 
407         cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
408         cfgfile->DeleteEntry(wxT("WindowPositionX"));
409         cfgfile->DeleteEntry(wxT("WindowPositionY"));
410         cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
411         cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
412         
413         if (chkSaveWindowPosition->GetValue() == TRUE){
414         
415                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
416         
417                 // Get parent window details.
418                 
419                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
421                 // Get main window position data and save it.
422         
423                 wxRect frmMainPos = frmMainPtr->GetRect();
424         
425                 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
426                 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
427                 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
428                 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
430         } else {
431         
432                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
433         
434         }
435         
436         cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
437         
438         if (chkHideLocal->GetValue() == TRUE){
440                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
441                 *ReloadAccountConfig = TRUE;
442         
443         } else {
444         
445                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
446                 *ReloadAccountConfig = TRUE;
447         
448         }
450         delete cfgfile;
451         cfgfile = NULL;
453         this->Close();
457 void frmPreferences::CloseWindow(wxCommandEvent &event){
459         // Close the preferences window.
460         
461         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