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 XMLNode* md = tree.root()->child (X_("Metadata"));
261 md = new XMLNode (X_("Metadata"));
262 tree.root()->add_child_nocopy (*md);
264 XMLNode* desc = md->child (X_("description"));
266 desc = new XMLNode (X_("description"));
267 md->add_child_nocopy (*desc);
269 XMLNode* dn = new XMLNode (X_("content"), desc_txt);
270 desc->add_child_nocopy (*dn);
272 if (!tree.write ()) {
273 error << string_compose(X_("Could not write to template file \"%1\"."), file_path) << endmsg;
277 _save_desc.set_sensitive (false);
282 TemplateManager::key_event (GdkEventKey* ev)
284 if (ev->keyval == GDK_KEY_F2) {
288 if (ev->keyval == GDK_KEY_Delete) {
289 delete_selected_template ();
297 bool accept_all_files (string const &, void *)
302 static void _set_progress (Progress* p, size_t n, size_t t)
304 p->set_progress (float (n) / float(t));
309 TemplateManager::export_all_templates ()
312 char* td = g_dir_make_tmp ("ardour-templates-XXXXXX", &err);
315 error << string_compose(_("Could not make tmpdir: %1"), err->message) << endmsg;
318 const string tmpdir (td);
320 g_clear_error (&err);
322 FileChooserDialog dialog(_("Save Exported Template Archive"), FILE_CHOOSER_ACTION_SAVE);
323 dialog.set_filename (X_("templates.tar.xz"));
325 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
326 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
328 FileFilter archive_filter;
329 archive_filter.add_pattern (X_("*.tar.xz"));
330 archive_filter.set_name (_("Template archives"));
331 dialog.add_filter (archive_filter);
333 int result = dialog.run ();
335 if (result != RESPONSE_OK || !dialog.get_filename().length()) {
336 PBD::remove_directory (tmpdir);
340 string filename = dialog.get_filename ();
341 if (filename.compare (filename.size () - 7, 7, ".tar.xz")) {
342 filename += ".tar.xz";
345 if (g_file_test (filename.c_str(), G_FILE_TEST_EXISTS)) {
346 ArdourDialog dlg (_("File exists"), true);
347 Label msg (string_compose (_("The file %1 already exists."), filename));
348 dlg.get_vbox()->pack_start (msg);
350 dlg.add_button (_("Overwrite"), RESPONSE_ACCEPT);
351 dlg.add_button (_("Cancel"), RESPONSE_REJECT);
352 dlg.set_default_response (RESPONSE_REJECT);
356 if (result == RESPONSE_REJECT) {
357 PBD::remove_directory (tmpdir);
362 PBD::copy_recurse (templates_dir (), Glib::build_filename (tmpdir, Glib::path_get_basename (templates_dir ())));
364 vector<string> files;
365 PBD::find_files_matching_regex (files, tmpdir, string ("\\.template$"), /* recurse = */ true);
367 vector<string>::const_iterator it;
368 for (it = files.begin(); it != files.end(); ++it) {
369 const string bn = PBD::basename_nosuffix (*it);
370 const string old_path = Glib::build_filename (templates_dir (), bn);
371 const string new_path = Glib::build_filename ("$TEMPLATEDIR", bn);
374 if (!tree.read (*it)) {
377 if (adjust_xml_tree (tree, old_path, new_path)) {
382 find_files_matching_filter (files, tmpdir, accept_all_files, 0, false, true, true);
384 std::map<std::string, std::string> filemap;
385 for (it = files.begin(); it != files.end(); ++it) {
386 filemap[*it] = it->substr (tmpdir.size()+1, it->size() - tmpdir.size() - 1);
389 _current_action = _("Exporting templates");
391 PBD::FileArchive ar (filename);
392 PBD::ScopedConnectionList progress_connection;
393 ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
396 PBD::remove_directory (tmpdir);
400 TemplateManager::import_template_set ()
402 FileChooserDialog dialog (_("Import template archives"));
403 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
404 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
406 FileFilter archive_filter;
407 archive_filter.add_pattern (X_("*.tar.xz"));
408 archive_filter.set_name (_("Template archives"));
409 dialog.add_filter (archive_filter);
411 int result = dialog.run ();
413 if (result != RESPONSE_OK || !dialog.get_filename().length()) {
417 _current_action = _("Importing templates");
419 FileArchive ar (dialog.get_filename ());
420 PBD::ScopedConnectionList progress_connection;
421 ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
422 ar.inflate (user_config_directory ());
424 vector<string> files;
425 PBD::find_files_matching_regex (files, templates_dir (), string ("\\.template$"), /* recurse = */ true);
427 vector<string>::const_iterator it;
428 for (it = files.begin(); it != files.end(); ++it) {
429 const string bn = PBD::basename_nosuffix (*it);
430 const string old_path = Glib::build_filename ("$TEMPLATEDIR", bn);
431 const string new_path = Glib::build_filename (templates_dir (), bn);
434 if (!tree.read (*it)) {
437 if (adjust_xml_tree (tree, old_path, new_path)) {
443 TemplatesImported (); /* emit signal */
447 TemplateManager::adjust_plugin_paths (XMLNode* node, const string& name, const string& new_name) const
449 bool adjusted = false;
451 const XMLNodeList& procs = node->children (X_("Processor"));
452 XMLNodeConstIterator pit;
453 for (pit = procs.begin(); pit != procs.end(); ++pit) {
454 XMLNode* lv2_node = (*pit)->child (X_("lv2"));
460 if (!lv2_node->get_property (X_("template-dir"), template_dir)) {
464 const int suffix_pos = template_dir.size() - name.size();
465 if (suffix_pos < 0) {
466 cerr << "Template name\"" << name << "\" longer than template-dir \"" << template_dir << "\", WTH?" << endl;
470 if (template_dir.compare (suffix_pos, template_dir.size(), name)) {
471 cerr << "Template name \"" << name << "\" no suffix of template-dir \"" << template_dir << "\"" << endl;
475 const string new_template_dir = template_dir.substr (0, suffix_pos) + new_name;
476 lv2_node->set_property (X_("template-dir"), new_template_dir);
485 TemplateManager::update_progress_gui (float p)
488 _progress_bar.hide ();
492 _progress_bar.show ();
493 _progress_bar.set_text (_current_action);
494 _progress_bar.set_fraction (p);
498 void SessionTemplateManager::init ()
500 vector<TemplateInfo> templates;
501 find_session_templates (templates, /* read_xml = */ true);
502 setup_model (templates);
504 _progress_bar.hide ();
507 void RouteTemplateManager::init ()
509 vector<TemplateInfo> templates;
510 find_route_templates (templates);
511 setup_model (templates);
513 _progress_bar.hide ();
519 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name_)
521 const string old_path = item->get_value (_template_columns.path);
522 const string old_name = item->get_value (_template_columns.name);
523 const string new_name = string (new_name_);
525 const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
529 if (!tree.read(old_file_old_path)) {
530 error << string_compose (_("Could not parse template file \"%1\"."), old_file_old_path) << endmsg;
533 XMLNode* root = tree.root();
535 const XMLNode* const routes_node = root->child (X_("Routes"));
537 const XMLNodeList& routes = routes_node->children (X_("Route"));
538 XMLNodeConstIterator rit;
539 for (rit = routes.begin(); rit != routes.end(); ++rit) {
540 adjust_plugin_paths (*rit, old_name, new_name);
544 const string new_file_old_path = Glib::build_filename (old_path, new_name+".template");
546 tree.set_filename (new_file_old_path);
548 if (!tree.write ()) {
549 error << string_compose(_("Could not write to new template file \"%1\"."), new_file_old_path);
553 const string new_path = Glib::build_filename (user_template_directory (), new_name);
555 if (g_rename (old_path.c_str(), new_path.c_str()) != 0) {
556 error << string_compose (_("Could not rename template directory from \"%1\" to \"%2\": %3"),
557 old_path, new_path, strerror (errno)) << endmsg;
558 g_unlink (new_file_old_path.c_str());
562 const string old_file_new_path = Glib::build_filename (new_path, old_name+".template");
563 if (g_unlink (old_file_new_path.c_str())) {
564 error << string_compose (X_("Could not delete old template file \"%1\": %2"),
565 old_file_new_path, strerror (errno)) << endmsg;
568 item->set_value (_template_columns.name, new_name);
569 item->set_value (_template_columns.path, new_path);
573 SessionTemplateManager::delete_selected_template ()
575 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
579 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
585 PBD::remove_directory (it->get_value (_template_columns.path));
587 _template_model->erase (it);
588 row_selection_changed ();
592 SessionTemplateManager::templates_dir () const
594 return user_template_directory ();
599 SessionTemplateManager::template_file (const TreeModel::const_iterator& item) const
601 const string path = item->get_value (_template_columns.path);
602 const string name = item->get_value (_template_columns.name);
603 return Glib::build_filename (path, name+".template");
607 SessionTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
609 bool adjusted = false;
610 XMLNode* root = tree.root();
612 const XMLNode* const routes_node = root->child (X_("Routes"));
614 const XMLNodeList& routes = routes_node->children (X_("Route"));
615 XMLNodeConstIterator rit;
616 for (rit = routes.begin(); rit != routes.end(); ++rit) {
617 if (adjust_plugin_paths (*rit, old_name, new_name)) {
628 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
630 const string old_name = item->get_value (_template_columns.name);
631 const string old_filepath = item->get_value (_template_columns.path);
632 const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
635 if (!tree.read (old_filepath)) {
636 error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;
639 tree.root()->set_property (X_("name"), new_name);
640 tree.root()->children().front()->set_property (X_("name"), new_name);
642 const bool adjusted = adjust_plugin_paths (tree.root(), old_name, string (new_name));
644 const string old_state_dir = Glib::build_filename (user_route_template_directory(), old_name);
645 const string new_state_dir = Glib::build_filename (user_route_template_directory(), new_name);
648 if (g_file_test (old_state_dir.c_str(), G_FILE_TEST_EXISTS)) {
649 if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
650 error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
656 tree.set_filename (new_filepath);
658 if (!tree.write ()) {
659 error << string_compose(_("Could not write new template file \"%1\"."), new_filepath) << endmsg;
661 g_rename (new_state_dir.c_str(), old_state_dir.c_str());
666 if (g_unlink (old_filepath.c_str()) != 0) {
667 error << string_compose (_("Could not remove old template file \"%1\": %2"), old_filepath, strerror (errno)) << endmsg;
670 item->set_value (_template_columns.name, string (new_name));
671 item->set_value (_template_columns.path, new_filepath);
675 RouteTemplateManager::delete_selected_template ()
677 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
681 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
687 const string file_path = it->get_value (_template_columns.path);
689 if (g_unlink (file_path.c_str()) != 0) {
690 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
693 PBD::remove_directory (Glib::build_filename (user_route_template_directory (), it->get_value (_template_columns.name)));
695 _template_model->erase (it);
696 row_selection_changed ();
700 RouteTemplateManager::templates_dir () const
702 return user_route_template_directory ();
707 RouteTemplateManager::template_file (const TreeModel::const_iterator& item) const
709 return item->get_value (_template_columns.path);
713 RouteTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
715 return adjust_plugin_paths (tree.root(), old_name, string (new_name));