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