// frmPreferences.cpp - Preferences form. // // (c) 2012-2015 Xestia Software Development. // // This file is part of Xestia Address Book. // // Xestia Address Book is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by the // Free Software Foundation, version 3 of the license. // // Xestia Address Book is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with Xestia Address Book. If not, see #include "frmPreferences.h" #include "frmEditAccount.h" #include "frmNewAccount.h" #include "frmMain.h" #include "common/preferences.h" #include "common/dirs.h" #include "Bitmaps.h" #include "import/import.h" #include "export/export.h" #include #include #include #include #include frmPreferences::frmPreferences( wxWindow* parent ) : frmPreferencesADT( parent ) { // Setup the preferences window. wxMemoryInputStream astream(icons_accinet_png, sizeof(icons_accinet_png)); wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png)); wxMemoryInputStream cstream(icons_accunsupported_png, sizeof(icons_accunsupported_png)); wxMemoryInputStream windowposstream(icons_windowpos_png, sizeof(icons_windowpos_png)); wxMemoryInputStream hideaddressbooksstream(icons_hideaddressbooks_png, sizeof(icons_hideaddressbooks_png)); wxImage icons_windowpos_png(windowposstream, wxBITMAP_TYPE_PNG); wxBitmap WindowPosition(icons_windowpos_png, -1); wxImage icons_hideaddressbooks_png(hideaddressbooksstream, wxBITMAP_TYPE_PNG); wxBitmap HideAddressBooks(icons_hideaddressbooks_png, -1); bmpWindowPosition->SetBitmap(WindowPosition); bmpLocalAddressBooks->SetBitmap(HideAddressBooks); wxImage icons_accinet_png(astream, wxBITMAP_TYPE_PNG); wxBitmap AccInet(icons_accinet_png, -1); wxIcon wxIAccInet; wxIAccInet.CopyFromBitmap(AccInet); wxImage icons_acclocal_png(bstream, wxBITMAP_TYPE_PNG); wxBitmap AccNIcon(icons_acclocal_png, -1); wxIcon wxIAccNIcon; wxIAccNIcon.CopyFromBitmap(AccNIcon); wxImage icons_accunsupported_png(cstream, wxBITMAP_TYPE_PNG); wxBitmap AccUIcon(icons_accunsupported_png, -1); wxIcon wxIAccUIcon; wxIAccUIcon.CopyFromBitmap(AccUIcon); AccountID = AccImgList->Add(wxIAccNIcon); AccountNetID = AccImgList->Add(wxIAccInet); AccountUnsupportedID = AccImgList->Add(wxIAccUIcon); NbtPreferences->RemovePage(1); } frmPreferences::~frmPreferences(){ // Destory the preferences window. delete AccImgList; AccImgList = NULL; } void frmPreferences::EnableABButtons( wxListEvent& event ) { // Enable the account buttons. btnAccountModify->Enable(TRUE); btnAccountDelete->Enable(TRUE); } void frmPreferences::DisableABButtons( wxListEvent& event ) { // Disable the account buttons. btnAccountModify->Enable(FALSE); btnAccountDelete->Enable(FALSE); } void frmPreferences::AddABAccount(wxCommandEvent& event) { // Add new account. bool ReloadAccountsData = FALSE; frmNewAccount *frameNewAccount = new frmNewAccount(this); frameNewAccount->SetupPointers(&ReloadAccountsData); frameNewAccount->ShowModal(); delete frameNewAccount; frameNewAccount = NULL; if (ReloadAccountsData == TRUE){ // Reload the accounts as a change has been made within // the application. ReloadAccounts(); *ReloadAccountConfig = TRUE; } } void frmPreferences::ModifyABAccount( wxCommandEvent& event ) { // Get the settings for the account, setup the form for // editing the account and then display the window // with the settings. long lstAccountsIndex = -1; wxString AccFilename = GetAccountsFile(); wxString AccName; wxString AccType; // Check if an account has been selected before continuing. lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); if (lstAccountsIndex == -1){ // No account has been selected so exit this subroutine. return; } wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename); // Check that the account type is a supported account type. AccType = preferences->accounts.GetAccountType((int)lstAccountsIndex); if (AccType != "CardDAV" && AccType != "carddav" && AccType != "Local" && AccType != "local"){ wxMessageBox(_("Cannot modify the selected account settings as the account type is unsupported."), _("Unsupported account type"), wxICON_ERROR); return; } // Get the account name. AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex); frmEditAccount *frameEditAccount = new frmEditAccount ( this ); frameEditAccount->LoadPointers(cfgfile); frameEditAccount->LoadSettings(AccName); frameEditAccount->ShowModal(); bool DialogResult = frameEditAccount->GetDialogResult(); delete frameEditAccount; frameEditAccount = NULL; delete cfgfile; cfgfile = NULL; if (DialogResult == false){ return; } // Reload the account list in the preferences window. ReloadAccounts(); *ReloadAccountConfig = TRUE; btnAccountModify->Enable(FALSE); btnAccountDelete->Enable(FALSE); } void frmPreferences::ModifyABAccount( wxMouseEvent& event ) { wxCommandEvent NullEvent; ModifyABAccount(NullEvent); } void frmPreferences::DeleteABAccount( wxCommandEvent& event ) { // Display a confirmation dialog to confirm deletion. long lstAccountsIndex = -1; 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); if (dlgdel.ShowModal() == wxID_YES){ // Remove the selected item from the accounts list // and mark in the accounts list as deleted (Don't write to // accounts file). lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); bool ContinueAcc = TRUE; wxString AccountDir; wxString AccountType; wxString AccountDirFull; wxString AccountDirDelFull; wxString AccountName; wxString AccName; long itemindex = 0; wxString AccFilename = GetAccountsFile(); wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename); AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex); // Get the account directory name and account type.. ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex); while (ContinueAcc){ if (AccountName == AccName){ cfgfile->SetPath(AccountName); cfgfile->Read("accountdir", &AccountDir); cfgfile->Read("type", &AccountType); break; } cfgfile->SetPath(wxT("/")); ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex); } AccountDirFull.Append(AccountDir); AccountDirFull.Append(wxT(".")); if (AccountType == wxT("CardDAV")){ AccountDirFull.Append(wxT("local")); } else if (AccountType == wxT("Local")){ AccountDirFull.Append(wxT("carddav")); } else { AccountDirFull.Append(AccountType.Lower()); } lstAccounts->DeleteItem(lstAccountsIndex); // Delete the directory that contains the account information. if (!AccountDirFull.IsEmpty()){ AccountDirDelFull.Append(wxString::FromUTF8(getenv("HOME"))); AccountDirDelFull.Append(wxT("/.xestiaab/accounts/")); AccountDirDelFull.Append(AccountDirFull); wxRmDir(AccountDirDelFull); } // Delete the account from the configuration file. cfgfile->SetPath(wxT("/")); cfgfile->DeleteGroup(AccountName); cfgfile->Flush(); // Set flag for reloading accounts on window closure. *ReloadAccountConfig = TRUE; } btnAccountModify->Enable(FALSE); btnAccountDelete->Enable(FALSE); } void frmPreferences::LoadPreferences( wxInitDialogEvent& event ) { // Setup the preferences filename string. Default is the // *nix systems one (/home/$USER/.xestiaab/preferences) preffilename = GetUserPrefDir(); preferences = new XABPreferences(preffilename); // Setup the General Tab. bool SaveWindowPos = preferences->GetBoolData(wxT("SaveWindowPosition")); bool HideLocalABs = preferences->GetBoolData(wxT("HideLocalAddressBooks")); bool UseBackgroundContactColour = preferences->GetBoolData(wxT("UseBackgroundContactColour")); if (SaveWindowPos == true){ chkSaveWindowPosition->SetValue(true); } if (HideLocalABs == true){ chkHideLocal->SetValue(true); } if (UseBackgroundContactColour == true){ chkUseBackgroundColour->SetValue(true); clpContactBackgroundColour->SetColour(preferences->GetBackgroundContactColourData()); } else { clpContactBackgroundColour->Enable(false); } // Setup the Themes tab. wxListItem themecol0; themecol0.SetId(0); themecol0.SetWidth(250); themecol0.SetText(_("Theme")); lstThemes->InsertColumn(0,themecol0); // Setup the Accounts tab. ReloadAccounts(); } void frmPreferences::ReloadAccounts(){ // Reload the accounts in the accounts list. if (FirstLoad == FALSE){ delete preferences; preferences = NULL; preferences = new XABPreferences(preffilename); } else { FirstLoad = FALSE; } wxListItem accountscol0, accountscol1, accountscol2, accountscol3; lstAccounts->ClearAll(); accountscol0.SetId(0); accountscol0.SetWidth(24); lstAccounts->InsertColumn(0,accountscol0); lstAccounts->SetImageList(AccImgList, wxIMAGE_LIST_SMALL); accountscol1.SetId(1); accountscol1.SetText(_("Name")); accountscol1.SetWidth(224); lstAccounts->InsertColumn(1,accountscol1); accountscol2.SetId(2); accountscol2.SetText(_("Type")); accountscol2.SetWidth(96); lstAccounts->InsertColumn(2,accountscol2); wxString AccType; for (int i = 0; i < preferences->accounts.GetCount() ; i++){ wxListItem col0; col0.SetId(i); if (preferences->accounts.GetAccountType(i) == wxT("CardDAV")){ col0.SetImage(AccountNetID); } else if (preferences->accounts.GetAccountType(i) == wxT("Local")){ col0.SetImage(AccountID); } else { col0.SetImage(AccountUnsupportedID); } long itemindex = lstAccounts->InsertItem( col0 ); lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i)); lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i)); } } void frmPreferences::SetupPointers(bool *ReloadAccountInc){ // Setup the pointers for the preferences form. ReloadAccountConfig = ReloadAccountInc; } void frmPreferences::SavePreferences(wxCommandEvent &event){ // Load up the preferences file. wxString SetFilename = GetSettingsFile(); wxFileConfig *cfgfile = new wxFileConfig("", "", SetFilename); // Update the settings file. cfgfile->DeleteEntry(wxT("SaveWindowPosition")); cfgfile->DeleteEntry(wxT("WindowPositionX")); cfgfile->DeleteEntry(wxT("WindowPositionY")); cfgfile->DeleteEntry(wxT("WindowPositionHeight")); cfgfile->DeleteEntry(wxT("WindowPositionWidth")); if (chkSaveWindowPosition->GetValue() == TRUE){ cfgfile->Write(wxT("SaveWindowPosition"), wxT("true")); // Get parent window details. frmMain *frmMainPtr = (frmMain*)this->GetParent(); // Get main window position data and save it. wxRect frmMainPos = frmMainPtr->GetRect(); cfgfile->Write(wxT("WindowPositionX"), frmMainPos.GetX()); cfgfile->Write(wxT("WindowPositionY"), frmMainPos.GetY()); cfgfile->Write(wxT("WindowPositionHeight"), frmMainPos.GetHeight()); cfgfile->Write(wxT("WindowPositionWidth"), frmMainPos.GetWidth()); } else { cfgfile->Write(wxT("SaveWindowPosition"), wxT("false")); } cfgfile->DeleteEntry(wxT("HideLocalAddressBooks")); if (chkHideLocal->GetValue() == TRUE){ cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("true")); *ReloadAccountConfig = TRUE; } else { cfgfile->Write(wxT("HideLocalAddressBooks"), wxT("false")); *ReloadAccountConfig = TRUE; } if (chkUseBackgroundColour->GetValue() == TRUE){ cfgfile->Write(wxT("UseBackgroundContactColour"), wxT("true")); cfgfile->Write(wxT("BackgroundContactColour"), clpContactBackgroundColour->GetColour().GetAsString(wxC2S_HTML_SYNTAX)); *ReloadAccountConfig = TRUE; } else { cfgfile->Write(wxT("UseBackgroundContactColour"), wxT("false")); *ReloadAccountConfig = TRUE; } delete cfgfile; cfgfile = NULL; this->Close(); } void frmPreferences::EnableBackgroundColourPicker(wxCommandEvent &event){ if (chkUseBackgroundColour->GetValue() == true){ clpContactBackgroundColour->Enable(true); } else { clpContactBackgroundColour->Enable(false); } } void frmPreferences::CloseWindow(wxCommandEvent &event){ // Close the preferences window. this->Close(); }