1 /* abGate - LV2 Noise Gate Plugin
2  *
3  * Copyright 2011 Antanas Bružas
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 3 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #include "preset_widget.h"
21 
preset_widget(main_window * m_win,void (* set_parameters)(main_window * main_w,float th,float at,float ho,float de,float ra),void (* get_parameters)(main_window * main_w,float & th,float & at,float & ho,float & de,float & ra))22 preset_widget::preset_widget(main_window *m_win, void (*set_parameters)(main_window *main_w, float th, float at, float ho, float de, float ra),
23 		void (*get_parameters)(main_window *main_w, float &th, float &at, float &ho, float &de, float &ra)) :
24 		load_button("Load"), save_button("Save"), delete_button("Delete"), presets_label("<span foreground='#111111' weight='heavy'>Presets:</span>") {
25 
26 	// External functions
27 	global_get = get_parameters;
28 	global_set = set_parameters;
29 
30 	main_win = m_win;
31 
32 	m_fix.set_size_request(500, 40);
33 
34 	/* ========== Colors ========== */
35 	Gdk::Color c_black("#111111");
36 	Gdk::Color c_green("#436d0d");
37 	Gdk::Color c_red("#870b0b");
38 	Gdk::Color c_dred("#4e0707");
39 	Gdk::Color c_dgreen("#273f09");
40 
41 	modify_bg(STATE_NORMAL, c_green);
42 
43 	/* ===== Background modifications ===== */
44 	load_button.modify_bg(STATE_NORMAL, c_black);
45 	load_button.modify_bg(STATE_ACTIVE, c_dgreen);
46 	load_button.modify_bg(STATE_PRELIGHT, c_green);
47 
48 	save_button.modify_bg(STATE_NORMAL, c_black);
49 	save_button.modify_bg(STATE_ACTIVE, c_dgreen);
50 	save_button.modify_bg(STATE_PRELIGHT, c_green);
51 
52 	delete_button.modify_bg(STATE_NORMAL, c_black);
53 	delete_button.modify_bg(STATE_ACTIVE, c_dred);
54 	delete_button.modify_bg(STATE_PRELIGHT, c_red);
55 
56 	/* ===== Pango markup for the label ===== */
57 	presets_label.set_use_markup();
58 
59 	m_box.pack_start(presets_label);
60 	m_box.pack_start(preset_combo);
61 	m_box.pack_start(load_button);
62 	m_box.pack_start(save_button);
63 	m_box.pack_start(delete_button);
64 	m_box.set_homogeneous(false);
65 	m_box.set_size_request(424, 28);
66 	m_box.set_spacing(2);
67 	m_fix.put(m_box, 80, 25);
68 	add(m_fix);
69 	show_all_children();
70 
71 	/* ===== Moving to directories part ===== */
72 	home_dir = getenv("HOME");
73 	string cmd;
74 	dir = home_dir + FILE_NAME;
75 	const char *directory = (home_dir + "/.abGate").c_str();
76 
77 	struct stat st;
78 	if (stat(directory, &st) != 0) {
79 		cmd = "mkdir " + home_dir + "/.abGate && touch " + dir;
80 		if (system(cmd.c_str())) {
81 			bool ignor = true;
82 		}
83 	}
84 
85 	// Signals
86 	load_button.signal_clicked().connect(sigc::mem_fun(*this, &preset_widget::load_clicked));
87 	save_button.signal_clicked().connect(sigc::mem_fun(*this, &preset_widget::save_clicked));
88 	delete_button.signal_clicked().connect(sigc::mem_fun(*this, &preset_widget::delete_clicked));
89 }
90 
load_clicked()91 void preset_widget::load_clicked() {
92 	Entry* entry = preset_combo.get_entry();
93 	if (entry) {
94 		ustring presetname = entry->get_text();
95 		if (!presetname.empty()) {
96 			presets *p_load = new presets();
97 			// Getting all the names
98 			vector<string> names_vector = p_load->get_names_xml(dir);
99 			// Checking if we already have this name
100 			bool have;
101 			for (int i = 0; i < names_vector.size(); i++) {
102 				if (presetname == names_vector[i]) {
103 					have = true;
104 					break;
105 				} else {
106 					have = false;
107 				}
108 			}
109 			if (have == true) {
110 				preset pre_load = p_load->get_one_xml(presetname, dir);
111 				global_set(main_win, pre_load.param_value[1], pre_load.param_value[2], pre_load.param_value[3], pre_load.param_value[4], pre_load.param_value[5]);
112 			}
113 		}
114 	}
115 }
116 
save_clicked()117 void preset_widget::save_clicked() {
118 	Entry* entry = preset_combo.get_entry();
119 	if (entry) {
120 		ustring presetname = entry->get_text();
121 		if (!presetname.empty()) {
122 			presets *p_save = new presets();
123 			// Getting all the names
124 			vector<string> names_vector = p_save->get_names_xml(dir);
125 			// Checking if we already have this name
126 			bool have;
127 			for (int i = 0; i < names_vector.size(); i++) {
128 				if (presetname == names_vector[i]) {
129 					have = true;
130 					break;
131 				} else {
132 					have = false;
133 				}
134 			}
135 
136 			if (have == true) {
137 				list<preset> preset_list = p_save->get_xml(dir);
138 				list<preset>::iterator it;
139 
140 				for (it = preset_list.begin(); it != preset_list.end(); it++) {
141 					if (it->name == presetname) {
142 						preset_list.erase(it);
143 						break;
144 					}
145 				}
146 
147 				// open and after close the file to clear the content of it
148 				ofstream presetsfile(dir.c_str());
149 				presetsfile.close();
150 
151 				preset *pre_delete = new preset();
152 				for (it = preset_list.begin(); it != preset_list.end(); it++) {
153 					float parameters[6] = { it->param_value[0], it->param_value[1], it->param_value[2], it->param_value[3], it->param_value[4], it->param_value[5] };
154 					pre_delete->construct(it->name, parameters);
155 					p_save->set_xml(*pre_delete, false, dir);
156 				}
157 				// Adding new preset
158 				preset *pre_save = new preset();
159 				global_get(main_win, th, at, ho, de, ra);
160 				float parameters[6] = { 1, th, at, ho, de, ra };
161 				pre_save->construct(presetname, parameters);
162 				p_save->set_xml(*pre_save, false, dir);
163 			} else {
164 				// Adding new preset
165 				preset *pre_save = new preset();
166 				global_get(main_win, th, at, ho, de, ra);
167 				float parameters[6] = { 1, th, at, ho, de, ra };
168 				pre_save->construct(presetname, parameters);
169 				p_save->set_xml(*pre_save, false, dir);
170 				preset_combo.append_text(presetname);
171 			}
172 		}
173 	}
174 }
175 
delete_clicked()176 void preset_widget::delete_clicked() {
177 	Entry* entry = preset_combo.get_entry();
178 	if (entry) {
179 		ustring presetname = entry->get_text();
180 		if (!presetname.empty()) {
181 			presets *p_delete = new presets();
182 			// Getting all the names
183 			vector<string> names_vector = p_delete->get_names_xml(dir);
184 			// Checking if we already have this name
185 			bool have;
186 			for (int i = 0; i < names_vector.size(); i++) {
187 				if (presetname == names_vector[i]) {
188 					have = true;
189 					break;
190 				} else {
191 					have = false;
192 				}
193 			}
194 			if (have == true) {
195 				MessageDialog dialog((Window&) (*this->get_toplevel()), "Do you really want to delete preset <span weight='heavy'><i>" + presetname + "</i></span>?", true, MESSAGE_QUESTION,
196 						BUTTONS_OK_CANCEL);
197 				int result = dialog.run();
198 				if (result == RESPONSE_OK) {
199 					list<preset> preset_list = p_delete->get_xml(dir);
200 					list<preset>::iterator it;
201 
202 					for (it = preset_list.begin(); it != preset_list.end(); it++) {
203 						if (it->name == presetname) {
204 							preset_list.erase(it);
205 							break;
206 						}
207 					}
208 
209 					// open and after close the file to clear the content of it
210 					ofstream presetsfile(dir.c_str());
211 					presetsfile.close();
212 
213 					preset *pre_delete = new preset();
214 					for (it = preset_list.begin(); it != preset_list.end(); it++) {
215 						float parameters[6] = { it->param_value[0], it->param_value[1], it->param_value[2], it->param_value[3], it->param_value[4], it->param_value[5] };
216 						pre_delete->construct(it->name, parameters);
217 						p_delete->set_xml(*pre_delete, false, dir);
218 					}
219 					preset_combo.remove_text(presetname);
220 					entry->set_text("");
221 				}
222 			}
223 		}
224 	}
225 }
226 
load_combo_list()227 void preset_widget::load_combo_list() {
228 
229 	preset_combo.clear_items();
230 	ustring presetname;
231 
232 	presets *p_names = new presets();
233 	vector<string> names_vector = p_names->get_names_xml(dir);
234 
235 	for (int i = 0; i < names_vector.size(); i++) {
236 		presetname = names_vector[i];
237 		preset_combo.append_text(presetname);
238 	}
239 }
240 
~preset_widget()241 preset_widget::~preset_widget() {
242 }
243