Fix typos
[ardour.git] / gtk2_ardour / editor_snapshots.cc
1 /*
2     Copyright (C) 2000-2009 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20
21 #include <glib.h>
22 #include <glibmm.h>
23 #include <glibmm/datetime.h>
24
25 #include <gtkmm/liststore.h>
26
27 #include "pbd/file_utils.h"
28 #include "pbd/gstdio_compat.h"
29 #include "pbd/i18n.h"
30
31 #include "ardour/filename_extensions.h"
32 #include "ardour/profile.h"
33 #include "ardour/session.h"
34 #include "ardour/session_state_utils.h"
35 #include "ardour/session_directory.h"
36
37 #include "widgets/choice.h"
38 #include "widgets/prompter.h"
39
40 #include "editor_snapshots.h"
41 #include "ardour_ui.h"
42 #include "utils.h"
43
44 using namespace std;
45 using namespace PBD;
46 using namespace Gtk;
47 using namespace ARDOUR;
48 using namespace ARDOUR_UI_UTILS;
49
50 EditorSnapshots::EditorSnapshots (Editor* e)
51         : EditorComponent (e)
52 {
53         _snap_model = ListStore::create (_columns);
54         _snap_display.set_model (_snap_model);
55         _snap_display.append_column (_("Snapshot (click to load)"), _columns.visible_name);
56         _snap_display.append_column (_("Modified Date"), _columns.time_formatted);
57         _snap_display.set_size_request (75, -1);
58         _snap_display.set_headers_visible (true);
59         _snap_display.set_reorderable (false);
60         _snap_scroller.add (_snap_display);
61         _snap_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
62
63         _snap_display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorSnapshots::selection_changed));
64         _snap_display.signal_button_press_event().connect (sigc::mem_fun (*this, &EditorSnapshots::button_press), false);
65         
66         _back_model = ListStore::create (_columns);
67         _back_display.set_model (_back_model);
68         _back_display.append_column (_("Auto-Backup (click to load)"), _columns.visible_name);
69         _back_display.append_column (_("Modified Date"), _columns.time_formatted);
70         _back_display.set_size_request (75, -1);
71         _back_display.set_headers_visible (true);
72         _back_display.set_reorderable (false);
73         _back_scroller.add (_back_display);
74         _back_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
75
76         _back_display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorSnapshots::backup_selection_changed));
77         
78         _pane.add(_snap_scroller);
79 if(Profile->get_mixbus()) {
80         _pane.add(_back_scroller);
81 }
82 }
83
84 void
85 EditorSnapshots::set_session (Session* s)
86 {
87         SessionHandlePtr::set_session (s);
88
89         redisplay ();
90 }
91
92 /** A new snapshot has been selected.
93  */
94 void
95 EditorSnapshots::selection_changed ()
96 {
97         if (_snap_display.get_selection()->count_selected_rows() > 0) {
98
99                 TreeModel::iterator i = _snap_display.get_selection()->get_selected();
100
101                 std::string snap_name = (*i)[_columns.real_name];
102
103                 if (snap_name.length() == 0) {
104                         return;
105                 }
106
107                 if (_session->snap_name() == snap_name) {
108                         return;
109                 }
110
111                 _snap_display.set_sensitive (false);
112                 ARDOUR_UI::instance()->load_session (_session->path(), string (snap_name));
113                 _snap_display.set_sensitive (true);
114         }
115 }
116
117 bool
118 EditorSnapshots::button_press (GdkEventButton* ev)
119 {
120         if (ev->button == 3) {
121                 /* Right-click on the snapshot list. Work out which snapshot it
122                    was over. */
123                 Gtk::TreeModel::Path path;
124                 Gtk::TreeViewColumn* col;
125                 int cx;
126                 int cy;
127                 _snap_display.get_path_at_pos ((int) ev->x, (int) ev->y, path, col, cx, cy);
128                 Gtk::TreeModel::iterator iter = _snap_model->get_iter (path);
129                 if (iter) {
130                         Gtk::TreeModel::Row row = *iter;
131                         popup_context_menu (ev->button, ev->time, row[_columns.real_name]);
132                 }
133                 return true;
134         }
135
136         return false;
137 }
138
139
140 /** Pop up the snapshot display context menu.
141  * @param button Button used to open the menu.
142  * @param time Menu open time.
143  * @param snapshot_name Name of the snapshot that the menu click was over.
144  */
145 void
146 EditorSnapshots::popup_context_menu (int button, int32_t time, std::string snapshot_name)
147 {
148         using namespace Menu_Helpers;
149
150         MenuList& items (_menu.items());
151         items.clear ();
152
153         const bool modification_allowed = (_session->snap_name() != snapshot_name && _session->name() != snapshot_name);
154
155         add_item_with_sensitivity (items, MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::remove), snapshot_name)), modification_allowed);
156
157         add_item_with_sensitivity (items, MenuElem (_("Rename..."), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::rename), snapshot_name)), modification_allowed);
158
159         _menu.popup (button, time);
160 }
161
162 void
163 EditorSnapshots::rename (std::string old_name)
164 {
165         ArdourWidgets::Prompter prompter(true);
166
167         string new_name;
168
169         prompter.set_name ("Prompter");
170         prompter.set_title (_("Rename Snapshot"));
171         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
172         prompter.set_prompt (_("New name of snapshot"));
173         prompter.set_initial_text (old_name);
174
175         if (prompter.run() == RESPONSE_ACCEPT) {
176                 prompter.get_result (new_name);
177                 if (new_name.length()) {
178                         _session->rename_state (old_name, new_name);
179                         redisplay ();
180                 }
181         }
182 }
183
184
185 void
186 EditorSnapshots::remove (std::string name)
187 {
188         vector<string> choices;
189
190         std::string prompt = string_compose (_("Do you really want to remove snapshot \"%1\" ?\n(which cannot be undone)"), name);
191
192         choices.push_back (_("No, do nothing."));
193         choices.push_back (_("Yes, remove it."));
194
195         ArdourWidgets::Choice prompter (_("Remove snapshot"), prompt, choices);
196
197         if (prompter.run () == 1) {
198                 _session->remove_state (name);
199                 redisplay ();
200         }
201 }
202
203 void
204 EditorSnapshots::redisplay ()
205 {
206         if (_session == 0) {
207                 return;
208         }
209
210         //fill the snapshots pane
211         {
212                 vector<std::string> state_file_paths;
213
214                 get_state_files_in_directory (_session->session_directory().root_path(),
215                                                                           state_file_paths);
216
217                 if (state_file_paths.empty()) {
218                         return;
219                 }
220
221                 vector<string> state_file_names (get_file_names_no_extension(state_file_paths));
222
223                 _snap_model->clear ();
224
225                 for (vector<string>::iterator i = state_file_names.begin(); i != state_file_names.end(); ++i)
226                 {
227                         string statename = (*i);
228                         TreeModel::Row row = *(_snap_model->append());
229
230                         /* this lingers on in case we ever want to change the visible
231                            name of the snapshot.
232                         */
233
234                         string display_name;
235                         display_name = statename;
236
237                         if (statename == _session->snap_name()) {
238                                 _snap_display.get_selection()->select(row);
239                         }
240
241                         std::string s = Glib::build_filename (_session->path(), statename + ARDOUR::statefile_suffix);
242
243                         GStatBuf gsb;
244                         g_stat (s.c_str(), &gsb);
245                         Glib::DateTime gdt(Glib::DateTime::create_now_local (gsb.st_mtime));
246
247                         row[_columns.visible_name] = display_name;
248                         row[_columns.real_name] = statename;
249                         row[_columns.time_formatted] = gdt.format ("%F %H:%M");
250                 }
251         }
252         
253         //fill the backup pane
254         {
255                 vector<std::string> state_file_paths;
256
257                 get_state_files_in_directory (_session->session_directory().backup_path(),
258                                                                           state_file_paths);
259
260                 if (state_file_paths.empty()) {
261                         return;
262                 }
263
264                 vector<string> state_file_names (get_file_names_no_extension(state_file_paths));
265
266                 _back_model->clear ();
267
268                 for (vector<string>::iterator i = state_file_names.begin(); i != state_file_names.end(); ++i)
269                 {
270                         string statename = (*i);
271                         TreeModel::Row row = *(_back_model->append());
272
273                         /* this lingers on in case we ever want to change the visible
274                            name of the snapshot.
275                         */
276
277                         string display_name;
278                         display_name = statename;
279
280                         std::string s = Glib::build_filename (_session->path(), statename + ARDOUR::statefile_suffix);
281
282                         GStatBuf gsb;
283                         g_stat (s.c_str(), &gsb);
284                         Glib::DateTime gdt(Glib::DateTime::create_now_local (gsb.st_mtime));
285
286                         row[_columns.visible_name] = display_name;
287                         row[_columns.real_name] = statename;
288                         row[_columns.time_formatted] = gdt.format ("%F %H:%M");
289                 }
290         }
291         
292 }
293
294 /** A new backup has been selected.
295  */
296 void
297 EditorSnapshots::backup_selection_changed ()
298 {
299         if (_back_display.get_selection()->count_selected_rows() > 0) {
300
301                 TreeModel::iterator i = _back_display.get_selection()->get_selected();
302
303                 std::string back_name = (*i)[_columns.real_name];
304
305                 //copy the backup file to the session root folder, so we can open it
306                 std::string back_path = _session->session_directory().backup_path() + G_DIR_SEPARATOR + back_name + ARDOUR::statefile_suffix;
307                 std::string copy_path = _session->session_directory().root_path() + G_DIR_SEPARATOR + back_name + ARDOUR::statefile_suffix;
308                 PBD::copy_file (back_path, copy_path);
309                 
310                 //now open the copy
311                 _snap_display.set_sensitive (false);
312                 ARDOUR_UI::instance()->load_session (_session->path(), string (back_name));
313                 _snap_display.set_sensitive (true);
314         }
315 }
316