Allow strips to add or remove personal sends
[ardour.git] / gtk2_ardour / template_dialog.cc
index 1346f7dd8b60d19acf735e7d9272f0861b587b34..e250791b62b6550e9c0861e632d740bcdfafd832 100644 (file)
 */
 
 #include <map>
+#include <vector>
+#include <cerrno>
 
 #include <glib/gstdio.h>
 
 #include <gtkmm/filechooserdialog.h>
+#include <gtkmm/frame.h>
+#include <gtkmm/liststore.h>
 #include <gtkmm/notebook.h>
+#include <gtkmm/progressbar.h>
 #include <gtkmm/separator.h>
 #include <gtkmm/scrolledwindow.h>
 #include <gtkmm/stock.h>
+#include <gtkmm/textview.h>
 #include <gtkmm/treeiter.h>
+#include <gtkmm/treeview.h>
 
 #include "pbd/basename.h"
 #include "pbd/error.h"
 #include "pbd/i18n.h"
 #include "pbd/xml++.h"
 
+#include "gtkmm2ext/gui_thread.h"
+
+#include "ardour/filename_extensions.h"
+#include "ardour/filesystem_paths.h"
 #include "ardour/template_utils.h"
 
+#include "progress_reporter.h"
+
 #include "template_dialog.h"
 
 using namespace std;
@@ -45,6 +58,132 @@ using namespace Gtk;
 using namespace PBD;
 using namespace ARDOUR;
 
+class TemplateManager : public Gtk::HBox,
+                       public ProgressReporter
+{
+public:
+       virtual ~TemplateManager () {}
+
+       virtual void init () = 0;
+       void handle_dirty_description ();
+
+       PBD::Signal0<void> TemplatesImported;
+
+protected:
+       TemplateManager ();
+
+       Gtk::TextView _description_editor;
+       Gtk::Button _save_desc;
+
+       void setup_model (const std::vector<ARDOUR::TemplateInfo>& templates);
+
+       void row_selection_changed ();
+
+       virtual void delete_selected_template () = 0;
+       bool adjust_plugin_paths (XMLNode* node, const std::string& name, const std::string& new_name) const;
+
+       struct SessionTemplateColumns : public Gtk::TreeModel::ColumnRecord {
+               SessionTemplateColumns () {
+                       add (name);
+                       add (path);
+                       add (description);
+               }
+
+               Gtk::TreeModelColumn<std::string> name;
+               Gtk::TreeModelColumn<std::string> path;
+               Gtk::TreeModelColumn<std::string> description;
+       };
+
+       Glib::RefPtr<Gtk::ListStore>  _template_model;
+       SessionTemplateColumns _template_columns;
+
+       Gtk::TreeModel::const_iterator _current_selection;
+
+       Gtk::ProgressBar _progress_bar;
+       std::string _current_action;
+
+private:
+       void render_template_names (Gtk::CellRenderer* rnd, const Gtk::TreeModel::iterator& it);
+       void validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name);
+       void start_edit ();
+
+       void set_desc_dirty ();
+
+       bool key_event (GdkEventKey* ev);
+
+       virtual void get_templates (vector<TemplateInfo>& templates) const = 0;
+
+       virtual void rename_template (Gtk::TreeModel::iterator& item, const Glib::ustring& new_name) = 0;
+
+       virtual void save_template_desc ();
+
+       void export_all_templates ();
+       void import_template_set ();
+
+       virtual std::string templates_dir () const = 0;
+       virtual std::string template_file (const Gtk::TreeModel::const_iterator& item) const = 0;
+
+       virtual bool adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const = 0;
+
+       Gtk::TreeView _template_treeview;
+       Gtk::CellRendererText _validating_cellrenderer;
+       Gtk::TreeView::Column _validated_column;
+
+       bool _desc_dirty;
+
+       Gtk::Button _remove_button;
+       Gtk::Button _rename_button;
+
+       Gtk::Button _export_all_templates_button;
+       Gtk::Button _import_template_set_button;
+
+       sigc::connection _cursor_changed_connection;
+
+       void update_progress_gui (float p);
+};
+
+class SessionTemplateManager : public TemplateManager
+{
+public:
+       SessionTemplateManager () : TemplateManager () {}
+       ~SessionTemplateManager () {}
+
+       void init ();
+
+       void get_templates (vector<TemplateInfo>& templates) const;
+
+private:
+       void rename_template (Gtk::TreeModel::iterator& item, const Glib::ustring& new_name);
+       void delete_selected_template ();
+
+       std::string templates_dir () const;
+       std::string template_file (const Gtk::TreeModel::const_iterator& item) const;
+
+       bool adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const;
+};
+
+
+class RouteTemplateManager : public TemplateManager
+{
+public:
+       RouteTemplateManager () : TemplateManager () {}
+       ~RouteTemplateManager () {}
+
+       void init ();
+
+       void get_templates (vector<TemplateInfo>& templates) const;
+
+private:
+       void rename_template (Gtk::TreeModel::iterator& item, const Glib::ustring& new_name);
+       void delete_selected_template ();
+
+       std::string templates_dir () const;
+       std::string template_file (const Gtk::TreeModel::const_iterator& item) const;
+
+       bool adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const;
+};
+
+
 
 TemplateDialog::TemplateDialog ()
        : ArdourDialog ("Manage Templates")
