3e4797dac1de8bbf361a531f07c11709e5481f4c
[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 SearchPath
23 template_search_path ()
24 {
25         SearchPath spath (ardour_data_search_path());
26         spath.add_subdirectory_to_paths(templates_dir_name);
27         return spath;
28 }
29
30 SearchPath
31 route_template_search_path ()
32 {
33         SearchPath spath (ardour_data_search_path());
34         spath.add_subdirectory_to_paths(route_templates_dir_name);
35         return spath;
36 }
37
38 sys::path
39 user_template_directory ()
40 {
41         sys::path p(user_config_directory());
42         p /= templates_dir_name;
43
44         return p;
45 }
46
47 sys::path
48 user_route_template_directory ()
49 {
50         sys::path p(user_config_directory());
51         p /= route_templates_dir_name;
52         
53         return p;
54 }
55
56 static bool
57 template_filter (const string &str, void */*arg*/)
58 {
59         if (!Glib::file_test (str, Glib::FILE_TEST_IS_DIR)) {
60                 return false;
61         }
62         
63         return true;
64 }
65
66 static bool
67 route_template_filter (const string &str, void */*arg*/)
68 {
69         if (str.find (template_suffix) == str.length() - strlen (template_suffix)) {
70                 return true;
71         }
72         
73         return false;
74 }
75
76 string
77 session_template_dir_to_file (string const & dir)
78 {
79         sys::path dir_path = dir;
80         sys::path file_path = dir;
81         file_path /= dir_path.leaf() + template_suffix;
82         return file_path.to_string ();
83 }
84
85
86 void
87 find_session_templates (vector<TemplateInfo>& template_names)
88 {
89         vector<string *> *templates;
90         PathScanner scanner;
91         SearchPath spath (template_search_path());
92
93         templates = scanner (spath.to_string(), template_filter, 0, true, true);
94
95         if (!templates) {
96                 cerr << "Found nothing along " << spath.to_string() << endl;
97                 return;
98         }
99
100         cerr << "Found " << templates->size() << " along " << spath.to_string() << endl;
101
102         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
103                 string file = session_template_dir_to_file (**i);
104
105                 XMLTree tree;
106
107                 if (!tree.read (file.c_str())) {
108                         continue;
109                 }
110
111                 TemplateInfo rti;
112
113                 rti.name = basename_nosuffix (**i);
114                 rti.path = **i;
115
116                 template_names.push_back (rti);
117         }
118
119         delete templates;
120 }
121
122 void
123 find_route_templates (vector<TemplateInfo>& template_names)
124 {
125         vector<string *> *templates;
126         PathScanner scanner;
127         SearchPath spath (route_template_search_path());
128
129         templates = scanner (spath.to_string(), route_template_filter, 0, false, true);
130
131         if (!templates) {
132                 return;
133         }
134
135         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
136                 string fullpath = *(*i);
137
138                 XMLTree tree;
139
140                 if (!tree.read (fullpath.c_str())) {
141                         continue;
142                 }
143
144                 XMLNode* root = tree.root();
145
146                 TemplateInfo rti;
147
148                 rti.name = IO::name_from_state (*root->children().front());
149                 rti.path = fullpath;
150
151                 template_names.push_back (rti);
152         }
153
154         delete templates;
155 }
156
157 }