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