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 "(https?|s?ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\
54 #define BODY "([^\\ \\n]+)"
55 #define END_BODY "([^\\ \\n]*[^,;\?><()\\ \"\\.\\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", error->message);
103 g_clear_error (&error);
104 g_object_unref (gui);
108 for (name = first_object; name; name = va_arg (args, const gchar *)) {
109 object_ptr = va_arg (args, GObject**);
111 *object_ptr = gtk_builder_get_object (gui, name);
114 g_warning ("File is missing object '%s'.", name);
123 empathy_builder_get_file (const gchar *filename,
124 const gchar *first_object,
130 va_start (args, first_object);
131 gui = builder_get_file_valist (filename, first_object, args);
138 empathy_builder_connect (GtkBuilder *gui,
149 va_start (args, first_object);
150 for (name = first_object; name; name = va_arg (args, const gchar *)) {
151 signal = va_arg (args, const gchar *);
152 callback = va_arg (args, GCallback);
154 object = gtk_builder_get_object (gui, name);
156 g_warning ("File is missing object '%s'.", name);
160 g_signal_connect (object, signal, callback, user_data);
167 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
170 /* On construction gui sinks the initial reference to widget. When gui
171 * is finalized it will drop its ref to widget. We take our own ref to
172 * prevent widget being finalised. The widget is forced to have a
173 * floating reference, like when it was initially unowned so that it can
174 * be used like any other GtkWidget. */
176 g_object_ref (widget);
177 g_object_force_floating (G_OBJECT (widget));
178 g_object_unref (gui);
184 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
187 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
188 return EMPATHY_IMAGE_AVAILABLE;
189 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
190 return EMPATHY_IMAGE_BUSY;
191 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
192 return EMPATHY_IMAGE_AWAY;
193 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
194 return EMPATHY_IMAGE_EXT_AWAY;
195 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
196 return EMPATHY_IMAGE_HIDDEN;
197 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
198 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
199 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
200 return EMPATHY_IMAGE_OFFLINE;
201 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
209 empathy_icon_name_for_contact (EmpathyContact *contact)
211 TpConnectionPresenceType presence;
213 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
214 EMPATHY_IMAGE_OFFLINE);
216 presence = empathy_contact_get_presence (contact);
217 return empathy_icon_name_for_presence (presence);
221 empathy_pixbuf_from_data (gchar *data,
224 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
228 empathy_pixbuf_from_data_and_mime (gchar *data,
232 GdkPixbufLoader *loader;
233 GdkPixbufFormat *format;
234 GdkPixbuf *pixbuf = NULL;
236 GError *error = NULL;
242 loader = gdk_pixbuf_loader_new ();
243 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
244 DEBUG ("Failed to write to pixbuf loader: %s",
245 error ? error->message : "No error given");
248 if (!gdk_pixbuf_loader_close (loader, &error)) {
249 DEBUG ("Failed to close pixbuf loader: %s",
250 error ? error->message : "No error given");
254 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
256 g_object_ref (pixbuf);
258 if (mime_type != NULL) {
259 format = gdk_pixbuf_loader_get_format (loader);
260 mime_types = gdk_pixbuf_format_get_mime_types (format);
262 *mime_type = g_strdup (*mime_types);
263 if (mime_types[1] != NULL) {
264 DEBUG ("Loader supports more than one mime "
265 "type! Picking the first one, %s",
268 g_strfreev (mime_types);
273 g_clear_error (&error);
274 g_object_unref (loader);
282 gboolean preserve_aspect_ratio;
286 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
289 struct SizeData *data)
291 g_return_if_fail (width > 0 && height > 0);
293 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
294 if (width < data->width && height < data->height) {
299 if (data->width < 0) {
300 width = width * (double) data->height / (gdouble) height;
301 height = data->height;
302 } else if (data->height < 0) {
303 height = height * (double) data->width / (double) width;
305 } else if ((double) height * (double) data->width >
306 (double) width * (double) data->height) {
307 width = 0.5 + (double) width * (double) data->height / (double) height;
308 height = data->height;
310 height = 0.5 + (double) height * (double) data->width / (double) width;
314 if (data->width > 0) {
318 if (data->height > 0) {
319 height = data->height;
323 gdk_pixbuf_loader_set_size (loader, width, height);
327 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
329 gint width, height, rowstride;
332 width = gdk_pixbuf_get_width (pixbuf);
333 height = gdk_pixbuf_get_height (pixbuf);
334 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
335 pixels = gdk_pixbuf_get_pixels (pixbuf);
337 if (width < 6 || height < 6) {
345 pixels[rowstride + 3] = 0x80;
346 pixels[rowstride * 2 + 3] = 0xC0;
349 pixels[width * 4 - 1] = 0;
350 pixels[width * 4 - 5] = 0x80;
351 pixels[width * 4 - 9] = 0xC0;
352 pixels[rowstride + (width * 4) - 1] = 0x80;
353 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
356 pixels[(height - 1) * rowstride + 3] = 0;
357 pixels[(height - 1) * rowstride + 7] = 0x80;
358 pixels[(height - 1) * rowstride + 11] = 0xC0;
359 pixels[(height - 2) * rowstride + 3] = 0x80;
360 pixels[(height - 3) * rowstride + 3] = 0xC0;
363 pixels[height * rowstride - 1] = 0;
364 pixels[(height - 1) * rowstride - 1] = 0x80;
365 pixels[(height - 2) * rowstride - 1] = 0xC0;
366 pixels[height * rowstride - 5] = 0x80;
367 pixels[height * rowstride - 9] = 0xC0;
371 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
373 gint width, height, rowstride, i;
377 width = gdk_pixbuf_get_width (pixbuf);
378 height = gdk_pixbuf_get_height (pixbuf);
379 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
380 pixels = gdk_pixbuf_get_pixels (pixbuf);
383 for (i = 3; i < rowstride; i+=4) {
389 for (i = 1; i < height - 1; i++) {
390 row = pixels + (i*rowstride);
391 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
396 row = pixels + ((height-1) * rowstride);
397 for (i = 3; i < rowstride; i+=4) {
407 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
412 GdkPixbufLoader *loader;
413 struct SizeData data;
414 GError *error = NULL;
421 data.height = height;
422 data.preserve_aspect_ratio = TRUE;
424 loader = gdk_pixbuf_loader_new ();
426 g_signal_connect (loader, "size-prepared",
427 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
430 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
431 g_warning ("Couldn't write avatar image:%p with "
432 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
433 avatar->data, avatar->len, error->message);
434 g_error_free (error);
438 gdk_pixbuf_loader_close (loader, NULL);
440 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
441 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
442 GdkPixbuf *rounded_pixbuf;
444 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
445 gdk_pixbuf_get_width (pixbuf),
446 gdk_pixbuf_get_height (pixbuf));
447 gdk_pixbuf_copy_area (pixbuf, 0, 0,
448 gdk_pixbuf_get_width (pixbuf),
449 gdk_pixbuf_get_height (pixbuf),
452 pixbuf = rounded_pixbuf;
454 g_object_ref (pixbuf);
457 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
458 empathy_avatar_pixbuf_roundify (pixbuf);
461 g_object_unref (loader);
467 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
471 EmpathyAvatar *avatar;
473 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
475 avatar = empathy_contact_get_avatar (contact);
477 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
481 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
486 width = gdk_pixbuf_get_width (pixbuf);
487 height = gdk_pixbuf_get_height (pixbuf);
489 if (width > 0 && (width > max_size || height > max_size)) {
490 factor = (gdouble) max_size / MAX (width, height);
492 width = width * factor;
493 height = height * factor;
495 return gdk_pixbuf_scale_simple (pixbuf,
500 return g_object_ref (pixbuf);
504 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
509 GError *error = NULL;
515 theme = gtk_icon_theme_get_default ();
517 pixbuf = gtk_icon_theme_load_icon (theme,
523 DEBUG ("Error loading icon: %s", error->message);
524 g_clear_error (&error);
531 empathy_pixbuf_from_icon_name (const gchar *icon_name,
532 GtkIconSize icon_size)
541 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
545 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
549 empathy_filename_from_icon_name (const gchar *icon_name,
550 GtkIconSize icon_size)
552 GtkIconTheme *icon_theme;
553 GtkIconInfo *icon_info;
558 icon_theme = gtk_icon_theme_get_default ();
560 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
564 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
565 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
566 gtk_icon_info_free (icon_info);
571 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
572 * that to make it easier to apply changes from the original code.
574 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
576 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
577 * decomposable character it consumes the decomposition length from the given
578 * offset. So it's useful when the offset was calculated for the normalized
579 * version of str, but we need a pointer to str itself. */
581 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
583 gchar *casefold, *normal;
589 q = g_utf8_next_char (p);
590 casefold = g_utf8_casefold (p, q - p);
591 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
592 offset -= g_utf8_strlen (normal, -1);
601 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
605 const gchar *ret = NULL;
608 gchar *caseless_haystack;
611 g_return_val_if_fail (haystack != NULL, NULL);
612 g_return_val_if_fail (needle != NULL, NULL);
614 casefold = g_utf8_casefold (haystack, -1);
615 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
618 needle_len = g_utf8_strlen (needle, -1);
619 haystack_len = g_utf8_strlen (caseless_haystack, -1);
623 ret = (gchar *) haystack;
627 if (haystack_len < needle_len)
633 p = (gchar *) caseless_haystack;
634 needle_len = strlen (needle);
639 if ((strncmp (p, needle, needle_len) == 0))
641 ret = pointer_from_offset_skipping_decomp (haystack, i);
645 p = g_utf8_next_char (p);
650 g_free (caseless_haystack);
656 g_utf8_caselessnmatch (const char *s1, const char *s2,
657 gssize n1, gssize n2)
660 gchar *normalized_s1;
661 gchar *normalized_s2;
664 gboolean ret = FALSE;
666 g_return_val_if_fail (s1 != NULL, FALSE);
667 g_return_val_if_fail (s2 != NULL, FALSE);
668 g_return_val_if_fail (n1 > 0, FALSE);
669 g_return_val_if_fail (n2 > 0, FALSE);
671 casefold = g_utf8_casefold (s1, n1);
672 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
675 casefold = g_utf8_casefold (s2, n2);
676 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
679 len_s1 = strlen (normalized_s1);
680 len_s2 = strlen (normalized_s2);
685 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
688 g_free (normalized_s1);
689 g_free (normalized_s2);
695 forward_chars_with_skipping (GtkTextIter *iter,
697 gboolean skip_invisible,
698 gboolean skip_nontext,
699 gboolean skip_decomp)
703 g_return_if_fail (count >= 0);
709 gboolean ignored = FALSE;
711 /* minimal workaround to avoid the infinite loop of bug #168247.
712 * It doesn't fix the problemjust the symptom...
714 if (gtk_text_iter_is_end (iter))
717 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
720 if (!ignored && skip_invisible &&
721 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
724 if (!ignored && skip_decomp)
726 /* being UTF8 correct sucks; this accounts for extra
727 offsets coming from canonical decompositions of
728 UTF8 characters (e.g. accented characters) which
729 g_utf8_normalize () performs */
734 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
735 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
736 i -= (g_utf8_strlen (normal, -1) - 1);
740 gtk_text_iter_forward_char (iter);
748 lines_match (const GtkTextIter *start,
750 gboolean visible_only,
752 GtkTextIter *match_start,
753 GtkTextIter *match_end)
760 if (*lines == NULL || **lines == '\0')
763 *match_start = *start;
770 gtk_text_iter_forward_line (&next);
772 /* No more text in buffer, but *lines is nonempty */
773 if (gtk_text_iter_equal (start, &next))
779 line_text = gtk_text_iter_get_visible_slice (start, &next);
781 line_text = gtk_text_iter_get_slice (start, &next);
786 line_text = gtk_text_iter_get_visible_text (start, &next);
788 line_text = gtk_text_iter_get_text (start, &next);
791 if (match_start) /* if this is the first line we're matching */
793 found = g_utf8_strcasestr (line_text, *lines);
797 /* If it's not the first line, we have to match from the
800 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
813 /* Get offset to start of search string */
814 offset = g_utf8_strlen (line_text, found - line_text);
818 /* If match start needs to be returned, set it to the
819 * start of the search string.
821 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
827 /* Go to end of search string */
828 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
837 /* pass NULL for match_start, since we don't need to find the
840 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
843 /* strsplit () that retains the delimiter as part of the string. */
845 strbreakup (const char *string,
846 const char *delimiter,
849 GSList *string_list = NULL, *slist;
850 gchar **str_array, *s, *casefold, *new_string;
853 g_return_val_if_fail (string != NULL, NULL);
854 g_return_val_if_fail (delimiter != NULL, NULL);
857 max_tokens = G_MAXINT;
859 s = strstr (string, delimiter);
862 guint delimiter_len = strlen (delimiter);
868 len = s - string + delimiter_len;
869 new_string = g_new (gchar, len + 1);
870 strncpy (new_string, string, len);
872 casefold = g_utf8_casefold (new_string, -1);
874 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
876 string_list = g_slist_prepend (string_list, new_string);
878 string = s + delimiter_len;
879 s = strstr (string, delimiter);
880 } while (--max_tokens && s);
886 casefold = g_utf8_casefold (string, -1);
887 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
889 string_list = g_slist_prepend (string_list, new_string);
892 str_array = g_new (gchar*, n);
896 str_array[i--] = NULL;
897 for (slist = string_list; slist; slist = slist->next)
898 str_array[i--] = slist->data;
900 g_slist_free (string_list);
906 empathy_text_iter_forward_search (const GtkTextIter *iter,
908 GtkTextIter *match_start,
909 GtkTextIter *match_end,
910 const GtkTextIter *limit)
912 gchar **lines = NULL;
914 gboolean retval = FALSE;
916 gboolean visible_only;
919 g_return_val_if_fail (iter != NULL, FALSE);
920 g_return_val_if_fail (str != NULL, FALSE);
922 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
926 /* If we can move one char, return the empty string there */
929 if (gtk_text_iter_forward_char (&match)) {
930 if (limit && gtk_text_iter_equal (&match, limit)) {
935 *match_start = match;
949 /* locate all lines */
950 lines = strbreakup (str, "\n", -1);
955 /* This loop has an inefficient worst-case, where
956 * gtk_text_iter_get_text () is called repeatedly on
961 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
965 if (lines_match (&search, (const gchar**)lines,
966 visible_only, slice, &match, &end)) {
968 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
972 *match_start = match;
980 } while (gtk_text_iter_forward_line (&search));
982 g_strfreev ((gchar **) lines);
988 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
992 const gchar *ret = NULL;
995 gchar *caseless_haystack;
998 g_return_val_if_fail (haystack != NULL, NULL);
999 g_return_val_if_fail (needle != NULL, NULL);
1001 casefold = g_utf8_casefold (haystack, -1);
1002 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1005 needle_len = g_utf8_strlen (needle, -1);
1006 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1008 if (needle_len == 0)
1010 ret = (gchar *) haystack;
1014 if (haystack_len < needle_len)
1020 i = haystack_len - needle_len;
1021 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1022 needle_len = strlen (needle);
1024 while (p >= caseless_haystack)
1026 if (strncmp (p, needle, needle_len) == 0)
1028 ret = pointer_from_offset_skipping_decomp (haystack, i);
1032 p = g_utf8_prev_char (p);
1037 g_free (caseless_haystack);
1043 backward_lines_match (const GtkTextIter *start,
1044 const gchar **lines,
1045 gboolean visible_only,
1047 GtkTextIter *match_start,
1048 GtkTextIter *match_end)
1050 GtkTextIter line, next;
1055 if (*lines == NULL || **lines == '\0')
1058 *match_start = *start;
1060 *match_end = *start;
1064 line = next = *start;
1065 if (gtk_text_iter_get_line_offset (&next) == 0)
1067 if (!gtk_text_iter_backward_line (&next))
1071 gtk_text_iter_set_line_offset (&next, 0);
1076 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1078 line_text = gtk_text_iter_get_slice (&next, &line);
1083 line_text = gtk_text_iter_get_visible_text (&next, &line);
1085 line_text = gtk_text_iter_get_text (&next, &line);
1088 if (match_start) /* if this is the first line we're matching */
1090 found = g_utf8_strrcasestr (line_text, *lines);
1094 /* If it's not the first line, we have to match from the
1095 * start of the line.
1097 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1110 /* Get offset to start of search string */
1111 offset = g_utf8_strlen (line_text, found - line_text);
1113 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1115 /* If match start needs to be returned, set it to the
1116 * start of the search string.
1120 *match_start = next;
1123 /* Go to end of search string */
1124 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1133 /* try to match the rest of the lines forward, passing NULL
1134 * for match_start so lines_match will try to match the entire
1136 return lines_match (&next, lines, visible_only,
1137 slice, NULL, match_end);
1141 empathy_text_iter_backward_search (const GtkTextIter *iter,
1143 GtkTextIter *match_start,
1144 GtkTextIter *match_end,
1145 const GtkTextIter *limit)
1147 gchar **lines = NULL;
1149 gboolean retval = FALSE;
1151 gboolean visible_only;
1154 g_return_val_if_fail (iter != NULL, FALSE);
1155 g_return_val_if_fail (str != NULL, FALSE);
1157 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1162 /* If we can move one char, return the empty string there */
1165 if (gtk_text_iter_backward_char (&match))
1167 if (limit && gtk_text_iter_equal (&match, limit))
1171 *match_start = match;
1182 visible_only = TRUE;
1185 /* locate all lines */
1186 lines = strbreakup (str, "\n", -1);
1192 /* This loop has an inefficient worst-case, where
1193 * gtk_text_iter_get_text () is called repeatedly on
1198 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1201 if (backward_lines_match (&search, (const gchar**)lines,
1202 visible_only, slice, &match, &end))
1204 if (limit == NULL || (limit &&
1205 gtk_text_iter_compare (&end, limit) > 0))
1210 *match_start = match;
1217 if (gtk_text_iter_get_line_offset (&search) == 0)
1219 if (!gtk_text_iter_backward_line (&search))
1224 gtk_text_iter_set_line_offset (&search, 0);
1228 g_strfreev ((gchar **) lines);
1234 empathy_window_get_is_visible (GtkWindow *window)
1236 GdkWindowState state;
1237 GdkWindow *gdk_window;
1239 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1241 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1246 state = gdk_window_get_state (gdk_window);
1247 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1255 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1257 GdkRectangle icon_location;
1260 GdkWindow *gdk_window;
1262 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1263 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1264 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1266 data[0] = icon_location.x;
1267 data[1] = icon_location.y;
1268 data[2] = icon_location.width;
1269 data[3] = icon_location.height;
1271 XChangeProperty (dpy,
1272 GDK_WINDOW_XID (gdk_window),
1273 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1274 "_NET_WM_ICON_GEOMETRY"),
1275 XA_CARDINAL, 32, PropModeReplace,
1276 (guchar *)&data, 4);
1278 gtk_window_set_skip_taskbar_hint (window, TRUE);
1279 gtk_window_iconify (window);
1282 /* Takes care of moving the window to the current workspace. */
1284 empathy_window_present (GtkWindow *window,
1285 gboolean steal_focus)
1289 g_return_if_fail (GTK_IS_WINDOW (window));
1291 /* There are three cases: hidden, visible, visible on another
1295 if (!empathy_window_get_is_visible (window)) {
1296 /* Hide it so present brings it to the current workspace. */
1297 gtk_widget_hide (GTK_WIDGET (window));
1300 timestamp = gtk_get_current_event_time ();
1301 gtk_window_present_with_time (window, timestamp);
1302 gtk_window_set_skip_taskbar_hint (window, FALSE);
1303 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1304 * it deiconify automatically. */
1305 gtk_window_deiconify (window);
1309 empathy_get_toplevel_window (GtkWidget *widget)
1311 GtkWidget *toplevel;
1313 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1315 toplevel = gtk_widget_get_toplevel (widget);
1316 if (GTK_IS_WINDOW (toplevel) &&
1317 GTK_WIDGET_TOPLEVEL (toplevel)) {
1318 return GTK_WINDOW (toplevel);
1324 /** empathy_make_absolute_url:
1327 * The URL opening code can't handle schemeless strings, so we try to be
1328 * smart and add http if there is no scheme or doesn't look like a mail
1329 * address. This should work in most cases, and let us click on strings
1330 * like "www.gnome.org".
1332 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1333 * g_free when your are done with it
1336 empathy_make_absolute_url (const gchar *url)
1338 g_return_val_if_fail (url != NULL, NULL);
1340 if (g_str_has_prefix (url, "ghelp:") ||
1341 g_str_has_prefix (url, "mailto:") ||
1342 strstr (url, ":/")) {
1343 return g_strdup (url);
1346 if (strstr (url, "@")) {
1347 return g_strdup_printf ("mailto:%s", url);
1350 return g_strdup_printf ("http://%s", url);
1354 empathy_url_show (GtkWidget *parent,
1358 GError *error = NULL;
1360 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1361 g_return_if_fail (url != NULL);
1363 real_url = empathy_make_absolute_url (url);
1365 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1366 gtk_get_current_event_time (), &error);
1371 dialog = gtk_message_dialog_new (NULL, 0,
1372 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1373 _("Unable to open URI"));
1374 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1375 "%s", error->message);
1377 g_signal_connect (dialog, "response",
1378 G_CALLBACK (gtk_widget_destroy),
1380 gtk_window_present (GTK_WINDOW (dialog));
1382 g_clear_error (&error);
1389 link_button_hook (GtkLinkButton *button,
1393 empathy_url_show (GTK_WIDGET (button), link);
1397 empathy_link_button_new (const gchar *url,
1400 static gboolean hook = FALSE;
1404 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1407 return gtk_link_button_new_with_label (url, title);
1411 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1416 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1418 g_signal_handlers_block_by_func (widget, callback, user_data);
1419 g_object_set (widget, "active", active, NULL);
1420 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1424 file_manager_send_file_response_cb (GtkDialog *widget,
1426 EmpathyContact *contact)
1428 EmpathyFTFactory *factory;
1431 GtkRecentManager *manager;
1433 if (response_id == GTK_RESPONSE_OK) {
1434 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1435 uri = g_file_get_uri (file);
1437 factory = empathy_ft_factory_dup_singleton ();
1439 empathy_ft_factory_new_transfer_outgoing (factory, contact,
1442 manager = gtk_recent_manager_get_default ();
1443 gtk_recent_manager_add_item (manager, uri);
1446 g_object_unref (factory);
1447 g_object_unref (file);
1450 gtk_widget_destroy (GTK_WIDGET (widget));
1454 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1459 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1461 DEBUG ("Creating selection file chooser");
1463 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1465 GTK_FILE_CHOOSER_ACTION_OPEN,
1467 GTK_RESPONSE_CANCEL,
1471 button = gtk_button_new_with_mnemonic (_("_Send"));
1472 gtk_button_set_image (GTK_BUTTON (button),
1473 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1474 GTK_ICON_SIZE_BUTTON));
1475 gtk_widget_show (button);
1476 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1478 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1479 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1482 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1484 g_signal_connect (widget, "response",
1485 G_CALLBACK (file_manager_send_file_response_cb),
1488 gtk_widget_show (widget);
1492 file_manager_receive_file_response_cb (GtkDialog *dialog,
1493 GtkResponseType response,
1494 EmpathyFTHandler *handler)
1496 EmpathyFTFactory *factory;
1499 if (response == GTK_RESPONSE_OK) {
1500 factory = empathy_ft_factory_dup_singleton ();
1501 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1503 empathy_ft_factory_set_destination_for_incoming_handler
1504 (factory, handler, file);
1506 g_object_unref (factory);
1507 g_object_unref (file);
1509 /* unref the handler, as we dismissed the file chooser,
1510 * and refused the transfer.
1512 g_object_unref (handler);
1515 gtk_widget_destroy (GTK_WIDGET (dialog));
1519 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1523 widget = gtk_file_chooser_dialog_new (_("Select a destination"),
1525 GTK_FILE_CHOOSER_ACTION_SAVE,
1527 GTK_RESPONSE_CANCEL,
1531 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1532 empathy_ft_handler_get_filename (handler));
1533 gtk_file_chooser_set_do_overwrite_confirmation
1534 (GTK_FILE_CHOOSER (widget), TRUE);
1536 g_signal_connect (widget, "response",
1537 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1539 gtk_widget_show (widget);