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