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-ft-factory.h>
55 empathy_gtk_init (void)
57 static gboolean initialized = FALSE;
63 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
64 PKGDATADIR G_DIR_SEPARATOR_S "icons");
70 builder_get_file_valist (const gchar *filename,
71 const gchar *first_object,
79 DEBUG ("Loading file %s", filename);
81 gui = gtk_builder_new ();
82 gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
83 if (!gtk_builder_add_from_file (gui, filename, &error)) {
84 g_critical ("GtkBuilder Error (%s): %s",
85 filename, error->message);
86 g_clear_error (&error);
89 /* we need to iterate and set all of the pointers to NULL */
90 for (name = first_object; name;
91 name = va_arg (args, const gchar *)) {
92 object_ptr = va_arg (args, GObject**);
100 for (name = first_object; name; name = va_arg (args, const gchar *)) {
101 object_ptr = va_arg (args, GObject**);
103 *object_ptr = gtk_builder_get_object (gui, name);
106 g_warning ("File is missing object '%s'.", name);
115 empathy_builder_get_file (const gchar *filename,
116 const gchar *first_object,
122 va_start (args, first_object);
123 gui = builder_get_file_valist (filename, first_object, args);
130 empathy_builder_connect (GtkBuilder *gui,
132 const gchar *first_object,
141 va_start (args, first_object);
142 for (name = first_object; name; name = va_arg (args, const gchar *)) {
143 sig = va_arg (args, const gchar *);
144 callback = va_arg (args, GCallback);
146 object = gtk_builder_get_object (gui, name);
148 g_warning ("File is missing object '%s'.", name);
152 g_signal_connect (object, sig, callback, user_data);
159 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
162 /* On construction gui sinks the initial reference to widget. When gui
163 * is finalized it will drop its ref to widget. We take our own ref to
164 * prevent widget being finalised. The widget is forced to have a
165 * floating reference, like when it was initially unowned so that it can
166 * be used like any other GtkWidget. */
168 g_object_ref (widget);
169 g_object_force_floating (G_OBJECT (widget));
170 g_object_unref (gui);
176 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
179 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
180 return EMPATHY_IMAGE_AVAILABLE;
181 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
182 return EMPATHY_IMAGE_BUSY;
183 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
184 return EMPATHY_IMAGE_AWAY;
185 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
186 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
187 EMPATHY_IMAGE_EXT_AWAY))
188 return EMPATHY_IMAGE_EXT_AWAY;
190 /* The 'extended-away' icon is not an official one so we fallback to idle if
191 * it's not implemented */
192 return EMPATHY_IMAGE_IDLE;
193 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
194 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
195 EMPATHY_IMAGE_HIDDEN))
196 return EMPATHY_IMAGE_HIDDEN;
198 /* The 'hidden' icon is not an official one so we fallback to offline if
199 * it's not implemented */
200 return EMPATHY_IMAGE_OFFLINE;
201 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
202 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
203 return EMPATHY_IMAGE_OFFLINE;
204 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
205 return EMPATHY_IMAGE_PENDING;
206 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
215 empathy_icon_name_for_contact (EmpathyContact *contact)
217 TpConnectionPresenceType presence;
219 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
220 EMPATHY_IMAGE_OFFLINE);
222 presence = empathy_contact_get_presence (contact);
223 return empathy_icon_name_for_presence (presence);
227 empathy_icon_name_for_individual (FolksIndividual *individual)
229 FolksPresenceType folks_presence;
230 TpConnectionPresenceType presence;
233 folks_presence_details_get_presence_type (
234 FOLKS_PRESENCE_DETAILS (individual));
235 presence = empathy_folks_presence_type_to_tp (folks_presence);
237 return empathy_icon_name_for_presence (presence);
241 empathy_protocol_name_for_contact (EmpathyContact *contact)
245 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
247 account = empathy_contact_get_account (contact);
248 if (account == NULL) {
252 return tp_account_get_icon_name (account);
256 empathy_pixbuf_from_data (gchar *data,
259 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
263 empathy_pixbuf_from_data_and_mime (gchar *data,
267 GdkPixbufLoader *loader;
268 GdkPixbufFormat *format;
269 GdkPixbuf *pixbuf = NULL;
271 GError *error = NULL;
277 loader = gdk_pixbuf_loader_new ();
278 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
279 DEBUG ("Failed to write to pixbuf loader: %s",
280 error ? error->message : "No error given");
283 if (!gdk_pixbuf_loader_close (loader, &error)) {
284 DEBUG ("Failed to close pixbuf loader: %s",
285 error ? error->message : "No error given");
289 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
291 g_object_ref (pixbuf);
293 if (mime_type != NULL) {
294 format = gdk_pixbuf_loader_get_format (loader);
295 mime_types = gdk_pixbuf_format_get_mime_types (format);
297 *mime_type = g_strdup (*mime_types);
298 if (mime_types[1] != NULL) {
299 DEBUG ("Loader supports more than one mime "
300 "type! Picking the first one, %s",
303 g_strfreev (mime_types);
308 g_clear_error (&error);
309 g_object_unref (loader);
317 gboolean preserve_aspect_ratio;
321 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
324 struct SizeData *data)
326 g_return_if_fail (width > 0 && height > 0);
328 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
329 if (width < data->width && height < data->height) {
334 if (data->width < 0) {
335 width = width * (double) data->height / (gdouble) height;
336 height = data->height;
337 } else if (data->height < 0) {
338 height = height * (double) data->width / (double) width;
340 } else if ((double) height * (double) data->width >
341 (double) width * (double) data->height) {
342 width = 0.5 + (double) width * (double) data->height / (double) height;
343 height = data->height;
345 height = 0.5 + (double) height * (double) data->width / (double) width;
349 if (data->width > 0) {
353 if (data->height > 0) {
354 height = data->height;
358 gdk_pixbuf_loader_set_size (loader, width, height);
362 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
364 gint width, height, rowstride;
367 width = gdk_pixbuf_get_width (pixbuf);
368 height = gdk_pixbuf_get_height (pixbuf);
369 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
370 pixels = gdk_pixbuf_get_pixels (pixbuf);
372 if (width < 6 || height < 6) {
380 pixels[rowstride + 3] = 0x80;
381 pixels[rowstride * 2 + 3] = 0xC0;
384 pixels[width * 4 - 1] = 0;
385 pixels[width * 4 - 5] = 0x80;
386 pixels[width * 4 - 9] = 0xC0;
387 pixels[rowstride + (width * 4) - 1] = 0x80;
388 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
391 pixels[(height - 1) * rowstride + 3] = 0;
392 pixels[(height - 1) * rowstride + 7] = 0x80;
393 pixels[(height - 1) * rowstride + 11] = 0xC0;
394 pixels[(height - 2) * rowstride + 3] = 0x80;
395 pixels[(height - 3) * rowstride + 3] = 0xC0;
398 pixels[height * rowstride - 1] = 0;
399 pixels[(height - 1) * rowstride - 1] = 0x80;
400 pixels[(height - 2) * rowstride - 1] = 0xC0;
401 pixels[height * rowstride - 5] = 0x80;
402 pixels[height * rowstride - 9] = 0xC0;
406 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
408 gint width, height, rowstride, i;
412 width = gdk_pixbuf_get_width (pixbuf);
413 height = gdk_pixbuf_get_height (pixbuf);
414 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
415 pixels = gdk_pixbuf_get_pixels (pixbuf);
418 for (i = 3; i < rowstride; i+=4) {
424 for (i = 1; i < height - 1; i++) {
425 row = pixels + (i*rowstride);
426 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
431 row = pixels + ((height-1) * rowstride);
432 for (i = 3; i < rowstride; i+=4) {
442 avatar_pixbuf_from_loader (GdkPixbufLoader *loader)
446 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
447 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
448 GdkPixbuf *rounded_pixbuf;
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),
458 pixbuf = rounded_pixbuf;
460 g_object_ref (pixbuf);
463 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
464 empathy_avatar_pixbuf_roundify (pixbuf);
471 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
476 GdkPixbufLoader *loader;
477 struct SizeData data;
478 GError *error = NULL;
485 data.height = height;
486 data.preserve_aspect_ratio = TRUE;
488 loader = gdk_pixbuf_loader_new ();
490 g_signal_connect (loader, "size-prepared",
491 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
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);
502 gdk_pixbuf_loader_close (loader, NULL);
503 pixbuf = avatar_pixbuf_from_loader (loader);
505 g_object_unref (loader);
511 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
515 EmpathyAvatar *avatar;
517 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
519 avatar = empathy_contact_get_avatar (contact);
521 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
525 FolksIndividual *individual;
526 GSimpleAsyncResult *result;
529 } PixbufAvatarFromIndividualClosure;
531 static PixbufAvatarFromIndividualClosure *
532 pixbuf_avatar_from_individual_closure_new (FolksIndividual *individual,
533 GSimpleAsyncResult *result,
537 PixbufAvatarFromIndividualClosure *closure;
539 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
540 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
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;
552 pixbuf_avatar_from_individual_closure_free (
553 PixbufAvatarFromIndividualClosure *closure)
555 g_object_unref (closure->individual);
556 g_object_unref (closure->result);
561 avatar_file_load_contents_cb (GObject *object,
562 GAsyncResult *result,
565 GFile *file = G_FILE (object);
566 PixbufAvatarFromIndividualClosure *closure = user_data;
569 struct SizeData size_data;
570 GError *error = NULL;
571 GdkPixbufLoader *loader = NULL;
573 if (!g_file_load_contents_finish (file, result, &data, &data_size,
575 DEBUG ("failed to load avatar from file: %s",
577 g_simple_async_result_set_from_error (closure->result, error);
581 size_data.width = closure->width;
582 size_data.height = closure->height;
583 size_data.preserve_aspect_ratio = TRUE;
585 loader = gdk_pixbuf_loader_new ();
587 g_signal_connect (loader, "size-prepared",
588 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
591 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size,
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);
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);
605 g_simple_async_result_set_op_res_gpointer (closure->result,
606 avatar_pixbuf_from_loader (loader), g_object_unref);
609 g_simple_async_result_complete (closure->result);
611 g_clear_error (&error);
613 tp_clear_object (&loader);
614 pixbuf_avatar_from_individual_closure_free (closure);
618 empathy_pixbuf_avatar_from_individual_scaled_async (
619 FolksIndividual *individual,
622 GCancellable *cancellable,
623 GAsyncReadyCallback callback,
627 GSimpleAsyncResult *result;
628 PixbufAvatarFromIndividualClosure *closure;
630 result = g_simple_async_result_new (G_OBJECT (individual),
632 empathy_pixbuf_avatar_from_individual_scaled_async);
635 folks_avatar_details_get_avatar (FOLKS_AVATAR_DETAILS (individual));
636 if (avatar_file == NULL)
639 closure = pixbuf_avatar_from_individual_closure_new (individual, result,
644 g_file_load_contents_async (avatar_file, cancellable,
645 avatar_file_load_contents_cb, closure);
647 g_object_unref (result);
652 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
653 g_simple_async_result_complete (result);
654 g_object_unref (result);
657 /* Return a ref on the GdkPixbuf */
659 empathy_pixbuf_avatar_from_individual_scaled_finish (
660 FolksIndividual *individual,
661 GAsyncResult *result,
664 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
665 gboolean result_valid;
668 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
669 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
671 if (g_simple_async_result_propagate_error (simple, error))
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);
679 pixbuf = g_simple_async_result_get_op_res_gpointer (simple);
680 return pixbuf != NULL ? g_object_ref (pixbuf) : NULL;
684 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
685 gboolean show_protocol)
687 const gchar *icon_name;
689 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
691 icon_name = empathy_icon_name_for_contact (contact);
693 if (icon_name == NULL) {
696 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
702 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
703 const gchar *icon_name,
704 gboolean show_protocol)
706 GdkPixbuf *pix_status;
707 GdkPixbuf *pix_protocol;
708 gchar *icon_filename;
710 gint numerator, denominator;
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);
719 icon_filename = empathy_filename_from_icon_name (icon_name,
721 if (icon_filename == NULL) {
722 DEBUG ("icon name: %s could not be found\n", icon_name);
726 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
728 if (pix_status == NULL) {
729 DEBUG ("Could not open icon %s\n", icon_filename);
730 g_free (icon_filename);
734 g_free (icon_filename);
739 height = gdk_pixbuf_get_height (pix_status);
740 width = gdk_pixbuf_get_width (pix_status);
742 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
743 width * numerator / denominator,
744 height * numerator / denominator);
746 if (pix_protocol == NULL) {
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,
754 GDK_INTERP_BILINEAR, 255);
756 g_object_unref (pix_protocol);
762 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
768 GdkPixbuf *pixbuf = NULL;
770 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
772 account = empathy_contact_get_account (contact);
773 filename = empathy_filename_from_icon_name (tp_account_get_icon_name (account),
775 if (filename != NULL) {
776 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
784 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
789 width = gdk_pixbuf_get_width (pixbuf);
790 height = gdk_pixbuf_get_height (pixbuf);
792 if (width > 0 && (width > max_size || height > max_size)) {
793 factor = (gdouble) max_size / MAX (width, height);
795 width = width * factor;
796 height = height * factor;
798 return gdk_pixbuf_scale_simple (pixbuf,
803 return g_object_ref (pixbuf);
807 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
812 GError *error = NULL;
818 theme = gtk_icon_theme_get_default ();
820 pixbuf = gtk_icon_theme_load_icon (theme,
826 DEBUG ("Error loading icon: %s", error->message);
827 g_clear_error (&error);
834 empathy_pixbuf_from_icon_name (const gchar *icon_name,
835 GtkIconSize icon_size)
844 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
848 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
852 empathy_filename_from_icon_name (const gchar *icon_name,
853 GtkIconSize icon_size)
855 GtkIconTheme *icon_theme;
856 GtkIconInfo *icon_info;
861 icon_theme = gtk_icon_theme_get_default ();
863 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
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);
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.
877 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
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. */
884 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
886 gchar *casefold, *normal;
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);
904 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
908 const gchar *ret = NULL;
911 gchar *caseless_haystack;
914 g_return_val_if_fail (haystack != NULL, NULL);
915 g_return_val_if_fail (needle != NULL, NULL);
917 casefold = g_utf8_casefold (haystack, -1);
918 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
921 needle_len = g_utf8_strlen (needle, -1);
922 haystack_len = g_utf8_strlen (caseless_haystack, -1);
926 ret = (gchar *) haystack;
930 if (haystack_len < needle_len)
936 p = (gchar *) caseless_haystack;
937 needle_len = strlen (needle);
942 if ((strncmp (p, needle, needle_len) == 0))
944 ret = pointer_from_offset_skipping_decomp (haystack, i);
948 p = g_utf8_next_char (p);
953 g_free (caseless_haystack);
959 g_utf8_caselessnmatch (const char *s1, const char *s2,
960 gssize n1, gssize n2)
963 gchar *normalized_s1;
964 gchar *normalized_s2;
967 gboolean ret = FALSE;
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);
974 casefold = g_utf8_casefold (s1, n1);
975 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
978 casefold = g_utf8_casefold (s2, n2);
979 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
982 len_s1 = strlen (normalized_s1);
983 len_s2 = strlen (normalized_s2);
988 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
991 g_free (normalized_s1);
992 g_free (normalized_s2);
998 forward_chars_with_skipping (GtkTextIter *iter,
1000 gboolean skip_invisible,
1001 gboolean skip_nontext,
1002 gboolean skip_decomp)
1006 g_return_if_fail (count >= 0);
1012 gboolean ignored = FALSE;
1014 /* minimal workaround to avoid the infinite loop of bug #168247.
1015 * It doesn't fix the problemjust the symptom...
1017 if (gtk_text_iter_is_end (iter))
1020 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
1023 if (!ignored && skip_invisible &&
1024 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
1027 if (!ignored && skip_decomp)
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 */
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);
1043 gtk_text_iter_forward_char (iter);
1051 lines_match (const GtkTextIter *start,
1052 const gchar **lines,
1053 gboolean visible_only,
1055 GtkTextIter *match_start,
1056 GtkTextIter *match_end)
1063 if (*lines == NULL || **lines == '\0')
1066 *match_start = *start;
1068 *match_end = *start;
1073 gtk_text_iter_forward_line (&next);
1075 /* No more text in buffer, but *lines is nonempty */
1076 if (gtk_text_iter_equal (start, &next))
1082 line_text = gtk_text_iter_get_visible_slice (start, &next);
1084 line_text = gtk_text_iter_get_slice (start, &next);
1089 line_text = gtk_text_iter_get_visible_text (start, &next);
1091 line_text = gtk_text_iter_get_text (start, &next);
1094 if (match_start) /* if this is the first line we're matching */
1096 found = g_utf8_strcasestr (line_text, *lines);
1100 /* If it's not the first line, we have to match from the
1101 * start of the line.
1103 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1116 /* Get offset to start of search string */
1117 offset = g_utf8_strlen (line_text, found - line_text);
1121 /* If match start needs to be returned, set it to the
1122 * start of the search string.
1124 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1127 *match_start = next;
1130 /* Go to end of search string */
1131 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1140 /* pass NULL for match_start, since we don't need to find the
1143 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
1146 /* strsplit () that retains the delimiter as part of the string. */
1148 strbreakup (const char *string,
1149 const char *delimiter,
1152 GSList *string_list = NULL, *slist;
1153 gchar **str_array, *s, *casefold, *new_string;
1156 g_return_val_if_fail (string != NULL, NULL);
1157 g_return_val_if_fail (delimiter != NULL, NULL);
1160 max_tokens = G_MAXINT;
1162 s = strstr (string, delimiter);
1165 guint delimiter_len = strlen (delimiter);
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);
1179 string_list = g_slist_prepend (string_list, new_string);
1181 string = s + delimiter_len;
1182 s = strstr (string, delimiter);
1183 } while (--max_tokens && s);
1189 casefold = g_utf8_casefold (string, -1);
1190 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1192 string_list = g_slist_prepend (string_list, new_string);
1195 str_array = g_new (gchar*, n);
1199 str_array[i--] = NULL;
1200 for (slist = string_list; slist; slist = slist->next)
1201 str_array[i--] = slist->data;
1203 g_slist_free (string_list);
1209 empathy_text_iter_forward_search (const GtkTextIter *iter,
1211 GtkTextIter *match_start,
1212 GtkTextIter *match_end,
1213 const GtkTextIter *limit)
1215 gchar **lines = NULL;
1217 gboolean retval = FALSE;
1219 gboolean visible_only;
1222 g_return_val_if_fail (iter != NULL, FALSE);
1223 g_return_val_if_fail (str != NULL, FALSE);
1225 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
1229 /* If we can move one char, return the empty string there */
1232 if (gtk_text_iter_forward_char (&match)) {
1233 if (limit && gtk_text_iter_equal (&match, limit)) {
1238 *match_start = match;
1249 visible_only = TRUE;
1252 /* locate all lines */
1253 lines = strbreakup (str, "\n", -1);
1258 /* This loop has an inefficient worst-case, where
1259 * gtk_text_iter_get_text () is called repeatedly on
1264 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
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)) {
1275 *match_start = match;
1283 } while (gtk_text_iter_forward_line (&search));
1285 g_strfreev ((gchar **) lines);
1290 static const gchar *
1291 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1295 const gchar *ret = NULL;
1298 gchar *caseless_haystack;
1301 g_return_val_if_fail (haystack != NULL, NULL);
1302 g_return_val_if_fail (needle != NULL, NULL);
1304 casefold = g_utf8_casefold (haystack, -1);
1305 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1308 needle_len = g_utf8_strlen (needle, -1);
1309 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1311 if (needle_len == 0)
1313 ret = (gchar *) haystack;
1317 if (haystack_len < needle_len)
1323 i = haystack_len - needle_len;
1324 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1325 needle_len = strlen (needle);
1327 while (p >= caseless_haystack)
1329 if (strncmp (p, needle, needle_len) == 0)
1331 ret = pointer_from_offset_skipping_decomp (haystack, i);
1335 p = g_utf8_prev_char (p);
1340 g_free (caseless_haystack);
1346 backward_lines_match (const GtkTextIter *start,
1347 const gchar **lines,
1348 gboolean visible_only,
1350 GtkTextIter *match_start,
1351 GtkTextIter *match_end)
1353 GtkTextIter line, next;
1358 if (*lines == NULL || **lines == '\0')
1361 *match_start = *start;
1363 *match_end = *start;
1367 line = next = *start;
1368 if (gtk_text_iter_get_line_offset (&next) == 0)
1370 if (!gtk_text_iter_backward_line (&next))
1374 gtk_text_iter_set_line_offset (&next, 0);
1379 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1381 line_text = gtk_text_iter_get_slice (&next, &line);
1386 line_text = gtk_text_iter_get_visible_text (&next, &line);
1388 line_text = gtk_text_iter_get_text (&next, &line);
1391 if (match_start) /* if this is the first line we're matching */
1393 found = g_utf8_strrcasestr (line_text, *lines);
1397 /* If it's not the first line, we have to match from the
1398 * start of the line.
1400 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1413 /* Get offset to start of search string */
1414 offset = g_utf8_strlen (line_text, found - line_text);
1416 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1418 /* If match start needs to be returned, set it to the
1419 * start of the search string.
1423 *match_start = next;
1426 /* Go to end of search string */
1427 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
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
1439 return lines_match (&next, lines, visible_only,
1440 slice, NULL, match_end);
1444 empathy_text_iter_backward_search (const GtkTextIter *iter,
1446 GtkTextIter *match_start,
1447 GtkTextIter *match_end,
1448 const GtkTextIter *limit)
1450 gchar **lines = NULL;
1452 gboolean retval = FALSE;
1454 gboolean visible_only;
1457 g_return_val_if_fail (iter != NULL, FALSE);
1458 g_return_val_if_fail (str != NULL, FALSE);
1460 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1465 /* If we can move one char, return the empty string there */
1468 if (gtk_text_iter_backward_char (&match))
1470 if (limit && gtk_text_iter_equal (&match, limit))
1474 *match_start = match;
1485 visible_only = TRUE;
1488 /* locate all lines */
1489 lines = strbreakup (str, "\n", -1);
1495 /* This loop has an inefficient worst-case, where
1496 * gtk_text_iter_get_text () is called repeatedly on
1501 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1504 if (backward_lines_match (&search, (const gchar**)lines,
1505 visible_only, slice, &match, &end))
1507 if (limit == NULL || (limit &&
1508 gtk_text_iter_compare (&end, limit) > 0))
1513 *match_start = match;
1520 if (gtk_text_iter_get_line_offset (&search) == 0)
1522 if (!gtk_text_iter_backward_line (&search))
1527 gtk_text_iter_set_line_offset (&search, 0);
1531 g_strfreev ((gchar **) lines);
1537 empathy_window_get_is_visible (GtkWindow *window)
1539 GdkWindowState state;
1540 GdkWindow *gdk_window;
1542 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1544 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1549 state = gdk_window_get_state (gdk_window);
1550 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1558 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1560 GdkRectangle icon_location;
1563 GdkWindow *gdk_window;
1565 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1566 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1567 dpy = GDK_WINDOW_XDISPLAY (gdk_window);
1569 data[0] = icon_location.x;
1570 data[1] = icon_location.y;
1571 data[2] = icon_location.width;
1572 data[3] = icon_location.height;
1574 XChangeProperty (dpy,
1575 GDK_WINDOW_XID (gdk_window),
1576 gdk_x11_get_xatom_by_name_for_display (
1577 gdk_window_get_display (gdk_window),
1578 "_NET_WM_ICON_GEOMETRY"),
1579 XA_CARDINAL, 32, PropModeReplace,
1580 (guchar *)&data, 4);
1582 gtk_window_set_skip_taskbar_hint (window, TRUE);
1583 gtk_window_iconify (window);
1586 /* Takes care of moving the window to the current workspace. */
1588 empathy_window_present_with_time (GtkWindow *window,
1591 GdkWindow *gdk_window;
1593 g_return_if_fail (GTK_IS_WINDOW (window));
1595 /* Move the window to the current workspace before trying to show it.
1596 * This is the behaviour people expect when clicking on the statusbar icon. */
1597 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1602 /* Has no effect if the WM has viewports, like compiz */
1603 gdk_x11_window_move_to_current_desktop (gdk_window);
1605 /* If window is still off-screen, hide it to force it to
1606 * reposition on the current workspace. */
1607 gtk_window_get_position (window, &x, &y);
1608 gtk_window_get_size (window, &w, &h);
1609 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1610 gtk_widget_hide (GTK_WIDGET (window));
1613 if (timestamp == GDK_CURRENT_TIME)
1614 gtk_window_present (window);
1616 gtk_window_present_with_time (window, timestamp);
1618 gtk_window_set_skip_taskbar_hint (window, FALSE);
1619 gtk_window_deiconify (window);
1623 empathy_window_present (GtkWindow *window)
1625 empathy_window_present_with_time (window, gtk_get_current_event_time ());
1629 empathy_get_toplevel_window (GtkWidget *widget)
1631 GtkWidget *toplevel;
1633 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1635 toplevel = gtk_widget_get_toplevel (widget);
1636 if (GTK_IS_WINDOW (toplevel) &&
1637 gtk_widget_is_toplevel (toplevel)) {
1638 return GTK_WINDOW (toplevel);
1644 /** empathy_make_absolute_url_len:
1648 * Same as #empathy_make_absolute_url but for a limited string length
1651 empathy_make_absolute_url_len (const gchar *url,
1654 g_return_val_if_fail (url != NULL, NULL);
1656 if (g_str_has_prefix (url, "ghelp:") ||
1657 g_str_has_prefix (url, "mailto:") ||
1658 strstr (url, ":/")) {
1659 return g_strndup (url, len);
1662 if (strstr (url, "@")) {
1663 return g_strdup_printf ("mailto:%.*s", len, url);
1666 return g_strdup_printf ("http://%.*s", len, url);
1669 /** empathy_make_absolute_url:
1672 * The URL opening code can't handle schemeless strings, so we try to be
1673 * smart and add http if there is no scheme or doesn't look like a mail
1674 * address. This should work in most cases, and let us click on strings
1675 * like "www.gnome.org".
1677 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1678 * g_free when your are done with it
1681 empathy_make_absolute_url (const gchar *url)
1683 return empathy_make_absolute_url_len (url, strlen (url));
1687 empathy_url_show (GtkWidget *parent,
1691 GError *error = NULL;
1693 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1694 g_return_if_fail (url != NULL);
1696 real_url = empathy_make_absolute_url (url);
1698 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1699 gtk_get_current_event_time (), &error);
1704 dialog = gtk_message_dialog_new (NULL, 0,
1705 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1706 _("Unable to open URI"));
1707 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1708 "%s", error->message);
1710 g_signal_connect (dialog, "response",
1711 G_CALLBACK (gtk_widget_destroy),
1713 gtk_window_present (GTK_WINDOW (dialog));
1715 g_clear_error (&error);
1722 empathy_send_file (EmpathyContact *contact, GFile *file)
1724 EmpathyFTFactory *factory;
1725 GtkRecentManager *manager;
1728 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1729 g_return_if_fail (G_IS_FILE (file));
1731 factory = empathy_ft_factory_dup_singleton ();
1733 empathy_ft_factory_new_transfer_outgoing (factory, contact, file);
1735 uri = g_file_get_uri (file);
1736 manager = gtk_recent_manager_get_default ();
1737 gtk_recent_manager_add_item (manager, uri);
1740 g_object_unref (factory);
1744 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1749 /* Only handle a single file for now. It would be wicked cool to be
1750 able to do multiple files, offering to zip them or whatever like
1751 nautilus-sendto does. Note that text/uri-list is defined to have
1752 each line terminated by \r\n, but we can be tolerant of applications
1753 that only use \n or don't terminate single-line entries.
1755 nl = strstr (uri_list, "\r\n");
1757 nl = strchr (uri_list, '\n');
1760 gchar *uri = g_strndup (uri_list, nl - uri_list);
1761 file = g_file_new_for_uri (uri);
1765 file = g_file_new_for_uri (uri_list);
1768 empathy_send_file (contact, file);
1770 g_object_unref (file);
1774 file_manager_send_file_response_cb (GtkDialog *widget,
1776 EmpathyContact *contact)
1780 if (response_id == GTK_RESPONSE_OK) {
1781 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1783 empathy_send_file (contact, file);
1785 g_object_unref (file);
1788 gtk_widget_destroy (GTK_WIDGET (widget));
1792 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1797 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1799 DEBUG ("Creating selection file chooser");
1801 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1803 GTK_FILE_CHOOSER_ACTION_OPEN,
1805 GTK_RESPONSE_CANCEL,
1809 button = gtk_button_new_with_mnemonic (_("_Send"));
1810 gtk_button_set_image (GTK_BUTTON (button),
1811 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1812 GTK_ICON_SIZE_BUTTON));
1813 gtk_widget_show (button);
1814 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1816 gtk_widget_set_can_default (button, TRUE);
1817 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1820 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1822 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1825 g_signal_connect (widget, "response",
1826 G_CALLBACK (file_manager_send_file_response_cb),
1829 gtk_widget_show (widget);
1833 file_manager_receive_file_response_cb (GtkDialog *dialog,
1834 GtkResponseType response,
1835 EmpathyFTHandler *handler)
1837 EmpathyFTFactory *factory;
1840 if (response == GTK_RESPONSE_OK) {
1843 guint64 free_space, file_size;
1844 GError *error = NULL;
1846 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1847 parent = g_file_get_parent (file);
1848 info = g_file_query_filesystem_info (parent,
1849 G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
1852 g_object_unref (parent);
1854 if (error != NULL) {
1855 g_warning ("Error: %s", error->message);
1857 g_object_unref (file);
1861 free_space = g_file_info_get_attribute_uint64 (info,
1862 G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
1863 file_size = empathy_ft_handler_get_total_bytes (handler);
1865 g_object_unref (info);
1867 if (file_size > free_space) {
1868 GtkWidget *message = gtk_message_dialog_new (
1869 GTK_WINDOW (dialog),
1873 _("Insufficient free space to save file"));
1874 char *file_size_str, *free_space_str;
1876 file_size_str = g_format_size_for_display (file_size);
1877 free_space_str = g_format_size_for_display (free_space);
1879 gtk_message_dialog_format_secondary_text (
1880 GTK_MESSAGE_DIALOG (message),
1881 _("%s of free space are required to save this "
1882 "file, but only %s is available. Please "
1883 "choose another location."),
1884 file_size_str, free_space_str);
1886 gtk_dialog_run (GTK_DIALOG (message));
1888 g_free (file_size_str);
1889 g_free (free_space_str);
1890 gtk_widget_destroy (message);
1892 g_object_unref (file);
1897 factory = empathy_ft_factory_dup_singleton ();
1899 empathy_ft_factory_set_destination_for_incoming_handler (
1900 factory, handler, file);
1902 g_object_unref (factory);
1903 g_object_unref (file);
1905 /* unref the handler, as we dismissed the file chooser,
1906 * and refused the transfer.
1908 g_object_unref (handler);
1911 gtk_widget_destroy (GTK_WIDGET (dialog));
1915 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1919 EmpathyContact *contact;
1922 contact = empathy_ft_handler_get_contact (handler);
1923 g_assert (contact != NULL);
1925 title = g_strdup_printf (_("Incoming file from %s"),
1926 empathy_contact_get_alias (contact));
1928 widget = gtk_file_chooser_dialog_new (title,
1930 GTK_FILE_CHOOSER_ACTION_SAVE,
1932 GTK_RESPONSE_CANCEL,
1936 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1937 empathy_ft_handler_get_filename (handler));
1938 gtk_file_chooser_set_do_overwrite_confirmation
1939 (GTK_FILE_CHOOSER (widget), TRUE);
1941 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1943 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1944 dir = g_get_home_dir ();
1946 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1948 g_signal_connect (widget, "response",
1949 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1951 gtk_widget_show (widget);
1956 empathy_make_color_whiter (GdkRGBA *color)
1958 const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
1960 color->red = (color->red + white.red) / 2;
1961 color->green = (color->green + white.green) / 2;
1962 color->blue = (color->blue + white.blue) / 2;
1966 menu_deactivate_cb (GtkMenu *menu,
1969 /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
1970 g_signal_handlers_disconnect_by_func (menu,
1971 menu_deactivate_cb, user_data);
1973 gtk_menu_detach (menu);
1976 /* Convenient function to create a GtkMenu attached to @attach_to and detach
1977 * it when the menu is not displayed any more. This is useful when creating a
1978 * context menu that we want to get rid as soon as it as been displayed. */
1980 empathy_context_menu_new (GtkWidget *attach_to)
1984 menu = gtk_menu_new ();
1986 gtk_menu_attach_to_widget (GTK_MENU (menu), attach_to, NULL);
1988 /* menu is initially unowned but gtk_menu_attach_to_widget () taked its
1989 * floating ref. We can either wait that @attach_to releases its ref when
1990 * it will be destroyed (when leaving Empathy most of the time) or explicitely
1991 * detach the menu when it's not displayed any more.
1992 * We go for the latter as we don't want to keep useless menus in memory
1993 * during the whole lifetime of Empathy. */
1994 g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb), NULL);