a long tricky day of playing with ArdourStartup and session naming/loading etc.
[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                 XMLNode* root = tree.root();
98                 
99                 TemplateInfo rti;
100
101                 rti.name = basename_nosuffix (fullpath);
102                 rti.path = fullpath;
103
104                 template_names.push_back (rti);
105         }
106
107         free (templates);
108 }
109
110 void
111 find_route_templates (vector<TemplateInfo>& template_names)
112 {
113         vector<string *> *templates;
114         PathScanner scanner;
115         SearchPath spath (system_route_template_directory());
116         spath += user_route_template_directory ();
117
118         templates = scanner (spath.to_string(), template_filter, 0, false, true);
119         
120         if (!templates) {
121                 return;
122         }
123         
124         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
125                 string fullpath = *(*i);
126
127                 XMLTree tree;
128
129                 if (!tree.read (fullpath.c_str())) {
130                         continue;
131                 }
132
133                 XMLNode* root = tree.root();
134                 
135                 TemplateInfo rti;
136
137                 rti.name = IO::name_from_state (*root->children().front());
138                 rti.path = fullpath;
139
140                 template_names.push_back (rti);
141         }
142
143         free (templates);
144 }
145
146 }