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/frame.h>
28 #include <gtkmm/notebook.h>
29 #include <gtkmm/separator.h>
30 #include <gtkmm/scrolledwindow.h>
31 #include <gtkmm/stock.h>
32 #include <gtkmm/treeiter.h>
34 #include "pbd/basename.h"
35 #include "pbd/error.h"
36 #include "pbd/file_archive.h"
37 #include "pbd/file_utils.h"
39 #include "pbd/xml++.h"
41 #include "gtkmm2ext/gui_thread.h"
43 #include "ardour/filesystem_paths.h"
44 #include "ardour/template_utils.h"
46 #include "template_dialog.h"
51 using namespace ARDOUR;
54 TemplateDialog::TemplateDialog ()
55 : ArdourDialog ("Manage Templates")
57 Notebook* nb = manage (new Notebook);
59 SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
60 nb->append_page (*session_tm, _("Session Templates"));
62 RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
63 nb->append_page (*route_tm, _("Track Templates"));
65 get_vbox()->pack_start (*nb);
66 add_button (_("Ok"), Gtk::RESPONSE_OK);
68 get_vbox()->show_all();
73 session_tm->TemplatesImported.connect (*this, invalidator (*this), boost::bind (&RouteTemplateManager::init, route_tm), gui_context ());
74 route_tm->TemplatesImported.connect (*this, invalidator (*this), boost::bind (&SessionTemplateManager::init, session_tm), gui_context ());
77 TemplateManager::TemplateManager ()
80 , _save_desc (_("Save Description"))
82 , _remove_button (_("Remove"))
83 , _rename_button (_("Rename"))
84 , _export_all_templates_button (_("Export all"))
85 , _import_template_set_button (_("Import"))
87 _template_model = ListStore::create (_template_columns);
88 _template_treeview.set_model (_template_model);
90 _validated_column.set_title (_("Template Name"));
91 _validated_column.pack_start (_validating_cellrenderer);
92 _template_treeview.append_column (_validated_column);
93 _validating_cellrenderer.property_editable() = true;
95 _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
96 _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
97 _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
98 _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
100 ScrolledWindow* sw = manage (new ScrolledWindow);
101 sw->property_hscrollbar_policy() = POLICY_AUTOMATIC;
102 sw->add (_template_treeview);
103 sw->set_size_request (300, 200);
105 VBox* vb_btns = manage (new VBox);
106 vb_btns->set_spacing (4);
107 vb_btns->pack_start (_rename_button, false, false);
108 vb_btns->pack_start (_remove_button, false, false);
109 vb_btns->pack_start (_save_desc, false, false);
111 _rename_button.set_sensitive (false);
112 _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
113 _remove_button.set_sensitive (false);
114 _remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::delete_selected_template));
116 vb_btns->pack_start (*(manage (new VSeparator ())));
118 vb_btns->pack_start (_export_all_templates_button, false, false);
119 vb_btns->pack_start (_import_template_set_button, false, false);
121 _export_all_templates_button.set_sensitive (false);
122 _export_all_templates_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::export_all_templates));
124 _import_template_set_button.set_sensitive (true);
125 _import_template_set_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::import_template_set));
129 VBox* vb = manage (new VBox);
130 vb->pack_start (*sw);
131 vb->pack_start (_progress_bar);
133 Frame* desc_frame = manage (new Frame (_("Description")));
135 _description_editor.set_wrap_mode (Gtk::WRAP_WORD);
136 _description_editor.set_size_request (300,400);
137 _description_editor.set_border_width (6);
139 _save_desc.set_sensitive (false);
140 _save_desc.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::save_template_desc));
142 _description_editor.get_buffer()->signal_changed().connect (sigc::mem_fun (*this, &TemplateManager::set_desc_dirty));
144 desc_frame->add (_description_editor);
147 pack_start (*desc_frame);
148 pack_start (*vb_btns);
150 show_all_children ();
151 _progress_bar.hide ();
155 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
157 _template_model->clear ();
159 for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
161 row = *(_template_model->append ());
163 row[_template_columns.name] = it->name;
164 row[_template_columns.path] = it->path;
165 row[_template_columns.description] = it->description;
168 _export_all_templates_button.set_sensitive (!templates.empty ());
172 TemplateManager::row_selection_changed ()
174 bool has_selection = false;
175 if (_template_treeview.get_selection()->count_selected_rows () != 0) {
176 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
178 has_selection = true;
179 const string desc = it->get_value (_template_columns.description);
180 _description_editor.get_buffer()->set_text (desc);
182 _save_desc.set_sensitive (false);
186 _rename_button.set_sensitive (has_selection);
187 _remove_button.set_sensitive (has_selection);
191 TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
194 _validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
199 TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
201 const TreePath path (path_string);
202 TreeModel::iterator current = _template_model->get_iter (path);
204 if (current->get_value (_template_columns.name) == new_name) {
208 TreeModel::Children rows = _template_model->children ();
211 for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
212 if (it->get_value (_template_columns.name) == new_name) {
219 error << string_compose (_("Template of name \"%1\" already exists"), new_name) << endmsg;
224 rename_template (current, new_name);
228 TemplateManager::start_edit ()
230 TreeModel::Path path;
232 _template_treeview.get_cursor (path, col);
233 _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
237 TemplateManager::set_desc_dirty ()
240 _save_desc.set_sensitive (true);
244 TemplateManager::save_template_desc ()
246 const Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
247 const string file_path = template_file (it);
249 const string desc_txt = _description_editor.get_buffer()->get_text ();
250 it->set_value (_template_columns.description, desc_txt);
254 if (!tree.read(file_path)) {
255 error << string_compose (_("Could not parse template file \"%1\"."), file_path) << endmsg;
259 tree.root()->remove_nodes (X_("description"));
260 XMLNode* desc = new XMLNode (X_("description"));
262 XMLNode* dn = new XMLNode (X_("content"), desc_txt);
263 desc->add_child_nocopy (*dn);
265 tree.root()->add_child_nocopy (*desc);
267 if (!tree.write ()) {
268 error << string_compose(X_("Could not write to template file \"%1\"."), file_path) << endmsg;
272 _save_desc.set_sensitive (false);
277 TemplateManager::key_event (GdkEventKey* ev)
279 if (ev->keyval == GDK_KEY_F2) {
283 if (ev->keyval == GDK_KEY_Delete) {
284 delete_selected_template ();
292 bool accept_all_files (string const &, void *)
297 static void _set_progress (Progress* p, size_t n, size_t t)
299 p->set_progress (float (n) / float(t));
304 TemplateManager::export_all_templates ()
307 char* td = g_dir_make_tmp ("ardour-templates-XXXXXX", &err);
310 error << string_compose(_("Could not make tmpdir: %1"), err->message) << endmsg;
313 const string tmpdir (td);
315 g_clear_error (&err);
317 FileChooserDialog dialog(_("Save Exported Template Archive"), FILE_CHOOSER_ACTION_SAVE);
318 dialog.set_filename (X_("templates.tar.xz"));
320 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
321 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
323 FileFilter archive_filter;
324 archive_filter.add_pattern (X_("*.tar.xz"));
325 archive_filter.set_name (_("Template archives"));
326 dialog.add_filter (archive_filter);
328 int result = dialog.run ();
330 if (result != RESPONSE_OK || !dialog.get_filename().length()) {
331 PBD::remove_directory (tmpdir);
335 string filename = dialog.get_filename ();
336 if (filename.compare (filename.size () - 7, 7, ".tar.xz")) {
337 filename += ".tar.xz";
340 if (g_file_test (filename.c_str(), G_FILE_TEST_EXISTS)) {
341 ArdourDialog dlg (_("File exists"), true);
342 Label msg (string_compose (_("The file %1 already exists."), filename));
343 dlg.get_vbox()->pack_start (msg);
345 dlg.add_button (_("Overwrite"), RESPONSE_ACCEPT);
346 dlg.add_button (_("Cancel"), RESPONSE_REJECT);
347 dlg.set_default_response (RESPONSE_REJECT);
351 if (result == RESPONSE_REJECT) {
352 PBD::remove_directory (tmpdir);
357 PBD::copy_recurse (templates_dir (), Glib::build_filename (tmpdir, Glib::path_get_basename (templates_dir ())));
359 vector<string> files;
360 PBD::find_files_matching_regex (files, tmpdir, string ("\\.template$"), /* recurse = */ true);
362 vector<string>::const_iterator it;
363 for (it = files.begin(); it != files.end(); ++it) {
364 const string bn = PBD::basename_nosuffix (*it);
365 const string old_path = Glib::build_filename (templates_dir (), bn);
366 const string new_path = Glib::build_filename ("$TEMPLATEDIR", bn);
369 if (!tree.read (*it)) {
372 if (adjust_xml_tree (tree, old_path, new_path)) {
377 find_files_matching_filter (files, tmpdir, accept_all_files, 0, false, true, true);
379 std::map<std::string, std::string> filemap;
380 for (it = files.begin(); it != files.end(); ++it) {
381 filemap[*it] = it->substr (tmpdir.size()+1, it->size() - tmpdir.size() - 1);
384 _current_action = _("Exporting templates");
386 PBD::FileArchive ar (filename);
387 PBD::ScopedConnectionList progress_connection;
388 ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
391 PBD::remove_directory (tmpdir);
395 TemplateManager::import_template_set ()
397 FileChooserDialog dialog (_("Import template archives"));
398 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
399 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
401 FileFilter archive_filter;
402 archive_filter.add_pattern (X_("*.tar.xz"));
403 archive_filter.set_name (_("Template archives"));
404 dialog.add_filter (archive_filter);
406 int result = dialog.run ();
408 if (result != RESPONSE_OK || !dialog.get_filename().length()) {
412 _current_action = _("Importing templates");
414 FileArchive ar (dialog.get_filename ());
415 PBD::ScopedConnectionList progress_connection;
416 ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
417 ar.inflate (user_config_directory ());
419 vector<string> files;
420 PBD::find_files_matching_regex (files, templates_dir (), string ("\\.template$"), /* recurse = */ true);
422 vector<string>::const_iterator it;
423 for (it = files.begin(); it != files.end(); ++it) {
424 const string bn = PBD::basename_nosuffix (*it);
425 const string old_path = Glib::build_filename ("$TEMPLATEDIR", bn);
426 const string new_path = Glib::build_filename (templates_dir (), bn);
429 if (!tree.read (*it)) {
432 if (adjust_xml_tree (tree, old_path, new_path)) {
438 TemplatesImported (); /* emit signal */
442 TemplateManager::adjust_plugin_paths (XMLNode* node, const string& name, const string& new_name) const
444 bool adjusted = false;
446 const XMLNodeList& procs = node->children (X_("Processor"));
447 XMLNodeConstIterator pit;
448 for (pit = procs.begin(); pit != procs.end(); ++pit) {
449 XMLNode* lv2_node = (*pit)->child (X_("lv2"));
455 if (!lv2_node->get_property (X_("template-dir"), template_dir)) {
459 const int suffix_pos = template_dir.size() - name.size();
460 if (suffix_pos < 0) {
461 cerr << "Template name\"" << name << "\" longer than template-dir \"" << template_dir << "\", WTH?" << endl;
465 if (template_dir.compare (suffix_pos, template_dir.size(), name)) {
466 cerr << "Template name \"" << name << "\" no suffix of template-dir \"" << template_dir << "\"" << endl;
470 const string new_template_dir = template_dir.substr (0, suffix_pos) + new_name;
471 lv2_node->set_property (X_("template-dir"), new_template_dir);
480 TemplateManager::update_progress_gui (float p)
483 _progress_bar.hide ();
487 _progress_bar.show ();
488 _progress_bar.set_text (_current_action);
489 _progress_bar.set_fraction (p);
493 void SessionTemplateManager::init ()
495 vector<TemplateInfo> templates;
496 find_session_templates (templates, /* read_xml = */ true);
497 setup_model (templates);
499 _progress_bar.hide ();
502 void RouteTemplateManager::init ()
504 vector<TemplateInfo> templates;
505 find_route_templates (templates);
506 setup_model (templates);
508 _progress_bar.hide ();
514 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name_)
516 const string old_path = item->get_value (_template_columns.path);
517 const string old_name = item->get_value (_template_columns.name);
518 const string new_name = string (new_name_);
520 const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
524 if (!tree.read(old_file_old_path)) {
525 error << string_compose (_("Could not parse template file \"%1\"."), old_file_old_path) << endmsg;
528 XMLNode* root = tree.root();
530 const XMLNode* const routes_node = root->child (X_("Routes"));
532 const XMLNodeList& routes = routes_node->children (X_("Route"));
533 XMLNodeConstIterator rit;
534 for (rit = routes.begin(); rit != routes.end(); ++rit) {
535 adjust_plugin_paths (*rit, old_name, new_name);
539 const string new_file_old_path = Glib::build_filename (old_path, new_name+".template");
541 tree.set_filename (new_file_old_path);
543 if (!tree.write ()) {
544 error << string_compose(_("Could not write to new template file \"%1\"."), new_file_old_path);
548 const string new_path = Glib::build_filename (user_template_directory (), new_name);
550 if (g_rename (old_path.c_str(), new_path.c_str()) != 0) {
551 error << string_compose (_("Could not rename template directory from \"%1\" to \"%2\": %3"),
552 old_path, new_path, strerror (errno)) << endmsg;
553 g_unlink (new_file_old_path.c_str());
557 const string old_file_new_path = Glib::build_filename (new_path, old_name+".template");
558 if (g_unlink (old_file_new_path.c_str())) {
559 error << string_compose (X_("Could not delete old template file \"%1\": %2"),
560 old_file_new_path, strerror (errno)) << endmsg;
563 item->set_value (_template_columns.name, new_name);
564 item->set_value (_template_columns.path, new_path);
568 SessionTemplateManager::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 PBD::remove_directory (it->get_value (_template_columns.path));
582 _template_model->erase (it);
583 row_selection_changed ();
587 SessionTemplateManager::templates_dir () const
589 return user_template_directory ();
594 SessionTemplateManager::template_file (const TreeModel::const_iterator& item) const
596 const string path = item->get_value (_template_columns.path);
597 const string name = item->get_value (_template_columns.name);
598 return Glib::build_filename (path, name+".template");
602 SessionTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
604 bool adjusted = false;
605 XMLNode* root = tree.root();
607 const XMLNode* const routes_node = root->child (X_("Routes"));
609 const XMLNodeList& routes = routes_node->children (X_("Route"));
610 XMLNodeConstIterator rit;
611 for (rit = routes.begin(); rit != routes.end(); ++rit) {
612 if (adjust_plugin_paths (*rit, old_name, new_name)) {
623 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
625 const string old_name = item->get_value (_template_columns.name);
626 const string old_filepath = item->get_value (_template_columns.path);
627 const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
630 if (!tree.read (old_filepath)) {
631 error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;
634 tree.root()->set_property (X_("name"), new_name);
635 tree.root()->children().front()->set_property (X_("name"), new_name);
637 const bool adjusted = adjust_plugin_paths (tree.root(), old_name, string (new_name));
639 const string old_state_dir = Glib::build_filename (user_route_template_directory(), old_name);
640 const string new_state_dir = Glib::build_filename (user_route_template_directory(), new_name);
643 if (g_file_test (old_state_dir.c_str(), G_FILE_TEST_EXISTS)) {
644 if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
645 error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
651 tree.set_filename (new_filepath);
653 if (!tree.write ()) {
654 error << string_compose(_("Could not write new template file \"%1\"."), new_filepath) << endmsg;
656 g_rename (new_state_dir.c_str(), old_state_dir.c_str());
661 if (g_unlink (old_filepath.c_str()) != 0) {
662 error << string_compose (_("Could not remove old template file \"%1\": %2"), old_filepath, strerror (errno)) << endmsg;
665 item->set_value (_template_columns.name, string (new_name));
666 item->set_value (_template_columns.path, new_filepath);
670 RouteTemplateManager::delete_selected_template ()
672 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
676 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
682 const string file_path = it->get_value (_template_columns.path);
684 if (g_unlink (file_path.c_str()) != 0) {
685 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
688 PBD::remove_directory (Glib::build_filename (user_route_template_directory (), it->get_value (_template_columns.name)));
690 _template_model->erase (it);
691 row_selection_changed ();
695 RouteTemplateManager::templates_dir () const
697 return user_route_template_directory ();
702 RouteTemplateManager::template_file (const TreeModel::const_iterator& item) const
704 return item->get_value (_template_columns.path);
708 RouteTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
710 return adjust_plugin_paths (tree.root(), old_name, string (new_name));