VST cache rework (part one)
[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         /* 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                         error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
126                                                  p) << endmsg;
127                         exit (1);
128                 }
129         }
130
131         return p;
132 }
133
134 std::string
135 user_cache_directory ()
136 {
137         static std::string p;
138
139         if (!p.empty()) return p;
140
141 #ifdef __APPLE__
142         p = Glib::build_filename (Glib::get_home_dir(), "Library/Caches");
143 #else
144         const char* c = 0;
145
146         /* adopt freedesktop standards, and put .ardour3 into $XDG_CACHE_HOME
147          * defaulting to or ~/.config
148          */
149         if ((c = getenv ("XDG_CACHE_HOME")) != 0) {
150                 p = c;
151         } else {
152
153 #ifdef PLATFORM_WINDOWS
154                 // Not technically the home dir (since it needs to be a writable folder)
155                 const string home_dir = Glib::get_user_data_dir();
156 #else
157                 const string home_dir = Glib::get_home_dir();
158 #endif
159                 if (home_dir.empty ()) {
160                         error << "Unable to determine home directory" << endmsg;
161                         exit (1);
162                 }
163                 p = home_dir;
164
165 #ifndef PLATFORM_WINDOWS
166                 p = Glib::build_filename (p, ".cache");
167 #endif
168
169         }
170 #endif // end not __APPLE__
171
172         p = Glib::build_filename (p, user_config_directory_name ());
173
174 #ifdef PLATFORM_WINDOWS
175          /* On Windows Glib::get_user_data_dir is the folder to use for local
176                 * (as opposed to roaming) application data.
177                 * See documentation for CSIDL_LOCAL_APPDATA.
178                 * Glib::get_user_data_dir() == GLib::get_user_config_dir()
179                 * so we add an extra subdir *below* the config dir.
180                 */
181         p = Glib::build_filename (p, "cache");
182 #endif
183
184         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
185                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
186                         error << string_compose (_("Cannot create cache directory %1 - cannot run"),
187                                                    p) << endmsg;
188                         exit (1);
189                 }
190         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
191                 fatal << string_compose (_("Cache directory %1 already exists and is not a directory/folder - cannot run"),
192                                            p) << endmsg;
193                 exit (1);
194         }
195
196         return p;
197 }
198
199 std::string
200 ardour_dll_directory ()
201 {
202 #ifdef PLATFORM_WINDOWS
203         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
204         dll_dir_path = Glib::build_filename (dll_dir_path, "lib");
205         return Glib::build_filename (dll_dir_path, LIBARDOUR);
206 #else
207         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
208         if (s.empty()) {
209                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
210                 ::exit (1);
211         }       
212         return s;
213 #endif
214 }
215
216 #ifdef PLATFORM_WINDOWS
217 Searchpath
218 windows_search_path ()
219 {
220         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
221         dll_dir_path = Glib::build_filename (dll_dir_path, "share");
222         return Glib::build_filename (dll_dir_path, LIBARDOUR);
223 }
224 #endif
225
226 Searchpath
227 ardour_config_search_path ()
228 {
229         static Searchpath search_path;
230
231         if (search_path.empty()) {
232                 // Start by adding the user's personal config folder
233                 search_path += user_config_directory();
234 #ifdef PLATFORM_WINDOWS
235                 // On Windows, add am intermediate configuration folder
236                 // (one that's guaranteed to be writable by all users).
237                 const gchar* const *all_users_folder = g_get_system_config_dirs();
238                 // Despite its slightly odd name, the above returns a single entry which
239                 // corresponds to 'All Users' on Windows (according to the documentation)
240
241                 if (all_users_folder) {
242                         std::string writable_all_users_path = all_users_folder[0];
243                         writable_all_users_path += "\\";
244                         writable_all_users_path += PROGRAM_NAME;
245                         writable_all_users_path += "\\.config";
246 #ifdef _WIN64
247                         writable_all_users_path += "\\win64";
248 #else
249                         writable_all_users_path += "\\win32";
250 #endif
251                         search_path += writable_all_users_path;
252                 }
253
254                 // now add a suitable config path from the bundle
255                 search_path += windows_search_path ();
256 #endif
257                 // finally, add any paths from ARDOUR_CONFIG_PATH if it exists
258                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
259                 if (s.empty()) {
260                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment\n");
261                 } else {
262                         search_path += Searchpath (s);
263                 }
264         }
265
266         return search_path;
267 }
268
269 Searchpath
270 ardour_data_search_path ()
271 {
272         static Searchpath search_path;
273
274         if (search_path.empty()) {
275                 search_path += user_config_directory();
276 #ifdef PLATFORM_WINDOWS
277                 search_path += windows_search_path ();
278 #endif
279                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
280                 if (s.empty()) {
281                         std::cerr << _("ARDOUR_DATA_PATH not set in environment\n");
282                 } else {
283                         search_path += Searchpath (s);
284                 }
285         }
286
287         return search_path;
288 }
289
290 string
291 been_here_before_path (int version)
292 {
293         if (version < 0) {
294                 version = atoi (PROGRAM_VERSION);
295         }
296
297         return Glib::build_filename (user_config_directory (version), string (".a") + to_string (version, std::dec));
298 }
299
300
301 } // namespace ARDOUR