Just noticed a small problem with my previous commit. Windows config files should...
[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                         home_dir += "\\";
68                         home_dir += PROGRAM_NAME;
69                 }
70 #else
71                 const string home_dir = Glib::get_home_dir();
72 #endif
73                 if (home_dir.empty ()) {
74                         error << "Unable to determine home directory" << endmsg;
75                         exit (1);
76                 }
77
78                 p = home_dir;
79                 p = Glib::build_filename (p, ".config");
80         }
81 #endif
82
83         p = Glib::build_filename (p, user_config_dir_name);
84
85         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
86                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
87                         error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
88                                                    p) << endmsg;
89                         exit (1);
90                 }
91         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
92                 error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
93                                            p) << endmsg;
94                 exit (1);
95         }
96
97         return p;
98 }
99
100 std::string
101 user_cache_directory ()
102 {
103         static std::string p;
104
105         if (!p.empty()) return p;
106
107 #ifdef __APPLE__
108         p = Glib::build_filename (Glib::get_home_dir(), "Library/Caches");
109 #else
110         const char* c = 0;
111
112         /* adopt freedesktop standards, and put .ardour3 into $XDG_CACHE_HOME
113          * defaulting to or ~/.config
114          *
115          * NB this should work on windows, too, but we may want to prefer
116          * PBD::get_platform_fallback_folder (PBD::FOLDER_VST) or someplace
117          */
118         if ((c = getenv ("XDG_CACHE_HOME")) != 0) {
119                 p = c;
120         } else {
121 #ifdef PLATFORM_WINDOWS
122                 std::string home_dir;
123
124                 if (0 != PBD::get_win_special_folder(CSIDL_LOCAL_APPDATA)) {
125                         home_dir = PBD::get_win_special_folder(CSIDL_LOCAL_APPDATA);
126                         home_dir += "\\";
127                         home_dir += PROGRAM_NAME;
128                 }
129 #else
130                 const string home_dir = Glib::get_home_dir();
131 #endif
132
133                 if (home_dir.empty ()) {
134                         error << "Unable to determine home directory" << endmsg;
135                         exit (1);
136                 }
137
138                 p = home_dir;
139                 p = Glib::build_filename (p, ".cache");
140         }
141 #endif
142
143         p = Glib::build_filename (p, user_config_dir_name);
144
145         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
146                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
147                         error << string_compose (_("Cannot create cache directory %1 - cannot run"),
148                                                    p) << endmsg;
149                         exit (1);
150                 }
151         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
152                 error << string_compose (_("Cache directory %1 already exists and is not a directory/folder - cannot run"),
153                                            p) << endmsg;
154                 exit (1);
155         }
156
157         return p;
158 }
159
160 std::string
161 ardour_dll_directory ()
162 {
163 #ifdef PLATFORM_WINDOWS
164         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
165         dll_dir_path = Glib::build_filename (dll_dir_path, "lib");
166         return Glib::build_filename (dll_dir_path, "ardour3");
167 #else
168         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
169         if (s.empty()) {
170                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
171                 ::exit (1);
172         }       
173         return s;
174 #endif
175 }
176
177 #ifdef PLATFORM_WINDOWS
178 Searchpath
179 windows_search_path ()
180 {
181         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
182         dll_dir_path = Glib::build_filename (dll_dir_path, "share");
183         return Glib::build_filename (dll_dir_path, "ardour3");
184 }
185 #endif
186
187 Searchpath
188 ardour_config_search_path ()
189 {
190         static Searchpath search_path;
191
192         if (search_path.empty()) {
193                 search_path += user_config_directory();
194 #ifdef PLATFORM_WINDOWS
195                 search_path += windows_search_path ();
196 #else
197                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
198                 if (s.empty()) {
199                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment - exiting\n");
200                         ::exit (1);
201                 }
202                 
203                 search_path += Searchpath (s);
204 #endif
205         }
206
207         return search_path;
208 }
209
210 Searchpath
211 ardour_data_search_path ()
212 {
213         static Searchpath search_path;
214
215         if (search_path.empty()) {
216                 search_path += user_config_directory();
217 #ifdef PLATFORM_WINDOWS
218                 search_path += windows_search_path ();
219 #else
220                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
221                 if (s.empty()) {
222                         std::cerr << _("ARDOUR_DATA_PATH not set in environment - exiting\n");
223                         ::exit (1);
224                 }
225                 
226                 search_path += Searchpath (s);
227 #endif
228         }
229
230         return search_path;
231 }
232
233 } // namespace ARDOUR