1 // frmNewAccount.cpp - frmNewAccount 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 "frmNewAccount.h"
21 frmNewAccount::frmNewAccount( wxWindow* parent )
23 frmNewAccountADT( parent )
26 // Disable the previous button upon form creation.
28 btnPrevious->Disable();
29 txtServerAddress->Disable();
30 txtServerPort->Disable();
31 txtUsername->Disable();
32 txtPassword->Disable();
37 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
39 // TODO: Implement UpdateRequirements
42 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
44 // TODO: Implement ProcessPrevious
47 void frmNewAccount::ProcessNext( wxCommandEvent& event )
60 } else if (pageSeek == 2){
67 szrNewAccount->RecalcSizes();
70 btnNext->SetLabel(_("Finish"));
72 if (txtAccountName->IsEmpty() && pageSeek == 2){
82 } else if (pageSeek == 3){
86 wxString xestiaCALPrefDirectory;
87 wxString xestiaCALDirectory;
88 wxString accountSettingsFile;
92 int randomNumber = rand() % 32767;
93 wxString randomNumberSuffix = wxString::Format(wxT("%i"), randomNumber);
94 bool directoryCreated = FALSE;
96 #if defined(__HAIKU__)
98 //preffilename = wxT("noo");
100 #elif defined(__WIN32__)
102 xestiaCALPrefDirectory = GetUserPrefDir();
103 xestiaCALDirectory = GetUserDir();
105 accountSettingsFile = xestiaCALPrefDirectory + wxT("accounts");
107 // Open the file for writing.
109 wxFileConfig *cfgFile = new wxFileConfig("", "", accountSettingsFile);
111 // Check if account name already exists and return an error message
112 // if this is the case.
114 wxString accountName;
117 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
121 if (txtAccountName->GetValue() == accountName){
123 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
128 cfgFile->SetPath(wxT("/"));
129 continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
133 if (cmbServerType->GetCurrentSelection() == 0){
135 // Create the account directory.
137 wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
139 if (wxMkdir(xestiaCALDirectory + wxT("\\accounts\\") + directoryName + wxT(".local"), 0740) == TRUE){
141 directoryCreated = TRUE;
145 if (directoryCreated == TRUE){
147 WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
151 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
161 *reloadAccountConfig = TRUE;
165 xestiaCALPrefDirectory = GetUserPrefDir();
166 xestiaCALDirectory = GetUserDir();
168 accountSettingsFile = xestiaCALPrefDirectory + wxT("accounts");
170 // Open the file for writing.
172 wxFileConfig *cfgFile = new wxFileConfig("", "", accountSettingsFile);
174 // Check if account name already exists and return an error message
175 // if this is the case.
177 wxString accountName;
180 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
184 if (txtAccountName->GetValue() == accountName){
186 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
191 cfgFile->SetPath(wxT("/"));
192 continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
196 if (cmbServerType->GetCurrentSelection() == 0){
198 // Create the account directory.
200 wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
202 if (wxMkdir(xestiaCALDirectory + wxT("/accounts/") + directoryName + wxT(".Local"), 0740) == TRUE){
204 directoryCreated = TRUE;
208 if (directoryCreated == TRUE){
210 WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
214 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
224 *reloadAccountConfig = true;
234 void frmNewAccount::CloseWindow( wxCommandEvent& event )
239 *reloadAccountConfig = FALSE;
244 void frmNewAccount::SetupPointers(bool *ReloadAccountInc, CalendarDataStorage *dataStorage){
246 // Setup the pointers for the new account window.
248 reloadAccountConfig = ReloadAccountInc;
252 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
255 // Check that the account name is valid.
257 wxString checkAccName = txtAccountName->GetValue();
259 if ((txtAccountName->IsEmpty() && pageSeek == 2) || checkAccName.Len() < 4){
271 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgFileIn, wxString accountType, wxString directoryName){
273 // Write the new account details.
275 cfgFileIn->SetPath(txtAccountName->GetValue());
276 cfgFileIn->Write(wxT("address"), txtServerAddress->GetValue());
277 cfgFileIn->Write(wxT("port"), txtServerPort->GetValue());
278 cfgFileIn->Write(wxT("username"), txtUsername->GetValue());
279 cfgFileIn->Write(wxT("password"), txtPassword->GetValue());
280 cfgFileIn->Write(wxT("prefix"), serverPrefix);
281 cfgFileIn->Write(wxT("accountdir"), directoryName);
283 if (chkUseSSL->GetValue() == TRUE){
285 cfgFileIn->Write(wxT("ssl"), wxT("true"));
289 cfgFileIn->Write(wxT("ssl"), wxT("false"));
293 cfgFileIn->Write(wxT("refresh"), wxT("1800"));
294 cfgFileIn->Write(wxT("type"), accountType);