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.
23 #include <glib/gstdio.h>
25 #include <gtkmm/filechooserdialog.h>
26 #include <gtkmm/notebook.h>
27 #include <gtkmm/separator.h>
28 #include <gtkmm/scrolledwindow.h>
29 #include <gtkmm/stock.h>
30 #include <gtkmm/treeiter.h>
32 #include "pbd/basename.h"
33 #include "pbd/error.h"
34 #include "pbd/file_archive.h"
35 #include "pbd/file_utils.h"
37 #include "pbd/xml++.h"
39 #include "ardour/template_utils.h"
41 #include "template_dialog.h"
46 using namespace ARDOUR;
49 TemplateDialog::TemplateDialog ()
50 : ArdourDialog ("Manage Templates")
52 Notebook* nb = manage (new Notebook);
54 SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
55 nb->append_page (*session_tm, _("Session Templates"));
57 RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
58 nb->append_page (*route_tm, _("Track Templates"));
60 get_vbox()->pack_start (*nb);
61 add_button (_("Ok"), Gtk::RESPONSE_OK);
63 get_vbox()->show_all();
69 TemplateManager::TemplateManager ()
72 , _remove_button (_("Remove"))
73 , _rename_button (_("Rename"))
74 , _export_all_templates_button (_("Export all"))
75 , _import_template_set_button (_("Import"))
77 _template_model = ListStore::create (_template_columns);
78 _template_treeview.set_model (_template_model);
80 _validated_column.set_title (_("Template Name"));
81 _validated_column.pack_start (_validating_cellrenderer);
82 _template_treeview.append_column (_validated_column);
83 _validating_cellrenderer.property_editable() = true;
85 _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
86 _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
87 _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
88 _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
90 ScrolledWindow* sw = manage (new ScrolledWindow);
91 sw->property_hscrollbar_policy() = POLICY_AUTOMATIC;
92 sw->add (_template_treeview);
93 sw->set_size_request (300, 200);
96 VBox* vb_btns = manage (new VBox);
97 vb_btns->set_spacing (4);
98 vb_btns->pack_start (_rename_button, false, false);
99 vb_btns->pack_start (_remove_button, false, false);
101 _rename_button.set_sensitive (false);
102 _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
103 _remove_button.set_sensitive (false);
104 _remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::delete_selected_template));
106 vb_btns->pack_start (*(manage (new VSeparator ())));
108 vb_btns->pack_start (_export_all_templates_button, false, false);
109 vb_btns->pack_start (_import_template_set_button, false, false);
111 _export_all_templates_button.set_sensitive (true);
112 _export_all_templates_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::export_all_templates));
114 _import_template_set_button.set_sensitive (true);
115 _import_template_set_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::import_template_set));
119 VBox* vb = manage (new VBox);
120 vb->pack_start (*sw);
121 vb->pack_start (_progress_bar);
124 pack_start (*vb_btns);
126 show_all_children ();
127 _progress_bar.hide ();
131 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
133 _template_model->clear ();
135 for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
137 row = *(_template_model->append ());
139 row[_template_columns.name] = it->name;
140 row[_template_columns.path] = it->path;
145 TemplateManager::row_selection_changed ()
147 bool has_selection = false;
148 if (_template_treeview.get_selection()->count_selected_rows () != 0) {
149 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
151 has_selection = true;
155 _rename_button.set_sensitive (has_selection);
156 _remove_button.set_sensitive (has_selection);
160 TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
163 _validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
168 TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
170 const TreePath path (path_string);
171 TreeModel::iterator current = _template_model->get_iter (path);
173 if (current->get_value (_template_columns.name) == new_name) {
177 TreeModel::Children rows = _template_model->children ();
180 for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
181 if (it->get_value (_template_columns.name) == new_name) {
188 error << string_compose (_("Template of name \"%1\" already exists"), new_name) << endmsg;
193 rename_template (current, new_name);
197 TemplateManager::start_edit ()
199 TreeModel::Path path;
201 _template_treeview.get_cursor (path, col);
202 _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
206 TemplateManager::key_event (GdkEventKey* ev)
208 if (ev->keyval == GDK_KEY_F2) {
212 if (ev->keyval == GDK_KEY_Delete) {
213 delete_selected_template ();
221 bool accept_all_files (string const &, void *)
226 static void _set_progress (Progress* p, size_t n, size_t t)
228 p->set_progress (float (n) / float(t));
233 TemplateManager::export_all_templates ()
236 char* td = g_dir_make_tmp ("ardour-templates-XXXXXX", &err);
239 error << string_compose(_("Could not make tmpdir: %1"), err->message) << endmsg;
242 const string tmpdir (td);
244 g_clear_error (&err);
246 FileChooserDialog dialog(_("Save Exported Template Archive"), FILE_CHOOSER_ACTION_SAVE);
247 dialog.set_filename (X_("templates.tar.xz"));
249 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
250 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
252 FileFilter archive_filter;
253 archive_filter.add_pattern (X_("*.tar.xz"));
254 archive_filter.set_name (_("Template archives"));
255 dialog.add_filter (archive_filter);
257 int result = dialog.run ();
259 if (result != RESPONSE_OK || !dialog.get_filename().length()) {
263 PBD::copy_recurse (templates_dir (), tmpdir);
265 vector<string> files;
266 PBD::find_files_matching_regex (files, tmpdir, string ("\\.template$"), /* recurse = */ true);
268 vector<string>::const_iterator it;
269 for (it = files.begin(); it != files.end(); ++it) {
270 const string bn = PBD::basename_nosuffix (*it);
271 const string old_path = Glib::build_filename (templates_dir (), bn);
272 const string new_path = Glib::build_filename ("$TEMPLATEDIR", bn);
275 if (!tree.read (*it)) {
278 if (adjust_xml_tree (tree, old_path, new_path)) {
283 find_files_matching_filter (files, tmpdir, accept_all_files, 0, false, true, true);
285 std::map<std::string, std::string> filemap;
286 for (it = files.begin(); it != files.end(); ++it) {
287 filemap[*it] = it->substr (tmpdir.size()+1, it->size() - tmpdir.size() - 1);
290 _current_action = _("Exporting templates");
292 PBD::FileArchive ar (dialog.get_filename());
293 PBD::ScopedConnectionList progress_connection;
294 ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
297 PBD::remove_directory (tmpdir);
301 TemplateManager::import_template_set ()
303 FileChooserDialog dialog (_("Import template archives"));
304 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
305 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
307 FileFilter archive_filter;
308 archive_filter.add_pattern (X_("*.tar.xz"));
309 archive_filter.set_name (_("Template archives"));
310 dialog.add_filter (archive_filter);
312 int result = dialog.run ();
314 if (result != RESPONSE_OK || !dialog.get_filename().length()) {
318 if (!g_file_test (templates_dir().c_str(), G_FILE_TEST_IS_DIR)) {
319 cout << "making " << templates_dir() << endl;
320 g_mkdir (templates_dir().c_str(), 0755);
323 _current_action = _("Importing templates");
325 FileArchive ar (dialog.get_filename ());
326 PBD::ScopedConnectionList progress_connection;
327 ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
328 ar.inflate (templates_dir());
330 vector<string> files;
331 PBD::find_files_matching_regex (files, templates_dir (), string ("\\.template$"), /* recurse = */ true);
333 vector<string>::const_iterator it;
334 for (it = files.begin(); it != files.end(); ++it) {
335 const string bn = PBD::basename_nosuffix (*it);
336 const string old_path = Glib::build_filename ("$TEMPLATEDIR", bn);
337 const string new_path = Glib::build_filename (templates_dir (), bn);
340 if (!tree.read (*it)) {
343 if (adjust_xml_tree (tree, old_path, new_path)) {
352 TemplateManager::adjust_plugin_paths (XMLNode* node, const string& name, const string& new_name) const
354 bool adjusted = false;
356 const XMLNodeList& procs = node->children (X_("Processor"));
357 XMLNodeConstIterator pit;
358 for (pit = procs.begin(); pit != procs.end(); ++pit) {
359 XMLNode* lv2_node = (*pit)->child (X_("lv2"));
365 if (!lv2_node->get_property (X_("template-dir"), template_dir)) {
369 const int suffix_pos = template_dir.size() - name.size();
370 if (suffix_pos < 0) {
371 cerr << "Template name\"" << name << "\" longer than template-dir \"" << template_dir << "\", WTH?" << endl;
375 if (template_dir.compare (suffix_pos, template_dir.size(), name)) {
376 cerr << "Template name \"" << name << "\" no suffix of template-dir \"" << template_dir << "\"" << endl;
380 const string new_template_dir = template_dir.substr (0, suffix_pos) + new_name;
381 lv2_node->set_property (X_("template-dir"), new_template_dir);
390 TemplateManager::update_progress_gui (float p)
393 _progress_bar.hide ();
397 _progress_bar.show ();
398 _progress_bar.set_text (_current_action);
399 _progress_bar.set_fraction (p);
403 void SessionTemplateManager::init ()
405 vector<TemplateInfo> templates;
406 find_session_templates (templates);
407 setup_model (templates);
409 _progress_bar.hide ();
412 void RouteTemplateManager::init ()
414 vector<TemplateInfo> templates;
415 find_route_templates (templates);
416 setup_model (templates);
418 _progress_bar.hide ();
423 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name_)
425 const string old_path = item->get_value (_template_columns.path);
426 const string old_name = item->get_value (_template_columns.name);
427 const string new_name = string (new_name_);
429 const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
433 if (!tree.read(old_file_old_path)) {
434 error << string_compose (_("Could not parse template file \"%1\"."), old_file_old_path) << endmsg;
437 XMLNode* root = tree.root();
439 const XMLNode* const routes_node = root->child (X_("Routes"));
441 const XMLNodeList& routes = routes_node->children (X_("Route"));
442 XMLNodeConstIterator rit;
443 for (rit = routes.begin(); rit != routes.end(); ++rit) {
444 adjust_plugin_paths (*rit, old_name, new_name);
448 const string new_file_old_path = Glib::build_filename (old_path, new_name+".template");
450 tree.set_filename (new_file_old_path);
452 if (!tree.write ()) {
453 error << string_compose(_("Could not write to new template file \"%1\"."), new_file_old_path);
457 const string new_path = Glib::build_filename (user_template_directory (), new_name);
459 if (g_rename (old_path.c_str(), new_path.c_str()) != 0) {
460 error << string_compose (_("Could not rename template directory from \"%1\" to \"%2\": %3"),
461 old_path, new_path, strerror (errno)) << endmsg;
462 g_unlink (new_file_old_path.c_str());
466 const string old_file_new_path = Glib::build_filename (new_path, old_name+".template");
467 if (g_unlink (old_file_new_path.c_str())) {
468 error << string_compose (X_("Could not delete old template file \"%1\": %2"),
469 old_file_new_path, strerror (errno)) << endmsg;
472 item->set_value (_template_columns.name, new_name);
473 item->set_value (_template_columns.path, new_path);
477 SessionTemplateManager::delete_selected_template ()
479 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
483 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
489 PBD::remove_directory (it->get_value (_template_columns.path));
491 _template_model->erase (it);
492 row_selection_changed ();
497 SessionTemplateManager::templates_dir () const
499 return user_template_directory ();
503 SessionTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
505 bool adjusted = false;
506 XMLNode* root = tree.root();
508 const XMLNode* const routes_node = root->child (X_("Routes"));
510 const XMLNodeList& routes = routes_node->children (X_("Route"));
511 XMLNodeConstIterator rit;
512 for (rit = routes.begin(); rit != routes.end(); ++rit) {
513 if (adjust_plugin_paths (*rit, old_name, new_name)) {
524 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
526 const string old_name = item->get_value (_template_columns.name);
527 const string old_filepath = item->get_value (_template_columns.path);
528 const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
531 if (!tree.read (old_filepath)) {
532 error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;
535 tree.root()->children().front()->set_property (X_("name"), new_name);
537 const bool adjusted = adjust_plugin_paths (tree.root(), old_name, string (new_name));
539 const string old_state_dir = Glib::build_filename (user_route_template_directory(), old_name);
540 const string new_state_dir = Glib::build_filename (user_route_template_directory(), new_name);
543 if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
544 error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
549 tree.set_filename (new_filepath);
551 if (!tree.write ()) {
552 error << string_compose(_("Could not write new template file \"%1\"."), new_filepath) << endmsg;
554 g_rename (new_state_dir.c_str(), old_state_dir.c_str());
559 if (g_unlink (old_filepath.c_str()) != 0) {
560 error << string_compose (_("Could not remove old template file \"%1\": %2"), old_filepath, strerror (errno)) << endmsg;
563 item->set_value (_template_columns.name, string (new_name));
564 item->set_value (_template_columns.path, new_filepath);
568 RouteTemplateManager::delete_selected_template ()
570 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
574 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
580 const string file_path = it->get_value (_template_columns.path);
582 if (g_unlink (file_path.c_str()) != 0) {
583 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
586 PBD::remove_directory (Glib::build_filename (user_route_template_directory (), it->get_value (_template_columns.name)));
588 _template_model->erase (it);
589 row_selection_changed ();
593 RouteTemplateManager::templates_dir () const
595 return user_route_template_directory ();
599 RouteTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
601 return adjust_plugin_paths (tree.root(), old_name, string (new_name));