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