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 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);
32 wxIAccNIcon.CopyFromBitmap(AccNIcon);
34 wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG);
35 wxBitmap AccUIcon(icons_accunsupported_png, -1);
37 wxIAccUIcon.CopyFromBitmap(AccUIcon);
39 AccountID = AccImgList->Add(wxIAccNIcon);
40 AccountUnsupportedID = AccImgList->Add(wxIAccUIcon);
42 NbtPreferences->RemovePage(1);
43 btnAccountAdd->Show(false);
47 void frmPreferences::LoadPreferences( wxInitDialogEvent& event )
50 // Setup the preferences filename string. Default is the
51 // *nix systems one (/home/$USER/.xestiacal/preferences)
53 preffilename = GetUserPrefDir();
55 preferences = new XCALPreferences(preffilename);
57 // Setup the General Tab.
59 bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition"));
60 bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalCalendars"));
62 if (SaveWindowPos == TRUE){
64 chkSaveWindowPosition->SetValue(TRUE);
68 if (HideLocalABs == TRUE){
70 chkHideLocal->SetValue(TRUE);
74 // Setup the Themes tab.
79 themecol0.SetWidth(250);
80 themecol0.SetText(_("Theme"));
81 lstThemes->InsertColumn(0,themecol0);
83 // Setup the Accounts tab.
89 frmPreferences::~frmPreferences(){
91 // Destory the preferences window.
98 void frmPreferences::ReloadAccounts(){
100 // Reload the accounts in the accounts list.
102 if (FirstLoad == FALSE){
105 preferences = new XCALPreferences(preffilename);
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);
131 for (int i = 0; i < preferences->accounts.GetCount() ; i++){
136 /* if (preferences->accounts.GetAccountType(i) == wxT("CalDAV")){
138 col0.SetImage(AccountNetID);
142 if (preferences->accounts.GetAccountType(i) == wxT("Local")){
144 col0.SetImage(AccountID);
148 col0.SetImage(AccountUnsupportedID);
152 long itemindex = lstAccounts->InsertItem( col0 );
154 lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
155 lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
156 lstAccounts->SetItemData(itemindex, i);
162 void frmPreferences::DisableButtons( wxListEvent& event )
165 // Disable the account buttons.
167 btnAccountModify->Enable(FALSE);
168 btnAccountDelete->Enable(FALSE);
172 void frmPreferences::EnableButtons( wxListEvent& event )
175 // Enable the account buttons.
177 btnAccountModify->Enable(TRUE);
178 btnAccountDelete->Enable(TRUE);
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.
194 long lstAccountsIndex = -1;
195 wxString AccFilename = GetAccountsFile();
199 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
201 // Get the account name.
203 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
205 wxLIST_STATE_SELECTED);
207 // Check that the account type is a supported account type.
209 AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex);
211 if (AccType != "CardDAV" && AccType != "carddav" &&
212 AccType != "Local" && AccType != "local"){
214 wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR);
219 AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
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;
233 if (DialogResult == false){
237 // Reload the account list in the preferences window.
240 *ReloadAccountConfig = TRUE;
241 btnAccountModify->Enable(FALSE);
242 btnAccountDelete->Enable(FALSE);
246 void frmPreferences::DeleteAccount( wxCommandEvent& event )
249 // Display a confirmation dialog to confirm deletion.
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);
254 if (dlgdel.ShowModal() == wxID_YES){
256 // Remove the selected item from the accounts list
257 // and mark in the accounts list as deleted (Don't write to
260 lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
262 wxLIST_STATE_SELECTED);
264 bool ContinueAcc = TRUE;
266 wxString AccountType;
267 wxString AccountDirFull;
268 wxString AccountDirDelFull;
269 wxString AccountName;
273 wxString AccFilename = GetAccountsFile();
275 wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
277 AccName = preferences->accounts.GetAccountName((int)lstAccounts->GetItemData(lstAccountsIndex));
279 // Get the account directory name and account type..
281 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
285 if (AccountName == AccName){
287 cfgfile->SetPath(AccountName);
289 cfgfile->Read("accountdir", &AccountDir);
290 cfgfile->Read("type", &AccountType);
296 cfgfile->SetPath(wxT("/"));
297 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
301 AccountDirFull.Append(AccountDir);
302 AccountDirFull.Append(wxT("."));
304 /*if (AccountType == wxT("CalDAV")){
306 AccountDirFull.Append(wxT("local"));
310 if (AccountType == wxT("Local")){
312 AccountDirFull.Append(wxT("Local"));
316 AccountDirFull.Append(AccountType.Lower());
320 lstAccounts->DeleteItem(lstAccountsIndex);
322 // Delete the directory that contains the account information.
324 if (!AccountDirFull.IsEmpty()){
326 AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME")));
327 AccountDirDelFull.Append(wxT("/.xestiacal/accounts/"));
328 AccountDirDelFull.Append(AccountDirFull);
330 wxRmDir(AccountDirDelFull);
334 // Delete the account from the configuration file.
336 cfgfile->SetPath(wxT("/"));
337 cfgfile->DeleteGroup(AccountName);
340 // Set flag for reloading accounts on window closure.
342 *ReloadAccountConfig = TRUE;
346 btnAccountModify->Enable(FALSE);
347 btnAccountDelete->Enable(FALSE);
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.
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"));
368 if (chkSaveWindowPosition->GetValue() == TRUE){
370 cfgfile->Write(wxT("SaveWindowPosition"), wxT("true"));
372 // Get parent window details.
374 frmMain *frmMainPtr = (frmMain*)this->GetParent();
376 // Get main window position data and save it.
378 wxRect frmMainPos = frmMainPtr->GetRect();
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());
387 cfgfile->Write(wxT("SaveWindowPosition"), wxT("false"));
391 cfgfile->DeleteEntry(wxT("HideLocalCalendars"));
393 if (chkHideLocal->GetValue() == TRUE){
395 cfgfile->Write(wxT("HideLocalCalendars"), wxT("true"));
396 *ReloadAccountConfig = TRUE;
400 cfgfile->Write(wxT("HideLocalCalendars"), wxT("false"));
401 *ReloadAccountConfig = TRUE;
412 void frmPreferences::CloseWindow( wxCommandEvent& event )
415 // Close the preferences window.
421 void frmPreferences::SetupPointers(bool *ReloadAccountInc){
423 // Setup the pointers for the preferences form.
425 ReloadAccountConfig = ReloadAccountInc;