@@ -58,17 +197,27 @@ TemplateDialog::TemplateDialog ()
        nb->append_page (*route_tm, _("Track Templates"));
 
        get_vbox()->pack_start (*nb);
-       add_button (_("Ok"), Gtk::RESPONSE_OK);
+       add_button (_("Done"), Gtk::RESPONSE_OK);
 
        get_vbox()->show_all();
 
        session_tm->init ();
        route_tm->init ();
+
+       session_tm->TemplatesImported.connect (*this, invalidator (*this), boost::bind (&RouteTemplateManager::init, route_tm), gui_context ());
+       route_tm->TemplatesImported.connect (*this, invalidator (*this), boost::bind (&SessionTemplateManager::init, session_tm), gui_context ());
+
+       signal_hide().connect (sigc::mem_fun (session_tm, &TemplateManager::handle_dirty_description));
+       signal_hide().connect (sigc::mem_fun (route_tm, &TemplateManager::handle_dirty_description));
+       nb->signal_switch_page().connect (sigc::hide (sigc::hide (sigc::mem_fun (session_tm, &TemplateManager::handle_dirty_description))));
+       nb->signal_switch_page().connect (sigc::hide (sigc::hide (sigc::mem_fun (route_tm, &TemplateManager::handle_dirty_description))));
 }
 
 TemplateManager::TemplateManager ()
        : HBox ()
        , ProgressReporter ()
+       , _save_desc (_("Save Description"))
+       , _desc_dirty (false)
        , _remove_button (_("Remove"))
        , _rename_button (_("Rename"))
        , _export_all_templates_button (_("Export all"))
@@ -84,7 +233,7 @@ TemplateManager::TemplateManager ()
 
        _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
        _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
-       _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
+       _cursor_changed_connection = _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
        _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
 
        ScrolledWindow* sw = manage (new ScrolledWindow);
@@ -92,11 +241,11 @@ TemplateManager::TemplateManager ()
        sw->add (_template_treeview);
        sw->set_size_request (300, 200);
 
-
        VBox* vb_btns = manage (new VBox);
        vb_btns->set_spacing (4);
        vb_btns->pack_start (_rename_button, false, false);
        vb_btns->pack_start (_remove_button, false, false);
+       vb_btns->pack_start (_save_desc, false, false);
 
        _rename_button.set_sensitive (false);
        _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
