]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-ui-utils.c
account-settings: allow to change the service
[empathy.git] / libempathy-gtk / empathy-ui-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2002-2007 Imendio AB
4  * Copyright (C) 2007-2010 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Richard Hult <richard@imendio.com>
23  *          Martyn Russell <martyn@imendio.com>
24  *          Xavier Claessens <xclaesse@gmail.com>
25  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
26  *          Travis Reitter <travis.reitter@collabora.co.uk>
27  *
28  *          Part of this file is copied from GtkSourceView (gtksourceiter.c):
29  *          Paolo Maggi
30  *          Jeroen Zwartepoorte
31  */
32
33 #include <config.h>
34
35 #include <string.h>
36 #include <X11/Xatom.h>
37 #include <gdk/gdkx.h>
38 #include <glib/gi18n-lib.h>
39 #include <gtk/gtk.h>
40 #include <gio/gio.h>
41
42 #include <telepathy-glib/util.h>
43 #include <folks/folks.h>
44
45 #include "empathy-ui-utils.h"
46 #include "empathy-images.h"
47 #include "empathy-live-search.h"
48 #include "empathy-smiley-manager.h"
49
50 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
51 #include <libempathy/empathy-debug.h>
52 #include <libempathy/empathy-utils.h>
53 #include <libempathy/empathy-ft-factory.h>
54
55 void
56 empathy_gtk_init (void)
57 {
58         static gboolean initialized = FALSE;
59
60         if (initialized)
61                 return;
62
63         empathy_init ();
64         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
65                                            PKGDATADIR G_DIR_SEPARATOR_S "icons");
66
67         initialized = TRUE;
68 }
69
70 static GtkBuilder *
71 builder_get_file_valist (const gchar *filename,
72                          const gchar *first_object,
73                          va_list      args)
74 {
75         GtkBuilder  *gui;
76         const gchar *name;
77         GObject    **object_ptr;
78         GError      *error = NULL;
79
80         DEBUG ("Loading file %s", filename);
81
82         gui = gtk_builder_new ();
83         gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
84         if (!gtk_builder_add_from_file (gui, filename, &error)) {
85                 g_critical ("GtkBuilder Error (%s): %s",
86                                 filename, error->message);
87                 g_clear_error (&error);
88                 g_object_unref (gui);
89
90                 /* we need to iterate and set all of the pointers to NULL */
91                 for (name = first_object; name;
92                      name = va_arg (args, const gchar *)) {
93                         object_ptr = va_arg (args, GObject**);
94
95                         *object_ptr = NULL;
96                 }
97
98                 return NULL;
99         }
100
101         for (name = first_object; name; name = va_arg (args, const gchar *)) {
102                 object_ptr = va_arg (args, GObject**);
103
104                 *object_ptr = gtk_builder_get_object (gui, name);
105
106                 if (!*object_ptr) {
107                         g_warning ("File is missing object '%s'.", name);
108                         continue;
109                 }
110         }
111
112         return gui;
113 }
114
115 GtkBuilder *
116 empathy_builder_get_file (const gchar *filename,
117                           const gchar *first_object,
118                           ...)
119 {
120         GtkBuilder *gui;
121         va_list     args;
122
123         va_start (args, first_object);
124         gui = builder_get_file_valist (filename, first_object, args);
125         va_end (args);
126
127         return gui;
128 }
129
130 void
131 empathy_builder_connect (GtkBuilder  *gui,
132                          gpointer     user_data,
133                          const gchar *first_object,
134                          ...)
135 {
136         va_list      args;
137         const gchar *name;
138         const gchar *sig;
139         GObject     *object;
140         GCallback    callback;
141
142         va_start (args, first_object);
143         for (name = first_object; name; name = va_arg (args, const gchar *)) {
144                 sig = va_arg (args, const gchar *);
145                 callback = va_arg (args, GCallback);
146
147                 object = gtk_builder_get_object (gui, name);
148                 if (!object) {
149                         g_warning ("File is missing object '%s'.", name);
150                         continue;
151                 }
152
153                 g_signal_connect (object, sig, callback, user_data);
154         }
155
156         va_end (args);
157 }
158
159 GtkWidget *
160 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
161                                        GtkWidget  *widget)
162 {
163         /* On construction gui sinks the initial reference to widget. When gui
164          * is finalized it will drop its ref to widget. We take our own ref to
165          * prevent widget being finalised. The widget is forced to have a
166          * floating reference, like when it was initially unowned so that it can
167          * be used like any other GtkWidget. */
168
169         g_object_ref (widget);
170         g_object_force_floating (G_OBJECT (widget));
171         g_object_unref (gui);
172
173         return widget;
174 }
175
176 const gchar *
177 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
178 {
179         switch (presence) {
180         case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
181                 return EMPATHY_IMAGE_AVAILABLE;
182         case TP_CONNECTION_PRESENCE_TYPE_BUSY:
183                 return EMPATHY_IMAGE_BUSY;
184         case TP_CONNECTION_PRESENCE_TYPE_AWAY:
185                 return EMPATHY_IMAGE_AWAY;
186         case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
187                 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
188                                              EMPATHY_IMAGE_EXT_AWAY))
189                         return EMPATHY_IMAGE_EXT_AWAY;
190
191                 /* The 'extended-away' icon is not an official one so we fallback to idle if
192                  * it's not implemented */
193                 return EMPATHY_IMAGE_IDLE;
194         case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
195                 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
196                                              EMPATHY_IMAGE_HIDDEN))
197                         return EMPATHY_IMAGE_HIDDEN;
198
199                 /* The 'hidden' icon is not an official one so we fallback to offline if
200                  * it's not implemented */
201                 return EMPATHY_IMAGE_OFFLINE;
202         case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
203         case TP_CONNECTION_PRESENCE_TYPE_ERROR:
204                 return EMPATHY_IMAGE_OFFLINE;
205         case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
206                 return EMPATHY_IMAGE_PENDING;
207         case TP_CONNECTION_PRESENCE_TYPE_UNSET:
208         default:
209                 return NULL;
210         }
211
212         return NULL;
213 }
214
215 const gchar *
216 empathy_icon_name_for_contact (EmpathyContact *contact)
217 {
218         TpConnectionPresenceType presence;
219
220         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
221                               EMPATHY_IMAGE_OFFLINE);
222
223         presence = empathy_contact_get_presence (contact);
224         return empathy_icon_name_for_presence (presence);
225 }
226
227 const gchar *
228 empathy_icon_name_for_individual (FolksIndividual *individual)
229 {
230         FolksPresenceType folks_presence;
231         TpConnectionPresenceType presence;
232
233         folks_presence =
234             folks_presence_details_get_presence_type (
235                 FOLKS_PRESENCE_DETAILS (individual));
236         presence = empathy_folks_presence_type_to_tp (folks_presence);
237
238         return empathy_icon_name_for_presence (presence);
239 }
240
241 const gchar *
242 empathy_protocol_name_for_contact (EmpathyContact   *contact)
243 {
244         TpAccount     *account;
245
246         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
247
248         account = empathy_contact_get_account (contact);
249         if (account == NULL) {
250                 return NULL;
251         }
252
253         return tp_account_get_icon_name (account);
254 }
255
256 GdkPixbuf *
257 empathy_pixbuf_from_data (gchar *data,
258                           gsize  data_size)
259 {
260         return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
261 }
262
263 GdkPixbuf *
264 empathy_pixbuf_from_data_and_mime (gchar  *data,
265                                    gsize   data_size,
266                                    gchar **mime_type)
267 {
268         GdkPixbufLoader *loader;
269         GdkPixbufFormat *format;
270         GdkPixbuf       *pixbuf = NULL;
271         gchar          **mime_types;
272         GError          *error = NULL;
273
274         if (!data) {
275                 return NULL;
276         }
277
278         loader = gdk_pixbuf_loader_new ();
279         if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
280                 DEBUG ("Failed to write to pixbuf loader: %s",
281                         error ? error->message : "No error given");
282                 goto out;
283         }
284         if (!gdk_pixbuf_loader_close (loader, &error)) {
285                 DEBUG ("Failed to close pixbuf loader: %s",
286                         error ? error->message : "No error given");
287                 goto out;
288         }
289
290         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
291         if (pixbuf) {
292                 g_object_ref (pixbuf);
293
294                 if (mime_type != NULL) {
295                         format = gdk_pixbuf_loader_get_format (loader);
296                         mime_types = gdk_pixbuf_format_get_mime_types (format);
297
298                         *mime_type = g_strdup (*mime_types);
299                         if (mime_types[1] != NULL) {
300                                 DEBUG ("Loader supports more than one mime "
301                                         "type! Picking the first one, %s",
302                                         *mime_type);
303                         }
304                         g_strfreev (mime_types);
305                 }
306         }
307
308 out:
309         g_clear_error (&error);
310         g_object_unref (loader);
311
312         return pixbuf;
313 }
314
315 struct SizeData {
316         gint     width;
317         gint     height;
318         gboolean preserve_aspect_ratio;
319 };
320
321 static void
322 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
323                                      int              width,
324                                      int              height,
325                                      struct SizeData *data)
326 {
327         g_return_if_fail (width > 0 && height > 0);
328
329         if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
330                 if (width < data->width && height < data->height) {
331                         width = width;
332                         height = height;
333                 }
334
335                 if (data->width < 0) {
336                         width = width * (double) data->height / (gdouble) height;
337                         height = data->height;
338                 } else if (data->height < 0) {
339                         height = height * (double) data->width / (double) width;
340                         width = data->width;
341                 } else if ((double) height * (double) data->width >
342                            (double) width * (double) data->height) {
343                         width = 0.5 + (double) width * (double) data->height / (double) height;
344                         height = data->height;
345                 } else {
346                         height = 0.5 + (double) height * (double) data->width / (double) width;
347                         width = data->width;
348                 }
349         } else {
350                 if (data->width > 0) {
351                         width = data->width;
352                 }
353
354                 if (data->height > 0) {
355                         height = data->height;
356                 }
357         }
358
359         gdk_pixbuf_loader_set_size (loader, width, height);
360 }
361
362 static void
363 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
364 {
365         gint width, height, rowstride;
366         guchar *pixels;
367
368         width = gdk_pixbuf_get_width (pixbuf);
369         height = gdk_pixbuf_get_height (pixbuf);
370         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
371         pixels = gdk_pixbuf_get_pixels (pixbuf);
372
373         if (width < 6 || height < 6) {
374                 return;
375         }
376
377         /* Top left */
378         pixels[3] = 0;
379         pixels[7] = 0x80;
380         pixels[11] = 0xC0;
381         pixels[rowstride + 3] = 0x80;
382         pixels[rowstride * 2 + 3] = 0xC0;
383
384         /* Top right */
385         pixels[width * 4 - 1] = 0;
386         pixels[width * 4 - 5] = 0x80;
387         pixels[width * 4 - 9] = 0xC0;
388         pixels[rowstride + (width * 4) - 1] = 0x80;
389         pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
390
391         /* Bottom left */
392         pixels[(height - 1) * rowstride + 3] = 0;
393         pixels[(height - 1) * rowstride + 7] = 0x80;
394         pixels[(height - 1) * rowstride + 11] = 0xC0;
395         pixels[(height - 2) * rowstride + 3] = 0x80;
396         pixels[(height - 3) * rowstride + 3] = 0xC0;
397
398         /* Bottom right */
399         pixels[height * rowstride - 1] = 0;
400         pixels[(height - 1) * rowstride - 1] = 0x80;
401         pixels[(height - 2) * rowstride - 1] = 0xC0;
402         pixels[height * rowstride - 5] = 0x80;
403         pixels[height * rowstride - 9] = 0xC0;
404 }
405
406 static gboolean
407 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
408 {
409         gint height, rowstride, i;
410         guchar *pixels;
411         guchar *row;
412
413         height = gdk_pixbuf_get_height (pixbuf);
414         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
415         pixels = gdk_pixbuf_get_pixels (pixbuf);
416
417         row = pixels;
418         for (i = 3; i < rowstride; i+=4) {
419                 if (row[i] < 0xfe) {
420                         return FALSE;
421                 }
422         }
423
424         for (i = 1; i < height - 1; i++) {
425                 row = pixels + (i*rowstride);
426                 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
427                         return FALSE;
428                 }
429         }
430
431         row = pixels + ((height-1) * rowstride);
432         for (i = 3; i < rowstride; i+=4) {
433                 if (row[i] < 0xfe) {
434                         return FALSE;
435                 }
436         }
437
438         return TRUE;
439 }
440
441 static GdkPixbuf *
442 avatar_pixbuf_from_loader (GdkPixbufLoader *loader)
443 {
444         GdkPixbuf *pixbuf;
445
446         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
447         if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
448                 GdkPixbuf *rounded_pixbuf;
449
450                 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
451                                                  gdk_pixbuf_get_width (pixbuf),
452                                                  gdk_pixbuf_get_height (pixbuf));
453                 gdk_pixbuf_copy_area (pixbuf, 0, 0,
454                                       gdk_pixbuf_get_width (pixbuf),
455                                       gdk_pixbuf_get_height (pixbuf),
456                                       rounded_pixbuf,
457                                       0, 0);
458                 pixbuf = rounded_pixbuf;
459         } else {
460                 g_object_ref (pixbuf);
461         }
462
463         if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
464                 empathy_avatar_pixbuf_roundify (pixbuf);
465         }
466
467         return pixbuf;
468 }
469
470 GdkPixbuf *
471 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
472                                   gint          width,
473                                   gint          height)
474 {
475         GdkPixbuf        *pixbuf;
476         GdkPixbufLoader  *loader;
477         struct SizeData   data;
478         GError           *error = NULL;
479
480         if (!avatar) {
481                 return NULL;
482         }
483
484         data.width = width;
485         data.height = height;
486         data.preserve_aspect_ratio = TRUE;
487
488         loader = gdk_pixbuf_loader_new ();
489
490         g_signal_connect (loader, "size-prepared",
491                           G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
492                           &data);
493
494         if (avatar->len == 0) {
495                 g_warning ("Avatar has 0 length");
496                 return NULL;
497         } else if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
498                 g_warning ("Couldn't write avatar image:%p with "
499                            "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
500                            avatar->data, avatar->len, error->message);
501                 g_error_free (error);
502                 return NULL;
503         }
504
505         gdk_pixbuf_loader_close (loader, NULL);
506         pixbuf = avatar_pixbuf_from_loader (loader);
507
508         g_object_unref (loader);
509
510         return pixbuf;
511 }
512
513 GdkPixbuf *
514 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
515                                           gint           width,
516                                           gint           height)
517 {
518         EmpathyAvatar *avatar;
519
520         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
521
522         avatar = empathy_contact_get_avatar (contact);
523
524         return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
525 }
526
527 typedef struct {
528         FolksIndividual *individual;
529         GSimpleAsyncResult *result;
530         guint width;
531         guint height;
532         struct SizeData size_data;
533         GdkPixbufLoader *loader;
534         GCancellable *cancellable;
535         guint8 data[512];
536 } PixbufAvatarFromIndividualClosure;
537
538 static PixbufAvatarFromIndividualClosure *
539 pixbuf_avatar_from_individual_closure_new (FolksIndividual    *individual,
540                                            GSimpleAsyncResult *result,
541                                            gint                width,
542                                            gint                height,
543                                            GCancellable       *cancellable)
544 {
545         PixbufAvatarFromIndividualClosure *closure;
546
547         g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
548         g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
549
550         closure = g_new0 (PixbufAvatarFromIndividualClosure, 1);
551         closure->individual = g_object_ref (individual);
552         closure->result = g_object_ref (result);
553         closure->width = width;
554         closure->height = height;
555         if (cancellable != NULL)
556                 closure->cancellable = g_object_ref (cancellable);
557
558         return closure;
559 }
560
561 static void
562 pixbuf_avatar_from_individual_closure_free (
563                 PixbufAvatarFromIndividualClosure *closure)
564 {
565         g_clear_object (&closure->cancellable);
566         tp_clear_object (&closure->loader);
567         g_object_unref (closure->individual);
568         g_object_unref (closure->result);
569         g_free (closure);
570 }
571
572 static void
573 avatar_icon_load_close_cb (GObject      *object,
574                            GAsyncResult *result,
575                            gpointer      user_data)
576 {
577         GError *error = NULL;
578
579         g_input_stream_close_finish (G_INPUT_STREAM (object), result, &error);
580
581         if (error != NULL) {
582                 DEBUG ("Failed to close pixbuf stream: %s", error->message);
583                 g_error_free (error);
584         }
585 }
586
587 static void
588 avatar_icon_load_read_cb (GObject      *object,
589                           GAsyncResult *result,
590                           gpointer      user_data)
591 {
592         GInputStream *stream = G_INPUT_STREAM (object);
593         PixbufAvatarFromIndividualClosure *closure = user_data;
594         gssize n_read;
595         GError *error = NULL;
596
597         /* Finish reading this chunk from the stream */
598         n_read = g_input_stream_read_finish (stream, result, &error);
599         if (error != NULL) {
600                 DEBUG ("Failed to finish read from pixbuf stream: %s",
601                         error->message);
602                 g_simple_async_result_set_from_error (closure->result, error);
603                 goto out_close;
604         }
605
606         /* Write the chunk to the pixbuf loader */
607         if (!gdk_pixbuf_loader_write (closure->loader, (guchar *) closure->data,
608                         n_read, &error)) {
609                 DEBUG ("Failed to write to pixbuf loader: %s",
610                         error ? error->message : "No error given");
611                 g_simple_async_result_set_from_error (closure->result, error);
612                 goto out_close;
613         }
614
615         if (n_read == 0) {
616                 /* EOF? */
617                 if (!gdk_pixbuf_loader_close (closure->loader, &error)) {
618                         DEBUG ("Failed to close pixbuf loader: %s",
619                                 error ? error->message : "No error given");
620                         g_simple_async_result_set_from_error (closure->result, error);
621                         goto out;
622                 }
623
624                 /* We're done. */
625                 g_simple_async_result_set_op_res_gpointer (closure->result,
626                         avatar_pixbuf_from_loader (closure->loader),
627                         g_object_unref);
628
629                 goto out;
630         } else {
631                 /* Loop round and read another chunk. */
632                 g_input_stream_read_async (stream, closure->data,
633                         G_N_ELEMENTS (closure->data),
634                         G_PRIORITY_DEFAULT, closure->cancellable,
635                         avatar_icon_load_read_cb, closure);
636
637                 return;
638         }
639
640 out_close:
641         /* We must close the pixbuf loader before unreffing it. */
642         gdk_pixbuf_loader_close (closure->loader, NULL);
643
644 out:
645         /* Close the file for safety (even though it should be
646          * automatically closed when the stream is finalised). */
647         g_input_stream_close_async (stream, G_PRIORITY_DEFAULT, NULL,
648                 (GAsyncReadyCallback) avatar_icon_load_close_cb, NULL);
649
650         g_simple_async_result_complete (closure->result);
651
652         g_clear_error (&error);
653         pixbuf_avatar_from_individual_closure_free (closure);
654 }
655
656 static void
657 avatar_icon_load_cb (GObject      *object,
658                      GAsyncResult *result,
659                      gpointer      user_data)
660 {
661         GLoadableIcon *icon = G_LOADABLE_ICON (object);
662         PixbufAvatarFromIndividualClosure *closure = user_data;
663         GInputStream *stream;
664         GError *error = NULL;
665
666         stream = g_loadable_icon_load_finish (icon, result, NULL, &error);
667         if (error != NULL) {
668                 DEBUG ("Failed to open avatar stream: %s", error->message);
669                 g_simple_async_result_set_from_error (closure->result, error);
670                 goto out;
671         }
672
673         closure->size_data.width = closure->width;
674         closure->size_data.height = closure->height;
675         closure->size_data.preserve_aspect_ratio = TRUE;
676
677         /* Load the data into a pixbuf loader in chunks. */
678         closure->loader = gdk_pixbuf_loader_new ();
679
680         g_signal_connect (closure->loader, "size-prepared",
681                           G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
682                           &(closure->size_data));
683
684         /* Begin to read the first chunk. */
685         g_input_stream_read_async (stream, closure->data,
686                         G_N_ELEMENTS (closure->data),
687                         G_PRIORITY_DEFAULT, closure->cancellable,
688                         avatar_icon_load_read_cb, closure);
689
690         g_object_unref (stream);
691
692         return;
693
694 out:
695         g_simple_async_result_complete (closure->result);
696
697         g_clear_error (&error);
698         tp_clear_object (&stream);
699         pixbuf_avatar_from_individual_closure_free (closure);
700 }
701
702 void
703 empathy_pixbuf_avatar_from_individual_scaled_async (
704                 FolksIndividual     *individual,
705                 gint                 width,
706                 gint                 height,
707                 GCancellable        *cancellable,
708                 GAsyncReadyCallback  callback,
709                 gpointer             user_data)
710 {
711         GLoadableIcon *avatar_icon;
712         GSimpleAsyncResult *result;
713         PixbufAvatarFromIndividualClosure *closure;
714
715         result = g_simple_async_result_new (G_OBJECT (individual),
716                         callback, user_data,
717                         empathy_pixbuf_avatar_from_individual_scaled_async);
718
719         avatar_icon =
720                 folks_avatar_details_get_avatar (FOLKS_AVATAR_DETAILS (individual));
721         if (avatar_icon == NULL)
722                 goto out;
723
724         closure = pixbuf_avatar_from_individual_closure_new (individual, result,
725                                                              width, height,
726                                                              cancellable);
727         if (closure == NULL)
728                 goto out;
729
730         g_loadable_icon_load_async (avatar_icon, width, cancellable,
731                         avatar_icon_load_cb, closure);
732
733         g_object_unref (result);
734
735         return;
736
737 out:
738         g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
739         g_simple_async_result_complete (result);
740         g_object_unref (result);
741 }
742
743 /* Return a ref on the GdkPixbuf */
744 GdkPixbuf *
745 empathy_pixbuf_avatar_from_individual_scaled_finish (
746                 FolksIndividual *individual,
747                 GAsyncResult *result,
748                 GError **error)
749 {
750         GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
751         gboolean result_valid;
752         GdkPixbuf *pixbuf;
753
754         g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
755         g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
756
757         if (g_simple_async_result_propagate_error (simple, error))
758                 return NULL;
759
760         result_valid = g_simple_async_result_is_valid (result,
761                         G_OBJECT (individual),
762                         empathy_pixbuf_avatar_from_individual_scaled_async);
763         g_return_val_if_fail (result_valid, NULL);
764
765         pixbuf = g_simple_async_result_get_op_res_gpointer (simple);
766         return pixbuf != NULL ? g_object_ref (pixbuf) : NULL;
767 }
768
769 GdkPixbuf *
770 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
771                                    gboolean       show_protocol)
772 {
773         const gchar *icon_name;
774
775         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
776
777         icon_name = empathy_icon_name_for_contact (contact);
778
779         if (icon_name == NULL) {
780                 return NULL;
781         }
782         return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
783             icon_name,
784             show_protocol);
785 }
786
787 GdkPixbuf *
788 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
789                                           const gchar    *icon_name,
790                                           gboolean       show_protocol)
791 {
792         GdkPixbuf *pix_status;
793         GdkPixbuf *pix_protocol;
794         gchar     *icon_filename;
795         gint       height, width;
796         gint       numerator, denominator;
797
798         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
799                         (show_protocol == FALSE), NULL);
800         g_return_val_if_fail (icon_name != NULL, NULL);
801
802         numerator = 3;
803         denominator = 4;
804
805         icon_filename = empathy_filename_from_icon_name (icon_name,
806                                                          GTK_ICON_SIZE_MENU);
807         if (icon_filename == NULL) {
808                 DEBUG ("icon name: %s could not be found\n", icon_name);
809                 return NULL;
810         }
811
812         pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
813
814         if (pix_status == NULL) {
815                 DEBUG ("Could not open icon %s\n", icon_filename);
816                 g_free (icon_filename);
817                 return NULL;
818         }
819
820         g_free (icon_filename);
821
822         if (!show_protocol)
823                 return pix_status;
824
825         height = gdk_pixbuf_get_height (pix_status);
826         width = gdk_pixbuf_get_width (pix_status);
827
828         pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
829                                                                     width * numerator / denominator,
830                                                                     height * numerator / denominator);
831
832         if (pix_protocol == NULL) {
833                 return pix_status;
834         }
835         gdk_pixbuf_composite (pix_protocol, pix_status,
836             0, height - height * numerator / denominator,
837             width * numerator / denominator, height * numerator / denominator,
838             0, height - height * numerator / denominator,
839             1, 1,
840             GDK_INTERP_BILINEAR, 255);
841
842         g_object_unref (pix_protocol);
843
844         return pix_status;
845 }
846
847 GdkPixbuf *
848 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
849                                           gint           width,
850                                           gint           height)
851 {
852         TpAccount *account;
853         gchar     *filename;
854         GdkPixbuf *pixbuf = NULL;
855
856         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
857
858         account = empathy_contact_get_account (contact);
859         filename = empathy_filename_from_icon_name (tp_account_get_icon_name (account),
860                                                     GTK_ICON_SIZE_MENU);
861         if (filename != NULL) {
862                 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
863                 g_free (filename);
864         }
865
866         return pixbuf;
867 }
868
869 GdkPixbuf *
870 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
871 {
872         gint      width, height;
873         gdouble   factor;
874
875         width = gdk_pixbuf_get_width (pixbuf);
876         height = gdk_pixbuf_get_height (pixbuf);
877
878         if (width > 0 && (width > max_size || height > max_size)) {
879                 factor = (gdouble) max_size / MAX (width, height);
880
881                 width = width * factor;
882                 height = height * factor;
883
884                 return gdk_pixbuf_scale_simple (pixbuf,
885                                                 width, height,
886                                                 GDK_INTERP_HYPER);
887         }
888
889         return g_object_ref (pixbuf);
890 }
891
892 GdkPixbuf *
893 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
894                                      gint size)
895 {
896         GtkIconTheme *theme;
897         GdkPixbuf *pixbuf;
898         GError *error = NULL;
899
900         if (!icon_name) {
901                 return NULL;
902         }
903
904         theme = gtk_icon_theme_get_default ();
905
906         pixbuf = gtk_icon_theme_load_icon (theme,
907                                            icon_name,
908                                            size,
909                                            0,
910                                            &error);
911         if (error) {
912                 DEBUG ("Error loading icon: %s", error->message);
913                 g_clear_error (&error);
914         }
915
916         return pixbuf;
917 }
918
919 GdkPixbuf *
920 empathy_pixbuf_from_icon_name (const gchar *icon_name,
921                                GtkIconSize  icon_size)
922 {
923         gint  w, h;
924         gint  size = 48;
925
926         if (!icon_name) {
927                 return NULL;
928         }
929
930         if (gtk_icon_size_lookup (icon_size, &w, &h)) {
931                 size = (w + h) / 2;
932         }
933
934         return empathy_pixbuf_from_icon_name_sized (icon_name, size);
935 }
936
937 gchar *
938 empathy_filename_from_icon_name (const gchar *icon_name,
939                                  GtkIconSize  icon_size)
940 {
941         GtkIconTheme *icon_theme;
942         GtkIconInfo  *icon_info;
943         gint          w, h;
944         gint          size = 48;
945         gchar        *ret;
946
947         icon_theme = gtk_icon_theme_get_default ();
948
949         if (gtk_icon_size_lookup (icon_size, &w, &h)) {
950                 size = (w + h) / 2;
951         }
952
953         icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
954         if (icon_info == NULL)
955                 return NULL;
956
957         ret = g_strdup (gtk_icon_info_get_filename (icon_info));
958         gtk_icon_info_free (icon_info);
959
960         return ret;
961 }
962
963 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
964  * that to make it easier to apply changes from the original code.
965  */
966 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
967
968 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
969  * decomposable character it consumes the decomposition length from the given
970  * offset.  So it's useful when the offset was calculated for the normalized
971  * version of str, but we need a pointer to str itself. */
972 static const gchar *
973 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
974 {
975         gchar *casefold, *normal;
976         const gchar *p, *q;
977
978         p = str;
979         while (offset > 0)
980         {
981                 q = g_utf8_next_char (p);
982                 casefold = g_utf8_casefold (p, q - p);
983                 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
984                 offset -= g_utf8_strlen (normal, -1);
985                 g_free (casefold);
986                 g_free (normal);
987                 p = q;
988         }
989         return p;
990 }
991
992 static const gchar *
993 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
994 {
995         gsize needle_len;
996         gsize haystack_len;
997         const gchar *ret = NULL;
998         gchar *p;
999         gchar *casefold;
1000         gchar *caseless_haystack;
1001         gint i;
1002
1003         g_return_val_if_fail (haystack != NULL, NULL);
1004         g_return_val_if_fail (needle != NULL, NULL);
1005
1006         casefold = g_utf8_casefold (haystack, -1);
1007         caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1008         g_free (casefold);
1009
1010         needle_len = g_utf8_strlen (needle, -1);
1011         haystack_len = g_utf8_strlen (caseless_haystack, -1);
1012
1013         if (needle_len == 0)
1014         {
1015                 ret = (gchar *) haystack;
1016                 goto finally_1;
1017         }
1018
1019         if (haystack_len < needle_len)
1020         {
1021                 ret = NULL;
1022                 goto finally_1;
1023         }
1024
1025         p = (gchar *) caseless_haystack;
1026         needle_len = strlen (needle);
1027         i = 0;
1028
1029         while (*p)
1030         {
1031                 if ((strncmp (p, needle, needle_len) == 0))
1032                 {
1033                         ret = pointer_from_offset_skipping_decomp (haystack, i);
1034                         goto finally_1;
1035                 }
1036
1037                 p = g_utf8_next_char (p);
1038                 i++;
1039         }
1040
1041 finally_1:
1042         g_free (caseless_haystack);
1043
1044         return ret;
1045 }
1046
1047 static gboolean
1048 g_utf8_caselessnmatch (const char *s1, const char *s2,
1049                        gssize n1, gssize n2)
1050 {
1051         gchar *casefold;
1052         gchar *normalized_s1;
1053         gchar *normalized_s2;
1054         gint len_s1;
1055         gint len_s2;
1056         gboolean ret = FALSE;
1057
1058         g_return_val_if_fail (s1 != NULL, FALSE);
1059         g_return_val_if_fail (s2 != NULL, FALSE);
1060         g_return_val_if_fail (n1 > 0, FALSE);
1061         g_return_val_if_fail (n2 > 0, FALSE);
1062
1063         casefold = g_utf8_casefold (s1, n1);
1064         normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1065         g_free (casefold);
1066
1067         casefold = g_utf8_casefold (s2, n2);
1068         normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1069         g_free (casefold);
1070
1071         len_s1 = strlen (normalized_s1);
1072         len_s2 = strlen (normalized_s2);
1073
1074         if (len_s1 < len_s2)
1075                 goto finally_2;
1076
1077         ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
1078
1079 finally_2:
1080         g_free (normalized_s1);
1081         g_free (normalized_s2);
1082
1083         return ret;
1084 }
1085
1086 static void
1087 forward_chars_with_skipping (GtkTextIter *iter,
1088                              gint         count,
1089                              gboolean     skip_invisible,
1090                              gboolean     skip_nontext,
1091                              gboolean     skip_decomp)
1092 {
1093         gint i;
1094
1095         g_return_if_fail (count >= 0);
1096
1097         i = count;
1098
1099         while (i > 0)
1100         {
1101                 gboolean ignored = FALSE;
1102
1103                 /* minimal workaround to avoid the infinite loop of bug #168247.
1104                  * It doesn't fix the problemjust the symptom...
1105                  */
1106                 if (gtk_text_iter_is_end (iter))
1107                         return;
1108
1109                 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
1110                         ignored = TRUE;
1111
1112                 if (!ignored && skip_invisible &&
1113                     /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
1114                         ignored = TRUE;
1115
1116                 if (!ignored && skip_decomp)
1117                 {
1118                         /* being UTF8 correct sucks; this accounts for extra
1119                            offsets coming from canonical decompositions of
1120                            UTF8 characters (e.g. accented characters) which
1121                            g_utf8_normalize () performs */
1122                         gchar *normal;
1123                         gchar buffer[6];
1124                         gint buffer_len;
1125
1126                         buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
1127                         normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
1128                         i -= (g_utf8_strlen (normal, -1) - 1);
1129                         g_free (normal);
1130                 }
1131
1132                 gtk_text_iter_forward_char (iter);
1133
1134                 if (!ignored)
1135                         --i;
1136         }
1137 }
1138
1139 static gboolean
1140 lines_match (const GtkTextIter *start,
1141              const gchar      **lines,
1142              gboolean           visible_only,
1143              gboolean           slice,
1144              GtkTextIter       *match_start,
1145              GtkTextIter       *match_end)
1146 {
1147         GtkTextIter next;
1148         gchar *line_text;
1149         const gchar *found;
1150         gint offset;
1151
1152         if (*lines == NULL || **lines == '\0')
1153         {
1154                 if (match_start)
1155                         *match_start = *start;
1156                 if (match_end)
1157                         *match_end = *start;
1158                 return TRUE;
1159         }
1160
1161         next = *start;
1162         gtk_text_iter_forward_line (&next);
1163
1164         /* No more text in buffer, but *lines is nonempty */
1165         if (gtk_text_iter_equal (start, &next))
1166                 return FALSE;
1167
1168         if (slice)
1169         {
1170                 if (visible_only)
1171                         line_text = gtk_text_iter_get_visible_slice (start, &next);
1172                 else
1173                         line_text = gtk_text_iter_get_slice (start, &next);
1174         }
1175         else
1176         {
1177                 if (visible_only)
1178                         line_text = gtk_text_iter_get_visible_text (start, &next);
1179                 else
1180                         line_text = gtk_text_iter_get_text (start, &next);
1181         }
1182
1183         if (match_start) /* if this is the first line we're matching */
1184         {
1185                 found = g_utf8_strcasestr (line_text, *lines);
1186         }
1187         else
1188         {
1189                 /* If it's not the first line, we have to match from the
1190                  * start of the line.
1191                  */
1192                 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1193                                            strlen (*lines)))
1194                         found = line_text;
1195                 else
1196                         found = NULL;
1197         }
1198
1199         if (found == NULL)
1200         {
1201                 g_free (line_text);
1202                 return FALSE;
1203         }
1204
1205         /* Get offset to start of search string */
1206         offset = g_utf8_strlen (line_text, found - line_text);
1207
1208         next = *start;
1209
1210         /* If match start needs to be returned, set it to the
1211          * start of the search string.
1212          */
1213         forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1214         if (match_start)
1215         {
1216                 *match_start = next;
1217         }
1218
1219         /* Go to end of search string */
1220         forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1221
1222         g_free (line_text);
1223
1224         ++lines;
1225
1226         if (match_end)
1227                 *match_end = next;
1228
1229         /* pass NULL for match_start, since we don't need to find the
1230          * start again.
1231          */
1232         return lines_match (&next, lines, visible_only, slice, NULL, match_end);
1233 }
1234
1235 /* strsplit () that retains the delimiter as part of the string. */
1236 static gchar **
1237 strbreakup (const char *string,
1238             const char *delimiter,
1239             gint        max_tokens)
1240 {
1241         GSList *string_list = NULL, *slist;
1242         gchar **str_array, *s, *casefold, *new_string;
1243         guint i, n = 1;
1244
1245         g_return_val_if_fail (string != NULL, NULL);
1246         g_return_val_if_fail (delimiter != NULL, NULL);
1247
1248         if (max_tokens < 1)
1249                 max_tokens = G_MAXINT;
1250
1251         s = strstr (string, delimiter);
1252         if (s)
1253         {
1254                 guint delimiter_len = strlen (delimiter);
1255
1256                 do
1257                 {
1258                         guint len;
1259
1260                         len = s - string + delimiter_len;
1261                         new_string = g_new (gchar, len + 1);
1262                         strncpy (new_string, string, len);
1263                         new_string[len] = 0;
1264                         casefold = g_utf8_casefold (new_string, -1);
1265                         g_free (new_string);
1266                         new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1267                         g_free (casefold);
1268                         string_list = g_slist_prepend (string_list, new_string);
1269                         n++;
1270                         string = s + delimiter_len;
1271                         s = strstr (string, delimiter);
1272                 } while (--max_tokens && s);
1273         }
1274
1275         if (*string)
1276         {
1277                 n++;
1278                 casefold = g_utf8_casefold (string, -1);
1279                 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1280                 g_free (casefold);
1281                 string_list = g_slist_prepend (string_list, new_string);
1282         }
1283
1284         str_array = g_new (gchar*, n);
1285
1286         i = n - 1;
1287
1288         str_array[i--] = NULL;
1289         for (slist = string_list; slist; slist = slist->next)
1290                 str_array[i--] = slist->data;
1291
1292         g_slist_free (string_list);
1293
1294         return str_array;
1295 }
1296
1297 gboolean
1298 empathy_text_iter_forward_search (const GtkTextIter   *iter,
1299                                  const gchar         *str,
1300                                  GtkTextIter         *match_start,
1301                                  GtkTextIter         *match_end,
1302                                  const GtkTextIter   *limit)
1303 {
1304         gchar **lines = NULL;
1305         GtkTextIter match;
1306         gboolean retval = FALSE;
1307         GtkTextIter search;
1308         gboolean visible_only;
1309         gboolean slice;
1310
1311         g_return_val_if_fail (iter != NULL, FALSE);
1312         g_return_val_if_fail (str != NULL, FALSE);
1313
1314         if (limit && gtk_text_iter_compare (iter, limit) >= 0)
1315                 return FALSE;
1316
1317         if (*str == '\0') {
1318                 /* If we can move one char, return the empty string there */
1319                 match = *iter;
1320
1321                 if (gtk_text_iter_forward_char (&match)) {
1322                         if (limit && gtk_text_iter_equal (&match, limit)) {
1323                                 return FALSE;
1324                         }
1325
1326                         if (match_start) {
1327                                 *match_start = match;
1328                         }
1329                         if (match_end) {
1330                                 *match_end = match;
1331                         }
1332                         return TRUE;
1333                 } else {
1334                         return FALSE;
1335                 }
1336         }
1337
1338         visible_only = TRUE;
1339         slice = FALSE;
1340
1341         /* locate all lines */
1342         lines = strbreakup (str, "\n", -1);
1343
1344         search = *iter;
1345
1346         do {
1347                 /* This loop has an inefficient worst-case, where
1348                  * gtk_text_iter_get_text () is called repeatedly on
1349                  * a single line.
1350                  */
1351                 GtkTextIter end;
1352
1353                 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1354                         break;
1355                 }
1356
1357                 if (lines_match (&search, (const gchar**)lines,
1358                                  visible_only, slice, &match, &end)) {
1359                         if (limit == NULL ||
1360                             (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1361                                 retval = TRUE;
1362
1363                                 if (match_start) {
1364                                         *match_start = match;
1365                                 }
1366                                 if (match_end) {
1367                                         *match_end = end;
1368                                 }
1369                         }
1370                         break;
1371                 }
1372         } while (gtk_text_iter_forward_line (&search));
1373
1374         g_strfreev ((gchar **) lines);
1375
1376         return retval;
1377 }
1378
1379 static const gchar *
1380 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1381 {
1382         gsize needle_len;
1383         gsize haystack_len;
1384         const gchar *ret = NULL;
1385         gchar *p;
1386         gchar *casefold;
1387         gchar *caseless_haystack;
1388         gint i;
1389
1390         g_return_val_if_fail (haystack != NULL, NULL);
1391         g_return_val_if_fail (needle != NULL, NULL);
1392
1393         casefold = g_utf8_casefold (haystack, -1);
1394         caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1395         g_free (casefold);
1396
1397         needle_len = g_utf8_strlen (needle, -1);
1398         haystack_len = g_utf8_strlen (caseless_haystack, -1);
1399
1400         if (needle_len == 0)
1401         {
1402                 ret = (gchar *) haystack;
1403                 goto finally_1;
1404         }
1405
1406         if (haystack_len < needle_len)
1407         {
1408                 ret = NULL;
1409                 goto finally_1;
1410         }
1411
1412         i = haystack_len - needle_len;
1413         p = g_utf8_offset_to_pointer (caseless_haystack, i);
1414         needle_len = strlen (needle);
1415
1416         while (p >= caseless_haystack)
1417         {
1418                 if (strncmp (p, needle, needle_len) == 0)
1419                 {
1420                         ret = pointer_from_offset_skipping_decomp (haystack, i);
1421                         goto finally_1;
1422                 }
1423
1424                 p = g_utf8_prev_char (p);
1425                 i--;
1426         }
1427
1428 finally_1:
1429         g_free (caseless_haystack);
1430
1431         return ret;
1432 }
1433
1434 static gboolean
1435 backward_lines_match (const GtkTextIter *start,
1436                       const gchar      **lines,
1437                       gboolean           visible_only,
1438                       gboolean           slice,
1439                       GtkTextIter       *match_start,
1440                       GtkTextIter       *match_end)
1441 {
1442         GtkTextIter line, next;
1443         gchar *line_text;
1444         const gchar *found;
1445         gint offset;
1446
1447         if (*lines == NULL || **lines == '\0')
1448         {
1449                 if (match_start)
1450                         *match_start = *start;
1451                 if (match_end)
1452                         *match_end = *start;
1453                 return TRUE;
1454         }
1455
1456         line = next = *start;
1457         if (gtk_text_iter_get_line_offset (&next) == 0)
1458         {
1459                 if (!gtk_text_iter_backward_line (&next))
1460                         return FALSE;
1461         }
1462         else
1463                 gtk_text_iter_set_line_offset (&next, 0);
1464
1465         if (slice)
1466         {
1467                 if (visible_only)
1468                         line_text = gtk_text_iter_get_visible_slice (&next, &line);
1469                 else
1470                         line_text = gtk_text_iter_get_slice (&next, &line);
1471         }
1472         else
1473         {
1474                 if (visible_only)
1475                         line_text = gtk_text_iter_get_visible_text (&next, &line);
1476                 else
1477                         line_text = gtk_text_iter_get_text (&next, &line);
1478         }
1479
1480         if (match_start) /* if this is the first line we're matching */
1481         {
1482                 found = g_utf8_strrcasestr (line_text, *lines);
1483         }
1484         else
1485         {
1486                 /* If it's not the first line, we have to match from the
1487                  * start of the line.
1488                  */
1489                 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1490                                            strlen (*lines)))
1491                         found = line_text;
1492                 else
1493                         found = NULL;
1494         }
1495
1496         if (found == NULL)
1497         {
1498                 g_free (line_text);
1499                 return FALSE;
1500         }
1501
1502         /* Get offset to start of search string */
1503         offset = g_utf8_strlen (line_text, found - line_text);
1504
1505         forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1506
1507         /* If match start needs to be returned, set it to the
1508          * start of the search string.
1509          */
1510         if (match_start)
1511         {
1512                 *match_start = next;
1513         }
1514
1515         /* Go to end of search string */
1516         forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1517
1518         g_free (line_text);
1519
1520         ++lines;
1521
1522         if (match_end)
1523                 *match_end = next;
1524
1525         /* try to match the rest of the lines forward, passing NULL
1526          * for match_start so lines_match will try to match the entire
1527          * line */
1528         return lines_match (&next, lines, visible_only,
1529                             slice, NULL, match_end);
1530 }
1531
1532 gboolean
1533 empathy_text_iter_backward_search (const GtkTextIter   *iter,
1534                                   const gchar         *str,
1535                                   GtkTextIter         *match_start,
1536                                   GtkTextIter         *match_end,
1537                                   const GtkTextIter   *limit)
1538 {
1539         gchar **lines = NULL;
1540         GtkTextIter match;
1541         gboolean retval = FALSE;
1542         GtkTextIter search;
1543         gboolean visible_only;
1544         gboolean slice;
1545
1546         g_return_val_if_fail (iter != NULL, FALSE);
1547         g_return_val_if_fail (str != NULL, FALSE);
1548
1549         if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1550                 return FALSE;
1551
1552         if (*str == '\0')
1553         {
1554                 /* If we can move one char, return the empty string there */
1555                 match = *iter;
1556
1557                 if (gtk_text_iter_backward_char (&match))
1558                 {
1559                         if (limit && gtk_text_iter_equal (&match, limit))
1560                                 return FALSE;
1561
1562                         if (match_start)
1563                                 *match_start = match;
1564                         if (match_end)
1565                                 *match_end = match;
1566                         return TRUE;
1567                 }
1568                 else
1569                 {
1570                         return FALSE;
1571                 }
1572         }
1573
1574         visible_only = TRUE;
1575         slice = TRUE;
1576
1577         /* locate all lines */
1578         lines = strbreakup (str, "\n", -1);
1579
1580         search = *iter;
1581
1582         while (TRUE)
1583         {
1584                 /* This loop has an inefficient worst-case, where
1585                  * gtk_text_iter_get_text () is called repeatedly on
1586                  * a single line.
1587                  */
1588                 GtkTextIter end;
1589
1590                 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1591                         break;
1592
1593                 if (backward_lines_match (&search, (const gchar**)lines,
1594                                           visible_only, slice, &match, &end))
1595                 {
1596                         if (limit == NULL || (limit &&
1597                                               gtk_text_iter_compare (&end, limit) > 0))
1598                         {
1599                                 retval = TRUE;
1600
1601                                 if (match_start)
1602                                         *match_start = match;
1603                                 if (match_end)
1604                                         *match_end = end;
1605                         }
1606                         break;
1607                 }
1608
1609                 if (gtk_text_iter_get_line_offset (&search) == 0)
1610                 {
1611                         if (!gtk_text_iter_backward_line (&search))
1612                                 break;
1613                 }
1614                 else
1615                 {
1616                         gtk_text_iter_set_line_offset (&search, 0);
1617                 }
1618         }
1619
1620         g_strfreev ((gchar **) lines);
1621
1622         return retval;
1623 }
1624
1625 /* Takes care of moving the window to the current workspace. */
1626 void
1627 empathy_window_present_with_time (GtkWindow *window,
1628                         guint32 timestamp)
1629 {
1630         GdkWindow *gdk_window;
1631
1632         g_return_if_fail (GTK_IS_WINDOW (window));
1633
1634         /* Move the window to the current workspace before trying to show it.
1635          * This is the behaviour people expect when clicking on the statusbar icon. */
1636         gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1637         if (gdk_window) {
1638                 gint x, y;
1639                 gint w, h;
1640
1641                 /* Has no effect if the WM has viewports, like compiz */
1642                 gdk_x11_window_move_to_current_desktop (gdk_window);
1643
1644                 /* If window is still off-screen, hide it to force it to
1645                  * reposition on the current workspace. */
1646                 gtk_window_get_position (window, &x, &y);
1647                 gtk_window_get_size (window, &w, &h);
1648                 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1649                         gtk_widget_hide (GTK_WIDGET (window));
1650         }
1651
1652         if (timestamp == GDK_CURRENT_TIME)
1653                 gtk_window_present (window);
1654         else
1655                 gtk_window_present_with_time (window, timestamp);
1656 }
1657
1658 void
1659 empathy_window_present (GtkWindow *window)
1660 {
1661   empathy_window_present_with_time (window, gtk_get_current_event_time ());
1662 }
1663
1664 GtkWindow *
1665 empathy_get_toplevel_window (GtkWidget *widget)
1666 {
1667         GtkWidget *toplevel;
1668
1669         g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1670
1671         toplevel = gtk_widget_get_toplevel (widget);
1672         if (GTK_IS_WINDOW (toplevel) &&
1673             gtk_widget_is_toplevel (toplevel)) {
1674                 return GTK_WINDOW (toplevel);
1675         }
1676
1677         return NULL;
1678 }
1679
1680 /** empathy_make_absolute_url_len:
1681  * @url: an url
1682  * @len: a length
1683  *
1684  * Same as #empathy_make_absolute_url but for a limited string length
1685  */
1686 gchar *
1687 empathy_make_absolute_url_len (const gchar *url,
1688                                guint len)
1689 {
1690         g_return_val_if_fail (url != NULL, NULL);
1691
1692         if (g_str_has_prefix (url, "ghelp:") ||
1693             g_str_has_prefix (url, "mailto:") ||
1694             strstr (url, ":/")) {
1695                 return g_strndup (url, len);
1696         }
1697
1698         if (strstr (url, "@")) {
1699                 return g_strdup_printf ("mailto:%.*s", len, url);
1700         }
1701
1702         return g_strdup_printf ("http://%.*s", len, url);
1703 }
1704
1705 /** empathy_make_absolute_url:
1706  * @url: an url
1707  *
1708  * The URL opening code can't handle schemeless strings, so we try to be
1709  * smart and add http if there is no scheme or doesn't look like a mail
1710  * address. This should work in most cases, and let us click on strings
1711  * like "www.gnome.org".
1712  *
1713  * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1714  * g_free when your are done with it
1715  */
1716 gchar *
1717 empathy_make_absolute_url (const gchar *url)
1718 {
1719         return empathy_make_absolute_url_len (url, strlen (url));
1720 }
1721
1722 void
1723 empathy_url_show (GtkWidget *parent,
1724                   const char *url)
1725 {
1726         gchar  *real_url;
1727         GError *error = NULL;
1728
1729         g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1730         g_return_if_fail (url != NULL);
1731
1732         real_url = empathy_make_absolute_url (url);
1733
1734         gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1735                       gtk_get_current_event_time (), &error);
1736
1737         if (error) {
1738                 GtkWidget *dialog;
1739
1740                 dialog = gtk_message_dialog_new (NULL, 0,
1741                                                  GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1742                                                  _("Unable to open URI"));
1743                 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1744                                                           "%s", error->message);
1745
1746                 g_signal_connect (dialog, "response",
1747                                   G_CALLBACK (gtk_widget_destroy),
1748                                   NULL);
1749                 gtk_window_present (GTK_WINDOW (dialog));
1750
1751                 g_clear_error (&error);
1752         }
1753
1754         g_free (real_url);
1755 }
1756
1757 void
1758 empathy_send_file (EmpathyContact *contact, GFile *file)
1759 {
1760         EmpathyFTFactory *factory;
1761         GtkRecentManager *manager;
1762         gchar *uri;
1763
1764         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1765         g_return_if_fail (G_IS_FILE (file));
1766
1767         factory = empathy_ft_factory_dup_singleton ();
1768
1769         empathy_ft_factory_new_transfer_outgoing (factory, contact, file,
1770                 empathy_get_current_action_time ());
1771
1772         uri = g_file_get_uri (file);
1773         manager = gtk_recent_manager_get_default ();
1774         gtk_recent_manager_add_item (manager, uri);
1775         g_free (uri);
1776
1777         g_object_unref (factory);
1778 }
1779
1780 void
1781 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1782 {
1783         const gchar *nl;
1784         GFile *file;
1785
1786         /* Only handle a single file for now.  It would be wicked cool to be
1787            able to do multiple files, offering to zip them or whatever like
1788            nautilus-sendto does.  Note that text/uri-list is defined to have
1789            each line terminated by \r\n, but we can be tolerant of applications
1790            that only use \n or don't terminate single-line entries.
1791         */
1792         nl = strstr (uri_list, "\r\n");
1793         if (!nl) {
1794                 nl = strchr (uri_list, '\n');
1795         }
1796         if (nl) {
1797                 gchar *uri = g_strndup (uri_list, nl - uri_list);
1798                 file = g_file_new_for_uri (uri);
1799                 g_free (uri);
1800         }
1801         else {
1802                 file = g_file_new_for_uri (uri_list);
1803         }
1804
1805         empathy_send_file (contact, file);
1806
1807         g_object_unref (file);
1808 }
1809
1810 static void
1811 file_manager_send_file_response_cb (GtkDialog      *widget,
1812                                     gint            response_id,
1813                                     EmpathyContact *contact)
1814 {
1815         GFile *file;
1816
1817         if (response_id == GTK_RESPONSE_OK) {
1818                 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1819
1820                 empathy_send_file (contact, file);
1821
1822                 g_object_unref (file);
1823         }
1824
1825         g_object_unref (contact);
1826         gtk_widget_destroy (GTK_WIDGET (widget));
1827 }
1828
1829 static gboolean
1830 filter_cb (const GtkFileFilterInfo *filter_info,
1831                 gpointer data)
1832 {
1833         /* filter out socket files */
1834         return tp_strdiff (filter_info->mime_type, "inode/socket");
1835 }
1836
1837 static GtkFileFilter *
1838 create_file_filter (void)
1839 {
1840         GtkFileFilter *filter;
1841
1842         filter = gtk_file_filter_new ();
1843
1844         gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_MIME_TYPE, filter_cb,
1845                 NULL, NULL);
1846
1847         return filter;
1848 }
1849
1850 void
1851 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1852 {
1853         GtkWidget               *widget;
1854         GtkWidget               *button;
1855
1856         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1857
1858         DEBUG ("Creating selection file chooser");
1859
1860         widget = gtk_file_chooser_dialog_new (_("Select a file"),
1861                                               NULL,
1862                                               GTK_FILE_CHOOSER_ACTION_OPEN,
1863                                               GTK_STOCK_CANCEL,
1864                                               GTK_RESPONSE_CANCEL,
1865                                               NULL);
1866
1867         /* send button */
1868         button = gtk_button_new_with_mnemonic (_("_Send"));
1869         gtk_button_set_image (GTK_BUTTON (button),
1870                 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1871                                               GTK_ICON_SIZE_BUTTON));
1872         gtk_widget_show (button);
1873         gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1874                                       GTK_RESPONSE_OK);
1875         gtk_widget_set_can_default (button, TRUE);
1876         gtk_dialog_set_default_response (GTK_DIALOG (widget),
1877                                          GTK_RESPONSE_OK);
1878
1879         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1880
1881         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1882                 g_get_home_dir ());
1883
1884         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (widget),
1885                 create_file_filter ());
1886
1887         g_signal_connect (widget, "response",
1888                           G_CALLBACK (file_manager_send_file_response_cb),
1889                           g_object_ref (contact));
1890
1891         gtk_widget_show (widget);
1892 }
1893
1894 static void
1895 file_manager_receive_file_response_cb (GtkDialog *dialog,
1896                                        GtkResponseType response,
1897                                        EmpathyFTHandler *handler)
1898 {
1899         EmpathyFTFactory *factory;
1900         GFile *file;
1901
1902         if (response == GTK_RESPONSE_OK) {
1903                 GFile *parent;
1904                 GFileInfo *info;
1905                 guint64 free_space, file_size;
1906                 GError *error = NULL;
1907
1908                 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1909                 parent = g_file_get_parent (file);
1910                 info = g_file_query_filesystem_info (parent,
1911                                 G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
1912                                 NULL, &error);
1913
1914                 g_object_unref (parent);
1915
1916                 if (error != NULL) {
1917                         g_warning ("Error: %s", error->message);
1918
1919                         g_object_unref (file);
1920                         return;
1921                 }
1922
1923                 free_space = g_file_info_get_attribute_uint64 (info,
1924                                 G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
1925                 file_size = empathy_ft_handler_get_total_bytes (handler);
1926
1927                 g_object_unref (info);
1928
1929                 if (file_size > free_space) {
1930                         GtkWidget *message = gtk_message_dialog_new (
1931                                 GTK_WINDOW (dialog),
1932                                 GTK_DIALOG_MODAL,
1933                                 GTK_MESSAGE_ERROR,
1934                                 GTK_BUTTONS_CLOSE,
1935                                 _("Insufficient free space to save file"));
1936                         char *file_size_str, *free_space_str;
1937
1938                         file_size_str = g_format_size (file_size);
1939                         free_space_str = g_format_size (free_space);
1940
1941                         gtk_message_dialog_format_secondary_text (
1942                                 GTK_MESSAGE_DIALOG (message),
1943                                 _("%s of free space are required to save this "
1944                                   "file, but only %s is available. Please "
1945                                   "choose another location."),
1946                                 file_size_str, free_space_str);
1947
1948                         gtk_dialog_run (GTK_DIALOG (message));
1949
1950                         g_free (file_size_str);
1951                         g_free (free_space_str);
1952                         gtk_widget_destroy (message);
1953
1954                         g_object_unref (file);
1955
1956                         return;
1957                 }
1958
1959                 factory = empathy_ft_factory_dup_singleton ();
1960
1961                 empathy_ft_factory_set_destination_for_incoming_handler (
1962                                 factory, handler, file);
1963
1964                 g_object_unref (factory);
1965                 g_object_unref (file);
1966         } else {
1967                 /* unref the handler, as we dismissed the file chooser,
1968                  * and refused the transfer.
1969                  */
1970                 g_object_unref (handler);
1971         }
1972
1973         gtk_widget_destroy (GTK_WIDGET (dialog));
1974 }
1975
1976 void
1977 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1978 {
1979         GtkWidget *widget;
1980         const gchar *dir;
1981         EmpathyContact *contact;
1982         gchar *title;
1983
1984         contact = empathy_ft_handler_get_contact (handler);
1985         g_assert (contact != NULL);
1986
1987         title = g_strdup_printf (_("Incoming file from %s"),
1988                 empathy_contact_get_alias (contact));
1989
1990         widget = gtk_file_chooser_dialog_new (title,
1991                                               NULL,
1992                                               GTK_FILE_CHOOSER_ACTION_SAVE,
1993                                               GTK_STOCK_CANCEL,
1994                                               GTK_RESPONSE_CANCEL,
1995                                               GTK_STOCK_SAVE,
1996                                               GTK_RESPONSE_OK,
1997                                               NULL);
1998         gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1999                 empathy_ft_handler_get_filename (handler));
2000         gtk_file_chooser_set_do_overwrite_confirmation
2001                 (GTK_FILE_CHOOSER (widget), TRUE);
2002
2003         dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
2004         if (dir == NULL)
2005                 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
2006                 dir = g_get_home_dir ();
2007
2008         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
2009
2010         g_signal_connect (widget, "response",
2011                 G_CALLBACK (file_manager_receive_file_response_cb), handler);
2012
2013         gtk_widget_show (widget);
2014         g_free (title);
2015 }
2016
2017 void
2018 empathy_make_color_whiter (GdkRGBA *color)
2019 {
2020         const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
2021
2022         color->red = (color->red + white.red) / 2;
2023         color->green = (color->green + white.green) / 2;
2024         color->blue = (color->blue + white.blue) / 2;
2025 }
2026
2027 static void
2028 menu_deactivate_cb (GtkMenu *menu,
2029         gpointer user_data)
2030 {
2031         /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
2032         g_signal_handlers_disconnect_by_func (menu,
2033                      menu_deactivate_cb, user_data);
2034
2035         gtk_menu_detach (menu);
2036 }
2037
2038 /* Convenient function to create a GtkMenu attached to @attach_to and detach
2039  * it when the menu is not displayed any more. This is useful when creating a
2040  * context menu that we want to get rid as soon as it as been displayed. */
2041 GtkWidget *
2042 empathy_context_menu_new (GtkWidget *attach_to)
2043 {
2044         GtkWidget *menu;
2045
2046         menu = gtk_menu_new ();
2047
2048         gtk_menu_attach_to_widget (GTK_MENU (menu), attach_to, NULL);
2049
2050         /* menu is initially unowned but gtk_menu_attach_to_widget () taked its
2051          * floating ref. We can either wait that @attach_to releases its ref when
2052          * it will be destroyed (when leaving Empathy most of the time) or explicitely
2053          * detach the menu when it's not displayed any more.
2054          * We go for the latter as we don't want to keep useless menus in memory
2055          * during the whole lifetime of Empathy. */
2056         g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb), NULL);
2057
2058         return menu;
2059 }
2060
2061 gint64
2062 empathy_get_current_action_time (void)
2063 {
2064   return (tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
2065 }
2066
2067 /* @words = empathy_live_search_strip_utf8_string (@text);
2068  *
2069  * User has to pass both so we don't have to compute @words ourself each time
2070  * this function is called. */
2071 gboolean
2072 empathy_individual_match_string (FolksIndividual *individual,
2073     const char *text,
2074     GPtrArray *words)
2075 {
2076   const gchar *str;
2077   GeeSet *personas;
2078   GeeIterator *iter;
2079   gboolean retval = FALSE;
2080
2081   /* check alias name */
2082   str = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual));
2083
2084   if (empathy_live_search_match_words (str, words))
2085     return TRUE;
2086
2087   personas = folks_individual_get_personas (individual);
2088
2089   /* check contact id, remove the @server.com part */
2090   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2091   while (retval == FALSE && gee_iterator_next (iter))
2092     {
2093       FolksPersona *persona = gee_iterator_get (iter);
2094       const gchar *p;
2095
2096       if (empathy_folks_persona_is_interesting (persona))
2097         {
2098           str = folks_persona_get_display_id (persona);
2099
2100           /* Accept the persona if @text is a full prefix of his ID; that allows
2101            * user to find, say, a jabber contact by typing his JID. */
2102           if (g_str_has_prefix (str, text))
2103             {
2104               retval = TRUE;
2105             }
2106           else
2107             {
2108               gchar *dup_str = NULL;
2109               gboolean visible;
2110
2111               p = strstr (str, "@");
2112               if (p != NULL)
2113                 str = dup_str = g_strndup (str, p - str);
2114
2115               visible = empathy_live_search_match_words (str, words);
2116               g_free (dup_str);
2117               if (visible)
2118                 retval = TRUE;
2119             }
2120         }
2121       g_clear_object (&persona);
2122     }
2123   g_clear_object (&iter);
2124
2125   /* FIXME: Add more rules here, we could check phone numbers in
2126    * contact's vCard for example. */
2127   return retval;
2128 }
2129
2130 void
2131 empathy_launch_program (const gchar *dir,
2132     const gchar *name,
2133     const gchar *args)
2134 {
2135   GdkDisplay *display;
2136   GError *error = NULL;
2137   gchar *path, *cmd;
2138   GAppInfo *app_info;
2139   GdkAppLaunchContext *context = NULL;
2140
2141   /* Try to run from source directory if possible */
2142   path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
2143       name, NULL);
2144
2145   if (!g_file_test (path, G_FILE_TEST_EXISTS))
2146     {
2147       g_free (path);
2148       path = g_build_filename (dir, name, NULL);
2149     }
2150
2151   if (args != NULL)
2152     cmd = g_strconcat (path, " ", args, NULL);
2153   else
2154     cmd = g_strdup (path);
2155
2156   app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
2157   if (app_info == NULL)
2158     {
2159       DEBUG ("Failed to create app info: %s", error->message);
2160       g_error_free (error);
2161       goto out;
2162     }
2163
2164   display = gdk_display_get_default ();
2165   context = gdk_display_get_app_launch_context (display);
2166
2167   if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
2168       &error))
2169     {
2170       g_warning ("Failed to launch %s: %s", name, error->message);
2171       g_error_free (error);
2172       goto out;
2173     }
2174
2175 out:
2176   tp_clear_object (&app_info);
2177   tp_clear_object (&context);
2178   g_free (path);
2179   g_free (cmd);
2180 }
2181
2182 /* Most of the workspace manipulation code has been copied from libwnck
2183  * Copyright (C) 2001 Havoc Pennington
2184  * Copyright (C) 2005-2007 Vincent Untz
2185  */
2186 static void
2187 _wnck_activate_workspace (Screen *screen,
2188     int new_active_space,
2189     Time timestamp)
2190 {
2191   Display *display;
2192   Window   root;
2193   XEvent   xev;
2194
2195   display = DisplayOfScreen (screen);
2196   root = RootWindowOfScreen (screen);
2197
2198   xev.xclient.type = ClientMessage;
2199   xev.xclient.serial = 0;
2200   xev.xclient.send_event = True;
2201   xev.xclient.display = display;
2202   xev.xclient.window = root;
2203   xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_CURRENT_DESKTOP");
2204   xev.xclient.format = 32;
2205   xev.xclient.data.l[0] = new_active_space;
2206   xev.xclient.data.l[1] = timestamp;
2207   xev.xclient.data.l[2] = 0;
2208   xev.xclient.data.l[3] = 0;
2209   xev.xclient.data.l[4] = 0;
2210
2211   gdk_error_trap_push ();
2212   XSendEvent (display, root, False,
2213       SubstructureRedirectMask | SubstructureNotifyMask,
2214       &xev);
2215   XSync (display, False);
2216   gdk_error_trap_pop_ignored ();
2217 }
2218
2219 static gboolean
2220 _wnck_get_cardinal (Screen *screen,
2221     Window xwindow,
2222     Atom atom,
2223     int *val)
2224 {
2225   Display *display;
2226   Atom type;
2227   int format;
2228   gulong nitems;
2229   gulong bytes_after;
2230   gulong *num;
2231   int err, result;
2232
2233   display = DisplayOfScreen (screen);
2234
2235   *val = 0;
2236
2237   gdk_error_trap_push ();
2238   type = None;
2239   result = XGetWindowProperty (display, xwindow, atom,
2240       0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems,
2241       &bytes_after, (void *) &num);
2242   err = gdk_error_trap_pop ();
2243   if (err != Success ||
2244       result != Success)
2245     return FALSE;
2246
2247   if (type != XA_CARDINAL)
2248     {
2249       XFree (num);
2250       return FALSE;
2251     }
2252
2253   *val = *num;
2254
2255   XFree (num);
2256
2257   return TRUE;
2258 }
2259
2260 static int
2261 window_get_workspace (Screen *xscreen,
2262     Window win)
2263 {
2264   int number;
2265
2266   if (!_wnck_get_cardinal (xscreen, win,
2267         gdk_x11_get_xatom_by_name ("_NET_WM_DESKTOP"), &number))
2268     return -1;
2269
2270   return number;
2271 }
2272
2273 /* Ask X to move to the desktop on which @window currently is
2274  * and the present @window. */
2275 void
2276 empathy_move_to_window_desktop (GtkWindow *window,
2277     guint32 timestamp)
2278 {
2279   GdkScreen *screen;
2280   Screen *xscreen;
2281   GdkWindow *gdk_window;
2282   int workspace;
2283
2284   screen = gtk_window_get_screen (window);
2285   xscreen = gdk_x11_screen_get_xscreen (screen);
2286   gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
2287
2288   workspace = window_get_workspace (xscreen,
2289       gdk_x11_window_get_xid (gdk_window));
2290   if (workspace == -1)
2291     goto out;
2292
2293   _wnck_activate_workspace (xscreen, workspace, timestamp);
2294
2295 out:
2296   gtk_window_present_with_time (window, timestamp);
2297 }