d355231115f13beebceeb44292f8435018bda47f
[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/file_utils.h"
26 #include "pbd/stl_delete.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/search_paths.h"
34 #include "ardour/io.h"
35
36 using namespace std;
37 using namespace PBD;
38
39 namespace ARDOUR {
40
41 std::string
42 user_template_directory ()
43 {
44         return Glib::build_filename (user_config_directory(), templates_dir_name);
45 }
46
47 std::string
48 user_route_template_directory ()
49 {
50         return Glib::build_filename (user_config_directory(), route_templates_dir_name);
51 }
52
53 static bool
54 template_filter (const string &str, void* /*arg*/)
55 {
56         if (!Glib::file_test (str, Glib::FILE_TEST_IS_DIR)) {
57                 return false;
58         }
59
60         return true;
61 }
62
63 static bool
64 route_template_filter (const string &str, void* /*arg*/)
65 {
66         if (str.find (template_suffix) == str.length() - strlen (template_suffix)) {
67                 return true;
68         }
69
70         return false;
71 }
72
73 string
74 session_template_dir_to_file (string const & dir)
75 {
76         return Glib::build_filename (dir, Glib::path_get_basename(dir) + template_suffix);
77 }
78
79
80 void
81 find_session_templates (vector<TemplateInfo>& template_names, bool read_xml)
82 {
83         vector<string> templates;
84
85         find_paths_matching_filter (templates, template_search_path(), template_filter, 0, true, true);
86
87         if (templates.empty()) {
88                 cerr << "Found nothing along " << template_search_path().to_string() << endl;
89                 return;
90         }
91
92         cerr << "Found " << templates.size() << " along " << template_search_path().to_string() << endl;
93
94         for (vector<string>::iterator i = templates.begin(); i != templates.end(); ++i) {
95                 string file = session_template_dir_to_file (*i);
96
97                 TemplateInfo rti;
98                 rti.name = Glib::path_get_basename (*i);
99                 rti.path = *i;
100
101                 if (read_xml) {
102                         XMLTree tree;
103                         if (!tree.read (file.c_str())) {
104                                 continue;
105                         }
106                         // TODO extract description,
107                         // compare to Session::get_info_from_path
108                 }
109
110                 template_names.push_back (rti);
111         }
112 }
113
114 void
115 find_route_templates (vector<TemplateInfo>& template_names)
116 {
117         vector<string> templates;
118
119         find_files_matching_filter (templates, route_template_search_path(), route_template_filter, 0, false, true);
120
121         if (templates.empty()) {
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
145 }