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