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