@@ -108,7 +257,7 @@ TemplateManager::TemplateManager ()
        vb_btns->pack_start (_export_all_templates_button, false, false);
        vb_btns->pack_start (_import_template_set_button, false, false);
 
-       _export_all_templates_button.set_sensitive (true);
+       _export_all_templates_button.set_sensitive (false);
        _export_all_templates_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::export_all_templates));
 
        _import_template_set_button.set_sensitive (true);
@@ -120,7 +269,21 @@ TemplateManager::TemplateManager ()
        vb->pack_start (*sw);
        vb->pack_start (_progress_bar);
 
+       Frame* desc_frame = manage (new Frame (_("Description")));
+
+       _description_editor.set_wrap_mode (Gtk::WRAP_WORD);
+       _description_editor.set_size_request (300,400);
+       _description_editor.set_border_width (6);
+
+       _save_desc.set_sensitive (false);
+       _save_desc.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::save_template_desc));
+
+       _description_editor.get_buffer()->signal_changed().connect (sigc::mem_fun (*this, &TemplateManager::set_desc_dirty));
+
+       desc_frame->add (_description_editor);
+
        pack_start (*vb);
+       pack_start (*desc_frame);
        pack_start (*vb_btns);
 
        show_all_children ();
@@ -138,22 +301,57 @@ TemplateManager::setup_model (const vector<TemplateInfo>& templates)
 
                row[_template_columns.name] = it->name;
                row[_template_columns.path] = it->path;
+               row[_template_columns.description] = it->description;
+       }
+
+       _export_all_templates_button.set_sensitive (!templates.empty ());
+}
+
+void
+TemplateManager::handle_dirty_description ()
+{
+       if (_desc_dirty && _current_selection) {
+               ArdourDialog dlg (_("Description not saved"), true);
+               const string name = _current_selection->get_value (_template_columns.name);
+               Label msg (string_compose (_("The description of template \"%1\" has been modified but has not been saved yet.\n"
+                                            "Do you want to save it?"), name));
+               dlg.get_vbox()->pack_start (msg);
+               msg.show ();
+               dlg.add_button (_("Save"), RESPONSE_ACCEPT);
+               dlg.add_button (_("Discard"), RESPONSE_REJECT);
+               dlg.set_default_response (RESPONSE_REJECT);
+
+               int response = dlg.run ();
+
+               if (response == RESPONSE_ACCEPT) {
+                       save_template_desc ();
+               } else {
+                       _description_editor.get_buffer()->set_text (_current_selection->get_value (_template_columns.description));
+               }
        }
 }
 
 void
 TemplateManager::row_selection_changed ()
 {
-       bool has_selection = false;
-       if (_template_treeview.get_selection()->count_selected_rows () != 0) {
-               Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
-               if (it) {
-                       has_selection = true;
-               }
+       if (_current_selection) {
+               handle_dirty_description ();
+       } else {
+               _description_editor.get_buffer()->set_text ("");
        }
 
-       _rename_button.set_sensitive (has_selection);
-       _remove_button.set_sensitive (has_selection);
+       _current_selection = _template_treeview.get_selection()->get_selected ();
+       if (_current_selection) {
+               const string desc = _current_selection->get_value (_template_columns.description);
+               _description_editor.get_buffer()->set_text (desc);
+       }
+
+       _desc_dirty = false;
+       _save_desc.set_sensitive (false);
+
+       _description_editor.set_sensitive (_current_selection);
+       _rename_button.set_sensitive (_current_selection);
+       _remove_button.set_sensitive (_current_selection);
 }
 
 void
@@ -199,7 +397,54 @@ TemplateManager::start_edit ()
        TreeModel::Path path;
        TreeViewColumn* col;
        _template_treeview.get_cursor (path, col);
+       _cursor_changed_connection.block ();
        _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
+       _cursor_changed_connection.unblock ();
+}
+
+void
+TemplateManager::set_desc_dirty ()
+{
+       _desc_dirty = true;
+       _save_desc.set_sensitive (true);
+}
+
+void
+TemplateManager::save_template_desc ()
+{
+       const string file_path = template_file (_current_selection);
+
+       string desc_txt = _description_editor.get_buffer()->get_text ();
+       string::reverse_iterator wss = desc_txt.rbegin();
+       while (wss != desc_txt.rend() && isspace (*wss)) {
+               desc_txt.erase (--(wss++).base());
+       }
+
+       _current_selection->set_value (_template_columns.description, desc_txt);
+
+       XMLTree tree;
+
+       if (!tree.read(file_path)) {
+               error << string_compose (_("Could not parse template file \"%1\"."), file_path) << endmsg;
+               return;
+       }
+
+       tree.root()->remove_nodes_and_delete (X_("description"));
+
+       if (!desc_txt.empty ()) {
+               XMLNode* desc = new XMLNode (X_("description"));
+               XMLNode* dn = new XMLNode (X_("content"), desc_txt);
+               desc->add_child_nocopy (*dn);
+               tree.root()->add_child_nocopy (*desc);
+       }
+
+       if (!tree.write ()) {
+               error << string_compose(X_("Could not write to template file \"%1\"."), file_path) << endmsg;
+               return;
+       }
+
+       _save_desc.set_sensitive (false);
+       _desc_dirty = false;
 }
 
 bool
