1 // Copyright (C) 2011-2015 Petr Pavlu <setup@dagobah.cz>
3 // This file is part of CenterIM.
5 // CenterIM 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.
10 // CenterIM 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.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
18 #include "OptionWindow.h"
24 #include <cppconsui/TreeView.h>
26 OptionWindow::OptionWindow() : SplitDialog(0, 0, 80, 24, _("Config options"))
28 setColorScheme(CenterIM::SCHEME_GENERALWINDOW
);
30 auto treeview
= new CppConsUI::TreeView(AUTOSIZE
, AUTOSIZE
);
31 setContainer(*treeview
);
34 CppConsUI::TreeView::NodeReference parent
;
35 parent
= treeview
->appendNode(treeview
->getRootNode(),
36 *(new CppConsUI::TreeView::ToggleCollapseButton(_("Buddy list"))));
37 treeview
->setCollapsed(parent
, true);
38 treeview
->appendNode(parent
, *(new BooleanOption(_("Show empty groups"),
39 CONF_PREFIX
"/blist/show_empty_groups")));
40 treeview
->appendNode(parent
, *(new BooleanOption(_("Show offline buddies"),
41 CONF_PREFIX
"/blist/show_offline_buddies")));
42 c
= new ChoiceOption(_("List mode"), CONF_PREFIX
"/blist/list_mode");
43 c
->addOption(_("Normal"), "normal");
44 c
->addOption(_("Flat"), "flat");
45 treeview
->appendNode(parent
, *c
);
47 _("Group sort mode"), CONF_PREFIX
"/blist/group_sort_mode");
48 c
->addOption(_("Manually"), "user");
49 c
->addOption(_("By name"), "name");
50 treeview
->appendNode(parent
, *c
);
52 _("Buddy sort mode"), CONF_PREFIX
"/blist/buddy_sort_mode");
53 c
->addOption(_("By name"), "name");
54 c
->addOption(_("By status"), "status");
55 c
->addOption(_("By activity"), "activity");
56 treeview
->appendNode(parent
, *c
);
58 _("Colorization mode"), CONF_PREFIX
"/blist/colorization_mode");
59 c
->addOption(_("None"), "none");
60 c
->addOption(_("By status"), "status");
61 c
->addOption(_("By account"), "account");
62 treeview
->appendNode(parent
, *c
);
64 parent
= treeview
->appendNode(treeview
->getRootNode(),
65 *(new CppConsUI::TreeView::ToggleCollapseButton(_("Dimensions"))));
66 treeview
->setCollapsed(parent
, true);
68 parent
, *(new IntegerOption(_("Buddy list window width"),
69 CONF_PREFIX
"/dimensions/buddylist_width",
70 sigc::mem_fun(this, &OptionWindow::getPercentUnit
))));
72 parent
, *(new IntegerOption(_("Log window height"),
73 CONF_PREFIX
"/dimensions/log_height",
74 sigc::mem_fun(this, &OptionWindow::getPercentUnit
))));
75 treeview
->appendNode(parent
, *(new BooleanOption(_("Show header"),
76 CONF_PREFIX
"/dimensions/show_header")));
77 treeview
->appendNode(parent
, *(new BooleanOption(_("Show footer"),
78 CONF_PREFIX
"/dimensions/show_footer")));
80 parent
= treeview
->appendNode(treeview
->getRootNode(),
81 *(new CppConsUI::TreeView::ToggleCollapseButton(_("Idle settings"))));
82 treeview
->setCollapsed(parent
, true);
84 parent
, *(new BooleanOption(_("Change to away status when idle"),
85 "/purple/away/away_when_idle")));
87 parent
, *(new IntegerOption(_("Time before becoming idle"),
88 "/purple/away/mins_before_away",
89 sigc::mem_fun(this, &OptionWindow::getMinUnit
))));
90 c
= new ChoiceOption(_("Report idle time"), "/purple/away/idle_reporting");
91 c
->addOption(_("Never"), "none");
92 c
->addOption(_("From last sent message"), "purple");
93 c
->addOption(_("Based on keyboard activity"), "system");
94 treeview
->appendNode(parent
, *c
);
96 parent
= treeview
->appendNode(treeview
->getRootNode(),
97 *(new CppConsUI::TreeView::ToggleCollapseButton(_("Conversations"))));
98 treeview
->setCollapsed(parent
, true);
99 treeview
->appendNode(parent
, *(new BooleanOption(_("Beep on new message"),
100 CONF_PREFIX
"/chat/beep_on_msg")));
101 treeview
->appendNode(
102 parent
, *(new BooleanOption(_("Send typing notification"),
103 "/purple/conversations/im/send_typing")));
105 parent
= treeview
->appendNode(treeview
->getRootNode(),
106 *(new CppConsUI::TreeView::ToggleCollapseButton(_("System logging"))));
107 treeview
->setCollapsed(parent
, true);
108 #define ADD_DEBUG_OPTIONS() \
110 c->addOption(_("None"), "none"); \
111 c->addOption(_("Error"), "error"); \
112 c->addOption(_("Critical"), "critical"); \
113 c->addOption(_("Warning"), "warning"); \
114 c->addOption(_("Message"), "message"); \
115 c->addOption(_("Info"), "info"); \
116 c->addOption(_("Debug"), "debug"); \
118 c
= new ChoiceOption(_("CIM log level"), CONF_PREFIX
"/log/log_level_cim");
120 treeview
->appendNode(parent
, *c
);
122 c
= new ChoiceOption(
123 _("Purple log level"), CONF_PREFIX
"/log/log_level_purple");
125 treeview
->appendNode(parent
, *c
);
127 c
= new ChoiceOption(_("GLib log level"), CONF_PREFIX
"/log/log_level_glib");
129 treeview
->appendNode(parent
, *c
);
130 #undef ADD_DEBUG_OPTIONS
132 parent
= treeview
->appendNode(treeview
->getRootNode(),
133 *(new CppConsUI::TreeView::ToggleCollapseButton(_("Libpurple logging"))));
134 treeview
->setCollapsed(parent
, true);
135 c
= new ChoiceOption(_("Log format"), "/purple/logging/format");
136 GList
*opts
= purple_log_logger_get_options();
137 for (GList
*o
= opts
; o
!= nullptr; o
= o
->next
) {
138 const char *human
= reinterpret_cast<const char *>(o
->data
);
140 g_assert(o
!= nullptr);
141 const char *value
= reinterpret_cast<const char *>(o
->data
);
142 c
->addOption(human
, value
);
145 treeview
->appendNode(parent
, *c
);
146 treeview
->appendNode(
147 parent
, *(new BooleanOption(
148 _("Log all instant messages"), "/purple/logging/log_ims")));
149 treeview
->appendNode(parent
,
150 *(new BooleanOption(_("Log all chats"), "/purple/logging/log_chats")));
151 treeview
->appendNode(
152 parent
, *(new BooleanOption(_("Log all status changes to system log"),
153 "/purple/logging/log_system")));
155 CppConsUI::Button
*b
;
156 parent
= treeview
->appendNode(treeview
->getRootNode(),
157 *(new CppConsUI::TreeView::ToggleCollapseButton(_("Config files"))));
158 treeview
->setCollapsed(parent
, true);
159 b
= new CppConsUI::Button(AUTOSIZE
, 1, _("Reload key bindings"));
160 b
->signal_activate
.connect(
161 sigc::mem_fun(this, &OptionWindow::reloadKeyBindings
));
162 treeview
->appendNode(parent
, *b
);
163 b
= new CppConsUI::Button(AUTOSIZE
, 1, _("Reload color schemes"));
164 b
->signal_activate
.connect(
165 sigc::mem_fun(this, &OptionWindow::reloadColorSchemes
));
166 treeview
->appendNode(parent
, *b
);
168 buttons_
->appendItem(
169 _("Done"), sigc::hide(sigc::mem_fun(this, &OptionWindow::close
)));
174 void OptionWindow::onScreenResized()
176 moveResizeRect(CENTERIM
->getScreenArea(CenterIM::CHAT_AREA
));
179 OptionWindow::BooleanOption::BooleanOption(const char *text
, const char *config
)
182 g_assert(text
!= nullptr);
183 g_assert(config
!= nullptr);
185 pref_
= g_strdup(config
);
186 setChecked(purple_prefs_get_bool(pref_
));
187 signal_toggle
.connect(sigc::mem_fun(this, &BooleanOption::onToggle
));
190 OptionWindow::BooleanOption::~BooleanOption()
195 void OptionWindow::BooleanOption::onToggle(
196 CheckBox
& /*activator*/, bool new_state
)
198 purple_prefs_set_bool(pref_
, new_state
);
201 OptionWindow::StringOption::StringOption(const char *text
, const char *config
)
202 : Button(FLAG_VALUE
, text
)
204 g_assert(text
!= nullptr);
205 g_assert(config
!= nullptr);
207 pref_
= g_strdup(config
);
208 setValue(purple_prefs_get_string(pref_
));
209 signal_activate
.connect(sigc::mem_fun(this, &StringOption::onActivate
));
212 OptionWindow::StringOption::~StringOption()
217 void OptionWindow::StringOption::onActivate(Button
& /*activator*/)
219 auto dialog
= new CppConsUI::InputDialog(getText(), getValue());
220 dialog
->signal_response
.connect(
221 sigc::mem_fun(this, &StringOption::responseHandler
));
225 void OptionWindow::StringOption::responseHandler(
226 CppConsUI::InputDialog
&activator
,
227 CppConsUI::AbstractDialog::ResponseType response
)
229 if (response
!= AbstractDialog::RESPONSE_OK
)
232 purple_prefs_set_string(pref_
, activator
.getText());
233 setValue(purple_prefs_get_string(pref_
));
236 OptionWindow::IntegerOption::IntegerOption(const char *text
, const char *config
)
237 : Button(FLAG_VALUE
, text
), has_unit_(false)
239 g_assert(text
!= nullptr);
240 g_assert(config
!= nullptr);
242 pref_
= g_strdup(config
);
243 setValue(purple_prefs_get_int(pref_
));
244 signal_activate
.connect(sigc::mem_fun(this, &IntegerOption::onActivate
));
247 OptionWindow::IntegerOption::IntegerOption(
248 const char *text
, const char *config
, sigc::slot
<const char *, int> unit_fun
)
249 : Button(FLAG_VALUE
| FLAG_UNIT
, text
), has_unit_(true), unit_fun_(unit_fun
)
251 g_assert(text
!= nullptr);
252 g_assert(config
!= nullptr);
254 pref_
= g_strdup(config
);
255 int val
= purple_prefs_get_int(pref_
);
257 setUnit(unit_fun_(val
));
258 signal_activate
.connect(sigc::mem_fun(this, &IntegerOption::onActivate
));
261 OptionWindow::IntegerOption::~IntegerOption()
266 void OptionWindow::IntegerOption::onActivate(Button
& /*activator*/)
268 auto dialog
= new CppConsUI::InputDialog(getText(), getValue());
269 dialog
->setFlags(CppConsUI::TextEntry::FLAG_NUMERIC
);
270 dialog
->signal_response
.connect(
271 sigc::mem_fun(this, &IntegerOption::responseHandler
));
275 void OptionWindow::IntegerOption::responseHandler(
276 CppConsUI::InputDialog
&activator
,
277 CppConsUI::AbstractDialog::ResponseType response
)
279 if (response
!= AbstractDialog::RESPONSE_OK
)
283 if (!Utils::stringToNumber(activator
.getText(), INT_MIN
, INT_MAX
, &num
))
286 purple_prefs_set_int(pref_
, num
);
287 int val
= purple_prefs_get_int(pref_
);
288 setValue(purple_prefs_get_int(pref_
));
290 setUnit(unit_fun_(val
));
293 OptionWindow::ChoiceOption::ChoiceOption(const char *text
, const char *config
)
296 g_assert(text
!= nullptr);
297 g_assert(config
!= nullptr);
299 pref_
= g_strdup(config
);
300 signal_selection_changed
.connect(
301 sigc::mem_fun(this, &ChoiceOption::onSelectionChanged
));
304 OptionWindow::ChoiceOption::~ChoiceOption()
306 for (ComboBoxEntry
&entry
: options_
)
307 g_free(reinterpret_cast<char *>(entry
.data
));
312 void OptionWindow::ChoiceOption::addOption(const char *title
, const char *value
)
314 g_assert(title
!= nullptr);
315 g_assert(value
!= nullptr);
317 int item
= addOptionPtr(title
, g_strdup(value
));
318 if (g_ascii_strcasecmp(purple_prefs_get_string(pref_
), value
) == 0)
322 void OptionWindow::ChoiceOption::onSelectionChanged(ComboBox
& /*activator*/,
323 int /*new_entry*/, const char * /*title*/, intptr_t data
)
325 purple_prefs_set_string(pref_
, reinterpret_cast<const char *>(data
));
328 const char *OptionWindow::getPercentUnit(int /*i*/) const
333 const char *OptionWindow::getMinUnit(int i
) const
335 return ngettext("minute", "minutes", i
);
338 void OptionWindow::reloadKeyBindings(CppConsUI::Button
& /*activator*/) const
340 if (CENTERIM
->loadKeyConfig())
341 LOG
->message(_("Keybinding file was successfully reloaded."));
344 void OptionWindow::reloadColorSchemes(CppConsUI::Button
& /*activator*/) const
346 if (CENTERIM
->loadColorSchemeConfig())
347 LOG
->message(_("Colorscheme file was successfully reloaded."));
350 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: