major rationalization of use of search paths. ardour now has just 4 functions used...
[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 #include "pbd/strsplit.h"
25
26 #include <glibmm/miscutils.h>
27 #include <glibmm/fileutils.h>
28
29 #include "ardour/directory_names.h"
30 #include "ardour/filesystem_paths.h"
31
32 #include "i18n.h"
33
34 using namespace PBD;
35
36 namespace ARDOUR {
37
38 using std::string;
39
40 sys::path
41 user_config_directory ()
42 {
43         sys::path p;
44
45 #ifdef __APPLE__
46         p = Glib::get_home_dir();
47         p /= "Library/Preferences";
48
49 #else
50         const char* c = 0;
51
52         /* adopt freedesktop standards, and put .ardour3 into $XDG_CONFIG_HOME or ~/.config
53          */
54
55         if ((c = getenv ("XDG_CONFIG_HOME")) != 0) {
56                 p = c;
57         } else {
58                 const string home_dir = Glib::get_home_dir();
59
60                 if (home_dir.empty ()) {
61                         const string error_msg = "Unable to determine home directory";
62
63                         // log the error
64                         error << error_msg << endmsg;
65
66                         throw sys::filesystem_error(error_msg);
67                 }
68
69                 p = home_dir;
70                 p /= ".config";
71         }
72 #endif
73
74         p /= user_config_dir_name;
75
76         std::string ps (p.to_string());
77
78         if (!Glib::file_test (ps, Glib::FILE_TEST_EXISTS)) {
79                 if (g_mkdir_with_parents (ps.c_str(), 0755)) {
80                         error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
81                                                    ps) << endmsg;
82                         exit (1);
83                 }
84         } else if (!Glib::file_test (ps, Glib::FILE_TEST_IS_DIR)) {
85                 error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
86                                            ps) << endmsg;
87                 exit (1);
88         }
89
90         return p;
91 }
92
93 sys::path
94 ardour_dll_directory ()
95 {
96         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
97         if (s.empty()) {
98                 std::cerr << _("ARDOUR_CONFIG_PATH not set in environment - exiting\n");
99                 ::exit (1);
100         }       
101         return sys::path (s);
102 }
103
104 SearchPath
105 ardour_config_search_path ()
106 {
107         static bool have_path = false;
108         static SearchPath search_path;
109
110         if (!have_path) {
111                 SearchPath sp (user_config_directory());
112                 
113                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
114                 if (s.empty()) {
115                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment - exiting\n");
116                         ::exit (1);
117                 }
118                 
119                 std::vector<string> ss;
120                 split (s, ss, ':');
121                 for (std::vector<string>::iterator i = ss.begin(); i != ss.end(); ++i) {
122                         sp += sys::path (*i);
123                 }
124                 
125                 search_path = sp;
126                 have_path = true;
127                 std::cerr << "CONFIG PATH: " << search_path.to_string() << std::endl;
128         }
129
130         return search_path;
131 }
132
133 SearchPath
134 ardour_data_search_path ()
135 {
136         static bool have_path = false;
137         static SearchPath search_path;
138
139         if (!have_path) {
140                 SearchPath sp (user_config_directory());
141                 
142                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
143                 if (s.empty()) {
144                         std::cerr << _("ARDOUR_DATA_PATH not set in environment - exiting\n");
145                         ::exit (1);
146                 }
147                 
148                 std::vector<string> ss;
149                 split (s, ss, ':');
150                 for (std::vector<string>::iterator i = ss.begin(); i != ss.end(); ++i) {
151                         sp += sys::path (*i);
152                 }
153                 
154                 search_path = sp;
155                 have_path = true;
156                 std::cerr << "DATA PATH: " << search_path.to_string() << std::endl;
157         }
158
159         return search_path;
160 }
161
162 } // namespace ARDOUR