Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmPreferences: Make the General tab be the first tab that is shown
[xestiacalendar/.git] / source / forms / preferences / frmPreferences.cpp
1 // frmPreferences.cpp - frmPreferences form functions
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar 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 Calendar 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 Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "frmPreferences.h"
21 frmPreferences::frmPreferences( wxWindow* parent )
22 :
23 frmPreferencesADT( parent )
24 {
26         wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png));
27         wxMemoryInputStream cstream(icons_accunsupported_png, sizeof(icons_accunsupported_png));
29         wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG);
30         wxBitmap accNIcon(icons_acclocal_png, -1);
31         wxIcon wxIAccNIcon;
32         wxIAccNIcon.CopyFromBitmap(accNIcon);
33         
34         wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
35         wxBitmap accUIcon(icons_accunsupported_png, -1);
36         wxIcon wxIAccUIcon;
37         wxIAccUIcon.CopyFromBitmap(accUIcon);
38         
39         accountID = accImgList->Add(wxIAccNIcon);
40         accountUnsupportedID = accImgList->Add(wxIAccUIcon);
42         NbtPreferences->RemovePage(1);
43         btnAccountAdd->Show(false);
44     NbtPreferences->ChangeSelection(0);
45         
46 }
48 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
49 {
50         
51         // Setup the preferences filename string. Default is the
52         // *nix systems one (/home/$USER/.xestiacal/preferences)
53     
54         prefFilename = GetUserPrefDir();
56         preferences = new XCALPreferences(prefFilename);
57   
58         // Setup the General Tab.
59     
60         bool saveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
61         bool hideLocalABs = preferences->GetBoolData(wxT("HideLocalCalendars"));
62     
63         if (saveWindowPos == TRUE){
64     
65                 chkSaveWindowPosition->SetValue(TRUE);
66     
67         }
68     
69         if (hideLocalABs == TRUE){
70     
71                 chkHideLocal->SetValue(TRUE);
72     
73         }
74     
75         // Setup the Themes tab.
77         wxListItem themecol0;
78     
79         themecol0.SetId(0);
80         themecol0.SetWidth(250);
81         themecol0.SetText(_("Theme"));
82         lstThemes->InsertColumn(0,themecol0);
83     
84         // Setup the Accounts tab.
85     
86         ReloadAccounts();
87         
88 }
90 frmPreferences::~frmPreferences(){
91         
92         // Destory the preferences window.
93         
94         delete accImgList;
95         accImgList = NULL;
96         
97 }
99 void frmPreferences::ReloadAccounts(){
101         // Reload the accounts in the accounts list.
102         
103         if (firstLoad == FALSE){
104                 delete preferences;
105                 preferences = NULL;
106                 preferences = new XCALPreferences(prefFilename);
107         } else {
108                 firstLoad = FALSE;
109         }
111         wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
113         lstAccounts->ClearAll();
115         accountscol0.SetId(0);
116         accountscol0.SetWidth(24);
117         lstAccounts->InsertColumn(0,accountscol0);
118         lstAccounts->SetImageList(accImgList, wxIMAGE_LIST_SMALL);
120         accountscol1.SetId(1);
121         accountscol1.SetText(_("Name"));
122         accountscol1.SetWidth(224);    
123         lstAccounts->InsertColumn(1,accountscol1);   
125         accountscol2.SetId(2);
126         accountscol2.SetText(_("Type"));
127         accountscol2.SetWidth(96);
128         lstAccounts->InsertColumn(2,accountscol2);
129     
130         wxString accType;
131     
132         for (int i = 0; i < preferences->accounts.GetCount() ; i++){
134                 wxListItem col0;
135                 col0.SetId(i);
136                 
137                 /* if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
138         
139                         col0.SetImage(AccountNetID);
140         
141                 } else  */
142                 
143                 if (preferences->accounts.GetAccountType(i) == wxT("Local")){
144         
145                         col0.SetImage(accountID);
146         
147                 } else {
149                         col0.SetImage(accountUnsupportedID);
150                         
151                 }
152         
153                 long itemindex = lstAccounts->InsertItem( col0 );
154         
155                 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
156                 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
157                 lstAccounts->SetItemData(itemindex, i);
158         
159         }
160     
163 void frmPreferences::DisableButtons( wxListEvent& event )
165         
166         // Disable the account buttons.
167         
168         btnAccountModify->Enable(FALSE);
169         btnAccountDelete->Enable(FALSE);
170         
173 void frmPreferences::EnableButtons( wxListEvent& event )
175         
176         // Enable the account buttons.
177         
178         btnAccountModify->Enable(TRUE);
179         btnAccountDelete->Enable(TRUE);
180         
183 void frmPreferences::AddAccount( wxCommandEvent& event )
185 // TODO: Implement AddABAccount
188 void frmPreferences::ModifyAccount( wxCommandEvent& event )
191         // Get the settings for the account, setup the form for
192         // editing the account and then display the window
193         // with the settings.
194     
195         long lstAccountsIndex = -1;
196         wxString accFilename = GetAccountsFile();
197         wxString accName;
198         wxString accType;
199     
200         wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
201         
202         // Get the account name.
203     
204         lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
205                 wxLIST_NEXT_ALL,
206                 wxLIST_STATE_SELECTED);
207         
208         // Check that the account type is a supported account type.
209         
210         accType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
211         
212         if (accType != "CardDAV" && accType != "carddav" &&
213                 accType != "Local" && accType != "local"){
214         
215                 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
216                 return;
217                         
218         }
219     
220         accName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
221     
222         frmEditAccount *frameEditAccount = new frmEditAccount ( this );
223         frameEditAccount->LoadPointers(cfgFile);
224         frameEditAccount->LoadSettings(accName);
225         frameEditAccount->ShowModal();
227         bool dialogResult = frameEditAccount->GetDialogResult();
229         delete frameEditAccount;
230         frameEditAccount = NULL;
231         delete cfgFile;
232         cfgFile = NULL;
234         if (dialogResult == false){
235                 return;
236         }
238         // Reload the account list in the preferences window.
240         ReloadAccounts();
241         *reloadAccountConfig = TRUE;
242         btnAccountModify->Enable(FALSE);
243         btnAccountDelete->Enable(FALSE);
244         
247 void frmPreferences::DeleteAccount( wxCommandEvent& event )
249         
250         // Display a confirmation dialog to confirm deletion.
251     
252         long lstAccountsIndex = -1;
253         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);
254     
255         if (dlgdel.ShowModal() == wxID_YES){
256                 
257                 // Remove the selected item from the accounts list
258                 // and mark in the accounts list as deleted (Don't write to
259                 // accounts file).
260         
261                 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
262                         wxLIST_NEXT_ALL,
263                         wxLIST_STATE_SELECTED);
264         
265                 bool continueAcc = TRUE;
266                 wxString accountDir;
267                 wxString accountType;
268                 wxString accountDirFull;
269                 wxString accountDirDelFull;
270                 wxString accountName;
271                 wxString accName;
272                 long itemIndex = 0;
273         
274                 wxString accFilename = GetAccountsFile();
275     
276                 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
277         
278                 accName = preferences->accounts.GetAccountName((int)lstAccounts->GetItemData(lstAccountsIndex));
279         
280                 // Get the account directory name and account type..
281         
282                 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
283         
284                 while (continueAcc){
286                         if (accountName == accName){
288                                 cfgFile->SetPath(accountName);
289                         
290                                 cfgFile->Read("accountdir", &accountDir);
291                                 cfgFile->Read("type", &accountType);
292                 
293                                 break;
295                         }
296                 
297                         cfgFile->SetPath(wxT("/"));
298                         continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
299         
300                 }
301         
302                 accountDirFull.Append(accountDir);
303                 accountDirFull.Append(wxT("."));
304         
305                 /*if (AccountType == wxT("CalDAV")){
306         
307                         AccountDirFull.Append(wxT("local"));
308         
309                 } else */
310                 
311                 if (accountType == wxT("Local")){
312         
313                         accountDirFull.Append(wxT("Local"));
314         
315                 }/*else {
316                         
317                         AccountDirFull.Append(AccountType.Lower());
318                         
319                 }*/
320         
321                 lstAccounts->DeleteItem(lstAccountsIndex);
322         
323                 // Delete the directory that contains the account information.
324         
325                 if (!accountDirFull.IsEmpty()){
326         
327                         accountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
328                         accountDirDelFull.Append(wxT("/.xestiacal/accounts/"));
329                         accountDirDelFull.Append(accountDirFull);
330         
331                         wxRmDir(accountDirDelFull);
332         
333                 }
334         
335                 // Delete the account from the configuration file.
336         
337                 cfgFile->SetPath(wxT("/"));
338                 cfgFile->DeleteGroup(accountName);
339                 cfgFile->Flush();
340         
341                 // Set flag for reloading accounts on window closure.
342     
343                 *reloadAccountConfig = TRUE;
344     
345         }
347         btnAccountModify->Enable(FALSE);
348         btnAccountDelete->Enable(FALSE);
349         
352 void frmPreferences::SavePreferences( wxCommandEvent& event )
355         // Load up the preferences file.
357         wxString setFilename = GetSettingsFile();
359         wxFileConfig *cfgFile = new wxFileConfig("", "", setFilename);
361         // Update the settings file.
362                 
363         cfgFile->DeleteEntry(wxT("SaveWindowPosition"));
364         cfgFile->DeleteEntry(wxT("WindowPositionX"));
365         cfgFile->DeleteEntry(wxT("WindowPositionY"));
366         cfgFile->DeleteEntry(wxT("WindowPositionHeight"));
367         cfgFile->DeleteEntry(wxT("WindowPositionWidth"));
368         
369         if (chkSaveWindowPosition->GetValue() == TRUE){
370         
371                 cfgFile->Write(wxT("SaveWindowPosition"), wxT("true"));
372         
373                 // Get parent window details.
374                 
375                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
377                 // Get main window position data and save it.
378         
379                 wxRect frmMainPos = frmMainPtr->GetRect();
380         
381                 cfgFile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
382                 cfgFile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
383                 cfgFile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
384                 cfgFile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
386         } else {
387         
388                 cfgFile->Write(wxT("SaveWindowPosition"), wxT("false"));
389         
390         }
391         
392         cfgFile->DeleteEntry(wxT("HideLocalCalendars"));
393         
394         if (chkHideLocal->GetValue() == TRUE){
396                 cfgFile->Write(wxT("HideLocalCalendars"), wxT("true"));
397                 *reloadAccountConfig = TRUE;
398         
399         } else {
400         
401                 cfgFile->Write(wxT("HideLocalCalendars"), wxT("false"));
402                 *reloadAccountConfig = TRUE;
403         
404         }
406         delete cfgFile;
407         cfgFile = NULL;
409         this->Close();
410         
413 void frmPreferences::CloseWindow( wxCommandEvent& event )
415         
416         // Close the preferences window.
417         
418         this->Close();
419         
422 void frmPreferences::SetupPointers(bool *reloadAccountInc){
424         // Setup the pointers for the preferences form.
425         
426         reloadAccountConfig = reloadAccountInc;
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