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>
53 #define SCHEMES "([a-zA-Z\\+]+)"
54 #define INVALID_CHARS " \n\"'"
55 #define INVALID_CHARS_EXT INVALID_CHARS "\\[\\]<>(){},;:?"
56 #define BODY "([^"INVALID_CHARS"]+)"
57 #define BODY_END "([^"INVALID_CHARS"]*)[^"INVALID_CHARS_EXT".]"
58 #define BODY_STRICT "([^"INVALID_CHARS_EXT"]+)"
59 #define URI_REGEX "("SCHEMES"://"BODY_END")" \
60 "|((www|ftp)\\."BODY_END")" \
61 "|((mailto:)?"BODY_STRICT"@"BODY"\\."BODY_END")"
64 empathy_gtk_init (void)
66 static gboolean initialized = FALSE;
72 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
73 PKGDATADIR G_DIR_SEPARATOR_S "icons");
79 empathy_uri_regex_dup_singleton (void)
81 static GRegex *uri_regex = NULL;
83 /* We intentionally leak the regex so it's not recomputed */
85 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
88 return g_regex_ref (uri_regex);
92 builder_get_file_valist (const gchar *filename,
93 const gchar *first_object,
101 DEBUG ("Loading file %s", filename);
103 gui = gtk_builder_new ();
104 if (!gtk_builder_add_from_file (gui, filename, &error)) {
105 g_critical ("GtkBuilder Error (%s): %s",
106 filename, error->message);
107 g_clear_error (&error);
108 g_object_unref (gui);
110 /* we need to iterate and set all of the pointers to NULL */
111 for (name = first_object; name;
112 name = va_arg (args, const gchar *)) {
113 object_ptr = va_arg (args, GObject**);
121 for (name = first_object; name; name = va_arg (args, const gchar *)) {
122 object_ptr = va_arg (args, GObject**);
124 *object_ptr = gtk_builder_get_object (gui, name);
127 g_warning ("File is missing object '%s'.", name);
136 empathy_builder_get_file (const gchar *filename,
137 const gchar *first_object,
143 va_start (args, first_object);
144 gui = builder_get_file_valist (filename, first_object, args);
151 empathy_builder_connect (GtkBuilder *gui,
162 va_start (args, first_object);
163 for (name = first_object; name; name = va_arg (args, const gchar *)) {
164 sig = va_arg (args, const gchar *);
165 callback = va_arg (args, GCallback);
167 object = gtk_builder_get_object (gui, name);
169 g_warning ("File is missing object '%s'.", name);
173 g_signal_connect (object, sig, callback, user_data);
180 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
183 /* On construction gui sinks the initial reference to widget. When gui
184 * is finalized it will drop its ref to widget. We take our own ref to
185 * prevent widget being finalised. The widget is forced to have a
186 * floating reference, like when it was initially unowned so that it can
187 * be used like any other GtkWidget. */
189 g_object_ref (widget);
190 g_object_force_floating (G_OBJECT (widget));
191 g_object_unref (gui);
197 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
200 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
201 return EMPATHY_IMAGE_AVAILABLE;
202 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
203 return EMPATHY_IMAGE_BUSY;
204 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
205 return EMPATHY_IMAGE_AWAY;
206 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
207 return EMPATHY_IMAGE_EXT_AWAY;
208 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
209 return EMPATHY_IMAGE_HIDDEN;
210 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
211 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
212 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
213 return EMPATHY_IMAGE_OFFLINE;
214 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
222 empathy_icon_name_for_contact (EmpathyContact *contact)
224 TpConnectionPresenceType presence;
226 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
227 EMPATHY_IMAGE_OFFLINE);
229 presence = empathy_contact_get_presence (contact);
230 return empathy_icon_name_for_presence (presence);
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_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
499 width = gdk_pixbuf_get_width (pixbuf);
500 height = gdk_pixbuf_get_height (pixbuf);
502 if (width > 0 && (width > max_size || height > max_size)) {
503 factor = (gdouble) max_size / MAX (width, height);
505 width = width * factor;
506 height = height * factor;
508 return gdk_pixbuf_scale_simple (pixbuf,
513 return g_object_ref (pixbuf);
517 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
522 GError *error = NULL;
528 theme = gtk_icon_theme_get_default ();
530 pixbuf = gtk_icon_theme_load_icon (theme,
536 DEBUG ("Error loading icon: %s", error->message);
537 g_clear_error (&error);
544 empathy_pixbuf_from_icon_name (const gchar *icon_name,
545 GtkIconSize icon_size)
554 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
558 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
562 empathy_filename_from_icon_name (const gchar *icon_name,
563 GtkIconSize icon_size)
565 GtkIconTheme *icon_theme;
566 GtkIconInfo *icon_info;
571 icon_theme = gtk_icon_theme_get_default ();
573 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
577 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
578 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
579 gtk_icon_info_free (icon_info);
584 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
585 * that to make it easier to apply changes from the original code.
587 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
589 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
590 * decomposable character it consumes the decomposition length from the given
591 * offset. So it's useful when the offset was calculated for the normalized
592 * version of str, but we need a pointer to str itself. */
594 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
596 gchar *casefold, *normal;
602 q = g_utf8_next_char (p);
603 casefold = g_utf8_casefold (p, q - p);
604 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
605 offset -= g_utf8_strlen (normal, -1);
614 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
618 const gchar *ret = NULL;
621 gchar *caseless_haystack;
624 g_return_val_if_fail (haystack != NULL, NULL);
625 g_return_val_if_fail (needle != NULL, NULL);
627 casefold = g_utf8_casefold (haystack, -1);
628 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
631 needle_len = g_utf8_strlen (needle, -1);
632 haystack_len = g_utf8_strlen (caseless_haystack, -1);
636 ret = (gchar *) haystack;
640 if (haystack_len < needle_len)
646 p = (gchar *) caseless_haystack;
647 needle_len = strlen (needle);
652 if ((strncmp (p, needle, needle_len) == 0))
654 ret = pointer_from_offset_skipping_decomp (haystack, i);
658 p = g_utf8_next_char (p);
663 g_free (caseless_haystack);
669 g_utf8_caselessnmatch (const char *s1, const char *s2,
670 gssize n1, gssize n2)
673 gchar *normalized_s1;
674 gchar *normalized_s2;
677 gboolean ret = FALSE;
679 g_return_val_if_fail (s1 != NULL, FALSE);
680 g_return_val_if_fail (s2 != NULL, FALSE);
681 g_return_val_if_fail (n1 > 0, FALSE);
682 g_return_val_if_fail (n2 > 0, FALSE);
684 casefold = g_utf8_casefold (s1, n1);
685 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
688 casefold = g_utf8_casefold (s2, n2);
689 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
692 len_s1 = strlen (normalized_s1);
693 len_s2 = strlen (normalized_s2);
698 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
701 g_free (normalized_s1);
702 g_free (normalized_s2);
708 forward_chars_with_skipping (GtkTextIter *iter,
710 gboolean skip_invisible,
711 gboolean skip_nontext,
712 gboolean skip_decomp)
716 g_return_if_fail (count >= 0);
722 gboolean ignored = FALSE;
724 /* minimal workaround to avoid the infinite loop of bug #168247.
725 * It doesn't fix the problemjust the symptom...
727 if (gtk_text_iter_is_end (iter))
730 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
733 if (!ignored && skip_invisible &&
734 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
737 if (!ignored && skip_decomp)
739 /* being UTF8 correct sucks; this accounts for extra
740 offsets coming from canonical decompositions of
741 UTF8 characters (e.g. accented characters) which
742 g_utf8_normalize () performs */
747 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
748 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
749 i -= (g_utf8_strlen (normal, -1) - 1);
753 gtk_text_iter_forward_char (iter);
761 lines_match (const GtkTextIter *start,
763 gboolean visible_only,
765 GtkTextIter *match_start,
766 GtkTextIter *match_end)
773 if (*lines == NULL || **lines == '\0')
776 *match_start = *start;
783 gtk_text_iter_forward_line (&next);
785 /* No more text in buffer, but *lines is nonempty */
786 if (gtk_text_iter_equal (start, &next))
792 line_text = gtk_text_iter_get_visible_slice (start, &next);
794 line_text = gtk_text_iter_get_slice (start, &next);
799 line_text = gtk_text_iter_get_visible_text (start, &next);
801 line_text = gtk_text_iter_get_text (start, &next);
804 if (match_start) /* if this is the first line we're matching */
806 found = g_utf8_strcasestr (line_text, *lines);
810 /* If it's not the first line, we have to match from the
813 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
826 /* Get offset to start of search string */
827 offset = g_utf8_strlen (line_text, found - line_text);
831 /* If match start needs to be returned, set it to the
832 * start of the search string.
834 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
840 /* Go to end of search string */
841 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
850 /* pass NULL for match_start, since we don't need to find the
853 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
856 /* strsplit () that retains the delimiter as part of the string. */
858 strbreakup (const char *string,
859 const char *delimiter,
862 GSList *string_list = NULL, *slist;
863 gchar **str_array, *s, *casefold, *new_string;
866 g_return_val_if_fail (string != NULL, NULL);
867 g_return_val_if_fail (delimiter != NULL, NULL);
870 max_tokens = G_MAXINT;
872 s = strstr (string, delimiter);
875 guint delimiter_len = strlen (delimiter);
881 len = s - string + delimiter_len;
882 new_string = g_new (gchar, len + 1);
883 strncpy (new_string, string, len);
885 casefold = g_utf8_casefold (new_string, -1);
887 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
889 string_list = g_slist_prepend (string_list, new_string);
891 string = s + delimiter_len;
892 s = strstr (string, delimiter);
893 } while (--max_tokens && s);
899 casefold = g_utf8_casefold (string, -1);
900 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
902 string_list = g_slist_prepend (string_list, new_string);
905 str_array = g_new (gchar*, n);
909 str_array[i--] = NULL;
910 for (slist = string_list; slist; slist = slist->next)
911 str_array[i--] = slist->data;
913 g_slist_free (string_list);
919 empathy_text_iter_forward_search (const GtkTextIter *iter,
921 GtkTextIter *match_start,
922 GtkTextIter *match_end,
923 const GtkTextIter *limit)
925 gchar **lines = NULL;
927 gboolean retval = FALSE;
929 gboolean visible_only;
932 g_return_val_if_fail (iter != NULL, FALSE);
933 g_return_val_if_fail (str != NULL, FALSE);
935 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
939 /* If we can move one char, return the empty string there */
942 if (gtk_text_iter_forward_char (&match)) {
943 if (limit && gtk_text_iter_equal (&match, limit)) {
948 *match_start = match;
962 /* locate all lines */
963 lines = strbreakup (str, "\n", -1);
968 /* This loop has an inefficient worst-case, where
969 * gtk_text_iter_get_text () is called repeatedly on
974 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
978 if (lines_match (&search, (const gchar**)lines,
979 visible_only, slice, &match, &end)) {
981 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
985 *match_start = match;
993 } while (gtk_text_iter_forward_line (&search));
995 g_strfreev ((gchar **) lines);
1000 static const gchar *
1001 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1005 const gchar *ret = NULL;
1008 gchar *caseless_haystack;
1011 g_return_val_if_fail (haystack != NULL, NULL);
1012 g_return_val_if_fail (needle != NULL, NULL);
1014 casefold = g_utf8_casefold (haystack, -1);
1015 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1018 needle_len = g_utf8_strlen (needle, -1);
1019 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1021 if (needle_len == 0)
1023 ret = (gchar *) haystack;
1027 if (haystack_len < needle_len)
1033 i = haystack_len - needle_len;
1034 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1035 needle_len = strlen (needle);
1037 while (p >= caseless_haystack)
1039 if (strncmp (p, needle, needle_len) == 0)
1041 ret = pointer_from_offset_skipping_decomp (haystack, i);
1045 p = g_utf8_prev_char (p);
1050 g_free (caseless_haystack);
1056 backward_lines_match (const GtkTextIter *start,
1057 const gchar **lines,
1058 gboolean visible_only,
1060 GtkTextIter *match_start,
1061 GtkTextIter *match_end)
1063 GtkTextIter line, next;
1068 if (*lines == NULL || **lines == '\0')
1071 *match_start = *start;
1073 *match_end = *start;
1077 line = next = *start;
1078 if (gtk_text_iter_get_line_offset (&next) == 0)
1080 if (!gtk_text_iter_backward_line (&next))
1084 gtk_text_iter_set_line_offset (&next, 0);
1089 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1091 line_text = gtk_text_iter_get_slice (&next, &line);
1096 line_text = gtk_text_iter_get_visible_text (&next, &line);
1098 line_text = gtk_text_iter_get_text (&next, &line);
1101 if (match_start) /* if this is the first line we're matching */
1103 found = g_utf8_strrcasestr (line_text, *lines);
1107 /* If it's not the first line, we have to match from the
1108 * start of the line.
1110 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1123 /* Get offset to start of search string */
1124 offset = g_utf8_strlen (line_text, found - line_text);
1126 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1128 /* If match start needs to be returned, set it to the
1129 * start of the search string.
1133 *match_start = next;
1136 /* Go to end of search string */
1137 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1146 /* try to match the rest of the lines forward, passing NULL
1147 * for match_start so lines_match will try to match the entire
1149 return lines_match (&next, lines, visible_only,
1150 slice, NULL, match_end);
1154 empathy_text_iter_backward_search (const GtkTextIter *iter,
1156 GtkTextIter *match_start,
1157 GtkTextIter *match_end,
1158 const GtkTextIter *limit)
1160 gchar **lines = NULL;
1162 gboolean retval = FALSE;
1164 gboolean visible_only;
1167 g_return_val_if_fail (iter != NULL, FALSE);
1168 g_return_val_if_fail (str != NULL, FALSE);
1170 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1175 /* If we can move one char, return the empty string there */
1178 if (gtk_text_iter_backward_char (&match))
1180 if (limit && gtk_text_iter_equal (&match, limit))
1184 *match_start = match;
1195 visible_only = TRUE;
1198 /* locate all lines */
1199 lines = strbreakup (str, "\n", -1);
1205 /* This loop has an inefficient worst-case, where
1206 * gtk_text_iter_get_text () is called repeatedly on
1211 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1214 if (backward_lines_match (&search, (const gchar**)lines,
1215 visible_only, slice, &match, &end))
1217 if (limit == NULL || (limit &&
1218 gtk_text_iter_compare (&end, limit) > 0))
1223 *match_start = match;
1230 if (gtk_text_iter_get_line_offset (&search) == 0)
1232 if (!gtk_text_iter_backward_line (&search))
1237 gtk_text_iter_set_line_offset (&search, 0);
1241 g_strfreev ((gchar **) lines);
1247 empathy_window_get_is_visible (GtkWindow *window)
1249 GdkWindowState state;
1250 GdkWindow *gdk_window;
1252 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1254 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1259 state = gdk_window_get_state (gdk_window);
1260 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1268 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1270 GdkRectangle icon_location;
1273 GdkWindow *gdk_window;
1275 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1276 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1277 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1279 data[0] = icon_location.x;
1280 data[1] = icon_location.y;
1281 data[2] = icon_location.width;
1282 data[3] = icon_location.height;
1284 XChangeProperty (dpy,
1285 GDK_WINDOW_XID (gdk_window),
1286 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1287 "_NET_WM_ICON_GEOMETRY"),
1288 XA_CARDINAL, 32, PropModeReplace,
1289 (guchar *)&data, 4);
1291 gtk_window_set_skip_taskbar_hint (window, TRUE);
1292 gtk_window_iconify (window);
1295 /* Takes care of moving the window to the current workspace. */
1297 empathy_window_present (GtkWindow *window,
1298 gboolean steal_focus)
1301 GdkWindow *gdk_window;
1303 g_return_if_fail (GTK_IS_WINDOW (window));
1305 /* Move the window to the current workspace before trying to show it.
1306 * This is the behaviour people expect when clicking on the statusbar icon. */
1307 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1312 /* Has no effect if the WM has viewports, like compiz */
1313 gdk_x11_window_move_to_current_desktop (gdk_window);
1315 /* If window is still off-screen, hide it to force it to
1316 * reposition on the current workspace. */
1317 gtk_window_get_position (window, &x, &y);
1318 gtk_window_get_size (window, &w, &h);
1319 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1320 gtk_widget_hide (GTK_WIDGET (window));
1323 timestamp = gtk_get_current_event_time ();
1324 gtk_window_present_with_time (window, timestamp);
1325 gtk_window_set_skip_taskbar_hint (window, FALSE);
1326 gtk_window_deiconify (window);
1330 empathy_get_toplevel_window (GtkWidget *widget)
1332 GtkWidget *toplevel;
1334 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1336 toplevel = gtk_widget_get_toplevel (widget);
1337 if (GTK_IS_WINDOW (toplevel) &&
1338 gtk_widget_is_toplevel (toplevel)) {
1339 return GTK_WINDOW (toplevel);
1345 /** empathy_make_absolute_url_len:
1349 * Same as #empathy_make_absolute_url but for a limited string length
1352 empathy_make_absolute_url_len (const gchar *url,
1355 g_return_val_if_fail (url != NULL, NULL);
1357 if (g_str_has_prefix (url, "ghelp:") ||
1358 g_str_has_prefix (url, "mailto:") ||
1359 strstr (url, ":/")) {
1360 return g_strndup (url, len);
1363 if (strstr (url, "@")) {
1364 return g_strdup_printf ("mailto:%.*s", len, url);
1367 return g_strdup_printf ("http://%.*s", len, url);
1370 /** empathy_make_absolute_url:
1373 * The URL opening code can't handle schemeless strings, so we try to be
1374 * smart and add http if there is no scheme or doesn't look like a mail
1375 * address. This should work in most cases, and let us click on strings
1376 * like "www.gnome.org".
1378 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1379 * g_free when your are done with it
1382 empathy_make_absolute_url (const gchar *url)
1384 return empathy_make_absolute_url_len (url, strlen (url));
1388 empathy_url_show (GtkWidget *parent,
1392 GError *error = NULL;
1394 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1395 g_return_if_fail (url != NULL);
1397 real_url = empathy_make_absolute_url (url);
1399 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1400 gtk_get_current_event_time (), &error);
1405 dialog = gtk_message_dialog_new (NULL, 0,
1406 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1407 _("Unable to open URI"));
1408 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1409 "%s", error->message);
1411 g_signal_connect (dialog, "response",
1412 G_CALLBACK (gtk_widget_destroy),
1414 gtk_window_present (GTK_WINDOW (dialog));
1416 g_clear_error (&error);
1423 link_button_hook (GtkLinkButton *button,
1427 empathy_url_show (GTK_WIDGET (button), link);
1431 empathy_link_button_new (const gchar *url,
1434 static gboolean hook = FALSE;
1438 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1441 return gtk_link_button_new_with_label (url, title);
1445 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1450 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1452 g_signal_handlers_block_by_func (widget, callback, user_data);
1453 g_object_set (widget, "active", active, NULL);
1454 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1458 empathy_send_file (EmpathyContact *contact, GFile *file)
1460 EmpathyFTFactory *factory;
1461 GtkRecentManager *manager;
1464 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1465 g_return_if_fail (G_IS_FILE (file));
1467 factory = empathy_ft_factory_dup_singleton ();
1469 empathy_ft_factory_new_transfer_outgoing (factory, contact, file);
1471 uri = g_file_get_uri (file);
1472 manager = gtk_recent_manager_get_default ();
1473 gtk_recent_manager_add_item (manager, uri);
1476 g_object_unref (factory);
1480 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1485 /* Only handle a single file for now. It would be wicked cool to be
1486 able to do multiple files, offering to zip them or whatever like
1487 nautilus-sendto does. Note that text/uri-list is defined to have
1488 each line terminated by \r\n, but we can be tolerant of applications
1489 that only use \n or don't terminate single-line entries.
1491 nl = strstr (uri_list, "\r\n");
1493 nl = strchr (uri_list, '\n');
1496 gchar *uri = g_strndup (uri_list, nl - uri_list);
1497 file = g_file_new_for_uri (uri);
1501 file = g_file_new_for_uri (uri_list);
1504 empathy_send_file (contact, file);
1506 g_object_unref (file);
1510 file_manager_send_file_response_cb (GtkDialog *widget,
1512 EmpathyContact *contact)
1516 if (response_id == GTK_RESPONSE_OK) {
1517 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1519 empathy_send_file (contact, file);
1521 g_object_unref (file);
1524 gtk_widget_destroy (GTK_WIDGET (widget));
1528 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1533 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1535 DEBUG ("Creating selection file chooser");
1537 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1539 GTK_FILE_CHOOSER_ACTION_OPEN,
1541 GTK_RESPONSE_CANCEL,
1545 button = gtk_button_new_with_mnemonic (_("_Send"));
1546 gtk_button_set_image (GTK_BUTTON (button),
1547 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1548 GTK_ICON_SIZE_BUTTON));
1549 gtk_widget_show (button);
1550 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1552 gtk_widget_set_can_default (button, TRUE);
1553 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1556 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1558 g_signal_connect (widget, "response",
1559 G_CALLBACK (file_manager_send_file_response_cb),
1562 gtk_widget_show (widget);
1566 file_manager_receive_file_response_cb (GtkDialog *dialog,
1567 GtkResponseType response,
1568 EmpathyFTHandler *handler)
1570 EmpathyFTFactory *factory;
1573 if (response == GTK_RESPONSE_OK) {
1574 factory = empathy_ft_factory_dup_singleton ();
1575 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1577 empathy_ft_factory_set_destination_for_incoming_handler
1578 (factory, handler, file);
1580 g_object_unref (factory);
1581 g_object_unref (file);
1583 /* unref the handler, as we dismissed the file chooser,
1584 * and refused the transfer.
1586 g_object_unref (handler);
1589 gtk_widget_destroy (GTK_WIDGET (dialog));
1593 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1597 widget = gtk_file_chooser_dialog_new (_("Select a destination"),
1599 GTK_FILE_CHOOSER_ACTION_SAVE,
1601 GTK_RESPONSE_CANCEL,
1605 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1606 empathy_ft_handler_get_filename (handler));
1607 gtk_file_chooser_set_do_overwrite_confirmation
1608 (GTK_FILE_CHOOSER (widget), TRUE);
1610 g_signal_connect (widget, "response",
1611 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1613 gtk_widget_show (widget);
1617 empathy_string_parser_substr (const gchar *text,
1619 EmpathyStringParser *parsers,
1622 if (parsers != NULL && parsers[0].match_func != NULL) {
1623 parsers[0].match_func (text, len,
1624 parsers[0].replace_func, parsers + 1,
1630 empathy_string_match_link (const gchar *text,
1632 EmpathyStringReplace replace_func,
1633 EmpathyStringParser *sub_parsers,
1637 GMatchInfo *match_info;
1641 uri_regex = empathy_uri_regex_dup_singleton ();
1642 match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
1647 g_match_info_fetch_pos (match_info, 0, &s, &e);
1650 /* Append the text between last link (or the
1651 * start of the message) and this link */
1652 empathy_string_parser_substr (text + last,
1658 replace_func (text + s, e - s, NULL, user_data);
1661 } while (g_match_info_next (match_info, NULL));
1664 empathy_string_parser_substr (text + last, len - last,
1665 sub_parsers, user_data);
1667 g_match_info_free (match_info);
1668 g_regex_unref (uri_regex);
1672 empathy_string_match_smiley (const gchar *text,
1674 EmpathyStringReplace replace_func,
1675 EmpathyStringParser *sub_parsers,
1679 EmpathySmileyManager *smiley_manager;
1682 smiley_manager = empathy_smiley_manager_dup_singleton ();
1683 hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
1685 for (l = hits; l; l = l->next) {
1686 EmpathySmileyHit *hit = l->data;
1688 if (hit->start > last) {
1689 /* Append the text between last smiley (or the
1690 * start of the message) and this smiley */
1691 empathy_string_parser_substr (text + last,
1693 sub_parsers, user_data);
1696 replace_func (text + hit->start, hit->end - hit->start,
1701 empathy_smiley_hit_free (hit);
1703 g_slist_free (hits);
1704 g_object_unref (smiley_manager);
1706 empathy_string_parser_substr (text + last, len - last,
1707 sub_parsers, user_data);
1711 empathy_string_match_all (const gchar *text,
1713 EmpathyStringReplace replace_func,
1714 EmpathyStringParser *sub_parsers,
1717 replace_func (text, len, NULL, user_data);