// 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 )
{
wxMemoryInputStream astream(icons_accinet_png, sizeof(icons_accinet_png));
wxMemoryInputStream bstream(icons_acclocal_png, sizeof(icons_acclocal_png));
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);
AccountID = AccImgList->Add(wxIAccNIcon);
AccountNetID = AccImgList->Add(wxIAccInet);
NbtPreferences->RemovePage(1);
}
frmPreferences::~frmPreferences(){
delete AccImgList;
AccImgList = NULL;
}
void frmPreferences::EnableABButtons( wxListEvent& event )
{
btnAccountModify->Enable(TRUE);
btnAccountDelete->Enable(TRUE);
}
void frmPreferences::DisableABButtons( wxListEvent& event )
{
btnAccountModify->Enable(FALSE);
btnAccountDelete->Enable(FALSE);
}
void frmPreferences::AddABAccount(wxCommandEvent& event)
{
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;
wxFileConfig *cfgfile = new wxFileConfig("", "", AccFilename);
// Get the account name.
lstAccountsIndex = lstAccounts->GetNextItem(lstAccountsIndex,
wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED);
AccName = preferences->accounts.GetAccountName((int)lstAccountsIndex);
frmEditAccount *frameEditAccount = new frmEditAccount ( this );
frameEditAccount->LoadPointers(cfgfile);
frameEditAccount->LoadSettings(AccName);
frameEditAccount->ShowModal();
delete frameEditAccount;
frameEditAccount = NULL;
delete cfgfile;
cfgfile = NULL;
// Reload the account list in the preferences window.
ReloadAccounts();
*ReloadAccountConfig = TRUE;
}
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"));
}
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;
}
}
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"));
if (SaveWindowPos == TRUE){
chkSaveWindowPosition->SetValue(TRUE);
}
if (HideLocalABs == TRUE){
chkHideLocal->SetValue(TRUE);
}
// 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(){
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);
/*accountscol1.SetId(1);
accountscol1.SetText(_(""));
accountscol1.SetWidth(32);
lstAccounts->InsertColumn(1,accountscol1);
accountscol2.SetId(2);
accountscol2.SetText(_("Name"));
accountscol2.SetWidth(224);
lstAccounts->InsertColumn(2,accountscol2);
accountscol3.SetId(3);
accountscol3.SetText(_("Type"));
accountscol3.SetWidth(96);
lstAccounts->InsertColumn(3,accountscol3);*/
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);
}
//col0.SetText( wxString::Format(wxT("%i"),i) );
//col0.SetText(preferences.accounts.GetAccountName(i));
long itemindex = lstAccounts->InsertItem( col0 );
//AccType = preferences.accounts.GetAccountType(i);
//AccType.Trim();
lstAccounts->SetItem(itemindex, 1, preferences->accounts.GetAccountName(i));
lstAccounts->SetItem(itemindex, 2, preferences->accounts.GetAccountType(i));
//lstAccounts->SetItem(itemindex, 3, AccType);
}
}
void frmPreferences::SetupPointers(bool *ReloadAccountInc){
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;
}
delete cfgfile;
cfgfile = NULL;
this->Close();
}
void frmPreferences::CloseWindow(wxCommandEvent &event){
this->Close();
}