Mercurial > pidgin
annotate src/gtkstatusselector.c @ 10261:d4e9ff2edc4e
[gaim-migrate @ 11405]
This should fix segfault bug 1072604. Oops.
committer: Tailor Script <tailor@pidgin.im>
| author | Tim Ringenbach <marv@pidgin.im> |
|---|---|
| date | Thu, 25 Nov 2004 18:35:26 +0000 |
| parents | a7c8a8735a31 |
| children | ec140184437b |
| rev | line source |
|---|---|
| 10178 | 1 /** |
| 2 * @file gtkstatusselector.c Status selector widget | |
| 3 * @ingroup gtkui | |
| 4 * | |
| 5 * gaim | |
| 6 * | |
| 7 * Gaim is the legal property of its developers, whose names are too numerous | |
| 8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 9 * source distribution. | |
| 10 * | |
| 11 * This program is free software; you can redistribute it and/or modify | |
| 12 * it under the terms of the GNU General Public License as published by | |
| 13 * the Free Software Foundation; either version 2 of the License, or | |
| 14 * (at your option) any later version. | |
| 15 * | |
| 16 * This program is distributed in the hope that it will be useful, | |
| 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 19 * GNU General Public License for more details. | |
| 20 * | |
| 21 * You should have received a copy of the GNU General Public License | |
| 22 * along with this program; if not, write to the Free Software | |
| 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 24 */ | |
| 25 #include "internal.h" | |
| 26 #include "gtkgaim.h" | |
| 27 #include "gtkimhtml.h" | |
| 28 #include "gtkstatusselector.h" | |
| 29 #include "gtkutils.h" | |
| 30 | |
| 31 #include "account.h" | |
| 32 #include "debug.h" | |
| 33 #include "prefs.h" | |
| 34 | |
| 35 struct _GaimGtkStatusSelectorPrivate | |
| 36 { | |
| 37 GtkWidget *combo; | |
| 38 GtkWidget *entry; | |
| 10191 | 39 GtkWidget *frame; |
| 10178 | 40 |
| 41 GtkListStore *model; | |
| 10199 | 42 |
| 43 guint entry_timer; | |
| 10178 | 44 }; |
| 45 | |
| 46 enum | |
| 47 { | |
| 48 COLUMN_STATUS_TYPE_ID, | |
| 49 COLUMN_ICON, | |
| 50 COLUMN_NAME, | |
| 51 NUM_COLUMNS | |
| 52 }; | |
| 53 | |
| 54 static void gaim_gtk_status_selector_class_init(GaimGtkStatusSelectorClass *klass); | |
| 55 static void gaim_gtk_status_selector_init(GaimGtkStatusSelector *selector); | |
| 56 static void gaim_gtk_status_selector_finalize(GObject *obj); | |
| 57 static void gaim_gtk_status_selector_destroy(GtkObject *obj); | |
| 10225 | 58 #if GTK_CHECK_VERSION (2,4,0) |
| 10178 | 59 static void status_switched_cb(GtkWidget *combo, GaimGtkStatusSelector *selector); |
| 10199 | 60 static gboolean key_press_cb(GtkWidget *entry, GdkEventKey *event, gpointer user_data); |
| 10178 | 61 static void signed_on_off_cb(GaimConnection *gc, GaimGtkStatusSelector *selector); |
| 10225 | 62 #endif |
| 10178 | 63 static void rebuild_list(GaimGtkStatusSelector *selector); |
| 64 | |
| 65 static GtkVBox *parent_class = NULL; | |
| 66 | |
| 67 GType | |
| 68 gaim_gtk_status_selector_get_type(void) | |
| 69 { | |
| 70 static GType type = 0; | |
| 71 | |
| 72 if (!type) | |
| 73 { | |
| 74 static const GTypeInfo info = | |
| 75 { | |
| 76 sizeof(GaimGtkStatusSelectorClass), | |
| 77 NULL, | |
| 78 NULL, | |
| 79 (GClassInitFunc)gaim_gtk_status_selector_class_init, | |
| 80 NULL, | |
| 81 NULL, | |
| 82 sizeof(GaimGtkStatusSelector), | |
| 83 0, | |
| 84 (GInstanceInitFunc)gaim_gtk_status_selector_init | |
| 85 }; | |
| 86 | |
| 87 type = g_type_register_static(GTK_TYPE_VBOX, | |
| 88 "GaimGtkStatusSelector", &info, 0); | |
| 89 } | |
| 90 | |
| 91 return type; | |
| 92 } | |
| 93 | |
| 94 static void | |
| 95 gaim_gtk_status_selector_class_init(GaimGtkStatusSelectorClass *klass) | |
| 96 { | |
| 97 GObjectClass *gobject_class; | |
| 98 GtkObjectClass *object_class; | |
| 99 | |
| 100 parent_class = g_type_class_peek_parent(klass); | |
| 101 | |
| 102 gobject_class = G_OBJECT_CLASS(klass); | |
| 103 object_class = GTK_OBJECT_CLASS(klass); | |
| 104 | |
| 105 gobject_class->finalize = gaim_gtk_status_selector_finalize; | |
| 106 | |
| 107 object_class->destroy = gaim_gtk_status_selector_destroy; | |
| 108 } | |
| 109 | |
| 110 static void | |
| 111 gaim_gtk_status_selector_init(GaimGtkStatusSelector *selector) | |
| 112 { | |
| 10225 | 113 #if GTK_CHECK_VERSION(2,4,0) |
| 10178 | 114 GtkWidget *combo; |
| 115 GtkWidget *entry; | |
| 10191 | 116 GtkWidget *toolbar; |
| 117 GtkWidget *frame; | |
| 10178 | 118 GtkCellRenderer *renderer; |
| 119 #endif | |
| 120 | |
| 121 selector->priv = g_new0(GaimGtkStatusSelectorPrivate, 1); | |
| 122 | |
| 123 #if GTK_CHECK_VERSION(2,4,0) | |
| 124 selector->priv->model = gtk_list_store_new(NUM_COLUMNS, G_TYPE_POINTER, | |
| 125 GDK_TYPE_PIXBUF, G_TYPE_STRING); | |
| 126 | |
| 127 combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(selector->priv->model)); | |
| 128 selector->priv->combo = combo; | |
| 129 | |
| 130 g_object_unref(G_OBJECT(selector->priv->model)); | |
| 131 | |
| 132 renderer = gtk_cell_renderer_pixbuf_new(); | |
| 133 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, FALSE); | |
| 134 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, | |
| 135 "pixbuf", COLUMN_ICON, | |
| 136 NULL); | |
| 137 | |
| 138 renderer = gtk_cell_renderer_text_new(); | |
| 139 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE); | |
| 140 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, | |
| 141 "text", COLUMN_NAME, | |
| 142 NULL); | |
| 143 | |
| 144 g_signal_connect(G_OBJECT(combo), "changed", | |
| 145 G_CALLBACK(status_switched_cb), selector); | |
| 146 | |
| 147 /* TODO */ | |
| 148 | |
| 149 | |
| 150 gtk_widget_show(combo); | |
| 151 gtk_box_pack_start(GTK_BOX(selector), combo, FALSE, FALSE, 0); | |
| 152 | |
| 10191 | 153 frame = gaim_gtk_create_imhtml(TRUE, &entry, &toolbar); |
| 154 selector->priv->entry = entry; | |
| 155 selector->priv->frame = frame; | |
| 156 gtk_widget_set_name(entry, "gaim_gtkstatusselector_imhtml"); | |
| 157 gtk_box_pack_start(GTK_BOX(selector), frame, TRUE, TRUE, 0); | |
| 158 gtk_widget_hide(toolbar); | |
| 10178 | 159 |
| 10199 | 160 g_signal_connect(G_OBJECT(entry), "key_press_event", |
| 161 G_CALLBACK(key_press_cb), selector); | |
| 162 | |
| 10178 | 163 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", |
| 164 selector, GAIM_CALLBACK(signed_on_off_cb), | |
| 165 selector); | |
| 166 gaim_signal_connect(gaim_connections_get_handle(), "signed-off", | |
| 167 selector, GAIM_CALLBACK(signed_on_off_cb), | |
| 168 selector); | |
| 169 | |
| 170 rebuild_list(selector); | |
| 10225 | 171 #endif /* GTK < 2.4.0 */ |
| 10178 | 172 } |
| 173 | |
| 174 static void | |
| 175 gaim_gtk_status_selector_finalize(GObject *obj) | |
| 176 { | |
| 177 GaimGtkStatusSelector *selector; | |
| 178 | |
| 179 g_return_if_fail(obj != NULL); | |
| 180 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(obj)); | |
| 181 | |
| 182 selector = GAIM_GTK_STATUS_SELECTOR(obj); | |
| 183 | |
| 184 g_free(selector->priv); | |
| 185 | |
| 186 if (G_OBJECT_CLASS(parent_class)->finalize) | |
| 187 G_OBJECT_CLASS(parent_class)->finalize(obj); | |
| 188 } | |
| 189 | |
| 190 static void | |
| 191 gaim_gtk_status_selector_destroy(GtkObject *obj) | |
| 192 { | |
| 193 GaimGtkStatusSelector *selector; | |
| 194 | |
| 195 g_return_if_fail(obj != NULL); | |
| 196 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(obj)); | |
| 197 | |
| 198 selector = GAIM_GTK_STATUS_SELECTOR(obj); | |
| 199 | |
| 10187 | 200 gaim_signals_disconnect_by_handle(selector); |
| 10249 | 201 if (selector->priv->entry_timer != 0) |
| 202 gaim_timeout_remove(selector->priv->entry_timer); | |
| 10187 | 203 |
| 10178 | 204 if (GTK_OBJECT_CLASS(parent_class)->destroy) |
| 205 GTK_OBJECT_CLASS(parent_class)->destroy(obj); | |
| 206 } | |
| 207 | |
| 10225 | 208 #if GTK_CHECK_VERSION(2,4,0) |
| 10178 | 209 static void |
| 210 status_switched_cb(GtkWidget *combo, GaimGtkStatusSelector *selector) | |
| 211 { | |
| 212 GtkTreeIter iter; | |
| 10188 | 213 const char *status_type_id; |
| 10191 | 214 const char *text; |
| 10178 | 215 |
| 10188 | 216 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(selector->priv->combo), |
| 10178 | 217 &iter)) |
| 218 { | |
| 10188 | 219 return; |
| 220 } | |
| 221 | |
| 222 gtk_tree_model_get(GTK_TREE_MODEL(selector->priv->model), &iter, | |
| 223 COLUMN_NAME, &text, | |
| 224 COLUMN_STATUS_TYPE_ID, &status_type_id, | |
| 225 -1); | |
| 10178 | 226 |
| 10191 | 227 if (status_type_id == NULL) |
| 228 { | |
| 229 if (!strcmp(text, _("New Status"))) | |
| 230 { | |
| 231 /* TODO */ | |
| 232 } | |
| 233 } | |
| 10197 | 234 else |
| 10188 | 235 { |
| 10199 | 236 /* |
| 237 * If the chosen status does not require a message, then set the | |
| 238 * status immediately. Otherwise just register a timeout and the | |
| 239 * status will be set whenever the user stops typing the message. | |
| 240 */ | |
| 241 GList *l; | |
| 10188 | 242 GtkTextBuffer *buffer; |
| 243 gboolean allow_message = FALSE; | |
| 10178 | 244 |
| 10188 | 245 buffer = |
| 246 gtk_text_view_get_buffer(GTK_TEXT_VIEW(selector->priv->entry)); | |
| 247 | |
| 10199 | 248 gtk_text_buffer_set_text(buffer, text, -1); |
| 249 | |
| 250 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
| 251 { | |
| 252 GaimConnection *gc = (GaimConnection *)l->data; | |
| 253 GaimAccount *account = gaim_connection_get_account(gc); | |
| 254 GaimStatusType *status_type; | |
| 255 | |
| 256 status_type = gaim_account_get_status_type(account, | |
| 257 status_type_id); | |
| 258 | |
| 259 if (status_type == NULL) | |
| 260 continue; | |
| 261 | |
| 262 if (gaim_status_type_get_attr(status_type, "message") != NULL) | |
| 263 { | |
| 264 allow_message = TRUE; | |
| 265 } | |
| 266 else | |
| 267 { | |
| 268 gaim_account_set_status(account, | |
| 269 status_type_id, TRUE, | |
| 270 NULL); | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 if (allow_message) | |
| 275 { | |
| 276 gtk_widget_show(selector->priv->frame); | |
| 277 key_press_cb(NULL, NULL, selector); | |
| 278 } | |
| 279 else | |
| 280 gtk_widget_hide(selector->priv->frame); | |
| 281 } | |
| 282 } | |
| 10225 | 283 #endif |
| 10199 | 284 |
| 285 static gboolean | |
| 286 insert_text_timeout_cb(gpointer data) | |
| 287 { | |
| 10225 | 288 #if GTK_CHECK_VERSION(2,4,0) |
| 10199 | 289 GaimGtkStatusSelector *selector = (GaimGtkStatusSelector *)data; |
| 290 GtkTreeIter iter; | |
| 291 const char *status_type_id; | |
| 292 const char *text; | |
| 293 | |
| 294 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(selector->priv->combo), | |
| 295 &iter)) | |
| 296 { | |
| 297 return FALSE; | |
| 298 } | |
| 299 | |
| 300 gtk_tree_model_get(GTK_TREE_MODEL(selector->priv->model), &iter, | |
| 301 COLUMN_NAME, &text, | |
| 302 COLUMN_STATUS_TYPE_ID, &status_type_id, | |
| 303 -1); | |
| 304 | |
| 305 if (status_type_id == NULL) | |
| 306 { | |
| 307 if (!strcmp(text, _("New Status"))) | |
| 308 { | |
| 309 /* TODO */ | |
| 310 } | |
| 311 } | |
| 312 else | |
| 313 { | |
| 314 gchar *message; | |
| 315 GList *l; | |
| 316 | |
| 317 message = gtk_imhtml_get_markup(GTK_IMHTML(selector->priv->entry)); | |
| 10188 | 318 |
| 319 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
| 10178 | 320 { |
| 10188 | 321 GaimConnection *gc = (GaimConnection *)l->data; |
| 322 GaimAccount *account = gaim_connection_get_account(gc); | |
| 323 GaimStatusType *status_type; | |
| 10178 | 324 |
| 10188 | 325 status_type = gaim_account_get_status_type(account, |
| 326 status_type_id); | |
| 10178 | 327 |
| 10188 | 328 if (status_type == NULL) |
| 329 continue; | |
| 10178 | 330 |
| 10188 | 331 if (gaim_status_type_get_attr(status_type, "message") != NULL) |
| 10178 | 332 { |
| 10188 | 333 gaim_account_set_status(account, |
| 10197 | 334 status_type_id, TRUE, |
| 10188 | 335 "message", message, |
| 336 NULL); | |
| 337 } | |
| 338 } | |
| 10199 | 339 } |
| 10225 | 340 #endif |
| 10178 | 341 |
| 10199 | 342 return FALSE; |
| 343 } | |
| 344 | |
| 345 /** | |
| 346 * The user typed in the IMHTML entry widget. If the user is finished | |
| 347 * typing then we want to set the appropriate status message. So let's | |
| 348 * wait 3 seconds, and if they haven't typed anything else then set the | |
| 349 * status message. | |
| 350 */ | |
| 351 static gboolean | |
| 352 key_press_cb(GtkWidget *entry, GdkEventKey *event, gpointer user_data) | |
| 353 { | |
| 354 GaimGtkStatusSelector *selector = (GaimGtkStatusSelector *)user_data; | |
| 355 | |
| 356 if (selector->priv->entry_timer != 0) { | |
| 357 gaim_timeout_remove(selector->priv->entry_timer); | |
| 10178 | 358 } |
| 10199 | 359 |
| 360 selector->priv->entry_timer = gaim_timeout_add(3000, insert_text_timeout_cb, | |
| 361 selector); | |
| 362 | |
| 363 return FALSE; | |
| 10178 | 364 } |
| 365 | |
| 366 static void | |
| 367 signed_on_off_cb(GaimConnection *gc, GaimGtkStatusSelector *selector) | |
| 368 { | |
| 369 rebuild_list(selector); | |
| 370 } | |
| 371 | |
| 372 static GdkPixbuf * | |
| 373 load_icon(const char *basename) | |
| 374 { | |
| 375 char *filename; | |
| 376 GdkPixbuf *pixbuf, *scale = NULL; | |
| 377 | |
| 378 if (!strcmp(basename, "available.png")) | |
| 379 basename = "online.png"; | |
| 380 else if (!strcmp(basename, "hidden.png")) | |
| 381 basename = "invisible.png"; | |
| 382 | |
| 383 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "icons", | |
| 384 basename, NULL); | |
| 385 pixbuf = gdk_pixbuf_new_from_file(filename, NULL); | |
| 386 g_free(filename); | |
| 387 | |
| 388 if (pixbuf != NULL) | |
| 389 { | |
| 390 scale = gdk_pixbuf_scale_simple(pixbuf, 16, 16, | |
| 391 GDK_INTERP_BILINEAR); | |
| 392 | |
| 393 g_object_unref(G_OBJECT(pixbuf)); | |
| 394 } | |
| 395 else | |
| 396 { | |
| 397 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "status", | |
| 398 "default", basename, NULL); | |
| 399 scale = gdk_pixbuf_new_from_file(filename, NULL); | |
| 400 g_free(filename); | |
| 401 } | |
| 402 | |
| 403 return scale; | |
| 404 } | |
| 405 | |
| 406 static void | |
| 407 add_item(GaimGtkStatusSelector *selector, const char *status_type_id, | |
| 408 const char *text, GdkPixbuf *pixbuf) | |
| 409 { | |
| 410 GtkTreeIter iter; | |
| 411 | |
| 412 gtk_list_store_append(selector->priv->model, &iter); | |
| 413 gtk_list_store_set(selector->priv->model, &iter, | |
| 414 COLUMN_STATUS_TYPE_ID, status_type_id, | |
| 415 COLUMN_ICON, pixbuf, | |
| 416 COLUMN_NAME, text, | |
| 417 -1); | |
| 418 | |
| 419 if (pixbuf != NULL) | |
| 420 g_object_unref(G_OBJECT(pixbuf)); | |
| 421 } | |
| 422 | |
| 423 static void | |
| 424 rebuild_list(GaimGtkStatusSelector *selector) | |
| 425 { | |
| 426 gboolean single_prpl = TRUE; | |
| 427 GaimAccount *first_account = NULL; | |
| 428 const char *first_prpl_type = NULL; | |
| 429 GList *l; | |
| 430 | |
| 431 g_return_if_fail(selector != NULL); | |
| 432 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(selector)); | |
| 433 | |
| 434 gtk_list_store_clear(selector->priv->model); | |
| 435 | |
| 436 /* | |
| 437 * If the user only has one IM account or one type of IM account | |
| 438 * connected, they'll see all their statuses. This is ideal for those | |
| 439 * who use only one account, or one single protocol. Everyone else | |
| 440 * gets Available and Away and a list of saved statuses. | |
| 441 */ | |
| 442 for (l = gaim_connections_get_all(); l != NULL && single_prpl; l = l->next) | |
| 443 { | |
| 444 GaimConnection *gc = (GaimConnection *)l->data; | |
| 445 GaimAccount *account = gaim_connection_get_account(gc); | |
| 446 GaimPluginProtocolInfo *prpl_info; | |
| 447 const char *basename; | |
| 448 | |
| 449 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
| 450 basename = prpl_info->list_icon(account, NULL); | |
| 451 | |
| 452 if (first_prpl_type == NULL) | |
| 453 { | |
| 454 first_prpl_type = basename; | |
| 455 first_account = account; | |
| 456 } | |
|
10179
97ee3bf7bcf7
[gaim-migrate @ 11294]
Christian Hammond <chipx86@chipx86.com>
parents:
10178
diff
changeset
|
457 else if (strcmp(first_prpl_type, basename)) |
| 10178 | 458 single_prpl = FALSE; |
| 459 } | |
| 460 | |
| 461 if (single_prpl) | |
| 462 { | |
| 463 const GList *l; | |
| 464 | |
| 465 for (l = gaim_account_get_status_types(first_account); | |
| 466 l != NULL; | |
| 467 l = l->next) | |
| 468 { | |
| 469 GaimStatusType *status_type = (GaimStatusType *)l->data; | |
| 470 char filename[BUFSIZ]; | |
| 471 | |
| 472 if (!gaim_status_type_is_user_settable(status_type)) | |
| 473 continue; | |
| 474 | |
| 10207 | 475 /* |
| 476 * TODO Find a way to fallback to the GaimStatusPrimitive | |
| 477 * if an icon for this id does not exist. | |
| 478 */ | |
| 10178 | 479 g_snprintf(filename, sizeof(filename), "%s.png", |
| 480 gaim_status_type_get_id(status_type)); | |
| 481 | |
| 482 add_item(selector, | |
| 483 gaim_status_type_get_id(status_type), | |
| 484 gaim_status_type_get_name(status_type), | |
| 485 load_icon(filename)); | |
| 486 } | |
| 487 } | |
| 488 else | |
| 489 { | |
| 490 add_item(selector, "available", _("Available"), | |
| 491 load_icon("online.png")); | |
| 492 add_item(selector, "away", _("Away"), load_icon("away.png")); | |
| 493 } | |
| 494 | |
| 10191 | 495 /* TODO: Add saved statuses here? */ |
| 496 | |
| 10178 | 497 add_item(selector, NULL, _("New Status"), |
| 498 gtk_widget_render_icon(GTK_WIDGET(selector), GTK_STOCK_NEW, | |
| 499 GTK_ICON_SIZE_MENU, NULL)); | |
| 500 } | |
| 501 | |
| 502 GtkWidget * | |
| 503 gaim_gtk_status_selector_new(void) | |
| 504 { | |
| 505 GaimGtkStatusSelector *selector; | |
| 506 | |
| 507 selector = g_object_new(GAIM_GTK_TYPE_STATUS_SELECTOR, NULL); | |
| 508 | |
| 509 return GTK_WIDGET(selector); | |
| 510 } |
