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"
29 #include "pbd/xml++.h"
31 #include "ardour/template_utils.h"
33 #include "template_dialog.h"
38 using namespace ARDOUR;
40 TemplateDialog::TemplateDialog ()
41 : ArdourDialog ("Manage Templates")
43 Notebook* nb = manage (new Notebook);
45 SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
47 nb->append_page (*session_tm, _("Session Templates"));
49 RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
51 nb->append_page (*route_tm, _("Track Templates"));
53 get_vbox()->pack_start (*nb);
54 add_button (_("Ok"), Gtk::RESPONSE_OK);
59 TemplateManager::TemplateManager ()
61 , _remove_button (_("Remove"))
62 , _rename_button (_("Rename"))
64 _template_model = ListStore::create (_template_columns);
65 _template_treeview.set_model (_template_model);
67 _validated_column.set_title (_("Template Name"));
68 _validated_column.pack_start (_validating_cellrenderer);
69 _template_treeview.append_column (_validated_column);
70 _validating_cellrenderer.property_editable() = true;
72 _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
73 _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
74 _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
75 _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
77 ScrolledWindow* sw = manage (new ScrolledWindow);
78 sw->property_hscrollbar_policy() = POLICY_AUTOMATIC;
79 sw->add (_template_treeview);
80 sw->set_size_request (300, 200);
83 VBox* vb = manage (new VBox);
85 vb->pack_start (_rename_button, false, false);
86 vb->pack_start (_remove_button, false, false);
88 _rename_button.set_sensitive (false);
89 _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
90 _remove_button.set_sensitive (false);
91 _remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::delete_selected_template));
101 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
103 _template_model->clear ();
105 for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
107 row = *(_template_model->append ());
109 row[_template_columns.name] = it->name;
110 row[_template_columns.path] = it->path;
115 TemplateManager::row_selection_changed ()
117 bool has_selection = false;
118 if (_template_treeview.get_selection()->count_selected_rows () != 0) {
119 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
121 has_selection = true;
125 _rename_button.set_sensitive (has_selection);
126 _remove_button.set_sensitive (has_selection);
130 TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
133 _validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
138 TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
140 const TreePath path (path_string);
141 TreeModel::iterator current = _template_model->get_iter (path);
143 if (current->get_value (_template_columns.name) == new_name) {
147 TreeModel::Children rows = _template_model->children ();
150 for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
151 if (it->get_value (_template_columns.name) == new_name) {
158 error << string_compose (_("Template of name \"%1\" already exists"), new_name) << endmsg;
163 rename_template (current, new_name);
167 TemplateManager::start_edit ()
169 TreeModel::Path path;
171 _template_treeview.get_cursor (path, col);
172 _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
176 TemplateManager::key_event (GdkEventKey* ev)
178 if (ev->keyval == GDK_KEY_F2) {
182 if (ev->keyval == GDK_KEY_Delete) {
183 delete_selected_template ();
191 TemplateManager::adjust_plugin_paths (XMLNode* node, const string& name, const string& new_name) const
193 bool adjusted = false;
195 const XMLNodeList& procs = node->children (X_("Processor"));
196 XMLNodeConstIterator pit;
197 for (pit = procs.begin(); pit != procs.end(); ++pit) {
198 XMLNode* lv2_node = (*pit)->child (X_("lv2"));
204 if (!lv2_node->get_property (X_("template-dir"), template_dir)) {
208 const int suffix_pos = template_dir.size() - name.size();
209 if (suffix_pos < 0) {
210 cerr << "Template name\"" << name << "\" longer than template-dir \"" << template_dir << "\", WTH?" << endl;
214 if (template_dir.compare (suffix_pos, template_dir.size(), name)) {
215 cerr << "Template name \"" << name << "\" no suffix of template-dir \"" << template_dir << "\"" << endl;
219 const string new_template_dir = template_dir.substr (0, suffix_pos) + new_name;
220 lv2_node->set_property (X_("template-dir"), new_template_dir);
228 void SessionTemplateManager::init ()
230 vector<TemplateInfo> templates;
231 find_session_templates (templates);
232 setup_model (templates);
235 void RouteTemplateManager::init ()
237 vector<TemplateInfo> templates;
238 find_route_templates (templates);
239 setup_model (templates);
243 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name_)
245 const string old_path = item->get_value (_template_columns.path);
246 const string old_name = item->get_value (_template_columns.name);
247 const string new_name = string (new_name_);
249 const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
253 if (!tree.read(old_file_old_path)) {
254 error << string_compose (_("Could not parse template file \"%1\"."), old_file_old_path) << endmsg;
257 XMLNode* root = tree.root();
259 const XMLNode* const routes_node = root->child (X_("Routes"));
261 const XMLNodeList& routes = routes_node->children (X_("Route"));
262 XMLNodeConstIterator rit;
263 for (rit = routes.begin(); rit != routes.end(); ++rit) {
264 adjust_plugin_paths (*rit, old_name, new_name);
268 const string new_file_old_path = Glib::build_filename (old_path, new_name+".template");
270 tree.set_filename (new_file_old_path);
272 if (!tree.write ()) {
273 error << string_compose(_("Could not write to new template file \"%1\"."), new_file_old_path);
277 const string new_path = Glib::build_filename (user_template_directory (), new_name);
279 if (g_rename (old_path.c_str(), new_path.c_str()) != 0) {
280 error << string_compose (_("Could not rename template directory from \"%1\" to \"%2\": %3"),
281 old_path, new_path, strerror (errno)) << endmsg;
282 g_unlink (new_file_old_path.c_str());
286 const string old_file_new_path = Glib::build_filename (new_path, old_name+".template");
287 if (g_unlink (old_file_new_path.c_str())) {
288 error << string_compose (X_("Could not delete old template file \"%1\": %2"),
289 old_file_new_path, strerror (errno)) << endmsg;
292 item->set_value (_template_columns.name, new_name);
293 item->set_value (_template_columns.path, new_path);
298 SessionTemplateManager::delete_selected_template ()
300 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
304 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
310 const string path = it->get_value (_template_columns.path);
311 const string name = it->get_value (_template_columns.name);
312 const string file_path = Glib::build_filename (path, name+".template");
314 if (g_unlink (file_path.c_str()) != 0) {
315 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
319 if (g_rmdir (path.c_str()) != 0) {
320 error << string_compose(_("Could not delete template directory \"%1\": %2"), path, strerror (errno)) << endmsg;
323 _template_model->erase (it);
324 row_selection_changed ();
328 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
330 const string name = item->get_value (_template_columns.name);
331 const string old_filepath = item->get_value (_template_columns.path);
332 const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
335 if (!tree.read (old_filepath)) {
336 error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;
339 tree.root()->children().front()->set_property (X_("name"), new_name);
341 const bool adjusted = adjust_plugin_paths (tree.root(), name, string (new_name));
344 const string old_state_dir = Glib::build_filename (user_route_template_directory(), name);
345 const string new_state_dir = Glib::build_filename (user_route_template_directory(), new_name);
346 if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
347 error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
352 tree.set_filename (new_filepath);
354 if (!tree.write ()) {
355 error << string_compose(_("Could not write new template file \"%1\"."), new_filepath) << endmsg;
359 if (g_unlink (old_filepath.c_str()) != 0) {
360 error << string_compose (_("Could not remove old template file \"%1\": %2"), old_filepath, strerror (errno)) << endmsg;
363 item->set_value (_template_columns.name, string (new_name));
364 item->set_value (_template_columns.path, new_filepath);
368 RouteTemplateManager::delete_selected_template ()
370 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
374 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
380 const string file_path = it->get_value (_template_columns.path);
382 if (g_unlink (file_path.c_str()) != 0) {
383 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
387 _template_model->erase (it);
388 row_selection_changed ();