Reenable the correct sort column and type when redisplaying regions
[ardour.git] / gtk2_ardour / export_file_notebook.cc
1 /*
2  * Copyright (C) 2008-2012 Sakari Bergen <sakari.bergen@beatwaves.net>
3  * Copyright (C) 2009-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009 David Robillard <d@drobilla.net>
5  * Copyright (C) 2012-2016 Tim Mayberry <mojofunk@gmail.com>
6  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
7  * Copyright (C) 2014 Colin Fletcher <colin.m.fletcher@googlemail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "export_file_notebook.h"
25
26 #include "ardour/export_format_specification.h"
27
28 #include "gui_thread.h"
29 #include "utils.h"
30 #include "pbd/i18n.h"
31
32 using namespace ARDOUR;
33 using namespace ARDOUR_UI_UTILS;
34 using namespace PBD;
35
36 ExportFileNotebook::ExportFileNotebook () :
37   page_counter (1)
38 {
39         /* Last page */
40
41         new_file_button.set_image (*Gtk::manage (new Gtk::Image (::get_icon("add"))));
42         new_file_button.set_label (_("Add another format"));
43         new_file_button.set_alignment (0, 0.5);
44         new_file_button.set_relief (Gtk::RELIEF_NONE);
45
46         new_file_hbox.pack_start (new_file_button, true, true);
47         append_page (new_file_dummy, new_file_hbox);
48         set_tab_label_packing (new_file_dummy, true, true, Gtk::PACK_START);
49         new_file_hbox.show_all_children ();
50
51         page_change_connection = signal_switch_page().connect (sigc::mem_fun (*this, &ExportFileNotebook::handle_page_change));
52         new_file_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFileNotebook::add_new_file_page));
53 }
54
55 void
56 ExportFileNotebook::set_session_and_manager (ARDOUR::Session * s, boost::shared_ptr<ARDOUR::ExportProfileManager> manager)
57 {
58         SessionHandlePtr::set_session (s);
59         profile_manager = manager;
60
61         sync_with_manager ();
62 }
63
64 void
65 ExportFileNotebook::sync_with_manager ()
66 {
67         /* Clear pages from notebook
68            The page switch handling has to be disabled during removing all pages due to a gtk bug
69          */
70
71         page_change_connection.block();
72         while (get_n_pages() > 1) {
73                 remove_page (0);
74         }
75         page_change_connection.block(false);
76
77         page_counter = 1;
78         last_visible_page = 0;
79
80         /* File notebook */
81
82         ExportProfileManager::FormatStateList const & formats = profile_manager->get_formats ();
83         ExportProfileManager::FormatStateList::const_iterator format_it;
84         ExportProfileManager::FilenameStateList const & filenames = profile_manager->get_filenames ();
85         ExportProfileManager::FilenameStateList::const_iterator filename_it;
86         for (format_it = formats.begin(), filename_it = filenames.begin();
87              format_it != formats.end() && filename_it != filenames.end();
88              ++format_it, ++filename_it) {
89                 add_file_page (*format_it, *filename_it);
90         }
91
92         set_current_page (0);
93         update_soundcloud_upload ();
94         CriticalSelectionChanged ();
95 }
96
97 void
98 ExportFileNotebook::update_soundcloud_upload ()
99 {
100         int i;
101         bool show_credentials_entry = false;
102         ExportProfileManager::FormatStateList const & formats = profile_manager->get_formats ();
103         ExportProfileManager::FormatStateList::const_iterator format_it;
104
105         for (i = 0, format_it = formats.begin(); format_it != formats.end(); ++i, ++format_it) {
106                 FilePage * page;
107                 if ((page = dynamic_cast<FilePage *> (get_nth_page (i)))) {
108                         bool this_soundcloud_upload = page->get_soundcloud_upload ();
109                         (*format_it)->format->set_soundcloud_upload (this_soundcloud_upload);
110                         if (this_soundcloud_upload) {
111                                 show_credentials_entry  = true;
112                         }
113                 }
114         }
115         soundcloud_export_selector->set_visible (show_credentials_entry);
116 }
117
118 void
119 ExportFileNotebook::FilePage::analysis_changed ()
120 {
121         format_state->format->set_analyse (analysis_button.get_active ());
122         profile_manager->save_format_to_disk (format_state->format);
123 }
124
125 void
126 ExportFileNotebook::FilePage::update_analysis_button ()
127 {
128         analysis_button.set_active (format_state->format->analyse());
129 }
130
131 void
132 ExportFileNotebook::update_example_filenames ()
133 {
134         int i = 0;
135         FilePage * page;
136         while ((page = dynamic_cast<FilePage *> (get_nth_page (i++)))) {
137                 page->update_example_filename();
138         }
139 }
140
141 void
142 ExportFileNotebook::add_new_file_page ()
143 {
144         FilePage * page;
145         if ((page = dynamic_cast<FilePage *> (get_nth_page (get_current_page())))) {
146                 add_file_page (profile_manager->duplicate_format_state (page->get_format_state()),
147                                profile_manager->duplicate_filename_state (page->get_filename_state()));
148         }
149 }
150
151 void
152 ExportFileNotebook::add_file_page (ARDOUR::ExportProfileManager::FormatStatePtr format_state, ARDOUR::ExportProfileManager::FilenameStatePtr filename_state)
153 {
154         FilePage * page = Gtk::manage (new FilePage (_session, profile_manager, this, page_counter, format_state, filename_state));
155         page->CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
156         insert_page (*page, page->get_tab_widget(), get_n_pages() - 1);
157
158         update_remove_file_page_sensitivity ();
159         show_all_children();
160         ++page_counter;
161
162         CriticalSelectionChanged ();
163 }
164
165 void
166 ExportFileNotebook::remove_file_page (FilePage * page)
167 {
168         profile_manager->remove_format_state (page->get_format_state());
169         profile_manager->remove_filename_state (page->get_filename_state());
170
171         remove_page (*page);
172         update_remove_file_page_sensitivity ();
173
174         CriticalSelectionChanged ();
175 }
176
177 void
178 ExportFileNotebook::update_remove_file_page_sensitivity ()
179 {
180         FilePage * page;
181         if ((page = dynamic_cast<FilePage *> (get_nth_page (0)))) {
182                 if (get_n_pages() > 2) {
183                         page->set_remove_sensitive (true);
184                 } else {
185                         page->set_remove_sensitive (false);
186                 }
187         }
188 }
189
190 void
191 ExportFileNotebook::handle_page_change (GtkNotebookPage*, uint32_t page)
192 {
193         if (page + 1 == (uint32_t) get_n_pages()) {
194                 set_current_page (last_visible_page);
195         } else {
196                 last_visible_page = page;
197         }
198         update_soundcloud_upload ();
199 }
200
201 ExportFileNotebook::FilePage::FilePage (Session * s, ManagerPtr profile_manager, ExportFileNotebook * parent, uint32_t number,
202                                         ExportProfileManager::FormatStatePtr format_state,
203                                         ExportProfileManager::FilenameStatePtr filename_state) :
204         format_state (format_state),
205         filename_state (filename_state),
206         profile_manager (profile_manager),
207
208         format_label (_("Format"), Gtk::ALIGN_LEFT),
209         filename_label (_("Location"), Gtk::ALIGN_LEFT),
210         soundcloud_upload_button (_("Upload to Soundcloud")),
211         analysis_button (_("Analyze Exported Audio")),
212         tab_number (number)
213 {
214         set_border_width (12);
215
216         pack_start (format_label, false, false, 0);
217         pack_start (format_align, false, false, 0);
218         pack_start (filename_label, false, false, 0);
219         pack_start (filename_align, false, false, 0);
220
221         Gtk::HBox *hbox = Gtk::manage (new Gtk::HBox());
222         hbox->set_spacing (6);
223 #ifndef NDEBUG // SoundCloud upload is currently b0rked, needs debugging
224         hbox->pack_start (soundcloud_upload_button, false, false, 0);
225 #endif
226         hbox->pack_start (analysis_button, false, false, 0);
227         pack_start (*hbox, false, false, 0);
228
229         format_align.add (format_selector);
230         format_align.set_padding (6, 12, 18, 0);
231
232         filename_align.add (filename_selector);
233         filename_align.set_padding (0, 12, 18, 0);
234
235         Pango::AttrList bold;
236         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
237         bold.insert (b);
238
239         format_label.set_attributes (bold);
240         filename_label.set_attributes (bold);
241         tab_label.set_attributes (bold);
242
243         /* Set states */
244         format_selector.set_state (format_state, s);
245         filename_selector.set_state (filename_state, s);
246         analysis_button.set_active (format_state->format->analyse());
247         soundcloud_upload_button.set_active (format_state->format->soundcloud_upload());
248
249         /* Signals */
250
251         tab_close_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*parent, &ExportFileNotebook::remove_file_page), this));
252
253         profile_manager->FormatListChanged.connect (format_connection, invalidator (*this), boost::bind (&ExportFormatSelector::update_format_list, &format_selector), gui_context());
254
255         format_selector.FormatEdited.connect (sigc::mem_fun (*this, &ExportFileNotebook::FilePage::save_format_to_manager));
256         format_selector.FormatRemoved.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::remove_format_profile));
257         format_selector.NewFormat.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::get_new_format));
258         format_selector.FormatReverted.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::revert_format_profile));
259
260         format_selector.CriticalSelectionChanged.connect (
261                 sigc::mem_fun (*this, &ExportFileNotebook::FilePage::critical_selection_changed));
262         filename_selector.CriticalSelectionChanged.connect (
263                 sigc::mem_fun (*this, &ExportFileNotebook::FilePage::critical_selection_changed));
264
265         soundcloud_upload_button.signal_toggled().connect (sigc::mem_fun (*parent, &ExportFileNotebook::update_soundcloud_upload));
266         soundcloud_button_connection = soundcloud_upload_button.signal_toggled().connect (sigc::mem_fun (*this, &ExportFileNotebook::FilePage::soundcloud_upload_changed));
267         analysis_button_connection = analysis_button.signal_toggled().connect (sigc::mem_fun (*this, &ExportFileNotebook::FilePage::analysis_changed));
268         /* Tab widget */
269
270         tab_close_button.add (*Gtk::manage (new Gtk::Image (::get_icon("close"))));
271         tab_close_alignment.add (tab_close_button);
272         tab_close_alignment.set (0.5, 0.5, 0, 0);
273
274         tab_widget.pack_start (tab_label, false, false, 3);
275         tab_widget.pack_end (tab_close_alignment, false, false, 0);
276         tab_widget.show_all_children ();
277         update_tab_label ();
278         update_example_filename();
279
280         /* Done */
281
282         show_all_children ();
283 }
284
285 ExportFileNotebook::FilePage::~FilePage ()
286 {
287 }
288
289 void
290 ExportFileNotebook::FilePage::set_remove_sensitive (bool value)
291 {
292         tab_close_button.set_sensitive (value);
293 }
294
295 std::string
296 ExportFileNotebook::FilePage::get_format_name () const
297 {
298         if (format_state && format_state->format) {
299                 return format_state->format->name();
300         }
301         return _("No format!");
302 }
303
304 bool
305 ExportFileNotebook::FilePage::get_soundcloud_upload () const
306 {
307 #ifdef NDEBUG // SoundCloud upload is currently b0rked, needs debugging
308         return false;
309 #endif
310         return soundcloud_upload_button.get_active ();
311 }
312
313 void
314 ExportFileNotebook::FilePage::soundcloud_upload_changed ()
315 {
316         profile_manager->save_format_to_disk (format_state->format);
317 }
318
319 void
320 ExportFileNotebook::FilePage::update_soundcloud_upload_button ()
321 {
322         soundcloud_upload_button.set_active (format_state->format->soundcloud_upload());
323 }
324
325 void
326 ExportFileNotebook::FilePage::save_format_to_manager (FormatPtr format)
327 {
328         profile_manager->save_format_to_disk (format);
329 }
330
331 void
332 ExportFileNotebook::FilePage::update_tab_label ()
333 {
334         tab_label.set_text (string_compose (_("Format %1: %2"), tab_number, get_format_name()));
335 }
336
337 void
338 ExportFileNotebook::FilePage::update_example_filename()
339 {
340         if (profile_manager) {
341                 if (profile_manager->get_timespans().size() > 1
342                                 || profile_manager->get_timespans().front()->timespans->size() > 1) {
343                         filename_selector.require_timespan (true);
344                 } else {
345                         filename_selector.require_timespan (false);
346                 }
347
348                 std::string example;
349                 if (format_state->format) {
350                         example = profile_manager->get_sample_filename_for_format (
351                                 filename_state->filename, format_state->format);
352                 }
353
354                 if (example != "") {
355                         filename_selector.set_example_filename(Glib::path_get_basename (example));
356                 } else {
357                         filename_selector.set_example_filename("");
358                 }
359         }
360 }
361
362 void
363 ExportFileNotebook::FilePage::critical_selection_changed ()
364 {
365         update_tab_label();
366         update_example_filename();
367
368         soundcloud_button_connection.block ();
369         analysis_button_connection.block ();
370
371         update_analysis_button();
372         update_soundcloud_upload_button();
373
374         analysis_button_connection.unblock ();
375         soundcloud_button_connection.unblock ();
376
377         CriticalSelectionChanged();
378 }