Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
defaults: Check if default account exists each time XAB starts
[xestiaab/.git] / source / common / defaults.cpp
1 // defaults.cpp - Default settings 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 "defaults.h"
21 using namespace std;
23 void SetupDefaultAddressBook(){
24     
25         // Setup the default address book if there are none.
26     
27         // Check if the 'Default.local' directory exists.
28     
29         wxString DefaultLocalDir;
30     
31 #if defined(__HAIKU__)
32         
33 #elif defined(__WIN32__)
34     
35         DefaultLocalDir.Clear();
36         DefaultLocalDir.Append(wxString::FromUTF8(getenv("APPDATA")));
37         DefaultLocalDir.Append(wxT("\\Xestia\\Address Book\\accounts\\"));
38         DefaultLocalDir.Append(wxT("Default.local"));
39     
40 #elif defined(__APPLE__)
41     
42         DefaultLocalDir.Clear();
43         DefaultLocalDir.Append(wxString::FromUTF8(getenv("HOME")));
44         DefaultLocalDir.Append(wxT("/Library/Preferences/Xestia/Address Book/accounts/"));
45         DefaultLocalDir.Append(wxT("Default.local"));
46     
47 #else
48     
49         DefaultLocalDir.Clear();
50         DefaultLocalDir.Append(wxString::FromUTF8(getenv("HOME")));
51         DefaultLocalDir.Append(wxT("/.xestiaab/accounts/"));
52         DefaultLocalDir.Append(wxT("Default.local"));
53     
54 #endif
55     
56         if (wxDirExists(DefaultLocalDir) == FALSE){
57         
58                 if (wxMkdir(DefaultLocalDir, 0740) == TRUE){
59             
60                 }
61         
62         }
63     
64 }
66 void SetupDefaultSettings(){
67     
68         // Setup the default settings if they don't exist.
69     
70         // Setup default (non account) settings if they don't exist.
71     
72         wxString DefaultPrefDir;
73     
74 #if defined(__HAIKU__)
75     
76     
77 #elif defined(__WIN32__)
78     
79         DefaultPrefDir.Clear();
80         DefaultPrefDir.Append(wxString::FromUTF8(getenv("APPDATA")));
81         DefaultPrefDir.Append(wxT("\\Xestia\\Address Book\\preferences\\"));
82     
83 #elif defined(__APPLE__)
84     
85         DefaultPrefDir.Clear();
86         DefaultPrefDir.Append(wxString::FromUTF8(getenv("HOME")));
87         DefaultPrefDir.Append(wxT("/Library/Preferences/Xestia/Address Book/preferences/"));
88     
89 #else
90     
91         DefaultPrefDir.Clear();
92         DefaultPrefDir.Append(wxString::FromUTF8(getenv("HOME")));
93         DefaultPrefDir.Append(wxT("/.xestiaab/preferences/"));
94     
95 #endif
96     
97         // Create the accounts file if it doesn't exist.
98     
99         if (wxFileExists(DefaultPrefDir + wxT("accounts")) == FALSE){
100         
101                 wxString AccountsFilename;
102                 wxFFile AccountsFile;
103         
104                 AccountsFilename = DefaultPrefDir;
105                 AccountsFilename.Append(wxT("accounts"));
106         
107 #if wxABI_VERSION < 20900
108                 AccountsFile.Open(AccountsFilename.c_str(), wxT("w"));
109 #else
110                 AccountsFile.Open(AccountsFilename, wxT("w"));
111 #endif
112         
113                 AccountsFile.Write(wxT(""));
114         
115         }
116     
117         // Create the preferences file if it doesn't exist.
118     
119         if (wxFileExists(DefaultPrefDir + wxT("settings")) == FALSE){
120         
121                 wxString PrefsFilename;
122                 wxFFile PrefsFile;
123         
124                 PrefsFilename = DefaultPrefDir;
125                 PrefsFilename.Append(wxT("settings"));
126         
127 #if wxABI_VERSION < 20900
128                 PrefsFile.Open(PrefsFilename.c_str(), wxT("w"));
129 #else
130                 PrefsFile.Open(PrefsFilename, wxT("w"));
131 #endif
132         
133                 PrefsFile.Write(wxT("HideLocalAddressBooks=false\nSaveWindowPosition=true\n"));
134         
135         }
136         
137         // Check if the default account is in the accounts list.
138         // Add it if it isn't.
139         
140         wxString accountsConfigFile = DefaultPrefDir;
141         accountsConfigFile.Append(wxT("accounts"));
142         
143         wxFileConfig *accountConfigData = new wxFileConfig("", "", accountsConfigFile);
144         
145         long itemIndex = 0;
146         wxString accountName;
147         wxString accountType;
148         wxString accountDirectory;
149         bool continueProcessing = false;
150         bool defaultCalendarExists = false;
151     
152         continueProcessing = accountConfigData->GetFirstGroup(accountName, itemIndex);
153         
154         while (continueProcessing){
156                 accountConfigData->SetPath(accountName);
157                 accountConfigData->Read(wxT("type"), &accountType);
158                 accountConfigData->Read(wxT("accountdir"), &accountDirectory);          
159                 
160                 if (accountType == wxT("Local") && accountDirectory == wxT("Default")){
161                         
162                         defaultCalendarExists = true;
163                         
164                 }
165                 
166                 continueProcessing = accountConfigData->GetNextGroup(accountName, itemIndex);
167                 
168         }
169         
170         if (defaultCalendarExists == false){
171                 
172                 // Create the account in the accounts file.
173                 
174                 accountConfigData->SetPath(wxT("/Default"));
175                 accountConfigData->Write(wxT("type"), wxT("Local"));
176                 accountConfigData->Write(wxT("accountdir"), wxT("Default"));
177                 accountConfigData->Write(wxT("prefix"), wxT(""));
178                 accountConfigData->Write(wxT("address"), wxT(""));
179                 accountConfigData->Write(wxT("port"), wxT(""));
180                 accountConfigData->Write(wxT("ssl"), wxT("false"));
181                 accountConfigData->Write(wxT("username"), wxT(""));
182                 accountConfigData->Write(wxT("password"), wxT(""));
183                 accountConfigData->Write(wxT("prefix"), wxT(""));
184                 
185         }
186         
187         delete accountConfigData;
188         accountConfigData = nullptr;
189         
192 void SetupDirectories(){
193     
194         // Create the directories if they don't exist.
195     
196         wxString DefaultSettingsDir;
198 #if defined(__HAIKU__)
199     
200 #elif defined(__WIN32__)
201     
202         DefaultSettingsDir.Clear();
203         DefaultSettingsDir.Append(wxString::FromUTF8(getenv("APPDATA")));
204         DefaultSettingsDir.Append(wxT("\\Xestia\\"));
205     
206         if (wxDirExists(DefaultSettingsDir) == FALSE){
207         
208                 // Create the directory.
209         
210                 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){
211             
212                 }
213         
214         }
215     
216         // 'Address Book' to the directory.
217     
218         DefaultSettingsDir.Append(wxT("\\Address Book\\"));
219     
220 #elif defined(__APPLE__)
221     
222         DefaultSettingsDir.Clear();
223         DefaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
224         DefaultSettingsDir.Append(wxT("/Library/Preferences/Xestia/"));
225     
226         if (wxDirExists(DefaultSettingsDir) == FALSE){
227         
228                 // Create the directory.
229         
230                 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){
231             
232                 }
233         
234         }
235     
236         // Append 'Address Book' to the directory.
237     
238         DefaultSettingsDir.Append(wxT("/Address Book/"));
239     
240 #else
241     
242         DefaultSettingsDir.Clear();
243         DefaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
244         DefaultSettingsDir.Append(wxT("/.xestiaab/"));
245     
246 #endif
247     
248         // Check if the directory exists.
249     
250         if (wxDirExists(DefaultSettingsDir) == FALSE){
251         
252                 // Create the directory.
253         
254                 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){         
255             
256                 }
257         
258         }
259     
260         if (wxDirExists(DefaultSettingsDir + wxT("accounts")) == FALSE){
261         
262                 if (wxMkdir(DefaultSettingsDir + wxT("accounts"), 0740) == TRUE){               
263             
264                 }
265         
266         }
267     
268         if (wxDirExists(DefaultSettingsDir + wxT("preferences")) == FALSE){
269         
270                 if (wxMkdir(DefaultSettingsDir + wxT("preferences"), 0740) == TRUE){            
271             
272                 }
273         
274         }
275     
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