0675bb2f93762d57e67786b47bd5a468476e8edf
[ardour.git] / gtk2_ardour / startup.cc
1 #include <algorithm>
2
3 #include <gtkmm/main.h>
4 #include <gtkmm/filechooser.h>
5
6 #include "pbd/failed_constructor.h"
7 #include "pbd/file_utils.h"
8 #include "pbd/filesystem.h"
9
10 #include "ardour/filesystem_paths.h"
11 #include "ardour/recent_sessions.h"
12 #include "ardour/session.h"
13 #include "ardour/session_state_utils.h"
14 #include "ardour/template_utils.h"
15
16 #include "startup.h"
17 #include "i18n.h"
18
19 using namespace std;
20 using namespace Gtk;
21 using namespace Gdk;
22 using namespace Glib;
23 using namespace PBD;
24 using namespace ARDOUR;
25
26 ArdourStartup* ArdourStartup::the_startup = 0;
27
28 ArdourStartup::ArdourStartup ()
29         : applying (false)
30         , ic_new_session_button (_("Open a new session"))
31         , ic_existing_session_button (_("Open an existing session"))
32         , more_new_session_options_button (_("I'd like more options for this session"))
33         , new_folder_chooser (FILE_CHOOSER_ACTION_SELECT_FOLDER)
34 {
35         set_keep_above (true);
36         set_position (WIN_POS_CENTER);
37
38         sys::path icon_file;
39
40         if (!find_file_in_search_path (ardour_search_path() + system_data_search_path(), "ardour_icon_48px.png", icon_file)) {
41                 throw failed_constructor();
42         }
43
44         try {
45                 icon_pixbuf = Gdk::Pixbuf::create_from_file (icon_file.to_string());
46         }
47
48         catch (...) {
49                 throw failed_constructor();
50         }
51
52         sys::path been_here_before = user_config_directory();
53         been_here_before /= ".a3"; // XXXX use more specific version so we can catch upgrades
54         
55         if (!exists (been_here_before)) {
56                 // XXX touch been_here_before;
57                 setup_new_user_page ();
58                 setup_first_time_config_page ();
59         } else {
60                 setup_initial_choice_page ();
61         }
62
63         setup_session_page ();
64         setup_more_options_page ();
65         setup_final_page ();
66
67         the_startup = this;
68 }
69
70 ArdourStartup::~ArdourStartup ()
71 {
72 }
73
74 void
75 ArdourStartup::setup_new_user_page ()
76 {
77         Label* foomatic = manage (new Label (_("\
78 Ardour is a digital audio workstation. You can use it to\n\
79 record, edit and mix multi-track audio. You can produce your\n\
80 own CDs, mix video soundtracks, or just experiment with new\n\
81 ideas about music and sound.\n\
82 \n\
83 There are a few things that need to configured before you start\n\
84 using the program.\
85 ")));
86         
87         HBox* hbox = manage (new HBox);
88         HBox* vbox = manage (new HBox);
89
90         hbox->set_border_width (12);
91         vbox->set_border_width (12);
92
93         hbox->pack_start (*foomatic, false, true);
94         vbox->pack_start (*hbox, false, true);
95
96         foomatic->show ();
97         hbox->show ();
98         vbox->show ();
99
100         append_page (*vbox);
101         set_page_type (*vbox, ASSISTANT_PAGE_INTRO);
102         set_page_title (*vbox, _("Welcome to Ardour"));
103         set_page_header_image (*vbox, icon_pixbuf);
104         set_page_complete (*vbox, true);
105 }
106
107 void
108 ArdourStartup::setup_first_time_config_page ()
109 {
110         Gtk::FileChooserButton* fcb = manage (new FileChooserButton (_("Default session folder"), FILE_CHOOSER_ACTION_SELECT_FOLDER));
111         Gtk::Label* txt = manage (new Label);
112         HBox* hbox1 = manage (new HBox);
113         VBox* vbox = manage (new VBox);
114         
115         txt->set_markup (_("\
116 Each project that you work on with Ardour has its own folder.\n\
117 These can require a lot of disk space if you are recording audio.\n\
118 \n\
119 Where would you like new Ardour sessions to be stored by default?\n\
120 <i>(You can put new sessions anywhere - this is just a default)</i>"));
121
122         hbox1->set_border_width (6);
123         vbox->set_border_width (6);
124
125         hbox1->pack_start (*fcb, false, true);
126         vbox->pack_start (*txt, false, true);
127         vbox->pack_start (*hbox1, false, true);
128
129         fcb->show ();
130         txt->show ();
131         hbox1->show ();
132         vbox->show ();
133
134         append_page (*vbox);
135         set_page_title (*vbox, _("Default folder for new sessions"));
136         set_page_header_image (*vbox, icon_pixbuf);
137         set_page_type (*vbox, ASSISTANT_PAGE_CONTENT);
138
139         /* user can just skip all these settings if they want to */
140
141         set_page_complete (*vbox, true);
142 }
143
144 void
145 ArdourStartup::setup_initial_choice_page ()
146 {
147         ic_vbox.set_spacing (6);
148         ic_vbox.set_border_width (6);
149
150         RadioButton::Group g (ic_new_session_button.get_group());
151         ic_existing_session_button.set_group (g);
152
153         ic_vbox.pack_start (ic_new_session_button);
154         ic_vbox.pack_start (ic_existing_session_button);
155
156         ic_new_session_button.show ();
157         ic_existing_session_button.show ();
158         ic_vbox.show ();
159
160         append_page (ic_vbox);
161         set_page_title (ic_vbox, _("What would you like to do?"));
162         set_page_header_image (ic_vbox, icon_pixbuf);
163
164         /* user could just click on "Forward" if default
165          * choice is correct.
166          */
167
168         set_page_complete (ic_vbox, true);
169 }
170
171 void
172 ArdourStartup::setup_session_page ()
173 {
174         session_hbox.set_border_width (12);
175         session_vbox.set_border_width (12);
176
177         session_vbox.pack_start (session_hbox, true, true);
178         session_vbox.show ();
179         session_hbox.show ();
180
181         append_page (session_vbox);
182 }
183
184 void
185 ArdourStartup::setup_final_page ()
186 {
187         final_page.set_text ("Ardour is ready for use");
188         final_page.show ();
189         append_page (final_page);
190         set_page_complete (final_page, true);
191         set_page_header_image (final_page, icon_pixbuf);
192         set_page_type (final_page, ASSISTANT_PAGE_CONFIRM);
193 }
194
195 void
196 ArdourStartup::on_cancel ()
197 {
198         exit (1);
199 }
200
201 void
202 ArdourStartup::on_close ()
203 {
204         if (!applying) {
205                 exit (1);
206         }
207 }
208
209 void
210 ArdourStartup::on_apply ()
211 {
212         applying = true;
213
214         // XXX do stuff and then ....
215
216         gtk_main_quit ();
217 }
218
219 void
220 ArdourStartup::on_prepare (Gtk::Widget* page)
221 {
222         if (page == &session_vbox) {
223                 if (ic_new_session_button.get_active()) {
224                         /* new session requested */
225                         setup_new_session_page ();
226                 } else {
227                         /* existing session requested */
228                         setup_existing_session_page ();
229                 }
230         }
231 }
232
233 void
234 ArdourStartup::setup_new_session_page ()
235 {
236         if (!session_hbox.get_children().empty()) {
237                 session_hbox.remove (**session_hbox.get_children().begin());
238         }
239
240         if (session_new_vbox.get_children().empty()) {
241                 
242                 HBox* hbox1 = manage (new HBox);
243                 Label* label1 = manage (new Label);
244                 
245                 hbox1->set_spacing (6);
246                 hbox1->pack_start (*label1, false, false);
247                 hbox1->pack_start (new_name_entry, true, true);
248                 
249                 label1->set_text (_("Session name:"));
250                 
251                 hbox1->show();
252                 label1->show();
253                 new_name_entry.show ();
254                 
255                 new_name_entry.signal_changed().connect (mem_fun (*this, &ArdourStartup::new_name_changed));
256                 
257                 HBox* hbox2 = manage (new HBox);
258                 Label* label2 = manage (new Label);
259                 
260                 hbox2->set_spacing (6);
261                 hbox2->pack_start (*label2, false, false);
262                 hbox2->pack_start (new_folder_chooser, true, true);
263                 
264                 label2->set_text (_("Create session folder in:"));
265                 new_folder_chooser.set_current_folder(getenv ("HOME"));
266                 new_folder_chooser.set_title (_("Select folder for session"));
267                 
268                 hbox2->show();
269                 label2->show();
270                 new_folder_chooser.show ();
271                 
272                 if (is_directory (user_template_directory ())) {
273                         session_template_chooser.set_current_folder (user_template_directory().to_string());
274                 } else if (is_directory (system_template_directory ())) {
275                         session_template_chooser.set_current_folder (system_template_directory().to_string());
276                 } else {
277                         /* hmm, no templates ... what to do? */
278                 }
279                 
280                 if (is_directory (system_template_directory ())) {
281                         session_template_chooser.add_shortcut_folder (system_template_directory().to_string());
282                 }
283                 
284                 HBox* hbox3 = manage (new HBox);
285                 Label* label3 = manage (new Label);
286                 
287                 hbox3->set_spacing (6);
288                 hbox3->pack_start (*label3, false, false);
289                 hbox3->pack_start (session_template_chooser, true, true);
290                 
291                 label3->set_text (_("Use this template:"));
292                 
293                 hbox3->show ();
294                 label3->show ();
295                 session_template_chooser.show ();
296                 
297                 Gtk::FileFilter* template_filter = manage (new (Gtk::FileFilter));
298                 template_filter->add_pattern(X_("*.template"));
299                 session_template_chooser.set_filter (*template_filter);
300                 session_template_chooser.set_title (_("Select template"));
301                 
302                 
303                 HBox* hbox4 = manage (new HBox);
304         
305                 hbox4->set_spacing (6);
306                 hbox4->pack_start (more_new_session_options_button, false, false);
307                 
308                 hbox4->show ();
309                 more_new_session_options_button.show ();
310                 more_new_session_options_button.signal_clicked().connect (mem_fun (*this, &ArdourStartup::more_new_session_options_button_clicked));
311                 session_new_vbox.set_spacing (12);
312         
313                 session_new_vbox.pack_start (*hbox1, false, false);
314                 session_new_vbox.pack_start (*hbox2, false, false);
315                 session_new_vbox.pack_start (*hbox3, false, false);
316                 session_new_vbox.pack_start (*hbox4, false, false);
317         }
318
319         session_new_vbox.show ();
320         session_hbox.pack_start (session_new_vbox, false, false);
321         set_page_title (session_vbox, _("New Session"));
322 }
323
324 void
325 ArdourStartup::new_name_changed ()
326 {
327         if (!new_name_entry.get_text().empty()) {
328                 set_page_complete (session_vbox, true);
329         } else {
330                 set_page_complete (session_vbox, false);
331         }
332 }
333
334 void
335 ArdourStartup::redisplay_recent_sessions ()
336 {
337         std::vector<sys::path> session_directories;
338         RecentSessionsSorter cmp;
339         
340         recent_session_display.set_model (Glib::RefPtr<TreeModel>(0));
341         recent_session_model->clear ();
342
343         ARDOUR::RecentSessions rs;
344         ARDOUR::read_recent_sessions (rs);
345
346         if (rs.empty()) {
347                 recent_session_display.set_model (recent_session_model);
348                 return;
349         }
350         //
351         // sort them alphabetically
352         sort (rs.begin(), rs.end(), cmp);
353         
354         for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
355                 session_directories.push_back ((*i).second);
356         }
357         
358         for (vector<sys::path>::const_iterator i = session_directories.begin();
359                         i != session_directories.end(); ++i)
360         {
361                 std::vector<sys::path> state_file_paths;
362             
363                 // now get available states for this session
364
365                 get_state_files_in_directory (*i, state_file_paths);
366
367                 vector<string*>* states;
368                 vector<const gchar*> item;
369                 string fullpath = (*i).to_string();
370                 
371                 /* remove any trailing / */
372
373                 if (fullpath[fullpath.length()-1] == '/') {
374                         fullpath = fullpath.substr (0, fullpath.length()-1);
375                 }
376
377                 /* check whether session still exists */
378                 if (!Glib::file_test(fullpath.c_str(), Glib::FILE_TEST_EXISTS)) {
379                         /* session doesn't exist */
380                         cerr << "skipping non-existent session " << fullpath << endl;
381                         continue;
382                 }               
383                 
384                 /* now get available states for this session */
385
386                 if ((states = Session::possible_states (fullpath)) == 0) {
387                         /* no state file? */
388                         continue;
389                 }
390           
391                 std::vector<string> state_file_names(get_file_names_no_extension (state_file_paths));
392
393                 Gtk::TreeModel::Row row = *(recent_session_model->append());
394
395                 row[recent_session_columns.visible_name] = Glib::path_get_basename (fullpath);
396                 row[recent_session_columns.fullpath] = fullpath;
397                 
398                 if (state_file_names.size() > 1) {
399
400                         // add the children
401
402                         for (std::vector<std::string>::iterator i2 = state_file_names.begin();
403                                         i2 != state_file_names.end(); ++i2)
404                         {
405
406                                 Gtk::TreeModel::Row child_row = *(recent_session_model->append (row.children()));
407
408                                 child_row[recent_session_columns.visible_name] = *i2;
409                                 child_row[recent_session_columns.fullpath] = fullpath;
410                         }
411                 }
412         }
413
414         recent_session_display.set_model (recent_session_model);
415 }
416
417 void
418 ArdourStartup::recent_session_row_selected ()
419 {
420         if (recent_session_display.get_selection()->count_selected_rows() > 0) {
421                 set_page_complete (session_vbox, true);
422         } else {
423                 set_page_complete (session_vbox, false);
424         }
425 }
426
427 void
428 ArdourStartup::setup_existing_session_page ()
429 {
430         if (!session_hbox.get_children().empty()) {
431                 session_hbox.remove (**session_hbox.get_children().begin());
432         }
433
434         if (recent_scroller.get_children().empty()) {
435
436                 recent_session_model = TreeStore::create (recent_session_columns);
437                 recent_session_display.set_model (recent_session_model);
438                 recent_session_display.append_column (_("Recent Sessions"), recent_session_columns.visible_name);
439                 recent_session_display.set_headers_visible (false);
440                 recent_session_display.get_selection()->set_mode (SELECTION_BROWSE);
441                 
442                 recent_session_display.get_selection()->signal_changed().connect (mem_fun (*this, &ArdourStartup::recent_session_row_selected));
443                 
444                 recent_scroller.add (recent_session_display);
445                 recent_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
446                 
447                 recent_session_display.show();
448         }
449
450         recent_scroller.show();
451         redisplay_recent_sessions ();
452
453         session_hbox.pack_start (recent_scroller, true, true);
454         set_page_title (session_vbox, _("Select a session"));
455         set_page_type (session_vbox, ASSISTANT_PAGE_CONFIRM);
456 }
457
458 void
459 ArdourStartup::more_new_session_options_button_clicked ()
460 {
461         if (more_new_session_options_button.get_active()) {
462                 more_options_vbox.show ();
463         } else {
464                 more_options_vbox.hide ();
465         }
466 }
467
468 void
469 ArdourStartup::setup_more_options_page ()
470 {
471         Label* foomatic = manage (new Label);
472         foomatic->set_text (_("Here be more options...."));
473         foomatic->show ();
474
475         more_options_vbox.set_border_width (12);
476         more_options_hbox.set_border_width (12);
477         
478         more_options_hbox.pack_start (*foomatic, true, true);
479         more_options_vbox.pack_start (more_options_hbox, true, true);
480
481         more_options_hbox.show ();
482
483         /* note that more_options_vbox is NOT visible by
484          * default. this is entirely by design - this page
485          * should be skipped unless explicitly requested.
486          */
487         
488         append_page (more_options_vbox);
489         set_page_title (more_options_vbox, _("Advanced Session Options"));
490         set_page_complete (more_options_vbox, true);
491 }