]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-ui-utils.c
factor out empathy_individual_match_words()
[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 (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
495                 g_warning ("Couldn't write avatar image:%p with "
496                            "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
497                            avatar->data, avatar->len, error->message);
498                 g_error_free (error);
499                 return NULL;
500         }
501
502         gdk_pixbuf_loader_close (loader, NULL);
503         pixbuf = avatar_pixbuf_from_loader (loader);
504
505         g_object_unref (loader);
506
507         return pixbuf;
508 }
509
510 GdkPixbuf *
511 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
512                                           gint           width,
513                                           gint           height)
514 {
515         EmpathyAvatar *avatar;
516
517         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
518
519         avatar = empathy_contact_get_avatar (contact);
520
521         return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
522 }
523
524 typedef struct {
525         FolksIndividual *individual;
526         GSimpleAsyncResult *result;
527         guint width;
528         guint height;
529 } PixbufAvatarFromIndividualClosure;
530
531 static PixbufAvatarFromIndividualClosure *
532 pixbuf_avatar_from_individual_closure_new (FolksIndividual    *individual,
533                                            GSimpleAsyncResult *result,
534                                            gint                width,
535                                            gint                height)
536 {
537         PixbufAvatarFromIndividualClosure *closure;
538
539         g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
540         g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
541
542         closure = g_new0 (PixbufAvatarFromIndividualClosure, 1);
543         closure->individual = g_object_ref (individual);
544         closure->result = g_object_ref (result);
545         closure->width = width;
546         closure->height = height;
547
548         return closure;
549 }
550
551 static void
552 pixbuf_avatar_from_individual_closure_free (
553                 PixbufAvatarFromIndividualClosure *closure)
554 {
555         g_object_unref (closure->individual);
556         g_object_unref (closure->result);
557         g_free (closure);
558 }
559
560 static void
561 avatar_file_load_contents_cb (GObject      *object,
562                               GAsyncResult *result,
563                               gpointer      user_data)
564 {
565         GFile *file = G_FILE (object);
566         PixbufAvatarFromIndividualClosure *closure = user_data;
567         char *data = NULL;
568         gsize data_size;
569         struct SizeData size_data;
570         GError *error = NULL;
571         GdkPixbufLoader *loader = NULL;
572
573         if (!g_file_load_contents_finish (file, result, &data, &data_size,
574                                 NULL, &error)) {
575                 DEBUG ("failed to load avatar from file: %s",
576                                 error->message);
577                 g_simple_async_result_set_from_error (closure->result, error);
578                 goto out;
579         }
580
581         size_data.width = closure->width;
582         size_data.height = closure->height;
583         size_data.preserve_aspect_ratio = TRUE;
584
585         loader = gdk_pixbuf_loader_new ();
586
587         g_signal_connect (loader, "size-prepared",
588                           G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
589                           &size_data);
590
591         if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size,
592                                 &error)) {
593                 DEBUG ("Failed to write to pixbuf loader: %s",
594                         error ? error->message : "No error given");
595                 g_simple_async_result_set_from_error (closure->result, error);
596                 goto out;
597         }
598         if (!gdk_pixbuf_loader_close (loader, &error)) {
599                 DEBUG ("Failed to close pixbuf loader: %s",
600                         error ? error->message : "No error given");
601                 g_simple_async_result_set_from_error (closure->result, error);
602                 goto out;
603         }
604
605         g_simple_async_result_set_op_res_gpointer (closure->result,
606                         avatar_pixbuf_from_loader (loader), g_object_unref);
607
608 out:
609         g_simple_async_result_complete (closure->result);
610
611         g_clear_error (&error);
612         g_free (data);
613         tp_clear_object (&loader);
614         pixbuf_avatar_from_individual_closure_free (closure);
615 }
616
617 void
618 empathy_pixbuf_avatar_from_individual_scaled_async (
619                 FolksIndividual     *individual,
620                 gint                 width,
621                 gint                 height,
622                 GCancellable        *cancellable,
623                 GAsyncReadyCallback  callback,
624                 gpointer             user_data)
625 {
626         GFile *avatar_file;
627         GSimpleAsyncResult *result;
628         PixbufAvatarFromIndividualClosure *closure;
629
630         result = g_simple_async_result_new (G_OBJECT (individual),
631                         callback, user_data,
632                         empathy_pixbuf_avatar_from_individual_scaled_async);
633
634         avatar_file =
635                 folks_avatar_details_get_avatar (FOLKS_AVATAR_DETAILS (individual));
636         if (avatar_file == NULL)
637                 goto out;
638
639         closure = pixbuf_avatar_from_individual_closure_new (individual, result,
640                                                              width, height);
641         if (closure == NULL)
642                 goto out;
643
644         g_file_load_contents_async (avatar_file, cancellable,
645                         avatar_file_load_contents_cb, closure);
646
647         g_object_unref (result);
648
649         return;
650
651 out:
652         g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
653         g_simple_async_result_complete (result);
654         g_object_unref (result);
655 }
656
657 /* Return a ref on the GdkPixbuf */
658 GdkPixbuf *
659 empathy_pixbuf_avatar_from_individual_scaled_finish (
660                 FolksIndividual *individual,
661                 GAsyncResult *result,
662                 GError **error)
663 {
664         GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
665         gboolean result_valid;
666         GdkPixbuf *pixbuf;
667
668         g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
669         g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
670
671         if (g_simple_async_result_propagate_error (simple, error))
672                 return NULL;
673
674         result_valid = g_simple_async_result_is_valid (result,
675                         G_OBJECT (individual),
676                         empathy_pixbuf_avatar_from_individual_scaled_async);
677         g_return_val_if_fail (result_valid, NULL);
678
679         pixbuf = g_simple_async_result_get_op_res_gpointer (simple);
680         return pixbuf != NULL ? g_object_ref (pixbuf) : NULL;
681 }
682
683 GdkPixbuf *
684 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
685                                    gboolean       show_protocol)
686 {
687         const gchar *icon_name;
688
689         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
690
691         icon_name = empathy_icon_name_for_contact (contact);
692
693         if (icon_name == NULL) {
694                 return NULL;
695         }
696         return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
697             icon_name,
698             show_protocol);
699 }
700
701 GdkPixbuf *
702 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
703                                           const gchar    *icon_name,
704                                           gboolean       show_protocol)
705 {
706         GdkPixbuf *pix_status;
707         GdkPixbuf *pix_protocol;
708         gchar     *icon_filename;
709         gint       height, width;
710         gint       numerator, denominator;
711
712         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
713                         (show_protocol == FALSE), NULL);
714         g_return_val_if_fail (icon_name != NULL, NULL);
715
716         numerator = 3;
717         denominator = 4;
718
719         icon_filename = empathy_filename_from_icon_name (icon_name,
720                                                          GTK_ICON_SIZE_MENU);
721         if (icon_filename == NULL) {
722                 DEBUG ("icon name: %s could not be found\n", icon_name);
723                 return NULL;
724         }
725
726         pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
727
728         if (pix_status == NULL) {
729                 DEBUG ("Could not open icon %s\n", icon_filename);
730                 g_free (icon_filename);
731                 return NULL;
732         }
733
734         g_free (icon_filename);
735
736         if (!show_protocol)
737                 return pix_status;
738
739         height = gdk_pixbuf_get_height (pix_status);
740         width = gdk_pixbuf_get_width (pix_status);
741
742         pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
743                                                                     width * numerator / denominator,
744                                                                     height * numerator / denominator);
745
746         if (pix_protocol == NULL) {
747                 return pix_status;
748         }
749         gdk_pixbuf_composite (pix_protocol, pix_status,
750             0, height - height * numerator / denominator,
751             width * numerator / denominator, height * numerator / denominator,
752             0, height - height * numerator / denominator,
753             1, 1,
754             GDK_INTERP_BILINEAR, 255);
755
756         g_object_unref (pix_protocol);
757
758         return pix_status;
759 }
760
761 GdkPixbuf *
762 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
763                                           gint           width,
764                                           gint           height)
765 {
766         TpAccount *account;
767         gchar     *filename;
768         GdkPixbuf *pixbuf = NULL;
769
770         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
771
772         account = empathy_contact_get_account (contact);
773         filename = empathy_filename_from_icon_name (tp_account_get_icon_name (account),
774                                                     GTK_ICON_SIZE_MENU);
775         if (filename != NULL) {
776                 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
777                 g_free (filename);
778         }
779
780         return pixbuf;
781 }
782
783 GdkPixbuf *
784 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
785 {
786         gint      width, height;
787         gdouble   factor;
788
789         width = gdk_pixbuf_get_width (pixbuf);
790         height = gdk_pixbuf_get_height (pixbuf);
791
792         if (width > 0 && (width > max_size || height > max_size)) {
793                 factor = (gdouble) max_size / MAX (width, height);
794
795                 width = width * factor;
796                 height = height * factor;
797
798                 return gdk_pixbuf_scale_simple (pixbuf,
799                                                 width, height,
800                                                 GDK_INTERP_HYPER);
801         }
802
803         return g_object_ref (pixbuf);
804 }
805
806 GdkPixbuf *
807 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
808                                      gint size)
809 {
810         GtkIconTheme *theme;
811         GdkPixbuf *pixbuf;
812         GError *error = NULL;
813
814         if (!icon_name) {
815                 return NULL;
816         }
817
818         theme = gtk_icon_theme_get_default ();
819
820         pixbuf = gtk_icon_theme_load_icon (theme,
821                                            icon_name,
822                                            size,
823                                            0,
824                                            &error);
825         if (error) {
826                 DEBUG ("Error loading icon: %s", error->message);
827                 g_clear_error (&error);
828         }
829
830         return pixbuf;
831 }
832
833 GdkPixbuf *
834 empathy_pixbuf_from_icon_name (const gchar *icon_name,
835                                GtkIconSize  icon_size)
836 {
837         gint  w, h;
838         gint  size = 48;
839
840         if (!icon_name) {
841                 return NULL;
842         }
843
844         if (gtk_icon_size_lookup (icon_size, &w, &h)) {
845                 size = (w + h) / 2;
846         }
847
848         return empathy_pixbuf_from_icon_name_sized (icon_name, size);
849 }
850
851 gchar *
852 empathy_filename_from_icon_name (const gchar *icon_name,
853                                  GtkIconSize  icon_size)
854 {
855         GtkIconTheme *icon_theme;
856         GtkIconInfo  *icon_info;
857         gint          w, h;
858         gint          size = 48;
859         gchar        *ret;
860
861         icon_theme = gtk_icon_theme_get_default ();
862
863         if (gtk_icon_size_lookup (icon_size, &w, &h)) {
864                 size = (w + h) / 2;
865         }
866
867         icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
868         ret = g_strdup (gtk_icon_info_get_filename (icon_info));
869         gtk_icon_info_free (icon_info);
870
871         return ret;
872 }
873
874 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
875  * that to make it easier to apply changes from the original code.
876  */
877 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
878
879 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
880  * decomposable character it consumes the decomposition length from the given
881  * offset.  So it's useful when the offset was calculated for the normalized
882  * version of str, but we need a pointer to str itself. */
883 static const gchar *
884 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
885 {
886         gchar *casefold, *normal;
887         const gchar *p, *q;
888
889         p = str;
890         while (offset > 0)
891         {
892                 q = g_utf8_next_char (p);
893                 casefold = g_utf8_casefold (p, q - p);
894                 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
895                 offset -= g_utf8_strlen (normal, -1);
896                 g_free (casefold);
897                 g_free (normal);
898                 p = q;
899         }
900         return p;
901 }
902
903 static const gchar *
904 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
905 {
906         gsize needle_len;
907         gsize haystack_len;
908         const gchar *ret = NULL;
909         gchar *p;
910         gchar *casefold;
911         gchar *caseless_haystack;
912         gint i;
913
914         g_return_val_if_fail (haystack != NULL, NULL);
915         g_return_val_if_fail (needle != NULL, NULL);
916
917         casefold = g_utf8_casefold (haystack, -1);
918         caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
919         g_free (casefold);
920
921         needle_len = g_utf8_strlen (needle, -1);
922         haystack_len = g_utf8_strlen (caseless_haystack, -1);
923
924         if (needle_len == 0)
925         {
926                 ret = (gchar *) haystack;
927                 goto finally_1;
928         }
929
930         if (haystack_len < needle_len)
931         {
932                 ret = NULL;
933                 goto finally_1;
934         }
935
936         p = (gchar *) caseless_haystack;
937         needle_len = strlen (needle);
938         i = 0;
939
940         while (*p)
941         {
942                 if ((strncmp (p, needle, needle_len) == 0))
943                 {
944                         ret = pointer_from_offset_skipping_decomp (haystack, i);
945                         goto finally_1;
946                 }
947
948                 p = g_utf8_next_char (p);
949                 i++;
950         }
951
952 finally_1:
953         g_free (caseless_haystack);
954
955         return ret;
956 }
957
958 static gboolean
959 g_utf8_caselessnmatch (const char *s1, const char *s2,
960                        gssize n1, gssize n2)
961 {
962         gchar *casefold;
963         gchar *normalized_s1;
964         gchar *normalized_s2;
965         gint len_s1;
966         gint len_s2;
967         gboolean ret = FALSE;
968
969         g_return_val_if_fail (s1 != NULL, FALSE);
970         g_return_val_if_fail (s2 != NULL, FALSE);
971         g_return_val_if_fail (n1 > 0, FALSE);
972         g_return_val_if_fail (n2 > 0, FALSE);
973
974         casefold = g_utf8_casefold (s1, n1);
975         normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
976         g_free (casefold);
977
978         casefold = g_utf8_casefold (s2, n2);
979         normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
980         g_free (casefold);
981
982         len_s1 = strlen (normalized_s1);
983         len_s2 = strlen (normalized_s2);
984
985         if (len_s1 < len_s2)
986                 goto finally_2;
987
988         ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
989
990 finally_2:
991         g_free (normalized_s1);
992         g_free (normalized_s2);
993
994         return ret;
995 }
996
997 static void
998 forward_chars_with_skipping (GtkTextIter *iter,
999                              gint         count,
1000                              gboolean     skip_invisible,
1001                              gboolean     skip_nontext,
1002                              gboolean     skip_decomp)
1003 {
1004         gint i;
1005
1006         g_return_if_fail (count >= 0);
1007
1008         i = count;
1009
1010         while (i > 0)
1011         {
1012                 gboolean ignored = FALSE;
1013
1014                 /* minimal workaround to avoid the infinite loop of bug #168247.
1015                  * It doesn't fix the problemjust the symptom...
1016                  */
1017                 if (gtk_text_iter_is_end (iter))
1018                         return;
1019
1020                 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
1021                         ignored = TRUE;
1022
1023                 if (!ignored && skip_invisible &&
1024                     /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
1025                         ignored = TRUE;
1026
1027                 if (!ignored && skip_decomp)
1028                 {
1029                         /* being UTF8 correct sucks; this accounts for extra
1030                            offsets coming from canonical decompositions of
1031                            UTF8 characters (e.g. accented characters) which
1032                            g_utf8_normalize () performs */
1033                         gchar *normal;
1034                         gchar buffer[6];
1035                         gint buffer_len;
1036
1037                         buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
1038                         normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
1039                         i -= (g_utf8_strlen (normal, -1) - 1);
1040                         g_free (normal);
1041                 }
1042
1043                 gtk_text_iter_forward_char (iter);
1044
1045                 if (!ignored)
1046                         --i;
1047         }
1048 }
1049
1050 static gboolean
1051 lines_match (const GtkTextIter *start,
1052              const gchar      **lines,
1053              gboolean           visible_only,
1054              gboolean           slice,
1055              GtkTextIter       *match_start,
1056              GtkTextIter       *match_end)
1057 {
1058         GtkTextIter next;
1059         gchar *line_text;
1060         const gchar *found;
1061         gint offset;
1062
1063         if (*lines == NULL || **lines == '\0')
1064         {
1065                 if (match_start)
1066                         *match_start = *start;
1067                 if (match_end)
1068                         *match_end = *start;
1069                 return TRUE;
1070         }
1071
1072         next = *start;
1073         gtk_text_iter_forward_line (&next);
1074
1075         /* No more text in buffer, but *lines is nonempty */
1076         if (gtk_text_iter_equal (start, &next))
1077                 return FALSE;
1078
1079         if (slice)
1080         {
1081                 if (visible_only)
1082                         line_text = gtk_text_iter_get_visible_slice (start, &next);
1083                 else
1084                         line_text = gtk_text_iter_get_slice (start, &next);
1085         }
1086         else
1087         {
1088                 if (visible_only)
1089                         line_text = gtk_text_iter_get_visible_text (start, &next);
1090                 else
1091                         line_text = gtk_text_iter_get_text (start, &next);
1092         }
1093
1094         if (match_start) /* if this is the first line we're matching */
1095         {
1096                 found = g_utf8_strcasestr (line_text, *lines);
1097         }
1098         else
1099         {
1100                 /* If it's not the first line, we have to match from the
1101                  * start of the line.
1102                  */
1103                 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1104                                            strlen (*lines)))
1105                         found = line_text;
1106                 else
1107                         found = NULL;
1108         }
1109
1110         if (found == NULL)
1111         {
1112                 g_free (line_text);
1113                 return FALSE;
1114         }
1115
1116         /* Get offset to start of search string */
1117         offset = g_utf8_strlen (line_text, found - line_text);
1118
1119         next = *start;
1120
1121         /* If match start needs to be returned, set it to the
1122          * start of the search string.
1123          */
1124         forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1125         if (match_start)
1126         {
1127                 *match_start = next;
1128         }
1129
1130         /* Go to end of search string */
1131         forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1132
1133         g_free (line_text);
1134
1135         ++lines;
1136
1137         if (match_end)
1138                 *match_end = next;
1139
1140         /* pass NULL for match_start, since we don't need to find the
1141          * start again.
1142          */
1143         return lines_match (&next, lines, visible_only, slice, NULL, match_end);
1144 }
1145
1146 /* strsplit () that retains the delimiter as part of the string. */
1147 static gchar **
1148 strbreakup (const char *string,
1149             const char *delimiter,
1150             gint        max_tokens)
1151 {
1152         GSList *string_list = NULL, *slist;
1153         gchar **str_array, *s, *casefold, *new_string;
1154         guint i, n = 1;
1155
1156         g_return_val_if_fail (string != NULL, NULL);
1157         g_return_val_if_fail (delimiter != NULL, NULL);
1158
1159         if (max_tokens < 1)
1160                 max_tokens = G_MAXINT;
1161
1162         s = strstr (string, delimiter);
1163         if (s)
1164         {
1165                 guint delimiter_len = strlen (delimiter);
1166
1167                 do
1168                 {
1169                         guint len;
1170
1171                         len = s - string + delimiter_len;
1172                         new_string = g_new (gchar, len + 1);
1173                         strncpy (new_string, string, len);
1174                         new_string[len] = 0;
1175                         casefold = g_utf8_casefold (new_string, -1);
1176                         g_free (new_string);
1177                         new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1178                         g_free (casefold);
1179                         string_list = g_slist_prepend (string_list, new_string);
1180                         n++;
1181                         string = s + delimiter_len;
1182                         s = strstr (string, delimiter);
1183                 } while (--max_tokens && s);
1184         }
1185
1186         if (*string)
1187         {
1188                 n++;
1189                 casefold = g_utf8_casefold (string, -1);
1190                 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1191                 g_free (casefold);
1192                 string_list = g_slist_prepend (string_list, new_string);
1193         }
1194
1195         str_array = g_new (gchar*, n);
1196
1197         i = n - 1;
1198
1199         str_array[i--] = NULL;
1200         for (slist = string_list; slist; slist = slist->next)
1201                 str_array[i--] = slist->data;
1202
1203         g_slist_free (string_list);
1204
1205         return str_array;
1206 }
1207
1208 gboolean
1209 empathy_text_iter_forward_search (const GtkTextIter   *iter,
1210                                  const gchar         *str,
1211                                  GtkTextIter         *match_start,
1212                                  GtkTextIter         *match_end,
1213                                  const GtkTextIter   *limit)
1214 {
1215         gchar **lines = NULL;
1216         GtkTextIter match;
1217         gboolean retval = FALSE;
1218         GtkTextIter search;
1219         gboolean visible_only;
1220         gboolean slice;
1221
1222         g_return_val_if_fail (iter != NULL, FALSE);
1223         g_return_val_if_fail (str != NULL, FALSE);
1224
1225         if (limit && gtk_text_iter_compare (iter, limit) >= 0)
1226                 return FALSE;
1227
1228         if (*str == '\0') {
1229                 /* If we can move one char, return the empty string there */
1230                 match = *iter;
1231
1232                 if (gtk_text_iter_forward_char (&match)) {
1233                         if (limit && gtk_text_iter_equal (&match, limit)) {
1234                                 return FALSE;
1235                         }
1236
1237                         if (match_start) {
1238                                 *match_start = match;
1239                         }
1240                         if (match_end) {
1241                                 *match_end = match;
1242                         }
1243                         return TRUE;
1244                 } else {
1245                         return FALSE;
1246                 }
1247         }
1248
1249         visible_only = TRUE;
1250         slice = FALSE;
1251
1252         /* locate all lines */
1253         lines = strbreakup (str, "\n", -1);
1254
1255         search = *iter;
1256
1257         do {
1258                 /* This loop has an inefficient worst-case, where
1259                  * gtk_text_iter_get_text () is called repeatedly on
1260                  * a single line.
1261                  */
1262                 GtkTextIter end;
1263
1264                 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1265                         break;
1266                 }
1267
1268                 if (lines_match (&search, (const gchar**)lines,
1269                                  visible_only, slice, &match, &end)) {
1270                         if (limit == NULL ||
1271                             (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1272                                 retval = TRUE;
1273
1274                                 if (match_start) {
1275                                         *match_start = match;
1276                                 }
1277                                 if (match_end) {
1278                                         *match_end = end;
1279                                 }
1280                         }
1281                         break;
1282                 }
1283         } while (gtk_text_iter_forward_line (&search));
1284
1285         g_strfreev ((gchar **) lines);
1286
1287         return retval;
1288 }
1289
1290 static const gchar *
1291 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1292 {
1293         gsize needle_len;
1294         gsize haystack_len;
1295         const gchar *ret = NULL;
1296         gchar *p;
1297         gchar *casefold;
1298         gchar *caseless_haystack;
1299         gint i;
1300
1301         g_return_val_if_fail (haystack != NULL, NULL);
1302         g_return_val_if_fail (needle != NULL, NULL);
1303
1304         casefold = g_utf8_casefold (haystack, -1);
1305         caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1306         g_free (casefold);
1307
1308         needle_len = g_utf8_strlen (needle, -1);
1309         haystack_len = g_utf8_strlen (caseless_haystack, -1);
1310
1311         if (needle_len == 0)
1312         {
1313                 ret = (gchar *) haystack;
1314                 goto finally_1;
1315         }
1316
1317         if (haystack_len < needle_len)
1318         {
1319                 ret = NULL;
1320                 goto finally_1;
1321         }
1322
1323         i = haystack_len - needle_len;
1324         p = g_utf8_offset_to_pointer (caseless_haystack, i);
1325         needle_len = strlen (needle);
1326
1327         while (p >= caseless_haystack)
1328         {
1329                 if (strncmp (p, needle, needle_len) == 0)
1330                 {
1331                         ret = pointer_from_offset_skipping_decomp (haystack, i);
1332                         goto finally_1;
1333                 }
1334
1335                 p = g_utf8_prev_char (p);
1336                 i--;
1337         }
1338
1339 finally_1:
1340         g_free (caseless_haystack);
1341
1342         return ret;
1343 }
1344
1345 static gboolean
1346 backward_lines_match (const GtkTextIter *start,
1347                       const gchar      **lines,
1348                       gboolean           visible_only,
1349                       gboolean           slice,
1350                       GtkTextIter       *match_start,
1351                       GtkTextIter       *match_end)
1352 {
1353         GtkTextIter line, next;
1354         gchar *line_text;
1355         const gchar *found;
1356         gint offset;
1357
1358         if (*lines == NULL || **lines == '\0')
1359         {
1360                 if (match_start)
1361                         *match_start = *start;
1362                 if (match_end)
1363                         *match_end = *start;
1364                 return TRUE;
1365         }
1366
1367         line = next = *start;
1368         if (gtk_text_iter_get_line_offset (&next) == 0)
1369         {
1370                 if (!gtk_text_iter_backward_line (&next))
1371                         return FALSE;
1372         }
1373         else
1374                 gtk_text_iter_set_line_offset (&next, 0);
1375
1376         if (slice)
1377         {
1378                 if (visible_only)
1379                         line_text = gtk_text_iter_get_visible_slice (&next, &line);
1380                 else
1381                         line_text = gtk_text_iter_get_slice (&next, &line);
1382         }
1383         else
1384         {
1385                 if (visible_only)
1386                         line_text = gtk_text_iter_get_visible_text (&next, &line);
1387                 else
1388                         line_text = gtk_text_iter_get_text (&next, &line);
1389         }
1390
1391         if (match_start) /* if this is the first line we're matching */
1392         {
1393                 found = g_utf8_strrcasestr (line_text, *lines);
1394         }
1395         else
1396         {
1397                 /* If it's not the first line, we have to match from the
1398                  * start of the line.
1399                  */
1400                 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1401                                            strlen (*lines)))
1402                         found = line_text;
1403                 else
1404                         found = NULL;
1405         }
1406
1407         if (found == NULL)
1408         {
1409                 g_free (line_text);
1410                 return FALSE;
1411         }
1412
1413         /* Get offset to start of search string */
1414         offset = g_utf8_strlen (line_text, found - line_text);
1415
1416         forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1417
1418         /* If match start needs to be returned, set it to the
1419          * start of the search string.
1420          */
1421         if (match_start)
1422         {
1423                 *match_start = next;
1424         }
1425
1426         /* Go to end of search string */
1427         forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1428
1429         g_free (line_text);
1430
1431         ++lines;
1432
1433         if (match_end)
1434                 *match_end = next;
1435
1436         /* try to match the rest of the lines forward, passing NULL
1437          * for match_start so lines_match will try to match the entire
1438          * line */
1439         return lines_match (&next, lines, visible_only,
1440                             slice, NULL, match_end);
1441 }
1442
1443 gboolean
1444 empathy_text_iter_backward_search (const GtkTextIter   *iter,
1445                                   const gchar         *str,
1446                                   GtkTextIter         *match_start,
1447                                   GtkTextIter         *match_end,
1448                                   const GtkTextIter   *limit)
1449 {
1450         gchar **lines = NULL;
1451         GtkTextIter match;
1452         gboolean retval = FALSE;
1453         GtkTextIter search;
1454         gboolean visible_only;
1455         gboolean slice;
1456
1457         g_return_val_if_fail (iter != NULL, FALSE);
1458         g_return_val_if_fail (str != NULL, FALSE);
1459
1460         if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1461                 return FALSE;
1462
1463         if (*str == '\0')
1464         {
1465                 /* If we can move one char, return the empty string there */
1466                 match = *iter;
1467
1468                 if (gtk_text_iter_backward_char (&match))
1469                 {
1470                         if (limit && gtk_text_iter_equal (&match, limit))
1471                                 return FALSE;
1472
1473                         if (match_start)
1474                                 *match_start = match;
1475                         if (match_end)
1476                                 *match_end = match;
1477                         return TRUE;
1478                 }
1479                 else
1480                 {
1481                         return FALSE;
1482                 }
1483         }
1484
1485         visible_only = TRUE;
1486         slice = TRUE;
1487
1488         /* locate all lines */
1489         lines = strbreakup (str, "\n", -1);
1490
1491         search = *iter;
1492
1493         while (TRUE)
1494         {
1495                 /* This loop has an inefficient worst-case, where
1496                  * gtk_text_iter_get_text () is called repeatedly on
1497                  * a single line.
1498                  */
1499                 GtkTextIter end;
1500
1501                 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1502                         break;
1503
1504                 if (backward_lines_match (&search, (const gchar**)lines,
1505                                           visible_only, slice, &match, &end))
1506                 {
1507                         if (limit == NULL || (limit &&
1508                                               gtk_text_iter_compare (&end, limit) > 0))
1509                         {
1510                                 retval = TRUE;
1511
1512                                 if (match_start)
1513                                         *match_start = match;
1514                                 if (match_end)
1515                                         *match_end = end;
1516                         }
1517                         break;
1518                 }
1519
1520                 if (gtk_text_iter_get_line_offset (&search) == 0)
1521                 {
1522                         if (!gtk_text_iter_backward_line (&search))
1523                                 break;
1524                 }
1525                 else
1526                 {
1527                         gtk_text_iter_set_line_offset (&search, 0);
1528                 }
1529         }
1530
1531         g_strfreev ((gchar **) lines);
1532
1533         return retval;
1534 }
1535
1536 /* Takes care of moving the window to the current workspace. */
1537 void
1538 empathy_window_present_with_time (GtkWindow *window,
1539                         guint32 timestamp)
1540 {
1541         GdkWindow *gdk_window;
1542
1543         g_return_if_fail (GTK_IS_WINDOW (window));
1544
1545         /* Move the window to the current workspace before trying to show it.
1546          * This is the behaviour people expect when clicking on the statusbar icon. */
1547         gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1548         if (gdk_window) {
1549                 gint x, y;
1550                 gint w, h;
1551
1552                 /* Has no effect if the WM has viewports, like compiz */
1553                 gdk_x11_window_move_to_current_desktop (gdk_window);
1554
1555                 /* If window is still off-screen, hide it to force it to
1556                  * reposition on the current workspace. */
1557                 gtk_window_get_position (window, &x, &y);
1558                 gtk_window_get_size (window, &w, &h);
1559                 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1560                         gtk_widget_hide (GTK_WIDGET (window));
1561         }
1562
1563         if (timestamp == GDK_CURRENT_TIME)
1564                 gtk_window_present (window);
1565         else
1566                 gtk_window_present_with_time (window, timestamp);
1567 }
1568
1569 void
1570 empathy_window_present (GtkWindow *window)
1571 {
1572   empathy_window_present_with_time (window, gtk_get_current_event_time ());
1573 }
1574
1575 GtkWindow *
1576 empathy_get_toplevel_window (GtkWidget *widget)
1577 {
1578         GtkWidget *toplevel;
1579
1580         g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1581
1582         toplevel = gtk_widget_get_toplevel (widget);
1583         if (GTK_IS_WINDOW (toplevel) &&
1584             gtk_widget_is_toplevel (toplevel)) {
1585                 return GTK_WINDOW (toplevel);
1586         }
1587
1588         return NULL;
1589 }
1590
1591 /** empathy_make_absolute_url_len:
1592  * @url: an url
1593  * @len: a length
1594  *
1595  * Same as #empathy_make_absolute_url but for a limited string length
1596  */
1597 gchar *
1598 empathy_make_absolute_url_len (const gchar *url,
1599                                guint len)
1600 {
1601         g_return_val_if_fail (url != NULL, NULL);
1602
1603         if (g_str_has_prefix (url, "ghelp:") ||
1604             g_str_has_prefix (url, "mailto:") ||
1605             strstr (url, ":/")) {
1606                 return g_strndup (url, len);
1607         }
1608
1609         if (strstr (url, "@")) {
1610                 return g_strdup_printf ("mailto:%.*s", len, url);
1611         }
1612
1613         return g_strdup_printf ("http://%.*s", len, url);
1614 }
1615
1616 /** empathy_make_absolute_url:
1617  * @url: an url
1618  *
1619  * The URL opening code can't handle schemeless strings, so we try to be
1620  * smart and add http if there is no scheme or doesn't look like a mail
1621  * address. This should work in most cases, and let us click on strings
1622  * like "www.gnome.org".
1623  *
1624  * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1625  * g_free when your are done with it
1626  */
1627 gchar *
1628 empathy_make_absolute_url (const gchar *url)
1629 {
1630         return empathy_make_absolute_url_len (url, strlen (url));
1631 }
1632
1633 void
1634 empathy_url_show (GtkWidget *parent,
1635                   const char *url)
1636 {
1637         gchar  *real_url;
1638         GError *error = NULL;
1639
1640         g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1641         g_return_if_fail (url != NULL);
1642
1643         real_url = empathy_make_absolute_url (url);
1644
1645         gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1646                       gtk_get_current_event_time (), &error);
1647
1648         if (error) {
1649                 GtkWidget *dialog;
1650
1651                 dialog = gtk_message_dialog_new (NULL, 0,
1652                                                  GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1653                                                  _("Unable to open URI"));
1654                 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1655                                                           "%s", error->message);
1656
1657                 g_signal_connect (dialog, "response",
1658                                   G_CALLBACK (gtk_widget_destroy),
1659                                   NULL);
1660                 gtk_window_present (GTK_WINDOW (dialog));
1661
1662                 g_clear_error (&error);
1663         }
1664
1665         g_free (real_url);
1666 }
1667
1668 void
1669 empathy_send_file (EmpathyContact *contact, GFile *file)
1670 {
1671         EmpathyFTFactory *factory;
1672         GtkRecentManager *manager;
1673         gchar *uri;
1674
1675         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1676         g_return_if_fail (G_IS_FILE (file));
1677
1678         factory = empathy_ft_factory_dup_singleton ();
1679
1680         empathy_ft_factory_new_transfer_outgoing (factory, contact, file);
1681
1682         uri = g_file_get_uri (file);
1683         manager = gtk_recent_manager_get_default ();
1684         gtk_recent_manager_add_item (manager, uri);
1685         g_free (uri);
1686
1687         g_object_unref (factory);
1688 }
1689
1690 void
1691 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1692 {
1693         const gchar *nl;
1694         GFile *file;
1695
1696         /* Only handle a single file for now.  It would be wicked cool to be
1697            able to do multiple files, offering to zip them or whatever like
1698            nautilus-sendto does.  Note that text/uri-list is defined to have
1699            each line terminated by \r\n, but we can be tolerant of applications
1700            that only use \n or don't terminate single-line entries.
1701         */
1702         nl = strstr (uri_list, "\r\n");
1703         if (!nl) {
1704                 nl = strchr (uri_list, '\n');
1705         }
1706         if (nl) {
1707                 gchar *uri = g_strndup (uri_list, nl - uri_list);
1708                 file = g_file_new_for_uri (uri);
1709                 g_free (uri);
1710         }
1711         else {
1712                 file = g_file_new_for_uri (uri_list);
1713         }
1714
1715         empathy_send_file (contact, file);
1716
1717         g_object_unref (file);
1718 }
1719
1720 static void
1721 file_manager_send_file_response_cb (GtkDialog      *widget,
1722                                     gint            response_id,
1723                                     EmpathyContact *contact)
1724 {
1725         GFile *file;
1726
1727         if (response_id == GTK_RESPONSE_OK) {
1728                 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1729
1730                 empathy_send_file (contact, file);
1731
1732                 g_object_unref (file);
1733         }
1734
1735         gtk_widget_destroy (GTK_WIDGET (widget));
1736 }
1737
1738 void
1739 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1740 {
1741         GtkWidget               *widget;
1742         GtkWidget               *button;
1743
1744         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1745
1746         DEBUG ("Creating selection file chooser");
1747
1748         widget = gtk_file_chooser_dialog_new (_("Select a file"),
1749                                               NULL,
1750                                               GTK_FILE_CHOOSER_ACTION_OPEN,
1751                                               GTK_STOCK_CANCEL,
1752                                               GTK_RESPONSE_CANCEL,
1753                                               NULL);
1754
1755         /* send button */
1756         button = gtk_button_new_with_mnemonic (_("_Send"));
1757         gtk_button_set_image (GTK_BUTTON (button),
1758                 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1759                                               GTK_ICON_SIZE_BUTTON));
1760         gtk_widget_show (button);
1761         gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1762                                       GTK_RESPONSE_OK);
1763         gtk_widget_set_can_default (button, TRUE);
1764         gtk_dialog_set_default_response (GTK_DIALOG (widget),
1765                                          GTK_RESPONSE_OK);
1766
1767         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1768
1769         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1770                 g_get_home_dir ());
1771
1772         g_signal_connect (widget, "response",
1773                           G_CALLBACK (file_manager_send_file_response_cb),
1774                           contact);
1775
1776         gtk_widget_show (widget);
1777 }
1778
1779 static void
1780 file_manager_receive_file_response_cb (GtkDialog *dialog,
1781                                        GtkResponseType response,
1782                                        EmpathyFTHandler *handler)
1783 {
1784         EmpathyFTFactory *factory;
1785         GFile *file;
1786
1787         if (response == GTK_RESPONSE_OK) {
1788                 GFile *parent;
1789                 GFileInfo *info;
1790                 guint64 free_space, file_size;
1791                 GError *error = NULL;
1792
1793                 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1794                 parent = g_file_get_parent (file);
1795                 info = g_file_query_filesystem_info (parent,
1796                                 G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
1797                                 NULL, &error);
1798
1799                 g_object_unref (parent);
1800
1801                 if (error != NULL) {
1802                         g_warning ("Error: %s", error->message);
1803
1804                         g_object_unref (file);
1805                         return;
1806                 }
1807
1808                 free_space = g_file_info_get_attribute_uint64 (info,
1809                                 G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
1810                 file_size = empathy_ft_handler_get_total_bytes (handler);
1811
1812                 g_object_unref (info);
1813
1814                 if (file_size > free_space) {
1815                         GtkWidget *message = gtk_message_dialog_new (
1816                                 GTK_WINDOW (dialog),
1817                                 GTK_DIALOG_MODAL,
1818                                 GTK_MESSAGE_ERROR,
1819                                 GTK_BUTTONS_CLOSE,
1820                                 _("Insufficient free space to save file"));
1821                         char *file_size_str, *free_space_str;
1822
1823                         file_size_str = g_format_size_for_display (file_size);
1824                         free_space_str = g_format_size_for_display (free_space);
1825
1826                         gtk_message_dialog_format_secondary_text (
1827                                 GTK_MESSAGE_DIALOG (message),
1828                                 _("%s of free space are required to save this "
1829                                   "file, but only %s is available. Please "
1830                                   "choose another location."),
1831                                 file_size_str, free_space_str);
1832
1833                         gtk_dialog_run (GTK_DIALOG (message));
1834
1835                         g_free (file_size_str);
1836                         g_free (free_space_str);
1837                         gtk_widget_destroy (message);
1838
1839                         g_object_unref (file);
1840
1841                         return;
1842                 }
1843
1844                 factory = empathy_ft_factory_dup_singleton ();
1845
1846                 empathy_ft_factory_set_destination_for_incoming_handler (
1847                                 factory, handler, file);
1848
1849                 g_object_unref (factory);
1850                 g_object_unref (file);
1851         } else {
1852                 /* unref the handler, as we dismissed the file chooser,
1853                  * and refused the transfer.
1854                  */
1855                 g_object_unref (handler);
1856         }
1857
1858         gtk_widget_destroy (GTK_WIDGET (dialog));
1859 }
1860
1861 void
1862 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1863 {
1864         GtkWidget *widget;
1865         const gchar *dir;
1866         EmpathyContact *contact;
1867         gchar *title;
1868
1869         contact = empathy_ft_handler_get_contact (handler);
1870         g_assert (contact != NULL);
1871
1872         title = g_strdup_printf (_("Incoming file from %s"),
1873                 empathy_contact_get_alias (contact));
1874
1875         widget = gtk_file_chooser_dialog_new (title,
1876                                               NULL,
1877                                               GTK_FILE_CHOOSER_ACTION_SAVE,
1878                                               GTK_STOCK_CANCEL,
1879                                               GTK_RESPONSE_CANCEL,
1880                                               GTK_STOCK_SAVE,
1881                                               GTK_RESPONSE_OK,
1882                                               NULL);
1883         gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1884                 empathy_ft_handler_get_filename (handler));
1885         gtk_file_chooser_set_do_overwrite_confirmation
1886                 (GTK_FILE_CHOOSER (widget), TRUE);
1887
1888         dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1889         if (dir == NULL)
1890                 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1891                 dir = g_get_home_dir ();
1892
1893         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1894
1895         g_signal_connect (widget, "response",
1896                 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1897
1898         gtk_widget_show (widget);
1899         g_free (title);
1900 }
1901
1902 void
1903 empathy_make_color_whiter (GdkRGBA *color)
1904 {
1905         const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
1906
1907         color->red = (color->red + white.red) / 2;
1908         color->green = (color->green + white.green) / 2;
1909         color->blue = (color->blue + white.blue) / 2;
1910 }
1911
1912 static void
1913 menu_deactivate_cb (GtkMenu *menu,
1914         gpointer user_data)
1915 {
1916         /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
1917         g_signal_handlers_disconnect_by_func (menu,
1918                      menu_deactivate_cb, user_data);
1919
1920         gtk_menu_detach (menu);
1921 }
1922
1923 /* Convenient function to create a GtkMenu attached to @attach_to and detach
1924  * it when the menu is not displayed any more. This is useful when creating a
1925  * context menu that we want to get rid as soon as it as been displayed. */
1926 GtkWidget *
1927 empathy_context_menu_new (GtkWidget *attach_to)
1928 {
1929         GtkWidget *menu;
1930
1931         menu = gtk_menu_new ();
1932
1933         gtk_menu_attach_to_widget (GTK_MENU (menu), attach_to, NULL);
1934
1935         /* menu is initially unowned but gtk_menu_attach_to_widget () taked its
1936          * floating ref. We can either wait that @attach_to releases its ref when
1937          * it will be destroyed (when leaving Empathy most of the time) or explicitely
1938          * detach the menu when it's not displayed any more.
1939          * We go for the latter as we don't want to keep useless menus in memory
1940          * during the whole lifetime of Empathy. */
1941         g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb), NULL);
1942
1943         return menu;
1944 }
1945
1946 gint64
1947 empathy_get_current_action_time (void)
1948 {
1949   return (tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
1950 }
1951
1952 gboolean
1953 empathy_individual_match_words (FolksIndividual *individual,
1954     GPtrArray *words)
1955 {
1956   const gchar *str;
1957   GList *personas, *l;
1958
1959   /* check alias name */
1960   str = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual));
1961
1962   if (empathy_live_search_match_words (str, words))
1963     return TRUE;
1964
1965   personas = folks_individual_get_personas (individual);
1966
1967   /* check contact id, remove the @server.com part */
1968   for (l = personas; l; l = l->next)
1969     {
1970       const gchar *p;
1971       gchar *dup_str = NULL;
1972       gboolean visible;
1973
1974       if (!empathy_folks_persona_is_interesting (FOLKS_PERSONA (l->data)))
1975         continue;
1976
1977       str = folks_persona_get_display_id (l->data);
1978       p = strstr (str, "@");
1979       if (p != NULL)
1980         str = dup_str = g_strndup (str, p - str);
1981
1982       visible = empathy_live_search_match_words (str, words);
1983       g_free (dup_str);
1984       if (visible)
1985         return TRUE;
1986     }
1987
1988   /* FIXME: Add more rules here, we could check phone numbers in
1989    * contact's vCard for example. */
1990   return FALSE;
1991 }