add datetime to snapshot sidebar
[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 <glib/gstdio.h>
23 #include <glibmm.h>
24
25 #include <gtkmm/liststore.h>
26
27 #include "gtkmm2ext/choice.h"
28
29 #include "ardour/filename_extensions.h"
30 #include "ardour/session.h"
31 #include "ardour/session_state_utils.h"
32 #include "ardour/session_directory.h"
33
34 #include "editor_snapshots.h"
35 #include "ardour_ui.h"
36 #include "i18n.h"
37 #include "utils.h"
38 #include "prompter.h"
39
40 using namespace std;
41 using namespace PBD;
42 using namespace Gtk;
43 using namespace ARDOUR;
44 using namespace ARDOUR_UI_UTILS;
45
46 EditorSnapshots::EditorSnapshots (Editor* e)
47         : EditorComponent (e)
48 {
49         _model = ListStore::create (_columns);
50         _display.set_model (_model);
51         _display.append_column (X_("snapshot"), _columns.visible_name);
52         _display.append_column (X_("lastmod"), _columns.time_formatted);
53         _display.set_size_request (75, -1);
54         _display.set_headers_visible (false);
55         _display.set_reorderable (false);
56         _scroller.add (_display);
57         _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
58
59         _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorSnapshots::selection_changed));
60         _display.signal_button_press_event().connect (sigc::mem_fun (*this, &EditorSnapshots::button_press), false);
61 }
62
63 void
64 EditorSnapshots::set_session (Session* s)
65 {
66         SessionHandlePtr::set_session (s);
67
68         redisplay ();
69 }
70
71 /** A new snapshot has been selected.
72  */
73 void
74 EditorSnapshots::selection_changed ()
75 {
76         if (_display.get_selection()->count_selected_rows() > 0) {
77
78                 TreeModel::iterator i = _display.get_selection()->get_selected();
79
80                 std::string snap_name = (*i)[_columns.real_name];
81
82                 if (snap_name.length() == 0) {
83                         return;
84                 }
85
86                 if (_session->snap_name() == snap_name) {
87                         return;
88                 }
89
90                 ARDOUR_UI::instance()->load_session (_session->path(), string (snap_name));
91         }
92 }
93
94 bool
95 EditorSnapshots::button_press (GdkEventButton* ev)
96 {
97         if (ev->button == 3) {
98                 /* Right-click on the snapshot list. Work out which snapshot it
99                    was over. */
100                 Gtk::TreeModel::Path path;
101                 Gtk::TreeViewColumn* col;
102                 int cx;
103                 int cy;
104                 _display.get_path_at_pos ((int) ev->x, (int) ev->y, path, col, cx, cy);
105                 Gtk::TreeModel::iterator iter = _model->get_iter (path);
106                 if (iter) {
107                         Gtk::TreeModel::Row row = *iter;
108                         popup_context_menu (ev->button, ev->time, row[_columns.real_name]);
109                 }
110                 return true;
111         }
112
113         return false;
114 }
115
116
117 /** Pop up the snapshot display context menu.
118  * @param button Button used to open the menu.
119  * @param time Menu open time.
120  * @param snapshot_name Name of the snapshot that the menu click was over.
121  */
122 void
123 EditorSnapshots::popup_context_menu (int button, int32_t time, std::string snapshot_name)
124 {
125         using namespace Menu_Helpers;
126
127         MenuList& items (_menu.items());
128         items.clear ();
129
130         const bool modification_allowed = (_session->snap_name() != snapshot_name && _session->name() != snapshot_name);
131
132         add_item_with_sensitivity (items, MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::remove), snapshot_name)), modification_allowed);
133
134         add_item_with_sensitivity (items, MenuElem (_("Rename..."), sigc::bind (sigc::mem_fun (*this, &EditorSnapshots::rename), snapshot_name)), modification_allowed);
135
136         _menu.popup (button, time);
137 }
138
139 void
140 EditorSnapshots::rename (std::string old_name)
141 {
142         ArdourPrompter prompter(true);
143
144         string new_name;
145
146         prompter.set_name ("Prompter");
147         prompter.set_title (_("Rename Snapshot"));
148         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
149         prompter.set_prompt (_("New name of snapshot"));
150         prompter.set_initial_text (old_name);
151
152         if (prompter.run() == RESPONSE_ACCEPT) {
153                 prompter.get_result (new_name);
154                 if (new_name.length()) {
155                         _session->rename_state (old_name, new_name);
156                         redisplay ();
157                 }
158         }
159 }
160
161
162 void
163 EditorSnapshots::remove (std::string name)
164 {
165         vector<string> choices;
166
167         std::string prompt = string_compose (_("Do you really want to remove snapshot \"%1\" ?\n(which cannot be undone)"), name);
168
169         choices.push_back (_("No, do nothing."));
170         choices.push_back (_("Yes, remove it."));
171
172         Gtkmm2ext::Choice prompter (_("Remove snapshot"), prompt, choices);
173
174         if (prompter.run () == 1) {
175                 _session->remove_state (name);
176                 redisplay ();
177         }
178 }
179
180 void
181 EditorSnapshots::redisplay ()
182 {
183         if (_session == 0) {
184                 return;
185         }
186
187         vector<std::string> state_file_paths;
188
189         get_state_files_in_directory (_session->session_directory().root_path(),
190                                       state_file_paths);
191
192         if (state_file_paths.empty()) {
193                 return;
194         }
195
196         vector<string> state_file_names (get_file_names_no_extension(state_file_paths));
197
198         _model->clear ();
199
200         for (vector<string>::iterator i = state_file_names.begin(); i != state_file_names.end(); ++i)
201         {
202                 string statename = (*i);
203                 TreeModel::Row row = *(_model->append());
204
205                 /* this lingers on in case we ever want to change the visible
206                    name of the snapshot.
207                 */
208
209                 string display_name;
210                 display_name = statename;
211
212                 if (statename == _session->snap_name()) {
213                         _display.get_selection()->select(row);
214                 }
215
216                 std::string s = Glib::build_filename (_session->path(), statename + ARDOUR::statefile_suffix);
217
218                 GStatBuf gsb;
219                 g_stat (s.c_str(), &gsb);
220                 Glib::DateTime gdt(Glib::DateTime::create_now_local (gsb.st_mtime));
221
222                 row[_columns.visible_name] = display_name;
223                 row[_columns.real_name] = statename;
224                 row[_columns.time_formatted] = gdt.format ("%F %H:%M");
225         }
226 }
227