+// frmNewAccount.cpp - frmNewAccount form functions
+//
+// (c) 2016-2017 Xestia Software Development.
+//
+// This file is part of Xestia Calendar.
+//
+// Xestia Calendar 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 Calendar 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 Calendar. If not, see <http://www.gnu.org/licenses/>
+
#include "frmNewAccount.h"
frmNewAccount::frmNewAccount( wxWindow* parent )
frmNewAccountADT( parent )
{
+ // Disable the previous button upon form creation.
+
+ btnPrevious->Disable();
+ txtServerAddress->Disable();
+ txtServerPort->Disable();
+ txtUsername->Disable();
+ txtPassword->Disable();
+ chkUseSSL->Disable();
+
}
void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
// TODO: Implement UpdateRequirements
}
-void frmNewAccount::CheckAccountName( wxCommandEvent& event )
-{
-// TODO: Implement CheckAccountName
-}
-
void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
{
// TODO: Implement ProcessPrevious
void frmNewAccount::ProcessNext( wxCommandEvent& event )
{
-// TODO: Implement ProcessNext
+
+ pageSeek++;
+
+ if (pageSeek == 1){
+ pageSeek++;
+ }
+
+ if (pageSeek == 1){
+
+ // Skip this page.
+
+ } 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 xestiaCALPrefDirectory;
+ wxString xestiaCALDirectory;
+ 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__)
+
+ xestiaCALPrefDirectory = GetUserPrefDir();
+ xestiaCALDirectory = GetUserDir();
+
+ accountSettingsFile = xestiaCALPrefDirectory + 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() == 0){
+
+ // Create the account directory.
+
+ wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
+
+ if (wxMkdir(xestiaCALDirectory + 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
+
+ xestiaCALPrefDirectory = GetUserPrefDir();
+ xestiaCALDirectory = GetUserDir();
+
+ accountSettingsFile = xestiaCALPrefDirectory + 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() == 0){
+
+ // Create the account directory.
+
+ wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
+
+ if (wxMkdir(XestiaCALDirectory + 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::CloseWindow( wxCommandEvent& event )
{
-// TODO: Implement CloseWindow
+
+ // Close the window.
+
+ *reloadAccountConfig = FALSE;
+ this->Close();
+
+}
+
+void frmNewAccount::SetupPointers(bool *ReloadAccountInc, CalendarDataStorage *dataStorage){
+
+ // Setup the pointers for the new account window.
+
+ reloadAccountConfig = ReloadAccountInc;
+
+}
+
+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::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);
+
+}
\ No newline at end of file