2 * Copyright (C) 2002-2007 Imendio AB
3 * Copyright (C) 2007-2010 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
24 * Jonny Lamb <jonny.lamb@collabora.co.uk>
25 * Travis Reitter <travis.reitter@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 <telepathy-glib/util.h>
42 #include <folks/folks.h>
44 #include "empathy-ui-utils.h"
45 #include "empathy-images.h"
46 #include "empathy-live-search.h"
47 #include "empathy-smiley-manager.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
50 #include <libempathy/empathy-debug.h>
51 #include <libempathy/empathy-utils.h>
52 #include <libempathy/empathy-ft-factory.h>
55 empathy_gtk_init (void)
57 static gboolean initialized = FALSE;
64 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
65 PKGDATADIR G_DIR_SEPARATOR_S "icons");
67 /* Add icons from source dir if available */
68 if (g_getenv ("EMPATHY_SRCDIR") != NULL)
72 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "data",
73 "icons", "local-copy", NULL);
75 if (g_file_test (path, G_FILE_TEST_EXISTS))
76 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), path);
85 builder_get_file_valist (const gchar *filename,
86 const gchar *first_object,
94 DEBUG ("Loading file %s", filename);
96 gui = gtk_builder_new ();
97 gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
99 if (!gtk_builder_add_from_file (gui, filename, &error))
101 g_critical ("GtkBuilder Error (%s): %s",
102 filename, error->message);
104 g_clear_error (&error);
105 g_object_unref (gui);
107 /* we need to iterate and set all of the pointers to NULL */
108 for (name = first_object; name; name = va_arg (args, const gchar *))
110 object_ptr = va_arg (args, GObject**);
118 for (name = first_object; name; name = va_arg (args, const gchar *))
120 object_ptr = va_arg (args, GObject**);
122 *object_ptr = gtk_builder_get_object (gui, name);
126 g_warning ("File is missing object '%s'.", name);
135 empathy_builder_get_file (const gchar *filename,
136 const gchar *first_object,
142 va_start (args, first_object);
143 gui = builder_get_file_valist (filename, first_object, args);
150 empathy_builder_connect (GtkBuilder *gui,
152 const gchar *first_object,
161 va_start (args, first_object);
162 for (name = first_object; name; name = va_arg (args, const gchar *))
164 sig = va_arg (args, const gchar *);
165 callback = va_arg (args, GCallback);
167 object = gtk_builder_get_object (gui, name);
170 g_warning ("File is missing object '%s'.", name);
174 g_signal_connect (object, sig, callback, user_data);
181 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
184 /* On construction gui sinks the initial reference to widget. When gui
185 * is finalized it will drop its ref to widget. We take our own ref to
186 * prevent widget being finalised. The widget is forced to have a
187 * floating reference, like when it was initially unowned so that it can
188 * be used like any other GtkWidget. */
190 g_object_ref (widget);
191 g_object_force_floating (G_OBJECT (widget));
192 g_object_unref (gui);
198 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 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
210 EMPATHY_IMAGE_EXT_AWAY))
211 return EMPATHY_IMAGE_EXT_AWAY;
213 /* The 'extended-away' icon is not an official one so we fallback to
214 * idle if it's not implemented */
215 return EMPATHY_IMAGE_IDLE;
216 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
217 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
218 EMPATHY_IMAGE_HIDDEN))
219 return EMPATHY_IMAGE_HIDDEN;
221 /* The 'hidden' icon is not an official one so we fallback to offline if
222 * it's not implemented */
223 return EMPATHY_IMAGE_OFFLINE;
224 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
225 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
226 return EMPATHY_IMAGE_OFFLINE;
227 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
228 return EMPATHY_IMAGE_PENDING;
229 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
238 empathy_icon_name_for_contact (EmpathyContact *contact)
240 TpConnectionPresenceType presence;
242 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
243 EMPATHY_IMAGE_OFFLINE);
245 presence = empathy_contact_get_presence (contact);
246 return empathy_icon_name_for_presence (presence);
250 empathy_icon_name_for_individual (FolksIndividual *individual)
252 FolksPresenceType folks_presence;
253 TpConnectionPresenceType presence;
255 folks_presence = folks_presence_details_get_presence_type (
256 FOLKS_PRESENCE_DETAILS (individual));
257 presence = empathy_folks_presence_type_to_tp (folks_presence);
259 return empathy_icon_name_for_presence (presence);
263 empathy_protocol_name_for_contact (EmpathyContact *contact)
267 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
269 account = empathy_contact_get_account (contact);
273 return tp_account_get_icon_name (account);
277 empathy_pixbuf_from_data (gchar *data,
280 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
284 empathy_pixbuf_from_data_and_mime (gchar *data,
288 GdkPixbufLoader *loader;
289 GdkPixbufFormat *format;
290 GdkPixbuf *pixbuf = NULL;
292 GError *error = NULL;
297 loader = gdk_pixbuf_loader_new ();
298 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error))
300 DEBUG ("Failed to write to pixbuf loader: %s",
301 error ? error->message : "No error given");
305 if (!gdk_pixbuf_loader_close (loader, &error))
307 DEBUG ("Failed to close pixbuf loader: %s",
308 error ? error->message : "No error given");
312 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
315 g_object_ref (pixbuf);
317 if (mime_type != NULL)
319 format = gdk_pixbuf_loader_get_format (loader);
320 mime_types = gdk_pixbuf_format_get_mime_types (format);
322 *mime_type = g_strdup (*mime_types);
323 if (mime_types[1] != NULL)
324 DEBUG ("Loader supports more than one mime "
325 "type! Picking the first one, %s",
328 g_strfreev (mime_types);
333 g_clear_error (&error);
334 g_object_unref (loader);
343 gboolean preserve_aspect_ratio;
347 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
350 struct SizeData *data)
352 g_return_if_fail (width > 0 && height > 0);
354 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0))
356 if (width < data->width && height < data->height)
364 width = width * (double) data->height / (gdouble) height;
365 height = data->height;
367 else if (data->height < 0)
369 height = height * (double) data->width / (double) width;
372 else if ((double) height * (double) data->width >
373 (double) width * (double) data->height)
375 width = 0.5 + (double) width * (double) data->height / (double) height;
376 height = data->height;
380 height = 0.5 + (double) height * (double) data->width / (double) width;
389 if (data->height > 0)
390 height = data->height;
393 gdk_pixbuf_loader_set_size (loader, width, height);
397 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
399 gint width, height, rowstride;
402 width = gdk_pixbuf_get_width (pixbuf);
403 height = gdk_pixbuf_get_height (pixbuf);
404 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
405 pixels = gdk_pixbuf_get_pixels (pixbuf);
407 if (width < 6 || height < 6)
414 pixels[rowstride + 3] = 0x80;
415 pixels[rowstride * 2 + 3] = 0xC0;
418 pixels[width * 4 - 1] = 0;
419 pixels[width * 4 - 5] = 0x80;
420 pixels[width * 4 - 9] = 0xC0;
421 pixels[rowstride + (width * 4) - 1] = 0x80;
422 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
425 pixels[(height - 1) * rowstride + 3] = 0;
426 pixels[(height - 1) * rowstride + 7] = 0x80;
427 pixels[(height - 1) * rowstride + 11] = 0xC0;
428 pixels[(height - 2) * rowstride + 3] = 0x80;
429 pixels[(height - 3) * rowstride + 3] = 0xC0;
432 pixels[height * rowstride - 1] = 0;
433 pixels[(height - 1) * rowstride - 1] = 0x80;
434 pixels[(height - 2) * rowstride - 1] = 0xC0;
435 pixels[height * rowstride - 5] = 0x80;
436 pixels[height * rowstride - 9] = 0xC0;
440 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
442 gint height, rowstride, i;
446 height = gdk_pixbuf_get_height (pixbuf);
447 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
448 pixels = gdk_pixbuf_get_pixels (pixbuf);
451 for (i = 3; i < rowstride; i+=4)
455 for (i = 1; i < height - 1; i++)
457 row = pixels + (i*rowstride);
458 if (row[3] < 0xfe || row[rowstride-1] < 0xfe)
462 row = pixels + ((height-1) * rowstride);
463 for (i = 3; i < rowstride; i+=4)
471 pixbuf_round_corners (GdkPixbuf *pixbuf)
475 if (!gdk_pixbuf_get_has_alpha (pixbuf))
477 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
478 gdk_pixbuf_get_width (pixbuf),
479 gdk_pixbuf_get_height (pixbuf));
481 gdk_pixbuf_copy_area (pixbuf, 0, 0,
482 gdk_pixbuf_get_width (pixbuf),
483 gdk_pixbuf_get_height (pixbuf),
489 result = g_object_ref (pixbuf);
492 if (empathy_gdk_pixbuf_is_opaque (result))
493 empathy_avatar_pixbuf_roundify (result);
499 avatar_pixbuf_from_loader (GdkPixbufLoader *loader)
503 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
505 return pixbuf_round_corners (pixbuf);
509 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
514 GdkPixbufLoader *loader;
515 struct SizeData data;
516 GError *error = NULL;
522 data.height = height;
523 data.preserve_aspect_ratio = TRUE;
525 loader = gdk_pixbuf_loader_new ();
527 g_signal_connect (loader, "size-prepared",
528 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb), &data);
530 if (avatar->len == 0)
532 g_warning ("Avatar has 0 length");
535 else if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error))
537 g_warning ("Couldn't write avatar image:%p with "
538 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
539 avatar->data, avatar->len, error->message);
541 g_error_free (error);
545 gdk_pixbuf_loader_close (loader, NULL);
546 pixbuf = avatar_pixbuf_from_loader (loader);
548 g_object_unref (loader);
554 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
558 EmpathyAvatar *avatar;
560 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
562 avatar = empathy_contact_get_avatar (contact);
564 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
569 GSimpleAsyncResult *result;
572 GCancellable *cancellable;
573 } PixbufAvatarFromIndividualClosure;
575 static PixbufAvatarFromIndividualClosure *
576 pixbuf_avatar_from_individual_closure_new (FolksIndividual *individual,
577 GSimpleAsyncResult *result,
580 GCancellable *cancellable)
582 PixbufAvatarFromIndividualClosure *closure;
584 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
585 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
587 closure = g_new0 (PixbufAvatarFromIndividualClosure, 1);
588 closure->result = g_object_ref (result);
589 closure->width = width;
590 closure->height = height;
592 if (cancellable != NULL)
593 closure->cancellable = g_object_ref (cancellable);
599 pixbuf_avatar_from_individual_closure_free (
600 PixbufAvatarFromIndividualClosure *closure)
602 g_clear_object (&closure->cancellable);
603 g_object_unref (closure->result);
608 * @pixbuf: (transfer all)
610 * Return: (transfer all)
613 transform_pixbuf (GdkPixbuf *pixbuf)
617 result = pixbuf_round_corners (pixbuf);
618 g_object_unref (pixbuf);
624 avatar_icon_load_cb (GObject *object,
625 GAsyncResult *result,
628 GLoadableIcon *icon = G_LOADABLE_ICON (object);
629 PixbufAvatarFromIndividualClosure *closure = user_data;
630 GInputStream *stream;
631 GError *error = NULL;
633 GdkPixbuf *final_pixbuf;
635 stream = g_loadable_icon_load_finish (icon, result, NULL, &error);
638 DEBUG ("Failed to open avatar stream: %s", error->message);
639 g_simple_async_result_set_from_error (closure->result, error);
643 pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
644 closure->width, closure->height, TRUE, closure->cancellable, &error);
646 g_object_unref (stream);
650 DEBUG ("Failed to read avatar: %s", error->message);
651 g_simple_async_result_set_from_error (closure->result, error);
655 final_pixbuf = transform_pixbuf (pixbuf);
657 /* Pass ownership of final_pixbuf to the result */
658 g_simple_async_result_set_op_res_gpointer (closure->result,
659 final_pixbuf, g_object_unref);
662 g_simple_async_result_complete (closure->result);
664 g_clear_error (&error);
665 pixbuf_avatar_from_individual_closure_free (closure);
669 empathy_pixbuf_avatar_from_individual_scaled_async (
670 FolksIndividual *individual,
673 GCancellable *cancellable,
674 GAsyncReadyCallback callback,
677 GLoadableIcon *avatar_icon;
678 GSimpleAsyncResult *result;
679 PixbufAvatarFromIndividualClosure *closure;
681 result = g_simple_async_result_new (G_OBJECT (individual),
682 callback, user_data, empathy_pixbuf_avatar_from_individual_scaled_async);
684 avatar_icon = folks_avatar_details_get_avatar (
685 FOLKS_AVATAR_DETAILS (individual));
687 if (avatar_icon == NULL)
689 g_simple_async_result_set_error (result, G_IO_ERROR,
690 G_IO_ERROR_NOT_FOUND, "no avatar found");
692 g_simple_async_result_complete (result);
693 g_object_unref (result);
697 closure = pixbuf_avatar_from_individual_closure_new (individual, result,
698 width, height, cancellable);
700 g_return_if_fail (closure != NULL);
702 g_loadable_icon_load_async (avatar_icon, width, cancellable,
703 avatar_icon_load_cb, closure);
705 g_object_unref (result);
708 /* Return a ref on the GdkPixbuf */
710 empathy_pixbuf_avatar_from_individual_scaled_finish (
711 FolksIndividual *individual,
712 GAsyncResult *result,
715 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
716 gboolean result_valid;
719 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
720 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
722 if (g_simple_async_result_propagate_error (simple, error))
725 result_valid = g_simple_async_result_is_valid (result,
726 G_OBJECT (individual),
727 empathy_pixbuf_avatar_from_individual_scaled_async);
729 g_return_val_if_fail (result_valid, NULL);
731 pixbuf = g_simple_async_result_get_op_res_gpointer (simple);
732 return pixbuf != NULL ? g_object_ref (pixbuf) : NULL;
736 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
737 gboolean show_protocol)
739 const gchar *icon_name;
741 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
743 icon_name = empathy_icon_name_for_contact (contact);
745 if (icon_name == NULL)
748 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
749 icon_name, show_protocol);
752 static GdkPixbuf * empathy_pixbuf_protocol_from_contact_scaled (
753 EmpathyContact *contact,
758 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
759 const gchar *icon_name,
760 gboolean show_protocol)
762 GdkPixbuf *pix_status;
763 GdkPixbuf *pix_protocol;
764 gchar *icon_filename;
766 gint numerator, denominator;
768 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
769 (show_protocol == FALSE), NULL);
770 g_return_val_if_fail (icon_name != NULL, NULL);
775 icon_filename = empathy_filename_from_icon_name (icon_name,
778 if (icon_filename == NULL)
780 DEBUG ("icon name: %s could not be found\n", icon_name);
784 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
786 if (pix_status == NULL)
788 DEBUG ("Could not open icon %s\n", icon_filename);
789 g_free (icon_filename);
793 g_free (icon_filename);
798 height = gdk_pixbuf_get_height (pix_status);
799 width = gdk_pixbuf_get_width (pix_status);
801 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
802 width * numerator / denominator,
803 height * numerator / denominator);
805 if (pix_protocol == NULL)
808 gdk_pixbuf_composite (pix_protocol, pix_status,
809 0, height - height * numerator / denominator,
810 width * numerator / denominator, height * numerator / denominator,
811 0, height - height * numerator / denominator,
813 GDK_INTERP_BILINEAR, 255);
815 g_object_unref (pix_protocol);
821 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
827 GdkPixbuf *pixbuf = NULL;
829 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
831 account = empathy_contact_get_account (contact);
832 filename = empathy_filename_from_icon_name (
833 tp_account_get_icon_name (account), GTK_ICON_SIZE_MENU);
835 if (filename != NULL)
837 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
845 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf,
851 width = gdk_pixbuf_get_width (pixbuf);
852 height = gdk_pixbuf_get_height (pixbuf);
854 if (width > 0 && (width > max_size || height > max_size))
856 factor = (gdouble) max_size / MAX (width, height);
858 width = width * factor;
859 height = height * factor;
861 return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_HYPER);
864 return g_object_ref (pixbuf);
868 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
873 GError *error = NULL;
878 theme = gtk_icon_theme_get_default ();
880 pixbuf = gtk_icon_theme_load_icon (theme, icon_name, size, 0, &error);
884 DEBUG ("Error loading icon: %s", error->message);
885 g_clear_error (&error);
892 empathy_pixbuf_from_icon_name (const gchar *icon_name,
893 GtkIconSize icon_size)
901 if (gtk_icon_size_lookup (icon_size, &w, &h))
904 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
908 empathy_filename_from_icon_name (const gchar *icon_name,
909 GtkIconSize icon_size)
911 GtkIconTheme *icon_theme;
912 GtkIconInfo *icon_info;
917 icon_theme = gtk_icon_theme_get_default ();
919 if (gtk_icon_size_lookup (icon_size, &w, &h))
922 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
923 if (icon_info == NULL)
926 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
927 gtk_icon_info_free (icon_info);
932 /* Takes care of moving the window to the current workspace. */
934 empathy_window_present_with_time (GtkWindow *window,
937 GdkWindow *gdk_window;
939 g_return_if_fail (GTK_IS_WINDOW (window));
941 /* Move the window to the current workspace before trying to show it.
942 * This is the behaviour people expect when clicking on the statusbar icon. */
943 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
950 /* Has no effect if the WM has viewports, like compiz */
951 gdk_x11_window_move_to_current_desktop (gdk_window);
953 /* If window is still off-screen, hide it to force it to
954 * reposition on the current workspace. */
955 gtk_window_get_position (window, &x, &y);
956 gtk_window_get_size (window, &w, &h);
957 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
958 gtk_widget_hide (GTK_WIDGET (window));
961 if (timestamp == GDK_CURRENT_TIME)
962 gtk_window_present (window);
964 gtk_window_present_with_time (window, timestamp);
968 empathy_window_present (GtkWindow *window)
970 empathy_window_present_with_time (window, gtk_get_current_event_time ());
974 empathy_get_toplevel_window (GtkWidget *widget)
978 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
980 toplevel = gtk_widget_get_toplevel (widget);
981 if (GTK_IS_WINDOW (toplevel) &&
982 gtk_widget_is_toplevel (toplevel))
983 return GTK_WINDOW (toplevel);
988 /** empathy_make_absolute_url_len:
992 * Same as #empathy_make_absolute_url but for a limited string length
995 empathy_make_absolute_url_len (const gchar *url,
998 g_return_val_if_fail (url != NULL, NULL);
1000 if (g_str_has_prefix (url, "help:") ||
1001 g_str_has_prefix (url, "mailto:") ||
1003 return g_strndup (url, len);
1005 if (strstr (url, "@"))
1006 return g_strdup_printf ("mailto:%.*s", len, url);
1008 return g_strdup_printf ("http://%.*s", len, url);
1011 /** empathy_make_absolute_url:
1014 * The URL opening code can't handle schemeless strings, so we try to be
1015 * smart and add http if there is no scheme or doesn't look like a mail
1016 * address. This should work in most cases, and let us click on strings
1017 * like "www.gnome.org".
1019 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1020 * g_free when your are done with it
1023 empathy_make_absolute_url (const gchar *url)
1025 return empathy_make_absolute_url_len (url, strlen (url));
1029 empathy_url_show (GtkWidget *parent,
1033 GError *error = NULL;
1035 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1036 g_return_if_fail (url != NULL);
1038 real_url = empathy_make_absolute_url (url);
1040 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1041 gtk_get_current_event_time (), &error);
1047 dialog = gtk_message_dialog_new (NULL, 0,
1048 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1049 _("Unable to open URI"));
1051 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1052 "%s", error->message);
1054 g_signal_connect (dialog, "response",
1055 G_CALLBACK (gtk_widget_destroy), NULL);
1057 gtk_window_present (GTK_WINDOW (dialog));
1059 g_clear_error (&error);
1066 empathy_send_file (EmpathyContact *contact,
1069 EmpathyFTFactory *factory;
1070 GtkRecentManager *manager;
1073 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1074 g_return_if_fail (G_IS_FILE (file));
1076 factory = empathy_ft_factory_dup_singleton ();
1078 empathy_ft_factory_new_transfer_outgoing (factory, contact, file,
1079 empathy_get_current_action_time ());
1081 uri = g_file_get_uri (file);
1082 manager = gtk_recent_manager_get_default ();
1083 gtk_recent_manager_add_item (manager, uri);
1086 g_object_unref (factory);
1090 empathy_send_file_from_uri_list (EmpathyContact *contact,
1091 const gchar *uri_list)
1096 /* Only handle a single file for now. It would be wicked cool to be
1097 able to do multiple files, offering to zip them or whatever like
1098 nautilus-sendto does. Note that text/uri-list is defined to have
1099 each line terminated by \r\n, but we can be tolerant of applications
1100 that only use \n or don't terminate single-line entries.
1102 nl = strstr (uri_list, "\r\n");
1104 nl = strchr (uri_list, '\n');
1108 gchar *uri = g_strndup (uri_list, nl - uri_list);
1109 file = g_file_new_for_uri (uri);
1114 file = g_file_new_for_uri (uri_list);
1117 empathy_send_file (contact, file);
1119 g_object_unref (file);
1123 file_manager_send_file_response_cb (GtkDialog *widget,
1125 EmpathyContact *contact)
1129 if (response_id == GTK_RESPONSE_OK)
1131 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1133 empathy_send_file (contact, file);
1135 g_object_unref (file);
1138 g_object_unref (contact);
1139 gtk_widget_destroy (GTK_WIDGET (widget));
1143 filter_cb (const GtkFileFilterInfo *filter_info,
1146 /* filter out socket files */
1147 return tp_strdiff (filter_info->mime_type, "inode/socket");
1150 static GtkFileFilter *
1151 create_file_filter (void)
1153 GtkFileFilter *filter;
1155 filter = gtk_file_filter_new ();
1157 gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_MIME_TYPE, filter_cb,
1164 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1169 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1171 DEBUG ("Creating selection file chooser");
1173 widget = gtk_file_chooser_dialog_new (_("Select a file"), NULL,
1174 GTK_FILE_CHOOSER_ACTION_OPEN,
1175 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1179 button = gtk_button_new_with_mnemonic (_("_Send"));
1180 gtk_button_set_image (GTK_BUTTON (button),
1181 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1182 GTK_ICON_SIZE_BUTTON));
1183 gtk_widget_show (button);
1185 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1188 gtk_widget_set_can_default (button, TRUE);
1189 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1192 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1194 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1197 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (widget),
1198 create_file_filter ());
1200 g_signal_connect (widget, "response",
1201 G_CALLBACK (file_manager_send_file_response_cb), g_object_ref (contact));
1203 gtk_widget_show (widget);
1207 file_manager_receive_file_response_cb (GtkDialog *dialog,
1208 GtkResponseType response,
1209 EmpathyFTHandler *handler)
1211 EmpathyFTFactory *factory;
1214 if (response == GTK_RESPONSE_OK)
1218 guint64 free_space, file_size;
1219 GError *error = NULL;
1221 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1222 parent = g_file_get_parent (file);
1223 info = g_file_query_filesystem_info (parent,
1224 G_FILE_ATTRIBUTE_FILESYSTEM_FREE, NULL, &error);
1226 g_object_unref (parent);
1230 g_warning ("Error: %s", error->message);
1232 g_object_unref (file);
1236 free_space = g_file_info_get_attribute_uint64 (info,
1237 G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
1238 file_size = empathy_ft_handler_get_total_bytes (handler);
1240 g_object_unref (info);
1242 if (file_size > free_space)
1244 GtkWidget *message = gtk_message_dialog_new (GTK_WINDOW (dialog),
1245 GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
1247 _("Insufficient free space to save file"));
1248 char *file_size_str, *free_space_str;
1250 file_size_str = g_format_size (file_size);
1251 free_space_str = g_format_size (free_space);
1253 gtk_message_dialog_format_secondary_text (
1254 GTK_MESSAGE_DIALOG (message),
1255 _("%s of free space are required to save this "
1256 "file, but only %s is available. Please "
1257 "choose another location."),
1258 file_size_str, free_space_str);
1260 gtk_dialog_run (GTK_DIALOG (message));
1262 g_free (file_size_str);
1263 g_free (free_space_str);
1264 gtk_widget_destroy (message);
1266 g_object_unref (file);
1271 factory = empathy_ft_factory_dup_singleton ();
1273 empathy_ft_factory_set_destination_for_incoming_handler (
1274 factory, handler, file);
1276 g_object_unref (factory);
1277 g_object_unref (file);
1281 /* unref the handler, as we dismissed the file chooser,
1282 * and refused the transfer.
1284 g_object_unref (handler);
1287 gtk_widget_destroy (GTK_WIDGET (dialog));
1291 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1295 EmpathyContact *contact;
1298 contact = empathy_ft_handler_get_contact (handler);
1299 g_assert (contact != NULL);
1301 title = g_strdup_printf (_("Incoming file from %s"),
1302 empathy_contact_get_alias (contact));
1304 widget = gtk_file_chooser_dialog_new (title,
1305 NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
1306 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1307 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
1310 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1311 empathy_ft_handler_get_filename (handler));
1313 gtk_file_chooser_set_do_overwrite_confirmation
1314 (GTK_FILE_CHOOSER (widget), TRUE);
1316 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1318 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1319 dir = g_get_home_dir ();
1321 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1323 g_signal_connect (widget, "response",
1324 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1326 gtk_widget_show (widget);
1331 empathy_make_color_whiter (GdkRGBA *color)
1333 const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
1335 color->red = (color->red + white.red) / 2;
1336 color->green = (color->green + white.green) / 2;
1337 color->blue = (color->blue + white.blue) / 2;
1341 menu_deactivate_cb (GtkMenu *menu,
1344 /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
1345 g_signal_handlers_disconnect_by_func (menu,
1346 menu_deactivate_cb, user_data);
1348 gtk_menu_detach (menu);
1351 /* Convenient function to create a GtkMenu attached to @attach_to and detach
1352 * it when the menu is not displayed any more. This is useful when creating a
1353 * context menu that we want to get rid as soon as it as been displayed. */
1355 empathy_context_menu_new (GtkWidget *attach_to)
1359 menu = gtk_menu_new ();
1361 gtk_menu_attach_to_widget (GTK_MENU (menu), attach_to, NULL);
1363 /* menu is initially unowned but gtk_menu_attach_to_widget () taked its
1364 * floating ref. We can either wait that @attach_to releases its ref when
1365 * it will be destroyed (when leaving Empathy most of the time) or explicitely
1366 * detach the menu when it's not displayed any more.
1367 * We go for the latter as we don't want to keep useless menus in memory
1368 * during the whole lifetime of Empathy. */
1369 g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb), NULL);
1375 empathy_get_current_action_time (void)
1377 return (tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
1380 /* @words = empathy_live_search_strip_utf8_string (@text);
1382 * User has to pass both so we don't have to compute @words ourself each time
1383 * this function is called. */
1385 empathy_individual_match_string (FolksIndividual *individual,
1392 gboolean retval = FALSE;
1394 /* check alias name */
1395 str = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual));
1397 if (empathy_live_search_match_words (str, words))
1400 personas = folks_individual_get_personas (individual);
1402 /* check contact id, remove the @server.com part */
1403 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1404 while (retval == FALSE && gee_iterator_next (iter))
1406 FolksPersona *persona = gee_iterator_get (iter);
1409 if (empathy_folks_persona_is_interesting (persona))
1411 str = folks_persona_get_display_id (persona);
1413 /* Accept the persona if @text is a full prefix of his ID; that allows
1414 * user to find, say, a jabber contact by typing his JID. */
1415 if (g_str_has_prefix (str, text))
1421 gchar *dup_str = NULL;
1424 p = strstr (str, "@");
1426 str = dup_str = g_strndup (str, p - str);
1428 visible = empathy_live_search_match_words (str, words);
1434 g_clear_object (&persona);
1436 g_clear_object (&iter);
1438 /* FIXME: Add more rules here, we could check phone numbers in
1439 * contact's vCard for example. */
1444 empathy_launch_program (const gchar *dir,
1448 GdkDisplay *display;
1449 GError *error = NULL;
1452 GdkAppLaunchContext *context = NULL;
1454 /* Try to run from source directory if possible */
1455 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1458 if (!g_file_test (path, G_FILE_TEST_EXISTS))
1461 path = g_build_filename (dir, name, NULL);
1465 cmd = g_strconcat (path, " ", args, NULL);
1467 cmd = g_strdup (path);
1469 app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
1470 if (app_info == NULL)
1472 DEBUG ("Failed to create app info: %s", error->message);
1473 g_error_free (error);
1477 display = gdk_display_get_default ();
1478 context = gdk_display_get_app_launch_context (display);
1480 if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
1483 g_warning ("Failed to launch %s: %s", name, error->message);
1484 g_error_free (error);
1489 tp_clear_object (&app_info);
1490 tp_clear_object (&context);
1495 /* Most of the workspace manipulation code has been copied from libwnck
1496 * Copyright (C) 2001 Havoc Pennington
1497 * Copyright (C) 2005-2007 Vincent Untz
1500 _wnck_activate_workspace (Screen *screen,
1501 int new_active_space,
1508 display = DisplayOfScreen (screen);
1509 root = RootWindowOfScreen (screen);
1511 xev.xclient.type = ClientMessage;
1512 xev.xclient.serial = 0;
1513 xev.xclient.send_event = True;
1514 xev.xclient.display = display;
1515 xev.xclient.window = root;
1516 xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_CURRENT_DESKTOP");
1517 xev.xclient.format = 32;
1518 xev.xclient.data.l[0] = new_active_space;
1519 xev.xclient.data.l[1] = timestamp;
1520 xev.xclient.data.l[2] = 0;
1521 xev.xclient.data.l[3] = 0;
1522 xev.xclient.data.l[4] = 0;
1524 gdk_error_trap_push ();
1525 XSendEvent (display, root, False,
1526 SubstructureRedirectMask | SubstructureNotifyMask,
1528 XSync (display, False);
1529 gdk_error_trap_pop_ignored ();
1533 _wnck_get_cardinal (Screen *screen,
1546 display = DisplayOfScreen (screen);
1550 gdk_error_trap_push ();
1552 result = XGetWindowProperty (display, xwindow, atom,
1553 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems,
1554 &bytes_after, (void *) &num);
1555 err = gdk_error_trap_pop ();
1556 if (err != Success ||
1560 if (type != XA_CARDINAL)
1574 window_get_workspace (Screen *xscreen,
1579 if (!_wnck_get_cardinal (xscreen, win,
1580 gdk_x11_get_xatom_by_name ("_NET_WM_DESKTOP"), &number))
1586 /* Ask X to move to the desktop on which @window currently is
1587 * and the present @window. */
1589 empathy_move_to_window_desktop (GtkWindow *window,
1594 GdkWindow *gdk_window;
1597 screen = gtk_window_get_screen (window);
1598 xscreen = gdk_x11_screen_get_xscreen (screen);
1599 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1601 workspace = window_get_workspace (xscreen,
1602 gdk_x11_window_get_xid (gdk_window));
1603 if (workspace == -1)
1606 _wnck_activate_workspace (xscreen, workspace, timestamp);
1609 gtk_window_present_with_time (window, timestamp);
1613 empathy_set_css_provider (GtkWidget *widget)
1615 GtkCssProvider *provider;
1617 GError *error = NULL;
1620 filename = empathy_file_lookup ("empathy.css", "data");
1622 provider = gtk_css_provider_new ();
1624 if (!gtk_css_provider_load_from_path (provider, filename, &error))
1626 g_warning ("Failed to load css file '%s': %s", filename, error->message);
1627 g_error_free (error);
1632 screen = gtk_widget_get_screen (widget);
1634 screen = gdk_screen_get_default ();
1636 gtk_style_context_add_provider_for_screen (screen,
1637 GTK_STYLE_PROVIDER (provider),
1638 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
1642 g_object_unref (provider);