Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
camelCase: Converted remaining code that was missed
[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         
45 }
47 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
48 {
49         
50         // Setup the preferences filename string. Default is the
51         // *nix systems one (/home/$USER/.xestiacal/preferences)
52     
53         prefFilename = GetUserPrefDir();
55         preferences = new XCALPreferences(prefFilename);
56   
57         // Setup the General Tab.
58     
59         bool saveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
60         bool hideLocalABs = preferences->GetBoolData(wxT("HideLocalCalendars"));
61     
62         if (saveWindowPos == TRUE){
63     
64                 chkSaveWindowPosition->SetValue(TRUE);
65     
66         }
67     
68         if (hideLocalABs == TRUE){
69     
70                 chkHideLocal->SetValue(TRUE);
71     
72         }
73     
74         // Setup the Themes tab.
76         wxListItem themecol0;
77     
78         themecol0.SetId(0);
79         themecol0.SetWidth(250);
80         themecol0.SetText(_("Theme"));
81         lstThemes->InsertColumn(0,themecol0);
82     
83         // Setup the Accounts tab.
84     
85         ReloadAccounts();
86         
87 }
89 frmPreferences::~frmPreferences(){
90         
91         // Destory the preferences window.
92         
93         delete accImgList;
94         accImgList = NULL;
95         
96 }
98 void frmPreferences::ReloadAccounts(){
100         // Reload the accounts in the accounts list.
101         
102         if (firstLoad == FALSE){
103                 delete preferences;
104                 preferences = NULL;
105                 preferences = new XCALPreferences(prefFilename);
106         } else {
107                 firstLoad = FALSE;
108         }
110         wxListItem accountscol0, accountscol1, accountscol2, accountscol3;
112         lstAccounts->ClearAll();
114         accountscol0.SetId(0);
115         accountscol0.SetWidth(24);
116         lstAccounts->InsertColumn(0,accountscol0);
117         lstAccounts->SetImageList(accImgList, wxIMAGE_LIST_SMALL);
119         accountscol1.SetId(1);
120         accountscol1.SetText(_("Name"));
121         accountscol1.SetWidth(224);    
122         lstAccounts->InsertColumn(1,accountscol1);   
124         accountscol2.SetId(2);
125         accountscol2.SetText(_("Type"));
126         accountscol2.SetWidth(96);
127         lstAccounts->InsertColumn(2,accountscol2);
128     
129         wxString accType;
130     
131         for (int i = 0; i < preferences->accounts.GetCount() ; i++){
133                 wxListItem col0;
134                 col0.SetId(i);
135                 
136                 /* if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
137         
138                         col0.SetImage(AccountNetID);
139         
140                 } else  */
141                 
142                 if (preferences->accounts.GetAccountType(i) == wxT("Local")){
143         
144                         col0.SetImage(accountID);
145         
146                 } else {
148                         col0.SetImage(accountUnsupportedID);
149                         
150                 }
151         
152                 long itemindex = lstAccounts->InsertItem( col0 );
153         
154                 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
155                 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
156                 lstAccounts->SetItemData(itemindex, i);
157         
158         }
159     
162 void frmPreferences::DisableButtons( wxListEvent& event )
164         
165         // Disable the account buttons.
166         
167         btnAccountModify->Enable(FALSE);
168         btnAccountDelete->Enable(FALSE);
169         
172 void frmPreferences::EnableButtons( wxListEvent& event )
174         
175         // Enable the account buttons.
176         
177         btnAccountModify->Enable(TRUE);
178         btnAccountDelete->Enable(TRUE);
179         
182 void frmPreferences::AddAccount( wxCommandEvent& event )
184 // TODO: Implement AddABAccount
187 void frmPreferences::ModifyAccount( wxCommandEvent& event )
190         // Get the settings for the account, setup the form for
191         // editing the account and then display the window
192         // with the settings.
193     
194         long lstAccountsIndex = -1;
195         wxString accFilename = GetAccountsFile();
196         wxString accName;
197         wxString accType;
198     
199         wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
200         
201         // Get the account name.
202     
203         lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
204                 wxLIST_NEXT_ALL,
205                 wxLIST_STATE_SELECTED);
206         
207         // Check that the account type is a supported account type.
208         
209         accType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
210         
211         if (accType != "CardDAV" && accType != "carddav" &&
212                 accType != "Local" && accType != "local"){
213         
214                 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
215                 return;
216                         
217         }
218     
219         accName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
220     
221         frmEditAccount *frameEditAccount = new frmEditAccount ( this );
222         frameEditAccount->LoadPointers(cfgFile);
223         frameEditAccount->LoadSettings(accName);
224         frameEditAccount->ShowModal();
226         bool dialogResult = frameEditAccount->GetDialogResult();
228         delete frameEditAccount;
229         frameEditAccount = NULL;
230         delete cfgFile;
231         cfgFile = NULL;
233         if (dialogResult == false){
234                 return;
235         }
237         // Reload the account list in the preferences window.
239         ReloadAccounts();
240         *reloadAccountConfig = TRUE;
241         btnAccountModify->Enable(FALSE);
242         btnAccountDelete->Enable(FALSE);
243         
246 void frmPreferences::DeleteAccount( wxCommandEvent& event )
248         
249         // Display a confirmation dialog to confirm deletion.
250     
251         long lstAccountsIndex = -1;
252         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);
253     
254         if (dlgdel.ShowModal() == wxID_YES){
255                 
256                 // Remove the selected item from the accounts list
257                 // and mark in the accounts list as deleted (Don't write to
258                 // accounts file).
259         
260                 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
261                         wxLIST_NEXT_ALL,
262                         wxLIST_STATE_SELECTED);
263         
264                 bool continueAcc = TRUE;
265                 wxString accountDir;
266                 wxString accountType;
267                 wxString accountDirFull;
268                 wxString accountDirDelFull;
269                 wxString accountName;
270                 wxString accName;
271                 long itemIndex = 0;
272         
273                 wxString accFilename = GetAccountsFile();
274     
275                 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
276         
277                 accName = preferences->accounts.GetAccountName((int)lstAccounts->GetItemData(lstAccountsIndex));
278         
279                 // Get the account directory name and account type..
280         
281                 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
282         
283                 while (continueAcc){
285                         if (accountName == accName){
287                                 cfgFile->SetPath(accountName);
288                         
289                                 cfgFile->Read("accountdir", &accountDir);
290                                 cfgFile->Read("type", &accountType);
291                 
292                                 break;
294                         }
295                 
296                         cfgFile->SetPath(wxT("/"));
297                         continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
298         
299                 }
300         
301                 accountDirFull.Append(accountDir);
302                 accountDirFull.Append(wxT("."));
303         
304                 /*if (AccountType == wxT("CalDAV")){
305         
306                         AccountDirFull.Append(wxT("local"));
307         
308                 } else */
309                 
310                 if (accountType == wxT("Local")){
311         
312                         accountDirFull.Append(wxT("Local"));
313         
314                 }/*else {
315                         
316                         AccountDirFull.Append(AccountType.Lower());
317                         
318                 }*/
319         
320                 lstAccounts->DeleteItem(lstAccountsIndex);
321         
322                 // Delete the directory that contains the account information.
323         
324                 if (!accountDirFull.IsEmpty()){
325         
326                         accountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
327                         accountDirDelFull.Append(wxT("/.xestiacal/accounts/"));
328                         accountDirDelFull.Append(accountDirFull);
329         
330                         wxRmDir(accountDirDelFull);
331         
332                 }
333         
334                 // Delete the account from the configuration file.
335         
336                 cfgFile->SetPath(wxT("/"));
337                 cfgFile->DeleteGroup(accountName);
338                 cfgFile->Flush();
339         
340                 // Set flag for reloading accounts on window closure.
341     
342                 *reloadAccountConfig = TRUE;
343     
344         }
346         btnAccountModify->Enable(FALSE);
347         btnAccountDelete->Enable(FALSE);
348         
351 void frmPreferences::SavePreferences( wxCommandEvent& event )
354         // Load up the preferences file.
356         wxString setFilename = GetSettingsFile();
358         wxFileConfig *cfgFile = new wxFileConfig("", "", setFilename);
360         // Update the settings file.
361                 
362         cfgFile->DeleteEntry(wxT("SaveWindowPosition"));
363         cfgFile->DeleteEntry(wxT("WindowPositionX"));
364         cfgFile->DeleteEntry(wxT("WindowPositionY"));
365         cfgFile->DeleteEntry(wxT("WindowPositionHeight"));
366         cfgFile->DeleteEntry(wxT("WindowPositionWidth"));
367         
368         if (chkSaveWindowPosition->GetValue() == TRUE){
369         
370                 cfgFile->Write(wxT("SaveWindowPosition"), wxT("true"));
371         
372                 // Get parent window details.
373                 
374                 frmMain *frmMainPtr = (frmMain*)this->GetParent();
376                 // Get main window position data and save it.
377         
378                 wxRect frmMainPos = frmMainPtr->GetRect();
379         
380                 cfgFile->Write(wxT("WindowPositionX"), frmMainPos.GetX());
381                 cfgFile->Write(wxT("WindowPositionY"), frmMainPos.GetY());
382                 cfgFile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight());
383                 cfgFile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth());
385         } else {
386         
387                 cfgFile->Write(wxT("SaveWindowPosition"), wxT("false"));
388         
389         }
390         
391         cfgFile->DeleteEntry(wxT("HideLocalCalendars"));
392         
393         if (chkHideLocal->GetValue() == TRUE){
395                 cfgFile->Write(wxT("HideLocalCalendars"), wxT("true"));
396                 *reloadAccountConfig = TRUE;
397         
398         } else {
399         
400                 cfgFile->Write(wxT("HideLocalCalendars"), wxT("false"));
401                 *reloadAccountConfig = TRUE;
402         
403         }
405         delete cfgFile;
406         cfgFile = NULL;
408         this->Close();
409         
412 void frmPreferences::CloseWindow( wxCommandEvent& event )
414         
415         // Close the preferences window.
416         
417         this->Close();
418         
421 void frmPreferences::SetupPointers(bool *reloadAccountInc){
423         // Setup the pointers for the preferences form.
424         
425         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