1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
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.
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.
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
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>
28 * Part of this file is copied from GtkSourceView (gtksourceiter.c):
36 #include <X11/Xatom.h>
38 #include <glib/gi18n-lib.h>
42 #include <telepathy-glib/util.h>
43 #include <folks/folks.h>
45 #include "empathy-ui-utils.h"
46 #include "empathy-images.h"
47 #include "empathy-smiley-manager.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
50 #include <libempathy/empathy-debug.h>
51 #include <libempathy/empathy-utils.h>
52 #include <libempathy/empathy-dispatcher.h>
53 #include <libempathy/empathy-idle.h>
54 #include <libempathy/empathy-ft-factory.h>
57 empathy_gtk_init (void)
59 static gboolean initialized = FALSE;
65 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
66 PKGDATADIR G_DIR_SEPARATOR_S "icons");
72 builder_get_file_valist (const gchar *filename,
73 const gchar *first_object,
81 DEBUG ("Loading file %s", filename);
83 gui = gtk_builder_new ();
84 gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
85 if (!gtk_builder_add_from_file (gui, filename, &error)) {
86 g_critical ("GtkBuilder Error (%s): %s",
87 filename, error->message);
88 g_clear_error (&error);
91 /* we need to iterate and set all of the pointers to NULL */
92 for (name = first_object; name;
93 name = va_arg (args, const gchar *)) {
94 object_ptr = va_arg (args, GObject**);
102 for (name = first_object; name; name = va_arg (args, const gchar *)) {
103 object_ptr = va_arg (args, GObject**);
105 *object_ptr = gtk_builder_get_object (gui, name);
108 g_warning ("File is missing object '%s'.", name);
117 empathy_builder_get_file (const gchar *filename,
118 const gchar *first_object,
124 va_start (args, first_object);
125 gui = builder_get_file_valist (filename, first_object, args);
132 empathy_builder_connect (GtkBuilder *gui,
143 va_start (args, first_object);
144 for (name = first_object; name; name = va_arg (args, const gchar *)) {
145 sig = va_arg (args, const gchar *);
146 callback = va_arg (args, GCallback);
148 object = gtk_builder_get_object (gui, name);
150 g_warning ("File is missing object '%s'.", name);
154 g_signal_connect (object, sig, callback, user_data);
161 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
164 /* On construction gui sinks the initial reference to widget. When gui
165 * is finalized it will drop its ref to widget. We take our own ref to
166 * prevent widget being finalised. The widget is forced to have a
167 * floating reference, like when it was initially unowned so that it can
168 * be used like any other GtkWidget. */
170 g_object_ref (widget);
171 g_object_force_floating (G_OBJECT (widget));
172 g_object_unref (gui);
178 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
181 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
182 return EMPATHY_IMAGE_AVAILABLE;
183 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
184 return EMPATHY_IMAGE_BUSY;
185 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
186 return EMPATHY_IMAGE_AWAY;
187 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
188 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
189 EMPATHY_IMAGE_EXT_AWAY))
190 return EMPATHY_IMAGE_EXT_AWAY;
192 /* The 'extended-away' icon is not an official one so we fallback to idle if
193 * it's not implemented */
194 return EMPATHY_IMAGE_IDLE;
195 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
196 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
197 EMPATHY_IMAGE_HIDDEN))
198 return EMPATHY_IMAGE_HIDDEN;
200 /* The 'hidden' icon is not an official one so we fallback to offline if
201 * it's not implemented */
202 return EMPATHY_IMAGE_OFFLINE;
203 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
204 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
205 return EMPATHY_IMAGE_OFFLINE;
206 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
207 return EMPATHY_IMAGE_PENDING;
208 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
216 empathy_icon_name_for_contact (EmpathyContact *contact)
218 TpConnectionPresenceType presence;
220 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
221 EMPATHY_IMAGE_OFFLINE);
223 presence = empathy_contact_get_presence (contact);
224 return empathy_icon_name_for_presence (presence);
228 empathy_icon_name_for_individual (FolksIndividual *individual)
230 FolksPresenceType folks_presence;
231 TpConnectionPresenceType presence;
233 folks_presence = folks_individual_get_presence_type (individual);
234 presence = empathy_folks_presence_type_to_tp (folks_presence);
236 return empathy_icon_name_for_presence (presence);
240 empathy_protocol_name_for_contact (EmpathyContact *contact)
244 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
246 account = empathy_contact_get_account (contact);
247 if (account == NULL) {
251 return tp_account_get_icon_name (account);
255 empathy_pixbuf_from_data (gchar *data,
258 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
262 empathy_pixbuf_from_data_and_mime (gchar *data,
266 GdkPixbufLoader *loader;
267 GdkPixbufFormat *format;
268 GdkPixbuf *pixbuf = NULL;
270 GError *error = NULL;
276 loader = gdk_pixbuf_loader_new ();
277 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
278 DEBUG ("Failed to write to pixbuf loader: %s",
279 error ? error->message : "No error given");
282 if (!gdk_pixbuf_loader_close (loader, &error)) {
283 DEBUG ("Failed to close pixbuf loader: %s",
284 error ? error->message : "No error given");
288 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
290 g_object_ref (pixbuf);
292 if (mime_type != NULL) {
293 format = gdk_pixbuf_loader_get_format (loader);
294 mime_types = gdk_pixbuf_format_get_mime_types (format);
296 *mime_type = g_strdup (*mime_types);
297 if (mime_types[1] != NULL) {
298 DEBUG ("Loader supports more than one mime "
299 "type! Picking the first one, %s",
302 g_strfreev (mime_types);
307 g_clear_error (&error);
308 g_object_unref (loader);
316 gboolean preserve_aspect_ratio;
320 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
323 struct SizeData *data)
325 g_return_if_fail (width > 0 && height > 0);
327 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
328 if (width < data->width && height < data->height) {
333 if (data->width < 0) {
334 width = width * (double) data->height / (gdouble) height;
335 height = data->height;
336 } else if (data->height < 0) {
337 height = height * (double) data->width / (double) width;
339 } else if ((double) height * (double) data->width >
340 (double) width * (double) data->height) {
341 width = 0.5 + (double) width * (double) data->height / (double) height;
342 height = data->height;
344 height = 0.5 + (double) height * (double) data->width / (double) width;
348 if (data->width > 0) {
352 if (data->height > 0) {
353 height = data->height;
357 gdk_pixbuf_loader_set_size (loader, width, height);
361 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
363 gint width, height, rowstride;
366 width = gdk_pixbuf_get_width (pixbuf);
367 height = gdk_pixbuf_get_height (pixbuf);
368 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
369 pixels = gdk_pixbuf_get_pixels (pixbuf);
371 if (width < 6 || height < 6) {
379 pixels[rowstride + 3] = 0x80;
380 pixels[rowstride * 2 + 3] = 0xC0;
383 pixels[width * 4 - 1] = 0;
384 pixels[width * 4 - 5] = 0x80;
385 pixels[width * 4 - 9] = 0xC0;
386 pixels[rowstride + (width * 4) - 1] = 0x80;
387 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
390 pixels[(height - 1) * rowstride + 3] = 0;
391 pixels[(height - 1) * rowstride + 7] = 0x80;
392 pixels[(height - 1) * rowstride + 11] = 0xC0;
393 pixels[(height - 2) * rowstride + 3] = 0x80;
394 pixels[(height - 3) * rowstride + 3] = 0xC0;
397 pixels[height * rowstride - 1] = 0;
398 pixels[(height - 1) * rowstride - 1] = 0x80;
399 pixels[(height - 2) * rowstride - 1] = 0xC0;
400 pixels[height * rowstride - 5] = 0x80;
401 pixels[height * rowstride - 9] = 0xC0;
405 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
407 gint width, height, rowstride, i;
411 width = gdk_pixbuf_get_width (pixbuf);
412 height = gdk_pixbuf_get_height (pixbuf);
413 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
414 pixels = gdk_pixbuf_get_pixels (pixbuf);
417 for (i = 3; i < rowstride; i+=4) {
423 for (i = 1; i < height - 1; i++) {
424 row = pixels + (i*rowstride);
425 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
430 row = pixels + ((height-1) * rowstride);
431 for (i = 3; i < rowstride; i+=4) {
441 avatar_pixbuf_from_loader (GdkPixbufLoader *loader)
445 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
446 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
447 GdkPixbuf *rounded_pixbuf;
449 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
450 gdk_pixbuf_get_width (pixbuf),
451 gdk_pixbuf_get_height (pixbuf));
452 gdk_pixbuf_copy_area (pixbuf, 0, 0,
453 gdk_pixbuf_get_width (pixbuf),
454 gdk_pixbuf_get_height (pixbuf),
457 pixbuf = rounded_pixbuf;
459 g_object_ref (pixbuf);
462 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
463 empathy_avatar_pixbuf_roundify (pixbuf);
470 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
475 GdkPixbufLoader *loader;
476 struct SizeData data;
477 GError *error = NULL;
484 data.height = height;
485 data.preserve_aspect_ratio = TRUE;
487 loader = gdk_pixbuf_loader_new ();
489 g_signal_connect (loader, "size-prepared",
490 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
493 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
494 g_warning ("Couldn't write avatar image:%p with "
495 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
496 avatar->data, avatar->len, error->message);
497 g_error_free (error);
501 gdk_pixbuf_loader_close (loader, NULL);
502 pixbuf = avatar_pixbuf_from_loader (loader);
504 g_object_unref (loader);
510 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
514 EmpathyAvatar *avatar;
516 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
518 avatar = empathy_contact_get_avatar (contact);
520 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
524 FolksIndividual *individual;
525 GSimpleAsyncResult *result;
528 } PixbufAvatarFromIndividualClosure;
530 static PixbufAvatarFromIndividualClosure *
531 pixbuf_avatar_from_individual_closure_new (FolksIndividual *individual,
532 GSimpleAsyncResult *result,
536 PixbufAvatarFromIndividualClosure *closure;
538 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
539 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
541 closure = g_new0 (PixbufAvatarFromIndividualClosure, 1);
542 closure->individual = g_object_ref (individual);
543 closure->result = g_object_ref (result);
544 closure->width = width;
545 closure->height = height;
551 pixbuf_avatar_from_individual_closure_free (
552 PixbufAvatarFromIndividualClosure *closure)
554 g_object_unref (closure->individual);
555 g_object_unref (closure->result);
560 avatar_file_load_contents_cb (GObject *object,
561 GAsyncResult *result,
564 GFile *file = G_FILE (object);
565 PixbufAvatarFromIndividualClosure *closure = user_data;
568 struct SizeData size_data;
569 GError *error = NULL;
570 GdkPixbufLoader *loader = NULL;
572 if (!g_file_load_contents_finish (file, result, &data, &data_size,
574 DEBUG ("failed to load avatar from file: %s",
576 g_simple_async_result_set_from_error (closure->result, error);
580 size_data.width = closure->width;
581 size_data.height = closure->height;
582 size_data.preserve_aspect_ratio = TRUE;
584 loader = gdk_pixbuf_loader_new ();
586 g_signal_connect (loader, "size-prepared",
587 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
590 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size,
592 DEBUG ("Failed to write to pixbuf loader: %s",
593 error ? error->message : "No error given");
594 g_simple_async_result_set_from_error (closure->result, error);
597 if (!gdk_pixbuf_loader_close (loader, &error)) {
598 DEBUG ("Failed to close pixbuf loader: %s",
599 error ? error->message : "No error given");
600 g_simple_async_result_set_from_error (closure->result, error);
604 g_simple_async_result_set_op_res_gpointer (closure->result,
605 avatar_pixbuf_from_loader (loader), NULL);
608 g_simple_async_result_complete (closure->result);
610 g_clear_error (&error);
612 tp_clear_object (&loader);
613 pixbuf_avatar_from_individual_closure_free (closure);
617 empathy_pixbuf_avatar_from_individual_scaled_async (
618 FolksIndividual *individual,
619 GAsyncReadyCallback callback,
625 GSimpleAsyncResult *result;
626 PixbufAvatarFromIndividualClosure *closure;
628 result = g_simple_async_result_new (G_OBJECT (individual),
630 empathy_pixbuf_avatar_from_individual_scaled_finish);
632 avatar_file = folks_avatar_get_avatar (FOLKS_AVATAR (individual));
633 if (avatar_file == NULL)
636 closure = pixbuf_avatar_from_individual_closure_new (individual, result,
641 g_file_load_contents_async (avatar_file, NULL,
642 avatar_file_load_contents_cb, closure);
644 g_object_unref (result);
649 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
650 g_simple_async_result_complete (result);
651 g_object_unref (result);
655 empathy_pixbuf_avatar_from_individual_scaled_finish (
656 FolksIndividual *individual,
657 GAsyncResult *result,
660 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
661 gboolean result_valid;
663 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
664 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
666 if (g_simple_async_result_propagate_error (simple, error))
669 result_valid = g_simple_async_result_is_valid (result,
670 G_OBJECT (individual),
671 empathy_pixbuf_avatar_from_individual_scaled_finish);
672 g_return_val_if_fail (result_valid, NULL);
674 return g_simple_async_result_get_op_res_gpointer (simple);
678 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
679 gboolean show_protocol)
681 const gchar *icon_name;
683 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
685 icon_name = empathy_icon_name_for_contact (contact);
687 if (icon_name == NULL) {
690 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
696 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
697 const gchar *icon_name,
698 gboolean show_protocol)
700 GdkPixbuf *pix_status;
701 GdkPixbuf *pix_protocol;
702 gchar *icon_filename;
704 gint numerator, denominator;
706 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
707 (show_protocol == FALSE), NULL);
708 g_return_val_if_fail (icon_name != NULL, NULL);
713 icon_filename = empathy_filename_from_icon_name (icon_name,
715 if (icon_filename == NULL) {
716 DEBUG ("icon name: %s could not be found\n", icon_name);
720 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
722 if (pix_status == NULL) {
723 DEBUG ("Could not open icon %s\n", icon_filename);
724 g_free (icon_filename);
728 g_free (icon_filename);
733 height = gdk_pixbuf_get_height (pix_status);
734 width = gdk_pixbuf_get_width (pix_status);
736 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
737 width * numerator / denominator,
738 height * numerator / denominator);
740 if (pix_protocol == NULL) {
743 gdk_pixbuf_composite (pix_protocol, pix_status,
744 0, height - height * numerator / denominator,
745 width * numerator / denominator, height * numerator / denominator,
746 0, height - height * numerator / denominator,
748 GDK_INTERP_BILINEAR, 255);
750 g_object_unref (pix_protocol);
756 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
762 GdkPixbuf *pixbuf = NULL;
764 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
766 account = empathy_contact_get_account (contact);
767 filename = empathy_filename_from_icon_name (tp_account_get_icon_name (account),
769 if (filename != NULL) {
770 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
778 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
783 width = gdk_pixbuf_get_width (pixbuf);
784 height = gdk_pixbuf_get_height (pixbuf);
786 if (width > 0 && (width > max_size || height > max_size)) {
787 factor = (gdouble) max_size / MAX (width, height);
789 width = width * factor;
790 height = height * factor;
792 return gdk_pixbuf_scale_simple (pixbuf,
797 return g_object_ref (pixbuf);
801 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
806 GError *error = NULL;
812 theme = gtk_icon_theme_get_default ();
814 pixbuf = gtk_icon_theme_load_icon (theme,
820 DEBUG ("Error loading icon: %s", error->message);
821 g_clear_error (&error);
828 empathy_pixbuf_from_icon_name (const gchar *icon_name,
829 GtkIconSize icon_size)
838 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
842 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
846 empathy_filename_from_icon_name (const gchar *icon_name,
847 GtkIconSize icon_size)
849 GtkIconTheme *icon_theme;
850 GtkIconInfo *icon_info;
855 icon_theme = gtk_icon_theme_get_default ();
857 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
861 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
862 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
863 gtk_icon_info_free (icon_info);
868 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
869 * that to make it easier to apply changes from the original code.
871 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
873 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
874 * decomposable character it consumes the decomposition length from the given
875 * offset. So it's useful when the offset was calculated for the normalized
876 * version of str, but we need a pointer to str itself. */
878 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
880 gchar *casefold, *normal;
886 q = g_utf8_next_char (p);
887 casefold = g_utf8_casefold (p, q - p);
888 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
889 offset -= g_utf8_strlen (normal, -1);
898 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
902 const gchar *ret = NULL;
905 gchar *caseless_haystack;
908 g_return_val_if_fail (haystack != NULL, NULL);
909 g_return_val_if_fail (needle != NULL, NULL);
911 casefold = g_utf8_casefold (haystack, -1);
912 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
915 needle_len = g_utf8_strlen (needle, -1);
916 haystack_len = g_utf8_strlen (caseless_haystack, -1);
920 ret = (gchar *) haystack;
924 if (haystack_len < needle_len)
930 p = (gchar *) caseless_haystack;
931 needle_len = strlen (needle);
936 if ((strncmp (p, needle, needle_len) == 0))
938 ret = pointer_from_offset_skipping_decomp (haystack, i);
942 p = g_utf8_next_char (p);
947 g_free (caseless_haystack);
953 g_utf8_caselessnmatch (const char *s1, const char *s2,
954 gssize n1, gssize n2)
957 gchar *normalized_s1;
958 gchar *normalized_s2;
961 gboolean ret = FALSE;
963 g_return_val_if_fail (s1 != NULL, FALSE);
964 g_return_val_if_fail (s2 != NULL, FALSE);
965 g_return_val_if_fail (n1 > 0, FALSE);
966 g_return_val_if_fail (n2 > 0, FALSE);
968 casefold = g_utf8_casefold (s1, n1);
969 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
972 casefold = g_utf8_casefold (s2, n2);
973 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
976 len_s1 = strlen (normalized_s1);
977 len_s2 = strlen (normalized_s2);
982 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
985 g_free (normalized_s1);
986 g_free (normalized_s2);
992 forward_chars_with_skipping (GtkTextIter *iter,
994 gboolean skip_invisible,
995 gboolean skip_nontext,
996 gboolean skip_decomp)
1000 g_return_if_fail (count >= 0);
1006 gboolean ignored = FALSE;
1008 /* minimal workaround to avoid the infinite loop of bug #168247.
1009 * It doesn't fix the problemjust the symptom...
1011 if (gtk_text_iter_is_end (iter))
1014 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
1017 if (!ignored && skip_invisible &&
1018 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
1021 if (!ignored && skip_decomp)
1023 /* being UTF8 correct sucks; this accounts for extra
1024 offsets coming from canonical decompositions of
1025 UTF8 characters (e.g. accented characters) which
1026 g_utf8_normalize () performs */
1031 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
1032 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
1033 i -= (g_utf8_strlen (normal, -1) - 1);
1037 gtk_text_iter_forward_char (iter);
1045 lines_match (const GtkTextIter *start,
1046 const gchar **lines,
1047 gboolean visible_only,
1049 GtkTextIter *match_start,
1050 GtkTextIter *match_end)
1057 if (*lines == NULL || **lines == '\0')
1060 *match_start = *start;
1062 *match_end = *start;
1067 gtk_text_iter_forward_line (&next);
1069 /* No more text in buffer, but *lines is nonempty */
1070 if (gtk_text_iter_equal (start, &next))
1076 line_text = gtk_text_iter_get_visible_slice (start, &next);
1078 line_text = gtk_text_iter_get_slice (start, &next);
1083 line_text = gtk_text_iter_get_visible_text (start, &next);
1085 line_text = gtk_text_iter_get_text (start, &next);
1088 if (match_start) /* if this is the first line we're matching */
1090 found = g_utf8_strcasestr (line_text, *lines);
1094 /* If it's not the first line, we have to match from the
1095 * start of the line.
1097 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1110 /* Get offset to start of search string */
1111 offset = g_utf8_strlen (line_text, found - line_text);
1115 /* If match start needs to be returned, set it to the
1116 * start of the search string.
1118 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1121 *match_start = next;
1124 /* Go to end of search string */
1125 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1134 /* pass NULL for match_start, since we don't need to find the
1137 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
1140 /* strsplit () that retains the delimiter as part of the string. */
1142 strbreakup (const char *string,
1143 const char *delimiter,
1146 GSList *string_list = NULL, *slist;
1147 gchar **str_array, *s, *casefold, *new_string;
1150 g_return_val_if_fail (string != NULL, NULL);
1151 g_return_val_if_fail (delimiter != NULL, NULL);
1154 max_tokens = G_MAXINT;
1156 s = strstr (string, delimiter);
1159 guint delimiter_len = strlen (delimiter);
1165 len = s - string + delimiter_len;
1166 new_string = g_new (gchar, len + 1);
1167 strncpy (new_string, string, len);
1168 new_string[len] = 0;
1169 casefold = g_utf8_casefold (new_string, -1);
1170 g_free (new_string);
1171 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1173 string_list = g_slist_prepend (string_list, new_string);
1175 string = s + delimiter_len;
1176 s = strstr (string, delimiter);
1177 } while (--max_tokens && s);
1183 casefold = g_utf8_casefold (string, -1);
1184 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1186 string_list = g_slist_prepend (string_list, new_string);
1189 str_array = g_new (gchar*, n);
1193 str_array[i--] = NULL;
1194 for (slist = string_list; slist; slist = slist->next)
1195 str_array[i--] = slist->data;
1197 g_slist_free (string_list);
1203 empathy_text_iter_forward_search (const GtkTextIter *iter,
1205 GtkTextIter *match_start,
1206 GtkTextIter *match_end,
1207 const GtkTextIter *limit)
1209 gchar **lines = NULL;
1211 gboolean retval = FALSE;
1213 gboolean visible_only;
1216 g_return_val_if_fail (iter != NULL, FALSE);
1217 g_return_val_if_fail (str != NULL, FALSE);
1219 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
1223 /* If we can move one char, return the empty string there */
1226 if (gtk_text_iter_forward_char (&match)) {
1227 if (limit && gtk_text_iter_equal (&match, limit)) {
1232 *match_start = match;
1243 visible_only = TRUE;
1246 /* locate all lines */
1247 lines = strbreakup (str, "\n", -1);
1252 /* This loop has an inefficient worst-case, where
1253 * gtk_text_iter_get_text () is called repeatedly on
1258 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1262 if (lines_match (&search, (const gchar**)lines,
1263 visible_only, slice, &match, &end)) {
1264 if (limit == NULL ||
1265 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1269 *match_start = match;
1277 } while (gtk_text_iter_forward_line (&search));
1279 g_strfreev ((gchar **) lines);
1284 static const gchar *
1285 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1289 const gchar *ret = NULL;
1292 gchar *caseless_haystack;
1295 g_return_val_if_fail (haystack != NULL, NULL);
1296 g_return_val_if_fail (needle != NULL, NULL);
1298 casefold = g_utf8_casefold (haystack, -1);
1299 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1302 needle_len = g_utf8_strlen (needle, -1);
1303 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1305 if (needle_len == 0)
1307 ret = (gchar *) haystack;
1311 if (haystack_len < needle_len)
1317 i = haystack_len - needle_len;
1318 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1319 needle_len = strlen (needle);
1321 while (p >= caseless_haystack)
1323 if (strncmp (p, needle, needle_len) == 0)
1325 ret = pointer_from_offset_skipping_decomp (haystack, i);
1329 p = g_utf8_prev_char (p);
1334 g_free (caseless_haystack);
1340 backward_lines_match (const GtkTextIter *start,
1341 const gchar **lines,
1342 gboolean visible_only,
1344 GtkTextIter *match_start,
1345 GtkTextIter *match_end)
1347 GtkTextIter line, next;
1352 if (*lines == NULL || **lines == '\0')
1355 *match_start = *start;
1357 *match_end = *start;
1361 line = next = *start;
1362 if (gtk_text_iter_get_line_offset (&next) == 0)
1364 if (!gtk_text_iter_backward_line (&next))
1368 gtk_text_iter_set_line_offset (&next, 0);
1373 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1375 line_text = gtk_text_iter_get_slice (&next, &line);
1380 line_text = gtk_text_iter_get_visible_text (&next, &line);
1382 line_text = gtk_text_iter_get_text (&next, &line);
1385 if (match_start) /* if this is the first line we're matching */
1387 found = g_utf8_strrcasestr (line_text, *lines);
1391 /* If it's not the first line, we have to match from the
1392 * start of the line.
1394 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1407 /* Get offset to start of search string */
1408 offset = g_utf8_strlen (line_text, found - line_text);
1410 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1412 /* If match start needs to be returned, set it to the
1413 * start of the search string.
1417 *match_start = next;
1420 /* Go to end of search string */
1421 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1430 /* try to match the rest of the lines forward, passing NULL
1431 * for match_start so lines_match will try to match the entire
1433 return lines_match (&next, lines, visible_only,
1434 slice, NULL, match_end);
1438 empathy_text_iter_backward_search (const GtkTextIter *iter,
1440 GtkTextIter *match_start,
1441 GtkTextIter *match_end,
1442 const GtkTextIter *limit)
1444 gchar **lines = NULL;
1446 gboolean retval = FALSE;
1448 gboolean visible_only;
1451 g_return_val_if_fail (iter != NULL, FALSE);
1452 g_return_val_if_fail (str != NULL, FALSE);
1454 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1459 /* If we can move one char, return the empty string there */
1462 if (gtk_text_iter_backward_char (&match))
1464 if (limit && gtk_text_iter_equal (&match, limit))
1468 *match_start = match;
1479 visible_only = TRUE;
1482 /* locate all lines */
1483 lines = strbreakup (str, "\n", -1);
1489 /* This loop has an inefficient worst-case, where
1490 * gtk_text_iter_get_text () is called repeatedly on
1495 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1498 if (backward_lines_match (&search, (const gchar**)lines,
1499 visible_only, slice, &match, &end))
1501 if (limit == NULL || (limit &&
1502 gtk_text_iter_compare (&end, limit) > 0))
1507 *match_start = match;
1514 if (gtk_text_iter_get_line_offset (&search) == 0)
1516 if (!gtk_text_iter_backward_line (&search))
1521 gtk_text_iter_set_line_offset (&search, 0);
1525 g_strfreev ((gchar **) lines);
1531 empathy_window_get_is_visible (GtkWindow *window)
1533 GdkWindowState state;
1534 GdkWindow *gdk_window;
1536 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1538 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1543 state = gdk_window_get_state (gdk_window);
1544 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1552 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1554 GdkRectangle icon_location;
1557 GdkWindow *gdk_window;
1559 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1560 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1561 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1563 data[0] = icon_location.x;
1564 data[1] = icon_location.y;
1565 data[2] = icon_location.width;
1566 data[3] = icon_location.height;
1568 XChangeProperty (dpy,
1569 GDK_WINDOW_XID (gdk_window),
1570 gdk_x11_get_xatom_by_name_for_display (
1571 gdk_drawable_get_display (gdk_window),
1572 "_NET_WM_ICON_GEOMETRY"),
1573 XA_CARDINAL, 32, PropModeReplace,
1574 (guchar *)&data, 4);
1576 gtk_window_set_skip_taskbar_hint (window, TRUE);
1577 gtk_window_iconify (window);
1580 /* Takes care of moving the window to the current workspace. */
1582 empathy_window_present_with_time (GtkWindow *window,
1585 GdkWindow *gdk_window;
1587 g_return_if_fail (GTK_IS_WINDOW (window));
1589 /* Move the window to the current workspace before trying to show it.
1590 * This is the behaviour people expect when clicking on the statusbar icon. */
1591 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1596 /* Has no effect if the WM has viewports, like compiz */
1597 gdk_x11_window_move_to_current_desktop (gdk_window);
1599 /* If window is still off-screen, hide it to force it to
1600 * reposition on the current workspace. */
1601 gtk_window_get_position (window, &x, &y);
1602 gtk_window_get_size (window, &w, &h);
1603 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1604 gtk_widget_hide (GTK_WIDGET (window));
1607 gtk_window_present_with_time (window, timestamp);
1608 gtk_window_set_skip_taskbar_hint (window, FALSE);
1609 gtk_window_deiconify (window);
1613 empathy_window_present (GtkWindow *window)
1615 empathy_window_present_with_time (window, GDK_CURRENT_TIME);
1619 empathy_get_toplevel_window (GtkWidget *widget)
1621 GtkWidget *toplevel;
1623 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1625 toplevel = gtk_widget_get_toplevel (widget);
1626 if (GTK_IS_WINDOW (toplevel) &&
1627 gtk_widget_is_toplevel (toplevel)) {
1628 return GTK_WINDOW (toplevel);
1634 /** empathy_make_absolute_url_len:
1638 * Same as #empathy_make_absolute_url but for a limited string length
1641 empathy_make_absolute_url_len (const gchar *url,
1644 g_return_val_if_fail (url != NULL, NULL);
1646 if (g_str_has_prefix (url, "ghelp:") ||
1647 g_str_has_prefix (url, "mailto:") ||
1648 strstr (url, ":/")) {
1649 return g_strndup (url, len);
1652 if (strstr (url, "@")) {
1653 return g_strdup_printf ("mailto:%.*s", len, url);
1656 return g_strdup_printf ("http://%.*s", len, url);
1659 /** empathy_make_absolute_url:
1662 * The URL opening code can't handle schemeless strings, so we try to be
1663 * smart and add http if there is no scheme or doesn't look like a mail
1664 * address. This should work in most cases, and let us click on strings
1665 * like "www.gnome.org".
1667 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1668 * g_free when your are done with it
1671 empathy_make_absolute_url (const gchar *url)
1673 return empathy_make_absolute_url_len (url, strlen (url));
1677 empathy_url_show (GtkWidget *parent,
1681 GError *error = NULL;
1683 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1684 g_return_if_fail (url != NULL);
1686 real_url = empathy_make_absolute_url (url);
1688 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1689 gtk_get_current_event_time (), &error);
1694 dialog = gtk_message_dialog_new (NULL, 0,
1695 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1696 _("Unable to open URI"));
1697 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1698 "%s", error->message);
1700 g_signal_connect (dialog, "response",
1701 G_CALLBACK (gtk_widget_destroy),
1703 gtk_window_present (GTK_WINDOW (dialog));
1705 g_clear_error (&error);
1712 empathy_send_file (EmpathyContact *contact, GFile *file)
1714 EmpathyFTFactory *factory;
1715 GtkRecentManager *manager;
1718 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1719 g_return_if_fail (G_IS_FILE (file));
1721 factory = empathy_ft_factory_dup_singleton ();
1723 empathy_ft_factory_new_transfer_outgoing (factory, contact, file);
1725 uri = g_file_get_uri (file);
1726 manager = gtk_recent_manager_get_default ();
1727 gtk_recent_manager_add_item (manager, uri);
1730 g_object_unref (factory);
1734 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1739 /* Only handle a single file for now. It would be wicked cool to be
1740 able to do multiple files, offering to zip them or whatever like
1741 nautilus-sendto does. Note that text/uri-list is defined to have
1742 each line terminated by \r\n, but we can be tolerant of applications
1743 that only use \n or don't terminate single-line entries.
1745 nl = strstr (uri_list, "\r\n");
1747 nl = strchr (uri_list, '\n');
1750 gchar *uri = g_strndup (uri_list, nl - uri_list);
1751 file = g_file_new_for_uri (uri);
1755 file = g_file_new_for_uri (uri_list);
1758 empathy_send_file (contact, file);
1760 g_object_unref (file);
1764 file_manager_send_file_response_cb (GtkDialog *widget,
1766 EmpathyContact *contact)
1770 if (response_id == GTK_RESPONSE_OK) {
1771 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1773 empathy_send_file (contact, file);
1775 g_object_unref (file);
1778 gtk_widget_destroy (GTK_WIDGET (widget));
1782 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1787 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1789 DEBUG ("Creating selection file chooser");
1791 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1793 GTK_FILE_CHOOSER_ACTION_OPEN,
1795 GTK_RESPONSE_CANCEL,
1799 button = gtk_button_new_with_mnemonic (_("_Send"));
1800 gtk_button_set_image (GTK_BUTTON (button),
1801 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1802 GTK_ICON_SIZE_BUTTON));
1803 gtk_widget_show (button);
1804 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1806 gtk_widget_set_can_default (button, TRUE);
1807 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1810 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1812 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1815 g_signal_connect (widget, "response",
1816 G_CALLBACK (file_manager_send_file_response_cb),
1819 gtk_widget_show (widget);
1823 file_manager_receive_file_response_cb (GtkDialog *dialog,
1824 GtkResponseType response,
1825 EmpathyFTHandler *handler)
1827 EmpathyFTFactory *factory;
1830 if (response == GTK_RESPONSE_OK) {
1831 factory = empathy_ft_factory_dup_singleton ();
1832 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1834 empathy_ft_factory_set_destination_for_incoming_handler
1835 (factory, handler, file);
1837 g_object_unref (factory);
1838 g_object_unref (file);
1840 /* unref the handler, as we dismissed the file chooser,
1841 * and refused the transfer.
1843 g_object_unref (handler);
1846 gtk_widget_destroy (GTK_WIDGET (dialog));
1850 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1854 EmpathyContact *contact;
1857 contact = empathy_ft_handler_get_contact (handler);
1858 g_assert (contact != NULL);
1860 title = g_strdup_printf (_("Incoming file from %s"),
1861 empathy_contact_get_name (contact));
1863 widget = gtk_file_chooser_dialog_new (title,
1865 GTK_FILE_CHOOSER_ACTION_SAVE,
1867 GTK_RESPONSE_CANCEL,
1871 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1872 empathy_ft_handler_get_filename (handler));
1873 gtk_file_chooser_set_do_overwrite_confirmation
1874 (GTK_FILE_CHOOSER (widget), TRUE);
1876 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1878 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1879 dir = g_get_home_dir ();
1881 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1883 g_signal_connect (widget, "response",
1884 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1886 gtk_widget_show (widget);