1 // This file is part of GtkEveMon.
2 //
3 // GtkEveMon is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // You should have received a copy of the GNU General Public License
9 // along with GtkEveMon. If not, see <http://www.gnu.org/licenses/>.
10
11 #include <iostream>
12
13 #include "bits/config.h"
14 #include "gtkportrait.h"
15 #include "gtkconfwidgets.h"
16
GtkConfFileChooser(std::string const & key,Gtk::FileChooserAction action)17 GtkConfFileChooser::GtkConfFileChooser (std::string const& key,
18 Gtk::FileChooserAction action)
19 : Gtk::FileChooserButton(action)
20 {
21 this->value = Config::conf.get_value(key);
22
23 if ((**this->value).empty())
24 this->set_filename("/");
25 else
26 this->set_filename(**this->value);
27
28 this->signal_selection_changed().connect(sigc::mem_fun
29 (*this, &GtkConfFileChooser::on_selection_change));
30 }
31
32 /* ---------------------------------------------------------------- */
33
34 void
on_selection_change(void)35 GtkConfFileChooser::on_selection_change (void)
36 {
37 this->value->set(this->get_filename());
38 }
39
40 /* ================================================================ */
41
GtkConfCheckButton(Glib::ustring const & label,bool mnemonic,std::string const & conf_key)42 GtkConfCheckButton::GtkConfCheckButton (Glib::ustring const& label,
43 bool mnemonic, std::string const& conf_key)
44 : Gtk::CheckButton(label, mnemonic)
45 {
46 this->value = Config::conf.get_value(conf_key);
47 this->set_active(this->value->get_bool());
48
49 this->signal_toggled().connect(sigc::mem_fun
50 (*this, &GtkConfCheckButton::on_button_toggled));
51 }
52
53 /* ---------------------------------------------------------------- */
54
55 void
on_button_toggled(void)56 GtkConfCheckButton::on_button_toggled (void)
57 {
58 this->value->set(this->get_active());
59 }
60
61 /* ================================================================ */
62
GtkConfTextEntry(std::string const & conf_key)63 GtkConfTextEntry::GtkConfTextEntry (std::string const& conf_key)
64 {
65 this->value = Config::conf.get_value(conf_key);
66 this->set_text(value->get_string());
67
68 this->signal_changed().connect(sigc::mem_fun
69 (*this, &GtkConfTextEntry::on_text_changed));
70 }
71
72 /* ---------------------------------------------------------------- */
73
74 void
on_text_changed(void)75 GtkConfTextEntry::on_text_changed (void)
76 {
77 this->value->set(this->get_text());
78 }
79
80 /* ================================================================ */
81
GtkConfComboBox(std::string const & conf_key)82 GtkConfComboBox::GtkConfComboBox (std::string const& conf_key)
83 {
84 this->value = Config::conf.get_value(conf_key);
85 this->signal_changed().connect(sigc::mem_fun(*this,
86 &GtkConfComboBox::on_changed_signal));
87 }
88
89 /* ---------------------------------------------------------------- */
90
91 void
on_changed_signal(void)92 GtkConfComboBox::on_changed_signal (void)
93 {
94 this->value->set(this->values[this->get_active_row_number()]);
95 }
96
97 /* ---------------------------------------------------------------- */
98
99 void
append_conf_row(std::string const & text,std::string const & value)100 GtkConfComboBox::append_conf_row (std::string const& text,
101 std::string const& value)
102 {
103 this->values.push_back(value);
104 this->append(text);
105
106 if (value == this->value->get_string())
107 this->set_active((int)this->values.size() - 1);
108 }
109
110 /* ================================================================ */
111
GtkConfSectionSelection(void)112 GtkConfSectionSelection::GtkConfSectionSelection (void)
113 : selection_store(Gtk::ListStore::create(selection_cols))
114 {
115 this->set_model(this->selection_store);
116 this->pack_start(this->selection_cols.name, true);
117
118 this->changed_conn = this->signal_changed().connect(sigc::mem_fun
119 (*this, &GtkConfSectionSelection::on_combo_entry_changed));
120 }
121
122 /* ---------------------------------------------------------------- */
123
124 void
set_parent_config_section(std::string const & section,std::string const & select)125 GtkConfSectionSelection::set_parent_config_section (std::string const& section,
126 std::string const& select)
127 {
128 this->parent_section = Config::conf.get_or_create_section(section);
129 this->update_selection_store(select);
130 }
131
132 /* ---------------------------------------------------------------- */
133
134 void
update_selection_store(std::string const & select)135 GtkConfSectionSelection::update_selection_store (std::string const& select)
136 {
137 /* Stop signalling. This one emits a lot of on_combo_entry_changed */
138 this->changed_conn.block();
139 this->selection_store->clear();
140 this->changed_conn.unblock();
141
142 bool selected = false;
143 conf_sections_t::iterator iter;
144 for (iter = this->parent_section->sections_begin();
145 iter != this->parent_section->sections_end(); iter++)
146 {
147 Gtk::ListStore::iterator row = this->selection_store->append();
148 (*row)[this->selection_cols.name] = iter->first;
149 (*row)[this->selection_cols.section] = iter->second;
150 if (!select.empty() && iter->first == select)
151 {
152 this->set_active(row);
153 selected = true;
154 }
155 }
156
157 if (!selected)
158 {
159 if (this->selection_store->children().size() > 0)
160 this->set_active(0);
161 else
162 {
163 this->set_active(-1);
164 this->on_combo_entry_changed();
165 }
166 }
167 }
168
169 /* ---------------------------------------------------------------- */
170
171 void
on_combo_entry_changed(void)172 GtkConfSectionSelection::on_combo_entry_changed (void)
173 {
174 if (this->get_active_row_number() == -1)
175 {
176 this->sig_conf_section_changed.emit(ConfSectionPtr());
177 }
178 else
179 {
180 ConfSectionPtr section = this->get_active_section();
181 this->sig_conf_section_changed.emit(section);
182 }
183 }
184
185 /* ---------------------------------------------------------------- */
186
187 ConfSectionPtr
create_new_section(std::string const & name)188 GtkConfSectionSelection::create_new_section (std::string const& name)
189 {
190 ConfSectionPtr section;
191 try
192 {
193 /* Check if section exists. Select this if existing. */
194 section = this->parent_section->get_section(name);
195 this->update_selection_store(name);
196 }
197 catch (Exception& e)
198 {
199 /* Section does not exist. */
200 section = ConfSection::create();
201 this->parent_section->add(name, section);
202 this->update_selection_store(name);
203 }
204
205 return section;
206 }
207
208 /* ---------------------------------------------------------------- */
209
210 ConfSectionPtr
get_active_section(void)211 GtkConfSectionSelection::get_active_section (void)
212 {
213 if (this->get_active_row_number() == -1)
214 return ConfSectionPtr();
215
216 Gtk::ListStore::iterator iter = this->get_active();
217 return (*iter)[this->selection_cols.section];
218 }
219
220 /* ---------------------------------------------------------------- */
221
222 Glib::ustring
get_active_name(void)223 GtkConfSectionSelection::get_active_name (void)
224 {
225 if (this->get_active_row_number() == -1)
226 return "";
227
228 Gtk::ListStore::iterator iter = this->get_active();
229 return (*iter)[this->selection_cols.name];
230 }
231
232 /* ---------------------------------------------------------------- */
233
234 void
set_active_section(std::string const & name)235 GtkConfSectionSelection::set_active_section (std::string const& name)
236 {
237 Gtk::ListStore::iterator row;
238 for (row = this->selection_store->children().begin();
239 row != this->selection_store->children().end(); row++)
240 {
241 if ((*row)[this->selection_cols.name] == name)
242 {
243 this->set_active(row);
244 return;
245 }
246 }
247 }
248
249 /* ---------------------------------------------------------------- */
250
251 void
delete_section(std::string const & name)252 GtkConfSectionSelection::delete_section (std::string const& name)
253 {
254 this->parent_section->remove_section(name);
255 this->update_selection_store();
256 }
257
258 /* ---------------------------------------------------------------- */
259
260 void
rename_section(std::string const & from,std::string const & to)261 GtkConfSectionSelection::rename_section (std::string const& from,
262 std::string const& to)
263 {
264 if (from.empty() || to.empty())
265 throw Exception("Old name or new name not specified!");
266
267 if (from == to)
268 throw Exception("Old name and new name are identical!");
269
270 ConfSectionPtr sect;
271 try
272 {
273 sect = this->parent_section->get_section(from);
274 }
275 catch (Exception& e)
276 {
277 throw Exception("Old section name not found!");
278 }
279
280 try
281 {
282 /* Check if destination exists. */
283 ConfSectionPtr dest = this->parent_section->get_section(to);
284 throw Exception("New section name already exists!");
285 }
286 catch (Exception& e)
287 {
288 }
289
290 this->parent_section->add(to, sect);
291 this->parent_section->remove_section(from);
292 this->update_selection_store(to);
293 }
294
295 /* ---------------------------------------------------------------- */
296
297 sigc::signal<void, ConfSectionPtr>&
signal_conf_section_changed(void)298 GtkConfSectionSelection::signal_conf_section_changed (void)
299 {
300 return this->sig_conf_section_changed;
301 }
302