Only show user-presets in favorite sidebar
[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/error.h"
24 #include "pbd/string_convert.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 "pbd/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 = string_to<int32_t>(X_(PROGRAM_VERSION));
50         }
51
52         /* ARDOUR::Profile may not be available when this is
53            called, so rely on build-time detection of the
54            product name etc.
55         */
56
57 #ifdef USE_TRACKS_CODE_FEATURES
58         /* Tracks does not use versioned configuration folders, which may or
59            may not be problematic in the future.
60         */
61         return X_(PROGRAM_NAME);
62
63 #else
64         const string config_dir_name = string_compose ("%1%2", X_(PROGRAM_NAME), version);
65
66 #if defined (__APPLE__) || defined (PLATFORM_WINDOWS)
67         /* Use mixed-case folder name on OS X and Windows */
68         return config_dir_name;
69 #else
70         /* use lower case folder name on Linux */
71         return downcase (config_dir_name);
72 #endif
73 #endif
74 }
75
76 std::string
77 user_config_directory (int version)
78 {
79         std::string p;
80
81 #ifdef __APPLE__
82
83         p = Glib::build_filename (Glib::get_home_dir(), "Library/Preferences");
84
85 #else
86
87         const char* c = 0;
88         /* adopt freedesktop standards, and put .ardour3 into $XDG_CONFIG_HOME or ~/.config */
89         if ((c = getenv ("XDG_CONFIG_HOME")) != 0) {
90                 p = c;
91         } else {
92
93 #ifdef PLATFORM_WINDOWS
94                 // Not technically the home dir (since it needs to be a writable folder)
95                 const string home_dir = Glib::get_user_config_dir();
96 #else
97                 const string home_dir = Glib::get_home_dir();
98 #endif
99                 if (home_dir.empty ()) {
100                         error << "Unable to determine home directory" << endmsg;
101                         exit (1);
102                 }
103                 p = home_dir;
104
105 #ifndef PLATFORM_WINDOWS
106                 p = Glib::build_filename (p, ".config");
107 #endif
108
109         }
110 #endif // end not __APPLE__
111
112         p = Glib::build_filename (p, user_config_directory_name (version));
113
114         if (version < 0) {
115                 /* Only create the user config dir if the version was negative,
116                    meaning "for the current version.
117                 */
118                 if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
119                         if (g_mkdir_with_parents (p.c_str(), 0755)) {
120                                 error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
121                                                          p) << endmsg;
122                                 exit (1);
123                         }
124                         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
125                         fatal << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
126                                                  p) << endmsg;
127                         abort(); /*NOTREACHED*/
128                 }
129         }
130
131         return p;
132 }
133
134 std::string
135 user_cache_directory (std::string cachename)
136 {
137         std::string p;
138
139 #ifdef __APPLE__
140         p = Glib::build_filename (Glib::get_home_dir(), "Library/Caches");
141 #else
142         const char* c = 0;
143
144         /* adopt freedesktop standards, and put .ardour3 into $XDG_CACHE_HOME
145          * defaulting to or ~/.config
146          */
147         if ((c = getenv ("XDG_CACHE_HOME")) != 0) {
148                 p = c;
149         } else {
150
151 #ifdef PLATFORM_WINDOWS
152                 // Not technically the home dir (since it needs to be a writable folder)
153                 const string home_dir = Glib::get_user_data_dir();
154 #else
155                 const string home_dir = Glib::get_home_dir();
156 #endif
157                 if (home_dir.empty ()) {
158                         error << "Unable to determine home directory" << endmsg;
159                         exit (1);
160                 }
161                 p = home_dir;
162
163 #ifndef PLATFORM_WINDOWS
164                 p = Glib::build_filename (p, ".cache");
165 #endif
166
167         }
168 #endif // end not __APPLE__
169
170         if (cachename.empty ()) {
171                 p = Glib::build_filename (p, user_config_directory_name ());
172         } else {
173                 p = Glib::build_filename (p, cachename);
174         }
175
176 #ifdef PLATFORM_WINDOWS
177          /* On Windows Glib::get_user_data_dir is the folder to use for local
178                 * (as opposed to roaming) application data.
179                 * See documentation for CSIDL_LOCAL_APPDATA.
180                 * Glib::get_user_data_dir() == GLib::get_user_config_dir()
181                 * so we add an extra subdir *below* the config dir.
182                 */
183         p = Glib::build_filename (p, "cache");
184 #endif
185
186         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
187                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
188                         error << string_compose (_("Cannot create cache directory %1 - cannot run"),
189                                                    p) << endmsg;
190                         exit (1);
191                 }
192         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
193                 fatal << string_compose (_("Cache directory %1 already exists and is not a directory/folder - cannot run"),
194                                            p) << endmsg;
195                 abort(); /*NOTREACHED*/
196         }
197
198         return p;
199 }
200
201 std::string
202 ardour_dll_directory ()
203 {
204 #ifdef PLATFORM_WINDOWS
205         std::string dll_dir_path(windows_package_directory_path());
206         dll_dir_path = Glib::build_filename (dll_dir_path, "lib");
207         return Glib::build_filename (dll_dir_path, LIBARDOUR);
208 #else
209         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
210         if (s.empty()) {
211                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
212                 ::exit (1);
213         }
214         return s;
215 #endif
216 }
217
218 #ifdef PLATFORM_WINDOWS
219 Searchpath
220 windows_search_path ()
221 {
222         std::string dll_dir_path(windows_package_directory_path());
223         dll_dir_path = Glib::build_filename (dll_dir_path, "share");
224         return Glib::build_filename (dll_dir_path, LIBARDOUR);
225 }
226
227 std::string
228 windows_package_directory_path ()
229 {
230         char* package_dir =
231             g_win32_get_package_installation_directory_of_module (NULL);
232
233         if (package_dir == NULL) {
234                 fatal << string_compose (_("Cannot determine %1 package directory"),
235                                            PROGRAM_NAME) << endmsg;
236                 abort(); /*NOTREACHED*/
237         }
238
239         std::string package_dir_path(package_dir);
240         g_free(package_dir);
241         return package_dir_path;
242 }
243 #endif
244
245 Searchpath
246 ardour_config_search_path ()
247 {
248         static Searchpath search_path;
249
250         if (search_path.empty()) {
251                 // Start by adding the user's personal config folder
252                 search_path += user_config_directory();
253 #ifdef PLATFORM_WINDOWS
254                 // On Windows, add am intermediate configuration folder
255                 // (one that's guaranteed to be writable by all users).
256                 const gchar* const *all_users_folder = g_get_system_config_dirs();
257                 // Despite its slightly odd name, the above returns a single entry which
258                 // corresponds to 'All Users' on Windows (according to the documentation)
259
260                 if (all_users_folder) {
261                         std::string writable_all_users_path = all_users_folder[0];
262                         writable_all_users_path += "\\";
263                         writable_all_users_path += PROGRAM_NAME;
264                         writable_all_users_path += "\\.config";
265 #ifdef _WIN64
266                         writable_all_users_path += "\\win64";
267 #else
268                         writable_all_users_path += "\\win32";
269 #endif
270                         search_path += writable_all_users_path;
271                 }
272
273                 // now add a suitable config path from the bundle
274                 search_path += windows_search_path ();
275 #endif
276                 // finally, add any paths from ARDOUR_CONFIG_PATH if it exists
277                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
278                 if (s.empty()) {
279                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment\n");
280                 } else {
281                         search_path += Searchpath (s);
282                 }
283         }
284
285         return search_path;
286 }
287
288 Searchpath
289 ardour_data_search_path ()
290 {
291         static Searchpath search_path;
292
293         if (search_path.empty()) {
294                 search_path += user_config_directory();
295 #ifdef PLATFORM_WINDOWS
296                 search_path += windows_search_path ();
297 #endif
298                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
299                 if (s.empty()) {
300                         std::cerr << _("ARDOUR_DATA_PATH not set in environment\n");
301                 } else {
302                         search_path += Searchpath (s);
303                 }
304         }
305
306         return search_path;
307 }
308
309 string
310 been_here_before_path (int version)
311 {
312         if (version < 0) {
313                 version = atoi (PROGRAM_VERSION);
314         }
315
316         return Glib::build_filename (user_config_directory (version), string (".a") + to_string (version));
317 }
318
319
320 } // namespace ARDOUR