fix failure to find route templates
[ardour.git] / libs / ardour / template_utils.cc
1 #include <algorithm>
2 #include <cstring>
3
4 #include <glibmm.h>
5
6 #include "pbd/filesystem.h"
7 #include "pbd/basename.h"
8 #include "pbd/pathscanner.h"
9 #include "pbd/xml++.h"
10
11 #include "ardour/template_utils.h"
12 #include "ardour/directory_names.h"
13 #include "ardour/filesystem_paths.h"
14 #include "ardour/filename_extensions.h"
15 #include "ardour/io.h"
16
17 using namespace std;
18 using namespace PBD;
19
20 namespace ARDOUR {
21
22 sys::path
23 system_template_directory ()
24 {
25         SearchPath spath(system_data_search_path());
26         spath.add_subdirectory_to_paths(templates_dir_name);
27
28         // just return the first directory in the search path that exists
29         SearchPath::const_iterator i = std::find_if(spath.begin(), spath.end(), sys::exists);
30
31         if (i == spath.end()) return sys::path();
32
33         return *i;
34 }
35
36 sys::path
37 system_route_template_directory ()
38 {
39         SearchPath spath(system_data_search_path());
40         spath.add_subdirectory_to_paths(route_templates_dir_name);
41
42         // just return the first directory in the search path that exists
43         SearchPath::const_iterator i = std::find_if(spath.begin(), spath.end(), sys::exists);
44
45         if (i == spath.end()) return sys::path();
46
47         return *i;
48 }
49
50 sys::path
51 user_template_directory ()
52 {
53         sys::path p(user_config_directory());
54         p /= templates_dir_name;
55
56         return p;
57 }
58
59 sys::path
60 user_route_template_directory ()
61 {
62         sys::path p(user_config_directory());
63         p /= route_templates_dir_name;
64
65         return p;
66 }
67
68 static bool
69 template_filter (const string &str, void */*arg*/)
70 {
71         if (!Glib::file_test (str, Glib::FILE_TEST_IS_DIR)) {
72                 return false;
73         }
74         
75         return true;
76 }
77
78 static bool
79 route_template_filter (const string &str, void */*arg*/)
80 {
81         if (str.find (template_suffix) == str.length() - strlen (template_suffix)) {
82                 return true;
83         }
84         
85         return false;
86 }
87
88 string
89 session_template_dir_to_file (string const & dir)
90 {
91         sys::path dir_path = dir;
92         sys::path file_path = dir;
93         file_path /= dir_path.leaf() + template_suffix;
94         return file_path.to_string ();
95 }
96
97
98 void
99 find_session_templates (vector<TemplateInfo>& template_names)
100 {
101         vector<string *> *templates;
102         PathScanner scanner;
103         SearchPath spath (system_template_directory());
104         spath += user_template_directory ();
105
106         templates = scanner (spath.to_string(), template_filter, 0, true, true);
107
108         if (!templates) {
109                 cerr << "Found nothing along " << spath.to_string() << endl;
110                 return;
111         }
112
113         cerr << "Found " << templates->size() << " along " << spath.to_string() << endl;
114
115         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
116                 string file = session_template_dir_to_file (**i);
117
118                 XMLTree tree;
119
120                 if (!tree.read (file.c_str())) {
121                         continue;
122                 }
123
124                 TemplateInfo rti;
125
126                 rti.name = basename_nosuffix (**i);
127                 rti.path = **i;
128
129                 template_names.push_back (rti);
130         }
131
132         delete templates;
133 }
134
135 void
136 find_route_templates (vector<TemplateInfo>& template_names)
137 {
138         vector<string *> *templates;
139         PathScanner scanner;
140         SearchPath spath (system_route_template_directory());
141         spath += user_route_template_directory ();
142
143         templates = scanner (spath.to_string(), route_template_filter, 0, false, true);
144
145         if (!templates) {
146                 return;
147         }
148
149         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
150                 string fullpath = *(*i);
151
152                 XMLTree tree;
153
154                 if (!tree.read (fullpath.c_str())) {
155                         continue;
156                 }
157
158                 XMLNode* root = tree.root();
159
160                 TemplateInfo rti;
161
162                 rti.name = IO::name_from_state (*root->children().front());
163                 rti.path = fullpath;
164
165                 template_names.push_back (rti);
166         }
167
168         delete templates;
169 }
170
171 }