@@ -217,40 +462,14 @@ TemplateManager::key_event (GdkEventKey* ev)
        return false;
 }
 
-static string get_tmp_dir ()
-{
-       #ifdef PLATFORM_WINDOWS
-       char tmp[256] = "C:\\TEMP\\";
-       GetTempPath (sizeof (tmp), tmp);
-#else
-       char const* tmp = getenv("TMPDIR");
-       if (!tmp) {
-               tmp = "/tmp/";
-       }
-#endif
-       if ((strlen (tmp) + 21) > 1024) {
-               return string ();
-       }
-
-       char tmptpl[1024];
-       strcpy (tmptpl, tmp);
-       strcat (tmptpl, "ardour_template-XXXXXX");
-       char*  tmpdir = g_mkdtemp (tmptpl);
-
-       if (!tmpdir) {
-               return string ();
-       }
-
-       return string (tmpdir);
-}
-
-static
-bool accept_all_files (string const &, void *)
+static bool
+accept_all_files (string const &, void *)
 {
        return true;
 }
 
-static void _set_progress (Progress* p, size_t n, size_t t)
+static void
+_set_progress (Progress* p, size_t n, size_t t)
 {
        p->set_progress (float (n) / float(t));
 }
@@ -259,26 +478,56 @@ static void _set_progress (Progress* p, size_t n, size_t t)
 void
 TemplateManager::export_all_templates ()
 {
+       GError* err = NULL;
+       char* td = g_dir_make_tmp ("ardour-templates-XXXXXX", &err);
+
+       if (!td) {
+               error << string_compose(_("Could not make tmpdir: %1"), err->message) << endmsg;
+               return;
+       }
+       const string tmpdir (td);
+       g_free (td);
+       g_clear_error (&err);
+
        FileChooserDialog dialog(_("Save Exported Template Archive"), FILE_CHOOSER_ACTION_SAVE);
-       dialog.set_filename (X_("templates.tar.xz"));
+       dialog.set_filename (X_("templates"));
 
        dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
        dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
 
        FileFilter archive_filter;
-       archive_filter.add_pattern (X_("*.tar.xz"));
+       archive_filter.add_pattern (string_compose(X_("*%1"), ARDOUR::template_archive_suffix));
        archive_filter.set_name (_("Template archives"));
        dialog.add_filter (archive_filter);
 
        int result = dialog.run ();
 
        if (result != RESPONSE_OK || !dialog.get_filename().length()) {
+               PBD::remove_directory (tmpdir);
                return;
        }
 
-       const string tmpdir = get_tmp_dir();
+       string filename = dialog.get_filename ();
+       filename += ARDOUR::template_archive_suffix;
+
+       if (g_file_test (filename.c_str(), G_FILE_TEST_EXISTS)) {
+               ArdourDialog dlg (_("File exists"), true);
+               Label msg (string_compose (_("The file %1 already exists."), filename));
+               dlg.get_vbox()->pack_start (msg);
+               msg.show ();
+               dlg.add_button (_("Overwrite"), RESPONSE_ACCEPT);
+               dlg.add_button (_("Cancel"), RESPONSE_REJECT);
+               dlg.set_default_response (RESPONSE_REJECT);
 
-       PBD::copy_recurse (templates_dir (), tmpdir);
+               result = dlg.run ();
+
+               if (result == RESPONSE_REJECT) {
+                       PBD::remove_directory (tmpdir);
+                       return;
+               }
+       }
+
+       PBD::copy_recurse (templates_dir (), Glib::build_filename (tmpdir, Glib::path_get_basename (templates_dir ())));
 
        vector<string> files;
        PBD::find_files_matching_regex (files, tmpdir, string ("\\.template$"), /* recurse = */ true);
@@ -307,7 +556,7 @@ TemplateManager::export_all_templates ()
 
        _current_action = _("Exporting templates");
 
-       PBD::FileArchive ar (dialog.get_filename());
+       PBD::FileArchive ar (filename);
        PBD::ScopedConnectionList progress_connection;
        ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
        ar.create (filemap);
@@ -323,7 +572,7 @@ TemplateManager::import_template_set ()
        dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
 
        FileFilter archive_filter;
-       archive_filter.add_pattern (X_("*.tar.xz"));
+       archive_filter.add_pattern (string_compose(X_("*%1"), ARDOUR::template_archive_suffix));
        archive_filter.set_name (_("Template archives"));
        dialog.add_filter (archive_filter);
 
@@ -333,17 +582,12 @@ TemplateManager::import_template_set ()
                return;
        }
 
