RouteDialog: Move built-in types into template list experiment
[ardour.git] / gtk2_ardour / session_dialog.cc
1 /*
2   Copyright (C) 2013 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <algorithm>
25
26 #include <glib.h>
27 #include "pbd/gstdio_compat.h"
28
29 #include <glibmm.h>
30 #include <glibmm/datetime.h>
31
32 #include <gtkmm/filechooser.h>
33 #include <gtkmm/stock.h>
34
35 #include "pbd/basename.h"
36 #include "pbd/failed_constructor.h"
37 #include "pbd/file_utils.h"
38 #include "pbd/replace_all.h"
39 #include "pbd/whitespace.h"
40 #include "pbd/stacktrace.h"
41 #include "pbd/stl_delete.h"
42 #include "pbd/openuri.h"
43
44 #include "gtkmm2ext/utils.h"
45 #include "gtkmm2ext/keyboard.h"
46
47 #include "widgets/tooltips.h"
48
49 #include "ardour/audioengine.h"
50 #include "ardour/filesystem_paths.h"
51 #include "ardour/luascripting.h"
52 #include "ardour/recent_sessions.h"
53 #include "ardour/session.h"
54 #include "ardour/session_state_utils.h"
55 #include "ardour/template_utils.h"
56 #include "ardour/filename_extensions.h"
57
58 #include "LuaBridge/LuaBridge.h"
59
60 #include "ardour_ui.h"
61 #include "session_dialog.h"
62 #include "opts.h"
63 #include "engine_dialog.h"
64 #include "pbd/i18n.h"
65 #include "ui_config.h"
66 #include "utils.h"
67
68 using namespace std;
69 using namespace Gtk;
70 using namespace Gdk;
71 using namespace Glib;
72 using namespace PBD;
73 using namespace ARDOUR;
74 using namespace ArdourWidgets;
75 using namespace ARDOUR_UI_UTILS;
76
77 SessionDialog::SessionDialog (bool require_new, const std::string& session_name, const std::string& session_path, const std::string& template_name, bool cancel_not_quit)
78         : ArdourDialog (_("Session Setup"), true, true)
79         , new_only (require_new)
80         , _provided_session_name (session_name)
81         , _provided_session_path (session_path)
82         , new_folder_chooser (FILE_CHOOSER_ACTION_SELECT_FOLDER)
83         , _existing_session_chooser_used (false)
84 {
85         set_position (WIN_POS_CENTER);
86         get_vbox()->set_spacing (6);
87
88         cancel_button = add_button ((cancel_not_quit ? Stock::CANCEL : Stock::QUIT), RESPONSE_CANCEL);
89         back_button = add_button (Stock::GO_BACK, RESPONSE_NO);
90         open_button = add_button (Stock::OPEN, RESPONSE_ACCEPT);
91
92         back_button->signal_button_press_event().connect (sigc::mem_fun (*this, &SessionDialog::back_button_pressed), false);
93         open_button->signal_button_press_event().connect (sigc::mem_fun (*this, &SessionDialog::open_button_pressed), false);
94
95         open_button->set_sensitive (false);
96         back_button->set_sensitive (false);
97
98         /* this is where announcements will be displayed, but it may be empty
99          * and invisible most of the time.
100          */
101
102         info_frame.set_shadow_type(SHADOW_ETCHED_OUT);
103         info_frame.set_no_show_all (true);
104         info_frame.set_border_width (12);
105         get_vbox()->pack_start (info_frame, false, false);
106
107         setup_new_session_page ();
108
109         if (!new_only) {
110                 setup_initial_choice_box ();
111                 get_vbox()->pack_start (ic_vbox, true, true);
112         } else {
113                 get_vbox()->pack_start (session_new_vbox, true, true);
114         }
115
116         if (!template_name.empty()) {
117                 load_template_override = template_name;
118         }
119
120         get_vbox()->show_all ();
121
122         /* fill data models and show/hide accordingly */
123
124         populate_session_templates ();
125
126         if (recent_session_model) {
127                 int cnt = redisplay_recent_sessions ();
128                 if (cnt > 0) {
129                         recent_scroller.show();
130                         recent_label.show ();
131
132                         if (cnt > 4) {
133                                 recent_scroller.set_size_request (-1, 300);
134                         } else {
135                                 recent_scroller.set_size_request (-1, 80);
136                         }
137                 } else {
138                         recent_scroller.hide();
139                         recent_label.hide ();
140                 }
141         }
142
143         /* possibly get out of here immediately if everything is ready to go.
144            We still need to set up the whole dialog because of the way
145            ARDOUR_UI::get_session_parameters() might skip it on a first
146            pass then require it for a second pass (e.g. when there
147            is an error with session loading and we have to ask the user
148            what to do next).
149         */
150
151         if (!session_name.empty() && !require_new) {
152                 response (RESPONSE_OK);
153                 return;
154         }
155 }
156
157 SessionDialog::SessionDialog ()
158         : ArdourDialog (_("Recent Sessions"), true, true)
159         , new_only (false)
160         , _provided_session_name ("")
161         , _provided_session_path ("")
162         , _existing_session_chooser_used (false) // caller must check should_be_new
163 {
164         get_vbox()->set_spacing (6);
165
166         cancel_button = add_button (Stock::CANCEL, RESPONSE_CANCEL);
167         open_button = add_button (Stock::OPEN, RESPONSE_ACCEPT);
168         open_button->set_sensitive (false);
169
170         setup_recent_sessions ();
171
172         get_vbox()->pack_start (recent_scroller, true, true);
173         get_vbox()->show_all ();
174
175         recent_scroller.show();
176
177         int cnt = redisplay_recent_sessions ();
178         if (cnt > 4) {
179                 recent_scroller.set_size_request (-1, 300);
180         } else {
181                 recent_scroller.set_size_request (-1, 80);
182         }
183
184 }
185
186
187
188 SessionDialog::~SessionDialog()
189 {
190 }
191
192 void
193 SessionDialog::clear_given ()
194 {
195         _provided_session_path = "";
196         _provided_session_name = "";
197 }
198
199 uint32_t
200 SessionDialog::meta_master_bus_profile (std::string script_path) const
201 {
202         if (!Glib::file_test (script_path, Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR)) {
203                 return UINT32_MAX;
204         }
205
206         LuaState lua;
207         lua.sandbox (true);
208         lua_State* L = lua.getState();
209
210         lua.do_command (
211                         "ardourluainfo = {}"
212                         "function ardour (entry)"
213                         "  ardourluainfo['type'] = assert(entry['type'])"
214                         "  ardourluainfo['master_bus'] = entry['master_bus'] or 2"
215                         " end"
216                         );
217
218         int err = -1;
219
220         try {
221                 err = lua.do_file (script_path);
222         } catch (luabridge::LuaException const& e) {
223                 err = -1;
224         }  catch (...) {
225                 err = -1;
226         }
227
228
229         if (err) {
230                 return UINT32_MAX;
231         }
232
233         luabridge::LuaRef nfo = luabridge::getGlobal (L, "ardourluainfo");
234         if (nfo.type() != LUA_TTABLE) {
235                 return UINT32_MAX;
236         }
237
238         if (nfo["master_bus"].type() != LUA_TNUMBER || nfo["type"].type() != LUA_TSTRING) {
239                 return UINT32_MAX;
240         }
241
242         LuaScriptInfo::ScriptType type = LuaScriptInfo::str2type (nfo["type"].cast<std::string>());
243         if (type != LuaScriptInfo::SessionInit) {
244                 return UINT32_MAX;
245         }
246
247         return nfo["master_bus"].cast<uint32_t>();
248 }
249
250 uint32_t
251 SessionDialog::master_channel_count ()
252 {
253         if (use_session_template ()) {
254                 std::string tn = session_template_name();
255                 if (tn.substr (0, 11) == "urn:ardour:") {
256                         uint32_t mc = meta_master_bus_profile (tn.substr (11));
257                         if (mc != UINT32_MAX) {
258                                 return mc;
259                         }
260                 }
261         }
262         return 2;
263 }
264
265 bool
266 SessionDialog::use_session_template () const
267 {
268         if (!load_template_override.empty()) {
269                 return true;
270         }
271
272         if (template_chooser.get_selection()->count_selected_rows() > 0) {
273                 return true;
274         }
275
276         return false;
277 }
278
279 std::string
280 SessionDialog::session_template_name ()
281 {
282         if (!load_template_override.empty()) {
283                 string the_path (ARDOUR::user_template_directory());
284                 return Glib::build_filename (the_path, load_template_override + ARDOUR::template_suffix);
285         }
286
287         if (template_chooser.get_selection()->count_selected_rows() > 0) {
288
289                 TreeIter const iter = template_chooser.get_selection()->get_selected();
290
291                 if (iter) {
292                         string s = (*iter)[session_template_columns.path];
293                         return s;
294                 }
295         }
296
297         return string();
298 }
299
300 std::string
301 SessionDialog::session_name (bool& should_be_new)
302 {
303         if (!_provided_session_name.empty() && !new_only) {
304                 should_be_new = false;
305                 return _provided_session_name;
306         }
307
308         /* Try recent session selection */
309
310         TreeIter iter = recent_session_display.get_selection()->get_selected();
311
312         if (iter) {
313                 should_be_new = false;
314                 string s = (*iter)[recent_session_columns.fullpath];
315                 if (Glib::file_test (s, Glib::FILE_TEST_IS_REGULAR)) {
316                         return PBD::basename_nosuffix (s);
317                 }
318                 return (*iter)[recent_session_columns.visible_name];
319         }
320
321         if (_existing_session_chooser_used) {
322                 /* existing session chosen from file chooser */
323                 should_be_new = false;
324                 return existing_session_chooser.get_filename ();
325         } else {
326                 should_be_new = true;
327                 string val = new_name_entry.get_text ();
328                 strip_whitespace_edges (val);
329                 return val;
330         }
331 }
332
333 std::string
334 SessionDialog::session_folder ()
335 {
336         if (!_provided_session_path.empty() && !new_only) {
337                 return _provided_session_path;
338         }
339
340         /* Try recent session selection */
341
342         TreeIter iter = recent_session_display.get_selection()->get_selected();
343
344         if (iter) {
345                 string s = (*iter)[recent_session_columns.fullpath];
346                 if (Glib::file_test (s, Glib::FILE_TEST_IS_REGULAR)) {
347                         return Glib::path_get_dirname (s);
348                 }
349                 return s;
350         }
351
352         if (_existing_session_chooser_used) {
353                 /* existing session chosen from file chooser */
354                 return Glib::path_get_dirname (existing_session_chooser.get_current_folder ());
355         } else {
356                 std::string val = new_name_entry.get_text();
357                 strip_whitespace_edges (val);
358                 std::string legal_session_folder_name = legalize_for_path (val);
359                 return Glib::build_filename (new_folder_chooser.get_filename (), legal_session_folder_name);
360         }
361 }
362
363 void
364 SessionDialog::setup_recent_sessions ()
365 {
366         recent_session_model = TreeStore::create (recent_session_columns);
367         recent_session_model->signal_sort_column_changed().connect (sigc::mem_fun (*this, &SessionDialog::recent_session_sort_changed));
368
369         recent_session_display.set_model (recent_session_model);
370         recent_session_display.append_column (_("Session Name"), recent_session_columns.visible_name);
371         recent_session_display.append_column (_("Sample Rate"), recent_session_columns.sample_rate);
372         recent_session_display.append_column (_("File Resolution"), recent_session_columns.disk_format);
373         recent_session_display.append_column (_("Last Modified"), recent_session_columns.time_formatted);
374         recent_session_display.set_headers_visible (true);
375         recent_session_display.get_selection()->set_mode (SELECTION_SINGLE);
376
377         recent_session_display.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &SessionDialog::recent_session_row_selected));
378
379         recent_scroller.add (recent_session_display);
380         recent_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
381         recent_scroller.set_shadow_type (Gtk::SHADOW_IN);
382
383         recent_session_display.show();
384         recent_session_display.signal_row_activated().connect (sigc::mem_fun (*this, &SessionDialog::recent_row_activated));
385         recent_session_display.signal_button_press_event().connect (sigc::mem_fun (*this, &SessionDialog::recent_button_press), false);
386 }
387
388 void
389 SessionDialog::setup_initial_choice_box ()
390 {
391         ic_vbox.set_spacing (6);
392
393         HBox* centering_hbox = manage (new HBox);
394         VBox* centering_vbox = manage (new VBox);
395
396         centering_vbox->set_spacing (6);
397
398         Label* new_label = manage (new Label);
399         new_label->set_markup (string_compose ("<span weight=\"bold\" size=\"large\">%1</span>", _("New Session")));
400         new_label->set_justify (JUSTIFY_CENTER);
401
402         ic_new_session_button.add (*new_label);
403         ic_new_session_button.signal_clicked().connect (sigc::mem_fun (*this, &SessionDialog::new_session_button_clicked));
404
405         Gtk::HBox* hbox = manage (new HBox);
406         Gtk::VBox* vbox = manage (new VBox);
407         hbox->set_spacing (12);
408         vbox->set_spacing (12);
409
410         string image_path;
411
412         Searchpath rc (ARDOUR::ardour_data_search_path());
413         rc.add_subdirectory_to_paths ("resources");
414
415         if (find_file (rc, PROGRAM_NAME "-small-splash.png", image_path)) {
416                 Gtk::Image* image;
417                 if ((image = manage (new Gtk::Image (image_path))) != 0) {
418                         hbox->pack_start (*image, false, false);
419                 }
420         }
421
422         vbox->pack_start (ic_new_session_button, true, true, 20);
423         hbox->pack_start (*vbox, true, true, 20);
424
425         centering_vbox->pack_start (*hbox, false, false);
426
427         /* Possible update message */
428
429         if (ARDOUR_UI::instance()->announce_string() != "" ) {
430
431                 Box *info_box = manage (new VBox);
432                 info_box->set_border_width (12);
433                 info_box->set_spacing (6);
434
435                 info_box->pack_start (info_scroller_label, false, false);
436
437                 info_scroller_count = 0;
438                 info_scroller_connection = Glib::signal_timeout().connect (mem_fun(*this, &SessionDialog::info_scroller_update), 50);
439
440                 Gtk::Button *updates_button = manage (new Gtk::Button (_("Check the website for more...")));
441
442                 updates_button->signal_clicked().connect (mem_fun(*this, &SessionDialog::updates_button_clicked) );
443                 set_tooltip (*updates_button, _("Click to open the program website in your web browser"));
444
445                 info_box->pack_start (*updates_button, false, false);
446
447                 info_frame.add (*info_box);
448                 info_box->show_all ();
449                 info_frame.show ();
450         }
451
452         /* recent session scroller */
453         setup_recent_sessions ();
454
455         recent_label.set_no_show_all (true);
456         recent_scroller.set_no_show_all (true);
457
458         recent_label.set_markup (string_compose ("<span weight=\"bold\" size=\"large\">%1</span>", _("Recent Sessions")));
459
460         centering_vbox->pack_start (recent_label, false, false, 12);
461         centering_vbox->pack_start (recent_scroller, true, true);
462
463         /* Browse button */
464
465         existing_session_chooser.set_title (_("Select session file"));
466         existing_session_chooser.signal_file_set().connect (sigc::mem_fun (*this, &SessionDialog::existing_session_selected));
467         existing_session_chooser.set_current_folder(poor_mans_glob (Config->get_default_session_parent_dir()));
468
469         FileFilter session_filter;
470         session_filter.add_pattern (string_compose(X_("*%1"), ARDOUR::statefile_suffix));
471         session_filter.set_name (string_compose (_("%1 sessions"), PROGRAM_NAME));
472         existing_session_chooser.add_filter (session_filter);
473
474         FileFilter archive_filter;
475         archive_filter.add_pattern (X_("*.tar.xz"));
476         archive_filter.set_name (_("Session Archives"));
477         existing_session_chooser.add_filter (archive_filter);
478
479         existing_session_chooser.set_filter (session_filter);
480
481         Gtkmm2ext::add_volume_shortcuts (existing_session_chooser);
482
483         Label* browse_label = manage (new Label);
484         browse_label->set_markup (string_compose ("<span weight=\"bold\" size=\"large\">%1</span>", _("Other Sessions")));
485
486         centering_vbox->pack_start (*browse_label, false, false, 12);
487         centering_vbox->pack_start (existing_session_chooser, false, false);
488
489         /* --disable plugins UI */
490
491         _disable_plugins.set_label (_("Safe Mode: Disable all Plugins"));
492         _disable_plugins.set_flags (Gtk::CAN_FOCUS);
493         _disable_plugins.set_relief (Gtk::RELIEF_NORMAL);
494         _disable_plugins.set_mode (true);
495         _disable_plugins.set_active (ARDOUR::Session::get_disable_all_loaded_plugins());
496         _disable_plugins.set_border_width(0);
497         _disable_plugins.signal_clicked().connect (sigc::mem_fun (*this, &SessionDialog::disable_plugins_clicked));
498         centering_vbox->pack_start (_disable_plugins, false, false);
499
500         /* pack it all up */
501
502         centering_hbox->pack_start (*centering_vbox, true, true);
503         ic_vbox.pack_start (*centering_hbox, true, true);
504         ic_vbox.show_all ();
505 }
506
507 void
508 SessionDialog::session_selected ()
509 {
510         /* HACK HACK HACK ... change the "Apply" button label
511            to say "Open"
512         */
513
514         Gtk::Widget* tl = ic_vbox.get_toplevel();
515         Gtk::Window* win;
516         if ((win = dynamic_cast<Gtk::Window*>(tl)) != 0) {
517                 /* ::get_default_widget() is not wrapped in gtkmm */
518                 Gtk::Widget* def = wrap (gtk_window_get_default_widget (win->gobj()));
519                 Gtk::Button* button;
520                 if ((button = dynamic_cast<Gtk::Button*>(def)) != 0) {
521                         button->set_label (_("Open"));
522                 }
523         }
524 }
525
526 void
527 SessionDialog::new_session_button_clicked ()
528 {
529         _existing_session_chooser_used = false;
530         recent_session_display.get_selection()->unselect_all ();
531
532         get_vbox()->remove (ic_vbox);
533         get_vbox()->pack_start (session_new_vbox, true, true);
534         back_button->set_sensitive (true);
535         new_name_entry.grab_focus ();
536 }
537
538 bool
539 SessionDialog::back_button_pressed (GdkEventButton*)
540 {
541         get_vbox()->remove (session_new_vbox);
542         back_button->set_sensitive (false);
543         get_vbox()->pack_start (ic_vbox);
544
545         return true;
546 }
547
548 bool
549 SessionDialog::open_button_pressed (GdkEventButton* ev)
550 {
551         if (Gtkmm2ext::Keyboard::modifier_state_equals (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
552                 _disable_plugins.set_active();
553         }
554         response (RESPONSE_ACCEPT);
555         return true;
556 }
557
558 void
559 SessionDialog::populate_session_templates ()
560 {
561         vector<TemplateInfo> templates;
562
563         find_session_templates (templates, true);
564
565         template_model->clear ();
566
567         /* Add Lua Scripts dedicated to session-setup */
568         LuaScriptList& ms (LuaScripting::instance ().scripts (LuaScriptInfo::SessionInit));
569         for (LuaScriptList::const_iterator s = ms.begin(); s != ms.end(); ++s) {
570                 TreeModel::Row row = *(template_model->append ());
571                 row[session_template_columns.name] = (*s)->name;
572                 row[session_template_columns.path] = "urn:ardour:" + (*s)->path;
573                 row[session_template_columns.description] = (*s)->description;
574                 row[session_template_columns.created_with_short] = _("{Factory Template}");
575                 row[session_template_columns.created_with_long] = _("{Factory Template}");
576         }
577
578         /* Add Lua Action Scripts which can also be used for session-setup */
579         LuaScriptList& as (LuaScripting::instance ().scripts (LuaScriptInfo::EditorAction));
580         for (LuaScriptList::const_iterator s = as.begin(); s != as.end(); ++s) {
581                 if (!((*s)->subtype & LuaScriptInfo::SessionSetup)) {
582                         continue;
583                 }
584                 TreeModel::Row row = *(template_model->append ());
585                 row[session_template_columns.name] = (*s)->name;
586                 row[session_template_columns.path] = "urn:ardour:" + (*s)->path;
587                 row[session_template_columns.description] = (*s)->description;
588                 row[session_template_columns.created_with_short] = _("{Factory Template}");
589                 row[session_template_columns.created_with_long] = _("{Factory Template}");
590         }
591
592
593         //Add any "template sessions" found in the user's preferences folder
594         for (vector<TemplateInfo>::iterator x = templates.begin(); x != templates.end(); ++x) {
595                 TreeModel::Row row;
596
597                 row = *(template_model->append ());
598
599                 row[session_template_columns.name] = (*x).name;
600                 row[session_template_columns.path] = (*x).path;
601                 row[session_template_columns.description] = (*x).description;
602                 row[session_template_columns.created_with_long] = (*x).created_with;
603                 row[session_template_columns.created_with_short] = (*x).created_with.substr(0, (*x).created_with.find(" "));
604         }
605
606         //Add an explicit 'Empty Template' item
607         TreeModel::Row row = *template_model->prepend ();
608         row[session_template_columns.name] = (_("Empty Template"));
609         row[session_template_columns.path] = string();
610         row[session_template_columns.description] = _("An empty session with factory default settings.");
611         row[session_template_columns.created_with_short] = _("");
612         row[session_template_columns.created_with_long] = _("");
613
614         //auto-select the first item in the list
615         Gtk::TreeModel::Row first = template_model->children()[0];
616         if(first) {
617                 template_chooser.get_selection()->select(first);
618         }
619 }
620
621 void
622 SessionDialog::setup_new_session_page ()
623 {
624         session_new_vbox.set_border_width (8);
625         session_new_vbox.set_spacing (8);
626
627         Label* name_label = manage (new Label);
628         name_label->set_text (_("Session name:"));
629
630         HBox* name_hbox = manage (new HBox);
631         name_hbox->set_spacing (8);
632         name_hbox->pack_start (*name_label, false, true);
633         name_hbox->pack_start (new_name_entry, true, true);
634
635         //check to see if a session name was provided on command line
636         if (!ARDOUR_COMMAND_LINE::session_name.empty()) {
637                 new_name_entry.set_text  (Glib::path_get_basename (ARDOUR_COMMAND_LINE::session_name));
638                 open_button->set_sensitive (true);
639         }
640
641         new_name_entry.signal_changed().connect (sigc::mem_fun (*this, &SessionDialog::new_name_changed));
642         new_name_entry.signal_activate().connect (sigc::mem_fun (*this, &SessionDialog::new_name_activated));
643
644         //Folder location for the new session
645         Label* new_folder_label = manage (new Label);
646         new_folder_label->set_text (_("Create session folder in:"));
647         HBox* folder_box = manage (new HBox);
648         folder_box->set_spacing (8);
649         folder_box->pack_start (*new_folder_label, false, false);
650         folder_box->pack_start (new_folder_chooser, true, true);
651
652         //determine the text in the new folder selector
653         if (!ARDOUR_COMMAND_LINE::session_name.empty()) {
654                 new_folder_chooser.set_current_folder (poor_mans_glob (Glib::path_get_dirname (ARDOUR_COMMAND_LINE::session_name)));
655         } else if (ARDOUR_UI::instance()->session_loaded) {
656                 // point the new session file chooser at the parent directory of the current session
657                 string session_parent_dir = Glib::path_get_dirname(ARDOUR_UI::instance()->the_session()->path());
658                 new_folder_chooser.set_current_folder (session_parent_dir);
659                 string default_session_folder = poor_mans_glob (Config->get_default_session_parent_dir());
660
661                 try {
662                         /* add_shortcut_folder throws an exception if the folder being added already has a shortcut */
663                         new_folder_chooser.add_shortcut_folder (default_session_folder);
664                 }
665                 catch (Glib::Error & e) {
666                         std::cerr << "new_folder_chooser.add_shortcut_folder (" << default_session_folder << ") threw Glib::Error " << e.what() << std::endl;
667                 }
668         } else {
669                 new_folder_chooser.set_current_folder (poor_mans_glob (Config->get_default_session_parent_dir()));
670         }
671         new_folder_chooser.show ();
672         new_folder_chooser.set_title (_("Select folder for session"));
673         Gtkmm2ext::add_volume_shortcuts (new_folder_chooser);
674
675         //Template & Template Description area
676         HBox* template_hbox = manage (new HBox);
677
678         //if the "template override" is provided, don't give the user any template selections   (?)
679         if ( load_template_override.empty() ) {
680                 template_hbox->set_spacing (8);
681
682                 Gtk::ScrolledWindow *template_scroller = manage (new Gtk::ScrolledWindow());
683                 template_scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
684                 template_scroller->add (template_chooser);
685
686                 Gtk::ScrolledWindow *desc_scroller = manage (new Gtk::ScrolledWindow());
687                 desc_scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
688                 desc_scroller->add (template_desc);
689
690                 template_hbox->pack_start (*template_scroller, true, true);
691
692                 template_desc_frame.set_name (X_("TextHighlightFrame"));
693                 template_desc_frame.add (*desc_scroller);
694                 template_hbox->pack_start (template_desc_frame, true, true);
695         }
696
697         //template_desc is the textview that displays the currently selected template's description
698         template_desc.set_editable (false);
699         template_desc.set_wrap_mode (Gtk::WRAP_WORD);
700         template_desc.set_size_request (300,400);
701         template_desc.set_name (X_("TextOnBackground"));
702         template_desc.set_border_width (6);
703
704         //template_chooser is the treeview showing available templates
705         template_model = TreeStore::create (session_template_columns);
706         template_chooser.set_model (template_model);
707         template_chooser.append_column (_("Template"), session_template_columns.name);
708 #ifdef MIXBUS
709         template_chooser.append_column (_("Created With"), session_template_columns.created_with_short);
710 #endif
711         template_chooser.set_tooltip_column(4); // created_with_long
712         template_chooser.set_headers_visible (true);
713         template_chooser.get_selection()->set_mode (SELECTION_SINGLE);
714         template_chooser.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &SessionDialog::template_row_selected));
715         template_chooser.set_sensitive (true);
716
717         session_new_vbox.pack_start (*template_hbox, true, true);
718         session_new_vbox.pack_start (*folder_box, false, true);
719         session_new_vbox.pack_start (*name_hbox, false, true);
720         session_new_vbox.show_all ();
721 }
722
723 void
724 SessionDialog::new_name_changed ()
725 {
726         if (!new_name_entry.get_text().empty()) {
727                 session_selected ();
728                 open_button->set_sensitive (true);
729         } else {
730                 open_button->set_sensitive (false);
731         }
732 }
733
734 void
735 SessionDialog::new_name_activated ()
736 {
737         response (RESPONSE_ACCEPT);
738 }
739
740 int
741 SessionDialog::redisplay_recent_sessions ()
742 {
743         std::vector<std::string> session_directories;
744         RecentSessionsSorter cmp;
745
746         recent_session_display.set_model (Glib::RefPtr<TreeModel>(0));
747         recent_session_model->clear ();
748
749         ARDOUR::RecentSessions rs;
750         ARDOUR::read_recent_sessions (rs);
751
752         if (rs.empty()) {
753                 recent_session_display.set_model (recent_session_model);
754                 return 0;
755         }
756
757         // sort them alphabetically
758         sort (rs.begin(), rs.end(), cmp);
759
760         for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
761                 session_directories.push_back ((*i).second);
762         }
763
764         int session_snapshot_count = 0;
765
766         for (vector<std::string>::const_iterator i = session_directories.begin(); i != session_directories.end(); ++i)
767         {
768                 std::vector<std::string> state_file_paths;
769
770                 // now get available states for this session
771
772                 get_state_files_in_directory (*i, state_file_paths);
773
774                 vector<string> states;
775                 vector<const gchar*> item;
776                 string dirname = *i;
777
778                 /* remove any trailing / */
779
780                 if (dirname[dirname.length()-1] == '/') {
781                         dirname = dirname.substr (0, dirname.length()-1);
782                 }
783
784                 /* check whether session still exists */
785                 if (!Glib::file_test(dirname.c_str(), Glib::FILE_TEST_EXISTS)) {
786                         /* session doesn't exist */
787                         continue;
788                 }
789
790                 /* now get available states for this session */
791
792                 states = Session::possible_states (dirname);
793
794                 if (states.empty()) {
795                         /* no state file? */
796                         continue;
797                 }
798
799                 std::vector<string> state_file_names(get_file_names_no_extension (state_file_paths));
800
801                 if (state_file_names.empty()) {
802                         continue;
803                 }
804
805                 float sr;
806                 SampleFormat sf;
807                 std::string program_version;
808
809                 std::string state_file_basename;
810
811                 if (state_file_names.size() > 1) {
812                         state_file_basename = Session::get_snapshot_from_instant (dirname);
813                         std::string s = Glib::build_filename (dirname, state_file_basename + statefile_suffix);
814                         if (!Glib::file_test (s, Glib::FILE_TEST_IS_REGULAR)) {
815                                 state_file_basename = "";
816                         }
817                 }
818
819                 if (state_file_basename.empty()) {
820                         state_file_basename = state_file_names.front();
821                 }
822
823                 std::string s = Glib::build_filename (dirname, state_file_basename + statefile_suffix);
824
825                 int err = Session::get_info_from_path (s, sr, sf, program_version);
826                 if (err < 0) {
827                         // XML cannot be parsed, or unsuppored version
828                         continue;
829                 }
830
831                 GStatBuf gsb;
832                 g_stat (s.c_str(), &gsb);
833
834                 Gtk::TreeModel::Row row = *(recent_session_model->append());
835                 row[recent_session_columns.fullpath] = s;
836                 row[recent_session_columns.time_modified] = gsb.st_mtime;
837
838
839                 if (err == 0) {
840                         row[recent_session_columns.sample_rate] = rate_as_string (sr);
841                         switch (sf) {
842                         case FormatFloat:
843                                 row[recent_session_columns.disk_format] = _("32-bit float");
844                                 break;
845                         case FormatInt24:
846                                 row[recent_session_columns.disk_format] = _("24-bit");
847                                 break;
848                         case FormatInt16:
849                                 row[recent_session_columns.disk_format] = _("16-bit");
850                                 break;
851                         }
852                 } else {
853                         row[recent_session_columns.sample_rate] = "??";
854                         row[recent_session_columns.disk_format] = "--";
855                 }
856
857                 if (program_version.empty()) {
858                         row[recent_session_columns.tip] = Gtkmm2ext::markup_escape_text (dirname);
859                 } else {
860                         row[recent_session_columns.tip] = Gtkmm2ext::markup_escape_text (dirname + "\n" + string_compose (_("Last modified with: %1"), program_version));
861                 }
862
863                 ++session_snapshot_count;
864
865                 if (state_file_names.size() > 1) {
866                         // multiple session files in the session directory - show the directory name.
867                         // if there's not a session file with the same name as the session directory,
868                         // opening the parent item will fail, but expanding it will show the session
869                         // files that actually exist, and the right one can then be opened.
870                         row[recent_session_columns.visible_name] = Glib::path_get_basename (dirname);
871                         int64_t most_recent = 0;
872
873                         // add the children
874                         int kidcount = 0;
875                         for (std::vector<std::string>::iterator i2 = state_file_names.begin(); i2 != state_file_names.end(); ++i2) {
876
877                                 s = Glib::build_filename (dirname, *i2 + statefile_suffix);
878                                 Gtk::TreeModel::Row child_row = *(recent_session_model->append (row.children()));
879
880                                 child_row[recent_session_columns.visible_name] = *i2;
881                                 child_row[recent_session_columns.fullpath] = s;
882                                 child_row[recent_session_columns.tip] = Gtkmm2ext::markup_escape_text (dirname);
883                                 g_stat (s.c_str(), &gsb);
884                                 child_row[recent_session_columns.time_modified] = gsb.st_mtime;
885
886                                 Glib::DateTime gdt(Glib::DateTime::create_now_local (gsb.st_mtime));
887                                 child_row[recent_session_columns.time_formatted] = gdt.format ("%F %H:%M");
888
889                                 if (gsb.st_mtime > most_recent) {
890                                         most_recent = gsb.st_mtime;
891                                 }
892
893                                 if (++kidcount < 5) {
894                                         // parse "modified with" for the first 5 snapshots
895                                         if (Session::get_info_from_path (s, sr, sf, program_version) == 0) {
896 #if 0
897                                                 child_row[recent_session_columns.sample_rate] = rate_as_string (sr);
898                                                 switch (sf) {
899                                                 case FormatFloat:
900                                                         child_row[recent_session_columns.disk_format] = _("32-bit float");
901                                                         break;
902                                                 case FormatInt24:
903                                                         child_row[recent_session_columns.disk_format] = _("24-bit");
904                                                         break;
905                                                 case FormatInt16:
906                                                         child_row[recent_session_columns.disk_format] = _("16-bit");
907                                                         break;
908                                                 }
909 #else
910                                                 child_row[recent_session_columns.sample_rate] = "";
911                                                 child_row[recent_session_columns.disk_format] = "";
912 #endif
913                                         } else {
914                                                 child_row[recent_session_columns.sample_rate] = "??";
915                                                 child_row[recent_session_columns.disk_format] = "--";
916                                         }
917                                         if (!program_version.empty()) {
918                                                 child_row[recent_session_columns.tip] = Gtkmm2ext::markup_escape_text (string_compose (_("Last modified with: %1"), program_version));
919                                         }
920                                 } else {
921                                         child_row[recent_session_columns.sample_rate] = "";
922                                         child_row[recent_session_columns.disk_format] = "";
923                                 }
924
925                                 ++session_snapshot_count;
926                         }
927
928                         assert (most_recent >= row[recent_session_columns.time_modified]);
929                         row[recent_session_columns.time_modified] = most_recent;
930
931                 } else {
932                         // only a single session file in the directory - show its actual name.
933                         row[recent_session_columns.visible_name] = state_file_basename;
934                 }
935
936                 Glib::DateTime gdt(Glib::DateTime::create_now_local (row[recent_session_columns.time_modified]));
937                 row[recent_session_columns.time_formatted] = gdt.format ("%F %H:%M");
938         }
939
940         recent_session_display.set_tooltip_column(1); // recent_session_columns.tip
941         recent_session_display.set_model (recent_session_model);
942
943         // custom sort
944         Gtk::TreeView::Column* pColumn;
945         if ((pColumn = recent_session_display.get_column (0))) { // name
946                 pColumn->set_sort_column (recent_session_columns.visible_name);
947         }
948         if ((pColumn = recent_session_display.get_column (3))) { // date
949                 pColumn->set_sort_column (recent_session_columns.time_modified); // unixtime
950         }
951
952         int32_t sort = UIConfiguration::instance().get_recent_session_sort();
953         if (abs(sort) != 1 + recent_session_columns.visible_name.index () &&
954             abs(sort) != 1 + recent_session_columns.time_modified.index ()) {
955                 sort = 1 + recent_session_columns.visible_name.index();
956         }
957         recent_session_model->set_sort_column (abs (sort) -1, sort < 0 ? Gtk::SORT_DESCENDING : Gtk::SORT_ASCENDING);
958
959         return session_snapshot_count;
960 }
961
962 void
963 SessionDialog::recent_session_sort_changed ()
964 {
965         int column;
966         SortType order;
967         if (recent_session_model->get_sort_column_id (column, order)) {
968                 int32_t sort = (column + 1) * (order == Gtk::SORT_DESCENDING ? -1 : 1);
969                 if (sort != UIConfiguration::instance().get_recent_session_sort()) {
970                         UIConfiguration::instance().set_recent_session_sort(sort);
971                 }
972         }
973 }
974
975 void
976 SessionDialog::recent_session_row_selected ()
977 {
978         if (recent_session_display.get_selection()->count_selected_rows() > 0) {
979                 open_button->set_sensitive (true);
980                 session_selected ();
981         } else {
982                 open_button->set_sensitive (false);
983         }
984 }
985
986 void
987 SessionDialog::template_row_selected ()
988 {
989         if (template_chooser.get_selection()->count_selected_rows() > 0) {
990                 TreeIter iter = template_chooser.get_selection()->get_selected();
991
992                 if (iter) {
993                         string s = (*iter)[session_template_columns.description];
994                         template_desc.get_buffer()->set_text (s);
995                 }
996         }
997 }
998
999 void
1000 SessionDialog::recent_row_activated (const Gtk::TreePath&, Gtk::TreeViewColumn*)
1001 {
1002         response (RESPONSE_ACCEPT);
1003 }
1004
1005 bool
1006 SessionDialog::recent_button_press (GdkEventButton* ev)
1007 {
1008         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 3) ) {
1009
1010                 TreeModel::Path path;
1011                 TreeViewColumn* column;
1012                 int cellx, celly;
1013                 if (recent_session_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1014                         Glib::RefPtr<Gtk::TreeView::Selection> selection = recent_session_display.get_selection();
1015                         if (selection) {
1016                                 selection->unselect_all();
1017                                 selection->select(path);
1018                         }
1019                 }
1020
1021                 if (recent_session_display.get_selection()->count_selected_rows() > 0) {
1022                         recent_context_mennu (ev);
1023                 }
1024         }
1025         return false;
1026 }
1027
1028 void
1029 SessionDialog::recent_context_mennu (GdkEventButton *ev)
1030 {
1031         using namespace Gtk::Menu_Helpers;
1032
1033         TreeIter iter = recent_session_display.get_selection()->get_selected();
1034         assert (iter);
1035         string s = (*iter)[recent_session_columns.fullpath];
1036         if (Glib::file_test (s, Glib::FILE_TEST_IS_REGULAR)) {
1037                 s = Glib::path_get_dirname (s);
1038         }
1039         if (!Glib::file_test (s, Glib::FILE_TEST_IS_DIR)) {
1040                 return;
1041         }
1042
1043         Gtk::TreeModel::Path tpath = recent_session_model->get_path(iter);
1044         const bool is_child = tpath.up () && tpath.up ();
1045
1046         Gtk::Menu* m = manage (new Menu);
1047         MenuList& items = m->items ();
1048         items.push_back (MenuElem (s, sigc::bind (sigc::hide_return (sigc::ptr_fun (&PBD::open_folder)), s)));
1049         if (!is_child) {
1050                 items.push_back (SeparatorElem());
1051                 items.push_back (MenuElem (_("Remove session from recent list"), sigc::mem_fun (*this, &SessionDialog::recent_remove_selected)));
1052         }
1053         m->popup (ev->button, ev->time);
1054 }
1055
1056 void
1057 SessionDialog::recent_remove_selected ()
1058 {
1059         TreeIter iter = recent_session_display.get_selection()->get_selected();
1060         assert (iter);
1061         string s = (*iter)[recent_session_columns.fullpath];
1062         if (Glib::file_test (s, Glib::FILE_TEST_IS_REGULAR)) {
1063                 s = Glib::path_get_dirname (s);
1064         }
1065         ARDOUR::remove_recent_sessions (s);
1066         redisplay_recent_sessions ();
1067 }
1068
1069 void
1070 SessionDialog::disable_plugins_clicked ()
1071 {
1072         ARDOUR::Session::set_disable_all_loaded_plugins (_disable_plugins.get_active());
1073 }
1074
1075 void
1076 SessionDialog::existing_session_selected ()
1077 {
1078         _existing_session_chooser_used = true;
1079         recent_session_display.get_selection()->unselect_all();
1080         /* mark this sensitive in case we come back here after a failed open
1081          * attempt and the user has hacked up the fix. sigh.
1082          */
1083         open_button->set_sensitive (true);
1084         response (RESPONSE_ACCEPT);
1085 }
1086
1087 void
1088 SessionDialog::updates_button_clicked ()
1089 {
1090         //now open a browser window so user can see more
1091         PBD::open_uri (Config->get_updates_url());
1092 }
1093
1094 bool
1095 SessionDialog::info_scroller_update()
1096 {
1097         info_scroller_count++;
1098
1099         char buf[512];
1100         snprintf (buf, std::min(info_scroller_count,sizeof(buf)-1), "%s", ARDOUR_UI::instance()->announce_string().c_str() );
1101         buf[info_scroller_count] = 0;
1102         info_scroller_label.set_text (buf);
1103         info_scroller_label.show();
1104
1105         if (info_scroller_count > ARDOUR_UI::instance()->announce_string().length()) {
1106                 info_scroller_connection.disconnect();
1107         }
1108
1109         return true;
1110 }
1111
1112 bool
1113 SessionDialog::on_delete_event (GdkEventAny* ev)
1114 {
1115         response (RESPONSE_CANCEL);
1116         return ArdourDialog::on_delete_event (ev);
1117 }