1 /*
2  * Copyright (C) 2008-2011 Sakari Bergen <sakari.bergen@beatwaves.net>
3  * Copyright (C) 2009 David Robillard <d@drobilla.net>
4  * Copyright (C) 2010-2016 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include <gtkmm/messagedialog.h>
23 #include <gtkmm/stock.h>
24 
25 #include "ardour/export_preset.h"
26 
27 #include "export_preset_selector.h"
28 
29 #include "pbd/i18n.h"
30 
ExportPresetSelector()31 ExportPresetSelector::ExportPresetSelector () :
32   label (_("Preset"), Gtk::ALIGN_LEFT),
33   save_button (Gtk::Stock::SAVE),
34   remove_button (Gtk::Stock::REMOVE),
35   new_button (Gtk::Stock::NEW)
36 {
37 	list = Gtk::ListStore::create (cols);
38         list->set_sort_column (cols.label, Gtk::SORT_ASCENDING);
39 	entry.set_model (list);
40 	entry.set_text_column (cols.label);
41 
42 	pack_start (label, false, false, 0);
43 	pack_start (entry, true, true, 6);
44 	pack_start (save_button, false, false, 0);
45 	pack_start (remove_button, false, false, 6);
46 	pack_start (new_button, false, false, 0);
47 
48 	save_button.set_sensitive (false);
49 	remove_button.set_sensitive (false);
50 	new_button.set_sensitive (false);
51 
52 	select_connection = entry.signal_changed().connect (sigc::mem_fun (*this, &ExportPresetSelector::update_selection));
53 	save_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::save_current));
54 	new_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::create_new));
55 	remove_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportPresetSelector::remove_current));
56 
57 	show_all_children ();
58 }
59 
60 void
set_manager(boost::shared_ptr<ARDOUR::ExportProfileManager> manager)61 ExportPresetSelector::set_manager (boost::shared_ptr<ARDOUR::ExportProfileManager> manager)
62 {
63 	profile_manager = manager;
64 	sync_with_manager ();
65 }
66 
67 void
sync_with_manager()68 ExportPresetSelector::sync_with_manager ()
69 {
70 	list->clear();
71 
72 	PresetList const & presets = profile_manager->get_presets();
73 	Gtk::ListStore::iterator tree_it;
74 
75 	for (PresetList::const_iterator it = presets.begin(); it != presets.end(); ++it) {
76 		tree_it = list->append();
77 		tree_it->set_value (cols.preset, *it);
78 		tree_it->set_value (cols.label, std::string ((*it)->name()));
79 
80 		if (*it == current) {
81 			select_connection.block (true);
82 			entry.set_active (tree_it);
83 			select_connection.block (false);
84 		}
85 	}
86 }
87 
88 void
update_selection()89 ExportPresetSelector::update_selection ()
90 {
91 	Gtk::ListStore::iterator it = entry.get_active ();
92 	std::string text = entry.get_entry()->get_text();
93 	bool preset_name_exists = false;
94 
95 	for (PresetList::const_iterator it = profile_manager->get_presets().begin(); it != profile_manager->get_presets().end(); ++it) {
96 		if (!(*it)->name().compare (text)) { preset_name_exists = true; }
97 	}
98 
99 	if (list->iter_is_valid (it)) {
100 
101 		previous = current = it->get_value (cols.preset);
102 		if (!profile_manager->load_preset (current)) {
103 			Gtk::MessageDialog dialog (_("The selected preset did not load successfully!\nPerhaps it references a format that has been removed?"),
104 			                           false, Gtk::MESSAGE_WARNING);
105 			dialog.run ();
106 		}
107 		sync_with_manager ();
108 		CriticalSelectionChanged();
109 
110 		/* Make an edit, so that signal changed will be emitted on re-selection */
111 
112 		select_connection.block (true);
113 		entry.get_entry()->set_text ("");
114 		entry.get_entry()->set_text (text);
115 		select_connection.block (false);
116 
117 	} else { // Text has been edited, this should not make any changes in the profile manager
118 		if (previous && !text.compare (previous->name())) {
119 			current = previous;
120 		} else {
121 			current.reset ();
122 		}
123 	}
124 
125 	save_button.set_sensitive (current != 0);
126 	remove_button.set_sensitive (current != 0);
127 	new_button.set_sensitive (!current && !text.empty() && !preset_name_exists);
128 }
129 
130 void
create_new()131 ExportPresetSelector::create_new ()
132 {
133 	if (!profile_manager) { return; }
134 
135 	previous = current = profile_manager->new_preset (entry.get_entry()->get_text());
136 	sync_with_manager ();
137 	update_selection (); // Update preset widget states
138 }
139 
140 void
save_current()141 ExportPresetSelector::save_current ()
142 {
143 	if (!profile_manager) { return; }
144 
145 	previous = current = profile_manager->save_preset (entry.get_entry()->get_text());
146 	sync_with_manager ();
147 	update_selection (); // Update preset widget states
148 }
149 
150 void
remove_current()151 ExportPresetSelector::remove_current ()
152 {
153 	if (!profile_manager) { return; }
154 
155 	Gtk::MessageDialog dialog (_("Do you really want to remove this preset?"),
156 			false,
157 			Gtk::MESSAGE_QUESTION,
158 			Gtk::BUTTONS_YES_NO);
159 
160 	if (Gtk::RESPONSE_YES != dialog.run ()) {
161 		/* User has selected "no" or closed the dialog, better
162 		 * abort
163 		 */
164 		return;
165 	}
166 
167 	profile_manager->remove_preset();
168 	entry.get_entry()->set_text ("");
169 	sync_with_manager ();
170 }
171