-       if (!g_file_test (templates_dir().c_str(), G_FILE_TEST_IS_DIR)) {
-               cout << "making " << templates_dir() << endl;
-               g_mkdir (templates_dir().c_str(), 0755);
-       }
-
        _current_action = _("Importing templates");
 
        FileArchive ar (dialog.get_filename ());
        PBD::ScopedConnectionList progress_connection;
        ar.progress.connect_same_thread (progress_connection, boost::bind (&_set_progress, this, _1, _2));
-       ar.inflate (templates_dir());
+       ar.inflate (user_config_directory ());
 
        vector<string> files;
        PBD::find_files_matching_regex (files, templates_dir (), string ("\\.template$"), /* recurse = */ true);
@@ -364,6 +608,7 @@ TemplateManager::import_template_set ()
        }
 
        init ();
+       TemplatesImported (); /* emit signal */
 }
 
 bool
@@ -417,25 +662,43 @@ TemplateManager::update_progress_gui (float p)
        _progress_bar.set_fraction (p);
 }
 
-
-void SessionTemplateManager::init ()
+void
+SessionTemplateManager::init ()
 {
        vector<TemplateInfo> templates;
-       find_session_templates (templates);
+       get_templates (templates);
        setup_model (templates);
 
        _progress_bar.hide ();
+       _description_editor.set_sensitive (false);
+       _save_desc.set_sensitive (false);
 }
 
-void RouteTemplateManager::init ()
+void
+RouteTemplateManager::init ()
 {
        vector<TemplateInfo> templates;
-       find_route_templates (templates);
+       get_templates (templates);
        setup_model (templates);
 
        _progress_bar.hide ();
+       _description_editor.set_sensitive (false);
+       _save_desc.set_sensitive (false);
 }
 
