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