Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Enhancement to Modify Account button in the Preferences window.
[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         wxMemoryInputStream astream(icons_accinet_png, sizeof(icons_accinet_png));
43         wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png));
44         
45         wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG);
46         wxBitmap AccInet(icons_accinet_png, -1);
47         wxIcon wxIAccInet;
48         wxIAccInet.CopyFromBitmap(AccInet);
50         wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
51         wxBitmap AccNIcon(icons_acclocal_png, -1);
52         wxIcon wxIAccNIcon;
53         wxIAccNIcon.CopyFromBitmap(AccNIcon);
54         
55         AccountID = AccImgList->Add(wxIAccNIcon);
56         AccountNetID = AccImgList->Add(wxIAccInet);
58         NbtPreferences->RemovePage(1);
60 }
62 frmPreferences::~frmPreferences(){
64         delete AccImgList;
65         AccImgList = NULL;
67 }
69 void frmPreferences::EnableABButtons( wxListEvent& event )
70 {
71     btnAccountModify->Enable(TRUE);
72     btnAccountDelete->Enable(TRUE);    
73 }
75 void frmPreferences::DisableABButtons( wxListEvent& event )
76 {
77     btnAccountModify->Enable(FALSE);
78     btnAccountDelete->Enable(FALSE);    
79 }
81 void frmPreferences::AddABAccount(wxCommandEvent& event)
82 {
84         bool ReloadAccountsData = FALSE;
86         frmNewAccount *frameNewAccount = new frmNewAccount(this);
87         frameNewAccount->SetupPointers(&ReloadAccountsData);
88         frameNewAccount->ShowModal();
89         delete frameNewAccount;
90         frameNewAccount = NULL;
92         if (ReloadAccountsData == TRUE){
94                 // Reload the accounts as a change has been made within
95                 // the application.
97                 ReloadAccounts();
98                 *ReloadAccountConfig = TRUE;
100         }
104 void frmPreferences::ModifyABAccount( wxCommandEvent& event )
107     // Get the settings for the account, setup the form for
108     // editing the account and then display the window
109     // with the settings.
110     
111     long lstAccountsIndex = -1;
112     wxString AccFilename = GetAccountsFile();
113     wxString AccName;
114     
115     wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
116     
117     // Get the account name.
118     
119     lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
120                                 wxLIST_NEXT_ALL,
121                                 wxLIST_STATE_SELECTED);
122     
123     AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
124     
125     frmEditAccount *frameEditAccount = new frmEditAccount ( this );
126     frameEditAccount->LoadPointers(cfgfile);
127     frameEditAccount->LoadSettings(AccName);
128     frameEditAccount->ShowModal();
130     bool DialogResult = frameEditAccount->GetDialogResult();
132     delete frameEditAccount;
133     frameEditAccount = NULL;
134     delete cfgfile;
135     cfgfile = NULL;
137     if (DialogResult == false){
138             return;
139     }
141     // Reload the account list in the preferences window.
143     ReloadAccounts();
144     *ReloadAccountConfig = TRUE;
145     btnAccountModify->Enable(FALSE);
146     btnAccountDelete->Enable(FALSE);
147     
150 void frmPreferences::DeleteABAccount( wxCommandEvent& event )
152     // Display a confirmation dialog to confirm deletion.
153     
154     long lstAccountsIndex = -1;
155     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);
156     
157     if (dlgdel.ShowModal() == wxID_YES){
158         // Remove the selected item from the accounts list
159         // and mark in the accounts list as deleted (Don't write to
160         // accounts file).
161         
162         lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
163                                          wxLIST_NEXT_ALL,
164                                          wxLIST_STATE_SELECTED);
165         
166         bool ContinueAcc = TRUE;
167         wxString AccountDir;
168         wxString AccountType;
169         wxString AccountDirFull;
170         wxString AccountDirDelFull;
171         wxString AccountName;
172         wxString AccName;
173         long itemindex = 0;
174         
175         wxString AccFilename = GetAccountsFile();
176     
177         wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
178         
179         AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
180         
181         // Get the account directory name and account type..
182         
183         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
184         
185         while (ContinueAcc){
187                 if (AccountName == AccName){
189                         cfgfile->SetPath(AccountName);
190                         
191                         cfgfile->Read("accountdir", &AccountDir);
192                         cfgfile->Read("type", &AccountType);
193                 
194                         break;
196                 }
197                 
198                 cfgfile->SetPath(wxT("/"));
199                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
201         }
202         
203         AccountDirFull.Append(AccountDir);
204         AccountDirFull.Append(wxT("."));
205         
206         if (AccountType == wxT("CardDAV")){
207         
208                 AccountDirFull.Append(wxT("local"));
209         
210         } else if (AccountType == wxT("Local")){
211         
212                 AccountDirFull.Append(wxT("carddav"));
213         
214         }
215         
216         lstAccounts->DeleteItem(lstAccountsIndex);
217         
218         // Delete the directory that contains the account information.
219         
220         if (!AccountDirFull.IsEmpty()){
221         
222                 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
223                 AccountDirDelFull.Append(wxT("/.xestiaab/accounts/"));
224                 AccountDirDelFull.Append(AccountDirFull);
225         
226                 wxRmDir(AccountDirDelFull);
227         
228         }
229         
230         // Delete the account from the configuration file.
231         
232         cfgfile->SetPath(wxT("/"));
233         cfgfile->DeleteGroup(AccountName);
234         cfgfile->Flush();
235         
236         // Set flag for reloading accounts on window closure.
237     
238         *ReloadAccountConfig = TRUE;
239     
240     }
244 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
247 // Setup the preferences filename string. Default is the
248 // *nix systems one (/home/$USER/.xestiaab/preferences)
249     
250         preffilename = GetUserPrefDir();
252     preferences = new XABPreferences(preffilename);
253   
254     // Setup the General Tab.
255     
256     bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
257     bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks"));
258     
259     if (SaveWindowPos == TRUE){
260     
261         chkSaveWindowPosition->SetValue(TRUE);
262     
263     }
264     
265     if (HideLocalABs == TRUE){
266     
267         chkHideLocal->SetValue(TRUE);
268     
269     }
270     
271     // Setup the Themes tab.
273     wxListItem themecol0;
274     
275     themecol0.SetId(0);
276     themecol0.SetWidth(250);
277     themecol0.SetText(_("Theme"));
278     lstThemes->InsertColumn(0,themecol0);
279     
280     // Setup the Accounts tab.
281     
282     ReloadAccounts();   
283     
286 void frmPreferences::ReloadAccounts(){
288     if (FirstLoad == FALSE){
289         delete preferences;
290         preferences = NULL;
291         preferences = new XABPreferences(preffilename);    
292     } else {
293         FirstLoad = FALSE;
294     }
296     wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
298     lstAccounts->ClearAll();
300     accountscol0.SetId(0);
301     accountscol0.SetWidth(24);
302     lstAccounts->InsertColumn(0,accountscol0);
303     lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL);
305     accountscol1.SetId(1);
306     accountscol1.SetText(_("Name"));
307     accountscol1.SetWidth(224);    
308     lstAccounts->InsertColumn(1,accountscol1);   
310     accountscol2.SetId(2);
311     accountscol2.SetText(_("Type"));
312     accountscol2.SetWidth(96);
313     lstAccounts->InsertColumn(2,accountscol2);
314     
315     /*accountscol1.SetId(1);
316     accountscol1.SetText(_(""));
317     accountscol1.SetWidth(32);
318     lstAccounts->InsertColumn(1,accountscol1);
319     
320     accountscol2.SetId(2);
321     accountscol2.SetText(_("Name"));
322     accountscol2.SetWidth(224);    
323     lstAccounts->InsertColumn(2,accountscol2);
324     
325     accountscol3.SetId(3);
326     accountscol3.SetText(_("Type"));
327     accountscol3.SetWidth(96);
328     lstAccounts->InsertColumn(3,accountscol3);*/
329     
330     wxString AccType;
331     
332     for (int i = 0; i < preferences->accounts.GetCount() ; i++){
334         wxListItem col0;
335         col0.SetId(i);
336         if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){
337         
338                 col0.SetImage(AccountNetID);
339         
340         } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){
341         
342                 col0.SetImage(AccountID);
343         
344         }
345     
346         //col0.SetText( wxString::Format(wxT("%i"),i) );
347         //col0.SetText(preferences.accounts.GetAccountName(i));
348         
349         long itemindex = lstAccounts->InsertItem( col0 );
350         
351         //AccType = preferences.accounts.GetAccountType(i);
352         //AccType.Trim();
353         
354         lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
355         lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
356         //lstAccounts->SetItem(itemindex, 3, AccType);
357         
358     }
359     
362 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
364         ReloadAccountConfig = ReloadAccountInc;
368 void frmPreferences::SavePreferences(wxCommandEvent &event){
370         // Load up the preferences file.
372         wxString SetFilename = GetSettingsFile();
374         wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename);
376         // Update the settings file.
377                 
378         cfgfile->DeleteEntry(wxT("SaveWindowPosition"));
379         cfgfile->DeleteEntry(wxT("WindowPositionX"));
380         cfgfile->DeleteEntry(wxT("WindowPositionY"));
381         cfgfile->DeleteEntry(wxT("WindowPositionHeight"));
382         cfgfile->DeleteEntry(wxT("WindowPositionWidth"));
383         
384         if (chkSaveWindowPosition->GetValue() == TRUE){
385         
386                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
387         
388                 // Get parent window details.
389                 
390                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
392                 // Get main window position data and save it.
393         
394                 wxRect frmMainPos = frmMainPtr->GetRect();
395         
396                 cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
397                 cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
398                 cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
399                 cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
401         } else {
402         
403                 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
404         
405         }
406         
407         cfgfile->DeleteEntry(wxT("HideLocalAddressBooks"));
408         
409         if (chkHideLocal->GetValue() == TRUE){
411                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true"));
412                 *ReloadAccountConfig = TRUE;
413         
414         } else {
415         
416                 cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false"));
417                 *ReloadAccountConfig = TRUE;
418         
419         }
421         delete cfgfile;
422         cfgfile = NULL;
424         this->Close();
428 void frmPreferences::CloseWindow(wxCommandEvent &event){
430         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