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_from_account (EmpathyAccount *account)
190 profile = empathy_account_get_profile (account);
192 return mc_profile_get_icon_name (profile);
196 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
199 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
200 return EMPATHY_IMAGE_AVAILABLE;
201 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
202 return EMPATHY_IMAGE_BUSY;
203 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
204 return EMPATHY_IMAGE_AWAY;
205 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
206 return EMPATHY_IMAGE_EXT_AWAY;
207 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
208 return EMPATHY_IMAGE_HIDDEN;
209 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
210 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
211 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
212 return EMPATHY_IMAGE_OFFLINE;
213 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
221 empathy_icon_name_for_contact (EmpathyContact *contact)
223 TpConnectionPresenceType presence;
225 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
226 EMPATHY_IMAGE_OFFLINE);
228 presence = empathy_contact_get_presence (contact);
229 return empathy_icon_name_for_presence (presence);
233 empathy_pixbuf_from_data (gchar *data,
236 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
240 empathy_pixbuf_from_data_and_mime (gchar *data,
244 GdkPixbufLoader *loader;
245 GdkPixbufFormat *format;
246 GdkPixbuf *pixbuf = NULL;
248 GError *error = NULL;
254 loader = gdk_pixbuf_loader_new ();
255 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
256 DEBUG ("Failed to write to pixbuf loader: %s",
257 error ? error->message : "No error given");
260 if (!gdk_pixbuf_loader_close (loader, &error)) {
261 DEBUG ("Failed to close pixbuf loader: %s",
262 error ? error->message : "No error given");
266 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
268 g_object_ref (pixbuf);
270 if (mime_type != NULL) {
271 format = gdk_pixbuf_loader_get_format (loader);
272 mime_types = gdk_pixbuf_format_get_mime_types (format);
274 *mime_type = g_strdup (*mime_types);
275 if (mime_types[1] != NULL) {
276 DEBUG ("Loader supports more than one mime "
277 "type! Picking the first one, %s",
280 g_strfreev (mime_types);
285 g_clear_error (&error);
286 g_object_unref (loader);
294 gboolean preserve_aspect_ratio;
298 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
301 struct SizeData *data)
303 g_return_if_fail (width > 0 && height > 0);
305 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
306 if (width < data->width && height < data->height) {
311 if (data->width < 0) {
312 width = width * (double) data->height / (gdouble) height;
313 height = data->height;
314 } else if (data->height < 0) {
315 height = height * (double) data->width / (double) width;
317 } else if ((double) height * (double) data->width >
318 (double) width * (double) data->height) {
319 width = 0.5 + (double) width * (double) data->height / (double) height;
320 height = data->height;
322 height = 0.5 + (double) height * (double) data->width / (double) width;
326 if (data->width > 0) {
330 if (data->height > 0) {
331 height = data->height;
335 gdk_pixbuf_loader_set_size (loader, width, height);
339 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
341 gint width, height, rowstride;
344 width = gdk_pixbuf_get_width (pixbuf);
345 height = gdk_pixbuf_get_height (pixbuf);
346 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
347 pixels = gdk_pixbuf_get_pixels (pixbuf);
349 if (width < 6 || height < 6) {
357 pixels[rowstride + 3] = 0x80;
358 pixels[rowstride * 2 + 3] = 0xC0;
361 pixels[width * 4 - 1] = 0;
362 pixels[width * 4 - 5] = 0x80;
363 pixels[width * 4 - 9] = 0xC0;
364 pixels[rowstride + (width * 4) - 1] = 0x80;
365 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
368 pixels[(height - 1) * rowstride + 3] = 0;
369 pixels[(height - 1) * rowstride + 7] = 0x80;
370 pixels[(height - 1) * rowstride + 11] = 0xC0;
371 pixels[(height - 2) * rowstride + 3] = 0x80;
372 pixels[(height - 3) * rowstride + 3] = 0xC0;
375 pixels[height * rowstride - 1] = 0;
376 pixels[(height - 1) * rowstride - 1] = 0x80;
377 pixels[(height - 2) * rowstride - 1] = 0xC0;
378 pixels[height * rowstride - 5] = 0x80;
379 pixels[height * rowstride - 9] = 0xC0;
383 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
385 gint width, height, rowstride, i;
389 width = gdk_pixbuf_get_width (pixbuf);
390 height = gdk_pixbuf_get_height (pixbuf);
391 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
392 pixels = gdk_pixbuf_get_pixels (pixbuf);
395 for (i = 3; i < rowstride; i+=4) {
401 for (i = 1; i < height - 1; i++) {
402 row = pixels + (i*rowstride);
403 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
408 row = pixels + ((height-1) * rowstride);
409 for (i = 3; i < rowstride; i+=4) {
419 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
424 GdkPixbufLoader *loader;
425 struct SizeData data;
426 GError *error = NULL;
433 data.height = height;
434 data.preserve_aspect_ratio = TRUE;
436 loader = gdk_pixbuf_loader_new ();
438 g_signal_connect (loader, "size-prepared",
439 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
442 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
443 g_warning ("Couldn't write avatar image:%p with "
444 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
445 avatar->data, avatar->len, error->message);
446 g_error_free (error);
450 gdk_pixbuf_loader_close (loader, NULL);
452 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
453 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
454 GdkPixbuf *rounded_pixbuf;
456 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
457 gdk_pixbuf_get_width (pixbuf),
458 gdk_pixbuf_get_height (pixbuf));
459 gdk_pixbuf_copy_area (pixbuf, 0, 0,
460 gdk_pixbuf_get_width (pixbuf),
461 gdk_pixbuf_get_height (pixbuf),
464 pixbuf = rounded_pixbuf;
466 g_object_ref (pixbuf);
469 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
470 empathy_avatar_pixbuf_roundify (pixbuf);
473 g_object_unref (loader);
479 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
483 EmpathyAvatar *avatar;
485 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
487 avatar = empathy_contact_get_avatar (contact);
489 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
493 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
498 width = gdk_pixbuf_get_width (pixbuf);
499 height = gdk_pixbuf_get_height (pixbuf);
501 if (width > 0 && (width > max_size || height > max_size)) {
502 factor = (gdouble) max_size / MAX (width, height);
504 width = width * factor;
505 height = height * factor;
507 return gdk_pixbuf_scale_simple (pixbuf,
512 return g_object_ref (pixbuf);
516 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
521 GError *error = NULL;
527 theme = gtk_icon_theme_get_default ();
529 pixbuf = gtk_icon_theme_load_icon (theme,
535 DEBUG ("Error loading icon: %s", error->message);
536 g_clear_error (&error);
543 empathy_pixbuf_from_icon_name (const gchar *icon_name,
544 GtkIconSize icon_size)
553 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
557 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
561 empathy_filename_from_icon_name (const gchar *icon_name,
562 GtkIconSize icon_size)
564 GtkIconTheme *icon_theme;
565 GtkIconInfo *icon_info;
570 icon_theme = gtk_icon_theme_get_default ();
572 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
576 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
577 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
578 gtk_icon_info_free (icon_info);
583 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
584 * that to make it easier to apply changes from the original code.
586 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
588 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
589 * decomposable character it consumes the decomposition length from the given
590 * offset. So it's useful when the offset was calculated for the normalized
591 * version of str, but we need a pointer to str itself. */
593 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
595 gchar *casefold, *normal;
601 q = g_utf8_next_char (p);
602 casefold = g_utf8_casefold (p, q - p);
603 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
604 offset -= g_utf8_strlen (normal, -1);
613 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
617 const gchar *ret = NULL;
620 gchar *caseless_haystack;
623 g_return_val_if_fail (haystack != NULL, NULL);
624 g_return_val_if_fail (needle != NULL, NULL);
626 casefold = g_utf8_casefold (haystack, -1);
627 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
630 needle_len = g_utf8_strlen (needle, -1);
631 haystack_len = g_utf8_strlen (caseless_haystack, -1);
635 ret = (gchar *) haystack;
639 if (haystack_len < needle_len)
645 p = (gchar *) caseless_haystack;
646 needle_len = strlen (needle);
651 if ((strncmp (p, needle, needle_len) == 0))
653 ret = pointer_from_offset_skipping_decomp (haystack, i);
657 p = g_utf8_next_char (p);
662 g_free (caseless_haystack);
668 g_utf8_caselessnmatch (const char *s1, const char *s2,
669 gssize n1, gssize n2)
672 gchar *normalized_s1;
673 gchar *normalized_s2;
676 gboolean ret = FALSE;
678 g_return_val_if_fail (s1 != NULL, FALSE);
679 g_return_val_if_fail (s2 != NULL, FALSE);
680 g_return_val_if_fail (n1 > 0, FALSE);
681 g_return_val_if_fail (n2 > 0, FALSE);
683 casefold = g_utf8_casefold (s1, n1);
684 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
687 casefold = g_utf8_casefold (s2, n2);
688 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
691 len_s1 = strlen (normalized_s1);
692 len_s2 = strlen (normalized_s2);
697 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
700 g_free (normalized_s1);
701 g_free (normalized_s2);
707 forward_chars_with_skipping (GtkTextIter *iter,
709 gboolean skip_invisible,
710 gboolean skip_nontext,
711 gboolean skip_decomp)
715 g_return_if_fail (count >= 0);
721 gboolean ignored = FALSE;
723 /* minimal workaround to avoid the infinite loop of bug #168247.
724 * It doesn't fix the problemjust the symptom...
726 if (gtk_text_iter_is_end (iter))
729 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
732 if (!ignored && skip_invisible &&
733 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
736 if (!ignored && skip_decomp)
738 /* being UTF8 correct sucks; this accounts for extra
739 offsets coming from canonical decompositions of
740 UTF8 characters (e.g. accented characters) which
741 g_utf8_normalize () performs */
746 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
747 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
748 i -= (g_utf8_strlen (normal, -1) - 1);
752 gtk_text_iter_forward_char (iter);
760 lines_match (const GtkTextIter *start,
762 gboolean visible_only,
764 GtkTextIter *match_start,
765 GtkTextIter *match_end)
772 if (*lines == NULL || **lines == '\0')
775 *match_start = *start;
782 gtk_text_iter_forward_line (&next);
784 /* No more text in buffer, but *lines is nonempty */
785 if (gtk_text_iter_equal (start, &next))
791 line_text = gtk_text_iter_get_visible_slice (start, &next);
793 line_text = gtk_text_iter_get_slice (start, &next);
798 line_text = gtk_text_iter_get_visible_text (start, &next);
800 line_text = gtk_text_iter_get_text (start, &next);
803 if (match_start) /* if this is the first line we're matching */
805 found = g_utf8_strcasestr (line_text, *lines);
809 /* If it's not the first line, we have to match from the
812 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
825 /* Get offset to start of search string */
826 offset = g_utf8_strlen (line_text, found - line_text);
830 /* If match start needs to be returned, set it to the
831 * start of the search string.
833 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
839 /* Go to end of search string */
840 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
849 /* pass NULL for match_start, since we don't need to find the
852 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
855 /* strsplit () that retains the delimiter as part of the string. */
857 strbreakup (const char *string,
858 const char *delimiter,
861 GSList *string_list = NULL, *slist;
862 gchar **str_array, *s, *casefold, *new_string;
865 g_return_val_if_fail (string != NULL, NULL);
866 g_return_val_if_fail (delimiter != NULL, NULL);
869 max_tokens = G_MAXINT;
871 s = strstr (string, delimiter);
874 guint delimiter_len = strlen (delimiter);
880 len = s - string + delimiter_len;
881 new_string = g_new (gchar, len + 1);
882 strncpy (new_string, string, len);
884 casefold = g_utf8_casefold (new_string, -1);
886 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
888 string_list = g_slist_prepend (string_list, new_string);
890 string = s + delimiter_len;
891 s = strstr (string, delimiter);
892 } while (--max_tokens && s);
898 casefold = g_utf8_casefold (string, -1);
899 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
901 string_list = g_slist_prepend (string_list, new_string);
904 str_array = g_new (gchar*, n);
908 str_array[i--] = NULL;
909 for (slist = string_list; slist; slist = slist->next)
910 str_array[i--] = slist->data;
912 g_slist_free (string_list);
918 empathy_text_iter_forward_search (const GtkTextIter *iter,
920 GtkTextIter *match_start,
921 GtkTextIter *match_end,
922 const GtkTextIter *limit)
924 gchar **lines = NULL;
926 gboolean retval = FALSE;
928 gboolean visible_only;
931 g_return_val_if_fail (iter != NULL, FALSE);
932 g_return_val_if_fail (str != NULL, FALSE);
934 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
938 /* If we can move one char, return the empty string there */
941 if (gtk_text_iter_forward_char (&match)) {
942 if (limit && gtk_text_iter_equal (&match, limit)) {
947 *match_start = match;
961 /* locate all lines */
962 lines = strbreakup (str, "\n", -1);
967 /* This loop has an inefficient worst-case, where
968 * gtk_text_iter_get_text () is called repeatedly on
973 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
977 if (lines_match (&search, (const gchar**)lines,
978 visible_only, slice, &match, &end)) {
980 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
984 *match_start = match;
992 } while (gtk_text_iter_forward_line (&search));
994 g_strfreev ((gchar **) lines);
1000 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1004 const gchar *ret = NULL;
1007 gchar *caseless_haystack;
1010 g_return_val_if_fail (haystack != NULL, NULL);
1011 g_return_val_if_fail (needle != NULL, NULL);
1013 casefold = g_utf8_casefold (haystack, -1);
1014 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1017 needle_len = g_utf8_strlen (needle, -1);
1018 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1020 if (needle_len == 0)
1022 ret = (gchar *) haystack;
1026 if (haystack_len < needle_len)
1032 i = haystack_len - needle_len;
1033 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1034 needle_len = strlen (needle);
1036 while (p >= caseless_haystack)
1038 if (strncmp (p, needle, needle_len) == 0)
1040 ret = pointer_from_offset_skipping_decomp (haystack, i);
1044 p = g_utf8_prev_char (p);
1049 g_free (caseless_haystack);
1055 backward_lines_match (const GtkTextIter *start,
1056 const gchar **lines,
1057 gboolean visible_only,
1059 GtkTextIter *match_start,
1060 GtkTextIter *match_end)
1062 GtkTextIter line, next;
1067 if (*lines == NULL || **lines == '\0')
1070 *match_start = *start;
1072 *match_end = *start;
1076 line = next = *start;
1077 if (gtk_text_iter_get_line_offset (&next) == 0)
1079 if (!gtk_text_iter_backward_line (&next))
1083 gtk_text_iter_set_line_offset (&next, 0);
1088 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1090 line_text = gtk_text_iter_get_slice (&next, &line);
1095 line_text = gtk_text_iter_get_visible_text (&next, &line);
1097 line_text = gtk_text_iter_get_text (&next, &line);
1100 if (match_start) /* if this is the first line we're matching */
1102 found = g_utf8_strrcasestr (line_text, *lines);
1106 /* If it's not the first line, we have to match from the
1107 * start of the line.
1109 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1122 /* Get offset to start of search string */
1123 offset = g_utf8_strlen (line_text, found - line_text);
1125 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1127 /* If match start needs to be returned, set it to the
1128 * start of the search string.
1132 *match_start = next;
1135 /* Go to end of search string */
1136 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1145 /* try to match the rest of the lines forward, passing NULL
1146 * for match_start so lines_match will try to match the entire
1148 return lines_match (&next, lines, visible_only,
1149 slice, NULL, match_end);
1153 empathy_text_iter_backward_search (const GtkTextIter *iter,
1155 GtkTextIter *match_start,
1156 GtkTextIter *match_end,
1157 const GtkTextIter *limit)
1159 gchar **lines = NULL;
1161 gboolean retval = FALSE;
1163 gboolean visible_only;
1166 g_return_val_if_fail (iter != NULL, FALSE);
1167 g_return_val_if_fail (str != NULL, FALSE);
1169 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1174 /* If we can move one char, return the empty string there */
1177 if (gtk_text_iter_backward_char (&match))
1179 if (limit && gtk_text_iter_equal (&match, limit))
1183 *match_start = match;
1194 visible_only = TRUE;
1197 /* locate all lines */
1198 lines = strbreakup (str, "\n", -1);
1204 /* This loop has an inefficient worst-case, where
1205 * gtk_text_iter_get_text () is called repeatedly on
1210 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1213 if (backward_lines_match (&search, (const gchar**)lines,
1214 visible_only, slice, &match, &end))
1216 if (limit == NULL || (limit &&
1217 gtk_text_iter_compare (&end, limit) > 0))
1222 *match_start = match;
1229 if (gtk_text_iter_get_line_offset (&search) == 0)
1231 if (!gtk_text_iter_backward_line (&search))
1236 gtk_text_iter_set_line_offset (&search, 0);
1240 g_strfreev ((gchar **) lines);
1246 empathy_window_get_is_visible (GtkWindow *window)
1248 GdkWindowState state;
1249 GdkWindow *gdk_window;
1251 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1253 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1258 state = gdk_window_get_state (gdk_window);
1259 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1267 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1269 GdkRectangle icon_location;
1272 GdkWindow *gdk_window;
1274 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1275 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1276 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1278 data[0] = icon_location.x;
1279 data[1] = icon_location.y;
1280 data[2] = icon_location.width;
1281 data[3] = icon_location.height;
1283 XChangeProperty (dpy,
1284 GDK_WINDOW_XID (gdk_window),
1285 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1286 "_NET_WM_ICON_GEOMETRY"),
1287 XA_CARDINAL, 32, PropModeReplace,
1288 (guchar *)&data, 4);
1290 gtk_window_set_skip_taskbar_hint (window, TRUE);
1291 gtk_window_iconify (window);
1294 /* Takes care of moving the window to the current workspace. */
1296 empathy_window_present (GtkWindow *window,
1297 gboolean steal_focus)
1301 g_return_if_fail (GTK_IS_WINDOW (window));
1303 /* There are three cases: hidden, visible, visible on another
1307 if (!empathy_window_get_is_visible (window)) {
1308 /* Hide it so present brings it to the current workspace. */
1309 gtk_widget_hide (GTK_WIDGET (window));
1312 timestamp = gtk_get_current_event_time ();
1313 gtk_window_present_with_time (window, timestamp);
1314 gtk_window_set_skip_taskbar_hint (window, FALSE);
1315 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1316 * it deiconify automatically. */
1317 gtk_window_deiconify (window);
1321 empathy_get_toplevel_window (GtkWidget *widget)
1323 GtkWidget *toplevel;
1325 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1327 toplevel = gtk_widget_get_toplevel (widget);
1328 if (GTK_IS_WINDOW (toplevel) &&
1329 GTK_WIDGET_TOPLEVEL (toplevel)) {
1330 return GTK_WINDOW (toplevel);
1336 /** empathy_make_absolute_url:
1339 * The URL opening code can't handle schemeless strings, so we try to be
1340 * smart and add http if there is no scheme or doesn't look like a mail
1341 * address. This should work in most cases, and let us click on strings
1342 * like "www.gnome.org".
1344 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1345 * g_free when your are done with it
1348 empathy_make_absolute_url (const gchar *url)
1350 g_return_val_if_fail (url != NULL, NULL);
1352 if (g_str_has_prefix (url, "ghelp:") ||
1353 g_str_has_prefix (url, "mailto:") ||
1354 strstr (url, ":/")) {
1355 return g_strdup (url);
1358 if (strstr (url, "@")) {
1359 return g_strdup_printf ("mailto:%s", url);
1362 return g_strdup_printf ("http://%s", url);
1366 empathy_url_show (GtkWidget *parent,
1370 GError *error = NULL;
1372 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1373 g_return_if_fail (url != NULL);
1375 real_url = empathy_make_absolute_url (url);
1377 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1378 gtk_get_current_event_time (), &error);
1383 dialog = gtk_message_dialog_new (NULL, 0,
1384 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1385 _("Unable to open URI"));
1386 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1387 "%s", error->message);
1389 g_signal_connect (dialog, "response",
1390 G_CALLBACK (gtk_widget_destroy),
1392 gtk_window_present (GTK_WINDOW (dialog));
1394 g_clear_error (&error);
1401 link_button_hook (GtkLinkButton *button,
1405 empathy_url_show (GTK_WIDGET (button), link);
1409 empathy_link_button_new (const gchar *url,
1412 static gboolean hook = FALSE;
1416 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1419 return gtk_link_button_new_with_label (url, title);
1423 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1428 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1430 g_signal_handlers_block_by_func (widget, callback, user_data);
1431 g_object_set (widget, "active", active, NULL);
1432 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1436 file_manager_send_file_response_cb (GtkDialog *widget,
1438 EmpathyContact *contact)
1440 EmpathyFTFactory *factory;
1443 GtkRecentManager *manager;
1445 if (response_id == GTK_RESPONSE_OK) {
1446 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1447 uri = g_file_get_uri (file);
1449 factory = empathy_ft_factory_dup_singleton ();
1451 empathy_ft_factory_new_transfer_outgoing (factory, contact,
1454 manager = gtk_recent_manager_get_default ();
1455 gtk_recent_manager_add_item (manager, uri);
1458 g_object_unref (factory);
1459 g_object_unref (file);
1462 gtk_widget_destroy (GTK_WIDGET (widget));
1466 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1471 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1473 DEBUG ("Creating selection file chooser");
1475 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1477 GTK_FILE_CHOOSER_ACTION_OPEN,
1479 GTK_RESPONSE_CANCEL,
1483 button = gtk_button_new_with_mnemonic (_("_Send"));
1484 gtk_button_set_image (GTK_BUTTON (button),
1485 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1486 GTK_ICON_SIZE_BUTTON));
1487 gtk_widget_show (button);
1488 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1490 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1491 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1494 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1496 g_signal_connect (widget, "response",
1497 G_CALLBACK (file_manager_send_file_response_cb),
1500 gtk_widget_show (widget);
1504 file_manager_receive_file_response_cb (GtkDialog *dialog,
1505 GtkResponseType response,
1506 EmpathyFTHandler *handler)
1508 EmpathyFTFactory *factory;
1511 if (response == GTK_RESPONSE_OK) {
1512 factory = empathy_ft_factory_dup_singleton ();
1513 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1515 empathy_ft_factory_set_destination_for_incoming_handler
1516 (factory, handler, file);
1518 g_object_unref (factory);
1519 g_object_unref (file);
1521 /* unref the handler, as we dismissed the file chooser,
1522 * and refused the transfer.
1524 g_object_unref (handler);
1527 gtk_widget_destroy (GTK_WIDGET (dialog));
1531 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1535 widget = gtk_file_chooser_dialog_new (_("Select a destination"),
1537 GTK_FILE_CHOOSER_ACTION_SAVE,
1539 GTK_RESPONSE_CANCEL,
1543 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1544 empathy_ft_handler_get_filename (handler));
1545 gtk_file_chooser_set_do_overwrite_confirmation
1546 (GTK_FILE_CHOOSER (widget), TRUE);
1548 g_signal_connect (widget, "response",
1549 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1551 gtk_widget_show (widget);