Mercurial > pidgin
annotate src/gtkstatusselector.c @ 10186:61951f69ad2b
[gaim-migrate @ 11301]
This felt weird to me... I think these should be one of the following forms:
Mark Doliner (KingAnt)
Mark "KingAnt" Doliner
Mark 'KingAnt' Doliner
I lick the first one the most, I think
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Mon, 15 Nov 2004 02:57:34 +0000 |
parents | 9cff8cc12746 |
children | 5a1fe52036ad |
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; | |
39 GtkWidget *sw; | |
40 | |
41 #if GTK_CHECK_VERSION(2,4,0) | |
42 GtkListStore *model; | |
43 #endif | |
44 }; | |
45 | |
46 #if GTK_CHECK_VERSION(2,4,0) | |
47 enum | |
48 { | |
49 COLUMN_STATUS_TYPE_ID, | |
50 COLUMN_ICON, | |
51 COLUMN_NAME, | |
52 NUM_COLUMNS | |
53 }; | |
54 #endif /* GTK >= 2.4.0 */ | |
55 | |
56 static void gaim_gtk_status_selector_class_init(GaimGtkStatusSelectorClass *klass); | |
57 static void gaim_gtk_status_selector_init(GaimGtkStatusSelector *selector); | |
58 static void gaim_gtk_status_selector_finalize(GObject *obj); | |
59 static void gaim_gtk_status_selector_destroy(GtkObject *obj); | |
60 static void status_switched_cb(GtkWidget *combo, GaimGtkStatusSelector *selector); | |
61 static void signed_on_off_cb(GaimConnection *gc, GaimGtkStatusSelector *selector); | |
62 static void rebuild_list(GaimGtkStatusSelector *selector); | |
63 | |
64 static GtkVBox *parent_class = NULL; | |
65 | |
66 GType | |
67 gaim_gtk_status_selector_get_type(void) | |
68 { | |
69 static GType type = 0; | |
70 | |
71 if (!type) | |
72 { | |
73 static const GTypeInfo info = | |
74 { | |
75 sizeof(GaimGtkStatusSelectorClass), | |
76 NULL, | |
77 NULL, | |
78 (GClassInitFunc)gaim_gtk_status_selector_class_init, | |
79 NULL, | |
80 NULL, | |
81 sizeof(GaimGtkStatusSelector), | |
82 0, | |
83 (GInstanceInitFunc)gaim_gtk_status_selector_init | |
84 }; | |
85 | |
86 type = g_type_register_static(GTK_TYPE_VBOX, | |
87 "GaimGtkStatusSelector", &info, 0); | |
88 } | |
89 | |
90 return type; | |
91 } | |
92 | |
93 static void | |
94 gaim_gtk_status_selector_class_init(GaimGtkStatusSelectorClass *klass) | |
95 { | |
96 GObjectClass *gobject_class; | |
97 GtkObjectClass *object_class; | |
98 | |
99 parent_class = g_type_class_peek_parent(klass); | |
100 | |
101 gobject_class = G_OBJECT_CLASS(klass); | |
102 object_class = GTK_OBJECT_CLASS(klass); | |
103 | |
104 gobject_class->finalize = gaim_gtk_status_selector_finalize; | |
105 | |
106 object_class->destroy = gaim_gtk_status_selector_destroy; | |
107 } | |
108 | |
109 static void | |
110 gaim_gtk_status_selector_init(GaimGtkStatusSelector *selector) | |
111 { | |
112 GtkWidget *combo; | |
113 GtkWidget *entry; | |
114 GtkWidget *sw; | |
115 #if GTK_CHECK_VERSION(2,4,0) | |
116 GtkCellRenderer *renderer; | |
117 #endif | |
118 | |
119 selector->priv = g_new0(GaimGtkStatusSelectorPrivate, 1); | |
120 | |
121 #if GTK_CHECK_VERSION(2,4,0) | |
122 selector->priv->model = gtk_list_store_new(NUM_COLUMNS, G_TYPE_POINTER, | |
123 GDK_TYPE_PIXBUF, G_TYPE_STRING); | |
124 | |
125 combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(selector->priv->model)); | |
126 selector->priv->combo = combo; | |
127 | |
128 g_object_unref(G_OBJECT(selector->priv->model)); | |
129 | |
130 renderer = gtk_cell_renderer_pixbuf_new(); | |
131 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, FALSE); | |
132 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, | |
133 "pixbuf", COLUMN_ICON, | |
134 NULL); | |
135 | |
136 renderer = gtk_cell_renderer_text_new(); | |
137 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE); | |
138 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, | |
139 "text", COLUMN_NAME, | |
140 NULL); | |
141 | |
142 g_signal_connect(G_OBJECT(combo), "changed", | |
143 G_CALLBACK(status_switched_cb), selector); | |
144 #else /* GTK < 2.4.0 */ | |
145 | |
146 /* TODO */ | |
147 | |
148 #endif /* GTK < 2.4.0 */ | |
149 | |
150 gtk_widget_show(combo); | |
151 gtk_box_pack_start(GTK_BOX(selector), combo, FALSE, FALSE, 0); | |
152 | |
153 selector->priv->sw = sw = gtk_scrolled_window_new(NULL, NULL); | |
154 gtk_box_pack_start(GTK_BOX(selector), sw, TRUE, TRUE, 0); | |
155 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), | |
156 GTK_SHADOW_IN); | |
157 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), | |
158 GTK_POLICY_NEVER, | |
159 GTK_POLICY_AUTOMATIC); | |
160 | |
161 selector->priv->entry = entry = gtk_imhtml_new(NULL, NULL); | |
162 gtk_widget_show(entry); | |
163 gtk_container_add(GTK_CONTAINER(sw), entry); | |
164 gtk_widget_set_name(entry, "gaim_gtk_status_selector_imhtml"); | |
165 gtk_imhtml_set_editable(GTK_IMHTML(entry), TRUE); | |
166 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(entry), GTK_WRAP_WORD_CHAR); | |
167 | |
168 if (gaim_prefs_get_bool("/gaim/gtk/conversations/spellcheck")) | |
169 gaim_gtk_setup_gtkspell(GTK_TEXT_VIEW(entry)); | |
170 | |
171 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", | |
172 selector, GAIM_CALLBACK(signed_on_off_cb), | |
173 selector); | |
174 gaim_signal_connect(gaim_connections_get_handle(), "signed-off", | |
175 selector, GAIM_CALLBACK(signed_on_off_cb), | |
176 selector); | |
177 | |
178 rebuild_list(selector); | |
179 } | |
180 | |
181 static void | |
182 gaim_gtk_status_selector_finalize(GObject *obj) | |
183 { | |
184 GaimGtkStatusSelector *selector; | |
185 | |
186 g_return_if_fail(obj != NULL); | |
187 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(obj)); | |
188 | |
189 selector = GAIM_GTK_STATUS_SELECTOR(obj); | |
190 | |
191 g_free(selector->priv); | |
192 | |
193 if (G_OBJECT_CLASS(parent_class)->finalize) | |
194 G_OBJECT_CLASS(parent_class)->finalize(obj); | |
195 } | |
196 | |
197 static void | |
198 gaim_gtk_status_selector_destroy(GtkObject *obj) | |
199 { | |
200 GaimGtkStatusSelector *selector; | |
201 | |
202 g_return_if_fail(obj != NULL); | |
203 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(obj)); | |
204 | |
205 selector = GAIM_GTK_STATUS_SELECTOR(obj); | |
206 | |
207 if (GTK_OBJECT_CLASS(parent_class)->destroy) | |
208 GTK_OBJECT_CLASS(parent_class)->destroy(obj); | |
209 } | |
210 | |
211 static void | |
212 status_switched_cb(GtkWidget *combo, GaimGtkStatusSelector *selector) | |
213 { | |
214 GtkTreeIter iter; | |
215 | |
216 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(selector->priv->combo), | |
217 &iter)) | |
218 { | |
219 const char *status_type_id; | |
220 char *text; | |
221 GList *l; | |
222 | |
223 gtk_tree_model_get(GTK_TREE_MODEL(selector->priv->model), &iter, | |
224 COLUMN_NAME, &text, | |
225 COLUMN_STATUS_TYPE_ID, &status_type_id, | |
226 -1); | |
227 | |
228 if (!strcmp(text, _("New status"))) | |
229 { | |
230 /* TODO */ | |
231 } | |
232 else | |
233 { | |
234 const char *message = ""; | |
235 GtkTextBuffer *buffer; | |
236 gboolean allow_message = FALSE; | |
237 | |
238 buffer = | |
239 gtk_text_view_get_buffer(GTK_TEXT_VIEW(selector->priv->entry)); | |
240 | |
241 gtk_text_buffer_set_text(buffer, message, -1); | |
242 | |
243 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
244 { | |
245 GaimConnection *gc = (GaimConnection *)l->data; | |
246 GaimAccount *account = gaim_connection_get_account(gc); | |
247 GaimStatusType *status_type; | |
248 | |
249 status_type = gaim_account_get_status_type(account, | |
250 status_type_id); | |
251 | |
252 if (status_type == NULL) | |
253 continue; | |
254 | |
255 if (gaim_status_type_get_attr(status_type, "message") != NULL) | |
256 { | |
257 gaim_account_set_status(account, | |
258 "away", TRUE, | |
259 "message", message, | |
260 NULL); | |
261 | |
262 allow_message = TRUE; | |
263 } | |
264 else | |
265 { | |
266 gaim_account_set_status(gaim_connection_get_account(gc), | |
267 "away", TRUE, | |
268 NULL); | |
269 } | |
270 } | |
271 | |
272 if (allow_message) | |
273 gtk_widget_show(selector->priv->sw); | |
274 else | |
275 gtk_widget_hide(selector->priv->sw); | |
276 } | |
277 } | |
278 } | |
279 | |
280 static void | |
281 signed_on_off_cb(GaimConnection *gc, GaimGtkStatusSelector *selector) | |
282 { | |
283 rebuild_list(selector); | |
284 } | |
285 | |
286 static GdkPixbuf * | |
287 load_icon(const char *basename) | |
288 { | |
289 char *filename; | |
290 GdkPixbuf *pixbuf, *scale = NULL; | |
291 | |
292 if (!strcmp(basename, "available.png")) | |
293 basename = "online.png"; | |
294 else if (!strcmp(basename, "hidden.png")) | |
295 basename = "invisible.png"; | |
296 | |
297 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "icons", | |
298 basename, NULL); | |
299 pixbuf = gdk_pixbuf_new_from_file(filename, NULL); | |
300 g_free(filename); | |
301 | |
302 if (pixbuf != NULL) | |
303 { | |
304 scale = gdk_pixbuf_scale_simple(pixbuf, 16, 16, | |
305 GDK_INTERP_BILINEAR); | |
306 | |
307 g_object_unref(G_OBJECT(pixbuf)); | |
308 } | |
309 else | |
310 { | |
311 filename = g_build_filename(DATADIR, "pixmaps", "gaim", "status", | |
312 "default", basename, NULL); | |
313 scale = gdk_pixbuf_new_from_file(filename, NULL); | |
314 g_free(filename); | |
315 } | |
316 | |
317 return scale; | |
318 } | |
319 | |
320 static void | |
321 add_item(GaimGtkStatusSelector *selector, const char *status_type_id, | |
322 const char *text, GdkPixbuf *pixbuf) | |
323 { | |
324 GtkTreeIter iter; | |
325 | |
326 gtk_list_store_append(selector->priv->model, &iter); | |
327 gtk_list_store_set(selector->priv->model, &iter, | |
328 COLUMN_STATUS_TYPE_ID, status_type_id, | |
329 COLUMN_ICON, pixbuf, | |
330 COLUMN_NAME, text, | |
331 -1); | |
332 | |
333 if (pixbuf != NULL) | |
334 g_object_unref(G_OBJECT(pixbuf)); | |
335 } | |
336 | |
337 static void | |
338 rebuild_list(GaimGtkStatusSelector *selector) | |
339 { | |
340 gboolean single_prpl = TRUE; | |
341 GaimAccount *first_account = NULL; | |
342 const char *first_prpl_type = NULL; | |
343 GList *l; | |
344 | |
345 g_return_if_fail(selector != NULL); | |
346 g_return_if_fail(GAIM_GTK_IS_STATUS_SELECTOR(selector)); | |
347 | |
348 gtk_list_store_clear(selector->priv->model); | |
349 | |
350 /* | |
351 * If the user only has one IM account or one type of IM account | |
352 * connected, they'll see all their statuses. This is ideal for those | |
353 * who use only one account, or one single protocol. Everyone else | |
354 * gets Available and Away and a list of saved statuses. | |
355 */ | |
356 for (l = gaim_connections_get_all(); l != NULL && single_prpl; l = l->next) | |
357 { | |
358 GaimConnection *gc = (GaimConnection *)l->data; | |
359 GaimAccount *account = gaim_connection_get_account(gc); | |
360 GaimPluginProtocolInfo *prpl_info; | |
361 const char *basename; | |
362 | |
363 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
364 basename = prpl_info->list_icon(account, NULL); | |
365 | |
366 if (first_prpl_type == NULL) | |
367 { | |
368 first_prpl_type = basename; | |
369 first_account = account; | |
370 } | |
10179
97ee3bf7bcf7
[gaim-migrate @ 11294]
Christian Hammond <chipx86@chipx86.com>
parents:
10178
diff
changeset
|
371 else if (strcmp(first_prpl_type, basename)) |
10178 | 372 single_prpl = FALSE; |
373 } | |
374 | |
375 if (single_prpl) | |
376 { | |
377 const GList *l; | |
378 | |
379 for (l = gaim_account_get_status_types(first_account); | |
380 l != NULL; | |
381 l = l->next) | |
382 { | |
383 GaimStatusType *status_type = (GaimStatusType *)l->data; | |
384 char filename[BUFSIZ]; | |
385 | |
386 if (!gaim_status_type_is_user_settable(status_type)) | |
387 continue; | |
388 | |
389 g_snprintf(filename, sizeof(filename), "%s.png", | |
390 gaim_status_type_get_id(status_type)); | |
391 | |
392 add_item(selector, | |
393 gaim_status_type_get_id(status_type), | |
394 gaim_status_type_get_name(status_type), | |
395 load_icon(filename)); | |
396 } | |
397 } | |
398 else | |
399 { | |
400 add_item(selector, "available", _("Available"), | |
401 load_icon("online.png")); | |
402 add_item(selector, "away", _("Away"), load_icon("away.png")); | |
403 } | |
404 | |
405 add_item(selector, NULL, _("New Status"), | |
406 gtk_widget_render_icon(GTK_WIDGET(selector), GTK_STOCK_NEW, | |
407 GTK_ICON_SIZE_MENU, NULL)); | |
408 } | |
409 | |
410 GtkWidget * | |
411 gaim_gtk_status_selector_new(void) | |
412 { | |
413 GaimGtkStatusSelector *selector; | |
414 | |
415 selector = g_object_new(GAIM_GTK_TYPE_STATUS_SELECTOR, NULL); | |
416 | |
417 return GTK_WIDGET(selector); | |
418 } |