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