Also handle Route templates
[ardour.git] / gtk2_ardour / template_dialog.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3     Author: Johannes Mueller
4
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.
9
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.
14
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.
18
19 */
20
21 #include <glib/gstdio.h>
22
23 #include <gtkmm/notebook.h>
24 #include <gtkmm/scrolledwindow.h>
25 #include <gtkmm/treeiter.h>
26
27 #include "pbd/error.h"
28 #include "pbd/i18n.h"
29
30 #include "ardour/template_utils.h"
31
32 #include "template_dialog.h"
33
34 using namespace std;
35 using namespace Gtk;
36 using namespace PBD;
37 using namespace ARDOUR;
38
39 TemplateDialog::TemplateDialog ()
40         : ArdourDialog ("Manage Templates")
41 {
42         Notebook* nb = manage (new Notebook);
43
44         SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
45         session_tm->init ();
46         nb->append_page (*session_tm, _("Session Templates"));
47
48         RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
49         route_tm->init ();
50         nb->append_page (*route_tm, _("Track Templates"));
51
52         get_vbox()->pack_start (*nb);
53         add_button (_("Ok"), Gtk::RESPONSE_OK);
54
55         show_all_children ();
56 }
57
58 TemplateManager::TemplateManager ()
59         : HBox ()
60         , _remove_button (_("Remove"))
61         , _rename_button (_("Rename"))
62 {
63         _template_model = ListStore::create (_template_columns);
64         _template_treeview.set_model (_template_model);
65
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;
70
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));
75
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);
80
81
82         VBox* vb = manage (new VBox);
83         vb->set_spacing (4);
84         vb->pack_start (_rename_button, false, false);
85         vb->pack_start (_remove_button, false, false);
86
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));
91
92         set_spacing (6);
93         pack_start (*sw);
94         pack_start (*vb);
95
96         show_all_children ();
97 }
98
99 void
100 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
101 {
102         _template_model->clear ();
103
104         for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
105                 TreeModel::Row row;
106                 row = *(_template_model->append ());
107
108                 row[_template_columns.name] = it->name;
109                 row[_template_columns.path] = it->path;
110         }
111 }
112
113 void
114 TemplateManager::row_selection_changed ()
115 {
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 ();
119                 if (it) {
120                         has_selection = true;
121                 }
122         }
123
124         _rename_button.set_sensitive (has_selection);
125         _remove_button.set_sensitive (has_selection);
126 }
127
128 void
129 TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
130 {
131         if (it) {
132                 _validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
133         }
134 }
135
136 void
137 TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
138 {
139         const TreePath path (path_string);
140         TreeModel::iterator current = _template_model->get_iter (path);
141
142         if (current->get_value (_template_columns.name) == new_name) {
143                 return;
144         }
145
146         TreeModel::Children rows = _template_model->children ();
147
148         bool found = false;
149         for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
150                 if (it->get_value (_template_columns.name) == new_name) {
151                         found = true;
152                         break;
153                 }
154         }
155
156         if (found) {
157                 error << string_compose (_("Template of name \"%1\" already exists"), new_name) << endmsg;
158                 return;
159         }
160
161
162         rename_template (current, new_name);
163 }
164
165 void
166 TemplateManager::start_edit ()
167 {
168         TreeModel::Path path;
169         TreeViewColumn* col;
170         _template_treeview.get_cursor (path, col);
171         _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
172 }
173
174 bool
175 TemplateManager::key_event (GdkEventKey* ev)
176 {
177         if (ev->keyval == GDK_KEY_F2) {
178                 start_edit ();
179                 return true;
180         }
181         if (ev->keyval == GDK_KEY_Delete) {
182                 delete_selected_template ();
183                 return true;
184         }
185
186         return false;
187 }
188
189
190 void SessionTemplateManager::init ()
191 {
192         vector<TemplateInfo> templates;
193         find_session_templates (templates);
194         setup_model (templates);
195 }
196
197 void RouteTemplateManager::init ()
198 {
199         vector<TemplateInfo> templates;
200         find_route_templates (templates);
201         setup_model (templates);
202 }
203
204 void
205 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
206 {
207         const string path = item->get_value (_template_columns.path);
208         const string name = item->get_value (_template_columns.name);
209
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);
213
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;
216                 return;
217         }
218
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;
224                 }
225                 return;
226         }
227
228         item->set_value (_template_columns.name, string (new_name));
229         item->set_value (_template_columns.path, new_path);
230 }
231
232 void
233 SessionTemplateManager::delete_selected_template ()
234 {
235         if (_template_treeview.get_selection()->count_selected_rows() == 0) {
236                 return;
237         }
238
239         Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
240
241         if (!it) {
242                 return;
243         }
244
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");
248
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;
251                 return;
252         }
253
254         if (g_rmdir (path.c_str()) != 0) {
255                 error << string_compose(_("Could not delete template directory \"%1\": %2"), path, strerror (errno)) << endmsg;
256         }
257
258         _template_model->erase (it);
259         row_selection_changed ();
260 }
261
262 void
263 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
264 {
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");
267
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;
270                 return;
271         }
272
273         item->set_value (_template_columns.name, string (new_name));
274         item->set_value (_template_columns.path, new_filepath);
275 }
276
277 void
278 RouteTemplateManager::delete_selected_template ()
279 {
280         if (_template_treeview.get_selection()->count_selected_rows() == 0) {
281                 return;
282         }
283
284         Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
285
286         if (!it) {
287                 return;
288         }
289
290         const string file_path = it->get_value (_template_columns.path);
291
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;
294                 return;
295         }
296
297         _template_model->erase (it);
298         row_selection_changed ();
299 }