Use g_strcmp0() for code simplification
[pidgin-git.git] / pidgin / plugins / themeedit.c
blob0290d3607510985ffe58030ea9dd2b303a10350e
1 /* Pidgin
3 * Pidgin is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "pidgin.h"
23 #include "version.h"
25 #include "theme-manager.h"
27 #include "gtkblist.h"
28 #include "gtkblist-theme.h"
29 #include "gtkutils.h"
30 #include "gtkplugin.h"
32 #define PLUGIN_ID "gtk-theme-editor"
34 #include "themeedit-icon.h"
36 static gboolean
37 prop_type_is_color(PidginBlistTheme *theme, const char *prop)
39 PidginBlistThemeClass *klass = PIDGIN_BLIST_THEME_GET_CLASS(theme);
40 GParamSpec *spec = g_object_class_find_property(G_OBJECT_CLASS(klass), prop);
42 return G_IS_PARAM_SPEC_BOXED(spec);
45 #ifdef NOT_SADRUL
46 static void
47 save_blist_theme(GtkWidget *w, GtkWidget *window)
49 /* TODO: SAVE! */
50 gtk_widget_destroy(window);
52 #endif
54 static void
55 close_blist_theme(GtkWidget *w, GtkWidget *window)
57 gtk_widget_destroy(window);
60 static void
61 theme_color_selected(GtkDialog *dialog, gint response, const char *prop)
63 if (response == GTK_RESPONSE_OK) {
64 GtkWidget *colorsel;
65 GdkColor color;
66 PidginBlistTheme *theme;
68 #if GTK_CHECK_VERSION(2,14,0)
69 colorsel =
70 gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(dialog));
71 #else
72 colorsel = GTK_COLOR_SELECTION_DIALOG(dialog)->colorsel;
73 #endif
74 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel), &color);
76 theme = pidgin_blist_get_theme();
78 if (prop_type_is_color(theme, prop)) {
79 g_object_set(G_OBJECT(theme), prop, &color, NULL);
80 } else {
81 PidginThemeFont *font = NULL;
82 g_object_get(G_OBJECT(theme), prop, &font, NULL);
83 if (!font) {
84 font = pidgin_theme_font_new(NULL, &color);
85 g_object_set(G_OBJECT(theme), prop, font, NULL);
86 pidgin_theme_font_free(font);
87 } else {
88 pidgin_theme_font_set_color(font, &color);
91 pidgin_blist_set_theme(theme);
94 gtk_widget_destroy(GTK_WIDGET(dialog));
97 static void
98 theme_font_face_selected(GtkWidget *dialog, gint response, gpointer font)
100 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY) {
101 const char *fontname = gtk_font_selection_dialog_get_font_name(GTK_FONT_SELECTION_DIALOG(dialog));
102 pidgin_theme_font_set_font_face(font, fontname);
103 pidgin_blist_refresh(purple_get_blist());
105 gtk_widget_destroy(dialog);
108 static void
109 theme_font_select_face(GtkWidget *widget, gpointer prop)
111 GtkWidget *dialog;
112 PidginBlistTheme *theme;
113 PidginThemeFont *font = NULL;
114 const char *face;
116 theme = pidgin_blist_get_theme();
117 g_object_get(G_OBJECT(theme), prop, &font, NULL);
119 if (!font) {
120 font = pidgin_theme_font_new(NULL, NULL);
121 g_object_set(G_OBJECT(theme), prop, font, NULL);
122 pidgin_theme_font_free(font);
123 g_object_get(G_OBJECT(theme), prop, &font, NULL);
126 face = pidgin_theme_font_get_font_face(font);
127 dialog = gtk_font_selection_dialog_new(_("Select Font"));
128 if (face && *face)
129 gtk_font_selection_dialog_set_font_name(GTK_FONT_SELECTION_DIALOG(dialog),
130 face);
131 g_signal_connect(G_OBJECT(dialog), "response", G_CALLBACK(theme_font_face_selected),
132 font);
133 gtk_widget_show_all(dialog);
136 static void
137 theme_color_select(GtkWidget *widget, gpointer prop)
139 GtkWidget *dialog;
140 PidginBlistTheme *theme;
141 const GdkColor *color = NULL;
143 theme = pidgin_blist_get_theme();
145 if (prop_type_is_color(theme, prop)) {
146 g_object_get(G_OBJECT(theme), prop, &color, NULL);
147 } else {
148 PidginThemeFont *pair = NULL;
149 g_object_get(G_OBJECT(theme), prop, &pair, NULL);
150 if (pair)
151 color = pidgin_theme_font_get_color(pair);
154 dialog = gtk_color_selection_dialog_new(_("Select Color"));
155 #if GTK_CHECK_VERSION(2,14,0)
156 if (color)
157 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(
158 gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(dialog))),
159 color);
160 #else
161 if (color)
162 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dialog)->colorsel),
163 color);
164 #endif
165 g_signal_connect(G_OBJECT(dialog), "response", G_CALLBACK(theme_color_selected),
166 prop);
168 gtk_widget_show_all(dialog);
171 static GtkWidget *
172 pidgin_theme_create_color_selector(const char *text, const char *blurb, const char *prop,
173 GtkSizeGroup *sizegroup)
175 GtkWidget *color;
176 GtkWidget *hbox, *label;
178 hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_CAT_SPACE);
180 label = gtk_label_new(_(text));
181 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
182 gtk_size_group_add_widget(sizegroup, label);
183 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
184 #if GTK_CHECK_VERSION(2, 12, 0)
185 gtk_widget_set_tooltip_text(label, blurb);
186 #endif
188 color = pidgin_pixbuf_button_from_stock("", GTK_STOCK_SELECT_COLOR,
189 PIDGIN_BUTTON_HORIZONTAL);
190 g_signal_connect(G_OBJECT(color), "clicked", G_CALLBACK(theme_color_select),
191 (gpointer)prop);
192 gtk_box_pack_start(GTK_BOX(hbox), color, FALSE, FALSE, 0);
194 return hbox;
197 static GtkWidget *
198 pidgin_theme_create_font_selector(const char *text, const char *blurb, const char *prop,
199 GtkSizeGroup *sizegroup)
201 GtkWidget *color, *font;
202 GtkWidget *hbox, *label;
204 hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_CAT_SPACE);
206 label = gtk_label_new(_(text));
207 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
208 gtk_size_group_add_widget(sizegroup, label);
209 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
210 #if GTK_CHECK_VERSION(2, 12, 0)
211 gtk_widget_set_tooltip_text(label, blurb);
212 #endif
214 font = pidgin_pixbuf_button_from_stock("", GTK_STOCK_SELECT_FONT,
215 PIDGIN_BUTTON_HORIZONTAL);
216 g_signal_connect(G_OBJECT(font), "clicked", G_CALLBACK(theme_font_select_face),
217 (gpointer)prop);
218 gtk_box_pack_start(GTK_BOX(hbox), font, FALSE, FALSE, 0);
220 color = pidgin_pixbuf_button_from_stock("", GTK_STOCK_SELECT_COLOR,
221 PIDGIN_BUTTON_HORIZONTAL);
222 g_signal_connect(G_OBJECT(color), "clicked", G_CALLBACK(theme_color_select),
223 (gpointer)prop);
224 gtk_box_pack_start(GTK_BOX(hbox), color, FALSE, FALSE, 0);
226 return hbox;
229 static void
230 pidgin_blist_theme_edit(PurplePluginAction *unused)
232 GtkWidget *dialog;
233 GtkWidget *box;
234 GtkSizeGroup *group;
235 PidginBlistTheme *theme;
236 GObjectClass *klass;
237 int i, j;
238 static struct {
239 const char *header;
240 const char *props[12];
241 } sections[] = {
242 {N_("Contact"), {
243 "contact-color",
244 "contact",
245 "online",
246 "away",
247 "offline",
248 "idle",
249 "message",
250 "message_nick_said",
251 "status",
252 NULL
255 {N_("Group"), {
256 "expanded-color",
257 "expanded-text",
258 "collapsed-color",
259 "collapsed-text",
260 NULL
263 { NULL, { } }
266 dialog = pidgin_create_dialog(_("Pidgin Buddylist Theme Editor"), 0, "theme-editor-blist", FALSE);
267 box = pidgin_dialog_get_vbox_with_properties(GTK_DIALOG(dialog), FALSE, PIDGIN_HIG_BOX_SPACE);
269 theme = pidgin_blist_get_theme();
270 if (!theme) {
271 const char *author;
272 #ifndef _WIN32
273 author = getlogin();
274 #else
275 author = "user";
276 #endif
277 theme = g_object_new(PIDGIN_TYPE_BLIST_THEME, "type", "blist",
278 "author", author,
279 NULL);
280 pidgin_blist_set_theme(theme);
282 klass = G_OBJECT_CLASS(PIDGIN_BLIST_THEME_GET_CLASS(theme));
284 group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
285 for (i = 0; sections[i].header; i++) {
286 GtkWidget *vbox;
287 GtkWidget *hbox;
288 GParamSpec *spec;
290 vbox = pidgin_make_frame(box, _(sections[i].header));
291 for (j = 0; sections[i].props[j]; j++) {
292 const char *label;
293 const char *blurb;
294 spec = g_object_class_find_property(klass, sections[i].props[j]);
295 label = g_param_spec_get_nick(spec);
296 blurb = g_param_spec_get_blurb(spec);
297 if (G_IS_PARAM_SPEC_BOXED(spec)) {
298 hbox = pidgin_theme_create_color_selector(label, blurb,
299 sections[i].props[j], group);
300 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
301 } else {
302 hbox = pidgin_theme_create_font_selector(label, blurb,
303 sections[i].props[j], group);
304 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
309 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), TRUE);
310 #ifdef NOT_SADRUL
311 pidgin_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_SAVE, G_CALLBACK(save_blist_theme), dialog);
312 #endif
313 pidgin_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CLOSE, G_CALLBACK(close_blist_theme), dialog);
315 gtk_widget_show_all(dialog);
317 g_object_unref(group);
320 static gboolean
321 plugin_load(PurplePlugin *plugin)
323 return TRUE;
326 static GList *
327 actions(PurplePlugin *plugin, gpointer context)
329 GList *l = NULL;
330 PurplePluginAction *act = NULL;
332 act = purple_plugin_action_new(_("Edit Buddylist Theme"), pidgin_blist_theme_edit);
333 l = g_list_append(l, act);
334 act = purple_plugin_action_new(_("Edit Icon Theme"), pidgin_icon_theme_edit);
335 l = g_list_append(l, act);
337 return l;
340 static PurplePluginInfo info =
342 PURPLE_PLUGIN_MAGIC,
343 PURPLE_MAJOR_VERSION,
344 PURPLE_MINOR_VERSION,
345 PURPLE_PLUGIN_STANDARD, /**< type */
346 PIDGIN_PLUGIN_TYPE, /**< ui_requirement */
347 0, /**< flags */
348 NULL, /**< dependencies */
349 PURPLE_PRIORITY_DEFAULT, /**< priority */
351 PLUGIN_ID, /**< id */
352 N_("Pidgin Theme Editor"), /**< name */
353 DISPLAY_VERSION, /**< version */
354 /** summary */
355 N_("Pidgin Theme Editor."),
356 /** description */
357 N_("Pidgin Theme Editor"),
358 "Sadrul Habib Chowdhury <imadil@gmail.com>", /**< author */
359 PURPLE_WEBSITE, /**< homepage */
361 plugin_load, /**< load */
362 NULL, /**< unload */
363 NULL, /**< destroy */
365 NULL, /**< ui_info */
366 NULL, /**< extra_info */
367 NULL,
368 actions,
370 /* padding */
371 NULL,
372 NULL,
373 NULL,
374 NULL
377 static void
378 init_plugin(PurplePlugin *plugin)
382 PURPLE_INIT_PLUGIN(themeeditor, init_plugin, info)