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>
40 #include <canberra-gtk.h>
42 #include <libmissioncontrol/mc-profile.h>
44 #include "empathy-ui-utils.h"
45 #include "empathy-images.h"
46 #include "empathy-conf.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
49 #include <libempathy/empathy-debug.h>
50 #include <libempathy/empathy-utils.h>
51 #include <libempathy/empathy-dispatcher.h>
52 #include <libempathy/empathy-idle.h>
53 #include <libempathy/empathy-tp-file.h>
54 #include <libempathy/empathy-ft-factory.h>
55 #include <libempathy/empathy-ft-handler.h>
57 #define SCHEMES "(https?|s?ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\
59 #define BODY "([^\\ \\n]+)"
60 #define END_BODY "([^\\ \\n]*[^,;\?><()\\ \"\\.\\n])"
61 #define URI_REGEX "("SCHEMES"://"END_BODY")" \
62 "|((mailto:)?"BODY"@"BODY"\\."END_BODY")"\
63 "|((www|ftp)\\."END_BODY")"
66 empathy_gtk_init (void)
68 static gboolean initialized = FALSE;
74 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
75 PKGDATADIR G_DIR_SEPARATOR_S "icons");
81 empathy_uri_regex_dup_singleton (void)
83 static GRegex *uri_regex = NULL;
85 /* We intentionally leak the regex so it's not recomputed */
87 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
90 return g_regex_ref (uri_regex);
94 builder_get_file_valist (const gchar *filename,
95 const gchar *first_object,
100 GObject **object_ptr;
101 GError *error = NULL;
103 DEBUG ("Loading file %s", filename);
105 gui = gtk_builder_new ();
106 if (!gtk_builder_add_from_file (gui, filename, &error)) {
107 g_critical ("GtkBuilder Error: %s", error->message);
108 g_clear_error (&error);
109 g_object_unref (gui);
113 for (name = first_object; name; name = va_arg (args, const gchar *)) {
114 object_ptr = va_arg (args, GObject**);
116 *object_ptr = gtk_builder_get_object (gui, name);
119 g_warning ("File is missing object '%s'.", name);
128 empathy_builder_get_file (const gchar *filename,
129 const gchar *first_object,
135 va_start (args, first_object);
136 gui = builder_get_file_valist (filename, first_object, args);
143 empathy_builder_connect (GtkBuilder *gui,
154 va_start (args, first_object);
155 for (name = first_object; name; name = va_arg (args, const gchar *)) {
156 signal = va_arg (args, const gchar *);
157 callback = va_arg (args, GCallback);
159 object = gtk_builder_get_object (gui, name);
161 g_warning ("File is missing object '%s'.", name);
165 g_signal_connect (object, signal, callback, user_data);
172 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
175 /* On construction gui sinks the initial reference to widget. When gui
176 * is finalized it will drop its ref to widget. We take our own ref to
177 * prevent widget being finalised. The widget is forced to have a
178 * floating reference, like when it was initially unowned so that it can
179 * be used like any other GtkWidget. */
181 g_object_ref (widget);
182 g_object_force_floating (G_OBJECT (widget));
183 g_object_unref (gui);
189 empathy_icon_name_from_account (McAccount *account)
193 profile = mc_account_get_profile (account);
195 return mc_profile_get_icon_name (profile);
199 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
202 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
203 return EMPATHY_IMAGE_AVAILABLE;
204 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
205 return EMPATHY_IMAGE_BUSY;
206 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
207 return EMPATHY_IMAGE_AWAY;
208 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
209 return EMPATHY_IMAGE_EXT_AWAY;
210 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
211 return EMPATHY_IMAGE_HIDDEN;
212 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
213 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
214 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
215 return EMPATHY_IMAGE_OFFLINE;
216 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
224 empathy_icon_name_for_contact (EmpathyContact *contact)
226 TpConnectionPresenceType presence;
228 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
229 EMPATHY_IMAGE_OFFLINE);
231 presence = empathy_contact_get_presence (contact);
232 return empathy_icon_name_for_presence (presence);
236 empathy_pixbuf_from_data (gchar *data,
239 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
243 empathy_pixbuf_from_data_and_mime (gchar *data,
247 GdkPixbufLoader *loader;
248 GdkPixbufFormat *format;
249 GdkPixbuf *pixbuf = NULL;
251 GError *error = NULL;
257 loader = gdk_pixbuf_loader_new ();
258 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
259 DEBUG ("Failed to write to pixbuf loader: %s",
260 error ? error->message : "No error given");
263 if (!gdk_pixbuf_loader_close (loader, &error)) {
264 DEBUG ("Failed to close pixbuf loader: %s",
265 error ? error->message : "No error given");
269 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
271 g_object_ref (pixbuf);
273 if (mime_type != NULL) {
274 format = gdk_pixbuf_loader_get_format (loader);
275 mime_types = gdk_pixbuf_format_get_mime_types (format);
277 *mime_type = g_strdup (*mime_types);
278 if (mime_types[1] != NULL) {
279 DEBUG ("Loader supports more than one mime "
280 "type! Picking the first one, %s",
283 g_strfreev (mime_types);
288 g_clear_error (&error);
289 g_object_unref (loader);
297 gboolean preserve_aspect_ratio;
301 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
304 struct SizeData *data)
306 g_return_if_fail (width > 0 && height > 0);
308 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
309 if (width < data->width && height < data->height) {
314 if (data->width < 0) {
315 width = width * (double) data->height / (gdouble) height;
316 height = data->height;
317 } else if (data->height < 0) {
318 height = height * (double) data->width / (double) width;
320 } else if ((double) height * (double) data->width >
321 (double) width * (double) data->height) {
322 width = 0.5 + (double) width * (double) data->height / (double) height;
323 height = data->height;
325 height = 0.5 + (double) height * (double) data->width / (double) width;
329 if (data->width > 0) {
333 if (data->height > 0) {
334 height = data->height;
338 gdk_pixbuf_loader_set_size (loader, width, height);
342 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
344 gint width, height, rowstride;
347 width = gdk_pixbuf_get_width (pixbuf);
348 height = gdk_pixbuf_get_height (pixbuf);
349 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
350 pixels = gdk_pixbuf_get_pixels (pixbuf);
352 if (width < 6 || height < 6) {
360 pixels[rowstride + 3] = 0x80;
361 pixels[rowstride * 2 + 3] = 0xC0;
364 pixels[width * 4 - 1] = 0;
365 pixels[width * 4 - 5] = 0x80;
366 pixels[width * 4 - 9] = 0xC0;
367 pixels[rowstride + (width * 4) - 1] = 0x80;
368 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
371 pixels[(height - 1) * rowstride + 3] = 0;
372 pixels[(height - 1) * rowstride + 7] = 0x80;
373 pixels[(height - 1) * rowstride + 11] = 0xC0;
374 pixels[(height - 2) * rowstride + 3] = 0x80;
375 pixels[(height - 3) * rowstride + 3] = 0xC0;
378 pixels[height * rowstride - 1] = 0;
379 pixels[(height - 1) * rowstride - 1] = 0x80;
380 pixels[(height - 2) * rowstride - 1] = 0xC0;
381 pixels[height * rowstride - 5] = 0x80;
382 pixels[height * rowstride - 9] = 0xC0;
386 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
388 gint width, height, rowstride, i;
392 width = gdk_pixbuf_get_width (pixbuf);
393 height = gdk_pixbuf_get_height (pixbuf);
394 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
395 pixels = gdk_pixbuf_get_pixels (pixbuf);
398 for (i = 3; i < rowstride; i+=4) {
404 for (i = 1; i < height - 1; i++) {
405 row = pixels + (i*rowstride);
406 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
411 row = pixels + ((height-1) * rowstride);
412 for (i = 3; i < rowstride; i+=4) {
422 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
427 GdkPixbufLoader *loader;
428 struct SizeData data;
429 GError *error = NULL;
436 data.height = height;
437 data.preserve_aspect_ratio = TRUE;
439 loader = gdk_pixbuf_loader_new ();
441 g_signal_connect (loader, "size-prepared",
442 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
445 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
446 g_warning ("Couldn't write avatar image:%p with "
447 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
448 avatar->data, avatar->len, error->message);
449 g_error_free (error);
453 gdk_pixbuf_loader_close (loader, NULL);
455 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
456 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
457 GdkPixbuf *rounded_pixbuf;
459 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
460 gdk_pixbuf_get_width (pixbuf),
461 gdk_pixbuf_get_height (pixbuf));
462 gdk_pixbuf_copy_area (pixbuf, 0, 0,
463 gdk_pixbuf_get_width (pixbuf),
464 gdk_pixbuf_get_height (pixbuf),
467 pixbuf = rounded_pixbuf;
469 g_object_ref (pixbuf);
472 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
473 empathy_avatar_pixbuf_roundify (pixbuf);
476 g_object_unref (loader);
482 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
486 EmpathyAvatar *avatar;
488 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
490 avatar = empathy_contact_get_avatar (contact);
492 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
496 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
501 width = gdk_pixbuf_get_width (pixbuf);
502 height = gdk_pixbuf_get_height (pixbuf);
504 if (width > 0 && (width > max_size || height > max_size)) {
505 factor = (gdouble) max_size / MAX (width, height);
507 width = width * factor;
508 height = height * factor;
510 return gdk_pixbuf_scale_simple (pixbuf,
515 return g_object_ref (pixbuf);
519 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
524 GError *error = NULL;
530 theme = gtk_icon_theme_get_default ();
532 pixbuf = gtk_icon_theme_load_icon (theme,
538 DEBUG ("Error loading icon: %s", error->message);
539 g_clear_error (&error);
546 empathy_pixbuf_from_icon_name (const gchar *icon_name,
547 GtkIconSize icon_size)
556 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
560 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
563 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
564 * that to make it easier to apply changes from the original code.
566 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
568 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
569 * decomposable character it consumes the decomposition length from the given
570 * offset. So it's useful when the offset was calculated for the normalized
571 * version of str, but we need a pointer to str itself. */
573 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
575 gchar *casefold, *normal;
581 q = g_utf8_next_char (p);
582 casefold = g_utf8_casefold (p, q - p);
583 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
584 offset -= g_utf8_strlen (normal, -1);
593 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
597 const gchar *ret = NULL;
600 gchar *caseless_haystack;
603 g_return_val_if_fail (haystack != NULL, NULL);
604 g_return_val_if_fail (needle != NULL, NULL);
606 casefold = g_utf8_casefold (haystack, -1);
607 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
610 needle_len = g_utf8_strlen (needle, -1);
611 haystack_len = g_utf8_strlen (caseless_haystack, -1);
615 ret = (gchar *) haystack;
619 if (haystack_len < needle_len)
625 p = (gchar *) caseless_haystack;
626 needle_len = strlen (needle);
631 if ((strncmp (p, needle, needle_len) == 0))
633 ret = pointer_from_offset_skipping_decomp (haystack, i);
637 p = g_utf8_next_char (p);
642 g_free (caseless_haystack);
648 g_utf8_caselessnmatch (const char *s1, const char *s2,
649 gssize n1, gssize n2)
652 gchar *normalized_s1;
653 gchar *normalized_s2;
656 gboolean ret = FALSE;
658 g_return_val_if_fail (s1 != NULL, FALSE);
659 g_return_val_if_fail (s2 != NULL, FALSE);
660 g_return_val_if_fail (n1 > 0, FALSE);
661 g_return_val_if_fail (n2 > 0, FALSE);
663 casefold = g_utf8_casefold (s1, n1);
664 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
667 casefold = g_utf8_casefold (s2, n2);
668 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
671 len_s1 = strlen (normalized_s1);
672 len_s2 = strlen (normalized_s2);
677 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
680 g_free (normalized_s1);
681 g_free (normalized_s2);
687 forward_chars_with_skipping (GtkTextIter *iter,
689 gboolean skip_invisible,
690 gboolean skip_nontext,
691 gboolean skip_decomp)
695 g_return_if_fail (count >= 0);
701 gboolean ignored = FALSE;
703 /* minimal workaround to avoid the infinite loop of bug #168247.
704 * It doesn't fix the problemjust the symptom...
706 if (gtk_text_iter_is_end (iter))
709 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
712 if (!ignored && skip_invisible &&
713 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
716 if (!ignored && skip_decomp)
718 /* being UTF8 correct sucks; this accounts for extra
719 offsets coming from canonical decompositions of
720 UTF8 characters (e.g. accented characters) which
721 g_utf8_normalize () performs */
726 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
727 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
728 i -= (g_utf8_strlen (normal, -1) - 1);
732 gtk_text_iter_forward_char (iter);
740 lines_match (const GtkTextIter *start,
742 gboolean visible_only,
744 GtkTextIter *match_start,
745 GtkTextIter *match_end)
752 if (*lines == NULL || **lines == '\0')
755 *match_start = *start;
762 gtk_text_iter_forward_line (&next);
764 /* No more text in buffer, but *lines is nonempty */
765 if (gtk_text_iter_equal (start, &next))
771 line_text = gtk_text_iter_get_visible_slice (start, &next);
773 line_text = gtk_text_iter_get_slice (start, &next);
778 line_text = gtk_text_iter_get_visible_text (start, &next);
780 line_text = gtk_text_iter_get_text (start, &next);
783 if (match_start) /* if this is the first line we're matching */
785 found = g_utf8_strcasestr (line_text, *lines);
789 /* If it's not the first line, we have to match from the
792 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
805 /* Get offset to start of search string */
806 offset = g_utf8_strlen (line_text, found - line_text);
810 /* If match start needs to be returned, set it to the
811 * start of the search string.
813 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
819 /* Go to end of search string */
820 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
829 /* pass NULL for match_start, since we don't need to find the
832 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
835 /* strsplit () that retains the delimiter as part of the string. */
837 strbreakup (const char *string,
838 const char *delimiter,
841 GSList *string_list = NULL, *slist;
842 gchar **str_array, *s, *casefold, *new_string;
845 g_return_val_if_fail (string != NULL, NULL);
846 g_return_val_if_fail (delimiter != NULL, NULL);
849 max_tokens = G_MAXINT;
851 s = strstr (string, delimiter);
854 guint delimiter_len = strlen (delimiter);
860 len = s - string + delimiter_len;
861 new_string = g_new (gchar, len + 1);
862 strncpy (new_string, string, len);
864 casefold = g_utf8_casefold (new_string, -1);
866 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
868 string_list = g_slist_prepend (string_list, new_string);
870 string = s + delimiter_len;
871 s = strstr (string, delimiter);
872 } while (--max_tokens && s);
878 casefold = g_utf8_casefold (string, -1);
879 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
881 string_list = g_slist_prepend (string_list, new_string);
884 str_array = g_new (gchar*, n);
888 str_array[i--] = NULL;
889 for (slist = string_list; slist; slist = slist->next)
890 str_array[i--] = slist->data;
892 g_slist_free (string_list);
898 empathy_text_iter_forward_search (const GtkTextIter *iter,
900 GtkTextIter *match_start,
901 GtkTextIter *match_end,
902 const GtkTextIter *limit)
904 gchar **lines = NULL;
906 gboolean retval = FALSE;
908 gboolean visible_only;
911 g_return_val_if_fail (iter != NULL, FALSE);
912 g_return_val_if_fail (str != NULL, FALSE);
914 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
918 /* If we can move one char, return the empty string there */
921 if (gtk_text_iter_forward_char (&match)) {
922 if (limit && gtk_text_iter_equal (&match, limit)) {
927 *match_start = match;
941 /* locate all lines */
942 lines = strbreakup (str, "\n", -1);
947 /* This loop has an inefficient worst-case, where
948 * gtk_text_iter_get_text () is called repeatedly on
953 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
957 if (lines_match (&search, (const gchar**)lines,
958 visible_only, slice, &match, &end)) {
960 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
964 *match_start = match;
972 } while (gtk_text_iter_forward_line (&search));
974 g_strfreev ((gchar **) lines);
980 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
984 const gchar *ret = NULL;
987 gchar *caseless_haystack;
990 g_return_val_if_fail (haystack != NULL, NULL);
991 g_return_val_if_fail (needle != NULL, NULL);
993 casefold = g_utf8_casefold (haystack, -1);
994 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
997 needle_len = g_utf8_strlen (needle, -1);
998 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1000 if (needle_len == 0)
1002 ret = (gchar *) haystack;
1006 if (haystack_len < needle_len)
1012 i = haystack_len - needle_len;
1013 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1014 needle_len = strlen (needle);
1016 while (p >= caseless_haystack)
1018 if (strncmp (p, needle, needle_len) == 0)
1020 ret = pointer_from_offset_skipping_decomp (haystack, i);
1024 p = g_utf8_prev_char (p);
1029 g_free (caseless_haystack);
1035 backward_lines_match (const GtkTextIter *start,
1036 const gchar **lines,
1037 gboolean visible_only,
1039 GtkTextIter *match_start,
1040 GtkTextIter *match_end)
1042 GtkTextIter line, next;
1047 if (*lines == NULL || **lines == '\0')
1050 *match_start = *start;
1052 *match_end = *start;
1056 line = next = *start;
1057 if (gtk_text_iter_get_line_offset (&next) == 0)
1059 if (!gtk_text_iter_backward_line (&next))
1063 gtk_text_iter_set_line_offset (&next, 0);
1068 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1070 line_text = gtk_text_iter_get_slice (&next, &line);
1075 line_text = gtk_text_iter_get_visible_text (&next, &line);
1077 line_text = gtk_text_iter_get_text (&next, &line);
1080 if (match_start) /* if this is the first line we're matching */
1082 found = g_utf8_strrcasestr (line_text, *lines);
1086 /* If it's not the first line, we have to match from the
1087 * start of the line.
1089 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1102 /* Get offset to start of search string */
1103 offset = g_utf8_strlen (line_text, found - line_text);
1105 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1107 /* If match start needs to be returned, set it to the
1108 * start of the search string.
1112 *match_start = next;
1115 /* Go to end of search string */
1116 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1125 /* try to match the rest of the lines forward, passing NULL
1126 * for match_start so lines_match will try to match the entire
1128 return lines_match (&next, lines, visible_only,
1129 slice, NULL, match_end);
1133 empathy_text_iter_backward_search (const GtkTextIter *iter,
1135 GtkTextIter *match_start,
1136 GtkTextIter *match_end,
1137 const GtkTextIter *limit)
1139 gchar **lines = NULL;
1141 gboolean retval = FALSE;
1143 gboolean visible_only;
1146 g_return_val_if_fail (iter != NULL, FALSE);
1147 g_return_val_if_fail (str != NULL, FALSE);
1149 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1154 /* If we can move one char, return the empty string there */
1157 if (gtk_text_iter_backward_char (&match))
1159 if (limit && gtk_text_iter_equal (&match, limit))
1163 *match_start = match;
1174 visible_only = TRUE;
1177 /* locate all lines */
1178 lines = strbreakup (str, "\n", -1);
1184 /* This loop has an inefficient worst-case, where
1185 * gtk_text_iter_get_text () is called repeatedly on
1190 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1193 if (backward_lines_match (&search, (const gchar**)lines,
1194 visible_only, slice, &match, &end))
1196 if (limit == NULL || (limit &&
1197 gtk_text_iter_compare (&end, limit) > 0))
1202 *match_start = match;
1209 if (gtk_text_iter_get_line_offset (&search) == 0)
1211 if (!gtk_text_iter_backward_line (&search))
1216 gtk_text_iter_set_line_offset (&search, 0);
1220 g_strfreev ((gchar **) lines);
1226 empathy_window_get_is_visible (GtkWindow *window)
1228 GdkWindowState state;
1229 GdkWindow *gdk_window;
1231 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1233 gdk_window = GTK_WIDGET (window)->window;
1238 state = gdk_window_get_state (gdk_window);
1239 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1247 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1249 GdkRectangle icon_location;
1252 GdkWindow *gdk_window;
1254 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1255 gdk_window = GTK_WIDGET (window)->window;
1256 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1258 data[0] = icon_location.x;
1259 data[1] = icon_location.y;
1260 data[2] = icon_location.width;
1261 data[3] = icon_location.height;
1263 XChangeProperty (dpy,
1264 GDK_WINDOW_XID (gdk_window),
1265 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1266 "_NET_WM_ICON_GEOMETRY"),
1267 XA_CARDINAL, 32, PropModeReplace,
1268 (guchar *)&data, 4);
1270 gtk_window_set_skip_taskbar_hint (window, TRUE);
1271 gtk_window_iconify (window);
1274 /* Takes care of moving the window to the current workspace. */
1276 empathy_window_present (GtkWindow *window,
1277 gboolean steal_focus)
1281 g_return_if_fail (GTK_IS_WINDOW (window));
1283 /* There are three cases: hidden, visible, visible on another
1287 if (!empathy_window_get_is_visible (window)) {
1288 /* Hide it so present brings it to the current workspace. */
1289 gtk_widget_hide (GTK_WIDGET (window));
1292 timestamp = gtk_get_current_event_time ();
1293 gtk_window_set_skip_taskbar_hint (window, FALSE);
1294 gtk_window_present_with_time (window, timestamp);
1295 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1296 * it deiconify automatically. */
1297 gtk_window_deiconify (window);
1301 empathy_get_toplevel_window (GtkWidget *widget)
1303 GtkWidget *toplevel;
1305 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1307 toplevel = gtk_widget_get_toplevel (widget);
1308 if (GTK_IS_WINDOW (toplevel) &&
1309 GTK_WIDGET_TOPLEVEL (toplevel)) {
1310 return GTK_WINDOW (toplevel);
1316 /* The URL opening code can't handle schemeless strings, so we try to be
1317 * smart and add http if there is no scheme or doesn't look like a mail
1318 * address. This should work in most cases, and let us click on strings
1319 * like "www.gnome.org".
1322 fixup_url (const gchar *url)
1324 if (g_str_has_prefix (url, "ghelp:") ||
1325 g_str_has_prefix (url, "mailto:") ||
1326 strstr (url, ":/")) {
1330 if (strstr (url, "@")) {
1331 return g_strdup_printf ("mailto:%s", url);
1334 return g_strdup_printf ("http://%s", url);
1338 empathy_url_show (GtkWidget *parent,
1342 GError *error = NULL;
1344 real_url = fixup_url (url);
1349 gtk_show_uri (gtk_widget_get_screen (parent), url,
1350 gtk_get_current_event_time (), &error);
1355 dialog = gtk_message_dialog_new (NULL, 0,
1356 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1357 _("Unable to open URI"));
1358 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1359 "%s", error->message);
1361 g_signal_connect (dialog, "response",
1362 G_CALLBACK (gtk_widget_destroy),
1364 gtk_window_present (GTK_WINDOW (dialog));
1366 g_clear_error (&error);
1373 link_button_hook (GtkLinkButton *button,
1377 empathy_url_show (GTK_WIDGET (button), link);
1381 empathy_link_button_new (const gchar *url,
1384 static gboolean hook = FALSE;
1388 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1391 return gtk_link_button_new_with_label (url, title);
1395 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1400 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1402 g_signal_handlers_block_by_func (widget, callback, user_data);
1403 g_object_set (widget, "active", active, NULL);
1404 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1407 /* Sending files with the file chooser */
1410 file_manager_send_file_request_cb (EmpathyDispatchOperation *operation,
1411 const GError *error, gpointer user_data)
1413 GFile *file = (GFile *) user_data;
1414 EmpathyTpFile *tp_file;
1416 if (error != NULL) {
1417 DEBUG ("Couldn't request channel: %s", error->message);
1418 g_object_unref (file);
1422 DEBUG ("Starting to send file");
1424 tp_file = EMPATHY_TP_FILE (
1425 empathy_dispatch_operation_get_channel_wrapper (operation));
1427 empathy_tp_file_offer (tp_file, file, NULL);
1429 g_object_unref (file);
1434 new_ft_handler_cb (EmpathyFTFactory *factory,
1435 EmpathyFTHandler *handler,
1439 empathy_ft_handler_start_transfer (handler, g_cancellable_new ());
1443 file_manager_send_file_response_cb (GtkDialog *widget,
1445 EmpathyContact *contact)
1447 EmpathyFTFactory *factory;
1449 if (response_id == GTK_RESPONSE_OK) {
1453 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget));
1455 DEBUG ("File chooser selected files:");
1457 for (l = list; l; l = l->next) {
1461 GtkRecentManager *manager;
1464 GError *error = NULL;
1467 gfile = g_file_new_for_uri (uri);
1468 info = g_file_query_info (gfile,
1469 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1470 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1471 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1475 DEBUG ("Can't get info about the file: %s", error->message);
1476 g_clear_error (&error);
1477 g_object_unref (gfile);
1481 DEBUG ("\t%s", uri);
1482 filename = g_file_get_basename (gfile);
1483 g_file_info_get_modification_time (info, &mtime);
1485 empathy_dispatcher_send_file_to_contact (contact,
1486 filename, g_file_info_get_size (info), mtime.tv_sec,
1487 g_file_info_get_content_type (info),
1488 file_manager_send_file_request_cb, gfile);
1490 factory = empathy_ft_factory_dup_singleton ();
1491 g_signal_connect (factory, "new-ft-handler",
1492 G_CALLBACK (new_ft_handler_cb), NULL);
1493 empathy_ft_factory_new_transfer (factory, contact, gfile);
1495 g_object_unref (info);
1497 manager = gtk_recent_manager_get_default ();
1498 gtk_recent_manager_add_item (manager, uri);
1502 g_slist_free (list);
1505 gtk_widget_destroy (GTK_WIDGET (widget));
1509 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1514 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1516 DEBUG ("Creating selection file chooser");
1518 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1520 GTK_FILE_CHOOSER_ACTION_OPEN,
1522 GTK_RESPONSE_CANCEL,
1526 button = gtk_button_new_with_mnemonic (_("_Send"));
1527 gtk_button_set_image (GTK_BUTTON (button),
1528 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1529 GTK_ICON_SIZE_BUTTON));
1530 gtk_widget_show (button);
1531 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1533 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1534 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1537 g_signal_connect (widget, "response",
1538 G_CALLBACK (file_manager_send_file_response_cb),
1541 gtk_widget_show (widget);
1545 EmpathySound sound_id;
1546 const char * event_ca_id;
1547 const char * event_ca_description;
1548 const char * gconf_key;
1549 } EmpathySoundEntry;
1551 /* NOTE: these entries MUST be in the same order than EmpathySound enum */
1552 static EmpathySoundEntry sound_entries[LAST_EMPATHY_SOUND] = {
1553 { EMPATHY_SOUND_MESSAGE_INCOMING, "message-new-instant",
1554 N_("Received an instant message"), EMPATHY_PREFS_SOUNDS_INCOMING_MESSAGE } ,
1555 { EMPATHY_SOUND_MESSAGE_OUTGOING, "message-sent-instant",
1556 N_("Sent an instant message"), EMPATHY_PREFS_SOUNDS_OUTGOING_MESSAGE } ,
1557 { EMPATHY_SOUND_CONVERSATION_NEW, "message-new-instant",
1558 N_("Incoming chat request"), EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION },
1559 { EMPATHY_SOUND_CONTACT_CONNECTED, "service-login",
1560 N_("Contact connected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN },
1561 { EMPATHY_SOUND_CONTACT_DISCONNECTED, "service-logout",
1562 N_("Contact disconnected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT },
1563 { EMPATHY_SOUND_ACCOUNT_CONNECTED, "service-login",
1564 N_("Connected to server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN },
1565 { EMPATHY_SOUND_ACCOUNT_DISCONNECTED, "service-logout",
1566 N_("Disconnected from server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT },
1567 { EMPATHY_SOUND_PHONE_INCOMING, "phone-incoming-call",
1568 N_("Incoming voice call"), NULL },
1569 { EMPATHY_SOUND_PHONE_OUTGOING, "phone-outgoing-calling",
1570 N_("Outgoing voice call"), NULL },
1571 { EMPATHY_SOUND_PHONE_HANGUP, "phone-hangup",
1572 N_("Voice call ended"), NULL },
1577 empathy_sound_pref_is_enabled (const char *key)
1582 conf = empathy_conf_get ();
1585 empathy_conf_get_bool (conf, EMPATHY_PREFS_SOUNDS_ENABLED, &res);
1591 if (!empathy_check_available_state ()) {
1592 empathy_conf_get_bool (conf, EMPATHY_PREFS_SOUNDS_DISABLED_AWAY,
1599 empathy_conf_get_bool (conf, key, &res);
1605 empathy_sound_stop (EmpathySound sound_id)
1607 EmpathySoundEntry *entry;
1609 g_return_if_fail (sound_id < LAST_EMPATHY_SOUND);
1611 entry = &(sound_entries[sound_id]);
1612 g_return_if_fail (entry->sound_id == sound_id);
1614 ca_context_cancel (ca_gtk_context_get (), entry->sound_id);
1619 empathy_sound_play_full (GtkWidget *widget, EmpathySound sound_id,
1620 ca_finish_callback_t callback, gpointer user_data)
1622 EmpathySoundEntry *entry;
1623 gboolean should_play = TRUE;
1624 ca_proplist *p = NULL;
1627 g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
1629 entry = &(sound_entries[sound_id]);
1630 g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
1632 if (entry->gconf_key != NULL) {
1633 should_play = empathy_sound_pref_is_enabled (entry->gconf_key);
1639 c = ca_gtk_context_get ();
1640 ca_context_cancel (c, entry->sound_id);
1642 DEBUG ("Play sound \"%s\" (%s)",
1644 entry->event_ca_description);
1646 if (ca_proplist_create (&p) < 0)
1649 if (ca_proplist_sets (p, CA_PROP_EVENT_ID, entry->event_ca_id) < 0)
1652 if (ca_proplist_sets (p, CA_PROP_EVENT_DESCRIPTION,
1653 gettext (entry->event_ca_id)) < 0)
1656 if (ca_gtk_proplist_set_for_widget (p, widget) < 0)
1659 ca_context_play_full (ca_gtk_context_get (), entry->sound_id,
1660 p, callback, user_data);
1662 ca_proplist_destroy (p);
1668 ca_proplist_destroy (p);
1674 empathy_sound_play (GtkWidget *widget, EmpathySound sound_id)
1676 empathy_sound_play_full (widget, sound_id, NULL, NULL);