Export dialog: tidy code, & remove some superfluous debug output
[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 "i18n.h"
28
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 ExportFileNotebook::ExportFileNotebook () :
33   page_counter (1)
34 {
35         /* Last page */
36
37         new_file_button.set_image (*Gtk::manage (new Gtk::Image (::get_icon("add"))));
38         new_file_button.set_label (_("Add another format"));
39         new_file_button.set_alignment (0, 0.5);
40         new_file_button.set_relief (Gtk::RELIEF_NONE);
41
42         new_file_hbox.pack_start (new_file_button, true, true);
43         append_page (new_file_dummy, new_file_hbox);
44         set_tab_label_packing (new_file_dummy, true, true, Gtk::PACK_START);
45         new_file_hbox.show_all_children ();
46
47         page_change_connection = signal_switch_page().connect (sigc::mem_fun (*this, &ExportFileNotebook::handle_page_change));
48         new_file_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFileNotebook::add_new_file_page));
49 }
50
51 void
52 ExportFileNotebook::set_session_and_manager (ARDOUR::Session * s, boost::shared_ptr<ARDOUR::ExportProfileManager> manager)
53 {
54         SessionHandlePtr::set_session (s);
55         profile_manager = manager;
56
57         sync_with_manager ();
58 }
59
60 void
61 ExportFileNotebook::sync_with_manager ()
62 {
63         /* Clear pages from notebook
64            The page switch handling has to be disabled during removing all pages due to a gtk bug
65          */
66
67         page_change_connection.block();
68         while (get_n_pages() > 1) {
69                 remove_page (0);
70         }
71         page_change_connection.block(false);
72
73         page_counter = 1;
74         last_visible_page = 0;
75
76         /* File notebook */
77
78         ExportProfileManager::FormatStateList const & formats = profile_manager->get_formats ();
79         ExportProfileManager::FormatStateList::const_iterator format_it;
80         ExportProfileManager::FilenameStateList const & filenames = profile_manager->get_filenames ();
81         ExportProfileManager::FilenameStateList::const_iterator filename_it;
82         for (format_it = formats.begin(), filename_it = filenames.begin();
83              format_it != formats.end() && filename_it != filenames.end();
84              ++format_it, ++filename_it) {
85                 add_file_page (*format_it, *filename_it);
86         }
87
88         set_current_page (0);
89         update_soundcloud_upload ();
90         CriticalSelectionChanged ();
91 }
92
93 void
94 ExportFileNotebook::update_soundcloud_upload ()
95 {
96         int i;
97         ExportProfileManager::FormatStateList const & formats = profile_manager->get_formats ();
98         ExportProfileManager::FormatStateList::const_iterator format_it;
99
100         for (i = 0, format_it = formats.begin(); format_it != formats.end(); ++i, ++format_it) {
101                 FilePage * page;
102                 if ((page = dynamic_cast<FilePage *> (get_nth_page (i)))) {
103                         (*format_it)->format->set_soundcloud_upload (page->get_soundcloud_upload ());
104                 }
105         }
106 }
107
108 void
109 ExportFileNotebook::update_example_filenames ()
110 {
111         int i = 0;
112         FilePage * page;
113         while ((page = dynamic_cast<FilePage *> (get_nth_page (i++)))) {
114                 page->update_example_filename();
115         }
116 }
117
118 std::string
119 ExportFileNotebook::get_nth_format_name (uint32_t n)
120 {
121         FilePage * page;
122         if ((page = dynamic_cast<FilePage *> (get_nth_page (n - 1)))) {
123                 return page->get_format_name();
124         }
125         return "";
126 }
127
128 void
129 ExportFileNotebook::add_new_file_page ()
130 {
131         FilePage * page;
132         if ((page = dynamic_cast<FilePage *> (get_nth_page (get_current_page())))) {
133                 add_file_page (profile_manager->duplicate_format_state (page->get_format_state()),
134                                profile_manager->duplicate_filename_state (page->get_filename_state()));
135         }
136 }
137
138 void
139 ExportFileNotebook::add_file_page (ARDOUR::ExportProfileManager::FormatStatePtr format_state, ARDOUR::ExportProfileManager::FilenameStatePtr filename_state)
140 {
141         FilePage * page = Gtk::manage (new FilePage (_session, profile_manager, this, page_counter, format_state, filename_state));
142         page->CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
143         insert_page (*page, page->get_tab_widget(), get_n_pages() - 1);
144
145         update_remove_file_page_sensitivity ();
146         show_all_children();
147         ++page_counter;
148
149         CriticalSelectionChanged ();
150 }
151
152 void
153 ExportFileNotebook::remove_file_page (FilePage * page)
154 {
155         profile_manager->remove_format_state (page->get_format_state());
156         profile_manager->remove_filename_state (page->get_filename_state());
157
158         remove_page (*page);
159         update_remove_file_page_sensitivity ();
160
161         CriticalSelectionChanged ();
162 }
163
164 void
165 ExportFileNotebook::update_remove_file_page_sensitivity ()
166 {
167         FilePage * page;
168         if ((page = dynamic_cast<FilePage *> (get_nth_page (0)))) {
169                 if (get_n_pages() > 2) {
170                         page->set_remove_sensitive (true);
171                 } else {
172                         page->set_remove_sensitive (false);
173                 }
174         }
175 }
176
177 void
178 ExportFileNotebook::handle_page_change (GtkNotebookPage*, uint32_t page)
179 {
180         if (page + 1 == (uint32_t) get_n_pages()) {
181                 set_current_page (last_visible_page);
182         } else {
183                 last_visible_page = page;
184         }
185 }
186
187 ExportFileNotebook::FilePage::FilePage (Session * s, ManagerPtr profile_manager, ExportFileNotebook * parent, uint32_t number,
188                                         ExportProfileManager::FormatStatePtr format_state,
189                                         ExportProfileManager::FilenameStatePtr filename_state) :
190   format_state (format_state),
191   filename_state (filename_state),
192   profile_manager (profile_manager),
193
194   format_label (_("Format"), Gtk::ALIGN_LEFT),
195   filename_label (_("Location"), Gtk::ALIGN_LEFT),
196   soundcloud_upload_button (_("Upload to Soundcloud")),
197   tab_number (number)
198 {
199         set_border_width (12);
200
201         pack_start (format_label, false, false, 0);
202         pack_start (format_align, false, false, 0);
203         pack_start (filename_label, false, false, 0);
204         pack_start (filename_align, false, false, 0);
205         pack_start (soundcloud_upload_button, false, false, 0);
206
207         format_align.add (format_selector);
208         format_align.set_padding (6, 12, 18, 0);
209
210         filename_align.add (filename_selector);
211         filename_align.set_padding (0, 12, 18, 0);
212
213         Pango::AttrList bold;
214         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
215         bold.insert (b);
216
217         format_label.set_attributes (bold);
218         filename_label.set_attributes (bold);
219         tab_label.set_attributes (bold);
220
221         /* Set states */
222         format_selector.set_state (format_state, s);
223         filename_selector.set_state (filename_state, s);
224
225         /* Signals */
226
227         tab_close_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*parent, &ExportFileNotebook::remove_file_page), this));
228
229         profile_manager->FormatListChanged.connect (format_connection, invalidator (*this), boost::bind (&ExportFormatSelector::update_format_list, &format_selector), gui_context());
230
231         format_selector.FormatEdited.connect (sigc::mem_fun (*this, &ExportFileNotebook::FilePage::save_format_to_manager));
232         format_selector.FormatRemoved.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::remove_format_profile));
233         format_selector.NewFormat.connect (sigc::mem_fun (*profile_manager, &ExportProfileManager::get_new_format));
234
235         format_selector.CriticalSelectionChanged.connect (
236                 sigc::mem_fun (*this, &ExportFileNotebook::FilePage::critical_selection_changed));
237         filename_selector.CriticalSelectionChanged.connect (
238                 sigc::mem_fun (*this, &ExportFileNotebook::FilePage::critical_selection_changed));
239
240         soundcloud_upload_button.signal_toggled().connect (sigc::mem_fun (*parent, &ExportFileNotebook::update_soundcloud_upload));
241         /* Tab widget */
242
243         tab_close_button.add (*Gtk::manage (new Gtk::Image (::get_icon("close"))));
244         tab_close_alignment.add (tab_close_button);
245         tab_close_alignment.set (0.5, 0.5, 0, 0);
246
247         tab_widget.pack_start (tab_label, false, false, 3);
248         tab_widget.pack_end (tab_close_alignment, false, false, 0);
249         tab_widget.show_all_children ();
250         update_tab_label ();
251         update_example_filename();
252
253         /* Done */
254
255         show_all_children ();
256 }
257
258 ExportFileNotebook::FilePage::~FilePage ()
259 {
260 }
261
262 void
263 ExportFileNotebook::FilePage::set_remove_sensitive (bool value)
264 {
265         tab_close_button.set_sensitive (value);
266 }
267
268 std::string
269 ExportFileNotebook::FilePage::get_format_name () const
270 {
271         if (format_state && format_state->format) {
272                 return format_state->format->name();
273         }
274         return _("No format!");
275 }
276
277 bool
278 ExportFileNotebook::FilePage::get_soundcloud_upload () const
279 {
280         return soundcloud_upload_button.get_active ();
281 }
282
283 void
284 ExportFileNotebook::FilePage::save_format_to_manager (FormatPtr format)
285 {
286         profile_manager->save_format_to_disk (format);
287 }
288
289 void
290 ExportFileNotebook::FilePage::update_tab_label ()
291 {
292         tab_label.set_text (string_compose (_("Format %1: %2"), tab_number, get_format_name()));
293 }
294
295 void
296 ExportFileNotebook::FilePage::update_example_filename()
297 {
298         if (profile_manager) {
299
300                 std::string example;
301                 if (format_state->format) {
302                         example = profile_manager->get_sample_filename_for_format (
303                                 filename_state->filename, format_state->format);
304                 }
305                 
306                 if (example != "") {
307                         filename_selector.set_example_filename(Glib::path_get_basename (example));
308                 } else {
309                         filename_selector.set_example_filename("");
310                 }
311         }
312 }
313
314 void
315 ExportFileNotebook::FilePage::critical_selection_changed ()
316 {
317         update_tab_label();
318         update_example_filename();
319         CriticalSelectionChanged();
320 }