d7e4a3febc4ab267b00af7b5e4a6a4af7d16cae5
[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 #include "pbd/i18n.h"
37
38 using namespace std;
39 using namespace PBD;
40
41 namespace ARDOUR {
42
43 std::string
44 user_template_directory ()
45 {
46         return Glib::build_filename (user_config_directory(), templates_dir_name);
47 }
48
49 std::string
50 user_route_template_directory ()
51 {
52         return Glib::build_filename (user_config_directory(), route_templates_dir_name);
53 }
54
55 static bool
56 template_filter (const string &str, void* /*arg*/)
57 {
58         if (!Glib::file_test (str, Glib::FILE_TEST_IS_DIR)) {
59                 return false;
60         }
61
62         return true;
63 }
64
65 static bool
66 route_template_filter (const string &str, void* /*arg*/)
67 {
68         if (str.find (template_suffix) == str.length() - strlen (template_suffix)) {
69                 return true;
70         }
71
72         return false;
73 }
74
75 string
76 session_template_dir_to_file (string const & dir)
77 {
78         return Glib::build_filename (dir, Glib::path_get_basename(dir) + template_suffix);
79 }
80
81
82 void
83 find_session_templates (vector<TemplateInfo>& template_names, bool read_xml)
84 {
85         vector<string> templates;
86
87         find_paths_matching_filter (templates, template_search_path(), template_filter, 0, true, true);
88
89         if (templates.empty()) {
90                 cerr << "Found nothing along " << template_search_path().to_string() << endl;
91                 return;
92         }
93
94         cerr << "Found " << templates.size() << " along " << template_search_path().to_string() << endl;
95
96         for (vector<string>::iterator i = templates.begin(); i != templates.end(); ++i) {
97                 string file = session_template_dir_to_file (*i);
98
99                 TemplateInfo rti;
100                 rti.name = Glib::path_get_basename (*i);
101                 rti.path = *i;
102
103                 if (read_xml) {
104                         XMLTree tree;
105                         if (!tree.read (file.c_str())) {
106                                 continue;
107                         }
108
109                         string created_with = "(unknown)";
110                         XMLNode *pv = tree.root()->child("ProgramVersion");
111                         if (pv != 0) {
112                                 pv->get_property (X_("created-with"), created_with);
113                         }
114
115                         string description = "No Description";
116                         XMLNode *desc = tree.root()->child("description");
117                         if (desc != 0) {
118                                 description = desc->attribute_value();
119                         }
120
121                         rti.created_with = created_with;
122                         rti.description = description;
123
124                 }
125
126                 template_names.push_back (rti);
127         }
128 }
129
130 void
131 find_route_templates (vector<TemplateInfo>& template_names)
132 {
133         vector<string> templates;
134
135         find_files_matching_filter (templates, route_template_search_path(), route_template_filter, 0, false, true);
136
137         if (templates.empty()) {
138                 return;
139         }
140
141         for (vector<string>::iterator i = templates.begin(); i != templates.end(); ++i) {
142                 string fullpath = *i;
143
144                 XMLTree tree;
145
146                 if (!tree.read (fullpath.c_str())) {
147                         continue;
148                 }
149
150                 XMLNode* root = tree.root();
151
152                 string description = "No Description";
153                 XMLNode* desc = tree.root()->child ("description");
154                 if (desc) {
155                         description = desc->attribute_value ();
156                 }
157
158                 TemplateInfo rti;
159
160                 rti.name = IO::name_from_state (*root->children().front());
161                 rti.path = fullpath;
162                 rti.description = description;
163
164                 template_names.push_back (rti);
165         }
166 }
167
168 }