stop using STATIC_PATHS to define system search 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
21 #include "pbd/error.h"
22 #include "pbd/compose.h"
23 #include "pbd/filesystem_paths.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 sys::path
40 user_config_directory ()
41 {
42         sys::path p;
43
44 #ifdef __APPLE__
45         p = Glib::get_home_dir();
46         p /= "Library/Preferences";
47
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                         const string error_msg = "Unable to determine home directory";
61
62                         // log the error
63                         error << error_msg << endmsg;
64
65                         throw sys::filesystem_error(error_msg);
66                 }
67
68                 p = home_dir;
69                 p /= ".config";
70         }
71 #endif
72
73         p /= user_config_dir_name;
74
75         std::string ps (p.to_string());
76
77         if (!Glib::file_test (ps, Glib::FILE_TEST_EXISTS)) {
78                 if (g_mkdir_with_parents (ps.c_str(), 0755)) {
79                         error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
80                                                    ps) << endmsg;
81                         exit (1);
82                 }
83         } else if (!Glib::file_test (ps, Glib::FILE_TEST_IS_DIR)) {
84                 error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
85                                            ps) << endmsg;
86                 exit (1);
87         }
88
89         return p;
90 }
91
92 sys::path
93 ardour_module_directory ()
94 {
95         sys::path module_directory(MODULE_DIR);
96         module_directory /= "ardour3";
97         return module_directory;
98 }
99
100 SearchPath
101 ardour_search_path ()
102 {
103         SearchPath spath_env(Glib::getenv("ARDOUR_PATH"));
104         return spath_env;
105 }
106
107 SearchPath
108 system_config_search_path ()
109 {
110         SearchPath config_path(system_config_directories());
111
112         config_path.add_subdirectory_to_paths("ardour3");
113
114         return config_path;
115 }
116
117 SearchPath
118 system_data_search_path ()
119 {
120         SearchPath data_path(system_data_directories());
121
122         data_path.add_subdirectory_to_paths("ardour3");
123
124         return data_path;
125 }
126
127 } // namespace ARDOUR