acb691d7dbd63ea3f9c102aa4fcf48714b04bb59
[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                 std::string home_dir;
64
65                 if (0 != PBD::get_win_special_folder(CSIDL_LOCAL_APPDATA))
66                         home_dir = PBD::get_win_special_folder(CSIDL_LOCAL_APPDATA);
67 #else
68                 const string home_dir = Glib::get_home_dir();
69 #endif
70                 if (home_dir.empty ()) {
71                         error << "Unable to determine home directory" << endmsg;
72                         exit (1);
73                 }
74
75                 p = home_dir;
76                 p = Glib::build_filename (p, ".config");
77         }
78 #endif
79
80         p = Glib::build_filename (p, user_config_dir_name);
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          * NB this should work on windows, too, but we may want to prefer
113          * PBD::get_platform_fallback_folder (PBD::FOLDER_VST) or someplace
114          */
115         if ((c = getenv ("XDG_CACHE_HOME")) != 0) {
116                 p = c;
117         } else {
118 #ifdef PLATFORM_WINDOWS
119                 std::string home_dir;
120
121                 if (0 != PBD::get_win_special_folder(CSIDL_LOCAL_APPDATA))
122                         home_dir = PBD::get_win_special_folder(CSIDL_LOCAL_APPDATA);
123 #else
124                 const string home_dir = Glib::get_home_dir();
125 #endif
126
127                 if (home_dir.empty ()) {
128                         error << "Unable to determine home directory" << endmsg;
129                         exit (1);
130                 }
131
132                 p = home_dir;
133                 p = Glib::build_filename (p, ".cache");
134         }
135 #endif
136
137         p = Glib::build_filename (p, user_config_dir_name);
138
139         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
140                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
141                         error << string_compose (_("Cannot create cache directory %1 - cannot run"),
142                                                    p) << endmsg;
143                         exit (1);
144                 }
145         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
146                 error << string_compose (_("Cache directory %1 already exists and is not a directory/folder - cannot run"),
147                                            p) << endmsg;
148                 exit (1);
149         }
150
151         return p;
152 }
153
154 std::string
155 ardour_dll_directory ()
156 {
157 #ifdef PLATFORM_WINDOWS
158         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
159         dll_dir_path = Glib::build_filename (dll_dir_path, "lib");
160         return Glib::build_filename (dll_dir_path, "ardour3");
161 #else
162         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
163         if (s.empty()) {
164                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
165                 ::exit (1);
166         }       
167         return s;
168 #endif
169 }
170
171 #ifdef PLATFORM_WINDOWS
172 Searchpath
173 windows_search_path ()
174 {
175         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
176         dll_dir_path = Glib::build_filename (dll_dir_path, "share");
177         return Glib::build_filename (dll_dir_path, "ardour3");
178 }
179 #endif
180
181 Searchpath
182 ardour_config_search_path ()
183 {
184         static Searchpath search_path;
185
186         if (search_path.empty()) {
187                 search_path += user_config_directory();
188 #ifdef PLATFORM_WINDOWS
189                 search_path += windows_search_path ();
190 #else
191                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
192                 if (s.empty()) {
193                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment - exiting\n");
194                         ::exit (1);
195                 }
196                 
197                 search_path += Searchpath (s);
198 #endif
199         }
200
201         return search_path;
202 }
203
204 Searchpath
205 ardour_data_search_path ()
206 {
207         static Searchpath search_path;
208
209         if (search_path.empty()) {
210                 search_path += user_config_directory();
211 #ifdef PLATFORM_WINDOWS
212                 search_path += windows_search_path ();
213 #else
214                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
215                 if (s.empty()) {
216                         std::cerr << _("ARDOUR_DATA_PATH not set in environment - exiting\n");
217                         ::exit (1);
218                 }
219                 
220                 search_path += Searchpath (s);
221 #endif
222         }
223
224         return search_path;
225 }
226
227 } // namespace ARDOUR