add note to filesystem paths
[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 using namespace PBD;
34
35 namespace ARDOUR {
36
37 using std::string;
38
39 std::string
40 user_config_directory ()
41 {
42         static std::string p;
43
44         if (!p.empty()) return p;
45
46 #ifdef __APPLE__
47         p = Glib::build_filename (Glib::get_home_dir(), "Library/Preferences");
48 #else
49         const char* c = 0;
50
51         /* adopt freedesktop standards, and put .ardour3 into $XDG_CONFIG_HOME or ~/.config
52          */
53
54         if ((c = getenv ("XDG_CONFIG_HOME")) != 0) {
55                 p = c;
56         } else {
57                 const string home_dir = Glib::get_home_dir();
58
59                 if (home_dir.empty ()) {
60                         error << "Unable to determine home directory" << endmsg;
61                         exit (1);
62                 }
63
64                 p = home_dir;
65                 p = Glib::build_filename (p, ".config");
66         }
67 #endif
68
69         p = Glib::build_filename (p, user_config_dir_name);
70
71         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
72                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
73                         error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
74                                                    p) << endmsg;
75                         exit (1);
76                 }
77         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
78                 error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
79                                            p) << endmsg;
80                 exit (1);
81         }
82
83         return p;
84 }
85
86 std::string
87 user_cache_directory ()
88 {
89         static std::string p;
90
91         if (!p.empty()) return p;
92
93 #ifdef __APPLE__
94         p = Glib::build_filename (Glib::get_home_dir(), "Library/Caches");
95 #else
96         const char* c = 0;
97
98         /* adopt freedesktop standards, and put .ardour3 into $XDG_CACHE_HOME
99          * defaulting to or ~/.config
100          *
101          * NB this should work on windows, too, but we may want to prefer
102          * PBD::get_platform_fallback_folder (PBD::FOLDER_VST) or someplace
103          */
104         if ((c = getenv ("XDG_CACHE_HOME")) != 0) {
105                 p = c;
106         } else {
107                 const string home_dir = Glib::get_home_dir();
108
109                 if (home_dir.empty ()) {
110                         error << "Unable to determine home directory" << endmsg;
111                         exit (1);
112                 }
113
114                 p = home_dir;
115                 p = Glib::build_filename (p, ".cache");
116         }
117 #endif
118
119         p = Glib::build_filename (p, user_config_dir_name);
120
121         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
122                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
123                         error << string_compose (_("Cannot create cache directory %1 - cannot run"),
124                                                    p) << endmsg;
125                         exit (1);
126                 }
127         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
128                 error << string_compose (_("Cache directory %1 already exists and is not a directory/folder - cannot run"),
129                                            p) << endmsg;
130                 exit (1);
131         }
132
133         return p;
134 }
135
136 std::string
137 ardour_dll_directory ()
138 {
139 #ifdef PLATFORM_WINDOWS
140         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
141         dll_dir_path = Glib::build_filename (dll_dir_path, "lib");
142         return Glib::build_filename (dll_dir_path, "ardour3");
143 #else
144         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
145         if (s.empty()) {
146                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
147                 ::exit (1);
148         }       
149         return s;
150 #endif
151 }
152
153 #ifdef PLATFORM_WINDOWS
154 Searchpath
155 windows_search_path ()
156 {
157         std::string dll_dir_path(g_win32_get_package_installation_directory_of_module(NULL));
158         dll_dir_path = Glib::build_filename (dll_dir_path, "share");
159         return Glib::build_filename (dll_dir_path, "ardour3");
160 }
161 #endif
162
163 Searchpath
164 ardour_config_search_path ()
165 {
166         static Searchpath search_path;
167
168         if (search_path.empty()) {
169                 search_path += user_config_directory();
170 #ifdef PLATFORM_WINDOWS
171                 search_path += windows_search_path ();
172 #else
173                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
174                 if (s.empty()) {
175                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment - exiting\n");
176                         ::exit (1);
177                 }
178                 
179                 search_path += Searchpath (s);
180 #endif
181         }
182
183         return search_path;
184 }
185
186 Searchpath
187 ardour_data_search_path ()
188 {
189         static Searchpath search_path;
190
191         if (search_path.empty()) {
192                 search_path += user_config_directory();
193 #ifdef PLATFORM_WINDOWS
194                 search_path += windows_search_path ();
195 #else
196                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
197                 if (s.empty()) {
198                         std::cerr << _("ARDOUR_DATA_PATH not set in environment - exiting\n");
199                         ::exit (1);
200                 }
201                 
202                 search_path += Searchpath (s);
203 #endif
204         }
205
206         return search_path;
207 }
208
209 } // namespace ARDOUR