1 /* Pidgin
2  *
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.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "internal.h"
22 #include "pidgin.h"
23 #include "version.h"
24 
25 #include "theme-manager.h"
26 
27 #include "gtkblist.h"
28 #include "gtkblist-theme.h"
29 #include "gtkutils.h"
30 #include "gtkplugin.h"
31 
32 #define PLUGIN_ID "gtk-theme-editor"
33 
34 #include "themeedit-icon.h"
35 
36 static gboolean
prop_type_is_color(PidginBlistTheme * theme,const char * prop)37 prop_type_is_color(PidginBlistTheme *theme, const char *prop)
38 {
39 	PidginBlistThemeClass *klass = PIDGIN_BLIST_THEME_GET_CLASS(theme);
40 	GParamSpec *spec = g_object_class_find_property(G_OBJECT_CLASS(klass), prop);
41 
42 	return G_IS_PARAM_SPEC_BOXED(spec);
43 }
44 
45 #ifdef NOT_SADRUL
46 static void
save_blist_theme(GtkWidget * w,GtkWidget * window)47 save_blist_theme(GtkWidget *w, GtkWidget *window)
48 {
49 	/* TODO: SAVE! */
50 	gtk_widget_destroy(window);
51 }
52 #endif
53 
54 static void
close_blist_theme(GtkWidget * w,GtkWidget * window)55 close_blist_theme(GtkWidget *w, GtkWidget *window)
56 {
57 	gtk_widget_destroy(window);
58 }
59 
60 static void
theme_color_selected(GtkDialog * dialog,gint response,const char * prop)61 theme_color_selected(GtkDialog *dialog, gint response, const char *prop)
62 {
63 	if (response == GTK_RESPONSE_OK) {
64 		GtkWidget *colorsel;
65 		GdkColor color;
66 		PidginBlistTheme *theme;
67 
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);
75 
76 		theme = pidgin_blist_get_theme();
77 
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);
89 			}
90 		}
91 		pidgin_blist_set_theme(theme);
92 	}
93 
94 	gtk_widget_destroy(GTK_WIDGET(dialog));
95 }
96 
97 static void
theme_font_face_selected(GtkWidget * dialog,gint response,gpointer font)98 theme_font_face_selected(GtkWidget *dialog, gint response, gpointer font)
99 {
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());
104 	}
105 	gtk_widget_destroy(dialog);
106 }
107 
108 static void
theme_font_select_face(GtkWidget * widget,gpointer prop)109 theme_font_select_face(GtkWidget *widget, gpointer prop)
110 {
111 	GtkWidget *dialog;
112 	PidginBlistTheme *theme;
113 	PidginThemeFont *font = NULL;
114 	const char *face;
115 
116 	theme = pidgin_blist_get_theme();
117 	g_object_get(G_OBJECT(theme), prop, &font, NULL);
118 
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);
124 	}
125 
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);
134 }
135 
136 static void
theme_color_select(GtkWidget * widget,gpointer prop)137 theme_color_select(GtkWidget *widget, gpointer prop)
138 {
139 	GtkWidget *dialog;
140 	PidginBlistTheme *theme;
141 	const GdkColor *color = NULL;
142 
143 	theme = pidgin_blist_get_theme();
144 
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);
152 	}
153 
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);
167 
168 	gtk_widget_show_all(dialog);
169 }
170 
171 static GtkWidget *
pidgin_theme_create_color_selector(const char * text,const char * blurb,const char * prop,GtkSizeGroup * sizegroup)172 pidgin_theme_create_color_selector(const char *text, const char *blurb, const char *prop,
173 		GtkSizeGroup *sizegroup)
174 {
175 	GtkWidget *color;
176 	GtkWidget *hbox, *label;
177 
178 	hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_CAT_SPACE);
179 
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
187 
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);
193 
194 	return hbox;
195 }
196 
197 static GtkWidget *
pidgin_theme_create_font_selector(const char * text,const char * blurb,const char * prop,GtkSizeGroup * sizegroup)198 pidgin_theme_create_font_selector(const char *text, const char *blurb, const char *prop,
199 		GtkSizeGroup *sizegroup)
200 {
201 	GtkWidget *color, *font;
202 	GtkWidget *hbox, *label;
203 
204 	hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_CAT_SPACE);
205 
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
213 
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);
219 
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);
225 
226 	return hbox;
227 }
228 
229 static void
pidgin_blist_theme_edit(PurplePluginAction * unused)230 pidgin_blist_theme_edit(PurplePluginAction *unused)
231 {
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
253 				}
254 		},
255 		{N_("Group"), {
256 				      "expanded-color",
257 				      "expanded-text",
258 				      "collapsed-color",
259 				      "collapsed-text",
260 				      NULL
261 			      }
262 		},
263 		{ NULL, { } }
264 	};
265 
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);
268 
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);
281 	}
282 	klass = G_OBJECT_CLASS(PIDGIN_BLIST_THEME_GET_CLASS(theme));
283 
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;
289 
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);
305 			}
306 		}
307 	}
308 
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);
314 
315 	gtk_widget_show_all(dialog);
316 
317 	g_object_unref(group);
318 }
319 
320 static gboolean
plugin_load(PurplePlugin * plugin)321 plugin_load(PurplePlugin *plugin)
322 {
323 	return TRUE;
324 }
325 
326 static GList *
actions(PurplePlugin * plugin,gpointer context)327 actions(PurplePlugin *plugin, gpointer context)
328 {
329 	GList *l = NULL;
330 	PurplePluginAction *act = NULL;
331 
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);
336 
337 	return l;
338 }
339 
340 static PurplePluginInfo info =
341 {
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       */
350 
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       */
360 
361 	plugin_load,                           /**< load           */
362 	NULL,                                  /**< unload         */
363 	NULL,                                  /**< destroy        */
364 
365 	NULL,                                  /**< ui_info        */
366 	NULL,                                  /**< extra_info     */
367 	NULL,
368 	actions,
369 
370 	/* padding */
371 	NULL,
372 	NULL,
373 	NULL,
374 	NULL
375 };
376 
377 static void
init_plugin(PurplePlugin * plugin)378 init_plugin(PurplePlugin *plugin)
379 {
380 }
381 
382 PURPLE_INIT_PLUGIN(themeeditor, init_plugin, info)
383