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 <libmissioncontrol/mc-profile.h>
43 #include "empathy-ui-utils.h"
44 #include "empathy-images.h"
45 #include "empathy-conf.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
48 #include <libempathy/empathy-debug.h>
49 #include <libempathy/empathy-utils.h>
50 #include <libempathy/empathy-dispatcher.h>
51 #include <libempathy/empathy-idle.h>
52 #include <libempathy/empathy-ft-factory.h>
54 #define SCHEMES "(https?|s?ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\
56 #define BODY "([^\\ \\n]+)"
57 #define END_BODY "([^\\ \\n]*[^,;\?><()\\ \"\\.\\n])"
58 #define URI_REGEX "("SCHEMES"://"END_BODY")" \
59 "|((mailto:)?"BODY"@"BODY"\\."END_BODY")"\
60 "|((www|ftp)\\."END_BODY")"
63 empathy_gtk_init (void)
65 static gboolean initialized = FALSE;
71 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
72 PKGDATADIR G_DIR_SEPARATOR_S "icons");
78 empathy_uri_regex_dup_singleton (void)
80 static GRegex *uri_regex = NULL;
82 /* We intentionally leak the regex so it's not recomputed */
84 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
87 return g_regex_ref (uri_regex);
91 builder_get_file_valist (const gchar *filename,
92 const gchar *first_object,
100 DEBUG ("Loading file %s", filename);
102 gui = gtk_builder_new ();
103 if (!gtk_builder_add_from_file (gui, filename, &error)) {
104 g_critical ("GtkBuilder Error: %s", error->message);
105 g_clear_error (&error);
106 g_object_unref (gui);
110 for (name = first_object; name; name = va_arg (args, const gchar *)) {
111 object_ptr = va_arg (args, GObject**);
113 *object_ptr = gtk_builder_get_object (gui, name);
116 g_warning ("File is missing object '%s'.", name);
125 empathy_builder_get_file (const gchar *filename,
126 const gchar *first_object,
132 va_start (args, first_object);
133 gui = builder_get_file_valist (filename, first_object, args);
140 empathy_builder_connect (GtkBuilder *gui,
151 va_start (args, first_object);
152 for (name = first_object; name; name = va_arg (args, const gchar *)) {
153 signal = va_arg (args, const gchar *);
154 callback = va_arg (args, GCallback);
156 object = gtk_builder_get_object (gui, name);
158 g_warning ("File is missing object '%s'.", name);
162 g_signal_connect (object, signal, callback, user_data);
169 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
172 /* On construction gui sinks the initial reference to widget. When gui
173 * is finalized it will drop its ref to widget. We take our own ref to
174 * prevent widget being finalised. The widget is forced to have a
175 * floating reference, like when it was initially unowned so that it can
176 * be used like any other GtkWidget. */
178 g_object_ref (widget);
179 g_object_force_floating (G_OBJECT (widget));
180 g_object_unref (gui);
186 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
189 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
190 return EMPATHY_IMAGE_AVAILABLE;
191 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
192 return EMPATHY_IMAGE_BUSY;
193 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
194 return EMPATHY_IMAGE_AWAY;
195 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
196 return EMPATHY_IMAGE_EXT_AWAY;
197 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
198 return EMPATHY_IMAGE_HIDDEN;
199 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
200 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
201 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
202 return EMPATHY_IMAGE_OFFLINE;
203 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
211 empathy_icon_name_for_contact (EmpathyContact *contact)
213 TpConnectionPresenceType presence;
215 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
216 EMPATHY_IMAGE_OFFLINE);
218 presence = empathy_contact_get_presence (contact);
219 return empathy_icon_name_for_presence (presence);
223 empathy_pixbuf_from_data (gchar *data,
226 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
230 empathy_pixbuf_from_data_and_mime (gchar *data,
234 GdkPixbufLoader *loader;
235 GdkPixbufFormat *format;
236 GdkPixbuf *pixbuf = NULL;
238 GError *error = NULL;
244 loader = gdk_pixbuf_loader_new ();
245 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
246 DEBUG ("Failed to write to pixbuf loader: %s",
247 error ? error->message : "No error given");
250 if (!gdk_pixbuf_loader_close (loader, &error)) {
251 DEBUG ("Failed to close pixbuf loader: %s",
252 error ? error->message : "No error given");
256 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
258 g_object_ref (pixbuf);
260 if (mime_type != NULL) {
261 format = gdk_pixbuf_loader_get_format (loader);
262 mime_types = gdk_pixbuf_format_get_mime_types (format);
264 *mime_type = g_strdup (*mime_types);
265 if (mime_types[1] != NULL) {
266 DEBUG ("Loader supports more than one mime "
267 "type! Picking the first one, %s",
270 g_strfreev (mime_types);
275 g_clear_error (&error);
276 g_object_unref (loader);
284 gboolean preserve_aspect_ratio;
288 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
291 struct SizeData *data)
293 g_return_if_fail (width > 0 && height > 0);
295 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
296 if (width < data->width && height < data->height) {
301 if (data->width < 0) {
302 width = width * (double) data->height / (gdouble) height;
303 height = data->height;
304 } else if (data->height < 0) {
305 height = height * (double) data->width / (double) width;
307 } else if ((double) height * (double) data->width >
308 (double) width * (double) data->height) {
309 width = 0.5 + (double) width * (double) data->height / (double) height;
310 height = data->height;
312 height = 0.5 + (double) height * (double) data->width / (double) width;
316 if (data->width > 0) {
320 if (data->height > 0) {
321 height = data->height;
325 gdk_pixbuf_loader_set_size (loader, width, height);
329 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
331 gint width, height, rowstride;
334 width = gdk_pixbuf_get_width (pixbuf);
335 height = gdk_pixbuf_get_height (pixbuf);
336 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
337 pixels = gdk_pixbuf_get_pixels (pixbuf);
339 if (width < 6 || height < 6) {
347 pixels[rowstride + 3] = 0x80;
348 pixels[rowstride * 2 + 3] = 0xC0;
351 pixels[width * 4 - 1] = 0;
352 pixels[width * 4 - 5] = 0x80;
353 pixels[width * 4 - 9] = 0xC0;
354 pixels[rowstride + (width * 4) - 1] = 0x80;
355 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
358 pixels[(height - 1) * rowstride + 3] = 0;
359 pixels[(height - 1) * rowstride + 7] = 0x80;
360 pixels[(height - 1) * rowstride + 11] = 0xC0;
361 pixels[(height - 2) * rowstride + 3] = 0x80;
362 pixels[(height - 3) * rowstride + 3] = 0xC0;
365 pixels[height * rowstride - 1] = 0;
366 pixels[(height - 1) * rowstride - 1] = 0x80;
367 pixels[(height - 2) * rowstride - 1] = 0xC0;
368 pixels[height * rowstride - 5] = 0x80;
369 pixels[height * rowstride - 9] = 0xC0;
373 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
375 gint width, height, rowstride, i;
379 width = gdk_pixbuf_get_width (pixbuf);
380 height = gdk_pixbuf_get_height (pixbuf);
381 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
382 pixels = gdk_pixbuf_get_pixels (pixbuf);
385 for (i = 3; i < rowstride; i+=4) {
391 for (i = 1; i < height - 1; i++) {
392 row = pixels + (i*rowstride);
393 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
398 row = pixels + ((height-1) * rowstride);
399 for (i = 3; i < rowstride; i+=4) {
409 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
414 GdkPixbufLoader *loader;
415 struct SizeData data;
416 GError *error = NULL;
423 data.height = height;
424 data.preserve_aspect_ratio = TRUE;
426 loader = gdk_pixbuf_loader_new ();
428 g_signal_connect (loader, "size-prepared",
429 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
432 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
433 g_warning ("Couldn't write avatar image:%p with "
434 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
435 avatar->data, avatar->len, error->message);
436 g_error_free (error);
440 gdk_pixbuf_loader_close (loader, NULL);
442 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
443 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
444 GdkPixbuf *rounded_pixbuf;
446 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
447 gdk_pixbuf_get_width (pixbuf),
448 gdk_pixbuf_get_height (pixbuf));
449 gdk_pixbuf_copy_area (pixbuf, 0, 0,
450 gdk_pixbuf_get_width (pixbuf),
451 gdk_pixbuf_get_height (pixbuf),
454 pixbuf = rounded_pixbuf;
456 g_object_ref (pixbuf);
459 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
460 empathy_avatar_pixbuf_roundify (pixbuf);
463 g_object_unref (loader);
469 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
473 EmpathyAvatar *avatar;
475 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
477 avatar = empathy_contact_get_avatar (contact);
479 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
483 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
488 width = gdk_pixbuf_get_width (pixbuf);
489 height = gdk_pixbuf_get_height (pixbuf);
491 if (width > 0 && (width > max_size || height > max_size)) {
492 factor = (gdouble) max_size / MAX (width, height);
494 width = width * factor;
495 height = height * factor;
497 return gdk_pixbuf_scale_simple (pixbuf,
502 return g_object_ref (pixbuf);
506 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
511 GError *error = NULL;
517 theme = gtk_icon_theme_get_default ();
519 pixbuf = gtk_icon_theme_load_icon (theme,
525 DEBUG ("Error loading icon: %s", error->message);
526 g_clear_error (&error);
533 empathy_pixbuf_from_icon_name (const gchar *icon_name,
534 GtkIconSize icon_size)
543 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
547 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
551 empathy_filename_from_icon_name (const gchar *icon_name,
552 GtkIconSize icon_size)
554 GtkIconTheme *icon_theme;
555 GtkIconInfo *icon_info;
560 icon_theme = gtk_icon_theme_get_default ();
562 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
566 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
567 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
568 gtk_icon_info_free (icon_info);
573 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
574 * that to make it easier to apply changes from the original code.
576 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
578 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
579 * decomposable character it consumes the decomposition length from the given
580 * offset. So it's useful when the offset was calculated for the normalized
581 * version of str, but we need a pointer to str itself. */
583 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
585 gchar *casefold, *normal;
591 q = g_utf8_next_char (p);
592 casefold = g_utf8_casefold (p, q - p);
593 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
594 offset -= g_utf8_strlen (normal, -1);
603 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
607 const gchar *ret = NULL;
610 gchar *caseless_haystack;
613 g_return_val_if_fail (haystack != NULL, NULL);
614 g_return_val_if_fail (needle != NULL, NULL);
616 casefold = g_utf8_casefold (haystack, -1);
617 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
620 needle_len = g_utf8_strlen (needle, -1);
621 haystack_len = g_utf8_strlen (caseless_haystack, -1);
625 ret = (gchar *) haystack;
629 if (haystack_len < needle_len)
635 p = (gchar *) caseless_haystack;
636 needle_len = strlen (needle);
641 if ((strncmp (p, needle, needle_len) == 0))
643 ret = pointer_from_offset_skipping_decomp (haystack, i);
647 p = g_utf8_next_char (p);
652 g_free (caseless_haystack);
658 g_utf8_caselessnmatch (const char *s1, const char *s2,
659 gssize n1, gssize n2)
662 gchar *normalized_s1;
663 gchar *normalized_s2;
666 gboolean ret = FALSE;
668 g_return_val_if_fail (s1 != NULL, FALSE);
669 g_return_val_if_fail (s2 != NULL, FALSE);
670 g_return_val_if_fail (n1 > 0, FALSE);
671 g_return_val_if_fail (n2 > 0, FALSE);
673 casefold = g_utf8_casefold (s1, n1);
674 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
677 casefold = g_utf8_casefold (s2, n2);
678 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
681 len_s1 = strlen (normalized_s1);
682 len_s2 = strlen (normalized_s2);
687 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
690 g_free (normalized_s1);
691 g_free (normalized_s2);
697 forward_chars_with_skipping (GtkTextIter *iter,
699 gboolean skip_invisible,
700 gboolean skip_nontext,
701 gboolean skip_decomp)
705 g_return_if_fail (count >= 0);
711 gboolean ignored = FALSE;
713 /* minimal workaround to avoid the infinite loop of bug #168247.
714 * It doesn't fix the problemjust the symptom...
716 if (gtk_text_iter_is_end (iter))
719 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
722 if (!ignored && skip_invisible &&
723 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
726 if (!ignored && skip_decomp)
728 /* being UTF8 correct sucks; this accounts for extra
729 offsets coming from canonical decompositions of
730 UTF8 characters (e.g. accented characters) which
731 g_utf8_normalize () performs */
736 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
737 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
738 i -= (g_utf8_strlen (normal, -1) - 1);
742 gtk_text_iter_forward_char (iter);
750 lines_match (const GtkTextIter *start,
752 gboolean visible_only,
754 GtkTextIter *match_start,
755 GtkTextIter *match_end)
762 if (*lines == NULL || **lines == '\0')
765 *match_start = *start;
772 gtk_text_iter_forward_line (&next);
774 /* No more text in buffer, but *lines is nonempty */
775 if (gtk_text_iter_equal (start, &next))
781 line_text = gtk_text_iter_get_visible_slice (start, &next);
783 line_text = gtk_text_iter_get_slice (start, &next);
788 line_text = gtk_text_iter_get_visible_text (start, &next);
790 line_text = gtk_text_iter_get_text (start, &next);
793 if (match_start) /* if this is the first line we're matching */
795 found = g_utf8_strcasestr (line_text, *lines);
799 /* If it's not the first line, we have to match from the
802 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
815 /* Get offset to start of search string */
816 offset = g_utf8_strlen (line_text, found - line_text);
820 /* If match start needs to be returned, set it to the
821 * start of the search string.
823 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
829 /* Go to end of search string */
830 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
839 /* pass NULL for match_start, since we don't need to find the
842 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
845 /* strsplit () that retains the delimiter as part of the string. */
847 strbreakup (const char *string,
848 const char *delimiter,
851 GSList *string_list = NULL, *slist;
852 gchar **str_array, *s, *casefold, *new_string;
855 g_return_val_if_fail (string != NULL, NULL);
856 g_return_val_if_fail (delimiter != NULL, NULL);
859 max_tokens = G_MAXINT;
861 s = strstr (string, delimiter);
864 guint delimiter_len = strlen (delimiter);
870 len = s - string + delimiter_len;
871 new_string = g_new (gchar, len + 1);
872 strncpy (new_string, string, len);
874 casefold = g_utf8_casefold (new_string, -1);
876 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
878 string_list = g_slist_prepend (string_list, new_string);
880 string = s + delimiter_len;
881 s = strstr (string, delimiter);
882 } while (--max_tokens && s);
888 casefold = g_utf8_casefold (string, -1);
889 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
891 string_list = g_slist_prepend (string_list, new_string);
894 str_array = g_new (gchar*, n);
898 str_array[i--] = NULL;
899 for (slist = string_list; slist; slist = slist->next)
900 str_array[i--] = slist->data;
902 g_slist_free (string_list);
908 empathy_text_iter_forward_search (const GtkTextIter *iter,
910 GtkTextIter *match_start,
911 GtkTextIter *match_end,
912 const GtkTextIter *limit)
914 gchar **lines = NULL;
916 gboolean retval = FALSE;
918 gboolean visible_only;
921 g_return_val_if_fail (iter != NULL, FALSE);
922 g_return_val_if_fail (str != NULL, FALSE);
924 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
928 /* If we can move one char, return the empty string there */
931 if (gtk_text_iter_forward_char (&match)) {
932 if (limit && gtk_text_iter_equal (&match, limit)) {
937 *match_start = match;
951 /* locate all lines */
952 lines = strbreakup (str, "\n", -1);
957 /* This loop has an inefficient worst-case, where
958 * gtk_text_iter_get_text () is called repeatedly on
963 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
967 if (lines_match (&search, (const gchar**)lines,
968 visible_only, slice, &match, &end)) {
970 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
974 *match_start = match;
982 } while (gtk_text_iter_forward_line (&search));
984 g_strfreev ((gchar **) lines);
990 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
994 const gchar *ret = NULL;
997 gchar *caseless_haystack;
1000 g_return_val_if_fail (haystack != NULL, NULL);
1001 g_return_val_if_fail (needle != NULL, NULL);
1003 casefold = g_utf8_casefold (haystack, -1);
1004 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1007 needle_len = g_utf8_strlen (needle, -1);
1008 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1010 if (needle_len == 0)
1012 ret = (gchar *) haystack;
1016 if (haystack_len < needle_len)
1022 i = haystack_len - needle_len;
1023 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1024 needle_len = strlen (needle);
1026 while (p >= caseless_haystack)
1028 if (strncmp (p, needle, needle_len) == 0)
1030 ret = pointer_from_offset_skipping_decomp (haystack, i);
1034 p = g_utf8_prev_char (p);
1039 g_free (caseless_haystack);
1045 backward_lines_match (const GtkTextIter *start,
1046 const gchar **lines,
1047 gboolean visible_only,
1049 GtkTextIter *match_start,
1050 GtkTextIter *match_end)
1052 GtkTextIter line, next;
1057 if (*lines == NULL || **lines == '\0')
1060 *match_start = *start;
1062 *match_end = *start;
1066 line = next = *start;
1067 if (gtk_text_iter_get_line_offset (&next) == 0)
1069 if (!gtk_text_iter_backward_line (&next))
1073 gtk_text_iter_set_line_offset (&next, 0);
1078 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1080 line_text = gtk_text_iter_get_slice (&next, &line);
1085 line_text = gtk_text_iter_get_visible_text (&next, &line);
1087 line_text = gtk_text_iter_get_text (&next, &line);
1090 if (match_start) /* if this is the first line we're matching */
1092 found = g_utf8_strrcasestr (line_text, *lines);
1096 /* If it's not the first line, we have to match from the
1097 * start of the line.
1099 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1112 /* Get offset to start of search string */
1113 offset = g_utf8_strlen (line_text, found - line_text);
1115 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1117 /* If match start needs to be returned, set it to the
1118 * start of the search string.
1122 *match_start = next;
1125 /* Go to end of search string */
1126 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1135 /* try to match the rest of the lines forward, passing NULL
1136 * for match_start so lines_match will try to match the entire
1138 return lines_match (&next, lines, visible_only,
1139 slice, NULL, match_end);
1143 empathy_text_iter_backward_search (const GtkTextIter *iter,
1145 GtkTextIter *match_start,
1146 GtkTextIter *match_end,
1147 const GtkTextIter *limit)
1149 gchar **lines = NULL;
1151 gboolean retval = FALSE;
1153 gboolean visible_only;
1156 g_return_val_if_fail (iter != NULL, FALSE);
1157 g_return_val_if_fail (str != NULL, FALSE);
1159 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1164 /* If we can move one char, return the empty string there */
1167 if (gtk_text_iter_backward_char (&match))
1169 if (limit && gtk_text_iter_equal (&match, limit))
1173 *match_start = match;
1184 visible_only = TRUE;
1187 /* locate all lines */
1188 lines = strbreakup (str, "\n", -1);
1194 /* This loop has an inefficient worst-case, where
1195 * gtk_text_iter_get_text () is called repeatedly on
1200 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1203 if (backward_lines_match (&search, (const gchar**)lines,
1204 visible_only, slice, &match, &end))
1206 if (limit == NULL || (limit &&
1207 gtk_text_iter_compare (&end, limit) > 0))
1212 *match_start = match;
1219 if (gtk_text_iter_get_line_offset (&search) == 0)
1221 if (!gtk_text_iter_backward_line (&search))
1226 gtk_text_iter_set_line_offset (&search, 0);
1230 g_strfreev ((gchar **) lines);
1236 empathy_window_get_is_visible (GtkWindow *window)
1238 GdkWindowState state;
1239 GdkWindow *gdk_window;
1241 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1243 gdk_window = GTK_WIDGET (window)->window;
1248 state = gdk_window_get_state (gdk_window);
1249 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1257 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1259 GdkRectangle icon_location;
1262 GdkWindow *gdk_window;
1264 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1265 gdk_window = GTK_WIDGET (window)->window;
1266 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1268 data[0] = icon_location.x;
1269 data[1] = icon_location.y;
1270 data[2] = icon_location.width;
1271 data[3] = icon_location.height;
1273 XChangeProperty (dpy,
1274 GDK_WINDOW_XID (gdk_window),
1275 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1276 "_NET_WM_ICON_GEOMETRY"),
1277 XA_CARDINAL, 32, PropModeReplace,
1278 (guchar *)&data, 4);
1280 gtk_window_set_skip_taskbar_hint (window, TRUE);
1281 gtk_window_iconify (window);
1284 /* Takes care of moving the window to the current workspace. */
1286 empathy_window_present (GtkWindow *window,
1287 gboolean steal_focus)
1291 g_return_if_fail (GTK_IS_WINDOW (window));
1293 /* There are three cases: hidden, visible, visible on another
1297 if (!empathy_window_get_is_visible (window)) {
1298 /* Hide it so present brings it to the current workspace. */
1299 gtk_widget_hide (GTK_WIDGET (window));
1302 timestamp = gtk_get_current_event_time ();
1303 gtk_window_present_with_time (window, timestamp);
1304 gtk_window_set_skip_taskbar_hint (window, FALSE);
1305 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1306 * it deiconify automatically. */
1307 gtk_window_deiconify (window);
1311 empathy_get_toplevel_window (GtkWidget *widget)
1313 GtkWidget *toplevel;
1315 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1317 toplevel = gtk_widget_get_toplevel (widget);
1318 if (GTK_IS_WINDOW (toplevel) &&
1319 GTK_WIDGET_TOPLEVEL (toplevel)) {
1320 return GTK_WINDOW (toplevel);
1326 /* The URL opening code can't handle schemeless strings, so we try to be
1327 * smart and add http if there is no scheme or doesn't look like a mail
1328 * address. This should work in most cases, and let us click on strings
1329 * like "www.gnome.org".
1332 fixup_url (const gchar *url)
1334 if (g_str_has_prefix (url, "ghelp:") ||
1335 g_str_has_prefix (url, "mailto:") ||
1336 strstr (url, ":/")) {
1340 if (strstr (url, "@")) {
1341 return g_strdup_printf ("mailto:%s", url);
1344 return g_strdup_printf ("http://%s", url);
1348 empathy_url_show (GtkWidget *parent,
1352 GError *error = NULL;
1354 real_url = fixup_url (url);
1359 gtk_show_uri (gtk_widget_get_screen (parent), url,
1360 gtk_get_current_event_time (), &error);
1365 dialog = gtk_message_dialog_new (NULL, 0,
1366 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1367 _("Unable to open URI"));
1368 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1369 "%s", error->message);
1371 g_signal_connect (dialog, "response",
1372 G_CALLBACK (gtk_widget_destroy),
1374 gtk_window_present (GTK_WINDOW (dialog));
1376 g_clear_error (&error);
1383 link_button_hook (GtkLinkButton *button,
1387 empathy_url_show (GTK_WIDGET (button), link);
1391 empathy_link_button_new (const gchar *url,
1394 static gboolean hook = FALSE;
1398 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1401 return gtk_link_button_new_with_label (url, title);
1405 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1410 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1412 g_signal_handlers_block_by_func (widget, callback, user_data);
1413 g_object_set (widget, "active", active, NULL);
1414 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1418 file_manager_send_file_response_cb (GtkDialog *widget,
1420 EmpathyContact *contact)
1422 EmpathyFTFactory *factory;
1425 GtkRecentManager *manager;
1427 if (response_id == GTK_RESPONSE_OK) {
1428 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1429 uri = g_file_get_uri (file);
1431 factory = empathy_ft_factory_dup_singleton ();
1433 empathy_ft_factory_new_transfer_outgoing (factory, contact,
1436 manager = gtk_recent_manager_get_default ();
1437 gtk_recent_manager_add_item (manager, uri);
1440 g_object_unref (factory);
1441 g_object_unref (file);
1444 gtk_widget_destroy (GTK_WIDGET (widget));
1448 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1453 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1455 DEBUG ("Creating selection file chooser");
1457 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1459 GTK_FILE_CHOOSER_ACTION_OPEN,
1461 GTK_RESPONSE_CANCEL,
1465 button = gtk_button_new_with_mnemonic (_("_Send"));
1466 gtk_button_set_image (GTK_BUTTON (button),
1467 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1468 GTK_ICON_SIZE_BUTTON));
1469 gtk_widget_show (button);
1470 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1472 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1473 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1476 g_signal_connect (widget, "response",
1477 G_CALLBACK (file_manager_send_file_response_cb),
1480 gtk_widget_show (widget);
1484 file_manager_receive_file_response_cb (GtkDialog *dialog,
1485 GtkResponseType response,
1486 EmpathyFTHandler *handler)
1488 EmpathyFTFactory *factory;
1491 if (response == GTK_RESPONSE_OK) {
1492 factory = empathy_ft_factory_dup_singleton ();
1493 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1495 empathy_ft_factory_set_destination_for_incoming_handler
1496 (factory, handler, file);
1498 g_object_unref (factory);
1499 g_object_unref (file);
1501 /* unref the handler, as we dismissed the file chooser,
1502 * and refused the transfer.
1504 g_object_unref (handler);
1507 gtk_widget_destroy (GTK_WIDGET (dialog));
1511 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1515 widget = gtk_file_chooser_dialog_new (_("Select a destination"),
1517 GTK_FILE_CHOOSER_ACTION_SAVE,
1519 GTK_RESPONSE_CANCEL,
1523 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1524 empathy_ft_handler_get_filename (handler));
1525 gtk_file_chooser_set_do_overwrite_confirmation
1526 (GTK_FILE_CHOOSER (widget), TRUE);
1528 g_signal_connect (widget, "response",
1529 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1531 gtk_widget_show (widget);