1 // defaults.cpp - Default settings subroutines.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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/>
23 void SetupDefaultAddressBook(){
25 // Setup the default address book if there are none.
27 // Check if the 'Default.local' directory exists.
29 wxString DefaultLocalDir;
31 #if defined(__HAIKU__)
33 #elif defined(__WIN32__)
35 DefaultLocalDir.Clear();
36 DefaultLocalDir.Append(wxString::FromUTF8(getenv("APPDATA")));
37 DefaultLocalDir.Append(wxT("\\Xestia\\Address Book\\accounts\\"));
38 DefaultLocalDir.Append(wxT("Default.local"));
40 #elif defined(__APPLE__)
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"));
49 DefaultLocalDir.Clear();
50 DefaultLocalDir.Append(wxString::FromUTF8(getenv("HOME")));
51 DefaultLocalDir.Append(wxT("/.xestiaab/accounts/"));
52 DefaultLocalDir.Append(wxT("Default.local"));
56 if (wxDirExists(DefaultLocalDir) == FALSE){
58 if (wxMkdir(DefaultLocalDir, 0740) == TRUE){
66 void SetupDefaultSettings(){
68 // Setup the default settings if they don't exist.
70 // Setup default (non account) settings if they don't exist.
72 wxString DefaultPrefDir;
74 #if defined(__HAIKU__)
77 #elif defined(__WIN32__)
79 DefaultPrefDir.Clear();
80 DefaultPrefDir.Append(wxString::FromUTF8(getenv("APPDATA")));
81 DefaultPrefDir.Append(wxT("\\Xestia\\Address Book\\preferences\\"));
83 #elif defined(__APPLE__)
85 DefaultPrefDir.Clear();
86 DefaultPrefDir.Append(wxString::FromUTF8(getenv("HOME")));
87 DefaultPrefDir.Append(wxT("/Library/Preferences/Xestia/Address Book/preferences/"));
91 DefaultPrefDir.Clear();
92 DefaultPrefDir.Append(wxString::FromUTF8(getenv("HOME")));
93 DefaultPrefDir.Append(wxT("/.xestiaab/preferences/"));
97 // Create the accounts file if it doesn't exist.
99 if (wxFileExists(DefaultPrefDir + wxT("accounts")) == FALSE){
101 wxString AccountsFilename;
102 wxFFile AccountsFile;
104 AccountsFilename = DefaultPrefDir;
105 AccountsFilename.Append(wxT("accounts"));
107 #if wxABI_VERSION < 20900
108 AccountsFile.Open(AccountsFilename.c_str(), wxT("w"));
110 AccountsFile.Open(AccountsFilename, wxT("w"));
113 AccountsFile.Write(wxT(""));
117 // Create the preferences file if it doesn't exist.
119 if (wxFileExists(DefaultPrefDir + wxT("settings")) == FALSE){
121 wxString PrefsFilename;
124 PrefsFilename = DefaultPrefDir;
125 PrefsFilename.Append(wxT("settings"));
127 #if wxABI_VERSION < 20900
128 PrefsFile.Open(PrefsFilename.c_str(), wxT("w"));
130 PrefsFile.Open(PrefsFilename, wxT("w"));
133 PrefsFile.Write(wxT("HideLocalAddressBooks=false\nSaveWindowPosition=true\n"));
137 // Check if the default account is in the accounts list.
138 // Add it if it isn't.
140 wxString accountsConfigFile = DefaultPrefDir;
141 accountsConfigFile.Append(wxT("accounts"));
143 wxFileConfig *accountConfigData = new wxFileConfig("", "", accountsConfigFile);
146 wxString accountName;
147 wxString accountType;
148 wxString accountDirectory;
149 bool continueProcessing = false;
150 bool defaultCalendarExists = false;
152 continueProcessing = accountConfigData->GetFirstGroup(accountName, itemIndex);
154 while (continueProcessing){
156 accountConfigData->SetPath(accountName);
157 accountConfigData->Read(wxT("type"), &accountType);
158 accountConfigData->Read(wxT("accountdir"), &accountDirectory);
160 if (accountType == wxT("Local") && accountDirectory == wxT("Default")){
162 defaultCalendarExists = true;
166 continueProcessing = accountConfigData->GetNextGroup(accountName, itemIndex);
170 if (defaultCalendarExists == false){
172 // Create the account in the accounts file.
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 accountConfigData->Write(wxT("refresh"), wxT("1800"));
188 delete accountConfigData;
189 accountConfigData = nullptr;
193 void SetupDirectories(){
195 // Create the directories if they don't exist.
197 wxString DefaultSettingsDir;
199 #if defined(__HAIKU__)
201 #elif defined(__WIN32__)
203 DefaultSettingsDir.Clear();
204 DefaultSettingsDir.Append(wxString::FromUTF8(getenv("APPDATA")));
205 DefaultSettingsDir.Append(wxT("\\Xestia\\"));
207 if (wxDirExists(DefaultSettingsDir) == FALSE){
209 // Create the directory.
211 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){
217 // 'Address Book' to the directory.
219 DefaultSettingsDir.Append(wxT("\\Address Book\\"));
221 #elif defined(__APPLE__)
223 DefaultSettingsDir.Clear();
224 DefaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
225 DefaultSettingsDir.Append(wxT("/Library/Preferences/Xestia/"));
227 if (wxDirExists(DefaultSettingsDir) == FALSE){
229 // Create the directory.
231 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){
237 // Append 'Address Book' to the directory.
239 DefaultSettingsDir.Append(wxT("/Address Book/"));
243 DefaultSettingsDir.Clear();
244 DefaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
245 DefaultSettingsDir.Append(wxT("/.xestiaab/"));
249 // Check if the directory exists.
251 if (wxDirExists(DefaultSettingsDir) == FALSE){
253 // Create the directory.
255 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){
261 if (wxDirExists(DefaultSettingsDir + wxT("accounts")) == FALSE){
263 if (wxMkdir(DefaultSettingsDir + wxT("accounts"), 0740) == TRUE){
269 if (wxDirExists(DefaultSettingsDir + wxT("preferences")) == FALSE){
271 if (wxMkdir(DefaultSettingsDir + wxT("preferences"), 0740) == TRUE){