Mercurial > pidgin
annotate src/gtkstatusselector.c @ 10206:cf991e2b63cb
[gaim-migrate @ 11327]
Don't crash when signing on with Gadu-Gadu, and don't have 2 states
called "Invisible" (one should be "Invisible for Friends")--I think
that was inadvertant.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Fri, 19 Nov 2004 03:46:15 +0000 |
parents | 76e296e16def |
children | 9d03dd6ccc9a |
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 #if GTK_CHECK_VERSION(2,4,0) | |
42 GtkListStore *model; | |
43 #endif | |
10199 | 44 |
45 guint entry_timer; | |
10178 | 46 }; |
47 | |
48 #if GTK_CHECK_VERSION(2,4,0) | |
49 enum | |
50 { | |
51 COLUMN_STATUS_TYPE_ID, | |
52 COLUMN_ICON, | |
53 COLUMN_NAME, | |
54 NUM_COLUMNS | |
55 }; | |
56 #endif /* GTK >= 2.4.0 */ | |
57 | |
58 static void gaim_gtk_status_selector_class_init(GaimGtkStatusSelectorClass *klass); | |
59 static void gaim_gtk_status_selector_init(GaimGtkStatusSelector *selector); | |
60 static void gaim_gtk_status_selector_finalize(GObject *obj); | |
61 static void gaim_gtk_status_selector_destroy(GtkObject *obj); | |
62 static void status_switched_cb(GtkWidget *combo, GaimGtkStatusSelector *selector); | |
10199 | 63 static gboolean key_press_cb(GtkWidget *entry, GdkEventKey *event, gpointer user_data); |
10178 | 64 static void signed_on_off_cb(GaimConnection *gc, GaimGtkStatusSelector *selector); |
65 static void rebuild_list(GaimGtkStatusSelector *selector); | |
66 | |
67 static GtkVBox *parent_class = NULL; | |
68 | |
69 GType | |
70 gaim_gtk_status_selector_get_type(void) | |
71 { | |
72 static GType type = 0; | |
73 | |
74 if (!type) | |
75 { | |
76 static const GTypeInfo info = | |
77 { | |
78 sizeof(GaimGtkStatusSelectorClass), | |
79 NULL, | |
80 NULL, | |
81 (GClassInitFunc)gaim_gtk_status_selector_class_init, | |
82 NULL, | |
83 NULL, | |
84 sizeof(GaimGtkStatusSelector), | |
85 0, | |
86 (GInstanceInitFunc)gaim_gtk_status_selector_init | |
87 }; | |
88 | |
89 type = g_type_register_static(GTK_TYPE_VBOX, | |
90 "GaimGtkStatusSelector", &info, 0); | |
91 } | |
92 | |
93 return type; | |
94 } | |
95 | |
96 static void | |
97 gaim_gtk_status_selector_class_init(GaimGtkStatusSelectorClass *klass) | |
98 { | |
99 GObjectClass *gobject_class; | |
100 GtkObjectClass *object_class; | |
101 | |
102 parent_class = g_type_class_peek_parent(klass); | |
103 | |
104 gobject_class = G_OBJECT_CLASS(klass); | |
105 object_class = GTK_OBJECT_CLASS(klass); | |
106 | |
107 gobject_class->finalize = gaim_gtk_status_selector_finalize; | |
108 | |
109 object_class->destroy = gaim_gtk_status_selector_destroy; | |
110 } | |
111 | |
112 static void | |
113 gaim_gtk_status_selector_init(GaimGtkStatusSelector *selector) | |
114 { | |
115 GtkWidget *combo; | |
116 GtkWidget *entry; | |
10191 | 117 GtkWidget *toolbar; |
118 GtkWidget *frame; | |
10178 | 119 #if GTK_CHECK_VERSION(2,4,0) |
120 GtkCellRenderer *renderer; | |
121 #endif | |
122 | |
123 selector->priv = g_new0(GaimGtkStatusSelectorPrivate, 1); | |
124 | |
125 #if GTK_CHECK_VERSION(2,4,0) | |
126 selector->priv->model = gtk_list_store_new(NUM_COLUMNS, G_TYPE_POINTER, | |
127 GDK_TYPE_PIXBUF, G_TYPE_STRING); | |
128 | |
129 combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(selector->priv->model)); | |
130 selector->priv->combo = combo; | |
131 | |
132 g_object_unref(G_OBJECT(selector->priv->model)); | |
133 | |
134 renderer = gtk_cell_renderer_pixbuf_new(); | |
135 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, FALSE); | |
136 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, | |
137 "pixbuf", COLUMN_ICON, | |
138 NULL); | |
139 | |
140 renderer = gtk_cell_renderer_text_new(); | |
141 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE); | |
142 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, | |
143 "text", COLUMN_NAME, | |
144 NULL); | |
145 | |
146 g_signal_connect(G_OBJECT(combo), "changed", | |
147 G_CALLBACK(status_switched_cb), selector); | |
148 #else /* GTK < 2.4.0 */ | |
149 | |
150 /* TODO */ | |
151 | |
152 #endif /* GTK < 2.4.0 */ | |
153 | |
154 gtk_widget_show(combo); | |
155 gtk_box_pack_start(GTK_BOX(selector), combo, FALSE, FALSE, 0); | |
156 | |
10191 | 157 frame = gaim_gtk_create_imhtml(TRUE, &entry, &toolbar); |
158 selector->priv->entry = entry; | |
159 selector->priv->frame = frame; | |
160 gtk_widget_set_name(entry, "gaim_gtkstatusselector_imhtml"); | |
161 gtk_box_pack_start(GTK_BOX(selector), frame, TRUE, TRUE, 0); | |
162 gtk_widget_hide(toolbar); | |
10178 | 163 |
10199 | 164 g_signal_connect(G_OBJECT(entry), "key_press_event", |
165 G_CALLBACK(key_press_cb), selector); | |
166 | |
10178 | 167 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", |
168 selector, GAIM_CALLBACK(signed_on_off_cb), | |
169 selector); | |
170 gaim_signal_connect(gaim_connections_get_handle(), "signed-off", | |
171 selector, GAIM_CALLBACK(signed_on_off_cb), | |
172 selector); | |
173 | |
174 rebuild_list(selector); | |
175 } | |
176 | |
177 static void | |
178 gaim_gtk_status_selector_finalize(GObject *obj) | |
179 { | |
180 GaimGtkStatusSelector *selector; | |
181 | |
182 g_return_if_fail(obj != NULL); | |
183 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(obj)); | |
184 | |
185 selector = GAIM_GTK_STATUS_SELECTOR(obj); | |
186 | |
187 g_free(selector->priv); | |
188 | |
189 if (G_OBJECT_CLASS(parent_class)->finalize) | |
190 G_OBJECT_CLASS(parent_class)->finalize(obj); | |
191 } | |
192 | |
193 static void | |
194 gaim_gtk_status_selector_destroy(GtkObject *obj) | |
195 { | |
196 GaimGtkStatusSelector *selector; | |
197 | |
198 g_return_if_fail(obj != NULL); | |
199 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(obj)); | |
200 | |
201 selector = GAIM_GTK_STATUS_SELECTOR(obj); | |
202 | |
10187 | 203 gaim_signals_disconnect_by_handle(selector); |
204 | |
10178 | 205 if (GTK_OBJECT_CLASS(parent_class)->destroy) |
206 GTK_OBJECT_CLASS(parent_class)->destroy(obj); | |
207 } | |
208 | |
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 } | |
283 | |
284 static gboolean | |
285 insert_text_timeout_cb(gpointer data) | |
286 { | |
287 GaimGtkStatusSelector *selector = (GaimGtkStatusSelector *)data; | |
288 GtkTreeIter iter; | |
289 const char *status_type_id; | |
290 const char *text; | |
291 | |
292 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(selector->priv->combo), | |
293 &iter)) | |
294 { | |
295 return FALSE; | |
296 } | |
297 | |
298 gtk_tree_model_get(GTK_TREE_MODEL(selector->priv->model), &iter, | |
299 COLUMN_NAME, &text, | |
300 COLUMN_STATUS_TYPE_ID, &status_type_id, | |
301 -1); | |
302 | |
303 if (status_type_id == NULL) | |
304 { | |
305 if (!strcmp(text, _("New Status"))) | |
306 { | |
307 /* TODO */ | |
308 } | |
309 } | |
310 else | |
311 { | |
312 gchar *message; | |
313 GList *l; | |
314 | |
315 message = gtk_imhtml_get_markup(GTK_IMHTML(selector->priv->entry)); | |
10188 | 316 |
317 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
10178 | 318 { |
10188 | 319 GaimConnection *gc = (GaimConnection *)l->data; |
320 GaimAccount *account = gaim_connection_get_account(gc); | |
321 GaimStatusType *status_type; | |
10178 | 322 |
10188 | 323 status_type = gaim_account_get_status_type(account, |
324 status_type_id); | |
10178 | 325 |
10188 | 326 if (status_type == NULL) |
327 continue; | |
10178 | 328 |
10188 | 329 if (gaim_status_type_get_attr(status_type, "message") != NULL) |
10178 | 330 { |
10188 | 331 gaim_account_set_status(account, |
10197 | 332 status_type_id, TRUE, |
10188 | 333 "message", message, |
334 NULL); | |
335 } | |
336 } | |
10199 | 337 } |
10178 | 338 |
10199 | 339 return FALSE; |
340 } | |
341 | |
342 /** | |
343 * The user typed in the IMHTML entry widget. If the user is finished | |
344 * typing then we want to set the appropriate status message. So let's | |
345 * wait 3 seconds, and if they haven't typed anything else then set the | |
346 * status message. | |
347 */ | |
348 static gboolean | |
349 key_press_cb(GtkWidget *entry, GdkEventKey *event, gpointer user_data) | |
350 { | |
351 GaimGtkStatusSelector *selector = (GaimGtkStatusSelector *)user_data; | |
352 | |
353 if (selector->priv->entry_timer != 0) { | |
354 gaim_timeout_remove(selector->priv->entry_timer); | |
10178 | 355 } |
10199 | 356 |
357 selector->priv->entry_timer = gaim_timeout_add(3000, insert_text_timeout_cb, | |
358 selector); | |
359 | |
360 return FALSE; | |
10178 | 361 } |
362 | |
363 static void | |
364 signed_on_off_cb(GaimConnection *gc, GaimGtkStatusSelector *selector) | |
365 { | |
366 rebuild_list(selector); | |
367 } | |
368 | |
369 static GdkPixbuf * | |
370 load_icon(const char *basename) | |
371 { | |
372 char *filename; | |
373 GdkPixbuf *pixbuf, *scale = NULL; | |
374 | |
375 if (!strcmp(basename, "available.png")) | |
376 basename = "online.png"; | |
377 else if (!strcmp(basename, "hidden.png")) | |
378 basename = "invisible.png"; | |
379 | |
380 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "icons", | |
381 basename, NULL); | |
382 pixbuf = gdk_pixbuf_new_from_file(filename, NULL); | |
383 g_free(filename); | |
384 | |
385 if (pixbuf != NULL) | |
386 { | |
387 scale = gdk_pixbuf_scale_simple(pixbuf, 16, 16, | |
388 GDK_INTERP_BILINEAR); | |
389 | |
390 g_object_unref(G_OBJECT(pixbuf)); | |
391 } | |
392 else | |
393 { | |
394 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "status", | |
395 "default", basename, NULL); | |
396 scale = gdk_pixbuf_new_from_file(filename, NULL); | |
397 g_free(filename); | |
398 } | |
399 | |
400 return scale; | |
401 } | |
402 | |
403 static void | |
404 add_item(GaimGtkStatusSelector *selector, const char *status_type_id, | |
405 const char *text, GdkPixbuf *pixbuf) | |
406 { | |
407 GtkTreeIter iter; | |
408 | |
409 gtk_list_store_append(selector->priv->model, &iter); | |
410 gtk_list_store_set(selector->priv->model, &iter, | |
411 COLUMN_STATUS_TYPE_ID, status_type_id, | |
412 COLUMN_ICON, pixbuf, | |
413 COLUMN_NAME, text, | |
414 -1); | |
415 | |
416 if (pixbuf != NULL) | |
417 g_object_unref(G_OBJECT(pixbuf)); | |
418 } | |
419 | |
420 static void | |
421 rebuild_list(GaimGtkStatusSelector *selector) | |
422 { | |
423 gboolean single_prpl = TRUE; | |
424 GaimAccount *first_account = NULL; | |
425 const char *first_prpl_type = NULL; | |
426 GList *l; | |
427 | |
428 g_return_if_fail(selector != NULL); | |
429 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(selector)); | |
430 | |
431 gtk_list_store_clear(selector->priv->model); | |
432 | |
433 /* | |
434 * If the user only has one IM account or one type of IM account | |
435 * connected, they'll see all their statuses. This is ideal for those | |
436 * who use only one account, or one single protocol. Everyone else | |
437 * gets Available and Away and a list of saved statuses. | |
438 */ | |
439 for (l = gaim_connections_get_all(); l != NULL && single_prpl; l = l->next) | |
440 { | |
441 GaimConnection *gc = (GaimConnection *)l->data; | |
442 GaimAccount *account = gaim_connection_get_account(gc); | |
443 GaimPluginProtocolInfo *prpl_info; | |
444 const char *basename; | |
445 | |
446 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
447 basename = prpl_info->list_icon(account, NULL); | |
448 | |
449 if (first_prpl_type == NULL) | |
450 { | |
451 first_prpl_type = basename; | |
452 first_account = account; | |
453 } | |
10179
97ee3bf7bcf7
[gaim-migrate @ 11294]
Christian Hammond <chipx86@chipx86.com>
parents:
10178
diff
changeset
|
454 else if (strcmp(first_prpl_type, basename)) |
10178 | 455 single_prpl = FALSE; |
456 } | |
457 | |
458 if (single_prpl) | |
459 { | |
460 const GList *l; | |
461 | |
462 for (l = gaim_account_get_status_types(first_account); | |
463 l != NULL; | |
464 l = l->next) | |
465 { | |
466 GaimStatusType *status_type = (GaimStatusType *)l->data; | |
467 char filename[BUFSIZ]; | |
468 | |
469 if (!gaim_status_type_is_user_settable(status_type)) | |
470 continue; | |
471 | |
472 g_snprintf(filename, sizeof(filename), "%s.png", | |
473 gaim_status_type_get_id(status_type)); | |
474 | |
475 add_item(selector, | |
476 gaim_status_type_get_id(status_type), | |
477 gaim_status_type_get_name(status_type), | |
478 load_icon(filename)); | |
479 } | |
480 } | |
481 else | |
482 { | |
483 add_item(selector, "available", _("Available"), | |
484 load_icon("online.png")); | |
485 add_item(selector, "away", _("Away"), load_icon("away.png")); | |
486 } | |
487 | |
10191 | 488 /* TODO: Add saved statuses here? */ |
489 | |
10178 | 490 add_item(selector, NULL, _("New Status"), |
491 gtk_widget_render_icon(GTK_WIDGET(selector), GTK_STOCK_NEW, | |
492 GTK_ICON_SIZE_MENU, NULL)); | |
493 } | |
494 | |
495 GtkWidget * | |
496 gaim_gtk_status_selector_new(void) | |
497 { | |
498 GaimGtkStatusSelector *selector; | |
499 | |
500 selector = g_object_new(GAIM_GTK_TYPE_STATUS_SELECTOR, NULL); | |
501 | |
502 return GTK_WIDGET(selector); | |
503 } |