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-conf.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
46 #include <libempathy/empathy-debug.h>
47 #include <libempathy/empathy-utils.h>
48 #include <libempathy/empathy-dispatcher.h>
49 #include <libempathy/empathy-idle.h>
50 #include <libempathy/empathy-ft-factory.h>
52 #define SCHEMES "([a-zA-Z\\+]+)"
53 #define BODY_CHAR "[^\\ \\n\"\']"
54 #define BODY "("BODY_CHAR"+)"
55 #define END_BODY "("BODY_CHAR"*[^,;\?><()\\ \"\'\\.\\n])"
56 #define URI_REGEX "("SCHEMES"://"END_BODY")" \
57 "|((mailto:)?"BODY"@"BODY"\\."END_BODY")"\
58 "|((www|ftp)\\."END_BODY")"
61 empathy_gtk_init (void)
63 static gboolean initialized = FALSE;
69 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
70 PKGDATADIR G_DIR_SEPARATOR_S "icons");
76 empathy_uri_regex_dup_singleton (void)
78 static GRegex *uri_regex = NULL;
80 /* We intentionally leak the regex so it's not recomputed */
82 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
85 return g_regex_ref (uri_regex);
89 builder_get_file_valist (const gchar *filename,
90 const gchar *first_object,
98 DEBUG ("Loading file %s", filename);
100 gui = gtk_builder_new ();
101 if (!gtk_builder_add_from_file (gui, filename, &error)) {
102 g_critical ("GtkBuilder Error (%s): %s",
103 filename, error->message);
104 g_clear_error (&error);
105 g_object_unref (gui);
107 /* we need to iterate and set all of the pointers to NULL */
108 for (name = first_object; name;
109 name = va_arg (args, const gchar *)) {
110 object_ptr = va_arg (args, GObject**);
118 for (name = first_object; name; name = va_arg (args, const gchar *)) {
119 object_ptr = va_arg (args, GObject**);
121 *object_ptr = gtk_builder_get_object (gui, name);
124 g_warning ("File is missing object '%s'.", name);
133 empathy_builder_get_file (const gchar *filename,
134 const gchar *first_object,
140 va_start (args, first_object);
141 gui = builder_get_file_valist (filename, first_object, args);
148 empathy_builder_connect (GtkBuilder *gui,
159 va_start (args, first_object);
160 for (name = first_object; name; name = va_arg (args, const gchar *)) {
161 sig = va_arg (args, const gchar *);
162 callback = va_arg (args, GCallback);
164 object = gtk_builder_get_object (gui, name);
166 g_warning ("File is missing object '%s'.", name);
170 g_signal_connect (object, sig, callback, user_data);
177 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
180 /* On construction gui sinks the initial reference to widget. When gui
181 * is finalized it will drop its ref to widget. We take our own ref to
182 * prevent widget being finalised. The widget is forced to have a
183 * floating reference, like when it was initially unowned so that it can
184 * be used like any other GtkWidget. */
186 g_object_ref (widget);
187 g_object_force_floating (G_OBJECT (widget));
188 g_object_unref (gui);
194 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
197 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
198 return EMPATHY_IMAGE_AVAILABLE;
199 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
200 return EMPATHY_IMAGE_BUSY;
201 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
202 return EMPATHY_IMAGE_AWAY;
203 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
204 return EMPATHY_IMAGE_EXT_AWAY;
205 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
206 return EMPATHY_IMAGE_HIDDEN;
207 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
208 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
209 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
210 return EMPATHY_IMAGE_OFFLINE;
211 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
219 empathy_icon_name_for_contact (EmpathyContact *contact)
221 TpConnectionPresenceType presence;
223 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
224 EMPATHY_IMAGE_OFFLINE);
226 presence = empathy_contact_get_presence (contact);
227 return empathy_icon_name_for_presence (presence);
231 empathy_pixbuf_from_data (gchar *data,
234 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
238 empathy_pixbuf_from_data_and_mime (gchar *data,
242 GdkPixbufLoader *loader;
243 GdkPixbufFormat *format;
244 GdkPixbuf *pixbuf = NULL;
246 GError *error = NULL;
252 loader = gdk_pixbuf_loader_new ();
253 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
254 DEBUG ("Failed to write to pixbuf loader: %s",
255 error ? error->message : "No error given");
258 if (!gdk_pixbuf_loader_close (loader, &error)) {
259 DEBUG ("Failed to close pixbuf loader: %s",
260 error ? error->message : "No error given");
264 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
266 g_object_ref (pixbuf);
268 if (mime_type != NULL) {
269 format = gdk_pixbuf_loader_get_format (loader);
270 mime_types = gdk_pixbuf_format_get_mime_types (format);
272 *mime_type = g_strdup (*mime_types);
273 if (mime_types[1] != NULL) {
274 DEBUG ("Loader supports more than one mime "
275 "type! Picking the first one, %s",
278 g_strfreev (mime_types);
283 g_clear_error (&error);
284 g_object_unref (loader);
292 gboolean preserve_aspect_ratio;
296 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
299 struct SizeData *data)
301 g_return_if_fail (width > 0 && height > 0);
303 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
304 if (width < data->width && height < data->height) {
309 if (data->width < 0) {
310 width = width * (double) data->height / (gdouble) height;
311 height = data->height;
312 } else if (data->height < 0) {
313 height = height * (double) data->width / (double) width;
315 } else if ((double) height * (double) data->width >
316 (double) width * (double) data->height) {
317 width = 0.5 + (double) width * (double) data->height / (double) height;
318 height = data->height;
320 height = 0.5 + (double) height * (double) data->width / (double) width;
324 if (data->width > 0) {
328 if (data->height > 0) {
329 height = data->height;
333 gdk_pixbuf_loader_set_size (loader, width, height);
337 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
339 gint width, height, rowstride;
342 width = gdk_pixbuf_get_width (pixbuf);
343 height = gdk_pixbuf_get_height (pixbuf);
344 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
345 pixels = gdk_pixbuf_get_pixels (pixbuf);
347 if (width < 6 || height < 6) {
355 pixels[rowstride + 3] = 0x80;
356 pixels[rowstride * 2 + 3] = 0xC0;
359 pixels[width * 4 - 1] = 0;
360 pixels[width * 4 - 5] = 0x80;
361 pixels[width * 4 - 9] = 0xC0;
362 pixels[rowstride + (width * 4) - 1] = 0x80;
363 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
366 pixels[(height - 1) * rowstride + 3] = 0;
367 pixels[(height - 1) * rowstride + 7] = 0x80;
368 pixels[(height - 1) * rowstride + 11] = 0xC0;
369 pixels[(height - 2) * rowstride + 3] = 0x80;
370 pixels[(height - 3) * rowstride + 3] = 0xC0;
373 pixels[height * rowstride - 1] = 0;
374 pixels[(height - 1) * rowstride - 1] = 0x80;
375 pixels[(height - 2) * rowstride - 1] = 0xC0;
376 pixels[height * rowstride - 5] = 0x80;
377 pixels[height * rowstride - 9] = 0xC0;
381 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
383 gint width, height, rowstride, i;
387 width = gdk_pixbuf_get_width (pixbuf);
388 height = gdk_pixbuf_get_height (pixbuf);
389 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
390 pixels = gdk_pixbuf_get_pixels (pixbuf);
393 for (i = 3; i < rowstride; i+=4) {
399 for (i = 1; i < height - 1; i++) {
400 row = pixels + (i*rowstride);
401 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
406 row = pixels + ((height-1) * rowstride);
407 for (i = 3; i < rowstride; i+=4) {
417 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
422 GdkPixbufLoader *loader;
423 struct SizeData data;
424 GError *error = NULL;
431 data.height = height;
432 data.preserve_aspect_ratio = TRUE;
434 loader = gdk_pixbuf_loader_new ();
436 g_signal_connect (loader, "size-prepared",
437 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
440 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
441 g_warning ("Couldn't write avatar image:%p with "
442 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
443 avatar->data, avatar->len, error->message);
444 g_error_free (error);
448 gdk_pixbuf_loader_close (loader, NULL);
450 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
451 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
452 GdkPixbuf *rounded_pixbuf;
454 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
455 gdk_pixbuf_get_width (pixbuf),
456 gdk_pixbuf_get_height (pixbuf));
457 gdk_pixbuf_copy_area (pixbuf, 0, 0,
458 gdk_pixbuf_get_width (pixbuf),
459 gdk_pixbuf_get_height (pixbuf),
462 pixbuf = rounded_pixbuf;
464 g_object_ref (pixbuf);
467 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
468 empathy_avatar_pixbuf_roundify (pixbuf);
471 g_object_unref (loader);
477 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
481 EmpathyAvatar *avatar;
483 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
485 avatar = empathy_contact_get_avatar (contact);
487 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
491 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
496 width = gdk_pixbuf_get_width (pixbuf);
497 height = gdk_pixbuf_get_height (pixbuf);
499 if (width > 0 && (width > max_size || height > max_size)) {
500 factor = (gdouble) max_size / MAX (width, height);
502 width = width * factor;
503 height = height * factor;
505 return gdk_pixbuf_scale_simple (pixbuf,
510 return g_object_ref (pixbuf);
514 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
519 GError *error = NULL;
525 theme = gtk_icon_theme_get_default ();
527 pixbuf = gtk_icon_theme_load_icon (theme,
533 DEBUG ("Error loading icon: %s", error->message);
534 g_clear_error (&error);
541 empathy_pixbuf_from_icon_name (const gchar *icon_name,
542 GtkIconSize icon_size)
551 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
555 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
559 empathy_filename_from_icon_name (const gchar *icon_name,
560 GtkIconSize icon_size)
562 GtkIconTheme *icon_theme;
563 GtkIconInfo *icon_info;
568 icon_theme = gtk_icon_theme_get_default ();
570 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
574 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
575 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
576 gtk_icon_info_free (icon_info);
581 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
582 * that to make it easier to apply changes from the original code.
584 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
586 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
587 * decomposable character it consumes the decomposition length from the given
588 * offset. So it's useful when the offset was calculated for the normalized
589 * version of str, but we need a pointer to str itself. */
591 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
593 gchar *casefold, *normal;
599 q = g_utf8_next_char (p);
600 casefold = g_utf8_casefold (p, q - p);
601 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
602 offset -= g_utf8_strlen (normal, -1);
611 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
615 const gchar *ret = NULL;
618 gchar *caseless_haystack;
621 g_return_val_if_fail (haystack != NULL, NULL);
622 g_return_val_if_fail (needle != NULL, NULL);
624 casefold = g_utf8_casefold (haystack, -1);
625 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
628 needle_len = g_utf8_strlen (needle, -1);
629 haystack_len = g_utf8_strlen (caseless_haystack, -1);
633 ret = (gchar *) haystack;
637 if (haystack_len < needle_len)
643 p = (gchar *) caseless_haystack;
644 needle_len = strlen (needle);
649 if ((strncmp (p, needle, needle_len) == 0))
651 ret = pointer_from_offset_skipping_decomp (haystack, i);
655 p = g_utf8_next_char (p);
660 g_free (caseless_haystack);
666 g_utf8_caselessnmatch (const char *s1, const char *s2,
667 gssize n1, gssize n2)
670 gchar *normalized_s1;
671 gchar *normalized_s2;
674 gboolean ret = FALSE;
676 g_return_val_if_fail (s1 != NULL, FALSE);
677 g_return_val_if_fail (s2 != NULL, FALSE);
678 g_return_val_if_fail (n1 > 0, FALSE);
679 g_return_val_if_fail (n2 > 0, FALSE);
681 casefold = g_utf8_casefold (s1, n1);
682 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
685 casefold = g_utf8_casefold (s2, n2);
686 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
689 len_s1 = strlen (normalized_s1);
690 len_s2 = strlen (normalized_s2);
695 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
698 g_free (normalized_s1);
699 g_free (normalized_s2);
705 forward_chars_with_skipping (GtkTextIter *iter,
707 gboolean skip_invisible,
708 gboolean skip_nontext,
709 gboolean skip_decomp)
713 g_return_if_fail (count >= 0);
719 gboolean ignored = FALSE;
721 /* minimal workaround to avoid the infinite loop of bug #168247.
722 * It doesn't fix the problemjust the symptom...
724 if (gtk_text_iter_is_end (iter))
727 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
730 if (!ignored && skip_invisible &&
731 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
734 if (!ignored && skip_decomp)
736 /* being UTF8 correct sucks; this accounts for extra
737 offsets coming from canonical decompositions of
738 UTF8 characters (e.g. accented characters) which
739 g_utf8_normalize () performs */
744 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
745 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
746 i -= (g_utf8_strlen (normal, -1) - 1);
750 gtk_text_iter_forward_char (iter);
758 lines_match (const GtkTextIter *start,
760 gboolean visible_only,
762 GtkTextIter *match_start,
763 GtkTextIter *match_end)
770 if (*lines == NULL || **lines == '\0')
773 *match_start = *start;
780 gtk_text_iter_forward_line (&next);
782 /* No more text in buffer, but *lines is nonempty */
783 if (gtk_text_iter_equal (start, &next))
789 line_text = gtk_text_iter_get_visible_slice (start, &next);
791 line_text = gtk_text_iter_get_slice (start, &next);
796 line_text = gtk_text_iter_get_visible_text (start, &next);
798 line_text = gtk_text_iter_get_text (start, &next);
801 if (match_start) /* if this is the first line we're matching */
803 found = g_utf8_strcasestr (line_text, *lines);
807 /* If it's not the first line, we have to match from the
810 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
823 /* Get offset to start of search string */
824 offset = g_utf8_strlen (line_text, found - line_text);
828 /* If match start needs to be returned, set it to the
829 * start of the search string.
831 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
837 /* Go to end of search string */
838 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
847 /* pass NULL for match_start, since we don't need to find the
850 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
853 /* strsplit () that retains the delimiter as part of the string. */
855 strbreakup (const char *string,
856 const char *delimiter,
859 GSList *string_list = NULL, *slist;
860 gchar **str_array, *s, *casefold, *new_string;
863 g_return_val_if_fail (string != NULL, NULL);
864 g_return_val_if_fail (delimiter != NULL, NULL);
867 max_tokens = G_MAXINT;
869 s = strstr (string, delimiter);
872 guint delimiter_len = strlen (delimiter);
878 len = s - string + delimiter_len;
879 new_string = g_new (gchar, len + 1);
880 strncpy (new_string, string, len);
882 casefold = g_utf8_casefold (new_string, -1);
884 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
886 string_list = g_slist_prepend (string_list, new_string);
888 string = s + delimiter_len;
889 s = strstr (string, delimiter);
890 } while (--max_tokens && s);
896 casefold = g_utf8_casefold (string, -1);
897 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
899 string_list = g_slist_prepend (string_list, new_string);
902 str_array = g_new (gchar*, n);
906 str_array[i--] = NULL;
907 for (slist = string_list; slist; slist = slist->next)
908 str_array[i--] = slist->data;
910 g_slist_free (string_list);
916 empathy_text_iter_forward_search (const GtkTextIter *iter,
918 GtkTextIter *match_start,
919 GtkTextIter *match_end,
920 const GtkTextIter *limit)
922 gchar **lines = NULL;
924 gboolean retval = FALSE;
926 gboolean visible_only;
929 g_return_val_if_fail (iter != NULL, FALSE);
930 g_return_val_if_fail (str != NULL, FALSE);
932 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
936 /* If we can move one char, return the empty string there */
939 if (gtk_text_iter_forward_char (&match)) {
940 if (limit && gtk_text_iter_equal (&match, limit)) {
945 *match_start = match;
959 /* locate all lines */
960 lines = strbreakup (str, "\n", -1);
965 /* This loop has an inefficient worst-case, where
966 * gtk_text_iter_get_text () is called repeatedly on
971 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
975 if (lines_match (&search, (const gchar**)lines,
976 visible_only, slice, &match, &end)) {
978 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
982 *match_start = match;
990 } while (gtk_text_iter_forward_line (&search));
992 g_strfreev ((gchar **) lines);
998 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1002 const gchar *ret = NULL;
1005 gchar *caseless_haystack;
1008 g_return_val_if_fail (haystack != NULL, NULL);
1009 g_return_val_if_fail (needle != NULL, NULL);
1011 casefold = g_utf8_casefold (haystack, -1);
1012 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1015 needle_len = g_utf8_strlen (needle, -1);
1016 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1018 if (needle_len == 0)
1020 ret = (gchar *) haystack;
1024 if (haystack_len < needle_len)
1030 i = haystack_len - needle_len;
1031 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1032 needle_len = strlen (needle);
1034 while (p >= caseless_haystack)
1036 if (strncmp (p, needle, needle_len) == 0)
1038 ret = pointer_from_offset_skipping_decomp (haystack, i);
1042 p = g_utf8_prev_char (p);
1047 g_free (caseless_haystack);
1053 backward_lines_match (const GtkTextIter *start,
1054 const gchar **lines,
1055 gboolean visible_only,
1057 GtkTextIter *match_start,
1058 GtkTextIter *match_end)
1060 GtkTextIter line, next;
1065 if (*lines == NULL || **lines == '\0')
1068 *match_start = *start;
1070 *match_end = *start;
1074 line = next = *start;
1075 if (gtk_text_iter_get_line_offset (&next) == 0)
1077 if (!gtk_text_iter_backward_line (&next))
1081 gtk_text_iter_set_line_offset (&next, 0);
1086 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1088 line_text = gtk_text_iter_get_slice (&next, &line);
1093 line_text = gtk_text_iter_get_visible_text (&next, &line);
1095 line_text = gtk_text_iter_get_text (&next, &line);
1098 if (match_start) /* if this is the first line we're matching */
1100 found = g_utf8_strrcasestr (line_text, *lines);
1104 /* If it's not the first line, we have to match from the
1105 * start of the line.
1107 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1120 /* Get offset to start of search string */
1121 offset = g_utf8_strlen (line_text, found - line_text);
1123 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1125 /* If match start needs to be returned, set it to the
1126 * start of the search string.
1130 *match_start = next;
1133 /* Go to end of search string */
1134 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1143 /* try to match the rest of the lines forward, passing NULL
1144 * for match_start so lines_match will try to match the entire
1146 return lines_match (&next, lines, visible_only,
1147 slice, NULL, match_end);
1151 empathy_text_iter_backward_search (const GtkTextIter *iter,
1153 GtkTextIter *match_start,
1154 GtkTextIter *match_end,
1155 const GtkTextIter *limit)
1157 gchar **lines = NULL;
1159 gboolean retval = FALSE;
1161 gboolean visible_only;
1164 g_return_val_if_fail (iter != NULL, FALSE);
1165 g_return_val_if_fail (str != NULL, FALSE);
1167 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1172 /* If we can move one char, return the empty string there */
1175 if (gtk_text_iter_backward_char (&match))
1177 if (limit && gtk_text_iter_equal (&match, limit))
1181 *match_start = match;
1192 visible_only = TRUE;
1195 /* locate all lines */
1196 lines = strbreakup (str, "\n", -1);
1202 /* This loop has an inefficient worst-case, where
1203 * gtk_text_iter_get_text () is called repeatedly on
1208 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1211 if (backward_lines_match (&search, (const gchar**)lines,
1212 visible_only, slice, &match, &end))
1214 if (limit == NULL || (limit &&
1215 gtk_text_iter_compare (&end, limit) > 0))
1220 *match_start = match;
1227 if (gtk_text_iter_get_line_offset (&search) == 0)
1229 if (!gtk_text_iter_backward_line (&search))
1234 gtk_text_iter_set_line_offset (&search, 0);
1238 g_strfreev ((gchar **) lines);
1244 empathy_window_get_is_visible (GtkWindow *window)
1246 GdkWindowState state;
1247 GdkWindow *gdk_window;
1249 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1251 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1256 state = gdk_window_get_state (gdk_window);
1257 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1265 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1267 GdkRectangle icon_location;
1270 GdkWindow *gdk_window;
1272 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1273 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1274 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1276 data[0] = icon_location.x;
1277 data[1] = icon_location.y;
1278 data[2] = icon_location.width;
1279 data[3] = icon_location.height;
1281 XChangeProperty (dpy,
1282 GDK_WINDOW_XID (gdk_window),
1283 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1284 "_NET_WM_ICON_GEOMETRY"),
1285 XA_CARDINAL, 32, PropModeReplace,
1286 (guchar *)&data, 4);
1288 gtk_window_set_skip_taskbar_hint (window, TRUE);
1289 gtk_window_iconify (window);
1292 /* Takes care of moving the window to the current workspace. */
1294 empathy_window_present (GtkWindow *window,
1295 gboolean steal_focus)
1298 GdkWindow *gdk_window;
1300 g_return_if_fail (GTK_IS_WINDOW (window));
1302 /* Move the window to the current workspace before trying to show it.
1303 * This is the behaviour people expect when clicking on the statusbar icon. */
1304 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1309 /* Has no effect if the WM has viewports, like compiz */
1310 gdk_x11_window_move_to_current_desktop (gdk_window);
1312 /* If window is still off-screen, hide it to force it to
1313 * reposition on the current workspace. */
1314 gtk_window_get_position (window, &x, &y);
1315 gtk_window_get_size (window, &w, &h);
1316 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1317 gtk_widget_hide (GTK_WIDGET (window));
1320 timestamp = gtk_get_current_event_time ();
1321 gtk_window_present_with_time (window, timestamp);
1322 gtk_window_set_skip_taskbar_hint (window, FALSE);
1323 gtk_window_deiconify (window);
1327 empathy_get_toplevel_window (GtkWidget *widget)
1329 GtkWidget *toplevel;
1331 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1333 toplevel = gtk_widget_get_toplevel (widget);
1334 if (GTK_IS_WINDOW (toplevel) &&
1335 gtk_widget_is_toplevel (toplevel)) {
1336 return GTK_WINDOW (toplevel);
1342 /** empathy_make_absolute_url_len:
1346 * Same as #empathy_make_absolute_url but for a limited string length
1349 empathy_make_absolute_url_len (const gchar *url,
1352 g_return_val_if_fail (url != NULL, NULL);
1354 if (g_str_has_prefix (url, "ghelp:") ||
1355 g_str_has_prefix (url, "mailto:") ||
1356 strstr (url, ":/")) {
1357 return g_strndup (url, len);
1360 if (strstr (url, "@")) {
1361 return g_strdup_printf ("mailto:%.*s", len, url);
1364 return g_strdup_printf ("http://%.*s", len, url);
1367 /** empathy_make_absolute_url:
1370 * The URL opening code can't handle schemeless strings, so we try to be
1371 * smart and add http if there is no scheme or doesn't look like a mail
1372 * address. This should work in most cases, and let us click on strings
1373 * like "www.gnome.org".
1375 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1376 * g_free when your are done with it
1379 empathy_make_absolute_url (const gchar *url)
1381 return empathy_make_absolute_url_len (url, strlen (url));
1385 empathy_url_show (GtkWidget *parent,
1389 GError *error = NULL;
1391 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1392 g_return_if_fail (url != NULL);
1394 real_url = empathy_make_absolute_url (url);
1396 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1397 gtk_get_current_event_time (), &error);
1402 dialog = gtk_message_dialog_new (NULL, 0,
1403 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1404 _("Unable to open URI"));
1405 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1406 "%s", error->message);
1408 g_signal_connect (dialog, "response",
1409 G_CALLBACK (gtk_widget_destroy),
1411 gtk_window_present (GTK_WINDOW (dialog));
1413 g_clear_error (&error);
1420 link_button_hook (GtkLinkButton *button,
1424 empathy_url_show (GTK_WIDGET (button), link);
1428 empathy_link_button_new (const gchar *url,
1431 static gboolean hook = FALSE;
1435 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1438 return gtk_link_button_new_with_label (url, title);
1442 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1447 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1449 g_signal_handlers_block_by_func (widget, callback, user_data);
1450 g_object_set (widget, "active", active, NULL);
1451 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1455 file_manager_send_file_response_cb (GtkDialog *widget,
1457 EmpathyContact *contact)
1459 EmpathyFTFactory *factory;
1462 GtkRecentManager *manager;
1464 if (response_id == GTK_RESPONSE_OK) {
1465 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1466 uri = g_file_get_uri (file);
1468 factory = empathy_ft_factory_dup_singleton ();
1470 empathy_ft_factory_new_transfer_outgoing (factory, contact,
1473 manager = gtk_recent_manager_get_default ();
1474 gtk_recent_manager_add_item (manager, uri);
1477 g_object_unref (factory);
1478 g_object_unref (file);
1481 gtk_widget_destroy (GTK_WIDGET (widget));
1485 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1490 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1492 DEBUG ("Creating selection file chooser");
1494 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1496 GTK_FILE_CHOOSER_ACTION_OPEN,
1498 GTK_RESPONSE_CANCEL,
1502 button = gtk_button_new_with_mnemonic (_("_Send"));
1503 gtk_button_set_image (GTK_BUTTON (button),
1504 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1505 GTK_ICON_SIZE_BUTTON));
1506 gtk_widget_show (button);
1507 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1509 gtk_widget_set_can_default (button, TRUE);
1510 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1513 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1515 g_signal_connect (widget, "response",
1516 G_CALLBACK (file_manager_send_file_response_cb),
1519 gtk_widget_show (widget);
1523 file_manager_receive_file_response_cb (GtkDialog *dialog,
1524 GtkResponseType response,
1525 EmpathyFTHandler *handler)
1527 EmpathyFTFactory *factory;
1530 if (response == GTK_RESPONSE_OK) {
1531 factory = empathy_ft_factory_dup_singleton ();
1532 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1534 empathy_ft_factory_set_destination_for_incoming_handler
1535 (factory, handler, file);
1537 g_object_unref (factory);
1538 g_object_unref (file);
1540 /* unref the handler, as we dismissed the file chooser,
1541 * and refused the transfer.
1543 g_object_unref (handler);
1546 gtk_widget_destroy (GTK_WIDGET (dialog));
1550 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1554 widget = gtk_file_chooser_dialog_new (_("Select a destination"),
1556 GTK_FILE_CHOOSER_ACTION_SAVE,
1558 GTK_RESPONSE_CANCEL,
1562 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1563 empathy_ft_handler_get_filename (handler));
1564 gtk_file_chooser_set_do_overwrite_confirmation
1565 (GTK_FILE_CHOOSER (widget), TRUE);
1567 g_signal_connect (widget, "response",
1568 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1570 gtk_widget_show (widget);
1574 empathy_string_parser_substr (GString *string,
1577 EmpathyStringParser *parsers)
1579 if (parsers != NULL && parsers[0] != NULL) {
1580 parsers[0] (string, text, len, parsers + 1);
1582 g_string_append_len (string, text, len);
1587 empathy_string_parser_link (GString *string,
1593 GMatchInfo *match_info;
1597 /* Add <a href></a> arround links */
1598 uri_regex = empathy_uri_regex_dup_singleton ();
1599 match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
1606 g_match_info_fetch_pos (match_info, 0, &s, &e);
1609 /* Append the text between last link (or the
1610 * start of the message) and this link */
1611 empathy_string_parser_substr (string, text + last,
1616 /* Append the link inside <a href=""></a> tag */
1617 real_url = empathy_make_absolute_url_len (text + s, e - s);
1619 g_string_append_printf (string, "<a href=\"%s\">",
1621 g_string_append_len (string, text + s, e - s);
1622 g_string_append (string, "</a>");
1626 } while (g_match_info_next (match_info, NULL));
1629 empathy_string_parser_substr (string, text + last, len - last, user_data);
1631 g_match_info_free (match_info);
1632 g_regex_unref (uri_regex);
1636 empathy_string_parser_escape (GString *string,
1643 escaped = g_markup_escape_text (text, len);
1644 g_string_append (string, escaped);