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 BODY_CHAR "[^\\ \\n\"\']"
55 #define BODY "("BODY_CHAR"+)"
56 #define END_BODY "("BODY_CHAR"*[^,;\\:\?><(){}\\[\\]\\ \"\'\\.\\n])"
57 #define URI_REGEX "("SCHEMES"://"END_BODY")" \
58 "|((mailto:)?"BODY"@"BODY"\\."END_BODY")"\
59 "|((www|ftp)\\."END_BODY")"
62 empathy_gtk_init (void)
64 static gboolean initialized = FALSE;
70 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
71 PKGDATADIR G_DIR_SEPARATOR_S "icons");
77 empathy_uri_regex_dup_singleton (void)
79 static GRegex *uri_regex = NULL;
81 /* We intentionally leak the regex so it's not recomputed */
83 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
86 return g_regex_ref (uri_regex);
90 builder_get_file_valist (const gchar *filename,
91 const gchar *first_object,
99 DEBUG ("Loading file %s", filename);
101 gui = gtk_builder_new ();
102 if (!gtk_builder_add_from_file (gui, filename, &error)) {
103 g_critical ("GtkBuilder Error (%s): %s",
104 filename, error->message);
105 g_clear_error (&error);
106 g_object_unref (gui);
108 /* we need to iterate and set all of the pointers to NULL */
109 for (name = first_object; name;
110 name = va_arg (args, const gchar *)) {
111 object_ptr = va_arg (args, GObject**);
119 for (name = first_object; name; name = va_arg (args, const gchar *)) {
120 object_ptr = va_arg (args, GObject**);
122 *object_ptr = gtk_builder_get_object (gui, name);
125 g_warning ("File is missing object '%s'.", name);
134 empathy_builder_get_file (const gchar *filename,
135 const gchar *first_object,
141 va_start (args, first_object);
142 gui = builder_get_file_valist (filename, first_object, args);
149 empathy_builder_connect (GtkBuilder *gui,
160 va_start (args, first_object);
161 for (name = first_object; name; name = va_arg (args, const gchar *)) {
162 sig = va_arg (args, const gchar *);
163 callback = va_arg (args, GCallback);
165 object = gtk_builder_get_object (gui, name);
167 g_warning ("File is missing object '%s'.", name);
171 g_signal_connect (object, sig, callback, user_data);
178 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
181 /* On construction gui sinks the initial reference to widget. When gui
182 * is finalized it will drop its ref to widget. We take our own ref to
183 * prevent widget being finalised. The widget is forced to have a
184 * floating reference, like when it was initially unowned so that it can
185 * be used like any other GtkWidget. */
187 g_object_ref (widget);
188 g_object_force_floating (G_OBJECT (widget));
189 g_object_unref (gui);
195 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
198 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
199 return EMPATHY_IMAGE_AVAILABLE;
200 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
201 return EMPATHY_IMAGE_BUSY;
202 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
203 return EMPATHY_IMAGE_AWAY;
204 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
205 return EMPATHY_IMAGE_EXT_AWAY;
206 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
207 return EMPATHY_IMAGE_HIDDEN;
208 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
209 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
210 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
211 return EMPATHY_IMAGE_OFFLINE;
212 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
220 empathy_icon_name_for_contact (EmpathyContact *contact)
222 TpConnectionPresenceType presence;
224 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
225 EMPATHY_IMAGE_OFFLINE);
227 presence = empathy_contact_get_presence (contact);
228 return empathy_icon_name_for_presence (presence);
232 empathy_pixbuf_from_data (gchar *data,
235 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
239 empathy_pixbuf_from_data_and_mime (gchar *data,
243 GdkPixbufLoader *loader;
244 GdkPixbufFormat *format;
245 GdkPixbuf *pixbuf = NULL;
247 GError *error = NULL;
253 loader = gdk_pixbuf_loader_new ();
254 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
255 DEBUG ("Failed to write to pixbuf loader: %s",
256 error ? error->message : "No error given");
259 if (!gdk_pixbuf_loader_close (loader, &error)) {
260 DEBUG ("Failed to close pixbuf loader: %s",
261 error ? error->message : "No error given");
265 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
267 g_object_ref (pixbuf);
269 if (mime_type != NULL) {
270 format = gdk_pixbuf_loader_get_format (loader);
271 mime_types = gdk_pixbuf_format_get_mime_types (format);
273 *mime_type = g_strdup (*mime_types);
274 if (mime_types[1] != NULL) {
275 DEBUG ("Loader supports more than one mime "
276 "type! Picking the first one, %s",
279 g_strfreev (mime_types);
284 g_clear_error (&error);
285 g_object_unref (loader);
293 gboolean preserve_aspect_ratio;
297 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
300 struct SizeData *data)
302 g_return_if_fail (width > 0 && height > 0);
304 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
305 if (width < data->width && height < data->height) {
310 if (data->width < 0) {
311 width = width * (double) data->height / (gdouble) height;
312 height = data->height;
313 } else if (data->height < 0) {
314 height = height * (double) data->width / (double) width;
316 } else if ((double) height * (double) data->width >
317 (double) width * (double) data->height) {
318 width = 0.5 + (double) width * (double) data->height / (double) height;
319 height = data->height;
321 height = 0.5 + (double) height * (double) data->width / (double) width;
325 if (data->width > 0) {
329 if (data->height > 0) {
330 height = data->height;
334 gdk_pixbuf_loader_set_size (loader, width, height);
338 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
340 gint width, height, rowstride;
343 width = gdk_pixbuf_get_width (pixbuf);
344 height = gdk_pixbuf_get_height (pixbuf);
345 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
346 pixels = gdk_pixbuf_get_pixels (pixbuf);
348 if (width < 6 || height < 6) {
356 pixels[rowstride + 3] = 0x80;
357 pixels[rowstride * 2 + 3] = 0xC0;
360 pixels[width * 4 - 1] = 0;
361 pixels[width * 4 - 5] = 0x80;
362 pixels[width * 4 - 9] = 0xC0;
363 pixels[rowstride + (width * 4) - 1] = 0x80;
364 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
367 pixels[(height - 1) * rowstride + 3] = 0;
368 pixels[(height - 1) * rowstride + 7] = 0x80;
369 pixels[(height - 1) * rowstride + 11] = 0xC0;
370 pixels[(height - 2) * rowstride + 3] = 0x80;
371 pixels[(height - 3) * rowstride + 3] = 0xC0;
374 pixels[height * rowstride - 1] = 0;
375 pixels[(height - 1) * rowstride - 1] = 0x80;
376 pixels[(height - 2) * rowstride - 1] = 0xC0;
377 pixels[height * rowstride - 5] = 0x80;
378 pixels[height * rowstride - 9] = 0xC0;
382 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
384 gint width, height, rowstride, i;
388 width = gdk_pixbuf_get_width (pixbuf);
389 height = gdk_pixbuf_get_height (pixbuf);
390 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
391 pixels = gdk_pixbuf_get_pixels (pixbuf);
394 for (i = 3; i < rowstride; i+=4) {
400 for (i = 1; i < height - 1; i++) {
401 row = pixels + (i*rowstride);
402 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
407 row = pixels + ((height-1) * rowstride);
408 for (i = 3; i < rowstride; i+=4) {
418 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
423 GdkPixbufLoader *loader;
424 struct SizeData data;
425 GError *error = NULL;
432 data.height = height;
433 data.preserve_aspect_ratio = TRUE;
435 loader = gdk_pixbuf_loader_new ();
437 g_signal_connect (loader, "size-prepared",
438 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
441 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
442 g_warning ("Couldn't write avatar image:%p with "
443 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
444 avatar->data, avatar->len, error->message);
445 g_error_free (error);
449 gdk_pixbuf_loader_close (loader, NULL);
451 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
452 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
453 GdkPixbuf *rounded_pixbuf;
455 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
456 gdk_pixbuf_get_width (pixbuf),
457 gdk_pixbuf_get_height (pixbuf));
458 gdk_pixbuf_copy_area (pixbuf, 0, 0,
459 gdk_pixbuf_get_width (pixbuf),
460 gdk_pixbuf_get_height (pixbuf),
463 pixbuf = rounded_pixbuf;
465 g_object_ref (pixbuf);
468 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
469 empathy_avatar_pixbuf_roundify (pixbuf);
472 g_object_unref (loader);
478 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
482 EmpathyAvatar *avatar;
484 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
486 avatar = empathy_contact_get_avatar (contact);
488 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
492 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
497 width = gdk_pixbuf_get_width (pixbuf);
498 height = gdk_pixbuf_get_height (pixbuf);
500 if (width > 0 && (width > max_size || height > max_size)) {
501 factor = (gdouble) max_size / MAX (width, height);
503 width = width * factor;
504 height = height * factor;
506 return gdk_pixbuf_scale_simple (pixbuf,
511 return g_object_ref (pixbuf);
515 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
520 GError *error = NULL;
526 theme = gtk_icon_theme_get_default ();
528 pixbuf = gtk_icon_theme_load_icon (theme,
534 DEBUG ("Error loading icon: %s", error->message);
535 g_clear_error (&error);
542 empathy_pixbuf_from_icon_name (const gchar *icon_name,
543 GtkIconSize icon_size)
552 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
556 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
560 empathy_filename_from_icon_name (const gchar *icon_name,
561 GtkIconSize icon_size)
563 GtkIconTheme *icon_theme;
564 GtkIconInfo *icon_info;
569 icon_theme = gtk_icon_theme_get_default ();
571 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
575 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
576 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
577 gtk_icon_info_free (icon_info);
582 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
583 * that to make it easier to apply changes from the original code.
585 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
587 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
588 * decomposable character it consumes the decomposition length from the given
589 * offset. So it's useful when the offset was calculated for the normalized
590 * version of str, but we need a pointer to str itself. */
592 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
594 gchar *casefold, *normal;
600 q = g_utf8_next_char (p);
601 casefold = g_utf8_casefold (p, q - p);
602 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
603 offset -= g_utf8_strlen (normal, -1);
612 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
616 const gchar *ret = NULL;
619 gchar *caseless_haystack;
622 g_return_val_if_fail (haystack != NULL, NULL);
623 g_return_val_if_fail (needle != NULL, NULL);
625 casefold = g_utf8_casefold (haystack, -1);
626 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
629 needle_len = g_utf8_strlen (needle, -1);
630 haystack_len = g_utf8_strlen (caseless_haystack, -1);
634 ret = (gchar *) haystack;
638 if (haystack_len < needle_len)
644 p = (gchar *) caseless_haystack;
645 needle_len = strlen (needle);
650 if ((strncmp (p, needle, needle_len) == 0))
652 ret = pointer_from_offset_skipping_decomp (haystack, i);
656 p = g_utf8_next_char (p);
661 g_free (caseless_haystack);
667 g_utf8_caselessnmatch (const char *s1, const char *s2,
668 gssize n1, gssize n2)
671 gchar *normalized_s1;
672 gchar *normalized_s2;
675 gboolean ret = FALSE;
677 g_return_val_if_fail (s1 != NULL, FALSE);
678 g_return_val_if_fail (s2 != NULL, FALSE);
679 g_return_val_if_fail (n1 > 0, FALSE);
680 g_return_val_if_fail (n2 > 0, FALSE);
682 casefold = g_utf8_casefold (s1, n1);
683 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
686 casefold = g_utf8_casefold (s2, n2);
687 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
690 len_s1 = strlen (normalized_s1);
691 len_s2 = strlen (normalized_s2);
696 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
699 g_free (normalized_s1);
700 g_free (normalized_s2);
706 forward_chars_with_skipping (GtkTextIter *iter,
708 gboolean skip_invisible,
709 gboolean skip_nontext,
710 gboolean skip_decomp)
714 g_return_if_fail (count >= 0);
720 gboolean ignored = FALSE;
722 /* minimal workaround to avoid the infinite loop of bug #168247.
723 * It doesn't fix the problemjust the symptom...
725 if (gtk_text_iter_is_end (iter))
728 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
731 if (!ignored && skip_invisible &&
732 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
735 if (!ignored && skip_decomp)
737 /* being UTF8 correct sucks; this accounts for extra
738 offsets coming from canonical decompositions of
739 UTF8 characters (e.g. accented characters) which
740 g_utf8_normalize () performs */
745 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
746 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
747 i -= (g_utf8_strlen (normal, -1) - 1);
751 gtk_text_iter_forward_char (iter);
759 lines_match (const GtkTextIter *start,
761 gboolean visible_only,
763 GtkTextIter *match_start,
764 GtkTextIter *match_end)
771 if (*lines == NULL || **lines == '\0')
774 *match_start = *start;
781 gtk_text_iter_forward_line (&next);
783 /* No more text in buffer, but *lines is nonempty */
784 if (gtk_text_iter_equal (start, &next))
790 line_text = gtk_text_iter_get_visible_slice (start, &next);
792 line_text = gtk_text_iter_get_slice (start, &next);
797 line_text = gtk_text_iter_get_visible_text (start, &next);
799 line_text = gtk_text_iter_get_text (start, &next);
802 if (match_start) /* if this is the first line we're matching */
804 found = g_utf8_strcasestr (line_text, *lines);
808 /* If it's not the first line, we have to match from the
811 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
824 /* Get offset to start of search string */
825 offset = g_utf8_strlen (line_text, found - line_text);
829 /* If match start needs to be returned, set it to the
830 * start of the search string.
832 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
838 /* Go to end of search string */
839 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
848 /* pass NULL for match_start, since we don't need to find the
851 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
854 /* strsplit () that retains the delimiter as part of the string. */
856 strbreakup (const char *string,
857 const char *delimiter,
860 GSList *string_list = NULL, *slist;
861 gchar **str_array, *s, *casefold, *new_string;
864 g_return_val_if_fail (string != NULL, NULL);
865 g_return_val_if_fail (delimiter != NULL, NULL);
868 max_tokens = G_MAXINT;
870 s = strstr (string, delimiter);
873 guint delimiter_len = strlen (delimiter);
879 len = s - string + delimiter_len;
880 new_string = g_new (gchar, len + 1);
881 strncpy (new_string, string, len);
883 casefold = g_utf8_casefold (new_string, -1);
885 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
887 string_list = g_slist_prepend (string_list, new_string);
889 string = s + delimiter_len;
890 s = strstr (string, delimiter);
891 } while (--max_tokens && s);
897 casefold = g_utf8_casefold (string, -1);
898 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
900 string_list = g_slist_prepend (string_list, new_string);
903 str_array = g_new (gchar*, n);
907 str_array[i--] = NULL;
908 for (slist = string_list; slist; slist = slist->next)
909 str_array[i--] = slist->data;
911 g_slist_free (string_list);
917 empathy_text_iter_forward_search (const GtkTextIter *iter,
919 GtkTextIter *match_start,
920 GtkTextIter *match_end,
921 const GtkTextIter *limit)
923 gchar **lines = NULL;
925 gboolean retval = FALSE;
927 gboolean visible_only;
930 g_return_val_if_fail (iter != NULL, FALSE);
931 g_return_val_if_fail (str != NULL, FALSE);
933 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
937 /* If we can move one char, return the empty string there */
940 if (gtk_text_iter_forward_char (&match)) {
941 if (limit && gtk_text_iter_equal (&match, limit)) {
946 *match_start = match;
960 /* locate all lines */
961 lines = strbreakup (str, "\n", -1);
966 /* This loop has an inefficient worst-case, where
967 * gtk_text_iter_get_text () is called repeatedly on
972 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
976 if (lines_match (&search, (const gchar**)lines,
977 visible_only, slice, &match, &end)) {
979 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
983 *match_start = match;
991 } while (gtk_text_iter_forward_line (&search));
993 g_strfreev ((gchar **) lines);
999 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1003 const gchar *ret = NULL;
1006 gchar *caseless_haystack;
1009 g_return_val_if_fail (haystack != NULL, NULL);
1010 g_return_val_if_fail (needle != NULL, NULL);
1012 casefold = g_utf8_casefold (haystack, -1);
1013 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1016 needle_len = g_utf8_strlen (needle, -1);
1017 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1019 if (needle_len == 0)
1021 ret = (gchar *) haystack;
1025 if (haystack_len < needle_len)
1031 i = haystack_len - needle_len;
1032 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1033 needle_len = strlen (needle);
1035 while (p >= caseless_haystack)
1037 if (strncmp (p, needle, needle_len) == 0)
1039 ret = pointer_from_offset_skipping_decomp (haystack, i);
1043 p = g_utf8_prev_char (p);
1048 g_free (caseless_haystack);
1054 backward_lines_match (const GtkTextIter *start,
1055 const gchar **lines,
1056 gboolean visible_only,
1058 GtkTextIter *match_start,
1059 GtkTextIter *match_end)
1061 GtkTextIter line, next;
1066 if (*lines == NULL || **lines == '\0')
1069 *match_start = *start;
1071 *match_end = *start;
1075 line = next = *start;
1076 if (gtk_text_iter_get_line_offset (&next) == 0)
1078 if (!gtk_text_iter_backward_line (&next))
1082 gtk_text_iter_set_line_offset (&next, 0);
1087 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1089 line_text = gtk_text_iter_get_slice (&next, &line);
1094 line_text = gtk_text_iter_get_visible_text (&next, &line);
1096 line_text = gtk_text_iter_get_text (&next, &line);
1099 if (match_start) /* if this is the first line we're matching */
1101 found = g_utf8_strrcasestr (line_text, *lines);
1105 /* If it's not the first line, we have to match from the
1106 * start of the line.
1108 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1121 /* Get offset to start of search string */
1122 offset = g_utf8_strlen (line_text, found - line_text);
1124 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1126 /* If match start needs to be returned, set it to the
1127 * start of the search string.
1131 *match_start = next;
1134 /* Go to end of search string */
1135 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1144 /* try to match the rest of the lines forward, passing NULL
1145 * for match_start so lines_match will try to match the entire
1147 return lines_match (&next, lines, visible_only,
1148 slice, NULL, match_end);
1152 empathy_text_iter_backward_search (const GtkTextIter *iter,
1154 GtkTextIter *match_start,
1155 GtkTextIter *match_end,
1156 const GtkTextIter *limit)
1158 gchar **lines = NULL;
1160 gboolean retval = FALSE;
1162 gboolean visible_only;
1165 g_return_val_if_fail (iter != NULL, FALSE);
1166 g_return_val_if_fail (str != NULL, FALSE);
1168 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1173 /* If we can move one char, return the empty string there */
1176 if (gtk_text_iter_backward_char (&match))
1178 if (limit && gtk_text_iter_equal (&match, limit))
1182 *match_start = match;
1193 visible_only = TRUE;
1196 /* locate all lines */
1197 lines = strbreakup (str, "\n", -1);
1203 /* This loop has an inefficient worst-case, where
1204 * gtk_text_iter_get_text () is called repeatedly on
1209 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1212 if (backward_lines_match (&search, (const gchar**)lines,
1213 visible_only, slice, &match, &end))
1215 if (limit == NULL || (limit &&
1216 gtk_text_iter_compare (&end, limit) > 0))
1221 *match_start = match;
1228 if (gtk_text_iter_get_line_offset (&search) == 0)
1230 if (!gtk_text_iter_backward_line (&search))
1235 gtk_text_iter_set_line_offset (&search, 0);
1239 g_strfreev ((gchar **) lines);
1245 empathy_window_get_is_visible (GtkWindow *window)
1247 GdkWindowState state;
1248 GdkWindow *gdk_window;
1250 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1252 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1257 state = gdk_window_get_state (gdk_window);
1258 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1266 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1268 GdkRectangle icon_location;
1271 GdkWindow *gdk_window;
1273 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1274 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1275 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1277 data[0] = icon_location.x;
1278 data[1] = icon_location.y;
1279 data[2] = icon_location.width;
1280 data[3] = icon_location.height;
1282 XChangeProperty (dpy,
1283 GDK_WINDOW_XID (gdk_window),
1284 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1285 "_NET_WM_ICON_GEOMETRY"),
1286 XA_CARDINAL, 32, PropModeReplace,
1287 (guchar *)&data, 4);
1289 gtk_window_set_skip_taskbar_hint (window, TRUE);
1290 gtk_window_iconify (window);
1293 /* Takes care of moving the window to the current workspace. */
1295 empathy_window_present (GtkWindow *window,
1296 gboolean steal_focus)
1299 GdkWindow *gdk_window;
1301 g_return_if_fail (GTK_IS_WINDOW (window));
1303 /* Move the window to the current workspace before trying to show it.
1304 * This is the behaviour people expect when clicking on the statusbar icon. */
1305 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1310 /* Has no effect if the WM has viewports, like compiz */
1311 gdk_x11_window_move_to_current_desktop (gdk_window);
1313 /* If window is still off-screen, hide it to force it to
1314 * reposition on the current workspace. */
1315 gtk_window_get_position (window, &x, &y);
1316 gtk_window_get_size (window, &w, &h);
1317 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1318 gtk_widget_hide (GTK_WIDGET (window));
1321 timestamp = gtk_get_current_event_time ();
1322 gtk_window_present_with_time (window, timestamp);
1323 gtk_window_set_skip_taskbar_hint (window, FALSE);
1324 gtk_window_deiconify (window);
1328 empathy_get_toplevel_window (GtkWidget *widget)
1330 GtkWidget *toplevel;
1332 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1334 toplevel = gtk_widget_get_toplevel (widget);
1335 if (GTK_IS_WINDOW (toplevel) &&
1336 gtk_widget_is_toplevel (toplevel)) {
1337 return GTK_WINDOW (toplevel);
1343 /** empathy_make_absolute_url_len:
1347 * Same as #empathy_make_absolute_url but for a limited string length
1350 empathy_make_absolute_url_len (const gchar *url,
1353 g_return_val_if_fail (url != NULL, NULL);
1355 if (g_str_has_prefix (url, "ghelp:") ||
1356 g_str_has_prefix (url, "mailto:") ||
1357 strstr (url, ":/")) {
1358 return g_strndup (url, len);
1361 if (strstr (url, "@")) {
1362 return g_strdup_printf ("mailto:%.*s", len, url);
1365 return g_strdup_printf ("http://%.*s", len, url);
1368 /** empathy_make_absolute_url:
1371 * The URL opening code can't handle schemeless strings, so we try to be
1372 * smart and add http if there is no scheme or doesn't look like a mail
1373 * address. This should work in most cases, and let us click on strings
1374 * like "www.gnome.org".
1376 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1377 * g_free when your are done with it
1380 empathy_make_absolute_url (const gchar *url)
1382 return empathy_make_absolute_url_len (url, strlen (url));
1386 empathy_url_show (GtkWidget *parent,
1390 GError *error = NULL;
1392 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1393 g_return_if_fail (url != NULL);
1395 real_url = empathy_make_absolute_url (url);
1397 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1398 gtk_get_current_event_time (), &error);
1403 dialog = gtk_message_dialog_new (NULL, 0,
1404 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1405 _("Unable to open URI"));
1406 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1407 "%s", error->message);
1409 g_signal_connect (dialog, "response",
1410 G_CALLBACK (gtk_widget_destroy),
1412 gtk_window_present (GTK_WINDOW (dialog));
1414 g_clear_error (&error);
1421 link_button_hook (GtkLinkButton *button,
1425 empathy_url_show (GTK_WIDGET (button), link);
1429 empathy_link_button_new (const gchar *url,
1432 static gboolean hook = FALSE;
1436 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1439 return gtk_link_button_new_with_label (url, title);
1443 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1448 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1450 g_signal_handlers_block_by_func (widget, callback, user_data);
1451 g_object_set (widget, "active", active, NULL);
1452 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1456 file_manager_send_file_response_cb (GtkDialog *widget,
1458 EmpathyContact *contact)
1460 EmpathyFTFactory *factory;
1463 GtkRecentManager *manager;
1465 if (response_id == GTK_RESPONSE_OK) {
1466 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1467 uri = g_file_get_uri (file);
1469 factory = empathy_ft_factory_dup_singleton ();
1471 empathy_ft_factory_new_transfer_outgoing (factory, contact,
1474 manager = gtk_recent_manager_get_default ();
1475 gtk_recent_manager_add_item (manager, uri);
1478 g_object_unref (factory);
1479 g_object_unref (file);
1482 gtk_widget_destroy (GTK_WIDGET (widget));
1486 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1491 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1493 DEBUG ("Creating selection file chooser");
1495 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1497 GTK_FILE_CHOOSER_ACTION_OPEN,
1499 GTK_RESPONSE_CANCEL,
1503 button = gtk_button_new_with_mnemonic (_("_Send"));
1504 gtk_button_set_image (GTK_BUTTON (button),
1505 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1506 GTK_ICON_SIZE_BUTTON));
1507 gtk_widget_show (button);
1508 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1510 gtk_widget_set_can_default (button, TRUE);
1511 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1514 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1516 g_signal_connect (widget, "response",
1517 G_CALLBACK (file_manager_send_file_response_cb),
1520 gtk_widget_show (widget);
1524 file_manager_receive_file_response_cb (GtkDialog *dialog,
1525 GtkResponseType response,
1526 EmpathyFTHandler *handler)
1528 EmpathyFTFactory *factory;
1531 if (response == GTK_RESPONSE_OK) {
1532 factory = empathy_ft_factory_dup_singleton ();
1533 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1535 empathy_ft_factory_set_destination_for_incoming_handler
1536 (factory, handler, file);
1538 g_object_unref (factory);
1539 g_object_unref (file);
1541 /* unref the handler, as we dismissed the file chooser,
1542 * and refused the transfer.
1544 g_object_unref (handler);
1547 gtk_widget_destroy (GTK_WIDGET (dialog));
1551 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1555 widget = gtk_file_chooser_dialog_new (_("Select a destination"),
1557 GTK_FILE_CHOOSER_ACTION_SAVE,
1559 GTK_RESPONSE_CANCEL,
1563 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1564 empathy_ft_handler_get_filename (handler));
1565 gtk_file_chooser_set_do_overwrite_confirmation
1566 (GTK_FILE_CHOOSER (widget), TRUE);
1568 g_signal_connect (widget, "response",
1569 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1571 gtk_widget_show (widget);
1575 empathy_string_parser_substr (const gchar *text,
1577 EmpathyStringParser *parsers,
1580 if (parsers != NULL && parsers[0].match_func != NULL) {
1581 parsers[0].match_func (text, len,
1582 parsers[0].replace_func, parsers + 1,
1588 empathy_string_match_link (const gchar *text,
1590 EmpathyStringReplace replace_func,
1591 EmpathyStringParser *sub_parsers,
1595 GMatchInfo *match_info;
1599 uri_regex = empathy_uri_regex_dup_singleton ();
1600 match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
1605 g_match_info_fetch_pos (match_info, 0, &s, &e);
1608 /* Append the text between last link (or the
1609 * start of the message) and this link */
1610 empathy_string_parser_substr (text + last,
1616 replace_func (text + s, e - s, NULL, user_data);
1619 } while (g_match_info_next (match_info, NULL));
1622 empathy_string_parser_substr (text + last, len - last,
1623 sub_parsers, user_data);
1625 g_match_info_free (match_info);
1626 g_regex_unref (uri_regex);
1630 empathy_string_match_smiley (const gchar *text,
1632 EmpathyStringReplace replace_func,
1633 EmpathyStringParser *sub_parsers,
1637 EmpathySmileyManager *smiley_manager;
1640 smiley_manager = empathy_smiley_manager_dup_singleton ();
1641 hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
1643 for (l = hits; l; l = l->next) {
1644 EmpathySmileyHit *hit = l->data;
1646 if (hit->start > last) {
1647 /* Append the text between last smiley (or the
1648 * start of the message) and this smiley */
1649 empathy_string_parser_substr (text + last,
1651 sub_parsers, user_data);
1654 replace_func (text + hit->start, hit->end - hit->start,
1659 empathy_smiley_hit_free (hit);
1661 g_slist_free (hits);
1662 g_object_unref (smiley_manager);
1664 empathy_string_parser_substr (text + last, len - last,
1665 sub_parsers, user_data);
1669 empathy_string_match_all (const gchar *text,
1671 EmpathyStringReplace replace_func,
1672 EmpathyStringParser *sub_parsers,
1675 replace_func (text, len, NULL, user_data);