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-2008 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>
27 * Part of this file is copied from GtkSourceView (gtksourceiter.c):
35 #include <X11/Xatom.h>
37 #include <glib/gi18n-lib.h>
41 #include "empathy-ui-utils.h"
42 #include "empathy-images.h"
43 #include "empathy-smiley-manager.h"
44 #include "empathy-conf.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include <libempathy/empathy-debug.h>
48 #include <libempathy/empathy-utils.h>
49 #include <libempathy/empathy-dispatcher.h>
50 #include <libempathy/empathy-idle.h>
51 #include <libempathy/empathy-ft-factory.h>
54 empathy_gtk_init (void)
56 static gboolean initialized = FALSE;
62 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
63 PKGDATADIR G_DIR_SEPARATOR_S "icons");
69 builder_get_file_valist (const gchar *filename,
70 const gchar *first_object,
78 DEBUG ("Loading file %s", filename);
80 gui = gtk_builder_new ();
81 gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
82 if (!gtk_builder_add_from_file (gui, filename, &error)) {
83 g_critical ("GtkBuilder Error (%s): %s",
84 filename, error->message);
85 g_clear_error (&error);
88 /* we need to iterate and set all of the pointers to NULL */
89 for (name = first_object; name;
90 name = va_arg (args, const gchar *)) {
91 object_ptr = va_arg (args, GObject**);
99 for (name = first_object; name; name = va_arg (args, const gchar *)) {
100 object_ptr = va_arg (args, GObject**);
102 *object_ptr = gtk_builder_get_object (gui, name);
105 g_warning ("File is missing object '%s'.", name);
114 empathy_builder_get_file (const gchar *filename,
115 const gchar *first_object,
121 va_start (args, first_object);
122 gui = builder_get_file_valist (filename, first_object, args);
129 empathy_builder_connect (GtkBuilder *gui,
140 va_start (args, first_object);
141 for (name = first_object; name; name = va_arg (args, const gchar *)) {
142 sig = va_arg (args, const gchar *);
143 callback = va_arg (args, GCallback);
145 object = gtk_builder_get_object (gui, name);
147 g_warning ("File is missing object '%s'.", name);
151 g_signal_connect (object, sig, callback, user_data);
158 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
161 /* On construction gui sinks the initial reference to widget. When gui
162 * is finalized it will drop its ref to widget. We take our own ref to
163 * prevent widget being finalised. The widget is forced to have a
164 * floating reference, like when it was initially unowned so that it can
165 * be used like any other GtkWidget. */
167 g_object_ref (widget);
168 g_object_force_floating (G_OBJECT (widget));
169 g_object_unref (gui);
175 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
178 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
179 return EMPATHY_IMAGE_AVAILABLE;
180 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
181 return EMPATHY_IMAGE_BUSY;
182 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
183 return EMPATHY_IMAGE_AWAY;
184 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
185 return EMPATHY_IMAGE_EXT_AWAY;
186 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
187 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
188 EMPATHY_IMAGE_HIDDEN))
189 return EMPATHY_IMAGE_HIDDEN;
191 /* The 'hidden' icon is not an official one so we fallback to offline if
192 * it's not implemented */
193 return EMPATHY_IMAGE_OFFLINE;
194 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
195 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
196 return EMPATHY_IMAGE_OFFLINE;
197 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
198 return EMPATHY_IMAGE_PENDING;
199 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
207 empathy_icon_name_for_contact (EmpathyContact *contact)
209 TpConnectionPresenceType presence;
211 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
212 EMPATHY_IMAGE_OFFLINE);
214 presence = empathy_contact_get_presence (contact);
215 return empathy_icon_name_for_presence (presence);
219 empathy_protocol_name_for_contact (EmpathyContact *contact)
223 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
225 account = empathy_contact_get_account (contact);
226 if (account == NULL) {
230 return tp_account_get_icon_name (account);
234 empathy_pixbuf_from_data (gchar *data,
237 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
241 empathy_pixbuf_from_data_and_mime (gchar *data,
245 GdkPixbufLoader *loader;
246 GdkPixbufFormat *format;
247 GdkPixbuf *pixbuf = NULL;
249 GError *error = NULL;
255 loader = gdk_pixbuf_loader_new ();
256 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
257 DEBUG ("Failed to write to pixbuf loader: %s",
258 error ? error->message : "No error given");
261 if (!gdk_pixbuf_loader_close (loader, &error)) {
262 DEBUG ("Failed to close pixbuf loader: %s",
263 error ? error->message : "No error given");
267 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
269 g_object_ref (pixbuf);
271 if (mime_type != NULL) {
272 format = gdk_pixbuf_loader_get_format (loader);
273 mime_types = gdk_pixbuf_format_get_mime_types (format);
275 *mime_type = g_strdup (*mime_types);
276 if (mime_types[1] != NULL) {
277 DEBUG ("Loader supports more than one mime "
278 "type! Picking the first one, %s",
281 g_strfreev (mime_types);
286 g_clear_error (&error);
287 g_object_unref (loader);
295 gboolean preserve_aspect_ratio;
299 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
302 struct SizeData *data)
304 g_return_if_fail (width > 0 && height > 0);
306 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
307 if (width < data->width && height < data->height) {
312 if (data->width < 0) {
313 width = width * (double) data->height / (gdouble) height;
314 height = data->height;
315 } else if (data->height < 0) {
316 height = height * (double) data->width / (double) width;
318 } else if ((double) height * (double) data->width >
319 (double) width * (double) data->height) {
320 width = 0.5 + (double) width * (double) data->height / (double) height;
321 height = data->height;
323 height = 0.5 + (double) height * (double) data->width / (double) width;
327 if (data->width > 0) {
331 if (data->height > 0) {
332 height = data->height;
336 gdk_pixbuf_loader_set_size (loader, width, height);
340 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
342 gint width, height, rowstride;
345 width = gdk_pixbuf_get_width (pixbuf);
346 height = gdk_pixbuf_get_height (pixbuf);
347 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
348 pixels = gdk_pixbuf_get_pixels (pixbuf);
350 if (width < 6 || height < 6) {
358 pixels[rowstride + 3] = 0x80;
359 pixels[rowstride * 2 + 3] = 0xC0;
362 pixels[width * 4 - 1] = 0;
363 pixels[width * 4 - 5] = 0x80;
364 pixels[width * 4 - 9] = 0xC0;
365 pixels[rowstride + (width * 4) - 1] = 0x80;
366 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
369 pixels[(height - 1) * rowstride + 3] = 0;
370 pixels[(height - 1) * rowstride + 7] = 0x80;
371 pixels[(height - 1) * rowstride + 11] = 0xC0;
372 pixels[(height - 2) * rowstride + 3] = 0x80;
373 pixels[(height - 3) * rowstride + 3] = 0xC0;
376 pixels[height * rowstride - 1] = 0;
377 pixels[(height - 1) * rowstride - 1] = 0x80;
378 pixels[(height - 2) * rowstride - 1] = 0xC0;
379 pixels[height * rowstride - 5] = 0x80;
380 pixels[height * rowstride - 9] = 0xC0;
384 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
386 gint width, height, rowstride, i;
390 width = gdk_pixbuf_get_width (pixbuf);
391 height = gdk_pixbuf_get_height (pixbuf);
392 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
393 pixels = gdk_pixbuf_get_pixels (pixbuf);
396 for (i = 3; i < rowstride; i+=4) {
402 for (i = 1; i < height - 1; i++) {
403 row = pixels + (i*rowstride);
404 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
409 row = pixels + ((height-1) * rowstride);
410 for (i = 3; i < rowstride; i+=4) {
420 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
425 GdkPixbufLoader *loader;
426 struct SizeData data;
427 GError *error = NULL;
434 data.height = height;
435 data.preserve_aspect_ratio = TRUE;
437 loader = gdk_pixbuf_loader_new ();
439 g_signal_connect (loader, "size-prepared",
440 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
443 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
444 g_warning ("Couldn't write avatar image:%p with "
445 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
446 avatar->data, avatar->len, error->message);
447 g_error_free (error);
451 gdk_pixbuf_loader_close (loader, NULL);
453 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
454 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
455 GdkPixbuf *rounded_pixbuf;
457 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
458 gdk_pixbuf_get_width (pixbuf),
459 gdk_pixbuf_get_height (pixbuf));
460 gdk_pixbuf_copy_area (pixbuf, 0, 0,
461 gdk_pixbuf_get_width (pixbuf),
462 gdk_pixbuf_get_height (pixbuf),
465 pixbuf = rounded_pixbuf;
467 g_object_ref (pixbuf);
470 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
471 empathy_avatar_pixbuf_roundify (pixbuf);
474 g_object_unref (loader);
480 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
484 EmpathyAvatar *avatar;
486 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
488 avatar = empathy_contact_get_avatar (contact);
490 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
494 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
495 gboolean show_protocol)
497 const gchar *icon_name;
499 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
501 icon_name = empathy_icon_name_for_contact (contact);
503 if (icon_name == NULL) {
506 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
512 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
513 const gchar *icon_name,
514 gboolean show_protocol)
516 GdkPixbuf *pix_status;
517 GdkPixbuf *pix_protocol;
518 gchar *icon_filename;
520 gint numerator, denominator;
522 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
523 g_return_val_if_fail (icon_name != NULL, NULL);
528 icon_filename = empathy_filename_from_icon_name (icon_name,
530 if (icon_filename == NULL) {
531 DEBUG ("icon name: %s could not be found\n", icon_name);
535 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
537 g_free (icon_filename);
539 if (pix_status == NULL) {
540 DEBUG ("Could not open icon %s\n", icon_filename);
547 height = gdk_pixbuf_get_height (pix_status);
548 width = gdk_pixbuf_get_width (pix_status);
550 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
551 width * numerator / denominator,
552 height * numerator / denominator);
554 if (pix_protocol == NULL) {
557 gdk_pixbuf_composite (pix_protocol, pix_status,
558 0, height - height * numerator / denominator,
559 width * numerator / denominator, height * numerator / denominator,
560 0, height - height * numerator / denominator,
562 GDK_INTERP_BILINEAR, 255);
564 g_object_unref (pix_protocol);
570 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
576 GdkPixbuf *pixbuf = NULL;
578 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
580 account = empathy_contact_get_account (contact);
581 filename = empathy_filename_from_icon_name (tp_account_get_icon_name (account),
583 if (filename != NULL) {
584 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
592 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
597 width = gdk_pixbuf_get_width (pixbuf);
598 height = gdk_pixbuf_get_height (pixbuf);
600 if (width > 0 && (width > max_size || height > max_size)) {
601 factor = (gdouble) max_size / MAX (width, height);
603 width = width * factor;
604 height = height * factor;
606 return gdk_pixbuf_scale_simple (pixbuf,
611 return g_object_ref (pixbuf);
615 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
620 GError *error = NULL;
626 theme = gtk_icon_theme_get_default ();
628 pixbuf = gtk_icon_theme_load_icon (theme,
634 DEBUG ("Error loading icon: %s", error->message);
635 g_clear_error (&error);
642 empathy_pixbuf_from_icon_name (const gchar *icon_name,
643 GtkIconSize icon_size)
652 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
656 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
660 empathy_filename_from_icon_name (const gchar *icon_name,
661 GtkIconSize icon_size)
663 GtkIconTheme *icon_theme;
664 GtkIconInfo *icon_info;
669 icon_theme = gtk_icon_theme_get_default ();
671 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
675 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
676 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
677 gtk_icon_info_free (icon_info);
682 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
683 * that to make it easier to apply changes from the original code.
685 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
687 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
688 * decomposable character it consumes the decomposition length from the given
689 * offset. So it's useful when the offset was calculated for the normalized
690 * version of str, but we need a pointer to str itself. */
692 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
694 gchar *casefold, *normal;
700 q = g_utf8_next_char (p);
701 casefold = g_utf8_casefold (p, q - p);
702 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
703 offset -= g_utf8_strlen (normal, -1);
712 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
716 const gchar *ret = NULL;
719 gchar *caseless_haystack;
722 g_return_val_if_fail (haystack != NULL, NULL);
723 g_return_val_if_fail (needle != NULL, NULL);
725 casefold = g_utf8_casefold (haystack, -1);
726 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
729 needle_len = g_utf8_strlen (needle, -1);
730 haystack_len = g_utf8_strlen (caseless_haystack, -1);
734 ret = (gchar *) haystack;
738 if (haystack_len < needle_len)
744 p = (gchar *) caseless_haystack;
745 needle_len = strlen (needle);
750 if ((strncmp (p, needle, needle_len) == 0))
752 ret = pointer_from_offset_skipping_decomp (haystack, i);
756 p = g_utf8_next_char (p);
761 g_free (caseless_haystack);
767 g_utf8_caselessnmatch (const char *s1, const char *s2,
768 gssize n1, gssize n2)
771 gchar *normalized_s1;
772 gchar *normalized_s2;
775 gboolean ret = FALSE;
777 g_return_val_if_fail (s1 != NULL, FALSE);
778 g_return_val_if_fail (s2 != NULL, FALSE);
779 g_return_val_if_fail (n1 > 0, FALSE);
780 g_return_val_if_fail (n2 > 0, FALSE);
782 casefold = g_utf8_casefold (s1, n1);
783 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
786 casefold = g_utf8_casefold (s2, n2);
787 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
790 len_s1 = strlen (normalized_s1);
791 len_s2 = strlen (normalized_s2);
796 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
799 g_free (normalized_s1);
800 g_free (normalized_s2);
806 forward_chars_with_skipping (GtkTextIter *iter,
808 gboolean skip_invisible,
809 gboolean skip_nontext,
810 gboolean skip_decomp)
814 g_return_if_fail (count >= 0);
820 gboolean ignored = FALSE;
822 /* minimal workaround to avoid the infinite loop of bug #168247.
823 * It doesn't fix the problemjust the symptom...
825 if (gtk_text_iter_is_end (iter))
828 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
831 if (!ignored && skip_invisible &&
832 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
835 if (!ignored && skip_decomp)
837 /* being UTF8 correct sucks; this accounts for extra
838 offsets coming from canonical decompositions of
839 UTF8 characters (e.g. accented characters) which
840 g_utf8_normalize () performs */
845 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
846 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
847 i -= (g_utf8_strlen (normal, -1) - 1);
851 gtk_text_iter_forward_char (iter);
859 lines_match (const GtkTextIter *start,
861 gboolean visible_only,
863 GtkTextIter *match_start,
864 GtkTextIter *match_end)
871 if (*lines == NULL || **lines == '\0')
874 *match_start = *start;
881 gtk_text_iter_forward_line (&next);
883 /* No more text in buffer, but *lines is nonempty */
884 if (gtk_text_iter_equal (start, &next))
890 line_text = gtk_text_iter_get_visible_slice (start, &next);
892 line_text = gtk_text_iter_get_slice (start, &next);
897 line_text = gtk_text_iter_get_visible_text (start, &next);
899 line_text = gtk_text_iter_get_text (start, &next);
902 if (match_start) /* if this is the first line we're matching */
904 found = g_utf8_strcasestr (line_text, *lines);
908 /* If it's not the first line, we have to match from the
911 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
924 /* Get offset to start of search string */
925 offset = g_utf8_strlen (line_text, found - line_text);
929 /* If match start needs to be returned, set it to the
930 * start of the search string.
932 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
938 /* Go to end of search string */
939 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
948 /* pass NULL for match_start, since we don't need to find the
951 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
954 /* strsplit () that retains the delimiter as part of the string. */
956 strbreakup (const char *string,
957 const char *delimiter,
960 GSList *string_list = NULL, *slist;
961 gchar **str_array, *s, *casefold, *new_string;
964 g_return_val_if_fail (string != NULL, NULL);
965 g_return_val_if_fail (delimiter != NULL, NULL);
968 max_tokens = G_MAXINT;
970 s = strstr (string, delimiter);
973 guint delimiter_len = strlen (delimiter);
979 len = s - string + delimiter_len;
980 new_string = g_new (gchar, len + 1);
981 strncpy (new_string, string, len);
983 casefold = g_utf8_casefold (new_string, -1);
985 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
987 string_list = g_slist_prepend (string_list, new_string);
989 string = s + delimiter_len;
990 s = strstr (string, delimiter);
991 } while (--max_tokens && s);
997 casefold = g_utf8_casefold (string, -1);
998 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1000 string_list = g_slist_prepend (string_list, new_string);
1003 str_array = g_new (gchar*, n);
1007 str_array[i--] = NULL;
1008 for (slist = string_list; slist; slist = slist->next)
1009 str_array[i--] = slist->data;
1011 g_slist_free (string_list);
1017 empathy_text_iter_forward_search (const GtkTextIter *iter,
1019 GtkTextIter *match_start,
1020 GtkTextIter *match_end,
1021 const GtkTextIter *limit)
1023 gchar **lines = NULL;
1025 gboolean retval = FALSE;
1027 gboolean visible_only;
1030 g_return_val_if_fail (iter != NULL, FALSE);
1031 g_return_val_if_fail (str != NULL, FALSE);
1033 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
1037 /* If we can move one char, return the empty string there */
1040 if (gtk_text_iter_forward_char (&match)) {
1041 if (limit && gtk_text_iter_equal (&match, limit)) {
1046 *match_start = match;
1057 visible_only = TRUE;
1060 /* locate all lines */
1061 lines = strbreakup (str, "\n", -1);
1066 /* This loop has an inefficient worst-case, where
1067 * gtk_text_iter_get_text () is called repeatedly on
1072 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1076 if (lines_match (&search, (const gchar**)lines,
1077 visible_only, slice, &match, &end)) {
1078 if (limit == NULL ||
1079 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1083 *match_start = match;
1091 } while (gtk_text_iter_forward_line (&search));
1093 g_strfreev ((gchar **) lines);
1098 static const gchar *
1099 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1103 const gchar *ret = NULL;
1106 gchar *caseless_haystack;
1109 g_return_val_if_fail (haystack != NULL, NULL);
1110 g_return_val_if_fail (needle != NULL, NULL);
1112 casefold = g_utf8_casefold (haystack, -1);
1113 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1116 needle_len = g_utf8_strlen (needle, -1);
1117 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1119 if (needle_len == 0)
1121 ret = (gchar *) haystack;
1125 if (haystack_len < needle_len)
1131 i = haystack_len - needle_len;
1132 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1133 needle_len = strlen (needle);
1135 while (p >= caseless_haystack)
1137 if (strncmp (p, needle, needle_len) == 0)
1139 ret = pointer_from_offset_skipping_decomp (haystack, i);
1143 p = g_utf8_prev_char (p);
1148 g_free (caseless_haystack);
1154 backward_lines_match (const GtkTextIter *start,
1155 const gchar **lines,
1156 gboolean visible_only,
1158 GtkTextIter *match_start,
1159 GtkTextIter *match_end)
1161 GtkTextIter line, next;
1166 if (*lines == NULL || **lines == '\0')
1169 *match_start = *start;
1171 *match_end = *start;
1175 line = next = *start;
1176 if (gtk_text_iter_get_line_offset (&next) == 0)
1178 if (!gtk_text_iter_backward_line (&next))
1182 gtk_text_iter_set_line_offset (&next, 0);
1187 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1189 line_text = gtk_text_iter_get_slice (&next, &line);
1194 line_text = gtk_text_iter_get_visible_text (&next, &line);
1196 line_text = gtk_text_iter_get_text (&next, &line);
1199 if (match_start) /* if this is the first line we're matching */
1201 found = g_utf8_strrcasestr (line_text, *lines);
1205 /* If it's not the first line, we have to match from the
1206 * start of the line.
1208 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1221 /* Get offset to start of search string */
1222 offset = g_utf8_strlen (line_text, found - line_text);
1224 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1226 /* If match start needs to be returned, set it to the
1227 * start of the search string.
1231 *match_start = next;
1234 /* Go to end of search string */
1235 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1244 /* try to match the rest of the lines forward, passing NULL
1245 * for match_start so lines_match will try to match the entire
1247 return lines_match (&next, lines, visible_only,
1248 slice, NULL, match_end);
1252 empathy_text_iter_backward_search (const GtkTextIter *iter,
1254 GtkTextIter *match_start,
1255 GtkTextIter *match_end,
1256 const GtkTextIter *limit)
1258 gchar **lines = NULL;
1260 gboolean retval = FALSE;
1262 gboolean visible_only;
1265 g_return_val_if_fail (iter != NULL, FALSE);
1266 g_return_val_if_fail (str != NULL, FALSE);
1268 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1273 /* If we can move one char, return the empty string there */
1276 if (gtk_text_iter_backward_char (&match))
1278 if (limit && gtk_text_iter_equal (&match, limit))
1282 *match_start = match;
1293 visible_only = TRUE;
1296 /* locate all lines */
1297 lines = strbreakup (str, "\n", -1);
1303 /* This loop has an inefficient worst-case, where
1304 * gtk_text_iter_get_text () is called repeatedly on
1309 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1312 if (backward_lines_match (&search, (const gchar**)lines,
1313 visible_only, slice, &match, &end))
1315 if (limit == NULL || (limit &&
1316 gtk_text_iter_compare (&end, limit) > 0))
1321 *match_start = match;
1328 if (gtk_text_iter_get_line_offset (&search) == 0)
1330 if (!gtk_text_iter_backward_line (&search))
1335 gtk_text_iter_set_line_offset (&search, 0);
1339 g_strfreev ((gchar **) lines);
1345 empathy_window_get_is_visible (GtkWindow *window)
1347 GdkWindowState state;
1348 GdkWindow *gdk_window;
1350 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1352 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1357 state = gdk_window_get_state (gdk_window);
1358 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1366 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1368 GdkRectangle icon_location;
1371 GdkWindow *gdk_window;
1373 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1374 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1375 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1377 data[0] = icon_location.x;
1378 data[1] = icon_location.y;
1379 data[2] = icon_location.width;
1380 data[3] = icon_location.height;
1382 XChangeProperty (dpy,
1383 GDK_WINDOW_XID (gdk_window),
1384 gdk_x11_get_xatom_by_name_for_display (
1385 gdk_drawable_get_display (gdk_window),
1386 "_NET_WM_ICON_GEOMETRY"),
1387 XA_CARDINAL, 32, PropModeReplace,
1388 (guchar *)&data, 4);
1390 gtk_window_set_skip_taskbar_hint (window, TRUE);
1391 gtk_window_iconify (window);
1394 /* Takes care of moving the window to the current workspace. */
1396 empathy_window_present_with_time (GtkWindow *window,
1399 GdkWindow *gdk_window;
1401 g_return_if_fail (GTK_IS_WINDOW (window));
1403 /* Move the window to the current workspace before trying to show it.
1404 * This is the behaviour people expect when clicking on the statusbar icon. */
1405 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1410 /* Has no effect if the WM has viewports, like compiz */
1411 gdk_x11_window_move_to_current_desktop (gdk_window);
1413 /* If window is still off-screen, hide it to force it to
1414 * reposition on the current workspace. */
1415 gtk_window_get_position (window, &x, &y);
1416 gtk_window_get_size (window, &w, &h);
1417 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1418 gtk_widget_hide (GTK_WIDGET (window));
1421 gtk_window_present_with_time (window, timestamp);
1422 gtk_window_set_skip_taskbar_hint (window, FALSE);
1423 gtk_window_deiconify (window);
1427 empathy_window_present (GtkWindow *window)
1429 empathy_window_present_with_time (window, GDK_CURRENT_TIME);
1433 empathy_get_toplevel_window (GtkWidget *widget)
1435 GtkWidget *toplevel;
1437 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1439 toplevel = gtk_widget_get_toplevel (widget);
1440 if (GTK_IS_WINDOW (toplevel) &&
1441 gtk_widget_is_toplevel (toplevel)) {
1442 return GTK_WINDOW (toplevel);
1448 /** empathy_make_absolute_url_len:
1452 * Same as #empathy_make_absolute_url but for a limited string length
1455 empathy_make_absolute_url_len (const gchar *url,
1458 g_return_val_if_fail (url != NULL, NULL);
1460 if (g_str_has_prefix (url, "ghelp:") ||
1461 g_str_has_prefix (url, "mailto:") ||
1462 strstr (url, ":/")) {
1463 return g_strndup (url, len);
1466 if (strstr (url, "@")) {
1467 return g_strdup_printf ("mailto:%.*s", len, url);
1470 return g_strdup_printf ("http://%.*s", len, url);
1473 /** empathy_make_absolute_url:
1476 * The URL opening code can't handle schemeless strings, so we try to be
1477 * smart and add http if there is no scheme or doesn't look like a mail
1478 * address. This should work in most cases, and let us click on strings
1479 * like "www.gnome.org".
1481 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1482 * g_free when your are done with it
1485 empathy_make_absolute_url (const gchar *url)
1487 return empathy_make_absolute_url_len (url, strlen (url));
1491 empathy_url_show (GtkWidget *parent,
1495 GError *error = NULL;
1497 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1498 g_return_if_fail (url != NULL);
1500 real_url = empathy_make_absolute_url (url);
1502 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1503 gtk_get_current_event_time (), &error);
1508 dialog = gtk_message_dialog_new (NULL, 0,
1509 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1510 _("Unable to open URI"));
1511 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1512 "%s", error->message);
1514 g_signal_connect (dialog, "response",
1515 G_CALLBACK (gtk_widget_destroy),
1517 gtk_window_present (GTK_WINDOW (dialog));
1519 g_clear_error (&error);
1526 link_button_hook (GtkLinkButton *button,
1530 empathy_url_show (GTK_WIDGET (button), link);
1534 empathy_link_button_new (const gchar *url,
1537 static gboolean hook = FALSE;
1541 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1544 return gtk_link_button_new_with_label (url, title);
1548 empathy_send_file (EmpathyContact *contact, GFile *file)
1550 EmpathyFTFactory *factory;
1551 GtkRecentManager *manager;
1554 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1555 g_return_if_fail (G_IS_FILE (file));
1557 factory = empathy_ft_factory_dup_singleton ();
1559 empathy_ft_factory_new_transfer_outgoing (factory, contact, file);
1561 uri = g_file_get_uri (file);
1562 manager = gtk_recent_manager_get_default ();
1563 gtk_recent_manager_add_item (manager, uri);
1566 g_object_unref (factory);
1570 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1575 /* Only handle a single file for now. It would be wicked cool to be
1576 able to do multiple files, offering to zip them or whatever like
1577 nautilus-sendto does. Note that text/uri-list is defined to have
1578 each line terminated by \r\n, but we can be tolerant of applications
1579 that only use \n or don't terminate single-line entries.
1581 nl = strstr (uri_list, "\r\n");
1583 nl = strchr (uri_list, '\n');
1586 gchar *uri = g_strndup (uri_list, nl - uri_list);
1587 file = g_file_new_for_uri (uri);
1591 file = g_file_new_for_uri (uri_list);
1594 empathy_send_file (contact, file);
1596 g_object_unref (file);
1600 file_manager_send_file_response_cb (GtkDialog *widget,
1602 EmpathyContact *contact)
1606 if (response_id == GTK_RESPONSE_OK) {
1607 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1609 empathy_send_file (contact, file);
1611 g_object_unref (file);
1614 gtk_widget_destroy (GTK_WIDGET (widget));
1618 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1623 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1625 DEBUG ("Creating selection file chooser");
1627 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1629 GTK_FILE_CHOOSER_ACTION_OPEN,
1631 GTK_RESPONSE_CANCEL,
1635 button = gtk_button_new_with_mnemonic (_("_Send"));
1636 gtk_button_set_image (GTK_BUTTON (button),
1637 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1638 GTK_ICON_SIZE_BUTTON));
1639 gtk_widget_show (button);
1640 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1642 gtk_widget_set_can_default (button, TRUE);
1643 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1646 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1648 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1651 g_signal_connect (widget, "response",
1652 G_CALLBACK (file_manager_send_file_response_cb),
1655 gtk_widget_show (widget);
1659 file_manager_receive_file_response_cb (GtkDialog *dialog,
1660 GtkResponseType response,
1661 EmpathyFTHandler *handler)
1663 EmpathyFTFactory *factory;
1666 if (response == GTK_RESPONSE_OK) {
1667 factory = empathy_ft_factory_dup_singleton ();
1668 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1670 empathy_ft_factory_set_destination_for_incoming_handler
1671 (factory, handler, file);
1673 g_object_unref (factory);
1674 g_object_unref (file);
1676 /* unref the handler, as we dismissed the file chooser,
1677 * and refused the transfer.
1679 g_object_unref (handler);
1682 gtk_widget_destroy (GTK_WIDGET (dialog));
1686 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1691 widget = gtk_file_chooser_dialog_new (_("Select a destination"),
1693 GTK_FILE_CHOOSER_ACTION_SAVE,
1695 GTK_RESPONSE_CANCEL,
1699 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1700 empathy_ft_handler_get_filename (handler));
1701 gtk_file_chooser_set_do_overwrite_confirmation
1702 (GTK_FILE_CHOOSER (widget), TRUE);
1704 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1706 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1707 dir = g_get_home_dir ();
1709 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1711 g_signal_connect (widget, "response",
1712 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1714 gtk_widget_show (widget);