2 Copyright (C) 2010 Paul Davis
3 Author: Johannes Mueller
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <glib/gstdio.h>
23 #include <gtkmm/notebook.h>
24 #include <gtkmm/scrolledwindow.h>
25 #include <gtkmm/treeiter.h>
27 #include "pbd/error.h"
28 #include "pbd/file_utils.h"
30 #include "pbd/xml++.h"
32 #include "ardour/template_utils.h"
34 #include "template_dialog.h"
39 using namespace ARDOUR;
41 TemplateDialog::TemplateDialog ()
42 : ArdourDialog ("Manage Templates")
44 Notebook* nb = manage (new Notebook);
46 SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
48 nb->append_page (*session_tm, _("Session Templates"));
50 RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
52 nb->append_page (*route_tm, _("Track Templates"));
54 get_vbox()->pack_start (*nb);
55 add_button (_("Ok"), Gtk::RESPONSE_OK);
60 TemplateManager::TemplateManager ()
62 , _remove_button (_("Remove"))
63 , _rename_button (_("Rename"))
65 _template_model = ListStore::create (_template_columns);
66 _template_treeview.set_model (_template_model);
68 _validated_column.set_title (_("Template Name"));
69 _validated_column.pack_start (_validating_cellrenderer);
70 _template_treeview.append_column (_validated_column);
71 _validating_cellrenderer.property_editable() = true;
73 _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
74 _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
75 _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
76 _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
78 ScrolledWindow* sw = manage (new ScrolledWindow);
79 sw->property_hscrollbar_policy() = POLICY_AUTOMATIC;
80 sw->add (_template_treeview);
81 sw->set_size_request (300, 200);
84 VBox* vb = manage (new VBox);
86 vb->pack_start (_rename_button, false, false);
87 vb->pack_start (_remove_button, false, false);
89 _rename_button.set_sensitive (false);
90 _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
91 _remove_button.set_sensitive (false);
92 _remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::delete_selected_template));
102 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
104 _template_model->clear ();
106 for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
108 row = *(_template_model->append ());
110 row[_template_columns.name] = it->name;
111 row[_template_columns.path] = it->path;
116 TemplateManager::row_selection_changed ()
118 bool has_selection = false;
119 if (_template_treeview.get_selection()->count_selected_rows () != 0) {
120 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
122 has_selection = true;
126 _rename_button.set_sensitive (has_selection);
127 _remove_button.set_sensitive (has_selection);
131 TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
134 _validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
139 TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
141 const TreePath path (path_string);
142 TreeModel::iterator current = _template_model->get_iter (path);
144 if (current->get_value (_template_columns.name) == new_name) {
148 TreeModel::Children rows = _template_model->children ();
151 for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
152 if (it->get_value (_template_columns.name) == new_name) {
159 error << string_compose (_("Template of name \"%1\" already exists"), new_name) << endmsg;
164 rename_template (current, new_name);
168 TemplateManager::start_edit ()
170 TreeModel::Path path;
172 _template_treeview.get_cursor (path, col);
173 _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
177 TemplateManager::key_event (GdkEventKey* ev)
179 if (ev->keyval == GDK_KEY_F2) {
183 if (ev->keyval == GDK_KEY_Delete) {
184 delete_selected_template ();
192 TemplateManager::adjust_plugin_paths (XMLNode* node, const string& name, const string& new_name) const
194 bool adjusted = false;
196 const XMLNodeList& procs = node->children (X_("Processor"));
197 XMLNodeConstIterator pit;
198 for (pit = procs.begin(); pit != procs.end(); ++pit) {
199 XMLNode* lv2_node = (*pit)->child (X_("lv2"));
205 if (!lv2_node->get_property (X_("template-dir"), template_dir)) {
209 const int suffix_pos = template_dir.size() - name.size();
210 if (suffix_pos < 0) {
211 cerr << "Template name\"" << name << "\" longer than template-dir \"" << template_dir << "\", WTH?" << endl;
215 if (template_dir.compare (suffix_pos, template_dir.size(), name)) {
216 cerr << "Template name \"" << name << "\" no suffix of template-dir \"" << template_dir << "\"" << endl;
220 const string new_template_dir = template_dir.substr (0, suffix_pos) + new_name;
221 lv2_node->set_property (X_("template-dir"), new_template_dir);
229 void SessionTemplateManager::init ()
231 vector<TemplateInfo> templates;
232 find_session_templates (templates);
233 setup_model (templates);
236 void RouteTemplateManager::init ()
238 vector<TemplateInfo> templates;
239 find_route_templates (templates);
240 setup_model (templates);
244 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name_)
246 const string old_path = item->get_value (_template_columns.path);
247 const string old_name = item->get_value (_template_columns.name);
248 const string new_name = string (new_name_);
250 const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
254 if (!tree.read(old_file_old_path)) {
255 error << string_compose (_("Could not parse template file \"%1\"."), old_file_old_path) << endmsg;
258 XMLNode* root = tree.root();
260 const XMLNode* const routes_node = root->child (X_("Routes"));
262 const XMLNodeList& routes = routes_node->children (X_("Route"));
263 XMLNodeConstIterator rit;
264 for (rit = routes.begin(); rit != routes.end(); ++rit) {
265 adjust_plugin_paths (*rit, old_name, new_name);
269 const string new_file_old_path = Glib::build_filename (old_path, new_name+".template");
271 tree.set_filename (new_file_old_path);
273 if (!tree.write ()) {
274 error << string_compose(_("Could not write to new template file \"%1\"."), new_file_old_path);
278 const string new_path = Glib::build_filename (user_template_directory (), new_name);
280 if (g_rename (old_path.c_str(), new_path.c_str()) != 0) {
281 error << string_compose (_("Could not rename template directory from \"%1\" to \"%2\": %3"),
282 old_path, new_path, strerror (errno)) << endmsg;
283 g_unlink (new_file_old_path.c_str());
287 const string old_file_new_path = Glib::build_filename (new_path, old_name+".template");
288 if (g_unlink (old_file_new_path.c_str())) {
289 error << string_compose (X_("Could not delete old template file \"%1\": %2"),
290 old_file_new_path, strerror (errno)) << endmsg;
293 item->set_value (_template_columns.name, new_name);
294 item->set_value (_template_columns.path, new_path);
299 SessionTemplateManager::delete_selected_template ()
301 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
305 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
311 PBD::remove_directory (it->get_value (_template_columns.path));
313 _template_model->erase (it);
314 row_selection_changed ();
318 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
320 const string old_name = item->get_value (_template_columns.name);
321 const string old_filepath = item->get_value (_template_columns.path);
322 const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
325 if (!tree.read (old_filepath)) {
326 error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;
329 tree.root()->children().front()->set_property (X_("name"), new_name);
331 const bool adjusted = adjust_plugin_paths (tree.root(), old_name, string (new_name));
333 const string old_state_dir = Glib::build_filename (user_route_template_directory(), old_name);
334 const string new_state_dir = Glib::build_filename (user_route_template_directory(), new_name);
337 if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
338 error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
343 tree.set_filename (new_filepath);
345 if (!tree.write ()) {
346 error << string_compose(_("Could not write new template file \"%1\"."), new_filepath) << endmsg;
348 g_rename (new_state_dir.c_str(), old_state_dir.c_str());
353 if (g_unlink (old_filepath.c_str()) != 0) {
354 error << string_compose (_("Could not remove old template file \"%1\": %2"), old_filepath, strerror (errno)) << endmsg;
357 item->set_value (_template_columns.name, string (new_name));
358 item->set_value (_template_columns.path, new_filepath);
362 RouteTemplateManager::delete_selected_template ()
364 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
368 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
374 const string file_path = it->get_value (_template_columns.path);
376 if (g_unlink (file_path.c_str()) != 0) {
377 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
380 PBD::remove_directory (Glib::build_filename (user_route_template_directory (), it->get_value (_template_columns.name)));
382 _template_model->erase (it);
383 row_selection_changed ();