+void
+SessionTemplateManager::get_templates (vector<TemplateInfo>& templates) const
+{
+       find_session_templates (templates, /* read_xml = */ true);
+}
+
+void
+RouteTemplateManager::get_templates (vector<TemplateInfo>& templates) const
+{
+       find_route_templates (templates);
+}
+
+#include <cerrno>
 
 void
 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name_)
@@ -444,6 +707,10 @@ SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::
        const string old_name = item->get_value (_template_columns.name);
        const string new_name = string (new_name_);
 
+       if (old_name == new_name) {
+               return;
+       }
+
        const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
 
        XMLTree tree;
@@ -494,29 +761,32 @@ SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::
 void
 SessionTemplateManager::delete_selected_template ()
 {
-       if (_template_treeview.get_selection()->count_selected_rows() == 0) {
+       if (!_current_selection) {
                return;
        }
 
-       Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
+       PBD::remove_directory (_current_selection->get_value (_template_columns.path));
 
-       if (!it) {
-               return;
-       }
-
-       PBD::remove_directory (it->get_value (_template_columns.path));
-
-       _template_model->erase (it);
+       _template_model->erase (_current_selection);
+       _current_selection = TreeIter ();
        row_selection_changed ();
 }
 
-
 string
 SessionTemplateManager::templates_dir () const
 {
        return user_template_directory ();
 }
 
+
+string
+SessionTemplateManager::template_file (const TreeModel::const_iterator& item) const
+{
+       const string path = item->get_value (_template_columns.path);
+       const string name = item->get_value (_template_columns.name);
+       return Glib::build_filename (path, name+".template");
+}
+
 bool
 SessionTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
 {
@@ -545,11 +815,16 @@ RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::us
        const string old_filepath = item->get_value (_template_columns.path);
        const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
 
+       if (old_name == new_name) {
+               return;
+       }
+
        XMLTree tree;
        if (!tree.read (old_filepath)) {
                error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;
                return;
        }
+       tree.root()->set_property (X_("name"), new_name);
        tree.root()->children().front()->set_property (X_("name"), new_name);
 
        const bool adjusted = adjust_plugin_paths (tree.root(), old_name, string (new_name));
@@ -558,9 +833,11 @@ RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::us
        const string new_state_dir = Glib::build_filename (user_route_template_directory(), new_name);
 
        if (adjusted) {
-               if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
-                       error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
-                       return;
+               if (g_file_test (old_state_dir.c_str(), G_FILE_TEST_EXISTS)) {
+                       if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
+                               error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
+                               return;
+                       }
                }
        }
 
@@ -585,25 +862,20 @@ RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::us
 void
 RouteTemplateManager::delete_selected_template ()
 {
-       if (_template_treeview.get_selection()->count_selected_rows() == 0) {
+       if (!_current_selection) {
                return;
        }
 
-       Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
-
-       if (!it) {
-               return;
-       }
-
-       const string file_path = it->get_value (_template_columns.path);
+       const string file_path = _current_selection->get_value (_template_columns.path);
 
        if (g_unlink (file_path.c_str()) != 0) {
                error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
                return;
        }
-       PBD::remove_directory (Glib::build_filename (user_route_template_directory (), it->get_value (_template_columns.name)));
+       PBD::remove_directory (Glib::build_filename (user_route_template_directory (),
+                                                    _current_selection->get_value (_template_columns.name)));
 
-       _template_model->erase (it);
+       _template_model->erase (_current_selection);
        row_selection_changed ();
 }
 
@@ -613,6 +885,13 @@ RouteTemplateManager::templates_dir () const
        return user_route_template_directory ();
 }
 
+
+string
+RouteTemplateManager::template_file (const TreeModel::const_iterator& item) const
+{
+       return item->get_value (_template_columns.path);
+}
+
 bool
 RouteTemplateManager::adjust_xml_tree (XMLTree& tree, const std::string& old_name, const std::string& new_name) const
 {