Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Merge branch 'master' of ssh://gelforn.xestia.co.uk/scmrepos/xestiaab
[xestiaab/.git] / source / common / preferences.cpp
1 // preferences.cpp - Preferences subroutines.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book 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.
10 //
11 // Xestia Address Book 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.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include <iostream>
20 #include <iomanip>
21 #include <ios>
22 #include <string>
23 #include <wx/fileconf.h>
25 #include "preferences.h"
27 using namespace std;
29 void SavePreferences();
30 void LoadPreferences();
32 XABPreferences::XABPreferences(wxString PreferencesFilename){
34         // Load the settings into the XABPreferences object using the
35         // settings file received in PreferencesFilename.
37         wxString fullprefpath;
38     
39         bool accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
40                 accountsslvalid, accountusernamevalid, accountpasswordvalid,
41                 accountdirprefixvalid, accountdirvalid, accountrefreshvalid = FALSE;
42     
43         bool preaccountstage = FALSE;
44         bool isvalidaccount = FALSE;
45         bool isvalidsection = FALSE;
46         wxString accountname;
47         wxString accountaddress;
48         wxString accounttype;
49         long accountport = 8080;
50         long accountrefresh;
51         bool accountssl;
52         wxString accountusername;
53         wxString accountpassword;
54         wxString accountdirprefix;
55         wxString accountdir;
56     
57         wxString firstchar;
58         string s;
60         wxString segmentname;
61         wxString segmentvalue;
62     
63         // Load the settings.
64     
65         fullprefpath.Append(PreferencesFilename);
66         fullprefpath.Append(wxT("settings"));
68         wxFileConfig *settingfile = new wxFileConfig("", "", fullprefpath);
69     
70         wxString ValueInc;
71         settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
72     
73         if (ValueInc == wxT("true")){
74                 
75                 SaveWindowPos = TRUE;
76                 wxRect WindowPosition;
77         
78                 long PosX, PosY, PosH, PosW = 0;
80                 bool PosXValid, PosYValid, PosHValid, PosWValid = FALSE;
82                 PosXValid = settingfile->Read(wxT("WindowPositionX"), &PosX);
83                 PosYValid = settingfile->Read(wxT("WindowPositionY"), &PosY);
84                 PosHValid = settingfile->Read(wxT("WindowPositionHeight"), &PosH);
85                 PosWValid = settingfile->Read(wxT("WindowPositionWidth"), &PosW);
87                 if (PosXValid == TRUE && PosYValid == TRUE && PosHValid == TRUE && PosWValid == TRUE){
89                         WindowPosition.SetX((int)PosX);
90                         WindowPosition.SetY((int)PosY);
91                         WindowPosition.SetHeight((int)PosH);
92                         WindowPosition.SetWidth((int)PosW);
94                 } else {
96                         WindowPosition.SetX(-1);
97                         WindowPosition.SetY(-1);
98                         WindowPosition.SetHeight(500);
99                         WindowPosition.SetWidth(300);
101                 }
103                 SetMainWindowData(WindowPosition);
104         
105         }
107         settingfile->Read(wxT("HideLocalAddressBooks"), &ValueInc);
108     
109         if (ValueInc == wxT("true")){
110                 HideLocalABs = TRUE;
111         }
112     
113         delete settingfile;
114         settingfile = NULL;
115     
116         // Load the accounts.
117     
118         fullprefpath.Clear();
119         fullprefpath.Append(PreferencesFilename);
120         fullprefpath.Append(wxT("accounts"));
121  
122         wxFileConfig *cfgfile = new wxFileConfig("AddressBook", "Xestia", fullprefpath);
123     
124         wxString EntryName, EntryValue;
125         wxString AccAdr, AccUsr, AccPass, AccPrefix, AccDir;
126         wxString AccSSLInc;
127         bool AccSSL = FALSE;
128         int AccRef, AccPort = 0;
129         long itemindex = 0;
130         bool ContinueAcc = TRUE;
131     
132         ContinueAcc = cfgfile->GetFirstGroup(accountname, itemindex);
133     
134         while (ContinueAcc){
136                 cfgfile->SetPath(accountname);
137                 cfgfile->Read(wxT("type"), &EntryValue);
138          
139                 if (EntryValue == wxT("Local")){
140         
141                         // Get the account directory.
142         
143                         cfgfile->Read(wxT("accountdir"), &AccDir);
144         
145                         if (AccDir.Len() > 4){
147                                 accountdirvalid = TRUE;
148             
149                         } else {
151                                 accountdirvalid = FALSE;
153                         }
154             
155                         if (accountdirvalid == TRUE){
156             
157                                 accounts.AddAccount(accountname, wxT("Local"), wxT(""), 0,
158                                         0, wxT(""), wxT(""), 
159                                         wxT(""), AccDir, 0);
160             
161                         }
162         
163                 } else if (EntryValue == wxT("CardDAV")){
165                         cfgfile->Read(wxT("accountdir"), &AccDir);
166                         accountaddressvalid = cfgfile->Read(wxT("address"), &AccAdr);
167                         accountportvalid = cfgfile->Read(wxT("port"), &AccPort);
168                         accountsslvalid = cfgfile->Read(wxT("ssl"), &AccSSLInc);
169                         if (AccSSLInc == wxT("true")){
170                                 AccSSL = TRUE;
171                         }
172                         else {
173                                 AccSSL = FALSE;
174                         }
175                         accountusernamevalid = cfgfile->Read(wxT("username"), &AccUsr);
176                         accountpasswordvalid = cfgfile->Read(wxT("password"), &AccPass);
177                         accountdirprefixvalid = cfgfile->Read(wxT("prefix"), &AccPrefix);
178                         accountrefreshvalid = cfgfile->Read(wxT("refresh"), &AccRef);
179             
180                         if (AccDir.Len() > 4){
182                                 accountdirvalid = TRUE;
183             
184                         }
185                 
186                         // Make sure it is not bigger than 65535 or less than 1.
187                         // If so, default to port 8008.
188                 
189                         if (accountport < 1 || accountport > 65535){
190                                 accountport = 8008;
191                                 accountportvalid = TRUE;
192                         }
193             
194                         accounts.AddAccount(accountname, wxT("CardDAV"), AccAdr, AccPort,
195                                 AccSSL, AccUsr, AccPass, 
196                                 AccPrefix, AccDir, AccRef);
197             
198                 } else {
199                         
200                         cfgfile->Read(wxT("accountdir"), &AccDir);
201                         accountaddressvalid = cfgfile->Read(wxT("address"), &AccAdr);
202                         accountportvalid = cfgfile->Read(wxT("port"), &AccPort);
203                         accountsslvalid = cfgfile->Read(wxT("ssl"), &AccSSLInc);
204                         if (AccSSLInc == wxT("true")){
205                                 AccSSL = TRUE;
206                         }
207                         else {
208                                 AccSSL = FALSE;
209                         }
210                         accountusernamevalid = cfgfile->Read(wxT("username"), &AccUsr);
211                         accountpasswordvalid = cfgfile->Read(wxT("password"), &AccPass);
212                         accountdirprefixvalid = cfgfile->Read(wxT("prefix"), &AccPrefix);
213                         accountrefreshvalid = cfgfile->Read(wxT("refresh"), &AccRef);
214             
215                         if (AccDir.Len() > 4){
217                                 accountdirvalid = TRUE;
218             
219                         }
220                 
221                         // Make sure it is not bigger than 65535 or less than 1.
222                         // If so, default to port 8008.
223                 
224                         if (accountport < 1 || accountport > 65535){
225                                 accountport = 8008;
226                                 accountportvalid = TRUE;
227                         }
228             
229                         accounts.AddAccount(accountname, EntryValue, AccAdr, AccPort,
230                                 AccSSL, AccUsr, AccPass, 
231                                 AccPrefix, AccDir, AccRef);
232                         
233                 }
235                 // Clear up for the next account.
236     
237                 accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
238                 accountsslvalid, accountusernamevalid, accountpasswordvalid,
239                 accountdirprefixvalid, accountdirvalid, accountrefreshvalid, accountssl,
240                 isvalidaccount, isvalidsection = FALSE;
241         
242                 AccAdr.Clear();
243                 AccDir.Clear();
244                 AccUsr.Clear();
245                 AccPass.Clear();
246                 AccPrefix.Clear();
247                 accountname.Clear();
248                 accountaddress.Clear();
249                 accounttype.Clear();
250                 accountport = 0;
251                 accountrefresh = 0;
252                 accountusername.Clear();
253                 accountpassword.Clear();
254                 accountdirprefix.Clear();
255                 preaccountstage = TRUE;
256     
257                 cfgfile->SetPath(wxT("/"));
258                 ContinueAcc = cfgfile->GetNextGroup(accountname, itemindex);
259     
260         }
261   
264 XABPreferences::~XABPreferences(){
265         
268 bool XABPreferences::GetBoolData(wxString SettingName){
270         // GetBoolData from the XABPreferences object.
272         if (SettingName == wxT("SaveWindowPosition")) { return SaveWindowPos; }
273         else if (SettingName == wxT("HideLocalAddressBooks")) { return HideLocalABs; }
274         
275         return FALSE;
279 wxRect XABPreferences::GetMainWindowData(){
280         
281         // Return the main window data as a wxRect object.
282         
283         return MainWindowData;
287 void XABPreferences::SetMainWindowData(wxRect WindowData){
289         // Set the main window data from a wxRect object.
291         MainWindowData = WindowData;
295 // XABPrefAccounts
297 XABPrefAccounts::XABPrefAccounts(){
299         // Setup the default values for XABPrefAccounts.
301         AccountsCount = 0;
302         
305 int XABPrefAccounts::AddAccount(wxString NewAccName,
306         wxString NewAccType,
307         wxString NewAccAddress,
308         int NewAccPort,
309         int NewAccSSL,
310         wxString NewAccUser,
311         wxString NewAccPass,
312         wxString NewAccDirPrefix,
313         wxString NewAccDir,
314         long NewAccRefresh
315 ){
316   
317         // Add an account to the list of accounts in the
318         // XABPrefAccounts object.
319   
320         AccountName.Add(NewAccName, 1);
321         AccountType.Add(NewAccType, 1);
322         AccountAddress.Add(NewAccAddress, 1);
323         AccountPort.Add(NewAccPort, 1);
324         AccountSSL.Add(NewAccSSL, 1);
325         AccountUsername.Add(NewAccUser, 1);
326         AccountPassword.Add(NewAccPass, 1);
327         DirectoryPrefix.Add(NewAccDirPrefix, 1);
328         AccountDirectory.Add(NewAccDir, 1);
329         AccountRefresh.Add(NewAccRefresh, 1);
330     
331         AccountsCount++;
332     
333         return 0;
337 int XABPrefAccounts::GetCount(){
338   
339         // Get the count of accounts in the XABPrefAccounts object.
340   
341         return AccountsCount;
342   
345 wxString XABPrefAccounts::GetAccountName(int AccountNum){
346  
347         // Get the account name.
348  
349         if (AccountNum > AccountsCount){
350                 return wxT("");
351         }
352   
353         return AccountName[AccountNum];
354   
357 wxString XABPrefAccounts::GetAccountType(int AccountNum){
358  
359         // Get the account type.
360  
361         if (AccountNum > AccountsCount){
362                 return wxT("");
363         }
364   
365         return AccountType[AccountNum];
366   
369 wxString XABPrefAccounts::GetAccountAddress(int AccountNum){
370  
371         // Get the account server address.
372  
373         if (AccountNum > AccountsCount){
374                 return wxT("");
375         }
376   
377         return AccountAddress[AccountNum];
378   
381 int XABPrefAccounts::GetAccountPort(int AccountNum){
382  
383         // Get the account server port.
384  
385         if (AccountNum > AccountsCount){
386                 return 0;
387         }
388   
389         return AccountPort[AccountNum];
390   
393 bool XABPrefAccounts::GetAccountSSL(int AccountNum){
394  
395         // Get the account server SSL support.
396  
397         if (AccountNum > AccountsCount){
398                 return wxT("");
399         }
400   
401         return AccountSSL[AccountNum];
402   
405 wxString XABPrefAccounts::GetAccountUsername(int AccountNum){
406  
407         // Get the account username.
408  
409         if (AccountNum > AccountsCount){
410                 return wxT("");
411         }
412   
413         return AccountUsername[AccountNum];
414   
417 wxString XABPrefAccounts::GetAccountPassword(int AccountNum){
418  
419         // Get the account password.
420  
421         if (AccountNum > AccountsCount){
422                 return wxT("");
423         }
424   
425         return AccountPassword[AccountNum];
426   
429 wxString XABPrefAccounts::GetAccountDirectory(int AccountNum){
430  
431         // Get the account directory.
432  
433         if (AccountNum > AccountsCount){
434                 return wxT("");
435         }
436   
437         return AccountDirectory[AccountNum];
438   
441 wxString XABPrefAccounts::GetAccountDirPrefix(int AccountNum){
442  
443         // Get the account server directory prefix.
444  
445         if (AccountNum > AccountsCount){
446                 return wxT("");
447         }
448   
449         return DirectoryPrefix[AccountNum];
450   
453 long XABPrefAccounts::GetAccountRefresh(int AccountNum){
455         // Get the account refresh time.
457         if (AccountNum > AccountsCount){
458                 return 0;
459         }
460   
461         return AccountRefresh[AccountNum];
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy