// frmNewAccount.cpp - New Account 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 "frmNewAccount.h"
#include
#include
#include
#include
#if defined(__WIN32__)
#include
#include "common/win32ssl.h"
#endif
#include "common/dirs.h"
#include "common/svrblist.h"
#include "frmInvalidSSLCertificate.h"
DEFINE_EVENT_TYPE(UPDATERESULTS);
DEFINE_EVENT_TYPE(RUNCARDDAV2TEST);
BEGIN_EVENT_TABLE(frmNewAccount, wxDialog)
EVT_COMMAND(wxID_ANY, UPDATERESULTS, frmNewAccount::UpdateResults)
EVT_COMMAND(wxID_ANY, RUNCARDDAV2TEST, frmNewAccount::RunCardDAV2Test)
END_EVENT_TABLE()
frmNewAccount::frmNewAccount( wxWindow* parent )
:
frmNewAccountADT( parent )
{
// Disable the previous button upon form creation.
btnPrevious->Disable();
}
void frmNewAccount::CheckAccountName( wxCommandEvent& event )
{
// Check that the account name is valid.
wxString CheckAccName = txtAccountName->GetValue();
if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
btnNext->Disable();
} else {
btnNext->Enable();
}
}
void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
{
// Go to the previous page.
PageSeek--;
if (PageSeek == 0){
// Currently at the Connection test screen.
tabConn->Hide();
tabFinish->Hide();
tabType->Show();
szrNewAccount->RecalcSizes();
btnPrevious->Disable();
btnNext->Enable();
} else if (PageSeek == 1){
if (cmbServerType->GetCurrentSelection() == 0){
tabConn->Hide();
tabFinish->Hide();
tabType->Show();
PageSeek = 0;
btnPrevious->Disable();
btnNext->Enable();
btnNext->SetLabel(_("Next >"));
return;
}
// Currently at the Finish screen.
tabType->Hide();
tabConn->Show();
tabFinish->Hide();
szrNewAccount->RecalcSizes();
btnNext->SetLabel(_("Next >"));
btnNext->Enable();
}
}
void frmNewAccount::ProcessNext( wxCommandEvent& event )
{
// Go to the next page or setup the new account.
PageSeek++;
if (PageSeek == 1){
if (cmbServerType->GetCurrentSelection() == 0){
tabType->Hide();
tabConn->Hide();
tabFinish->Show();
PageSeek = 2;
btnPrevious->Enable();
szrNewAccount->RecalcSizes();
btnNext->Disable();
btnNext->SetLabel(_("Finish"));
return;
}
// Check if server address matches against the blacklist.
// Bring up warning message if it does.
if (CheckBlacklist(txtServerAddress->GetValue())){
int MessageBoxResult = wxMessageBox(_("The server with the address given does not support the CardDAV protocol properly and shouldn't be used.\n\nData loss is very likely.\n\nDo you still want to continue using this server?"), _("Server warning"), wxYES_NO, this);
if (MessageBoxResult == wxNO){
PageSeek--;
return;
}
}
btnNext->Disable();
bool ServerResult = FALSE;
bool ServerAction = FALSE;
bool UseSSL = TRUE;
wxString ServerMessage;
// Connection test screen.
tabType->Hide();
tabConn->Show();
tabFinish->Hide();
szrNewAccount->RecalcSizes();
btnPrevious->Disable();
// Reset screen.
lblServerConnResult->SetLabel(wxT(""));
lblServerResponse->SetLabel(wxT(""));
lblServerSSLResult->SetLabel(wxT(""));
lblServerSSLValid->SetLabel(wxT(""));
lblAbleToLoginResult->SetLabel(wxT(""));
lblCardDAVSupportResult->SetLabel(wxT(""));
// Depending on account type, run the test.
if (cmbServerType->GetCurrentSelection() == 1){
wxCommandEvent RunTest(RUNCARDDAV2TEST);
wxPostEvent(this, RunTest);
}
} else if (PageSeek == 2){
// Finish screen.
tabType->Hide();
tabConn->Hide();
tabFinish->Show();
szrNewAccount->RecalcSizes();
btnNext->Disable();
btnNext->SetLabel(_("Finish"));
if (txtAccountName->IsEmpty() && PageSeek == 2){
btnNext->Disable();
} else {
btnNext->Enable();
}
} else if (PageSeek == 3){
// Finished.
wxString XestiaABPrefDirectory;
wxString XestiaABDirectory;
wxString AccountSettingsFile;
//wxFile ASFile;
srand(time(0));
int RandomNumber = rand() % 32767;
wxString RandomNumberSuffix = wxString::Format(wxT("%i"), RandomNumber);
bool DirectoryCreated = FALSE;
#if defined(__HAIKU__)
//preffilename = wxT("noo");
#elif defined(__WIN32__)
XestiaABPrefDirectory = GetUserPrefDir();
XestiaABDirectory = GetUserDir();
AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
// Open the file for writing.
wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
// Check if account name already exists and return an error message
// if this is the case.
wxString AccountName;
long itemindex = 0;
bool ContinueAcc;
ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
while (ContinueAcc){
if (txtAccountName->GetValue() == AccountName){
wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
return;
}
cfgfile->SetPath(wxT("/"));
ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
}
if (cmbServerType->GetCurrentSelection() == 1){
// Create the account directory.
wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
DirectoryCreated = TRUE;
}
if (DirectoryCreated == TRUE){
WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
} else {
wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
return;
}
} else if (cmbServerType->GetCurrentSelection() == 0){
// Create the account directory.
wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
DirectoryCreated = TRUE;
}
if (DirectoryCreated == TRUE){
WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
} else {
wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
return;
}
}
delete cfgfile;
cfgfile = NULL;
*ReloadAccountConfig = TRUE;
#else
XestiaABPrefDirectory = GetUserPrefDir();
XestiaABDirectory = GetUserDir();
AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
// Open the file for writing.
wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
// Check if account name already exists and return an error message
// if this is the case.
wxString AccountName;
long itemindex = 0;
bool ContinueAcc;
ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
while (ContinueAcc){
if (txtAccountName->GetValue() == AccountName){
wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
return;
}
cfgfile->SetPath(wxT("/"));
ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
}
if (cmbServerType->GetCurrentSelection() == 1){
// Create the account directory.
wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
DirectoryCreated = TRUE;
}
if (DirectoryCreated == TRUE){
WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
} else {
wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
return;
}
} else if (cmbServerType->GetCurrentSelection() == 0){
// Create the account directory.
wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
DirectoryCreated = TRUE;
}
if (DirectoryCreated == TRUE){
WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
} else {
wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
return;
}
}
delete cfgfile;
cfgfile = NULL;
*ReloadAccountConfig = TRUE;
#endif
this->Close();
}
}
void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
// Write the new account details.
cfgfilein->SetPath(txtAccountName->GetValue());
cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
cfgfilein->Write(wxT("username"), txtUsername->GetValue());
cfgfilein->Write(wxT("password"), txtPassword->GetValue());
cfgfilein->Write(wxT("prefix"), ServerPrefix);
cfgfilein->Write(wxT("accountdir"), DirectoryName);
if (chkUseSSL->GetValue() == TRUE){
cfgfilein->Write(wxT("ssl"), wxT("true"));
} else {
cfgfilein->Write(wxT("ssl"), wxT("false"));
}
cfgfilein->Write(wxT("refresh"), wxT("1800"));
cfgfilein->Write(wxT("type"), AccountType);
}
void frmNewAccount::CloseWindow( wxCommandEvent& event )
{
// Close the window.
*ReloadAccountConfig = FALSE;
this->Close();
}
void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
{
// Update the options.
if (cmbServerType->GetCurrentSelection() == 1){
txtServerAddress->Enable();
txtServerPort->Enable();
txtUsername->Enable();
txtPassword->Enable();
chkUseSSL->Enable();
} else if (cmbServerType->GetCurrentSelection() == 0){
txtServerAddress->Disable();
txtServerPort->Disable();
txtUsername->Disable();
txtPassword->Disable();
chkUseSSL->Disable();
}
}
void frmNewAccount::UpdateResults( wxCommandEvent& event )
{
NewAccountResult *ResultDataPointer = static_cast(event.GetClientData());
NewAccountResult ResultData = (*ResultDataPointer);
delete(ResultDataPointer);
ResultDataPointer = nullptr;
bool ServerResult = true;
// Process the result if the server connected.
if (ResultData.Connected == false){
lblServerConnResult->SetLabel(_("Failed"));
ServerResult = false;
} else {
lblServerConnResult->SetLabel(_("Success"));
}
// Check the result if the server gave a valid response.
if (ResultData.ValidResponse == false){
lblServerResponse->SetLabel(_("No"));
ServerResult = false;
} else {
lblServerResponse->SetLabel(_("Yes"));
}
// Check the result if the server uses SSL.
if (ResultData.SSLStatus == false){
lblServerSSLResult->SetLabel(_("No"));
} else {
lblServerSSLResult->SetLabel(_("Yes"));
}
// Check the server has a valid SSL certificate.
switch(ResultData.SSLVerified){
case COSSL_VERIFIED:
lblServerSSLValid->SetLabel(_("Verified"));
break;
case COSSL_VERIFIED_USER:
lblServerSSLValid->SetLabel(_("Verified (User)"));
break;
case COSSL_UNABLETOVERIFY:
lblServerSSLValid->SetLabel(_("Unable to verify"));
ServerResult = false;
break;
case COSSL_NOTAPPLICABLE:
lblServerSSLValid->SetLabel(_("Not applicable"));
break;
}
// Check if able to log into the server.
if (ResultData.AuthPassed == false){
lblAbleToLoginResult->SetLabel(_("No"));
ServerResult = false;
} else {
lblAbleToLoginResult->SetLabel(_("Yes"));
}
// Check if server has CardDAV support.
if (ResultData.CanProcess == false){
lblCardDAVSupportResult->SetLabel(_("No"));
ServerResult = false;
} else {
lblCardDAVSupportResult->SetLabel(_("Yes"));
}
if (ServerResult == false){
lblConnectionResultText->SetLabel(wxString::Format(_("An error occured whilst connecting: %s"), ResultData.ErrorMessage));
} else {
btnNext->Enable();
lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
}
btnPrevious->Enable();
}
void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
// Setup the pointers for the new account window.
ReloadAccountConfig = ReloadAccountInc;
}
void frmNewAccount::SetErrorMessageLabel(){
lblConnectionResultText->SetLabel(_("A problem has occured whilst connecting to the CardDAV server.\nPlease review the above information and change the server details if needed.\nIf there are still problems, please speak to your system administrator(s)."));
}