1 // defaults.cpp - Default settings subroutines.
3 // (c) 2012-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar. Originally from Xestia Address Book.
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.
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.
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/>
21 void SetupDefaultCalendar(){
23 // Setup the default calendar if there are none.
25 // Check if the 'Default.local' directory exists.
27 wxString defaultLocalDir;
29 #if defined(__HAIKU__)
31 #elif defined(__WIN32__)
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"));
47 defaultLocalDir.Clear();
48 defaultLocalDir.Append(wxString::FromUTF8(getenv("HOME")));
49 defaultLocalDir.Append(wxT("/.xestiacal/accounts/"));
50 defaultLocalDir.Append(wxT("Default.local"));
54 if (wxDirExists(defaultLocalDir) == FALSE){
56 if (wxMkdir(defaultLocalDir, 0740) == TRUE){
64 void SetupDefaultSettings(){
66 // Setup the default settings if they don't exist.
68 // Setup default (non account) settings if they don't exist.
70 wxString defaultPrefDir;
72 #if defined(__HAIKU__)
75 #elif defined(__WIN32__)
77 defaultPrefDir.Clear();
78 defaultPrefDir.Append(wxString::FromUTF8(getenv("APPDATA")));
79 defaultPrefDir.Append(wxT("\\Xestia\\Calendar\\preferences\\"));
81 #elif defined(__APPLE__)
83 defaultPrefDir.Clear();
84 defaultPrefDir.Append(wxString::FromUTF8(getenv("HOME")));
85 defaultPrefDir.Append(wxT("/Library/Preferences/Xestia/Calendar/preferences/"));
89 defaultPrefDir.Clear();
90 defaultPrefDir.Append(wxString::FromUTF8(getenv("HOME")));
91 defaultPrefDir.Append(wxT("/.xestiacal/preferences/"));
95 // Create the accounts file if it doesn't exist.
97 if (wxFileExists(defaultPrefDir + wxT("accounts")) == FALSE){
99 wxString accountsFilename;
100 wxFFile accountsFile;
102 accountsFilename = defaultPrefDir;
103 accountsFilename.Append(wxT("accounts"));
105 #if wxABI_VERSION < 20900
106 accountsFile.Open(accountsFilename.c_str(), wxT("w"));
108 accountsFile.Open(accountsFilename, wxT("w"));
111 accountsFile.Write(wxT(""));
115 // Create the preferences file if it doesn't exist.
117 if (wxFileExists(defaultPrefDir + wxT("settings")) == FALSE){
119 wxString prefsFilename;
122 prefsFilename = defaultPrefDir;
123 prefsFilename.Append(wxT("settings"));
125 #if wxABI_VERSION < 20900
126 prefsFile.Open(prefsFilename.c_str(), wxT("w"));
128 prefsFile.Open(prefsFilename, wxT("w"));
131 prefsFile.Write(wxT("HideLocalCalendars=false\nSaveWindowPosition=true\n"));
135 // Check if the default account is in the accounts list.
136 // Add it if it isn't.
138 wxString accountsConfigFile = defaultPrefDir;
139 accountsConfigFile.Append(wxT("accounts"));
141 wxFileConfig *accountConfigData = new wxFileConfig("", "", accountsConfigFile);
144 wxString accountName;
145 wxString accountType;
146 wxString accountDirectory;
147 bool continueProcessing = false;
148 bool defaultCalendarExists = false;
150 continueProcessing = accountConfigData->GetFirstGroup(accountName, itemIndex);
152 while (continueProcessing){
154 accountConfigData->SetPath(accountName);
155 accountConfigData->Read(wxT("type"), &accountType);
156 accountConfigData->Read(wxT("accountdir"), &accountDirectory);
158 if (accountType == wxT("Local") && accountDirectory == wxT("Default")){
160 defaultCalendarExists = true;
164 continueProcessing = accountConfigData->GetNextGroup(accountName, itemIndex);
168 if (defaultCalendarExists == false){
170 // Create the account in the accounts file.
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(""));
185 delete accountConfigData;
186 accountConfigData = nullptr;
190 void SetupDirectories(){
192 // Create the directories if they don't exist.
194 wxString defaultSettingsDir;
196 #if defined(__HAIKU__)
198 #elif defined(__WIN32__)
200 defaultSettingsDir.Clear();
201 defaultSettingsDir.Append(wxString::FromUTF8(getenv("APPDATA")));
202 defaultSettingsDir.Append(wxT("\\Xestia\\"));
204 if (wxDirExists(defaultSettingsDir) == FALSE){
206 // Create the directory.
208 if (wxMkdir(defaultSettingsDir, 0740) == TRUE){
214 // 'Calendar' to the directory.
216 defaultSettingsDir.Append(wxT("\\Calendar\\"));
218 #elif defined(__APPLE__)
220 defaultSettingsDir.Clear();
221 defaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
222 defaultSettingsDir.Append(wxT("/Library/Preferences/Xestia/"));
224 if (wxDirExists(defaultSettingsDir) == FALSE){
226 // Create the directory.
228 if (wxMkdir(defaultSettingsDir, 0740) == TRUE){
234 // Append 'Calendar' to the directory.
236 defaultSettingsDir.Append(wxT("/Calendar/"));
240 defaultSettingsDir.Clear();
241 defaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
242 defaultSettingsDir.Append(wxT("/.xestiacal/"));
246 // Check if the directory exists.
248 if (wxDirExists(defaultSettingsDir) == FALSE){
250 // Create the directory.
252 if (wxMkdir(defaultSettingsDir, 0740) == TRUE){
258 if (wxDirExists(defaultSettingsDir + wxT("accounts")) == FALSE){
260 if (wxMkdir(defaultSettingsDir + wxT("accounts"), 0740) == TRUE){
266 if (wxDirExists(defaultSettingsDir + wxT("preferences")) == FALSE){
268 if (wxMkdir(defaultSettingsDir + wxT("preferences"), 0740) == TRUE){