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