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.
21 #include <glib/gstdio.h>
23 #include <gtkmm/notebook.h>
24 #include <gtkmm/scrolledwindow.h>
25 #include <gtkmm/treeiter.h>
27 #include "pbd/error.h"
30 #include "ardour/template_utils.h"
32 #include "template_dialog.h"
37 using namespace ARDOUR;
39 TemplateDialog::TemplateDialog ()
40 : ArdourDialog ("Manage Templates")
42 Notebook* nb = manage (new Notebook);
44 SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
46 nb->append_page (*session_tm, _("Session Templates"));
48 RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
50 nb->append_page (*route_tm, _("Track Templates"));
52 get_vbox()->pack_start (*nb);
53 add_button (_("Ok"), Gtk::RESPONSE_OK);
58 TemplateManager::TemplateManager ()
60 , _remove_button (_("Remove"))
61 , _rename_button (_("Rename"))
63 _template_model = ListStore::create (_template_columns);
64 _template_treeview.set_model (_template_model);
66 _validated_column.set_title (_("Template Name"));
67 _validated_column.pack_start (_validating_cellrenderer);
68 _template_treeview.append_column (_validated_column);
69 _validating_cellrenderer.property_editable() = true;
71 _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
72 _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
73 _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
74 _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
76 ScrolledWindow* sw = manage (new ScrolledWindow);
77 sw->property_hscrollbar_policy() = POLICY_AUTOMATIC;
78 sw->add (_template_treeview);
79 sw->set_size_request (300, 200);
82 VBox* vb = manage (new VBox);
84 vb->pack_start (_rename_button, false, false);
85 vb->pack_start (_remove_button, false, false);
87 _rename_button.set_sensitive (false);
88 _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
89 _remove_button.set_sensitive (false);
90 _remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::delete_selected_template));
100 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
102 _template_model->clear ();
104 for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
106 row = *(_template_model->append ());
108 row[_template_columns.name] = it->name;
109 row[_template_columns.path] = it->path;
114 TemplateManager::row_selection_changed ()
116 bool has_selection = false;
117 if (_template_treeview.get_selection()->count_selected_rows () != 0) {
118 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
120 has_selection = true;
124 _rename_button.set_sensitive (has_selection);
125 _remove_button.set_sensitive (has_selection);
129 TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
132 _validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
137 TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
139 const TreePath path (path_string);
140 TreeModel::iterator current = _template_model->get_iter (path);
142 if (current->get_value (_template_columns.name) == new_name) {
146 TreeModel::Children rows = _template_model->children ();
149 for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
150 if (it->get_value (_template_columns.name) == new_name) {
157 error << string_compose (_("Template of name \"%1\" already exists"), new_name) << endmsg;
162 rename_template (current, new_name);
166 TemplateManager::start_edit ()
168 TreeModel::Path path;
170 _template_treeview.get_cursor (path, col);
171 _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
175 TemplateManager::key_event (GdkEventKey* ev)
177 if (ev->keyval == GDK_KEY_F2) {
181 if (ev->keyval == GDK_KEY_Delete) {
182 delete_selected_template ();
190 void SessionTemplateManager::init ()
192 vector<TemplateInfo> templates;
193 find_session_templates (templates);
194 setup_model (templates);
197 void RouteTemplateManager::init ()
199 vector<TemplateInfo> templates;
200 find_route_templates (templates);
201 setup_model (templates);
205 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
207 const string path = item->get_value (_template_columns.path);
208 const string name = item->get_value (_template_columns.name);
210 const string old_filepath = Glib::build_filename (path, name+".template");
211 const string new_filepath = Glib::build_filename (path, new_name+".template");
212 const string new_path = Glib::build_filename (user_template_directory (), new_name);
214 if (g_rename (old_filepath.c_str(), new_filepath.c_str()) != 0) {
215 error << string_compose (_("Renaming of the template file failed: %1"), strerror (errno)) << endmsg;
219 if (g_rename (path.c_str(), new_path.c_str()) != 0) {
220 error << string_compose (_("Renaming of the template directory failed: %1"), strerror (errno)) << endmsg;
221 if (g_rename (new_filepath.c_str(), old_filepath.c_str()) != 0) {
222 error << string_compose (_("Couldn't even undo renaming of template file: %1. Please examine the situation using filemanager or terminal."),
223 strerror (errno)) << endmsg;
228 item->set_value (_template_columns.name, string (new_name));
229 item->set_value (_template_columns.path, new_path);
233 SessionTemplateManager::delete_selected_template ()
235 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
239 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
245 const string path = it->get_value (_template_columns.path);
246 const string name = it->get_value (_template_columns.name);
247 const string file_path = Glib::build_filename (path, name+".template");
249 if (g_unlink (file_path.c_str()) != 0) {
250 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
254 if (g_rmdir (path.c_str()) != 0) {
255 error << string_compose(_("Could not delete template directory \"%1\": %2"), path, strerror (errno)) << endmsg;
258 _template_model->erase (it);
259 row_selection_changed ();
263 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
265 const string old_filepath = item->get_value (_template_columns.path);
266 const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
268 if (g_rename (old_filepath.c_str(), new_filepath.c_str()) != 0) {
269 error << string_compose (_("Renaming of the template file failed: %1"), strerror (errno)) << endmsg;
273 item->set_value (_template_columns.name, string (new_name));
274 item->set_value (_template_columns.path, new_filepath);
278 RouteTemplateManager::delete_selected_template ()
280 if (_template_treeview.get_selection()->count_selected_rows() == 0) {
284 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
290 const string file_path = it->get_value (_template_columns.path);
292 if (g_unlink (file_path.c_str()) != 0) {
293 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
297 _template_model->erase (it);
298 row_selection_changed ();