Mercurial > pidgin
annotate src/gtkstatusselector.c @ 10248:e7c0a9e0c276
[gaim-migrate @ 11388]
Remove a timeout in the gtkstatusselector when all accounts sign off
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Tue, 23 Nov 2004 06:25:14 +0000 |
parents | ecf3ce2e2ab1 |
children | a7c8a8735a31 |
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); |
10248 | 201 gaim_timeout_remove(selector->priv->entry_timer); |
10187 | 202 |
10178 | 203 if (GTK_OBJECT_CLASS(parent_class)->destroy) |
204 GTK_OBJECT_CLASS(parent_class)->destroy(obj); | |
205 } | |
206 | |
10225 | 207 #if GTK_CHECK_VERSION(2,4,0) |
10178 | 208 static void |
209 status_switched_cb(GtkWidget *combo, GaimGtkStatusSelector *selector) | |
210 { | |
211 GtkTreeIter iter; | |
10188 | 212 const char *status_type_id; |
10191 | 213 const char *text; |
10178 | 214 |
10188 | 215 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(selector->priv->combo), |
10178 | 216 &iter)) |
217 { | |
10188 | 218 return; |
219 } | |
220 | |
221 gtk_tree_model_get(GTK_TREE_MODEL(selector->priv->model), &iter, | |
222 COLUMN_NAME, &text, | |
223 COLUMN_STATUS_TYPE_ID, &status_type_id, | |
224 -1); | |
10178 | 225 |
10191 | 226 if (status_type_id == NULL) |
227 { | |
228 if (!strcmp(text, _("New Status"))) | |
229 { | |
230 /* TODO */ | |
231 } | |
232 } | |
10197 | 233 else |
10188 | 234 { |
10199 | 235 /* |
236 * If the chosen status does not require a message, then set the | |
237 * status immediately. Otherwise just register a timeout and the | |
238 * status will be set whenever the user stops typing the message. | |
239 */ | |
240 GList *l; | |
10188 | 241 GtkTextBuffer *buffer; |
242 gboolean allow_message = FALSE; | |
10178 | 243 |
10188 | 244 buffer = |
245 gtk_text_view_get_buffer(GTK_TEXT_VIEW(selector->priv->entry)); | |
246 | |
10199 | 247 gtk_text_buffer_set_text(buffer, text, -1); |
248 | |
249 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
250 { | |
251 GaimConnection *gc = (GaimConnection *)l->data; | |
252 GaimAccount *account = gaim_connection_get_account(gc); | |
253 GaimStatusType *status_type; | |
254 | |
255 status_type = gaim_account_get_status_type(account, | |
256 status_type_id); | |
257 | |
258 if (status_type == NULL) | |
259 continue; | |
260 | |
261 if (gaim_status_type_get_attr(status_type, "message") != NULL) | |
262 { | |
263 allow_message = TRUE; | |
264 } | |
265 else | |
266 { | |
267 gaim_account_set_status(account, | |
268 status_type_id, TRUE, | |
269 NULL); | |
270 } | |
271 } | |
272 | |
273 if (allow_message) | |
274 { | |
275 gtk_widget_show(selector->priv->frame); | |
276 key_press_cb(NULL, NULL, selector); | |
277 } | |
278 else | |
279 gtk_widget_hide(selector->priv->frame); | |
280 } | |
281 } | |
10225 | 282 #endif |
10199 | 283 |
284 static gboolean | |
285 insert_text_timeout_cb(gpointer data) | |
286 { | |
10225 | 287 #if GTK_CHECK_VERSION(2,4,0) |
10199 | 288 GaimGtkStatusSelector *selector = (GaimGtkStatusSelector *)data; |
289 GtkTreeIter iter; | |
290 const char *status_type_id; | |
291 const char *text; | |
292 | |
293 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(selector->priv->combo), | |
294 &iter)) | |
295 { | |
296 return FALSE; | |
297 } | |
298 | |
299 gtk_tree_model_get(GTK_TREE_MODEL(selector->priv->model), &iter, | |
300 COLUMN_NAME, &text, | |
301 COLUMN_STATUS_TYPE_ID, &status_type_id, | |
302 -1); | |
303 | |
304 if (status_type_id == NULL) | |
305 { | |
306 if (!strcmp(text, _("New Status"))) | |
307 { | |
308 /* TODO */ | |
309 } | |
310 } | |
311 else | |
312 { | |
313 gchar *message; | |
314 GList *l; | |
315 | |
316 message = gtk_imhtml_get_markup(GTK_IMHTML(selector->priv->entry)); | |
10188 | 317 |
318 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
10178 | 319 { |
10188 | 320 GaimConnection *gc = (GaimConnection *)l->data; |
321 GaimAccount *account = gaim_connection_get_account(gc); | |
322 GaimStatusType *status_type; | |
10178 | 323 |
10188 | 324 status_type = gaim_account_get_status_type(account, |
325 status_type_id); | |
10178 | 326 |
10188 | 327 if (status_type == NULL) |
328 continue; | |
10178 | 329 |
10188 | 330 if (gaim_status_type_get_attr(status_type, "message") != NULL) |
10178 | 331 { |
10188 | 332 gaim_account_set_status(account, |
10197 | 333 status_type_id, TRUE, |
10188 | 334 "message", message, |
335 NULL); | |
336 } | |
337 } | |
10199 | 338 } |
10225 | 339 #endif |
10178 | 340 |
10199 | 341 return FALSE; |
342 } | |
343 | |
344 /** | |
345 * The user typed in the IMHTML entry widget. If the user is finished | |
346 * typing then we want to set the appropriate status message. So let's | |
347 * wait 3 seconds, and if they haven't typed anything else then set the | |
348 * status message. | |
349 */ | |
350 static gboolean | |
351 key_press_cb(GtkWidget *entry, GdkEventKey *event, gpointer user_data) | |
352 { | |
353 GaimGtkStatusSelector *selector = (GaimGtkStatusSelector *)user_data; | |
354 | |
355 if (selector->priv->entry_timer != 0) { | |
356 gaim_timeout_remove(selector->priv->entry_timer); | |
10178 | 357 } |
10199 | 358 |
359 selector->priv->entry_timer = gaim_timeout_add(3000, insert_text_timeout_cb, | |
360 selector); | |
361 | |
362 return FALSE; | |
10178 | 363 } |
364 | |
365 static void | |
366 signed_on_off_cb(GaimConnection *gc, GaimGtkStatusSelector *selector) | |
367 { | |
368 rebuild_list(selector); | |
369 } | |
370 | |
371 static GdkPixbuf * | |
372 load_icon(const char *basename) | |
373 { | |
374 char *filename; | |
375 GdkPixbuf *pixbuf, *scale = NULL; | |
376 | |
377 if (!strcmp(basename, "available.png")) | |
378 basename = "online.png"; | |
379 else if (!strcmp(basename, "hidden.png")) | |
380 basename = "invisible.png"; | |
381 | |
382 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "icons", | |
383 basename, NULL); | |
384 pixbuf = gdk_pixbuf_new_from_file(filename, NULL); | |
385 g_free(filename); | |
386 | |
387 if (pixbuf != NULL) | |
388 { | |
389 scale = gdk_pixbuf_scale_simple(pixbuf, 16, 16, | |
390 GDK_INTERP_BILINEAR); | |
391 | |
392 g_object_unref(G_OBJECT(pixbuf)); | |
393 } | |
394 else | |
395 { | |
396 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "status", | |
397 "default", basename, NULL); | |
398 scale = gdk_pixbuf_new_from_file(filename, NULL); | |
399 g_free(filename); | |
400 } | |
401 | |
402 return scale; | |
403 } | |
404 | |
405 static void | |
406 add_item(GaimGtkStatusSelector *selector, const char *status_type_id, | |
407 const char *text, GdkPixbuf *pixbuf) | |
408 { | |
409 GtkTreeIter iter; | |
410 | |
411 gtk_list_store_append(selector->priv->model, &iter); | |
412 gtk_list_store_set(selector->priv->model, &iter, | |
413 COLUMN_STATUS_TYPE_ID, status_type_id, | |
414 COLUMN_ICON, pixbuf, | |
415 COLUMN_NAME, text, | |
416 -1); | |
417 | |
418 if (pixbuf != NULL) | |
419 g_object_unref(G_OBJECT(pixbuf)); | |
420 } | |
421 | |
422 static void | |
423 rebuild_list(GaimGtkStatusSelector *selector) | |
424 { | |
425 gboolean single_prpl = TRUE; | |
426 GaimAccount *first_account = NULL; | |
427 const char *first_prpl_type = NULL; | |
428 GList *l; | |
429 | |
430 g_return_if_fail(selector != NULL); | |
431 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(selector)); | |
432 | |
433 gtk_list_store_clear(selector->priv->model); | |
434 | |
435 /* | |
436 * If the user only has one IM account or one type of IM account | |
437 * connected, they'll see all their statuses. This is ideal for those | |
438 * who use only one account, or one single protocol. Everyone else | |
439 * gets Available and Away and a list of saved statuses. | |
440 */ | |
441 for (l = gaim_connections_get_all(); l != NULL && single_prpl; l = l->next) | |
442 { | |
443 GaimConnection *gc = (GaimConnection *)l->data; | |
444 GaimAccount *account = gaim_connection_get_account(gc); | |
445 GaimPluginProtocolInfo *prpl_info; | |
446 const char *basename; | |
447 | |
448 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
449 basename = prpl_info->list_icon(account, NULL); | |
450 | |
451 if (first_prpl_type == NULL) | |
452 { | |
453 first_prpl_type = basename; | |
454 first_account = account; | |
455 } | |
10179
97ee3bf7bcf7
[gaim-migrate @ 11294]
Christian Hammond <chipx86@chipx86.com>
parents:
10178
diff
changeset
|
456 else if (strcmp(first_prpl_type, basename)) |
10178 | 457 single_prpl = FALSE; |
458 } | |
459 | |
460 if (single_prpl) | |
461 { | |
462 const GList *l; | |
463 | |
464 for (l = gaim_account_get_status_types(first_account); | |
465 l != NULL; | |
466 l = l->next) | |
467 { | |
468 GaimStatusType *status_type = (GaimStatusType *)l->data; | |
469 char filename[BUFSIZ]; | |
470 | |
471 if (!gaim_status_type_is_user_settable(status_type)) | |
472 continue; | |
473 | |
10207 | 474 /* |
475 * TODO Find a way to fallback to the GaimStatusPrimitive | |
476 * if an icon for this id does not exist. | |
477 */ | |
10178 | 478 g_snprintf(filename, sizeof(filename), "%s.png", |
479 gaim_status_type_get_id(status_type)); | |
480 | |
481 add_item(selector, | |
482 gaim_status_type_get_id(status_type), | |
483 gaim_status_type_get_name(status_type), | |
484 load_icon(filename)); | |
485 } | |
486 } | |
487 else | |
488 { | |
489 add_item(selector, "available", _("Available"), | |
490 load_icon("online.png")); | |
491 add_item(selector, "away", _("Away"), load_icon("away.png")); | |
492 } | |
493 | |
10191 | 494 /* TODO: Add saved statuses here? */ |
495 | |
10178 | 496 add_item(selector, NULL, _("New Status"), |
497 gtk_widget_render_icon(GTK_WIDGET(selector), GTK_STOCK_NEW, | |
498 GTK_ICON_SIZE_MENU, NULL)); | |
499 } | |
500 | |
501 GtkWidget * | |
502 gaim_gtk_status_selector_new(void) | |
503 { | |
504 GaimGtkStatusSelector *selector; | |
505 | |
506 selector = g_object_new(GAIM_GTK_TYPE_STATUS_SELECTOR, NULL); | |
507 | |
508 return GTK_WIDGET(selector); | |
509 } |