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