merged with trunk revs 2605-2627
[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 <pbd/error.h>
20 #include <pbd/compose.h>
21 #include <pbd/filesystem.h>
22
23 #include <ardour/directory_names.h>
24 #include <ardour/session_directory.h>
25 #include <ardour/utils.h>
26
27 #include "i18n.h"
28
29 namespace ARDOUR {
30
31 using namespace PBD::sys;
32
33 SessionDirectory::SessionDirectory (const path& session_path)
34         : m_root_path(session_path)
35 {
36
37 }
38
39 bool
40 SessionDirectory::create ()
41 {
42         bool is_new = false;
43
44         vector<path> sub_dirs = sub_directories ();
45         for (vector<path>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
46         {
47                 try
48                 {
49                         if(create_directories(*i)) {
50                                 PBD::info << string_compose(_("Created Session directory at path %1"), (*i).to_string()) << endmsg;
51                                 is_new = true;
52                         }
53                 }
54                 catch (PBD::sys::filesystem_error& ex)
55                 {
56                         // log the error
57                         PBD::error << string_compose(_("Cannot create Session directory at path %1 Error: %2"), (*i).to_string(), ex.what()) << endmsg;
58
59                         // and rethrow
60                         throw ex;
61                 }
62         }
63
64         return is_new;
65 }
66
67 bool
68 SessionDirectory::is_valid () const
69 {
70         if (!is_directory (m_root_path)) return false;
71
72         vector<path> sub_dirs = sub_directories ();
73
74         for (vector<path>::iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i) {
75                 if (!is_directory (*i)) {
76                         PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), (*i).to_string()) << endmsg;
77                         return false;
78                 }
79         }
80         return true;
81 }
82
83 const path
84 SessionDirectory::old_sound_path () const
85 {
86         return m_root_path / old_sound_dir_name;
87 }
88
89 const path
90 SessionDirectory::sources_root () const
91 {
92         const string legalized_root(legalize_for_path(m_root_path.leaf()));
93
94         return m_root_path / interchange_dir_name / legalized_root;
95 }
96
97 const path
98 SessionDirectory::sound_path () const
99 {
100         if(is_directory (old_sound_path ())) return old_sound_path();
101
102         // the new style sound directory
103         return sources_root() / sound_dir_name;
104 }
105
106 const path
107 SessionDirectory::midi_path () const
108 {
109         return sources_root() / midi_dir_name;
110 }
111
112 const path
113 SessionDirectory::peak_path () const
114 {
115         return m_root_path / peak_dir_name;
116 }
117
118 const path
119 SessionDirectory::dead_sound_path () const
120 {
121         return m_root_path / dead_sound_dir_name;
122 }
123
124 const path
125 SessionDirectory::dead_midi_path () const
126 {
127         return m_root_path / dead_midi_dir_name;
128 }
129
130 const path
131 SessionDirectory::export_path () const
132 {
133         return m_root_path / export_dir_name;
134 }
135
136 const vector<path>
137 SessionDirectory::sub_directories () const
138 {
139         vector<path> tmp_paths; 
140
141         tmp_paths.push_back ( sound_path () );
142         tmp_paths.push_back ( midi_path () );
143         tmp_paths.push_back ( peak_path () );
144         tmp_paths.push_back ( dead_sound_path () );
145         tmp_paths.push_back ( dead_midi_path () );
146         tmp_paths.push_back ( export_path () );
147
148         return tmp_paths;
149 }
150
151 } // namespace ARDOUR