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(""));
187 delete accountConfigData;
188 accountConfigData = nullptr;
192 void SetupDirectories(){
194 // Create the directories if they don't exist.
196 wxString DefaultSettingsDir;
198 #if defined(__HAIKU__)
200 #elif defined(__WIN32__)
202 DefaultSettingsDir.Clear();
203 DefaultSettingsDir.Append(wxString::FromUTF8(getenv("APPDATA")));
204 DefaultSettingsDir.Append(wxT("\\Xestia\\"));
206 if (wxDirExists(DefaultSettingsDir) == FALSE){
208 // Create the directory.
210 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){
216 // 'Address Book' to the directory.
218 DefaultSettingsDir.Append(wxT("\\Address Book\\"));
220 #elif defined(__APPLE__)
222 DefaultSettingsDir.Clear();
223 DefaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
224 DefaultSettingsDir.Append(wxT("/Library/Preferences/Xestia/"));
226 if (wxDirExists(DefaultSettingsDir) == FALSE){
228 // Create the directory.
230 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){
236 // Append 'Address Book' to the directory.
238 DefaultSettingsDir.Append(wxT("/Address Book/"));
242 DefaultSettingsDir.Clear();
243 DefaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
244 DefaultSettingsDir.Append(wxT("/.xestiaab/"));
248 // Check if the directory exists.
250 if (wxDirExists(DefaultSettingsDir) == FALSE){
252 // Create the directory.
254 if (wxMkdir(DefaultSettingsDir, 0740) == TRUE){
260 if (wxDirExists(DefaultSettingsDir + wxT("accounts")) == FALSE){
262 if (wxMkdir(DefaultSettingsDir + wxT("accounts"), 0740) == TRUE){
268 if (wxDirExists(DefaultSettingsDir + wxT("preferences")) == FALSE){
270 if (wxMkdir(DefaultSettingsDir + wxT("preferences"), 0740) == TRUE){