Add Import from session -functionality
[ardour.git] / gtk2_ardour / session_import_dialog.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
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 "session_import_dialog.h"
22
23 #include <pbd/failed_constructor.h>
24
25 #include <ardour/audio_region_importer.h>
26 #include <ardour/audio_playlist_importer.h>
27 #include <ardour/location_importer.h>
28 #include <ardour/tempo_map_importer.h>
29
30 #include <gtkmm2ext/utils.h>
31 #include <gtkmm2ext/window_title.h>
32
33 #include "prompter.h"
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37
38 SessionImportDialog::SessionImportDialog (ARDOUR::Session & target) :
39   ArdourDialog (_("Import from session")),
40   target (target),
41   file_browse_button (_("Browse"))
42 {
43         // File entry
44         file_entry.set_name ("ImportFileNameEntry");
45         file_entry.set_text ("/");
46         Gtkmm2ext::set_size_request_to_display_given_text (file_entry, X_("Kg/quite/a/reasonable/size/for/files/i/think"), 5, 8);
47         
48         file_browse_button.set_name ("EditorGTKButton");
49         file_browse_button.signal_clicked().connect (mem_fun(*this, &SessionImportDialog::browse));
50         
51         file_hbox.set_spacing (5);
52         file_hbox.set_border_width (5);
53         file_hbox.pack_start (file_entry, true, true);
54         file_hbox.pack_start (file_browse_button, false, false);
55         
56         file_frame.add (file_hbox);
57         file_frame.set_border_width (5);
58         file_frame.set_name ("ImportFrom");
59         file_frame.set_label (_("Import from Session"));
60         
61         get_vbox()->pack_start (file_frame, false, false);
62
63         // Session browser
64         session_tree = Gtk::TreeStore::create (sb_cols);
65         session_browser.set_model (session_tree);
66
67         session_browser.set_name ("SessionBrowser");
68         session_browser.append_column (_("Elements"), sb_cols.name);
69         session_browser.append_column_editable (_("Import"), sb_cols.queued);
70         session_browser.get_column(0)->set_min_width (180);
71         session_browser.get_column(1)->set_min_width (40);
72         session_browser.get_column(1)->set_sizing (Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
73         
74         session_scroll.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
75         session_scroll.add (session_browser);
76         session_scroll.set_size_request (220, 400);
77         
78         // Connect signals
79         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *> (session_browser.get_column_cell_renderer (1));
80         toggle->signal_toggled().connect(mem_fun (*this, &SessionImportDialog::update));
81         session_browser.signal_row_activated().connect(mem_fun (*this, &SessionImportDialog::show_info));
82         
83         get_vbox()->pack_start (session_scroll, false, false);
84         
85         // Tooltips
86         session_browser.set_has_tooltip();
87         session_browser.signal_query_tooltip().connect(mem_fun(*this, &SessionImportDialog::query_tooltip));
88         
89         // Buttons
90         cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
91         cancel_button->signal_clicked().connect (mem_fun (*this, &SessionImportDialog::end_dialog));
92         ok_button = add_button (_("Import"), Gtk::RESPONSE_ACCEPT);
93         ok_button->signal_clicked().connect (mem_fun (*this, &SessionImportDialog::do_merge));
94         
95         // prompt signals
96         ElementImporter::Rename.connect (mem_fun (*this, &SessionImportDialog::open_rename_dialog));
97         ElementImporter::Prompt.connect (mem_fun (*this, &SessionImportDialog::open_prompt_dialog));
98         
99         // Finalize
100         show_all();
101 }
102
103 void
104 SessionImportDialog::load_session (const string& filename)
105 {
106         tree.read (filename);
107         AudioRegionImportHandler *region_handler;
108         
109         region_handler = new AudioRegionImportHandler (tree, target);
110         handlers.push_back (HandlerPtr(region_handler));
111         handlers.push_back (HandlerPtr(new AudioPlaylistImportHandler (tree, target, *region_handler)));
112         handlers.push_back (HandlerPtr(new UnusedAudioPlaylistImportHandler (tree, target, *region_handler)));
113         handlers.push_back (HandlerPtr(new LocationImportHandler (tree, target)));
114         handlers.push_back (HandlerPtr(new TempoMapImportHandler (tree, target)));
115         
116         fill_list();
117         
118         if (ElementImportHandler::dirty()) {
119                 // Warn user
120                 string txt = _("Some elements had errors in them. Please see the log for details");
121                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
122                 msg.run();
123         }
124 }
125
126 void
127 SessionImportDialog::fill_list ()
128 {
129         session_tree->clear();
130         
131         // Loop through element types
132         for (HandlerList::iterator handler = handlers.begin(); handler != handlers.end(); ++handler) {
133                 Gtk::TreeModel::iterator iter = session_tree->append();
134                 Gtk::TreeModel::Row row = *iter;
135                 row[sb_cols.name] = (*handler)->get_info();
136                 row[sb_cols.queued] = false;
137                 row[sb_cols.element] = ElementPtr(); // "Null" pointer
138                 
139                 // Loop through elements
140                 ElementList &elements = (*handler)->elements;
141                 for (ElementList::iterator element = elements.begin(); element != elements.end(); ++element) {
142                         iter = session_tree->append(row.children());
143                         Gtk::TreeModel::Row child = *iter;
144                         child[sb_cols.name] = (*element)->get_name();
145                         child[sb_cols.queued] = false;
146                         child[sb_cols.element] = *element;
147                 }
148         }
149 }
150
151 void
152 SessionImportDialog::browse ()
153 {
154         Gtk::FileChooserDialog dialog(_("Import from session"), browse_action());
155         dialog.set_transient_for(*this);
156         dialog.set_filename (file_entry.get_text());
157
158         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
159         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
160   
161         int result = dialog.run();
162
163         if (result == Gtk::RESPONSE_OK) {
164                 string filename = dialog.get_filename();
165         
166                 if (filename.length()) {
167                         file_entry.set_text (filename);
168                         load_session (filename);
169                 }
170         }
171 }
172
173 void
174 SessionImportDialog::do_merge ()
175 {
176         
177         // element types
178         Gtk::TreeModel::Children types = session_browser.get_model()->children();
179         Gtk::TreeModel::Children::iterator ti;
180         for (ti = types.begin(); ti != types.end(); ++ti) {
181                 // elements
182                 Gtk::TreeModel::Children elements = ti->children();
183                 Gtk::TreeModel::Children::iterator ei;
184                 for (ei = elements.begin(); ei != elements.end(); ++ei) {
185                         if ((*ei)[sb_cols.queued]) {
186                                 ElementPtr element = (*ei)[sb_cols.element];
187                                 element->move();
188                         }
189                 }
190         }
191         
192         end_dialog();
193         
194         if (ElementImportHandler::errors()) {
195                 // Warn user
196                 string txt = _("Some elements had errors in them. Please see the log for details");
197                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
198                 msg.run();
199         }
200 }
201
202
203 void
204 SessionImportDialog::update (string path)
205 {
206         Gtk::TreeModel::iterator cell = session_browser.get_model()->get_iter (path);
207         
208         // Select all elements if element type is selected
209         if (path.size() == 1) {
210                 {
211                         // Prompt user for verification
212                         string txt = _("This will select all elements of this type!");
213                         Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL, true);
214                         if (msg.run() == Gtk::RESPONSE_CANCEL) {
215                                 (*cell)[sb_cols.queued] = false;
216                                 return;
217                         }
218                 }
219                 
220                 Gtk::TreeModel::Children elements = cell->children();
221                 Gtk::TreeModel::Children::iterator ei;
222                 for (ei = elements.begin(); ei != elements.end(); ++ei) {
223                         ElementPtr element = (*ei)[sb_cols.element];
224                         if (element->prepare_move()) {
225                                 (*ei)[sb_cols.queued] = true;
226                         } else {
227                                 (*cell)[sb_cols.queued] = false; // Not all are selected
228                         }
229                 }
230                 return;
231         }
232         
233         ElementPtr element = (*cell)[sb_cols.element];
234         if ((*cell)[sb_cols.queued]) {
235                 if (!element->prepare_move()) {
236                         (*cell)[sb_cols.queued] = false;
237                 }
238         } else {
239                 element->cancel_move();
240         }
241 }
242
243 void
244 SessionImportDialog::show_info(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column)
245 {
246         if (path.size() == 1) {
247                 return;
248         }
249         
250         Gtk::TreeModel::iterator cell = session_browser.get_model()->get_iter (path);
251         ElementPtr element = (*cell)[sb_cols.element];
252         string info = element->get_info();
253         
254         Gtk::MessageDialog msg (info, false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true);
255         msg.run();
256 }
257
258 bool
259 SessionImportDialog::query_tooltip(int x, int y, bool keyboard_tooltip, const Glib::RefPtr<Gtk::Tooltip>& tooltip)
260 {
261         Gtk::TreeModel::Path path;
262         Gtk::TreeViewColumn* column;
263         int cell_x, cell_y;
264         
265         // Get element
266         session_browser.get_path_at_pos (x, y, path, column, cell_x, cell_y);
267         if (path.gobj() == 0) {
268                 return false;
269         }
270         Gtk::TreeModel::iterator row = session_browser.get_model()->get_iter (path);
271         //--row; // FIXME Strange offset in rows, if someone figures this out, please fix
272         ElementPtr element = (*row)[sb_cols.element];
273         if (element.get() == 0) {
274                 return false;
275         }
276         
277         // Prepare tooltip
278         tooltip->set_text(element->get_info());
279         
280         return true;
281 }
282
283 void
284 SessionImportDialog::end_dialog ()
285 {
286         hide_all();
287
288         set_modal (false);
289         ok_button->set_sensitive(true);
290 }
291
292 std::pair<bool, string>
293 SessionImportDialog::open_rename_dialog (string text, string name)
294 {
295         ArdourPrompter prompter(true);
296         string new_name;
297
298         prompter.set_name ("Prompter");
299         prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
300         prompter.set_prompt (text);
301         prompter.set_initial_text (name);
302         
303         if (prompter.run() == Gtk::RESPONSE_ACCEPT) {
304                 prompter.get_result (new_name);
305                 if (new_name.length()) {
306                         name = new_name;
307                 }
308                 return std::make_pair (true, new_name);
309         }
310         return std::make_pair (false, new_name);
311 }
312
313 bool
314 SessionImportDialog::open_prompt_dialog (string text)
315 {
316         Gtk::MessageDialog msg (text, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL, true);
317         if (msg.run() == Gtk::RESPONSE_OK) {
318                 return true;
319         }
320         return false;
321 }