Merge branch 'master' into windows
[ardour.git] / libs / ardour / template_utils.cc
1 /*
2     Copyright (C) 2012 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <algorithm>
21 #include <cstring>
22
23 #include <glibmm.h>
24
25 #include "pbd/basename.h"
26 #include "pbd/pathscanner.h"
27 #include "pbd/xml++.h"
28
29 #include "ardour/template_utils.h"
30 #include "ardour/directory_names.h"
31 #include "ardour/filesystem_paths.h"
32 #include "ardour/filename_extensions.h"
33 #include "ardour/io.h"
34
35 using namespace std;
36 using namespace PBD;
37
38 namespace ARDOUR {
39
40 Searchpath
41 template_search_path ()
42 {
43         Searchpath spath (ardour_data_search_path());
44         spath.add_subdirectory_to_paths(templates_dir_name);
45         return spath;
46 }
47
48 Searchpath
49 route_template_search_path ()
50 {
51         Searchpath spath (ardour_data_search_path());
52         spath.add_subdirectory_to_paths(route_templates_dir_name);
53         return spath;
54 }
55
56 std::string
57 user_template_directory ()
58 {
59         return Glib::build_filename (user_config_directory(), templates_dir_name);
60 }
61
62 std::string
63 user_route_template_directory ()
64 {
65         return Glib::build_filename (user_config_directory(), route_templates_dir_name);
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         return Glib::build_filename (dir, Glib::path_get_basename(dir) + template_suffix);
92 }
93
94
95 void
96 find_session_templates (vector<TemplateInfo>& template_names)
97 {
98         vector<string *> *templates;
99         PathScanner scanner;
100         Searchpath spath (template_search_path());
101
102         templates = scanner (spath.to_string(), template_filter, 0, true, true);
103
104         if (!templates) {
105                 cerr << "Found nothing along " << spath.to_string() << endl;
106                 return;
107         }
108
109         cerr << "Found " << templates->size() << " along " << spath.to_string() << endl;
110
111         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
112                 string file = session_template_dir_to_file (**i);
113
114                 XMLTree tree;
115
116                 if (!tree.read (file.c_str())) {
117                         continue;
118                 }
119
120                 TemplateInfo rti;
121
122                 rti.name = basename_nosuffix (**i);
123                 rti.path = **i;
124
125                 template_names.push_back (rti);
126         }
127
128         delete templates;
129 }
130
131 void
132 find_route_templates (vector<TemplateInfo>& template_names)
133 {
134         vector<string *> *templates;
135         PathScanner scanner;
136         Searchpath spath (route_template_search_path());
137
138         templates = scanner (spath.to_string(), route_template_filter, 0, false, true);
139
140         if (!templates) {
141                 return;
142         }
143
144         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
145                 string fullpath = *(*i);
146
147                 XMLTree tree;
148
149                 if (!tree.read (fullpath.c_str())) {
150                         continue;
151                 }
152
153                 XMLNode* root = tree.root();
154
155                 TemplateInfo rti;
156
157                 rti.name = IO::name_from_state (*root->children().front());
158                 rti.path = fullpath;
159
160                 template_names.push_back (rti);
161         }
162
163         delete templates;
164 }
165
166 }