Only show user-presets in favorite sidebar
[ardour.git] / libs / ardour / session_directory.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 <cerrno>
20
21 #include <glibmm/fileutils.h>
22 #include <glibmm/miscutils.h>
23
24 #include "pbd/error.h"
25 #include "pbd/compose.h"
26 #include "pbd/file_utils.h"
27 #include "pbd/openuri.h"
28
29 #include "ardour/directory_names.h"
30 #include "ardour/session_directory.h"
31 #include "ardour/utils.h"
32
33 #include "pbd/i18n.h"
34
35 namespace ARDOUR {
36
37 using namespace std;
38 using namespace PBD::sys;
39
40
41 /* keep a static cache because SessionDirectory is used in various places. */
42 std::map<std::string,std::string> SessionDirectory::root_cache;
43
44 SessionDirectory::SessionDirectory (const std::string& session_path)
45         : m_root_path(session_path)
46 {
47
48 }
49
50 SessionDirectory&
51 SessionDirectory::operator= (const std::string& newpath)
52 {
53         m_root_path = newpath;
54         root_cache.clear ();
55         return *this;
56 }
57
58 bool
59 SessionDirectory::create ()
60 {
61         vector<std::string> sub_dirs = sub_directories ();
62         for (vector<std::string>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
63         {
64                 if (g_mkdir_with_parents (i->c_str(), 0755) != 0) {
65                         PBD::error << string_compose(_("Cannot create Session directory at path %1 Error: %2"), *i, g_strerror(errno)) << endmsg;
66                         return false;
67                 }
68         }
69
70         return true;
71 }
72
73 bool
74 SessionDirectory::is_valid () const
75 {
76         if (!Glib::file_test (m_root_path, Glib::FILE_TEST_IS_DIR)) return false;
77
78         vector<std::string> sub_dirs = sub_directories ();
79
80         for (vector<std::string>::iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i) {
81                 if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
82                         PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), *i) << endmsg;
83                         return false;
84                 }
85         }
86         return true;
87 }
88
89 const std::string
90 SessionDirectory::old_sound_path () const
91 {
92         return Glib::build_filename (m_root_path, old_sound_dir_name);
93 }
94
95 const std::string
96 SessionDirectory::sources_root () const
97 {
98         if (root_cache.find (m_root_path) != root_cache.end()) {
99                 return root_cache[m_root_path];
100         }
101
102         root_cache.clear ();
103
104         std::string p = m_root_path;
105
106         // TODO ideally we'd use the session's name() here, and not the containing folder's name.
107         std::string filename = Glib::path_get_basename(p);
108
109         if (filename == ".") {
110                 p = PBD::get_absolute_path (m_root_path);
111         }
112
113         const string legalized_root (legalize_for_path (Glib::path_get_basename(p)));
114
115         std::string sources_root_path = Glib::build_filename (m_root_path, interchange_dir_name);
116
117         /* check the interchange folder:
118          *
119          * 1) if a single subdir exists, use it, regardless of the name
120          * 2) if more than one dir is in interchange: abort, blame the user
121          * 3) if interchange does not exist or no subdir is present,
122          *    use the session-name to create one.
123          *
124          *    We use the name of the containing folder, not the actual
125          *    session name. The latter would require some API changes and
126          *    careful libardour updates:
127          *
128          *    The session object is created with the "snapshot-name", only
129          *    when loading the .ardour session file, the actual name is set.
130          *
131          *    SessionDirectory is created with the session itself
132          *    and picks up the wrong inital name.
133          *
134          *    SessionDirectory is also used directly by the AudioRegionImporter,
135          *    and the peak-file background thread (session.cc).
136          *
137          *        There is no actual benefit to use the session-name instead of
138          *        the folder-name. Under normal circumstances they are always
139          *        identical.  But it would be consistent to prefer the name.
140          */
141         try {
142                 Glib::Dir dir(sources_root_path);
143
144                 std::list<std::string> entries;
145
146                 for (Glib::DirIterator di = dir.begin(); di != dir.end(); di++) {
147                         // ignore hidden files (eg. OS X ".DS_Store")
148                         if ((*di).at(0) == '.') {
149                                 continue;
150                         }
151                         // and skip regular files (eg. Win Thumbs.db)
152                         string fullpath = Glib::build_filename (sources_root_path, *di);
153                         if (!Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR)) {
154                                 continue;
155                         }
156                         entries.push_back(*di);
157                 }
158
159                 if (entries.size() == 1) {
160                         if (entries.front() != legalized_root) {
161                                 PBD::info << _("session-dir and session-name mismatch. Please use 'Menu > Session > Rename' in the future to rename sessions.") << endmsg;
162                         }
163                         root_cache[m_root_path] = Glib::build_filename (sources_root_path, entries.front());
164                 }
165                 else if (entries.size() > 1) {
166                         PBD::open_folder (sources_root_path);
167                         PBD::fatal << string_compose (_("The session's interchange dir is tainted.\nThere is more than one folder in '%1'.\nPlease remove extra subdirs to reduce possible filename ambiguties."), sources_root_path) << endmsg;
168                         assert (0); // not reached
169                 }
170         } catch (Glib::FileError const&) {
171                 ;
172         }
173
174         if (root_cache.find (m_root_path) == root_cache.end()) {
175                 root_cache[m_root_path] = Glib::build_filename (sources_root_path, legalized_root);
176         }
177
178         return root_cache[m_root_path];
179 }
180
181 const std::string
182 SessionDirectory::sources_root_2X () const
183 {
184         std::string p = m_root_path;
185         std::string filename = Glib::path_get_basename(p);
186
187         if (filename == ".") {
188                 p = PBD::get_absolute_path (m_root_path);
189         }
190
191         const string legalized_root (legalize_for_path_2X (Glib::path_get_basename(p)));
192
193         std::string sources_root_path = Glib::build_filename (m_root_path, interchange_dir_name);
194         return Glib::build_filename (sources_root_path, legalized_root);
195 }
196
197 const std::string
198 SessionDirectory::sound_path () const
199 {
200         if (Glib::file_test (old_sound_path (), Glib::FILE_TEST_IS_DIR)) return old_sound_path();
201
202         // the new style sound directory
203         return Glib::build_filename (sources_root(), sound_dir_name);
204 }
205
206 const std::string
207 SessionDirectory::sound_path_2X () const
208 {
209         return Glib::build_filename (sources_root_2X(), sound_dir_name);
210 }
211
212 const std::string
213 SessionDirectory::midi_path () const
214 {
215         return Glib::build_filename (sources_root(), midi_dir_name);
216 }
217
218 const std::string
219 SessionDirectory::midi_patch_path () const
220 {
221         return Glib::build_filename (sources_root(), midi_patch_dir_name);
222 }
223
224 const std::string
225 SessionDirectory::video_path () const
226 {
227         return Glib::build_filename (sources_root(), video_dir_name);
228 }
229
230 const std::string
231 SessionDirectory::peak_path () const
232 {
233         return Glib::build_filename (m_root_path, peak_dir_name);
234 }
235
236 const std::string
237 SessionDirectory::dead_path () const
238 {
239         return Glib::build_filename (m_root_path, dead_dir_name);
240 }
241
242 const std::string
243 SessionDirectory::export_path () const
244 {
245         return Glib::build_filename (m_root_path, export_dir_name);
246 }
247
248 const std::string
249 SessionDirectory::backup_path () const
250 {
251         return Glib::build_filename (m_root_path, backup_dir_name);
252 }
253
254 const vector<std::string>
255 SessionDirectory::sub_directories () const
256 {
257         vector<std::string> tmp_paths;
258
259         tmp_paths.push_back (sound_path ());
260         tmp_paths.push_back (midi_path ());
261         tmp_paths.push_back (video_path ());
262         tmp_paths.push_back (peak_path ());
263         tmp_paths.push_back (dead_path ());
264         tmp_paths.push_back (export_path ());
265         tmp_paths.push_back (backup_path ());
266
267         return tmp_paths;
268 }
269
270 } // namespace ARDOUR