Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
a49ed419d6b4491920eb4a74e5b0e35073f0792c
[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 astream(icons_acclocal_png, sizeof(icons_acclocal_png));
27         wxMemoryInputStream bstream(icons_accinet_png, sizeof(icons_accinet_png));
28         wxMemoryInputStream cstream(icons_accunsupported_png, sizeof(icons_accunsupported_png));
30         wxImage icons_acclocal_png(astream, wxBITMAP_TYPE_PNG);
31         wxBitmap accNIcon(icons_acclocal_png, -1);
32         wxIcon wxIAccNIcon;
33         wxIAccNIcon.CopyFromBitmap(accNIcon);
35         wxImage icons_accinet_png(bstream, wxBITMAP_TYPE_PNG);
36         wxBitmap accIIcon(icons_accinet_png, -1);
37         wxIcon wxIAccIIcon;
38         wxIAccIIcon.CopyFromBitmap(accIIcon);
39         
40         wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
41         wxBitmap accUIcon(icons_accunsupported_png, -1);
42         wxIcon wxIAccUIcon;
43         wxIAccUIcon.CopyFromBitmap(accUIcon);
44         
45         accountID = accImgList->Add(wxIAccNIcon);
46         accountNetID = accImgList->Add(wxIAccIIcon);
47         accountUnsupportedID = accImgList->Add(wxIAccUIcon);
49         NbtPreferences->RemovePage(1);
50         btnAccountAdd->Show(false);
51         NbtPreferences->ChangeSelection(0);
52         
53 }
55 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
56 {
57         
58         // Setup the preferences filename string. Default is the
59         // *nix systems one (/home/$USER/.xestiacal/preferences)
60     
61         prefFilename = GetUserPrefDir();
63         preferences = new XCALPreferences(prefFilename);
64   
65         // Setup the General Tab.
66     
67         bool saveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
68         bool hideLocalABs = preferences->GetBoolData(wxT("HideLocalCalendars"));
69     
70         if (saveWindowPos == TRUE){
71     
72                 chkSaveWindowPosition->SetValue(TRUE);
73     
74         }
75     
76         if (hideLocalABs == TRUE){
77     
78                 chkHideLocal->SetValue(TRUE);
79     
80         }
81     
82         // Setup the Themes tab.
84         wxListItem themecol0;
85     
86         themecol0.SetId(0);
87         themecol0.SetWidth(250);
88         themecol0.SetText(_("Theme"));
89         lstThemes->InsertColumn(0,themecol0);
90     
91         // Setup the Accounts tab.
92     
93         ReloadAccounts();
94         
95 }
97 frmPreferences::~frmPreferences(){
98         
99         // Destory the preferences window.
100         
101         delete accImgList;
102         accImgList = NULL;
103         
106 void frmPreferences::ReloadAccounts(){
108         // Reload the accounts in the accounts list.
109         
110         if (firstLoad == FALSE){
111                 delete preferences;
112                 preferences = NULL;
113                 preferences = new XCALPreferences(prefFilename);
114         } else {
115                 firstLoad = FALSE;
116         }
118         wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
120         lstAccounts->ClearAll();
122         accountscol0.SetId(0);
123         accountscol0.SetWidth(24);
124         lstAccounts->InsertColumn(0,accountscol0);
125         lstAccounts->SetImageList(accImgList, wxIMAGE_LIST_SMALL);
127         accountscol1.SetId(1);
128         accountscol1.SetText(_("Name"));
129         accountscol1.SetWidth(224);    
130         lstAccounts->InsertColumn(1,accountscol1);   
132         accountscol2.SetId(2);
133         accountscol2.SetText(_("Type"));
134         accountscol2.SetWidth(96);
135         lstAccounts->InsertColumn(2,accountscol2);
136     
137         wxString accType;
138     
139         for (int i = 0; i < preferences->accounts.GetCount() ; i++){
141                 wxListItem col0;
142                 col0.SetId(i);
143                 
144                 /* if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
145         
146                         col0.SetImage(AccountNetID);
147         
148                 } else  */
149                 
150                 if (preferences->accounts.GetAccountType(i) == wxT("Local")){
151         
152                         col0.SetImage(accountID);
153         
154                 } else if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
155                 
156                         col0.SetImage(accountNetID);
157                 
158                 } else {
160                         col0.SetImage(accountUnsupportedID);
161                         
162                 }
163         
164                 long itemindex = lstAccounts->InsertItem( col0 );
165         
166                 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
167                 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
168                 lstAccounts->SetItemData(itemindex, i);
169         
170         }
171     
174 void frmPreferences::DisableButtons( wxListEvent& event )
176         
177         // Disable the account buttons.
178         
179         btnAccountModify->Enable(FALSE);
180         btnAccountDelete->Enable(FALSE);
181         
184 void frmPreferences::EnableButtons( wxListEvent& event )
186         
187         // Enable the account buttons.
188         
189         btnAccountModify->Enable(TRUE);
190         btnAccountDelete->Enable(TRUE);
191         
194 void frmPreferences::AddAccount( wxCommandEvent& event )
196 // TODO: Implement AddABAccount
199 void frmPreferences::ModifyAccount( wxCommandEvent& event )
202         // Get the settings for the account, setup the form for
203         // editing the account and then display the window
204         // with the settings.
205     
206         long lstAccountsIndex = -1;
207         wxString accFilename = GetAccountsFile();
208         wxString accName;
209         wxString accType;
210     
211         wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
212         
213         // Get the account name.
214     
215         lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
216                 wxLIST_NEXT_ALL,
217                 wxLIST_STATE_SELECTED);
218         
219         // Check that the account type is a supported account type.
220         
221         accType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
222         
223         if (accType != "CalDAV" && accType != "caldav" &&
224                 accType != "Local" && accType != "local"){
225         
226                 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
227                 return;
228                         
229         }
230     
231         accName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
232     
233         frmEditAccount *frameEditAccount = new frmEditAccount ( this );
234         frameEditAccount->LoadPointers(cfgFile);
235         frameEditAccount->LoadSettings(accName);
236         frameEditAccount->ShowModal();
238         bool dialogResult = frameEditAccount->GetDialogResult();
240         delete frameEditAccount;
241         frameEditAccount = NULL;
242         delete cfgFile;
243         cfgFile = NULL;
245         if (dialogResult == false){
246                 return;
247         }
249         // Reload the account list in the preferences window.
251         ReloadAccounts();
252         *reloadAccountConfig = TRUE;
253         btnAccountModify->Enable(FALSE);
254         btnAccountDelete->Enable(FALSE);
255         
258 void frmPreferences::DeleteAccount( wxCommandEvent& event )
260         
261         // Display a confirmation dialog to confirm deletion.
262     
263         long lstAccountsIndex = -1;
264         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);
265     
266         if (dlgdel.ShowModal() == wxID_YES){
267                 
268                 // Remove the selected item from the accounts list
269                 // and mark in the accounts list as deleted (Don't write to
270                 // accounts file).
271         
272                 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
273                         wxLIST_NEXT_ALL,
274                         wxLIST_STATE_SELECTED);
275         
276                 bool continueAcc = TRUE;
277                 wxString accountDir;
278                 wxString accountType;
279                 wxString accountDirFull;
280                 wxString accountDirDelFull;
281                 wxString accountName;
282                 wxString accName;
283                 long itemIndex = 0;
284         
285                 wxString accFilename = GetAccountsFile();
286     
287                 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
288         
289                 accName = preferences->accounts.GetAccountName((int)lstAccounts->GetItemData(lstAccountsIndex));
290         
291                 // Get the account directory name and account type..
292         
293                 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
294         
295                 while (continueAcc){
297                         if (accountName == accName){
299                                 cfgFile->SetPath(accountName);
300                         
301                                 cfgFile->Read("accountdir", &accountDir);
302                                 cfgFile->Read("type", &accountType);
303                 
304                                 break;
306                         }
307                 
308                         cfgFile->SetPath(wxT("/"));
309                         continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
310         
311                 }
312         
313                 accountDirFull.Append(accountDir);
314                 accountDirFull.Append(wxT("."));
315         
316                 /*if (AccountType == wxT("CalDAV")){
317         
318                         AccountDirFull.Append(wxT("local"));
319         
320                 } else */
321                 
322                 if (accountType == wxT("Local")){
323         
324                         accountDirFull.Append(wxT("Local"));
325         
326                 }/*else {
327                         
328                         AccountDirFull.Append(AccountType.Lower());
329                         
330                 }*/
331         
332                 lstAccounts->DeleteItem(lstAccountsIndex);
333         
334                 // Delete the directory that contains the account information.
335         
336                 if (!accountDirFull.IsEmpty()){
337         
338                         accountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
339                         accountDirDelFull.Append(wxT("/.xestiacal/accounts/"));
340                         accountDirDelFull.Append(accountDirFull);
341         
342                         wxRmDir(accountDirDelFull);
343         
344                 }
345         
346                 // Delete the account from the configuration file.
347         
348                 cfgFile->SetPath(wxT("/"));
349                 cfgFile->DeleteGroup(accountName);
350                 cfgFile->Flush();
351         
352                 // Set flag for reloading accounts on window closure.
353     
354                 *reloadAccountConfig = TRUE;
355     
356         }
358         btnAccountModify->Enable(FALSE);
359         btnAccountDelete->Enable(FALSE);
360         
363 void frmPreferences::SavePreferences( wxCommandEvent& event )
366         // Load up the preferences file.
368         wxString setFilename = GetSettingsFile();
370         wxFileConfig *cfgFile = new wxFileConfig("", "", setFilename);
372         // Update the settings file.
373                 
374         cfgFile->DeleteEntry(wxT("SaveWindowPosition"));
375         cfgFile->DeleteEntry(wxT("WindowPositionX"));
376         cfgFile->DeleteEntry(wxT("WindowPositionY"));
377         cfgFile->DeleteEntry(wxT("WindowPositionHeight"));
378         cfgFile->DeleteEntry(wxT("WindowPositionWidth"));
379         
380         if (chkSaveWindowPosition->GetValue() == TRUE){
381         
382                 cfgFile->Write(wxT("SaveWindowPosition"), wxT("true"));
383         
384                 // Get parent window details.
385                 
386                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
388                 // Get main window position data and save it.
389         
390                 wxRect frmMainPos = frmMainPtr->GetRect();
391         
392                 cfgFile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
393                 cfgFile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
394                 cfgFile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
395                 cfgFile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
397         } else {
398         
399                 cfgFile->Write(wxT("SaveWindowPosition"), wxT("false"));
400         
401         }
402         
403         cfgFile->DeleteEntry(wxT("HideLocalCalendars"));
404         
405         if (chkHideLocal->GetValue() == TRUE){
407                 cfgFile->Write(wxT("HideLocalCalendars"), wxT("true"));
408                 *reloadAccountConfig = TRUE;
409         
410         } else {
411         
412                 cfgFile->Write(wxT("HideLocalCalendars"), wxT("false"));
413                 *reloadAccountConfig = TRUE;
414         
415         }
417         delete cfgFile;
418         cfgFile = NULL;
420         this->Close();
421         
424 void frmPreferences::CloseWindow( wxCommandEvent& event )
426         
427         // Close the preferences window.
428         
429         this->Close();
430         
433 void frmPreferences::SetupPointers(bool *reloadAccountInc){
435         // Setup the pointers for the preferences form.
436         
437         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