Prefer testing the session instead of using a global variable
[ardour.git] / gtk2_ardour / template_dialog.cc
index 49d6f44f3f8ead0f3d686dca9d3604a1b1358cad..e250791b62b6550e9c0861e632d740bcdfafd832 100644 (file)
@@ -45,6 +45,7 @@
 
 #include "gtkmm2ext/gui_thread.h"
 
+#include "ardour/filename_extensions.h"
 #include "ardour/filesystem_paths.h"
 #include "ardour/template_utils.h"
 
@@ -71,6 +72,9 @@ public:
 protected:
        TemplateManager ();
 
+       Gtk::TextView _description_editor;
+       Gtk::Button _save_desc;
+
        void setup_model (const std::vector<ARDOUR::TemplateInfo>& templates);
 
        void row_selection_changed ();
@@ -107,6 +111,8 @@ private:
 
        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 ();
@@ -123,8 +129,6 @@ private:
        Gtk::CellRendererText _validating_cellrenderer;
        Gtk::TreeView::Column _validated_column;
 
-       Gtk::TextView _description_editor;
-       Gtk::Button _save_desc;
        bool _desc_dirty;
 
        Gtk::Button _remove_button;
@@ -133,6 +137,8 @@ private:
        Gtk::Button _export_all_templates_button;
        Gtk::Button _import_template_set_button;
 
+       sigc::connection _cursor_changed_connection;
+
        void update_progress_gui (float p);
 };
 
@@ -144,6 +150,8 @@ public:
 
        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 ();
@@ -163,6 +171,8 @@ public:
 
        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 ();
@@ -187,7 +197,7 @@ 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();
 
@@ -199,8 +209,8 @@ TemplateDialog::TemplateDialog ()
 
        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 (boost::bind (&TemplateManager::handle_dirty_description, session_tm));
-       nb->signal_switch_page().connect (boost::bind (&TemplateManager::handle_dirty_description, route_tm));
+       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 ()
@@ -223,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);
@@ -303,7 +313,7 @@ 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 discription of template \"%1\" has been modfied but has not been saved yet.\n"
+               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 ();
@@ -318,24 +328,28 @@ TemplateManager::handle_dirty_description ()
                } else {
                        _description_editor.get_buffer()->set_text (_current_selection->get_value (_template_columns.description));
                }
-
-               _desc_dirty = false;
        }
 }
 
 void
 TemplateManager::row_selection_changed ()
 {
-       handle_dirty_description ();
+       if (_current_selection) {
+               handle_dirty_description ();
+       } else {
+               _description_editor.get_buffer()->set_text ("");
+       }
 
        _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);
        }
 
+       _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);
 }
@@ -383,7 +397,9 @@ 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
@@ -398,7 +414,12 @@ TemplateManager::save_template_desc ()
 {
        const string file_path = template_file (_current_selection);
 
-       const string desc_txt = _description_editor.get_buffer()->get_text ();
+       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;
@@ -408,13 +429,14 @@ TemplateManager::save_template_desc ()
                return;
        }
 
-       tree.root()->remove_nodes (X_("description"));
-       XMLNode* desc = new XMLNode (X_("description"));
+       tree.root()->remove_nodes_and_delete (X_("description"));
 
-       XMLNode* dn = new XMLNode (X_("content"), desc_txt);
-       desc->add_child_nocopy (*dn);
-
-       tree.root()->add_child_nocopy (*desc);
+       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;
@@ -440,13 +462,14 @@ TemplateManager::key_event (GdkEventKey* ev)
        return false;
 }
 
-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));
 }
@@ -467,13 +490,13 @@ TemplateManager::export_all_templates ()
        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);
 
@@ -485,9 +508,7 @@ TemplateManager::export_all_templates ()
        }
 
        string filename = dialog.get_filename ();
-       if (filename.compare (filename.size () - 7, 7, ".tar.xz")) {
-               filename += ".tar.xz";
-       }
+       filename += ARDOUR::template_archive_suffix;
 
        if (g_file_test (filename.c_str(), G_FILE_TEST_EXISTS)) {
                ArdourDialog dlg (_("File exists"), true);
@@ -551,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);
 
@@ -641,23 +662,40 @@ TemplateManager::update_progress_gui (float p)
        _progress_bar.set_fraction (p);
 }
 
-
-void SessionTemplateManager::init ()
+void
+SessionTemplateManager::init ()
 {
        vector<TemplateInfo> templates;
-       find_session_templates (templates, /* read_xml = */ true);
+       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>
@@ -669,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;
@@ -726,6 +768,7 @@ SessionTemplateManager::delete_selected_template ()
        PBD::remove_directory (_current_selection->get_value (_template_columns.path));
 
        _template_model->erase (_current_selection);
+       _current_selection = TreeIter ();
        row_selection_changed ();
 }
 
@@ -772,6 +815,10 @@ 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;