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){
60 wxFile defaultCalendarDB;
62 #if defined(__WIN32__)
63 defaultCalendarDB.Create(defaultLocalDir + "\\calendarlist.db", false, wxPOSIX_USER_READ|wxPOSIX_USER_WRITE|wxPOSIX_GROUP_READ);
65 defaultCalendarDB.Create(defaultLocalDir + "/calendarlist.db", false, wxPOSIX_USER_READ|wxPOSIX_USER_WRITE|wxPOSIX_GROUP_READ);
72 void SetupDefaultSettings(){
74 // Setup the default settings if they don't exist.
76 // Setup default (non account) settings if they don't exist.
78 wxString defaultPrefDir;
80 #if defined(__HAIKU__)
83 #elif defined(__WIN32__)
85 defaultPrefDir.Clear();
86 defaultPrefDir.Append(wxString::FromUTF8(getenv("APPDATA")));
87 defaultPrefDir.Append(wxT("\\Xestia\\Calendar\\preferences\\"));
89 #elif defined(__APPLE__)
91 defaultPrefDir.Clear();
92 defaultPrefDir.Append(wxString::FromUTF8(getenv("HOME")));
93 defaultPrefDir.Append(wxT("/Library/Preferences/Xestia/Calendar/preferences/"));
97 defaultPrefDir.Clear();
98 defaultPrefDir.Append(wxString::FromUTF8(getenv("HOME")));
99 defaultPrefDir.Append(wxT("/.xestiacal/preferences/"));
103 // Create the accounts file if it doesn't exist.
105 if (wxFileExists(defaultPrefDir + wxT("accounts")) == FALSE){
107 wxString accountsFilename;
108 wxFFile accountsFile;
110 accountsFilename = defaultPrefDir;
111 accountsFilename.Append(wxT("accounts"));
113 #if wxABI_VERSION < 20900
114 accountsFile.Open(accountsFilename.c_str(), wxT("w"));
116 accountsFile.Open(accountsFilename, wxT("w"));
119 accountsFile.Write(wxT(""));
123 // Create the preferences file if it doesn't exist.
125 if (wxFileExists(defaultPrefDir + wxT("settings")) == FALSE){
127 wxString prefsFilename;
130 prefsFilename = defaultPrefDir;
131 prefsFilename.Append(wxT("settings"));
133 #if wxABI_VERSION < 20900
134 prefsFile.Open(prefsFilename.c_str(), wxT("w"));
136 prefsFile.Open(prefsFilename, wxT("w"));
139 prefsFile.Write(wxT("HideLocalCalendars=false\nSaveWindowPosition=true\n"));
143 // Check if the default account is in the accounts list.
144 // Add it if it isn't.
146 wxString accountsConfigFile = defaultPrefDir;
147 accountsConfigFile.Append(wxT("accounts"));
149 wxFileConfig *accountConfigData = new wxFileConfig("", "", accountsConfigFile);
152 wxString accountName;
153 wxString accountType;
154 wxString accountDirectory;
155 bool continueProcessing = false;
156 bool defaultCalendarExists = false;
158 continueProcessing = accountConfigData->GetFirstGroup(accountName, itemIndex);
160 while (continueProcessing){
162 accountConfigData->SetPath(accountName);
163 accountConfigData->Read(wxT("type"), &accountType);
164 accountConfigData->Read(wxT("accountdir"), &accountDirectory);
166 if (accountType == wxT("Local") && accountDirectory == wxT("Default")){
168 defaultCalendarExists = true;
172 continueProcessing = accountConfigData->GetNextGroup(accountName, itemIndex);
176 if (defaultCalendarExists == false){
178 // Create the account in the accounts file.
180 accountConfigData->SetPath(wxT("/Default"));
181 accountConfigData->Write(wxT("type"), wxT("Local"));
182 accountConfigData->Write(wxT("accountdir"), wxT("Default"));
183 accountConfigData->Write(wxT("prefix"), wxT(""));
184 accountConfigData->Write(wxT("address"), wxT(""));
185 accountConfigData->Write(wxT("port"), wxT(""));
186 accountConfigData->Write(wxT("ssl"), wxT("false"));
187 accountConfigData->Write(wxT("username"), wxT(""));
188 accountConfigData->Write(wxT("password"), wxT(""));
189 accountConfigData->Write(wxT("prefix"), wxT(""));
193 delete accountConfigData;
194 accountConfigData = nullptr;
198 void SetupDirectories(){
200 // Create the directories if they don't exist.
202 wxString defaultSettingsDir;
204 #if defined(__HAIKU__)
206 #elif defined(__WIN32__)
208 defaultSettingsDir.Clear();
209 defaultSettingsDir.Append(wxString::FromUTF8(getenv("APPDATA")));
210 defaultSettingsDir.Append(wxT("\\Xestia\\"));
212 if (wxDirExists(defaultSettingsDir) == FALSE){
214 // Create the directory.
216 if (wxMkdir(defaultSettingsDir, 0740) == TRUE){
222 // 'Calendar' to the directory.
224 defaultSettingsDir.Append(wxT("\\Calendar\\"));
226 #elif defined(__APPLE__)
228 defaultSettingsDir.Clear();
229 defaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
230 defaultSettingsDir.Append(wxT("/Library/Preferences/Xestia/"));
232 if (wxDirExists(defaultSettingsDir) == FALSE){
234 // Create the directory.
236 if (wxMkdir(defaultSettingsDir, 0740) == TRUE){
242 // Append 'Calendar' to the directory.
244 defaultSettingsDir.Append(wxT("/Calendar/"));
248 defaultSettingsDir.Clear();
249 defaultSettingsDir.Append(wxString::FromUTF8(getenv("HOME")));
250 defaultSettingsDir.Append(wxT("/.xestiacal/"));
254 // Check if the directory exists.
256 if (wxDirExists(defaultSettingsDir) == FALSE){
258 // Create the directory.
260 if (wxMkdir(defaultSettingsDir, 0740) == TRUE){
266 if (wxDirExists(defaultSettingsDir + wxT("accounts")) == FALSE){
268 if (wxMkdir(defaultSettingsDir + wxT("accounts"), 0740) == TRUE){
274 if (wxDirExists(defaultSettingsDir + wxT("preferences")) == FALSE){
276 if (wxMkdir(defaultSettingsDir + wxT("preferences"), 0740) == TRUE){