Replace use of PBD::sys::path in ARDOUR::user_config_directory
[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         std::string p;
43
44 #ifdef __APPLE__
45         p = Glib::build_filename (Glib::get_home_dir(), "Library/Preferences");
46 #else
47         const char* c = 0;
48
49         /* adopt freedesktop standards, and put .ardour3 into $XDG_CONFIG_HOME or ~/.config
50          */
51
52         if ((c = getenv ("XDG_CONFIG_HOME")) != 0) {
53                 p = c;
54         } else {
55                 const string home_dir = Glib::get_home_dir();
56
57                 if (home_dir.empty ()) {
58                         error << "Unable to determine home directory" << endmsg;
59                         exit (1);
60                 }
61
62                 p = home_dir;
63                 p = Glib::build_filename (p, ".config");
64         }
65 #endif
66
67         p = Glib::build_filename (p, user_config_dir_name);
68
69         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
70                 if (g_mkdir_with_parents (p.c_str(), 0755)) {
71                         error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
72                                                    p) << endmsg;
73                         exit (1);
74                 }
75         } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
76                 error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
77                                            p) << endmsg;
78                 exit (1);
79         }
80
81         return p;
82 }
83
84 std::string
85 ardour_dll_directory ()
86 {
87         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
88         if (s.empty()) {
89                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
90                 ::exit (1);
91         }       
92         return s;
93 }
94
95 SearchPath
96 ardour_config_search_path ()
97 {
98         static SearchPath search_path;
99
100         if (search_path.empty()) {
101                 search_path += user_config_directory();
102                 
103                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
104                 if (s.empty()) {
105                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment - exiting\n");
106                         ::exit (1);
107                 }
108                 
109                 search_path += SearchPath (s);
110         }
111
112         return search_path;
113 }
114
115 SearchPath
116 ardour_data_search_path ()
117 {
118         static SearchPath search_path;
119
120         if (search_path.empty()) {
121                 search_path += user_config_directory();
122                 
123                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
124                 if (s.empty()) {
125                         std::cerr << _("ARDOUR_DATA_PATH not set in environment - exiting\n");
126                         ::exit (1);
127                 }
128                 
129                 search_path += SearchPath (s);
130         }
131
132         return search_path;
133 }
134
135 } // namespace ARDOUR