only create user configuration directory if no explicit version was given
[ardour.git] / libs / ardour / filesystem_paths.cc
1 /*
2     Copyright (C) 2007 Tim Mayberry
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19 #include <cstdlib>
20 #include <iostream>
21
22 #include "pbd/compose.h"
23 #include "pbd/convert.h"
24 #include "pbd/error.h"
25
26 #include <glibmm/miscutils.h>
27 #include <glibmm/fileutils.h>
28
29 #include "ardour/directory_names.h"
30 #include "ardour/filesystem_paths.h"
31
32 #include "i18n.h"
33
34 #ifdef PLATFORM_WINDOWS
35 #include "shlobj.h"
36 #include "pbd/windows_special_dirs.h"
37 #endif
38
39 using namespace PBD;
40
41 namespace ARDOUR {
42
43 using std::string;
44
45 static std::string
46 user_config_directory_name (int version = -1)
47 {
48         if (version < 0) {
49                 version = atoi (X_(PROGRAM_VERSION));
50         }
51
52         const string config_dir_name = string_compose ("%1%2", X_(PROGRAM_NAME), version);
53         
54 #if defined (__APPLE__) || defined (PLATFORM_WINDOWS)
55         return config_dir_name;
56 #else
57         return downcase (config_dir_name);
58 #endif
59 }       
60
61 std::string
62 user_config_directory (int version)
63 {
64         static std::string p;
65
66         if (!p.empty()) return p;
67
68 #ifdef __APPLE__
69
70         p = Glib::build_filename (Glib::get_home_dir(), "Library/Preferences");
71
72 #else
73
74         const char* c = 0;
75         /* adopt freedesktop standards, and put .ardour3 into $XDG_CONFIG_HOME or ~/.config */
76         if ((c = getenv ("XDG_CONFIG_HOME")) != 0) {
77                 p = c;
78         } else {
79
80 #ifdef PLATFORM_WINDOWS
81                 // Not technically the home dir (since it needs to be a writable folder)
82                 const string home_dir = Glib::get_user_config_dir();
83 #else
84                 const string home_dir = Glib::get_home_dir();
85 #endif
86                 if (home_dir.empty ()) {
87                         error << "Unable to determine home directory" << endmsg;
88                         exit (1);
89                 }
90                 p = home_dir;
91
92 #ifndef PLATFORM_WINDOWS
93                 p = Glib::build_filename (p, ".config");
94 #endif
95
96         }
97 #endif // end not __APPLE__
98
99         p = Glib::build_filename (p, user_config_directory_name (version));
100
101         if (version < 0) {
102                 /* Only create the user config dir if the version was negative,
103                    meaning "for the current version.
104                 */
105                 if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
106                         if (g_mkdir_with_parents (p.c_str(), 0755)) {
107                                 error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
108                                                          p) << endmsg;
109                                 exit (1);
110                         }
111                         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
112                         error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
113                                                  p) << endmsg;
114                         exit (1);
115                 }
116         }
117
118         return p;
119 }
120
121 std::string
122 user_cache_directory ()
123 {
124         static std::string p;
125
126         if (!p.empty()) return p;
127
128 #ifdef __APPLE__
129         p = Glib::build_filename (Glib::get_home_dir(), "Library/Caches");
130 #else
131         const char* c = 0;
132
133         /* adopt freedesktop standards, and put .ardour3 into $XDG_CACHE_HOME
134          * defaulting to or ~/.config
135          */
136         if ((c = getenv ("XDG_CACHE_HOME")) != 0) {
137                 p = c;
138         } else {
139
140 #ifdef PLATFORM_WINDOWS
141                 // Not technically the home dir (since it needs to be a writable folder)
142                 const string home_dir = Glib::get_user_data_dir();
143 #else
144                 const string home_dir = Glib::get_home_dir();
145 #endif
146                 if (home_dir.empty ()) {
147                         error << "Unable to determine home directory" << endmsg;
148                         exit (1);
149                 }
150                 p = home_dir;
151
152 #ifndef PLATFORM_WINDOWS
153                 p = Glib::build_filename (p, ".cache");
154 #endif
155
156         }
157 #endif // end not __APPLE__
158
159         p = Glib::build_filename (p, user_config_directory_name ());
160
161 #ifdef PLATFORM_WINDOWS
162          /* On Windows Glib::get_user_data_dir is the folder to use for local
163                 * (as opposed to roaming) application data.
164                 * See documentation for CSIDL_LOCAL_APPDATA.
165                 * Glib::get_user_data_dir() == GLib::get_user_config_dir()
166                 * so we add an extra subdir *below* the config dir.
167                 */
168         p = Glib::build_filename (p, "cache");
169 #endif
170
171         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
172                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
173                         error << string_compose (_("Cannot create cache directory %1 - cannot run"),
174                                                    p) << endmsg;
175                         exit (1);
176                 }
177         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
178                 error << string_compose (_("Cache directory %1 already exists and is not a directory/folder - cannot run"),
179                                            p) << endmsg;
180                 exit (1);
181         }
182
183         return p;
184 }
185
186 std::string
187 ardour_dll_directory ()
188 {
189 #ifdef PLATFORM_WINDOWS
190         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
191         dll_dir_path = Glib::build_filename (dll_dir_path, "lib");
192         return Glib::build_filename (dll_dir_path, LIBARDOUR);
193 #else
194         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
195         if (s.empty()) {
196                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
197                 ::exit (1);
198         }       
199         return s;
200 #endif
201 }
202
203 #ifdef PLATFORM_WINDOWS
204 Searchpath
205 windows_search_path ()
206 {
207         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
208         dll_dir_path = Glib::build_filename (dll_dir_path, "share");
209         return Glib::build_filename (dll_dir_path, LIBARDOUR);
210 }
211 #endif
212
213 Searchpath
214 ardour_config_search_path ()
215 {
216         static Searchpath search_path;
217
218         if (search_path.empty()) {
219                 // Start by adding the user's personal config folder
220                 search_path += user_config_directory();
221 #ifdef PLATFORM_WINDOWS
222                 // On Windows, add am intermediate configuration folder
223                 // (one that's guaranteed to be writable by all users).
224                 const gchar* const *all_users_folder = g_get_system_config_dirs();
225                 // Despite its slightly odd name, the above returns a single entry which
226                 // corresponds to 'All Users' on Windows (according to the documentation)
227
228                 if (all_users_folder) {
229                         std::string writable_all_users_path = all_users_folder[0];
230                         writable_all_users_path += "\\";
231                         writable_all_users_path += PROGRAM_NAME;
232                         writable_all_users_path += "\\.config";
233 #ifdef _WIN64
234                         writable_all_users_path += "\\win64";
235 #else
236                         writable_all_users_path += "\\win32";
237 #endif
238                         search_path += writable_all_users_path;
239                 }
240
241                 // now add a suitable config path from the bundle
242                 search_path += windows_search_path ();
243 #endif
244                 // finally, add any paths from ARDOUR_CONFIG_PATH if it exists
245                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
246                 if (s.empty()) {
247                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment\n");
248                 } else {
249                         search_path += Searchpath (s);
250                 }
251         }
252
253         return search_path;
254 }
255
256 Searchpath
257 ardour_data_search_path ()
258 {
259         static Searchpath search_path;
260
261         if (search_path.empty()) {
262                 search_path += user_config_directory();
263 #ifdef PLATFORM_WINDOWS
264                 search_path += windows_search_path ();
265 #endif
266                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
267                 if (s.empty()) {
268                         std::cerr << _("ARDOUR_DATA_PATH not set in environment\n");
269                 } else {
270                         search_path += Searchpath (s);
271                 }
272         }
273
274         return search_path;
275 }
276
277 } // namespace ARDOUR