Change PBD::PathScanner API to return results by value to avoid inadvertent memory...
[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/stl_delete.h"
28 #include "pbd/xml++.h"
29
30 #include "ardour/template_utils.h"
31 #include "ardour/directory_names.h"
32 #include "ardour/filesystem_paths.h"
33 #include "ardour/filename_extensions.h"
34 #include "ardour/search_paths.h"
35 #include "ardour/io.h"
36
37 using namespace std;
38 using namespace PBD;
39
40 namespace ARDOUR {
41
42 std::string
43 user_template_directory ()
44 {
45         return Glib::build_filename (user_config_directory(), templates_dir_name);
46 }
47
48 std::string
49 user_route_template_directory ()
50 {
51         return Glib::build_filename (user_config_directory(), route_templates_dir_name);
52 }
53
54 static bool
55 template_filter (const string &str, void* /*arg*/)
56 {
57         if (!Glib::file_test (str, Glib::FILE_TEST_IS_DIR)) {
58                 return false;
59         }
60         
61         return true;
62 }
63
64 static bool
65 route_template_filter (const string &str, void* /*arg*/)
66 {
67         if (str.find (template_suffix) == str.length() - strlen (template_suffix)) {
68                 return true;
69         }
70         
71         return false;
72 }
73
74 string
75 session_template_dir_to_file (string const & dir)
76 {
77         return Glib::build_filename (dir, Glib::path_get_basename(dir) + template_suffix);
78 }
79
80
81 void
82 find_session_templates (vector<TemplateInfo>& template_names)
83 {
84         vector<string> templates;
85         PathScanner scanner;
86         Searchpath spath (template_search_path());
87
88         templates = scanner (spath.to_string(), template_filter, 0, true, true);
89
90         if (templates.empty()) {
91                 cerr << "Found nothing along " << spath.to_string() << endl;
92                 return;
93         }
94
95         cerr << "Found " << templates.size() << " along " << spath.to_string() << endl;
96
97         for (vector<string>::iterator i = templates.begin(); i != templates.end(); ++i) {
98                 string file = session_template_dir_to_file (*i);
99
100                 XMLTree tree;
101
102                 if (!tree.read (file.c_str())) {
103                         continue;
104                 }
105
106                 TemplateInfo rti;
107
108                 rti.name = basename_nosuffix (*i);
109                 rti.path = *i;
110
111                 template_names.push_back (rti);
112         }
113 }
114
115 void
116 find_route_templates (vector<TemplateInfo>& template_names)
117 {
118         vector<string> templates;
119         PathScanner scanner;
120         Searchpath spath (route_template_search_path());
121
122         templates = scanner (spath.to_string(), route_template_filter, 0, false, true);
123
124         if (templates.empty()) {
125                 return;
126         }
127
128         for (vector<string>::iterator i = templates.begin(); i != templates.end(); ++i) {
129                 string fullpath = *i;
130
131                 XMLTree tree;
132
133                 if (!tree.read (fullpath.c_str())) {
134                         continue;
135                 }
136
137                 XMLNode* root = tree.root();
138
139                 TemplateInfo rti;
140
141                 rti.name = IO::name_from_state (*root->children().front());
142                 rti.path = fullpath;
143
144                 template_names.push_back (rti);
145         }
146 }
147
148 }