1 // frmPreferences.cpp - frmPreferences form functions
3 // (c) 2016-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar.
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.
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.
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 )
23 frmPreferencesADT( parent )
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);
33 wxIAccNIcon.CopyFromBitmap(accNIcon);
35 wxImage icons_accinet_png(bstream, wxBITMAP_TYPE_PNG);
36 wxBitmap accIIcon(icons_accinet_png, -1);
38 wxIAccIIcon.CopyFromBitmap(accIIcon);
40 wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
41 wxBitmap accUIcon(icons_accunsupported_png, -1);
43 wxIAccUIcon.CopyFromBitmap(accUIcon);
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);
55 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
58 // Setup the preferences filename string. Default is the
59 // *nix systems one (/home/$USER/.xestiacal/preferences)
61 prefFilename = GetUserPrefDir();
63 preferences = new XCALPreferences(prefFilename);
65 // Setup the General Tab.
67 bool saveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
68 bool hideLocalABs = preferences->GetBoolData(wxT("HideLocalCalendars"));
70 if (saveWindowPos == TRUE){
72 chkSaveWindowPosition->SetValue(TRUE);
76 if (hideLocalABs == TRUE){
78 chkHideLocal->SetValue(TRUE);
82 // Setup the Themes tab.
87 themecol0.SetWidth(250);
88 themecol0.SetText(_("Theme"));
89 lstThemes->InsertColumn(0,themecol0);
91 // Setup the Accounts tab.
97 frmPreferences::~frmPreferences(){
99 // Destory the preferences window.
106 void frmPreferences::ReloadAccounts(){
108 // Reload the accounts in the accounts list.
110 if (firstLoad == FALSE){
113 preferences = new XCALPreferences(prefFilename);
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);
139 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
144 /* if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
146 col0.SetImage(AccountNetID);
150 if (preferences->accounts.GetAccountType(i) == wxT("Local")){
152 col0.SetImage(accountID);
154 } else if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
156 col0.SetImage(accountNetID);
160 col0.SetImage(accountUnsupportedID);
164 long itemindex = lstAccounts->InsertItem( col0 );
166 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
167 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
168 lstAccounts->SetItemData(itemindex, i);
174 void frmPreferences::DisableButtons( wxListEvent& event )
177 // Disable the account buttons.
179 btnAccountModify->Enable(FALSE);
180 btnAccountDelete->Enable(FALSE);
184 void frmPreferences::EnableButtons( wxListEvent& event )
187 // Enable the account buttons.
189 btnAccountModify->Enable(TRUE);
190 btnAccountDelete->Enable(TRUE);
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.
206 long lstAccountsIndex = -1;
207 wxString accFilename = GetAccountsFile();
211 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
213 // Get the account name.
215 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
217 wxLIST_STATE_SELECTED);
219 // Check that the account type is a supported account type.
221 accType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
223 if (accType != "CalDAV" && accType != "caldav" &&
224 accType != "Local" && accType != "local"){
226 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
231 accName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
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;
245 if (dialogResult == false){
249 // Reload the account list in the preferences window.
252 *reloadAccountConfig = TRUE;
253 btnAccountModify->Enable(FALSE);
254 btnAccountDelete->Enable(FALSE);
258 void frmPreferences::DeleteAccount( wxCommandEvent& event )
261 // Display a confirmation dialog to confirm deletion.
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);
266 if (dlgdel.ShowModal() == wxID_YES){
268 // Remove the selected item from the accounts list
269 // and mark in the accounts list as deleted (Don't write to
272 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
274 wxLIST_STATE_SELECTED);
276 bool continueAcc = TRUE;
278 wxString accountType;
279 wxString accountDirFull;
280 wxString accountDirDelFull;
281 wxString accountName;
285 wxString accFilename = GetAccountsFile();
287 wxFileConfig *cfgFile = new wxFileConfig("", "", accFilename);
289 accName = preferences->accounts.GetAccountName((int)lstAccounts->GetItemData(lstAccountsIndex));
291 // Get the account directory name and account type..
293 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
297 if (accountName == accName){
299 cfgFile->SetPath(accountName);
301 cfgFile->Read("accountdir", &accountDir);
302 cfgFile->Read("type", &accountType);
308 cfgFile->SetPath(wxT("/"));
309 continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
313 accountDirFull.Append(accountDir);
314 accountDirFull.Append(wxT("."));
316 if (accountType == wxT("CalDAV")){
318 accountDirFull.Append(wxT("CalDAV"));
320 } else if (accountType == wxT("Local")){
322 accountDirFull.Append(wxT("Local"));
326 lstAccounts->DeleteItem(lstAccountsIndex);
328 // Delete the directory that contains the account information.
330 if (!accountDirFull.IsEmpty()){
332 #if defined(__WIN32__)
334 accountDirDelFull.Append(GetUserDir());
335 accountDirDelFull.Append(wxT("\\accounts\\"));
336 accountDirDelFull.Append(accountDirFull);
340 accountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
341 accountDirDelFull.Append(wxT("/.xestiacal/accounts/"));
342 accountDirDelFull.Append(accountDirFull);
346 wxRmDir(accountDirDelFull);
350 // Delete the account from the configuration file.
352 cfgFile->SetPath(wxT("/"));
353 cfgFile->DeleteGroup(accountName);
356 // Set flag for reloading accounts on window closure.
358 *reloadAccountConfig = TRUE;
362 btnAccountModify->Enable(FALSE);
363 btnAccountDelete->Enable(FALSE);
367 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.
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"));
384 if (chkSaveWindowPosition->GetValue() == TRUE){
386 cfgFile->Write(wxT("SaveWindowPosition"), wxT("true"));
388 // Get parent window details.
390 frmMain *frmMainPtr = (frmMain*)this->GetParent();
392 // Get main window position data and save it.
394 wxRect frmMainPos = frmMainPtr->GetRect();
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());
403 cfgFile->Write(wxT("SaveWindowPosition"), wxT("false"));
407 cfgFile->DeleteEntry(wxT("HideLocalCalendars"));
409 if (chkHideLocal->GetValue() == TRUE){
411 cfgFile->Write(wxT("HideLocalCalendars"), wxT("true"));
412 *reloadAccountConfig = TRUE;
416 cfgFile->Write(wxT("HideLocalCalendars"), wxT("false"));
417 *reloadAccountConfig = TRUE;
428 void frmPreferences::CloseWindow( wxCommandEvent& event )
431 // Close the preferences window.
437 void frmPreferences::SetupPointers(bool *reloadAccountInc){
439 // Setup the pointers for the preferences form.
441 reloadAccountConfig = reloadAccountInc;