prepare for 'ardour4'
[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/error.h"
23 #include "pbd/compose.h"
24
25 #include <glibmm/miscutils.h>
26 #include <glibmm/fileutils.h>
27
28 #include "ardour/directory_names.h"
29 #include "ardour/filesystem_paths.h"
30
31 #include "i18n.h"
32
33 #ifdef PLATFORM_WINDOWS
34 #include "shlobj.h"
35 #include "pbd/windows_special_dirs.h"
36 #endif
37
38 using namespace PBD;
39
40 namespace ARDOUR {
41
42 using std::string;
43
44 std::string
45 user_config_directory ()
46 {
47         static std::string p;
48
49         if (!p.empty()) return p;
50
51 #ifdef __APPLE__
52
53         p = Glib::build_filename (Glib::get_home_dir(), "Library/Preferences");
54
55 #else
56
57         const char* c = 0;
58         /* adopt freedesktop standards, and put .ardour3 into $XDG_CONFIG_HOME or ~/.config */
59         if ((c = getenv ("XDG_CONFIG_HOME")) != 0) {
60                 p = c;
61         } else {
62
63 #ifdef PLATFORM_WINDOWS
64                 // Not technically the home dir (since it needs to be a writable folder)
65                 const string home_dir = Glib::get_user_config_dir();
66 #else
67                 const string home_dir = Glib::get_home_dir();
68 #endif
69                 if (home_dir.empty ()) {
70                         error << "Unable to determine home directory" << endmsg;
71                         exit (1);
72                 }
73                 p = home_dir;
74
75 #ifndef PLATFORM_WINDOWS
76                 p = Glib::build_filename (p, ".config");
77 #endif
78
79         }
80 #endif // end not __APPLE__
81
82         p = Glib::build_filename (p, user_config_dir_name);
83
84         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
85                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
86                         error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
87                                                    p) << endmsg;
88                         exit (1);
89                 }
90         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
91                 error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
92                                            p) << endmsg;
93                 exit (1);
94         }
95
96         return p;
97 }
98
99 std::string
100 user_cache_directory ()
101 {
102         static std::string p;
103
104         if (!p.empty()) return p;
105
106 #ifdef __APPLE__
107         p = Glib::build_filename (Glib::get_home_dir(), "Library/Caches");
108 #else
109         const char* c = 0;
110
111         /* adopt freedesktop standards, and put .ardour3 into $XDG_CACHE_HOME
112          * defaulting to or ~/.config
113          */
114         if ((c = getenv ("XDG_CACHE_HOME")) != 0) {
115                 p = c;
116         } else {
117
118 #ifdef PLATFORM_WINDOWS
119                 // Not technically the home dir (since it needs to be a writable folder)
120                 const string home_dir = Glib::get_user_data_dir();
121 #else
122                 const string home_dir = Glib::get_home_dir();
123 #endif
124                 if (home_dir.empty ()) {
125                         error << "Unable to determine home directory" << endmsg;
126                         exit (1);
127                 }
128                 p = home_dir;
129
130 #ifndef PLATFORM_WINDOWS
131                 p = Glib::build_filename (p, ".cache");
132 #endif
133
134         }
135 #endif // end not __APPLE__
136
137         p = Glib::build_filename (p, user_config_dir_name);
138
139 #ifdef PLATFORM_WINDOWS
140          /* On Windows Glib::get_user_data_dir is the folder to use for local
141                 * (as opposed to roaming) application data.
142                 * See documentation for CSIDL_LOCAL_APPDATA.
143                 * Glib::get_user_data_dir() == GLib::get_user_config_dir()
144                 * so we add an extra subdir *below* the config dir.
145                 */
146         p = Glib::build_filename (p, "cache");
147 #endif
148
149         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
150                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
151                         error << string_compose (_("Cannot create cache directory %1 - cannot run"),
152                                                    p) << endmsg;
153                         exit (1);
154                 }
155         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
156                 error << string_compose (_("Cache directory %1 already exists and is not a directory/folder - cannot run"),
157                                            p) << endmsg;
158                 exit (1);
159         }
160
161         return p;
162 }
163
164 std::string
165 ardour_dll_directory ()
166 {
167 #ifdef PLATFORM_WINDOWS
168         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
169         dll_dir_path = Glib::build_filename (dll_dir_path, "lib");
170         return Glib::build_filename (dll_dir_path, LIBARDOUR);
171 #else
172         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
173         if (s.empty()) {
174                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
175                 ::exit (1);
176         }       
177         return s;
178 #endif
179 }
180
181 #ifdef PLATFORM_WINDOWS
182 Searchpath
183 windows_search_path ()
184 {
185         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
186         dll_dir_path = Glib::build_filename (dll_dir_path, "share");
187         return Glib::build_filename (dll_dir_path, LIBARDOUR);
188 }
189 #endif
190
191 Searchpath
192 ardour_config_search_path ()
193 {
194         static Searchpath search_path;
195
196         if (search_path.empty()) {
197                 // Start by adding the user's personal config folder
198                 search_path += user_config_directory();
199 #ifdef PLATFORM_WINDOWS
200                 // On Windows, add am intermediate configuration folder
201                 // (one that's guaranteed to be writable by all users).
202                 const gchar* const *all_users_folder = g_get_system_config_dirs();
203                 // Despite its slightly odd name, the above returns a single entry which
204                 // corresponds to 'All Users' on Windows (according to the documentation)
205
206                 if (all_users_folder) {
207                         std::string writable_all_users_path = all_users_folder[0];
208                         writable_all_users_path += "\\";
209                         writable_all_users_path += PROGRAM_NAME;
210                         writable_all_users_path += "\\.config";
211 #ifdef _WIN64
212                         writable_all_users_path += "\\win64";
213 #else
214                         writable_all_users_path += "\\win32";
215 #endif
216                         search_path += writable_all_users_path;
217                 }
218
219                 // now add a suitable config path from the bundle
220                 search_path += windows_search_path ();
221 #endif
222                 // finally, add any paths from ARDOUR_CONFIG_PATH if it exists
223                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
224                 if (s.empty()) {
225                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment\n");
226                 } else {
227                         search_path += Searchpath (s);
228                 }
229         }
230
231         return search_path;
232 }
233
234 Searchpath
235 ardour_data_search_path ()
236 {
237         static Searchpath search_path;
238
239         if (search_path.empty()) {
240                 search_path += user_config_directory();
241 #ifdef PLATFORM_WINDOWS
242                 search_path += windows_search_path ();
243 #endif
244                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
245                 if (s.empty()) {
246                         std::cerr << _("ARDOUR_DATA_PATH not set in environment\n");
247                 } else {
248                         search_path += Searchpath (s);
249                 }
250         }
251
252         return search_path;
253 }
254
255 } // namespace ARDOUR