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