Adjust template names inside template files
[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 #include "pbd/xml++.h"
30
31 #include "ardour/template_utils.h"
32
33 #include "template_dialog.h"
34
35 using namespace std;
36 using namespace Gtk;
37 using namespace PBD;
38 using namespace ARDOUR;
39
40 TemplateDialog::TemplateDialog ()
41         : ArdourDialog ("Manage Templates")
42 {
43         Notebook* nb = manage (new Notebook);
44
45         SessionTemplateManager* session_tm = manage (new SessionTemplateManager);
46         session_tm->init ();
47         nb->append_page (*session_tm, _("Session Templates"));
48
49         RouteTemplateManager* route_tm = manage (new RouteTemplateManager);
50         route_tm->init ();
51         nb->append_page (*route_tm, _("Track Templates"));
52
53         get_vbox()->pack_start (*nb);
54         add_button (_("Ok"), Gtk::RESPONSE_OK);
55
56         show_all_children ();
57 }
58
59 TemplateManager::TemplateManager ()
60         : HBox ()
61         , _remove_button (_("Remove"))
62         , _rename_button (_("Rename"))
63 {
64         _template_model = ListStore::create (_template_columns);
65         _template_treeview.set_model (_template_model);
66
67         _validated_column.set_title (_("Template Name"));
68         _validated_column.pack_start (_validating_cellrenderer);
69         _template_treeview.append_column (_validated_column);
70         _validating_cellrenderer.property_editable() = true;
71
72         _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
73         _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
74         _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
75         _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
76
77         ScrolledWindow* sw = manage (new ScrolledWindow);
78         sw->property_hscrollbar_policy() = POLICY_AUTOMATIC;
79         sw->add (_template_treeview);
80         sw->set_size_request (300, 200);
81
82
83         VBox* vb = manage (new VBox);
84         vb->set_spacing (4);
85         vb->pack_start (_rename_button, false, false);
86         vb->pack_start (_remove_button, false, false);
87
88         _rename_button.set_sensitive (false);
89         _rename_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::start_edit));
90         _remove_button.set_sensitive (false);
91         _remove_button.signal_clicked().connect (sigc::mem_fun (*this, &TemplateManager::delete_selected_template));
92
93         set_spacing (6);
94         pack_start (*sw);
95         pack_start (*vb);
96
97         show_all_children ();
98 }
99
100 void
101 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
102 {
103         _template_model->clear ();
104
105         for (vector<TemplateInfo>::const_iterator it = templates.begin(); it != templates.end(); ++it) {
106                 TreeModel::Row row;
107                 row = *(_template_model->append ());
108
109                 row[_template_columns.name] = it->name;
110                 row[_template_columns.path] = it->path;
111         }
112 }
113
114 void
115 TemplateManager::row_selection_changed ()
116 {
117         bool has_selection = false;
118         if (_template_treeview.get_selection()->count_selected_rows () != 0) {
119                 Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected ();
120                 if (it) {
121                         has_selection = true;
122                 }
123         }
124
125         _rename_button.set_sensitive (has_selection);
126         _remove_button.set_sensitive (has_selection);
127 }
128
129 void
130 TemplateManager::render_template_names (Gtk::CellRenderer*, const Gtk::TreeModel::iterator& it)
131 {
132         if (it) {
133                 _validating_cellrenderer.property_text () = it->get_value (_template_columns.name);
134         }
135 }
136
137 void
138 TemplateManager::validate_edit (const Glib::ustring& path_string, const Glib::ustring& new_name)
139 {
140         const TreePath path (path_string);
141         TreeModel::iterator current = _template_model->get_iter (path);
142
143         if (current->get_value (_template_columns.name) == new_name) {
144                 return;
145         }
146
147         TreeModel::Children rows = _template_model->children ();
148
149         bool found = false;
150         for (TreeModel::Children::const_iterator it = rows.begin(); it != rows.end(); ++it) {
151                 if (it->get_value (_template_columns.name) == new_name) {
152                         found = true;
153                         break;
154                 }
155         }
156
157         if (found) {
158                 error << string_compose (_("Template of name \"%1\" already exists"), new_name) << endmsg;
159                 return;
160         }
161
162
163         rename_template (current, new_name);
164 }
165
166 void
167 TemplateManager::start_edit ()
168 {
169         TreeModel::Path path;
170         TreeViewColumn* col;
171         _template_treeview.get_cursor (path, col);
172         _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
173 }
174
175 bool
176 TemplateManager::key_event (GdkEventKey* ev)
177 {
178         if (ev->keyval == GDK_KEY_F2) {
179                 start_edit ();
180                 return true;
181         }
182         if (ev->keyval == GDK_KEY_Delete) {
183                 delete_selected_template ();
184                 return true;
185         }
186
187         return false;
188 }
189
190 bool
191 TemplateManager::adjust_plugin_paths (XMLNode* node, const string& name, const string& new_name) const
192 {
193         bool adjusted = false;
194
195         const XMLNodeList& procs = node->children (X_("Processor"));
196         XMLNodeConstIterator pit;
197         for (pit = procs.begin(); pit != procs.end(); ++pit) {
198                 XMLNode* lv2_node = (*pit)->child (X_("lv2"));
199                 if (!lv2_node) {
200                         continue;
201                 }
202                 string template_dir;
203
204                 if (!lv2_node->get_property (X_("template-dir"), template_dir)) {
205                         continue;
206                 }
207
208                 const int suffix_pos = template_dir.size() - name.size();
209                 if (suffix_pos < 0) {
210                         cerr << "Template name\"" << name << "\" longer than template-dir \"" << template_dir << "\", WTH?" << endl;
211                         continue;
212                 }
213
214                 if (template_dir.compare (suffix_pos, template_dir.size(), name)) {
215                         cerr << "Template name \"" << name << "\" no suffix of template-dir \"" << template_dir << "\"" << endl;
216                         continue;
217                 }
218
219                 const string new_template_dir = template_dir.substr (0, suffix_pos) + new_name;
220                 lv2_node->set_property (X_("template-dir"), new_template_dir);
221
222                 adjusted = true;
223         }
224
225         return adjusted;
226 }
227
228 void SessionTemplateManager::init ()
229 {
230         vector<TemplateInfo> templates;
231         find_session_templates (templates);
232         setup_model (templates);
233 }
234
235 void RouteTemplateManager::init ()
236 {
237         vector<TemplateInfo> templates;
238         find_route_templates (templates);
239         setup_model (templates);
240 }
241
242 void
243 SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name_)
244 {
245         const string old_path = item->get_value (_template_columns.path);
246         const string old_name = item->get_value (_template_columns.name);
247         const string new_name = string (new_name_);
248
249         const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
250
251         XMLTree tree;
252
253         if (!tree.read(old_file_old_path)) {
254                 error << string_compose (_("Could not parse template file \"%1\"."), old_file_old_path) << endmsg;
255                 return;
256         }
257         XMLNode* root = tree.root();
258
259         const XMLNode* const routes_node = root->child (X_("Routes"));
260         if (routes_node) {
261                 const XMLNodeList& routes = routes_node->children (X_("Route"));
262                 XMLNodeConstIterator rit;
263                 for (rit = routes.begin(); rit != routes.end(); ++rit) {
264                         adjust_plugin_paths (*rit, old_name, new_name);
265                 }
266         }
267
268         const string new_file_old_path = Glib::build_filename (old_path, new_name+".template");
269
270         tree.set_filename (new_file_old_path);
271
272         if (!tree.write ()) {
273                 error << string_compose(_("Could not write to new template file \"%1\"."), new_file_old_path);
274                 return;
275         }
276
277         const string new_path = Glib::build_filename (user_template_directory (), new_name);
278
279         if (g_rename (old_path.c_str(), new_path.c_str()) != 0) {
280                 error << string_compose (_("Could not rename template directory from \"%1\" to \"%2\": %3"),
281                                          old_path, new_path, strerror (errno)) << endmsg;
282                 g_unlink (new_file_old_path.c_str());
283                 return;
284         }
285
286         const string old_file_new_path = Glib::build_filename (new_path, old_name+".template");
287         if (g_unlink (old_file_new_path.c_str())) {
288                 error << string_compose (X_("Could not delete old template file \"%1\": %2"),
289                                          old_file_new_path, strerror (errno)) << endmsg;
290         }
291
292         item->set_value (_template_columns.name, new_name);
293         item->set_value (_template_columns.path, new_path);
294 }
295
296
297 void
298 SessionTemplateManager::delete_selected_template ()
299 {
300         if (_template_treeview.get_selection()->count_selected_rows() == 0) {
301                 return;
302         }
303
304         Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
305
306         if (!it) {
307                 return;
308         }
309
310         const string path = it->get_value (_template_columns.path);
311         const string name = it->get_value (_template_columns.name);
312         const string file_path = Glib::build_filename (path, name+".template");
313
314         if (g_unlink (file_path.c_str()) != 0) {
315                 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
316                 return;
317         }
318
319         if (g_rmdir (path.c_str()) != 0) {
320                 error << string_compose(_("Could not delete template directory \"%1\": %2"), path, strerror (errno)) << endmsg;
321         }
322
323         _template_model->erase (it);
324         row_selection_changed ();
325 }
326
327 void
328 RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::ustring& new_name)
329 {
330         const string name = item->get_value (_template_columns.name);
331         const string old_filepath = item->get_value (_template_columns.path);
332         const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
333
334         XMLTree tree;
335         if (!tree.read (old_filepath)) {
336                 error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;
337                 return;
338         }
339         tree.root()->children().front()->set_property (X_("name"), new_name);
340
341         const bool adjusted = adjust_plugin_paths (tree.root(), name, string (new_name));
342
343         if (adjusted) {
344                 const string old_state_dir = Glib::build_filename (user_route_template_directory(), name);
345                 const string new_state_dir = Glib::build_filename (user_route_template_directory(), new_name);
346                 if (g_rename (old_state_dir.c_str(), new_state_dir.c_str()) != 0) {
347                         error << string_compose (_("Could not rename state dir \"%1\" to \"%22\": %3"), old_state_dir, new_state_dir, strerror (errno)) << endmsg;
348                         return;
349                 }
350         }
351
352         tree.set_filename (new_filepath);
353
354         if (!tree.write ()) {
355                 error << string_compose(_("Could not write new template file \"%1\"."), new_filepath) << endmsg;
356                 return;
357         }
358
359         if (g_unlink (old_filepath.c_str()) != 0) {
360                 error << string_compose (_("Could not remove old template file \"%1\": %2"), old_filepath, strerror (errno)) << endmsg;
361         }
362
363         item->set_value (_template_columns.name, string (new_name));
364         item->set_value (_template_columns.path, new_filepath);
365 }
366
367 void
368 RouteTemplateManager::delete_selected_template ()
369 {
370         if (_template_treeview.get_selection()->count_selected_rows() == 0) {
371                 return;
372         }
373
374         Gtk::TreeModel::const_iterator it = _template_treeview.get_selection()->get_selected();
375
376         if (!it) {
377                 return;
378         }
379
380         const string file_path = it->get_value (_template_columns.path);
381
382         if (g_unlink (file_path.c_str()) != 0) {
383                 error << string_compose(_("Could not delete template file \"%1\": %2"), file_path, strerror (errno)) << endmsg;
384                 return;
385         }
386
387         _template_model->erase (it);
388         row_selection_changed ();
389 }