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>
40 #include <gio/gdesktopappinfo.h>
42 #include <telepathy-glib/util.h>
43 #include <folks/folks.h>
45 #include "empathy-ui-utils.h"
46 #include "empathy-images.h"
47 #include "empathy-live-search.h"
48 #include "empathy-smiley-manager.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
51 #include <libempathy/empathy-debug.h>
52 #include <libempathy/empathy-utils.h>
53 #include <libempathy/empathy-ft-factory.h>
56 empathy_gtk_init (void)
58 static gboolean initialized = FALSE;
65 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
66 PKGDATADIR G_DIR_SEPARATOR_S "icons");
68 /* Add icons from source dir if available */
69 if (g_getenv ("EMPATHY_SRCDIR") != NULL)
73 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "data",
74 "icons", "local-copy", NULL);
76 if (g_file_test (path, G_FILE_TEST_EXISTS))
77 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), path);
86 builder_get_file_valist (const gchar *filename,
87 const gchar *first_object,
95 DEBUG ("Loading file %s", filename);
97 gui = gtk_builder_new ();
98 gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
100 if (!gtk_builder_add_from_file (gui, filename, &error))
102 g_critical ("GtkBuilder Error (%s): %s",
103 filename, error->message);
105 g_clear_error (&error);
106 g_object_unref (gui);
108 /* we need to iterate and set all of the pointers to NULL */
109 for (name = first_object; name; name = va_arg (args, const gchar *))
111 object_ptr = va_arg (args, GObject**);
119 for (name = first_object; name; name = va_arg (args, const gchar *))
121 object_ptr = va_arg (args, GObject**);
123 *object_ptr = gtk_builder_get_object (gui, name);
127 g_warning ("File is missing object '%s'.", name);
136 empathy_builder_get_file (const gchar *filename,
137 const gchar *first_object,
143 va_start (args, first_object);
144 gui = builder_get_file_valist (filename, first_object, args);
151 empathy_builder_connect (GtkBuilder *gui,
153 const gchar *first_object,
162 va_start (args, first_object);
163 for (name = first_object; name; name = va_arg (args, const gchar *))
165 sig = va_arg (args, const gchar *);
166 callback = va_arg (args, GCallback);
168 object = gtk_builder_get_object (gui, name);
171 g_warning ("File is missing object '%s'.", name);
175 g_signal_connect (object, sig, callback, user_data);
182 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
185 /* On construction gui sinks the initial reference to widget. When gui
186 * is finalized it will drop its ref to widget. We take our own ref to
187 * prevent widget being finalised. The widget is forced to have a
188 * floating reference, like when it was initially unowned so that it can
189 * be used like any other GtkWidget. */
191 g_object_ref (widget);
192 g_object_force_floating (G_OBJECT (widget));
193 g_object_unref (gui);
199 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
203 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
204 return EMPATHY_IMAGE_AVAILABLE;
205 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
206 return EMPATHY_IMAGE_BUSY;
207 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
208 return EMPATHY_IMAGE_AWAY;
209 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
210 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
211 EMPATHY_IMAGE_EXT_AWAY))
212 return EMPATHY_IMAGE_EXT_AWAY;
214 /* The 'extended-away' icon is not an official one so we fallback to
215 * idle if it's not implemented */
216 return EMPATHY_IMAGE_IDLE;
217 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
218 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
219 EMPATHY_IMAGE_HIDDEN))
220 return EMPATHY_IMAGE_HIDDEN;
222 /* The 'hidden' icon is not an official one so we fallback to offline if
223 * it's not implemented */
224 return EMPATHY_IMAGE_OFFLINE;
225 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
226 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
227 return EMPATHY_IMAGE_OFFLINE;
228 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
229 return EMPATHY_IMAGE_PENDING;
230 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
239 empathy_icon_name_for_contact (EmpathyContact *contact)
241 TpConnectionPresenceType presence;
243 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
244 EMPATHY_IMAGE_OFFLINE);
246 presence = empathy_contact_get_presence (contact);
247 return empathy_icon_name_for_presence (presence);
251 empathy_icon_name_for_individual (FolksIndividual *individual)
253 FolksPresenceType folks_presence;
254 TpConnectionPresenceType presence;
256 folks_presence = folks_presence_details_get_presence_type (
257 FOLKS_PRESENCE_DETAILS (individual));
258 presence = empathy_folks_presence_type_to_tp (folks_presence);
260 return empathy_icon_name_for_presence (presence);
264 empathy_protocol_name_for_contact (EmpathyContact *contact)
268 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
270 account = empathy_contact_get_account (contact);
274 return tp_account_get_icon_name (account);
278 empathy_pixbuf_from_data (gchar *data,
281 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
285 empathy_pixbuf_from_data_and_mime (gchar *data,
289 GdkPixbufLoader *loader;
290 GdkPixbufFormat *format;
291 GdkPixbuf *pixbuf = NULL;
293 GError *error = NULL;
298 loader = gdk_pixbuf_loader_new ();
299 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error))
301 DEBUG ("Failed to write to pixbuf loader: %s",
302 error ? error->message : "No error given");
306 if (!gdk_pixbuf_loader_close (loader, &error))
308 DEBUG ("Failed to close pixbuf loader: %s",
309 error ? error->message : "No error given");
313 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
316 g_object_ref (pixbuf);
318 if (mime_type != NULL)
320 format = gdk_pixbuf_loader_get_format (loader);
321 mime_types = gdk_pixbuf_format_get_mime_types (format);
323 *mime_type = g_strdup (*mime_types);
324 if (mime_types[1] != NULL)
325 DEBUG ("Loader supports more than one mime "
326 "type! Picking the first one, %s",
329 g_strfreev (mime_types);
334 g_clear_error (&error);
335 g_object_unref (loader);
344 gboolean preserve_aspect_ratio;
348 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
351 struct SizeData *data)
353 g_return_if_fail (width > 0 && height > 0);
355 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0))
357 if (width < data->width && height < data->height)
365 width = width * (double) data->height / (gdouble) height;
366 height = data->height;
368 else if (data->height < 0)
370 height = height * (double) data->width / (double) width;
373 else if ((double) height * (double) data->width >
374 (double) width * (double) data->height)
376 width = 0.5 + (double) width * (double) data->height / (double) height;
377 height = data->height;
381 height = 0.5 + (double) height * (double) data->width / (double) width;
390 if (data->height > 0)
391 height = data->height;
394 gdk_pixbuf_loader_set_size (loader, width, height);
398 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
400 gint width, height, rowstride;
403 width = gdk_pixbuf_get_width (pixbuf);
404 height = gdk_pixbuf_get_height (pixbuf);
405 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
406 pixels = gdk_pixbuf_get_pixels (pixbuf);
408 if (width < 6 || height < 6)
415 pixels[rowstride + 3] = 0x80;
416 pixels[rowstride * 2 + 3] = 0xC0;
419 pixels[width * 4 - 1] = 0;
420 pixels[width * 4 - 5] = 0x80;
421 pixels[width * 4 - 9] = 0xC0;
422 pixels[rowstride + (width * 4) - 1] = 0x80;
423 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
426 pixels[(height - 1) * rowstride + 3] = 0;
427 pixels[(height - 1) * rowstride + 7] = 0x80;
428 pixels[(height - 1) * rowstride + 11] = 0xC0;
429 pixels[(height - 2) * rowstride + 3] = 0x80;
430 pixels[(height - 3) * rowstride + 3] = 0xC0;
433 pixels[height * rowstride - 1] = 0;
434 pixels[(height - 1) * rowstride - 1] = 0x80;
435 pixels[(height - 2) * rowstride - 1] = 0xC0;
436 pixels[height * rowstride - 5] = 0x80;
437 pixels[height * rowstride - 9] = 0xC0;
441 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
443 gint height, rowstride, i;
447 height = gdk_pixbuf_get_height (pixbuf);
448 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
449 pixels = gdk_pixbuf_get_pixels (pixbuf);
452 for (i = 3; i < rowstride; i+=4)
456 for (i = 1; i < height - 1; i++)
458 row = pixels + (i*rowstride);
459 if (row[3] < 0xfe || row[rowstride-1] < 0xfe)
463 row = pixels + ((height-1) * rowstride);
464 for (i = 3; i < rowstride; i+=4)
472 pixbuf_round_corners (GdkPixbuf *pixbuf)
476 if (!gdk_pixbuf_get_has_alpha (pixbuf))
478 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
479 gdk_pixbuf_get_width (pixbuf),
480 gdk_pixbuf_get_height (pixbuf));
482 gdk_pixbuf_copy_area (pixbuf, 0, 0,
483 gdk_pixbuf_get_width (pixbuf),
484 gdk_pixbuf_get_height (pixbuf),
490 result = g_object_ref (pixbuf);
493 if (empathy_gdk_pixbuf_is_opaque (result))
494 empathy_avatar_pixbuf_roundify (result);
500 avatar_pixbuf_from_loader (GdkPixbufLoader *loader)
504 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
506 return pixbuf_round_corners (pixbuf);
510 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
515 GdkPixbufLoader *loader;
516 struct SizeData data;
517 GError *error = NULL;
523 data.height = height;
524 data.preserve_aspect_ratio = TRUE;
526 loader = gdk_pixbuf_loader_new ();
528 g_signal_connect (loader, "size-prepared",
529 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb), &data);
531 if (avatar->len == 0)
533 g_warning ("Avatar has 0 length");
536 else if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error))
538 g_warning ("Couldn't write avatar image:%p with "
539 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
540 avatar->data, avatar->len, error->message);
542 g_error_free (error);
546 gdk_pixbuf_loader_close (loader, NULL);
547 pixbuf = avatar_pixbuf_from_loader (loader);
549 g_object_unref (loader);
555 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
559 EmpathyAvatar *avatar;
561 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
563 avatar = empathy_contact_get_avatar (contact);
565 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
570 GSimpleAsyncResult *result;
573 GCancellable *cancellable;
574 } PixbufAvatarFromIndividualClosure;
576 static PixbufAvatarFromIndividualClosure *
577 pixbuf_avatar_from_individual_closure_new (FolksIndividual *individual,
578 GSimpleAsyncResult *result,
581 GCancellable *cancellable)
583 PixbufAvatarFromIndividualClosure *closure;
585 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
586 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
588 closure = g_slice_new0 (PixbufAvatarFromIndividualClosure);
589 closure->result = g_object_ref (result);
590 closure->width = width;
591 closure->height = height;
593 if (cancellable != NULL)
594 closure->cancellable = g_object_ref (cancellable);
600 pixbuf_avatar_from_individual_closure_free (
601 PixbufAvatarFromIndividualClosure *closure)
603 g_clear_object (&closure->cancellable);
604 g_object_unref (closure->result);
605 g_slice_free (PixbufAvatarFromIndividualClosure, closure);
609 * @pixbuf: (transfer all)
611 * Return: (transfer all)
614 transform_pixbuf (GdkPixbuf *pixbuf)
618 result = pixbuf_round_corners (pixbuf);
619 g_object_unref (pixbuf);
625 avatar_icon_load_cb (GObject *object,
626 GAsyncResult *result,
629 GLoadableIcon *icon = G_LOADABLE_ICON (object);
630 PixbufAvatarFromIndividualClosure *closure = user_data;
631 GInputStream *stream;
632 GError *error = NULL;
634 GdkPixbuf *final_pixbuf;
636 stream = g_loadable_icon_load_finish (icon, result, NULL, &error);
639 DEBUG ("Failed to open avatar stream: %s", error->message);
640 g_simple_async_result_set_from_error (closure->result, error);
644 pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
645 closure->width, closure->height, TRUE, closure->cancellable, &error);
647 g_object_unref (stream);
651 DEBUG ("Failed to read avatar: %s", error->message);
652 g_simple_async_result_set_from_error (closure->result, error);
656 final_pixbuf = transform_pixbuf (pixbuf);
658 /* Pass ownership of final_pixbuf to the result */
659 g_simple_async_result_set_op_res_gpointer (closure->result,
660 final_pixbuf, g_object_unref);
663 g_simple_async_result_complete (closure->result);
665 g_clear_error (&error);
666 pixbuf_avatar_from_individual_closure_free (closure);
670 empathy_pixbuf_avatar_from_individual_scaled_async (
671 FolksIndividual *individual,
674 GCancellable *cancellable,
675 GAsyncReadyCallback callback,
678 GLoadableIcon *avatar_icon;
679 GSimpleAsyncResult *result;
680 PixbufAvatarFromIndividualClosure *closure;
682 result = g_simple_async_result_new (G_OBJECT (individual),
683 callback, user_data, empathy_pixbuf_avatar_from_individual_scaled_async);
685 avatar_icon = folks_avatar_details_get_avatar (
686 FOLKS_AVATAR_DETAILS (individual));
688 if (avatar_icon == NULL)
690 g_simple_async_result_set_error (result, G_IO_ERROR,
691 G_IO_ERROR_NOT_FOUND, "no avatar found");
693 g_simple_async_result_complete (result);
694 g_object_unref (result);
698 closure = pixbuf_avatar_from_individual_closure_new (individual, result,
699 width, height, cancellable);
701 g_return_if_fail (closure != NULL);
703 g_loadable_icon_load_async (avatar_icon, width, cancellable,
704 avatar_icon_load_cb, closure);
706 g_object_unref (result);
709 /* Return a ref on the GdkPixbuf */
711 empathy_pixbuf_avatar_from_individual_scaled_finish (
712 FolksIndividual *individual,
713 GAsyncResult *result,
716 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
717 gboolean result_valid;
720 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
721 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
723 if (g_simple_async_result_propagate_error (simple, error))
726 result_valid = g_simple_async_result_is_valid (result,
727 G_OBJECT (individual),
728 empathy_pixbuf_avatar_from_individual_scaled_async);
730 g_return_val_if_fail (result_valid, NULL);
732 pixbuf = g_simple_async_result_get_op_res_gpointer (simple);
733 return pixbuf != NULL ? g_object_ref (pixbuf) : NULL;
737 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
738 gboolean show_protocol)
740 const gchar *icon_name;
742 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
744 icon_name = empathy_icon_name_for_contact (contact);
746 if (icon_name == NULL)
749 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
750 icon_name, show_protocol);
753 static GdkPixbuf * empathy_pixbuf_protocol_from_contact_scaled (
754 EmpathyContact *contact,
759 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
760 const gchar *icon_name,
761 gboolean show_protocol)
763 GdkPixbuf *pix_status;
764 GdkPixbuf *pix_protocol;
765 gchar *icon_filename;
767 gint numerator, denominator;
769 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
770 (show_protocol == FALSE), NULL);
771 g_return_val_if_fail (icon_name != NULL, NULL);
776 icon_filename = empathy_filename_from_icon_name (icon_name,
779 if (icon_filename == NULL)
781 DEBUG ("icon name: %s could not be found\n", icon_name);
785 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
787 if (pix_status == NULL)
789 DEBUG ("Could not open icon %s\n", icon_filename);
790 g_free (icon_filename);
794 g_free (icon_filename);
799 height = gdk_pixbuf_get_height (pix_status);
800 width = gdk_pixbuf_get_width (pix_status);
802 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
803 width * numerator / denominator,
804 height * numerator / denominator);
806 if (pix_protocol == NULL)
809 gdk_pixbuf_composite (pix_protocol, pix_status,
810 0, height - height * numerator / denominator,
811 width * numerator / denominator, height * numerator / denominator,
812 0, height - height * numerator / denominator,
814 GDK_INTERP_BILINEAR, 255);
816 g_object_unref (pix_protocol);
822 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
828 GdkPixbuf *pixbuf = NULL;
830 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
832 account = empathy_contact_get_account (contact);
833 filename = empathy_filename_from_icon_name (
834 tp_account_get_icon_name (account), GTK_ICON_SIZE_MENU);
836 if (filename != NULL)
838 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
846 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf,
852 width = gdk_pixbuf_get_width (pixbuf);
853 height = gdk_pixbuf_get_height (pixbuf);
855 if (width > 0 && (width > max_size || height > max_size))
857 factor = (gdouble) max_size / MAX (width, height);
859 width = width * factor;
860 height = height * factor;
862 return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_HYPER);
865 return g_object_ref (pixbuf);
869 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
874 GError *error = NULL;
879 theme = gtk_icon_theme_get_default ();
881 pixbuf = gtk_icon_theme_load_icon (theme, icon_name, size, 0, &error);
885 DEBUG ("Error loading icon: %s", error->message);
886 g_clear_error (&error);
893 empathy_pixbuf_from_icon_name (const gchar *icon_name,
894 GtkIconSize icon_size)
902 if (gtk_icon_size_lookup (icon_size, &w, &h))
905 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
909 empathy_filename_from_icon_name (const gchar *icon_name,
910 GtkIconSize icon_size)
912 GtkIconTheme *icon_theme;
913 GtkIconInfo *icon_info;
918 icon_theme = gtk_icon_theme_get_default ();
920 if (gtk_icon_size_lookup (icon_size, &w, &h))
923 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
924 if (icon_info == NULL)
927 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
928 gtk_icon_info_free (icon_info);
933 /* Takes care of moving the window to the current workspace. */
935 empathy_window_present_with_time (GtkWindow *window,
938 GdkWindow *gdk_window;
940 g_return_if_fail (GTK_IS_WINDOW (window));
942 /* Move the window to the current workspace before trying to show it.
943 * This is the behaviour people expect when clicking on the statusbar icon. */
944 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
951 /* Has no effect if the WM has viewports, like compiz */
952 gdk_x11_window_move_to_current_desktop (gdk_window);
954 /* If window is still off-screen, hide it to force it to
955 * reposition on the current workspace. */
956 gtk_window_get_position (window, &x, &y);
957 gtk_window_get_size (window, &w, &h);
958 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
959 gtk_widget_hide (GTK_WIDGET (window));
962 if (timestamp == GDK_CURRENT_TIME)
963 gtk_window_present (window);
965 gtk_window_present_with_time (window, timestamp);
969 empathy_window_present (GtkWindow *window)
971 empathy_window_present_with_time (window, gtk_get_current_event_time ());
975 empathy_get_toplevel_window (GtkWidget *widget)
979 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
981 toplevel = gtk_widget_get_toplevel (widget);
982 if (GTK_IS_WINDOW (toplevel) &&
983 gtk_widget_is_toplevel (toplevel))
984 return GTK_WINDOW (toplevel);
989 /** empathy_make_absolute_url_len:
993 * Same as #empathy_make_absolute_url but for a limited string length
996 empathy_make_absolute_url_len (const gchar *url,
999 g_return_val_if_fail (url != NULL, NULL);
1001 if (g_str_has_prefix (url, "help:") ||
1002 g_str_has_prefix (url, "mailto:") ||
1004 return g_strndup (url, len);
1006 if (strstr (url, "@"))
1007 return g_strdup_printf ("mailto:%.*s", len, url);
1009 return g_strdup_printf ("http://%.*s", len, url);
1012 /** empathy_make_absolute_url:
1015 * The URL opening code can't handle schemeless strings, so we try to be
1016 * smart and add http if there is no scheme or doesn't look like a mail
1017 * address. This should work in most cases, and let us click on strings
1018 * like "www.gnome.org".
1020 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1021 * g_free when your are done with it
1024 empathy_make_absolute_url (const gchar *url)
1026 return empathy_make_absolute_url_len (url, strlen (url));
1030 empathy_url_show (GtkWidget *parent,
1034 GError *error = NULL;
1036 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1037 g_return_if_fail (url != NULL);
1039 real_url = empathy_make_absolute_url (url);
1041 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1042 gtk_get_current_event_time (), &error);
1048 dialog = gtk_message_dialog_new (NULL, 0,
1049 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1050 _("Unable to open URI"));
1052 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1053 "%s", error->message);
1055 g_signal_connect (dialog, "response",
1056 G_CALLBACK (gtk_widget_destroy), NULL);
1058 gtk_window_present (GTK_WINDOW (dialog));
1060 g_clear_error (&error);
1067 empathy_send_file (EmpathyContact *contact,
1070 EmpathyFTFactory *factory;
1071 GtkRecentManager *manager;
1074 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1075 g_return_if_fail (G_IS_FILE (file));
1077 factory = empathy_ft_factory_dup_singleton ();
1079 empathy_ft_factory_new_transfer_outgoing (factory, contact, file,
1080 empathy_get_current_action_time ());
1082 uri = g_file_get_uri (file);
1083 manager = gtk_recent_manager_get_default ();
1084 gtk_recent_manager_add_item (manager, uri);
1087 g_object_unref (factory);
1091 empathy_send_file_from_uri_list (EmpathyContact *contact,
1092 const gchar *uri_list)
1097 /* Only handle a single file for now. It would be wicked cool to be
1098 able to do multiple files, offering to zip them or whatever like
1099 nautilus-sendto does. Note that text/uri-list is defined to have
1100 each line terminated by \r\n, but we can be tolerant of applications
1101 that only use \n or don't terminate single-line entries.
1103 nl = strstr (uri_list, "\r\n");
1105 nl = strchr (uri_list, '\n');
1109 gchar *uri = g_strndup (uri_list, nl - uri_list);
1110 file = g_file_new_for_uri (uri);
1115 file = g_file_new_for_uri (uri_list);
1118 empathy_send_file (contact, file);
1120 g_object_unref (file);
1124 file_manager_send_file_response_cb (GtkDialog *widget,
1126 EmpathyContact *contact)
1130 if (response_id == GTK_RESPONSE_OK)
1132 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1134 empathy_send_file (contact, file);
1136 g_object_unref (file);
1139 g_object_unref (contact);
1140 gtk_widget_destroy (GTK_WIDGET (widget));
1144 filter_cb (const GtkFileFilterInfo *filter_info,
1147 /* filter out socket files */
1148 return tp_strdiff (filter_info->mime_type, "inode/socket");
1151 static GtkFileFilter *
1152 create_file_filter (void)
1154 GtkFileFilter *filter;
1156 filter = gtk_file_filter_new ();
1158 gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_MIME_TYPE, filter_cb,
1165 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1170 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1172 DEBUG ("Creating selection file chooser");
1174 widget = gtk_file_chooser_dialog_new (_("Select a file"), NULL,
1175 GTK_FILE_CHOOSER_ACTION_OPEN,
1176 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1180 button = gtk_button_new_with_mnemonic (_("_Send"));
1181 gtk_button_set_image (GTK_BUTTON (button),
1182 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1183 GTK_ICON_SIZE_BUTTON));
1184 gtk_widget_show (button);
1186 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1189 gtk_widget_set_can_default (button, TRUE);
1190 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1193 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1195 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1198 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (widget),
1199 create_file_filter ());
1201 g_signal_connect (widget, "response",
1202 G_CALLBACK (file_manager_send_file_response_cb), g_object_ref (contact));
1204 gtk_widget_show (widget);
1208 file_manager_receive_file_response_cb (GtkDialog *dialog,
1209 GtkResponseType response,
1210 EmpathyFTHandler *handler)
1212 EmpathyFTFactory *factory;
1215 if (response == GTK_RESPONSE_OK)
1219 guint64 free_space, file_size;
1220 GError *error = NULL;
1222 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1223 parent = g_file_get_parent (file);
1224 info = g_file_query_filesystem_info (parent,
1225 G_FILE_ATTRIBUTE_FILESYSTEM_FREE, NULL, &error);
1227 g_object_unref (parent);
1231 g_warning ("Error: %s", error->message);
1233 g_object_unref (file);
1237 free_space = g_file_info_get_attribute_uint64 (info,
1238 G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
1239 file_size = empathy_ft_handler_get_total_bytes (handler);
1241 g_object_unref (info);
1243 if (file_size > free_space)
1245 GtkWidget *message = gtk_message_dialog_new (GTK_WINDOW (dialog),
1246 GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
1248 _("Insufficient free space to save file"));
1249 char *file_size_str, *free_space_str;
1251 file_size_str = g_format_size (file_size);
1252 free_space_str = g_format_size (free_space);
1254 gtk_message_dialog_format_secondary_text (
1255 GTK_MESSAGE_DIALOG (message),
1256 _("%s of free space are required to save this "
1257 "file, but only %s is available. Please "
1258 "choose another location."),
1259 file_size_str, free_space_str);
1261 gtk_dialog_run (GTK_DIALOG (message));
1263 g_free (file_size_str);
1264 g_free (free_space_str);
1265 gtk_widget_destroy (message);
1267 g_object_unref (file);
1272 factory = empathy_ft_factory_dup_singleton ();
1274 empathy_ft_factory_set_destination_for_incoming_handler (
1275 factory, handler, file);
1277 g_object_unref (factory);
1278 g_object_unref (file);
1282 /* unref the handler, as we dismissed the file chooser,
1283 * and refused the transfer.
1285 g_object_unref (handler);
1288 gtk_widget_destroy (GTK_WIDGET (dialog));
1292 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1296 EmpathyContact *contact;
1299 contact = empathy_ft_handler_get_contact (handler);
1300 g_assert (contact != NULL);
1302 title = g_strdup_printf (_("Incoming file from %s"),
1303 empathy_contact_get_alias (contact));
1305 widget = gtk_file_chooser_dialog_new (title,
1306 NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
1307 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1308 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
1311 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1312 empathy_ft_handler_get_filename (handler));
1314 gtk_file_chooser_set_do_overwrite_confirmation
1315 (GTK_FILE_CHOOSER (widget), TRUE);
1317 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1319 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1320 dir = g_get_home_dir ();
1322 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1324 g_signal_connect (widget, "response",
1325 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1327 gtk_widget_show (widget);
1332 empathy_make_color_whiter (GdkRGBA *color)
1334 const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
1336 color->red = (color->red + white.red) / 2;
1337 color->green = (color->green + white.green) / 2;
1338 color->blue = (color->blue + white.blue) / 2;
1342 menu_deactivate_cb (GtkMenu *menu,
1345 /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
1346 g_signal_handlers_disconnect_by_func (menu,
1347 menu_deactivate_cb, user_data);
1349 gtk_menu_detach (menu);
1352 /* Convenient function to create a GtkMenu attached to @attach_to and detach
1353 * it when the menu is not displayed any more. This is useful when creating a
1354 * context menu that we want to get rid as soon as it as been displayed. */
1356 empathy_context_menu_new (GtkWidget *attach_to)
1360 menu = gtk_menu_new ();
1362 gtk_menu_attach_to_widget (GTK_MENU (menu), attach_to, NULL);
1364 /* menu is initially unowned but gtk_menu_attach_to_widget () taked its
1365 * floating ref. We can either wait that @attach_to releases its ref when
1366 * it will be destroyed (when leaving Empathy most of the time) or explicitely
1367 * detach the menu when it's not displayed any more.
1368 * We go for the latter as we don't want to keep useless menus in memory
1369 * during the whole lifetime of Empathy. */
1370 g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb), NULL);
1376 empathy_get_current_action_time (void)
1378 return (tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
1381 /* @words = empathy_live_search_strip_utf8_string (@text);
1383 * User has to pass both so we don't have to compute @words ourself each time
1384 * this function is called. */
1386 empathy_individual_match_string (FolksIndividual *individual,
1393 gboolean retval = FALSE;
1395 /* check alias name */
1396 str = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual));
1398 if (empathy_live_search_match_words (str, words))
1401 personas = folks_individual_get_personas (individual);
1403 /* check contact id, remove the @server.com part */
1404 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1405 while (retval == FALSE && gee_iterator_next (iter))
1407 FolksPersona *persona = gee_iterator_get (iter);
1410 if (empathy_folks_persona_is_interesting (persona))
1412 str = folks_persona_get_display_id (persona);
1414 /* Accept the persona if @text is a full prefix of his ID; that allows
1415 * user to find, say, a jabber contact by typing his JID. */
1416 if (g_str_has_prefix (str, text))
1422 gchar *dup_str = NULL;
1425 p = strstr (str, "@");
1427 str = dup_str = g_strndup (str, p - str);
1429 visible = empathy_live_search_match_words (str, words);
1435 g_clear_object (&persona);
1437 g_clear_object (&iter);
1439 /* FIXME: Add more rules here, we could check phone numbers in
1440 * contact's vCard for example. */
1445 empathy_launch_program (const gchar *dir,
1449 GdkDisplay *display;
1450 GError *error = NULL;
1453 GdkAppLaunchContext *context = NULL;
1455 /* Try to run from source directory if possible */
1456 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1459 if (!g_file_test (path, G_FILE_TEST_EXISTS))
1462 path = g_build_filename (dir, name, NULL);
1466 cmd = g_strconcat (path, " ", args, NULL);
1468 cmd = g_strdup (path);
1470 app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
1471 if (app_info == NULL)
1473 DEBUG ("Failed to create app info: %s", error->message);
1474 g_error_free (error);
1478 display = gdk_display_get_default ();
1479 context = gdk_display_get_app_launch_context (display);
1481 if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
1484 g_warning ("Failed to launch %s: %s", name, error->message);
1485 g_error_free (error);
1490 tp_clear_object (&app_info);
1491 tp_clear_object (&context);
1496 /* Most of the workspace manipulation code has been copied from libwnck
1497 * Copyright (C) 2001 Havoc Pennington
1498 * Copyright (C) 2005-2007 Vincent Untz
1501 _wnck_activate_workspace (Screen *screen,
1502 int new_active_space,
1509 display = DisplayOfScreen (screen);
1510 root = RootWindowOfScreen (screen);
1512 xev.xclient.type = ClientMessage;
1513 xev.xclient.serial = 0;
1514 xev.xclient.send_event = True;
1515 xev.xclient.display = display;
1516 xev.xclient.window = root;
1517 xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_CURRENT_DESKTOP");
1518 xev.xclient.format = 32;
1519 xev.xclient.data.l[0] = new_active_space;
1520 xev.xclient.data.l[1] = timestamp;
1521 xev.xclient.data.l[2] = 0;
1522 xev.xclient.data.l[3] = 0;
1523 xev.xclient.data.l[4] = 0;
1525 gdk_error_trap_push ();
1526 XSendEvent (display, root, False,
1527 SubstructureRedirectMask | SubstructureNotifyMask,
1529 XSync (display, False);
1530 gdk_error_trap_pop_ignored ();
1534 _wnck_get_cardinal (Screen *screen,
1547 display = DisplayOfScreen (screen);
1551 gdk_error_trap_push ();
1553 result = XGetWindowProperty (display, xwindow, atom,
1554 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems,
1555 &bytes_after, (void *) &num);
1556 err = gdk_error_trap_pop ();
1557 if (err != Success ||
1561 if (type != XA_CARDINAL)
1575 window_get_workspace (Screen *xscreen,
1580 if (!_wnck_get_cardinal (xscreen, win,
1581 gdk_x11_get_xatom_by_name ("_NET_WM_DESKTOP"), &number))
1587 /* Ask X to move to the desktop on which @window currently is
1588 * and the present @window. */
1590 empathy_move_to_window_desktop (GtkWindow *window,
1595 GdkWindow *gdk_window;
1598 screen = gtk_window_get_screen (window);
1599 xscreen = gdk_x11_screen_get_xscreen (screen);
1600 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1602 workspace = window_get_workspace (xscreen,
1603 gdk_x11_window_get_xid (gdk_window));
1604 if (workspace == -1)
1607 _wnck_activate_workspace (xscreen, workspace, timestamp);
1610 gtk_window_present_with_time (window, timestamp);
1614 empathy_set_css_provider (GtkWidget *widget)
1616 GtkCssProvider *provider;
1618 GError *error = NULL;
1621 filename = empathy_file_lookup ("empathy.css", "data");
1623 provider = gtk_css_provider_new ();
1625 if (!gtk_css_provider_load_from_path (provider, filename, &error))
1627 g_warning ("Failed to load css file '%s': %s", filename, error->message);
1628 g_error_free (error);
1633 screen = gtk_widget_get_screen (widget);
1635 screen = gdk_screen_get_default ();
1637 gtk_style_context_add_provider_for_screen (screen,
1638 GTK_STYLE_PROVIDER (provider),
1639 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
1643 g_object_unref (provider);
1647 launch_app_info (GAppInfo *app_info,
1650 GdkAppLaunchContext *context = NULL;
1651 GdkDisplay *display;
1654 display = gdk_display_get_default ();
1655 context = gdk_display_get_app_launch_context (display);
1657 if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
1660 DEBUG ("Failed to launch %s: %s",
1661 g_app_info_get_display_name (app_info), err->message);
1662 g_propagate_error (error, err);
1666 tp_clear_object (&context);
1671 empathy_launch_external_app (const gchar *desktop_file,
1675 GDesktopAppInfo *desktop_info;
1679 desktop_info = g_desktop_app_info_new (desktop_file);
1680 if (desktop_info == NULL)
1682 DEBUG ("%s not found", desktop_file);
1684 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1685 "%s not found", desktop_file);
1691 result = launch_app_info (G_APP_INFO (desktop_info), error);
1698 /* glib doesn't have API to start a desktop file with args... (#637875) */
1699 cmd = g_strdup_printf ("%s %s", g_app_info_get_commandline (
1700 (GAppInfo *) desktop_info), args);
1702 app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &err);
1703 if (app_info == NULL)
1705 DEBUG ("Failed to launch '%s': %s", cmd, err->message);
1707 g_object_unref (desktop_info);
1708 g_propagate_error (error, err);
1712 result = launch_app_info (app_info, error);
1714 g_object_unref (app_info);
1718 g_object_unref (desktop_info);