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.
24 #include <glib/gstdio.h>
26 #include <gtkmm/filechooserdialog.h>
27 #include <gtkmm/notebook.h>
28 #include <gtkmm/separator.h>
29 #include <gtkmm/scrolledwindow.h>
30 #include <gtkmm/stock.h>
31 #include <gtkmm/treeiter.h>
33 #include "pbd/basename.h"
34 #include "pbd/error.h"
35 #include "pbd/file_archive.h"
36 #include "pbd/file_utils.h"
38 #include "pbd/xml++.h"
40 #include "gtkmm2ext/gui_thread.h"
42 #include "ardour/filesystem_paths.h"
43 #include "ardour/template_utils.h"
45 #include "template_dialog.h"
50 using namespace ARDOUR;
53 TemplateDialog::TemplateDialog ()
54 : ArdourDialog ("Manage Templates")
56 Notebook* nb = manage (new Notebook);
58 SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
59 nb->append_page (*session_tm, _("Session Templates"));
61 RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
62 nb->append_page (*route_tm, _("Track Templates"));
64 get_vbox()->pack_start (*nb);
65 add_button (_("Ok"), Gtk::RESPONSE_OK);
67 get_vbox()->show_all();
72 session_tm->TemplatesImported.connect (*this, invalidator (*this), boost::bind (&RouteTemplateManager::init, route_tm), gui_context ());
73 route_tm->TemplatesImported.connect (*this, invalidator (*this), boost::bind (&SessionTemplateManager::init, session_tm), gui_context ());
76 TemplateManager::TemplateManager ()
79 , _remove_button (_("Remove"))
80 , _rename_button (_("Rename"))
81 , _export_all_templates_button (_("Export all"))
82 , _import_template_set_button (_("Import"))
84 _template_model = ListStore::create (_template_columns);
85 _template_treeview.set_model (_template_model);
87 _validated_column.set_title (_("Template Name"));
88 _validated_column.pack_start (_validating_cellrenderer);
89 _template_treeview.append_column (_validated_column);
90 _validating_cellrenderer.property_editable() = true;
92 _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
93 _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
94 _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
95 _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
97 ScrolledWindow* sw = manage (new ScrolledWindow);
98 sw->property_hscrollbar_policy() = POLICY_AUTOMATIC;
99 sw->add (_template_treeview);
100 sw->set_size_request (300, 200);
103 VBox* vb_btns = manage (new VBox);
104 vb_btns->set_spacing (4);
105 vb_btns->pack_start (_rename_button, false, false);
106 vb_btns->pack_start (_remove_button, false, false);
108 _rename_button.set_sensitive (false);
109 _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
110 _remove_button.set_sensitive (false);
111 _remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::delete_selected_template));
113 vb_btns->pack_start (*(manage (new VSeparator ())));
115 vb_btns->pack_start (_export_all_templates_button, false, false);
116 vb_btns->pack_start (_import_template_set_button, false, false);
118 _export_all_templates_button.set_sensitive (false);
119 _export_all_templates_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::export_all_templates));
121 _import_template_set_button.set_sensitive (true);
122 _import_template_set_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::import_template_set));
126 VBox* vb = manage (new VBox);
127 vb->pack_start (*sw);
128 vb->pack_start (_progress_bar);
131 pack_start (*vb_btns);
133 show_all_children ();
134 _progress_bar.hide ();
138 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
140 _template_model->clear ();
142 for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
144 row = *(_template_model->append ());
146 row[_template_columns.name] = it->name;
147 row[_template_columns.path] = it->path;
150 _export_all_templates_button.set_sensitive (!templates.empty ());
154 TemplateManager::row_selection_changed ()
156 bool has_selection = false;
157 if (_template_treeview.get_selection()->count_selected_rows () != 0) {
158 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
160 has_selection = true;
164 _rename_button.set_sensitive (has_selection);
165 _remove_button.set_sensitive (has_selection);
169 TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
172 _validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
177 TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
179 const TreePath path (path_string);
180 TreeModel::iterator current = _template_model->get_iter (path);
182 if (current->get_value (_template_columns.name) == new_name) {
186 TreeModel::Children rows = _template_model->children ();
189 for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
190 if (it->get_value (_template_columns.name) == new_name) {
197 error << string_compose (_("Template of name \"%1\" already exists"), new_name) << endmsg;
202 rename_template (current, new_name);
206 TemplateManager::start_edit ()
208 TreeModel::Path path;
210 _template_treeview.get_cursor (path, col);
211 _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
215 TemplateManager::key_event (GdkEventKey* ev)
217 if (ev->keyval == GDK_KEY_F2) {
221 if (ev->keyval == GDK_KEY_Delete) {
222 delete_selected_template ();
230 bool accept_all_files (string const &, void *)
235 static void _set_progress (Progress* p, size_t n, size_t t)
237 p->set_progress (float (n) / float(t));
242 TemplateManager::export_all_templates ()
245 char* td = g_dir_make_tmp ("ardour-templates-XXXXXX", &err);
248 error << string_compose(_("Could not make tmpdir: %1"), err->message) << endmsg;
251 const string tmpdir (td);
253 g_clear_error (&err);
255 FileChooserDialog dialog(_("Save Exported Template Archive"), FILE_CHOOSER_ACTION_SAVE);
256 dialog.set_filename (X_("templates.tar.xz"));
258 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
259 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
261 FileFilter archive_filter;
262 archive_filter.add_pattern (X_("*.tar.xz"));
263 archive_filter.set_name (_("Template archives"));
264 dialog.add_filter (archive_filter);
266 int result = dialog.run ();
268 if (result != RESPONSE_OK || !dialog.get_filename().length()) {
269 PBD::remove_directory (tmpdir);
273 string filename = dialog.get_filename ();
274 if (filename.compare (filename.size () - 7, 7, ".tar.xz")) {
275 filename += ".tar.xz";
278 if (g_file_test (filename.c_str(), G_FILE_TEST_EXISTS)) {
279 ArdourDialog dlg (_("File exists"), true);
280 Label msg (string_compose (_("The file %1 already exists."), filename));
281 dlg.get_vbox()->pack_start (msg);
283 dlg.add_button (_("Overwrite"), RESPONSE_ACCEPT);
284 dlg.add_button (_("Cancel"), RESPONSE_REJECT);
285 dlg.set_default_response (RESPONSE_REJECT);
289 if (result == RESPONSE_REJECT) {
290 PBD::remove_directory (tmpdir);
295 PBD::copy_recurse (templates_dir (), Glib::build_filename (tmpdir, Glib::path_get_basename (templates_dir ())));
297 vector<string> files;
298 PBD::find_files_matching_regex (files, tmpdir, string ("\\.template$"), /* recurse = */ true);
300 vector<string>::const_iterator it;
301 for (it = files.begin(); it != files.end(); ++it) {
302 const string bn = PBD::basename_nosuffix (*it);
303 const string old_path = Glib::build_filename (templates_dir (), bn);
304 const string new_path = Glib::build_filename ("$TEMPLATEDIR", bn);
307 if (!tree.read (*it)) {
310 if (adjust_xml_tree (tree, old_path, new_path)) {
315 find_files_matching_filter (files, tmpdir, accept_all_files, 0, false, true, true);
317 std::map<std::string, std::string> filemap;
318 for (it = files.begin(); it != files.end(); ++it) {
319 filemap[*it] = it->substr (tmpdir.size()+1, it->size() - tmpdir.size() - 1);
322 _current_action = _("Exporting templates");
324 PBD::FileArchive ar (filename);
325 PBD::ScopedConnectionList progress_connection;
326 ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
329 PBD::remove_directory (tmpdir);
333 TemplateManager::import_template_set ()
335 FileChooserDialog dialog (_("Import template archives"));
336 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
337 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
339 FileFilter archive_filter;
340 archive_filter.add_pattern (X_("*.tar.xz"));
341 archive_filter.set_name (_("Template archives"));
342 dialog.add_filter (archive_filter);
344 int result = dialog.run ();
346 if (result != RESPONSE_OK || !dialog.get_filename().length()) {
350 _current_action = _("Importing templates");
352 FileArchive ar (dialog.get_filename ());
353 PBD::ScopedConnectionList progress_connection;
354 ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
355 ar.inflate (user_config_directory ());
357 vector<string> files;
358 PBD::find_files_matching_regex (files, templates_dir (), string ("\\.template$"), /* recurse = */ true);
360 vector<string>::const_iterator it;
361 for (it = files.begin(); it != files.end(); ++it) {
362 const string bn = PBD::basename_nosuffix (*it);
363 const string old_path = Glib::build_filename ("$TEMPLATEDIR", bn);
364 const string new_path = Glib::build_filename (templates_dir (), bn);
367 if (!tree.read (*it)) {
370 if (adjust_xml_tree (tree, old_path, new_path)) {
376 TemplatesImported (); /* emit signal */
380 TemplateManager::adjust_plugin_paths (XMLNode* node, const string& name, const string& new_name) const
382 bool adjusted = false;
384 const XMLNodeList& procs = node->children (X_("Processor"));
385 XMLNodeConstIterator pit;
386 for (pit = procs.begin(); pit != procs.end(); ++pit) {
387 XMLNode* lv2_node = (*pit)->child (X_("lv2"));
393 if (!lv2_node->get_property (X_("template-dir"), template_dir)) {
397 const int suffix_pos = template_dir.size() - name.size();
398 if (suffix_pos < 0) {
399 cerr << "Template name\"" << name << "\" longer than template-dir \"" << template_dir << "\", WTH?" << endl;
403 if (template_dir.compare (suffix_pos, template_dir.size(), name)) {
404 cerr << "Template name \"" << name << "\" no suffix of template-dir \"" << template_dir << "\"" << endl;
408 const string new_template_dir = template_dir.substr (0, suffix_pos) + new_name;
409 lv2_node->set_property (X_("template-dir"), new_template_dir);
418 TemplateManager::update_progress_gui (float p)
421 _progress_bar.hide ();
425 _progress_bar.show ();
426 _progress_bar.set_text (_current_action);
427 _progress_bar.set_fraction (p);
431 void SessionTemplateManager::init ()
433 vector<TemplateInfo> templates;
434 find_session_templates (templates);
435 setup_model (templates);
437 _progress_bar.hide ();
440 void RouteTemplateManager::init ()
442 vector<TemplateInfo> templates;
443 find_route_templates (templates);
444 setup_model (templates);
446 _progress_bar.hide ();
452 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name_)
454 const string old_path = item->get_value (_template_columns.path);
455 const string old_name = item->get_value (_template_columns.name);
456 const string new_name = string (new_name_);
458 const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
462 if (!tree.read(old_file_old_path)) {
463 error << string_compose (_("Could not parse template file \"%1\"."), old_file_old_path) << endmsg;
466 XMLNode* root = tree.root();
468 const XMLNode* const routes_node = root->child (X_("Routes"));
470 const XMLNodeList& routes = routes_node->children (X_("Route"));
471 XMLNodeConstIterator rit;
472 for (rit = routes.begin(); rit != routes.end(); ++rit) {
473 adjust_plugin_paths (*rit, old_name, new_name);
477 const string new_file_old_path = Glib::build_filename (old_path, new_name+".template");
479 tree.set_filename (new_file_old_path);
481 if (!tree.write ()) {
482 error << string_compose(_("Could not write to new template file \"%1\"."), new_file_old_path);
486 const string new_path = Glib::build_filename (user_template_directory (), new_name);
488 if (g_rename (old_path.c_str(), new_path.c_str()) != 0) {
489 error << string_compose (_("Could not rename template directory from \"%1\" to \"%2\": %3"),
490 old_path, new_path, strerror (errno)) << endmsg;
491 g_unlink (new_file_old_path.c_str());
495 const string old_file_new_path = Glib::build_filename (new_path, old_name+".template");
496 if (g_unlink (old_file_new_path.c_str())) {
497 error << string_compose (X_("Could not delete old template file \"%1\": %2"),
498 old_file_new_path, strerror (errno)) << endmsg;
501 item->set_value (_template_columns.name, new_name);
502 item->set_value (_template_columns.path, new_path);
506 SessionTemplateManager::delete_selected_template ()
508 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
512 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
518 PBD::remove_directory (it->get_value (_template_columns.path));
520 _template_model->erase (it);
521 row_selection_changed ();
526 SessionTemplateManager::templates_dir () const
528 return user_template_directory ();
532 SessionTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
534 bool adjusted = false;
535 XMLNode* root = tree.root();
537 const XMLNode* const routes_node = root->child (X_("Routes"));
539 const XMLNodeList& routes = routes_node->children (X_("Route"));
540 XMLNodeConstIterator rit;
541 for (rit = routes.begin(); rit != routes.end(); ++rit) {
542 if (adjust_plugin_paths (*rit, old_name, new_name)) {
553 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
555 const string old_name = item->get_value (_template_columns.name);
556 const string old_filepath = item->get_value (_template_columns.path);
557 const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
560 if (!tree.read (old_filepath)) {
561 error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;
564 tree.root()->set_property (X_("name"), new_name);
565 tree.root()->children().front()->set_property (X_("name"), new_name);
567 const bool adjusted = adjust_plugin_paths (tree.root(), old_name, string (new_name));
569 const string old_state_dir = Glib::build_filename (user_route_template_directory(), old_name);
570 const string new_state_dir = Glib::build_filename (user_route_template_directory(), new_name);
573 if (g_file_test (old_state_dir.c_str(), G_FILE_TEST_EXISTS)) {
574 if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
575 error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
581 tree.set_filename (new_filepath);
583 if (!tree.write ()) {
584 error << string_compose(_("Could not write new template file \"%1\"."), new_filepath) << endmsg;
586 g_rename (new_state_dir.c_str(), old_state_dir.c_str());
591 if (g_unlink (old_filepath.c_str()) != 0) {
592 error << string_compose (_("Could not remove old template file \"%1\": %2"), old_filepath, strerror (errno)) << endmsg;
595 item->set_value (_template_columns.name, string (new_name));
596 item->set_value (_template_columns.path, new_filepath);
600 RouteTemplateManager::delete_selected_template ()
602 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
606 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
612 const string file_path = it->get_value (_template_columns.path);
614 if (g_unlink (file_path.c_str()) != 0) {
615 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
618 PBD::remove_directory (Glib::build_filename (user_route_template_directory (), it->get_value (_template_columns.name)));
620 _template_model->erase (it);
621 row_selection_changed ();
625 RouteTemplateManager::templates_dir () const
627 return user_route_template_directory ();
631 RouteTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
633 return adjust_plugin_paths (tree.root(), old_name, string (new_name));