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