Replace strcmp() with purple_strequal()
[pidgin-git.git] / pidgin / gtkpluginpref.c
blob024575a034ccb6cfff6049e90e62f1ff07e2c54a
1 /**
2 * @file gtkpluginpref.c GTK+ Plugin preferences
3 * @ingroup pidgin
4 */
6 /* pidgin
8 * Pidgin is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
30 #include "debug.h"
31 #include "internal.h"
32 #include "pluginpref.h"
33 #include "prefs.h"
35 #include "gtkimhtml.h"
36 #include "gtkpluginpref.h"
37 #include "gtkprefs.h"
38 #include "gtkutils.h"
40 static gboolean
41 entry_cb(GtkWidget *entry, gpointer data) {
42 char *pref = data;
44 purple_prefs_set_string(pref, gtk_entry_get_text(GTK_ENTRY(entry)));
46 return FALSE;
50 static void
51 imhtml_cb(GtkTextBuffer *buffer, gpointer data)
53 char *pref;
54 char *text;
55 GtkIMHtml *imhtml = data;
57 pref = g_object_get_data(G_OBJECT(imhtml), "pref-key");
58 g_return_if_fail(pref);
60 text = gtk_imhtml_get_markup(imhtml);
61 purple_prefs_set_string(pref, text);
62 g_free(text);
65 static void
66 imhtml_format_cb(GtkIMHtml *imhtml, GtkIMHtmlButtons buttons, gpointer data)
68 imhtml_cb(gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml)), data);
71 static void
72 make_string_pref(GtkWidget *parent, PurplePluginPref *pref, GtkSizeGroup *sg) {
73 GtkWidget *box, *gtk_label, *entry;
74 const gchar *pref_name;
75 const gchar *pref_label;
76 PurpleStringFormatType format;
78 pref_name = purple_plugin_pref_get_name(pref);
79 pref_label = purple_plugin_pref_get_label(pref);
80 format = purple_plugin_pref_get_format_type(pref);
82 switch(purple_plugin_pref_get_type(pref)) {
83 case PURPLE_PLUGIN_PREF_CHOICE:
84 gtk_label = pidgin_prefs_dropdown_from_list(parent, pref_label,
85 PURPLE_PREF_STRING, pref_name,
86 purple_plugin_pref_get_choices(pref));
87 gtk_misc_set_alignment(GTK_MISC(gtk_label), 0, 0.5);
89 if(sg)
90 gtk_size_group_add_widget(sg, gtk_label);
92 break;
93 case PURPLE_PLUGIN_PREF_NONE:
94 default:
95 if (format == PURPLE_STRING_FORMAT_TYPE_NONE)
97 entry = gtk_entry_new();
98 gtk_entry_set_text(GTK_ENTRY(entry), purple_prefs_get_string(pref_name));
99 gtk_entry_set_max_length(GTK_ENTRY(entry),
100 purple_plugin_pref_get_max_length(pref));
101 if (purple_plugin_pref_get_masked(pref))
103 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
104 #if !GTK_CHECK_VERSION(2,16,0)
105 if (gtk_entry_get_invisible_char(GTK_ENTRY(entry)) == '*')
106 gtk_entry_set_invisible_char(GTK_ENTRY(entry), PIDGIN_INVISIBLE_CHAR);
107 #endif /* Less than GTK+ 2.16 */
109 g_signal_connect(G_OBJECT(entry), "changed",
110 G_CALLBACK(entry_cb),
111 (gpointer)pref_name);
112 pidgin_add_widget_to_vbox(GTK_BOX(parent), pref_label, sg, entry, TRUE, NULL);
114 else
116 GtkWidget *hbox;
117 GtkWidget *spacer;
118 GtkWidget *imhtml;
119 GtkWidget *toolbar;
120 GtkWidget *frame;
122 box = gtk_vbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
124 gtk_widget_show(box);
125 gtk_box_pack_start(GTK_BOX(parent), box, FALSE, FALSE, 0);
127 gtk_label = gtk_label_new_with_mnemonic(pref_label);
128 gtk_misc_set_alignment(GTK_MISC(gtk_label), 0, 0.5);
129 gtk_widget_show(gtk_label);
130 gtk_box_pack_start(GTK_BOX(box), gtk_label, FALSE, FALSE, 0);
132 if(sg)
133 gtk_size_group_add_widget(sg, gtk_label);
135 hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
136 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
137 gtk_widget_show(hbox);
139 spacer = gtk_label_new(" ");
140 gtk_box_pack_start(GTK_BOX(hbox), spacer, FALSE, FALSE, 0);
141 gtk_widget_show(spacer);
143 frame = pidgin_create_imhtml(TRUE, &imhtml, &toolbar, NULL);
144 if (!(format & PURPLE_STRING_FORMAT_TYPE_HTML))
145 gtk_widget_destroy(toolbar);
147 gtk_imhtml_append_text(GTK_IMHTML(imhtml), purple_prefs_get_string(pref_name),
148 (format & PURPLE_STRING_FORMAT_TYPE_MULTILINE) ? 0 : GTK_IMHTML_NO_NEWLINE);
149 gtk_label_set_mnemonic_widget(GTK_LABEL(gtk_label), imhtml);
150 gtk_widget_show_all(frame);
151 g_object_set_data(G_OBJECT(imhtml), "pref-key", (gpointer)pref_name);
152 g_signal_connect(G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml))),
153 "changed", G_CALLBACK(imhtml_cb), imhtml);
154 g_signal_connect(G_OBJECT(imhtml),
155 "format_function_toggle", G_CALLBACK(imhtml_format_cb), imhtml);
156 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, 0);
159 break;
163 static void
164 make_int_pref(GtkWidget *parent, PurplePluginPref *pref, GtkSizeGroup *sg) {
165 GtkWidget *gtk_label;
166 const gchar *pref_name;
167 const gchar *pref_label;
168 gint max, min;
170 pref_name = purple_plugin_pref_get_name(pref);
171 pref_label = purple_plugin_pref_get_label(pref);
173 switch(purple_plugin_pref_get_type(pref)) {
174 case PURPLE_PLUGIN_PREF_CHOICE:
175 gtk_label = pidgin_prefs_dropdown_from_list(parent, pref_label,
176 PURPLE_PREF_INT, pref_name, purple_plugin_pref_get_choices(pref));
177 gtk_misc_set_alignment(GTK_MISC(gtk_label), 0, 0.5);
179 if(sg)
180 gtk_size_group_add_widget(sg, gtk_label);
182 break;
183 case PURPLE_PLUGIN_PREF_NONE:
184 default:
185 purple_plugin_pref_get_bounds(pref, &min, &max);
186 pidgin_prefs_labeled_spin_button(parent, pref_label,
187 pref_name, min, max, sg);
188 break;
193 static void
194 make_info_pref(GtkWidget *parent, PurplePluginPref *pref) {
195 GtkWidget *gtk_label = gtk_label_new(purple_plugin_pref_get_label(pref));
196 gtk_misc_set_alignment(GTK_MISC(gtk_label), 0, 0);
197 gtk_label_set_line_wrap(GTK_LABEL(gtk_label), TRUE);
198 gtk_box_pack_start(GTK_BOX(parent), gtk_label, FALSE, FALSE, 0);
199 gtk_widget_show(gtk_label);
203 GtkWidget *
204 pidgin_plugin_pref_create_frame(PurplePluginPrefFrame *frame) {
205 GtkWidget *ret, *parent;
206 GtkSizeGroup *sg;
207 GList *pp;
209 g_return_val_if_fail(frame, NULL);
211 sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
213 parent = ret = gtk_vbox_new(FALSE, 16);
214 gtk_container_set_border_width(GTK_CONTAINER(ret), PIDGIN_HIG_BORDER);
215 gtk_widget_show(ret);
217 for(pp = purple_plugin_pref_frame_get_prefs(frame);
218 pp != NULL;
219 pp = pp->next)
221 PurplePluginPref *pref = (PurplePluginPref *)pp->data;
223 const char *name = purple_plugin_pref_get_name(pref);
224 const char *label = purple_plugin_pref_get_label(pref);
226 if(name == NULL) {
227 if(label == NULL)
228 continue;
230 if(purple_plugin_pref_get_type(pref) == PURPLE_PLUGIN_PREF_INFO) {
231 make_info_pref(parent, pref);
232 } else {
233 parent = pidgin_make_frame(ret, label);
234 gtk_widget_show(parent);
237 continue;
240 switch(purple_prefs_get_type(name)) {
241 case PURPLE_PREF_BOOLEAN:
242 pidgin_prefs_checkbox(label, name, parent);
243 break;
244 case PURPLE_PREF_INT:
245 make_int_pref(parent, pref, sg);
246 break;
247 case PURPLE_PREF_STRING:
248 make_string_pref(parent, pref, sg);
249 break;
250 default:
251 break;
255 g_object_unref(sg);
257 return ret;