Change the meaning of the return value in SessionDirectory::create
[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 <glibmm/fileutils.h>
20 #include <glibmm/miscutils.h>
21
22 #include "pbd/error.h"
23 #include "pbd/compose.h"
24 #include "pbd/file_utils.h"
25
26 #include "ardour/directory_names.h"
27 #include "ardour/session_directory.h"
28 #include "ardour/utils.h"
29
30 #include "i18n.h"
31
32 namespace ARDOUR {
33
34 using namespace std;
35 using namespace PBD::sys;
36
37 SessionDirectory::SessionDirectory (const std::string& session_path)
38         : m_root_path(session_path)
39 {
40
41 }
42
43 SessionDirectory& 
44 SessionDirectory::operator= (const std::string& newpath)
45 {
46         m_root_path = newpath;
47         return *this;
48 }
49
50 bool
51 SessionDirectory::create ()
52 {
53         vector<std::string> sub_dirs = sub_directories ();
54         for (vector<std::string>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
55         {
56                 if (g_mkdir_with_parents (i->c_str(), 0755) != 0) {
57                         PBD::error << string_compose(_("Cannot create Session directory at path %1 Error: %2"), *i, g_strerror(errno)) << endmsg;
58                         return false;
59                 }
60         }
61
62         return true;
63 }
64
65 bool
66 SessionDirectory::is_valid () const
67 {
68         if (!Glib::file_test (m_root_path, Glib::FILE_TEST_IS_DIR)) return false;
69
70         vector<std::string> sub_dirs = sub_directories ();
71
72         for (vector<std::string>::iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i) {
73                 if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
74                         PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), *i) << endmsg;
75                         return false;
76                 }
77         }
78         return true;
79 }
80
81 const std::string
82 SessionDirectory::old_sound_path () const
83 {
84         return Glib::build_filename (m_root_path, old_sound_dir_name);
85 }
86
87 const std::string
88 SessionDirectory::sources_root () const
89 {
90         std::string p = m_root_path;
91         std::string filename = Glib::path_get_basename(p);
92
93         if (filename == ".") {
94                 p = PBD::get_absolute_path (m_root_path);
95         }
96
97         const string legalized_root (legalize_for_path (Glib::path_get_basename(p)));
98
99         std::string sources_root_path = Glib::build_filename (m_root_path, interchange_dir_name);
100         return Glib::build_filename (sources_root_path, legalized_root);
101 }
102
103 const std::string
104 SessionDirectory::sound_path () const
105 {
106         if (Glib::file_test (old_sound_path (), Glib::FILE_TEST_IS_DIR)) return old_sound_path();
107
108         // the new style sound directory
109         return Glib::build_filename (sources_root(), sound_dir_name);
110 }
111
112 const std::string
113 SessionDirectory::midi_path () const
114 {
115         return Glib::build_filename (sources_root(), midi_dir_name);
116 }
117
118 const std::string
119 SessionDirectory::midi_patch_path () const
120 {
121         return Glib::build_filename (sources_root(), midi_patch_dir_name);
122 }
123
124 const std::string
125 SessionDirectory::peak_path () const
126 {
127         return Glib::build_filename (m_root_path, peak_dir_name);
128 }
129
130 const std::string
131 SessionDirectory::dead_path () const
132 {
133         return Glib::build_filename (m_root_path, dead_dir_name);
134 }
135
136 const std::string
137 SessionDirectory::export_path () const
138 {
139         return Glib::build_filename (m_root_path, export_dir_name);
140 }
141
142 const vector<std::string>
143 SessionDirectory::sub_directories () const
144 {
145         vector<std::string> tmp_paths;
146
147         tmp_paths.push_back (sound_path ());
148         tmp_paths.push_back (midi_path ());
149         tmp_paths.push_back (peak_path ());
150         tmp_paths.push_back (dead_path ());
151         tmp_paths.push_back (export_path ());
152
153         return tmp_paths;
154 }
155
156 } // namespace ARDOUR