Mon, 08 Jan 2007 04:26:16 +0000
[gaim-migrate @ 18086]
core-ify some code...and someone tell me why my gvim and svn aren't getting along and I have to type out this damn message on the command line
| 15231 | 1 | /* |
| 2 | * Conversation Colors | |
| 3 | * Copyright (C) 2006 | |
| 4 | * | |
| 5 | * This program is free software; you can redistribute it and/or | |
| 6 | * modify it under the terms of the GNU General Public License as | |
| 7 | * published by the Free Software Foundation; either version 2 of the | |
| 8 | * License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This program is distributed in the hope that it will be useful, but | |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with this program; if not, write to the Free Software | |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
| 18 | * 02111-1307, USA. | |
| 19 | */ | |
| 20 | #include "internal.h" | |
| 21 | ||
| 22 | #define PLUGIN_ID "gtk-plugin_pack-convcolors" | |
| 23 | #define PLUGIN_NAME "Conversation Colors" | |
| 24 | #define PLUGIN_STATIC_NAME "Conversation Colors" | |
| 25 | #define PLUGIN_SUMMARY "Customize colors in the conversation window" | |
| 26 | #define PLUGIN_DESCRIPTION "Customize colors in the conversation window" | |
| 27 | #define PLUGIN_AUTHOR "Sadrul H Chowdhury <sadrul@users.sourceforge.net>" | |
| 28 | ||
| 29 | /* System headers */ | |
| 30 | #include <gdk/gdk.h> | |
| 31 | #include <glib.h> | |
| 32 | #include <gtk/gtk.h> | |
| 33 | ||
| 34 | /* Gaim headers */ | |
| 35 | #include <gtkplugin.h> | |
| 36 | #include <version.h> | |
| 37 | ||
| 38 | #include <conversation.h> | |
| 39 | #include <gtkconv.h> | |
| 40 | #include <gtkprefs.h> | |
| 41 | #include <gtkutils.h> | |
| 42 | ||
| 43 | #define PREF_PREFIX "/plugins/gtk/" PLUGIN_ID | |
| 44 | #define PREF_IGNORE PREF_PREFIX "/ignore_incoming" | |
| 45 | #define PREF_CHATS PREF_PREFIX "/chats" | |
| 46 | #define PREF_IMS PREF_PREFIX "/ims" | |
| 47 | ||
| 48 | #define PREF_SEND PREF_PREFIX "/send" | |
| 49 | #define PREF_SEND_C PREF_SEND "/color" | |
| 50 | #define PREF_SEND_F PREF_SEND "/format" | |
| 51 | ||
| 52 | #define PREF_RECV PREF_PREFIX "/recv" | |
| 53 | #define PREF_RECV_C PREF_RECV "/color" | |
| 54 | #define PREF_RECV_F PREF_RECV "/format" | |
| 55 | ||
| 56 | #define PREF_SYSTEM PREF_PREFIX "/system" | |
| 57 | #define PREF_SYSTEM_C PREF_SYSTEM "/color" | |
| 58 | #define PREF_SYSTEM_F PREF_SYSTEM "/format" | |
| 59 | ||
| 60 | #define PREF_ERROR PREF_PREFIX "/error" | |
| 61 | #define PREF_ERROR_C PREF_ERROR "/color" | |
| 62 | #define PREF_ERROR_F PREF_ERROR "/format" | |
| 63 | ||
| 64 | #define PREF_NICK PREF_PREFIX "/nick" | |
| 65 | #define PREF_NICK_C PREF_NICK "/color" | |
| 66 | #define PREF_NICK_F PREF_NICK "/format" | |
| 67 | ||
| 68 | enum | |
| 69 | { | |
| 70 | FONT_BOLD = 1 << 0, | |
| 71 | FONT_ITALIC = 1 << 1, | |
| 72 | FONT_UNDERLINE = 1 << 2 | |
| 73 | }; | |
| 74 | ||
| 75 | struct | |
| 76 | { | |
| 77 | GaimMessageFlags flag; | |
| 78 | char *prefix; | |
| 79 | const char *text; | |
| 80 | } formats[] = | |
| 81 | { | |
| 82 | {GAIM_MESSAGE_ERROR, PREF_ERROR, N_("Error Messages")}, | |
| 83 | {GAIM_MESSAGE_NICK, PREF_NICK, N_("Highlighted Messages")}, | |
| 84 | {GAIM_MESSAGE_SYSTEM, PREF_SYSTEM, N_("System Messages")}, | |
| 85 | {GAIM_MESSAGE_SEND, PREF_SEND, N_("Sent Messages")}, | |
| 86 | {GAIM_MESSAGE_RECV, PREF_RECV, N_("Received Messages")}, | |
| 87 | {0, NULL, NULL} | |
| 88 | }; | |
| 89 | ||
| 90 | static gboolean | |
| 91 | displaying_msg(GaimAccount *account, const char *who, char **displaying, | |
| 92 | GaimConversation *conv, GaimMessageFlags flags) | |
| 93 | { | |
| 94 | int i; | |
| 95 | char tmp[128], *t; | |
| 96 | gboolean bold, italic, underline; | |
| 97 | int f; | |
| 98 | const char *color; | |
| 99 | ||
| 100 | for (i = 0; formats[i].prefix; i++) | |
| 101 | if (flags & formats[i].flag) | |
| 102 | break; | |
| 103 | ||
| 104 | if (!formats[i].prefix) | |
| 105 | return FALSE; | |
| 106 | ||
| 107 | if ((gaim_conversation_get_type(conv) == GAIM_CONV_TYPE_IM && | |
| 108 | !gaim_prefs_get_bool(PREF_IMS)) || | |
| 109 | (gaim_conversation_get_type(conv) == GAIM_CONV_TYPE_CHAT && | |
| 110 | !gaim_prefs_get_bool(PREF_CHATS))) | |
| 111 | return FALSE; | |
| 112 | ||
| 113 | g_snprintf(tmp, sizeof(tmp), "%s/color", formats[i].prefix); | |
| 114 | color = gaim_prefs_get_string(tmp); | |
| 115 | ||
| 116 | g_snprintf(tmp, sizeof(tmp), "%s/format", formats[i].prefix); | |
| 117 | f = gaim_prefs_get_int(tmp); | |
| 118 | bold = (f & FONT_BOLD); | |
| 119 | italic = (f & FONT_ITALIC); | |
| 120 | underline = (f & FONT_UNDERLINE); | |
| 121 | ||
| 122 | if (gaim_prefs_get_bool(PREF_IGNORE)) | |
| 123 | { | |
| 124 | t = *displaying; | |
| 125 | *displaying = gaim_markup_strip_html(t); | |
| 126 | g_free(t); | |
| 127 | } | |
| 128 | ||
| 129 | if (color && *color) | |
| 130 | { | |
| 131 | t = *displaying; | |
| 132 | *displaying = g_strdup_printf("<FONT COLOR=\"%s\">%s</FONT>", color, t); | |
| 133 | g_free(t); | |
| 134 | } | |
| 135 | ||
| 136 | t = *displaying; | |
| 137 | *displaying = g_strdup_printf("%s%s%s%s%s%s%s", | |
| 138 | bold ? "<B>" : "</B>", | |
| 139 | italic ? "<I>" : "</I>", | |
| 140 | underline ? "<U>" : "</U>", | |
| 141 | t, | |
| 142 | bold ? "</B>" : "<B>", | |
| 143 | italic ? "</I>" : "<I>", | |
| 144 | underline ? "</U>" : "<U>" | |
| 145 | ); | |
| 146 | g_free(t); | |
| 147 | ||
| 148 | return FALSE; | |
| 149 | } | |
| 150 | ||
| 151 | static gboolean | |
| 152 | plugin_load(GaimPlugin *plugin) | |
| 153 | { | |
| 154 | gaim_signal_connect(gaim_gtk_conversations_get_handle(), | |
| 155 | "displaying-im-msg", plugin, | |
| 156 | GAIM_CALLBACK(displaying_msg), NULL); | |
| 157 | gaim_signal_connect(gaim_gtk_conversations_get_handle(), | |
| 158 | "displaying-chat-msg", plugin, | |
| 159 | GAIM_CALLBACK(displaying_msg), NULL); | |
| 160 | return TRUE; | |
| 161 | } | |
| 162 | ||
| 163 | static gboolean | |
| 164 | plugin_unload(GaimPlugin *plugin) | |
| 165 | { | |
| 166 | return TRUE; | |
| 167 | } | |
| 168 | ||
| 169 | /* Ripped from GaimRC */ | |
| 170 | static void | |
| 171 | color_response(GtkDialog *color_dialog, gint response, const char *data) | |
| 172 | { | |
| 173 | if (response == GTK_RESPONSE_OK) | |
| 174 | { | |
| 175 | GtkWidget *colorsel = GTK_COLOR_SELECTION_DIALOG(color_dialog)->colorsel; | |
| 176 | GdkColor color; | |
| 177 | char colorstr[8]; | |
| 178 | char tmp[128]; | |
| 179 | ||
| 180 | gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel), &color); | |
| 181 | ||
| 182 | g_snprintf(colorstr, sizeof(colorstr), "#%02X%02X%02X", | |
| 183 | color.red/256, color.green/256, color.blue/256); | |
| 184 | ||
| 185 | g_snprintf(tmp, sizeof(tmp), "%s/color", data); | |
| 186 | ||
| 187 | gaim_prefs_set_string(tmp, colorstr); | |
| 188 | } | |
| 189 | ||
| 190 | gtk_widget_destroy(GTK_WIDGET(color_dialog)); | |
| 191 | } | |
| 192 | ||
| 193 | static void | |
| 194 | set_color(GtkWidget *widget, const char *data) | |
| 195 | { | |
| 196 | GtkWidget *color_dialog = NULL; | |
| 197 | GdkColor color; | |
| 198 | char title[128]; | |
| 199 | char tmp[128]; | |
| 200 | ||
| 201 | g_snprintf(title, sizeof(title), _("Select Color for %s"), _(data)); | |
| 202 | color_dialog = gtk_color_selection_dialog_new(title); | |
| 203 | g_signal_connect(G_OBJECT(color_dialog), "response", | |
| 204 | G_CALLBACK(color_response), (gpointer)data); | |
| 205 | ||
| 206 | g_snprintf(tmp, sizeof(tmp), "%s/color", data); | |
| 207 | if (gdk_color_parse(gaim_prefs_get_string(tmp), &color)) | |
| 208 | { | |
| 209 | gtk_color_selection_set_current_color( | |
| 210 | GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(color_dialog)->colorsel), &color); | |
| 211 | } | |
| 212 | ||
| 213 | gtk_window_present(GTK_WINDOW(color_dialog)); | |
| 214 | } | |
| 215 | ||
| 216 | static void | |
| 217 | toggle_something(const char *prefix, int format) | |
| 218 | { | |
| 219 | int f; | |
| 220 | char tmp[128]; | |
| 221 | ||
| 222 | g_snprintf(tmp, sizeof(tmp), "%s/format", prefix); | |
| 223 | f = gaim_prefs_get_int(tmp); | |
| 224 | f ^= format; | |
| 225 | gaim_prefs_set_int(tmp, f); | |
| 226 | } | |
| 227 | ||
| 228 | static void | |
| 229 | toggle_bold(GtkWidget *widget, gpointer data) | |
| 230 | { | |
| 231 | toggle_something(data, FONT_BOLD); | |
| 232 | } | |
| 233 | ||
| 234 | static void | |
| 235 | toggle_italic(GtkWidget *widget, gpointer data) | |
| 236 | { | |
| 237 | toggle_something(data, FONT_ITALIC); | |
| 238 | } | |
| 239 | ||
| 240 | static void | |
| 241 | toggle_underline(GtkWidget *widget, gpointer data) | |
| 242 | { | |
| 243 | toggle_something(data, FONT_UNDERLINE); | |
| 244 | } | |
| 245 | ||
| 246 | static GtkWidget * | |
| 247 | get_config_frame(GaimPlugin *plugin) | |
| 248 | { | |
| 249 | GtkWidget *ret; | |
| 250 | GtkWidget *frame; | |
| 251 | int i; | |
| 252 | ||
| 253 | ret = gtk_vbox_new(FALSE, GAIM_HIG_CAT_SPACE); | |
| 254 | gtk_container_set_border_width(GTK_CONTAINER(ret), GAIM_HIG_BORDER); | |
| 255 | ||
| 256 | for (i = 0; formats[i].prefix; i++) | |
| 257 | { | |
| 258 | char tmp[128]; | |
| 259 | int f; | |
| 260 | GtkWidget *vbox, *hbox, *button; | |
| 261 | ||
| 262 | g_snprintf(tmp, sizeof(tmp), "%s/format", formats[i].prefix); | |
| 263 | f = gaim_prefs_get_int(tmp); | |
| 264 | ||
| 265 | frame = gaim_gtk_make_frame(ret, _(formats[i].text)); | |
| 266 | vbox = gtk_vbox_new(FALSE, GAIM_HIG_BOX_SPACE); | |
| 267 | gtk_box_pack_start(GTK_BOX(frame), vbox, FALSE, FALSE, 0); | |
| 268 | ||
| 269 | hbox = gtk_hbox_new(FALSE, GAIM_HIG_BOX_SPACE); | |
| 270 | gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); | |
| 271 | ||
| 272 | button = gaim_pixbuf_button_from_stock(" Color", GTK_STOCK_SELECT_COLOR, | |
| 273 | GAIM_BUTTON_HORIZONTAL); | |
| 274 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 275 | g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(set_color), | |
| 276 | formats[i].prefix); | |
| 277 | ||
| 278 | button = gtk_check_button_new_with_label(_("Bold")); | |
| 279 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 280 | if (f & FONT_BOLD) | |
| 281 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE); | |
| 282 | g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(toggle_bold), | |
| 283 | formats[i].prefix); | |
| 284 | ||
| 285 | button = gtk_check_button_new_with_label(_("Italic")); | |
| 286 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 287 | if (f & FONT_ITALIC) | |
| 288 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE); | |
| 289 | g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(toggle_italic), | |
| 290 | formats[i].prefix); | |
| 291 | ||
| 292 | button = gtk_check_button_new_with_label(_("Underline")); | |
| 293 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 294 | if (f & FONT_UNDERLINE) | |
| 295 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE); | |
| 296 | g_signal_connect(G_OBJECT(button), "clicked", | |
| 297 | G_CALLBACK(toggle_underline), formats[i].prefix); | |
| 298 | } | |
| 299 | ||
| 300 | frame = gaim_gtk_make_frame(ret, _("General")); | |
| 301 | gaim_gtk_prefs_checkbox(_("Ignore incoming format"), PREF_IGNORE, frame); | |
| 302 | gaim_gtk_prefs_checkbox(_("Apply in Chats"), PREF_CHATS, frame); | |
| 303 | gaim_gtk_prefs_checkbox(_("Apply in IMs"), PREF_IMS, frame); | |
| 304 | ||
| 305 | gtk_widget_show_all(ret); | |
| 306 | return ret; | |
| 307 | } | |
| 308 | ||
| 309 | static GaimGtkPluginUiInfo ui_info = | |
| 310 | { | |
| 311 | get_config_frame, | |
| 312 | 0, | |
| 313 | }; | |
| 314 | ||
| 315 | static GaimPluginInfo info = | |
| 316 | { | |
| 317 | GAIM_PLUGIN_MAGIC, /* Magic */ | |
| 318 | GAIM_MAJOR_VERSION, /* Gaim Major Version */ | |
| 319 | GAIM_MINOR_VERSION, /* Gaim Minor Version */ | |
| 320 | GAIM_PLUGIN_STANDARD, /* plugin type */ | |
| 321 | GAIM_GTK_PLUGIN_TYPE, /* ui requirement */ | |
| 322 | 0, /* flags */ | |
| 323 | NULL, /* dependencies */ | |
| 324 | GAIM_PRIORITY_DEFAULT, /* priority */ | |
| 325 | ||
| 326 | PLUGIN_ID, /* plugin id */ | |
| 327 | N_(PLUGIN_NAME), /* name */ | |
| 328 | VERSION, /* version */ | |
| 329 | N_(PLUGIN_SUMMARY), /* summary */ | |
| 330 | N_(PLUGIN_DESCRIPTION), /* description */ | |
| 331 | PLUGIN_AUTHOR, /* author */ | |
| 332 | GAIM_WEBSITE, /* website */ | |
| 333 | ||
| 334 | plugin_load, /* load */ | |
| 335 | plugin_unload, /* unload */ | |
| 336 | NULL, /* destroy */ | |
| 337 | ||
| 338 | &ui_info, /* ui_info */ | |
| 339 | NULL, /* extra_info */ | |
| 340 | NULL, /* prefs_info */ | |
| 341 | NULL /* actions */ | |
| 342 | }; | |
| 343 | ||
| 344 | static void | |
| 345 | init_plugin(GaimPlugin *plugin) | |
| 346 | { | |
| 347 | gaim_prefs_add_none(PREF_PREFIX); | |
| 348 | ||
| 349 | gaim_prefs_add_bool(PREF_IGNORE, TRUE); | |
| 350 | gaim_prefs_add_bool(PREF_CHATS, TRUE); | |
| 351 | gaim_prefs_add_bool(PREF_IMS, TRUE); | |
| 352 | ||
| 353 | gaim_prefs_add_none(PREF_SEND); | |
| 354 | gaim_prefs_add_none(PREF_RECV); | |
| 355 | gaim_prefs_add_none(PREF_SYSTEM); | |
| 356 | gaim_prefs_add_none(PREF_ERROR); | |
| 357 | gaim_prefs_add_none(PREF_NICK); | |
| 358 | ||
| 359 | gaim_prefs_add_string(PREF_SEND_C, "#909090"); | |
| 360 | gaim_prefs_add_string(PREF_RECV_C, "#000000"); | |
| 361 | gaim_prefs_add_string(PREF_SYSTEM_C, "#50a050"); | |
| 362 | gaim_prefs_add_string(PREF_ERROR_C, "#ff0000"); | |
| 363 | gaim_prefs_add_string(PREF_NICK_C, "#0000dd"); | |
| 364 | ||
| 365 | gaim_prefs_add_int(PREF_SEND_F, 0); | |
| 366 | gaim_prefs_add_int(PREF_RECV_F, 0); | |
| 367 | gaim_prefs_add_int(PREF_SYSTEM_F, FONT_ITALIC); | |
| 368 | gaim_prefs_add_int(PREF_ERROR_F, FONT_BOLD | FONT_UNDERLINE); | |
| 369 | gaim_prefs_add_int(PREF_NICK_F, FONT_BOLD); | |
| 370 | } | |
| 371 | ||
| 372 | GAIM_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info) |