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-2010 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>
26 * Travis Reitter <travis.reitter@collabora.co.uk>
28 * Part of this file is copied from GtkSourceView (gtksourceiter.c):
36 #include <X11/Xatom.h>
38 #include <glib/gi18n-lib.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;
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) {
71 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "data",
72 "icons", "local-copy", NULL);
73 if (g_file_test (path, G_FILE_TEST_EXISTS)) {
74 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), path);
84 builder_get_file_valist (const gchar *filename,
85 const gchar *first_object,
93 DEBUG ("Loading file %s", filename);
95 gui = gtk_builder_new ();
96 gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
97 if (!gtk_builder_add_from_file (gui, filename, &error)) {
98 g_critical ("GtkBuilder Error (%s): %s",
99 filename, error->message);
100 g_clear_error (&error);
101 g_object_unref (gui);
103 /* we need to iterate and set all of the pointers to NULL */
104 for (name = first_object; name;
105 name = va_arg (args, const gchar *)) {
106 object_ptr = va_arg (args, GObject**);
114 for (name = first_object; name; name = va_arg (args, const gchar *)) {
115 object_ptr = va_arg (args, GObject**);
117 *object_ptr = gtk_builder_get_object (gui, name);
120 g_warning ("File is missing object '%s'.", name);
129 empathy_builder_get_file (const gchar *filename,
130 const gchar *first_object,
136 va_start (args, first_object);
137 gui = builder_get_file_valist (filename, first_object, args);
144 empathy_builder_connect (GtkBuilder *gui,
146 const gchar *first_object,
155 va_start (args, first_object);
156 for (name = first_object; name; name = va_arg (args, const gchar *)) {
157 sig = va_arg (args, const gchar *);
158 callback = va_arg (args, GCallback);
160 object = gtk_builder_get_object (gui, name);
162 g_warning ("File is missing object '%s'.", name);
166 g_signal_connect (object, sig, callback, user_data);
173 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
176 /* On construction gui sinks the initial reference to widget. When gui
177 * is finalized it will drop its ref to widget. We take our own ref to
178 * prevent widget being finalised. The widget is forced to have a
179 * floating reference, like when it was initially unowned so that it can
180 * be used like any other GtkWidget. */
182 g_object_ref (widget);
183 g_object_force_floating (G_OBJECT (widget));
184 g_object_unref (gui);
190 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
193 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
194 return EMPATHY_IMAGE_AVAILABLE;
195 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
196 return EMPATHY_IMAGE_BUSY;
197 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
198 return EMPATHY_IMAGE_AWAY;
199 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
200 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
201 EMPATHY_IMAGE_EXT_AWAY))
202 return EMPATHY_IMAGE_EXT_AWAY;
204 /* The 'extended-away' icon is not an official one so we fallback to idle if
205 * it's not implemented */
206 return EMPATHY_IMAGE_IDLE;
207 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
208 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
209 EMPATHY_IMAGE_HIDDEN))
210 return EMPATHY_IMAGE_HIDDEN;
212 /* The 'hidden' icon is not an official one so we fallback to offline if
213 * it's not implemented */
214 return EMPATHY_IMAGE_OFFLINE;
215 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
216 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
217 return EMPATHY_IMAGE_OFFLINE;
218 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
219 return EMPATHY_IMAGE_PENDING;
220 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
229 empathy_icon_name_for_contact (EmpathyContact *contact)
231 TpConnectionPresenceType presence;
233 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
234 EMPATHY_IMAGE_OFFLINE);
236 presence = empathy_contact_get_presence (contact);
237 return empathy_icon_name_for_presence (presence);
241 empathy_icon_name_for_individual (FolksIndividual *individual)
243 FolksPresenceType folks_presence;
244 TpConnectionPresenceType presence;
247 folks_presence_details_get_presence_type (
248 FOLKS_PRESENCE_DETAILS (individual));
249 presence = empathy_folks_presence_type_to_tp (folks_presence);
251 return empathy_icon_name_for_presence (presence);
255 empathy_protocol_name_for_contact (EmpathyContact *contact)
259 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
261 account = empathy_contact_get_account (contact);
262 if (account == NULL) {
266 return tp_account_get_icon_name (account);
270 empathy_pixbuf_from_data (gchar *data,
273 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
277 empathy_pixbuf_from_data_and_mime (gchar *data,
281 GdkPixbufLoader *loader;
282 GdkPixbufFormat *format;
283 GdkPixbuf *pixbuf = NULL;
285 GError *error = NULL;
291 loader = gdk_pixbuf_loader_new ();
292 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
293 DEBUG ("Failed to write to pixbuf loader: %s",
294 error ? error->message : "No error given");
297 if (!gdk_pixbuf_loader_close (loader, &error)) {
298 DEBUG ("Failed to close pixbuf loader: %s",
299 error ? error->message : "No error given");
303 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
305 g_object_ref (pixbuf);
307 if (mime_type != NULL) {
308 format = gdk_pixbuf_loader_get_format (loader);
309 mime_types = gdk_pixbuf_format_get_mime_types (format);
311 *mime_type = g_strdup (*mime_types);
312 if (mime_types[1] != NULL) {
313 DEBUG ("Loader supports more than one mime "
314 "type! Picking the first one, %s",
317 g_strfreev (mime_types);
322 g_clear_error (&error);
323 g_object_unref (loader);
331 gboolean preserve_aspect_ratio;
335 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
338 struct SizeData *data)
340 g_return_if_fail (width > 0 && height > 0);
342 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
343 if (width < data->width && height < data->height) {
348 if (data->width < 0) {
349 width = width * (double) data->height / (gdouble) height;
350 height = data->height;
351 } else if (data->height < 0) {
352 height = height * (double) data->width / (double) width;
354 } else if ((double) height * (double) data->width >
355 (double) width * (double) data->height) {
356 width = 0.5 + (double) width * (double) data->height / (double) height;
357 height = data->height;
359 height = 0.5 + (double) height * (double) data->width / (double) width;
363 if (data->width > 0) {
367 if (data->height > 0) {
368 height = data->height;
372 gdk_pixbuf_loader_set_size (loader, width, height);
376 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
378 gint width, height, rowstride;
381 width = gdk_pixbuf_get_width (pixbuf);
382 height = gdk_pixbuf_get_height (pixbuf);
383 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
384 pixels = gdk_pixbuf_get_pixels (pixbuf);
386 if (width < 6 || height < 6) {
394 pixels[rowstride + 3] = 0x80;
395 pixels[rowstride * 2 + 3] = 0xC0;
398 pixels[width * 4 - 1] = 0;
399 pixels[width * 4 - 5] = 0x80;
400 pixels[width * 4 - 9] = 0xC0;
401 pixels[rowstride + (width * 4) - 1] = 0x80;
402 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
405 pixels[(height - 1) * rowstride + 3] = 0;
406 pixels[(height - 1) * rowstride + 7] = 0x80;
407 pixels[(height - 1) * rowstride + 11] = 0xC0;
408 pixels[(height - 2) * rowstride + 3] = 0x80;
409 pixels[(height - 3) * rowstride + 3] = 0xC0;
412 pixels[height * rowstride - 1] = 0;
413 pixels[(height - 1) * rowstride - 1] = 0x80;
414 pixels[(height - 2) * rowstride - 1] = 0xC0;
415 pixels[height * rowstride - 5] = 0x80;
416 pixels[height * rowstride - 9] = 0xC0;
420 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
422 gint height, rowstride, i;
426 height = gdk_pixbuf_get_height (pixbuf);
427 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
428 pixels = gdk_pixbuf_get_pixels (pixbuf);
431 for (i = 3; i < rowstride; i+=4) {
437 for (i = 1; i < height - 1; i++) {
438 row = pixels + (i*rowstride);
439 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
444 row = pixels + ((height-1) * rowstride);
445 for (i = 3; i < rowstride; i+=4) {
455 avatar_pixbuf_from_loader (GdkPixbufLoader *loader)
459 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
460 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
461 GdkPixbuf *rounded_pixbuf;
463 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
464 gdk_pixbuf_get_width (pixbuf),
465 gdk_pixbuf_get_height (pixbuf));
466 gdk_pixbuf_copy_area (pixbuf, 0, 0,
467 gdk_pixbuf_get_width (pixbuf),
468 gdk_pixbuf_get_height (pixbuf),
471 pixbuf = rounded_pixbuf;
473 g_object_ref (pixbuf);
476 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
477 empathy_avatar_pixbuf_roundify (pixbuf);
484 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
489 GdkPixbufLoader *loader;
490 struct SizeData data;
491 GError *error = NULL;
498 data.height = height;
499 data.preserve_aspect_ratio = TRUE;
501 loader = gdk_pixbuf_loader_new ();
503 g_signal_connect (loader, "size-prepared",
504 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
507 if (avatar->len == 0) {
508 g_warning ("Avatar has 0 length");
510 } else if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
511 g_warning ("Couldn't write avatar image:%p with "
512 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
513 avatar->data, avatar->len, error->message);
514 g_error_free (error);
518 gdk_pixbuf_loader_close (loader, NULL);
519 pixbuf = avatar_pixbuf_from_loader (loader);
521 g_object_unref (loader);
527 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
531 EmpathyAvatar *avatar;
533 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
535 avatar = empathy_contact_get_avatar (contact);
537 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
541 FolksIndividual *individual;
542 GSimpleAsyncResult *result;
545 struct SizeData size_data;
546 GdkPixbufLoader *loader;
547 GCancellable *cancellable;
549 } PixbufAvatarFromIndividualClosure;
551 static PixbufAvatarFromIndividualClosure *
552 pixbuf_avatar_from_individual_closure_new (FolksIndividual *individual,
553 GSimpleAsyncResult *result,
556 GCancellable *cancellable)
558 PixbufAvatarFromIndividualClosure *closure;
560 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
561 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
563 closure = g_new0 (PixbufAvatarFromIndividualClosure, 1);
564 closure->individual = g_object_ref (individual);
565 closure->result = g_object_ref (result);
566 closure->width = width;
567 closure->height = height;
568 if (cancellable != NULL)
569 closure->cancellable = g_object_ref (cancellable);
575 pixbuf_avatar_from_individual_closure_free (
576 PixbufAvatarFromIndividualClosure *closure)
578 g_clear_object (&closure->cancellable);
579 tp_clear_object (&closure->loader);
580 g_object_unref (closure->individual);
581 g_object_unref (closure->result);
586 avatar_icon_load_close_cb (GObject *object,
587 GAsyncResult *result,
590 GError *error = NULL;
592 g_input_stream_close_finish (G_INPUT_STREAM (object), result, &error);
595 DEBUG ("Failed to close pixbuf stream: %s", error->message);
596 g_error_free (error);
601 avatar_icon_load_read_cb (GObject *object,
602 GAsyncResult *result,
605 GInputStream *stream = G_INPUT_STREAM (object);
606 PixbufAvatarFromIndividualClosure *closure = user_data;
608 GError *error = NULL;
610 /* Finish reading this chunk from the stream */
611 n_read = g_input_stream_read_finish (stream, result, &error);
613 DEBUG ("Failed to finish read from pixbuf stream: %s",
615 g_simple_async_result_set_from_error (closure->result, error);
619 /* Write the chunk to the pixbuf loader */
620 if (!gdk_pixbuf_loader_write (closure->loader, (guchar *) closure->data,
622 DEBUG ("Failed to write to pixbuf loader: %s",
623 error ? error->message : "No error given");
624 g_simple_async_result_set_from_error (closure->result, error);
630 if (!gdk_pixbuf_loader_close (closure->loader, &error)) {
631 DEBUG ("Failed to close pixbuf loader: %s",
632 error ? error->message : "No error given");
633 g_simple_async_result_set_from_error (closure->result, error);
638 g_simple_async_result_set_op_res_gpointer (closure->result,
639 avatar_pixbuf_from_loader (closure->loader),
644 /* Loop round and read another chunk. */
645 g_input_stream_read_async (stream, closure->data,
646 G_N_ELEMENTS (closure->data),
647 G_PRIORITY_DEFAULT, closure->cancellable,
648 avatar_icon_load_read_cb, closure);
654 /* We must close the pixbuf loader before unreffing it. */
655 gdk_pixbuf_loader_close (closure->loader, NULL);
658 /* Close the file for safety (even though it should be
659 * automatically closed when the stream is finalised). */
660 g_input_stream_close_async (stream, G_PRIORITY_DEFAULT, NULL,
661 (GAsyncReadyCallback) avatar_icon_load_close_cb, NULL);
663 g_simple_async_result_complete (closure->result);
665 g_clear_error (&error);
666 pixbuf_avatar_from_individual_closure_free (closure);
670 avatar_icon_load_cb (GObject *object,
671 GAsyncResult *result,
674 GLoadableIcon *icon = G_LOADABLE_ICON (object);
675 PixbufAvatarFromIndividualClosure *closure = user_data;
676 GInputStream *stream;
677 GError *error = NULL;
679 stream = g_loadable_icon_load_finish (icon, result, NULL, &error);
681 DEBUG ("Failed to open avatar stream: %s", error->message);
682 g_simple_async_result_set_from_error (closure->result, error);
686 closure->size_data.width = closure->width;
687 closure->size_data.height = closure->height;
688 closure->size_data.preserve_aspect_ratio = TRUE;
690 /* Load the data into a pixbuf loader in chunks. */
691 closure->loader = gdk_pixbuf_loader_new ();
693 g_signal_connect (closure->loader, "size-prepared",
694 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
695 &(closure->size_data));
697 /* Begin to read the first chunk. */
698 g_input_stream_read_async (stream, closure->data,
699 G_N_ELEMENTS (closure->data),
700 G_PRIORITY_DEFAULT, closure->cancellable,
701 avatar_icon_load_read_cb, closure);
703 g_object_unref (stream);
708 g_simple_async_result_complete (closure->result);
710 g_clear_error (&error);
711 tp_clear_object (&stream);
712 pixbuf_avatar_from_individual_closure_free (closure);
716 empathy_pixbuf_avatar_from_individual_scaled_async (
717 FolksIndividual *individual,
720 GCancellable *cancellable,
721 GAsyncReadyCallback callback,
724 GLoadableIcon *avatar_icon;
725 GSimpleAsyncResult *result;
726 PixbufAvatarFromIndividualClosure *closure;
728 result = g_simple_async_result_new (G_OBJECT (individual),
730 empathy_pixbuf_avatar_from_individual_scaled_async);
733 folks_avatar_details_get_avatar (FOLKS_AVATAR_DETAILS (individual));
734 if (avatar_icon == NULL) {
735 g_simple_async_result_set_error (result, G_IO_ERROR,
736 G_IO_ERROR_NOT_FOUND, "no avatar found");
738 g_simple_async_result_complete (result);
739 g_object_unref (result);
743 closure = pixbuf_avatar_from_individual_closure_new (individual, result,
747 g_return_if_fail (closure != NULL);
749 g_loadable_icon_load_async (avatar_icon, width, cancellable,
750 avatar_icon_load_cb, closure);
752 g_object_unref (result);
755 /* Return a ref on the GdkPixbuf */
757 empathy_pixbuf_avatar_from_individual_scaled_finish (
758 FolksIndividual *individual,
759 GAsyncResult *result,
762 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
763 gboolean result_valid;
766 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
767 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
769 if (g_simple_async_result_propagate_error (simple, error))
772 result_valid = g_simple_async_result_is_valid (result,
773 G_OBJECT (individual),
774 empathy_pixbuf_avatar_from_individual_scaled_async);
775 g_return_val_if_fail (result_valid, NULL);
777 pixbuf = g_simple_async_result_get_op_res_gpointer (simple);
778 return pixbuf != NULL ? g_object_ref (pixbuf) : NULL;
782 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
783 gboolean show_protocol)
785 const gchar *icon_name;
787 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
789 icon_name = empathy_icon_name_for_contact (contact);
791 if (icon_name == NULL) {
794 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
800 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
801 const gchar *icon_name,
802 gboolean show_protocol)
804 GdkPixbuf *pix_status;
805 GdkPixbuf *pix_protocol;
806 gchar *icon_filename;
808 gint numerator, denominator;
810 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
811 (show_protocol == FALSE), NULL);
812 g_return_val_if_fail (icon_name != NULL, NULL);
817 icon_filename = empathy_filename_from_icon_name (icon_name,
819 if (icon_filename == NULL) {
820 DEBUG ("icon name: %s could not be found\n", icon_name);
824 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
826 if (pix_status == NULL) {
827 DEBUG ("Could not open icon %s\n", icon_filename);
828 g_free (icon_filename);
832 g_free (icon_filename);
837 height = gdk_pixbuf_get_height (pix_status);
838 width = gdk_pixbuf_get_width (pix_status);
840 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
841 width * numerator / denominator,
842 height * numerator / denominator);
844 if (pix_protocol == NULL) {
847 gdk_pixbuf_composite (pix_protocol, pix_status,
848 0, height - height * numerator / denominator,
849 width * numerator / denominator, height * numerator / denominator,
850 0, height - height * numerator / denominator,
852 GDK_INTERP_BILINEAR, 255);
854 g_object_unref (pix_protocol);
860 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
866 GdkPixbuf *pixbuf = NULL;
868 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
870 account = empathy_contact_get_account (contact);
871 filename = empathy_filename_from_icon_name (tp_account_get_icon_name (account),
873 if (filename != NULL) {
874 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
882 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
887 width = gdk_pixbuf_get_width (pixbuf);
888 height = gdk_pixbuf_get_height (pixbuf);
890 if (width > 0 && (width > max_size || height > max_size)) {
891 factor = (gdouble) max_size / MAX (width, height);
893 width = width * factor;
894 height = height * factor;
896 return gdk_pixbuf_scale_simple (pixbuf,
901 return g_object_ref (pixbuf);
905 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
910 GError *error = NULL;
916 theme = gtk_icon_theme_get_default ();
918 pixbuf = gtk_icon_theme_load_icon (theme,
924 DEBUG ("Error loading icon: %s", error->message);
925 g_clear_error (&error);
932 empathy_pixbuf_from_icon_name (const gchar *icon_name,
933 GtkIconSize icon_size)
942 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
946 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
950 empathy_filename_from_icon_name (const gchar *icon_name,
951 GtkIconSize icon_size)
953 GtkIconTheme *icon_theme;
954 GtkIconInfo *icon_info;
959 icon_theme = gtk_icon_theme_get_default ();
961 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
965 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
966 if (icon_info == NULL)
969 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
970 gtk_icon_info_free (icon_info);
975 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
976 * that to make it easier to apply changes from the original code.
978 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
980 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
981 * decomposable character it consumes the decomposition length from the given
982 * offset. So it's useful when the offset was calculated for the normalized
983 * version of str, but we need a pointer to str itself. */
985 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
987 gchar *casefold, *normal;
993 q = g_utf8_next_char (p);
994 casefold = g_utf8_casefold (p, q - p);
995 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
996 offset -= g_utf8_strlen (normal, -1);
1004 static const gchar *
1005 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
1009 const gchar *ret = NULL;
1012 gchar *caseless_haystack;
1015 g_return_val_if_fail (haystack != NULL, NULL);
1016 g_return_val_if_fail (needle != NULL, NULL);
1018 casefold = g_utf8_casefold (haystack, -1);
1019 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1022 needle_len = g_utf8_strlen (needle, -1);
1023 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1025 if (needle_len == 0)
1027 ret = (gchar *) haystack;
1031 if (haystack_len < needle_len)
1037 p = (gchar *) caseless_haystack;
1038 needle_len = strlen (needle);
1043 if ((strncmp (p, needle, needle_len) == 0))
1045 ret = pointer_from_offset_skipping_decomp (haystack, i);
1049 p = g_utf8_next_char (p);
1054 g_free (caseless_haystack);
1060 g_utf8_caselessnmatch (const char *s1, const char *s2,
1061 gssize n1, gssize n2)
1064 gchar *normalized_s1;
1065 gchar *normalized_s2;
1068 gboolean ret = FALSE;
1070 g_return_val_if_fail (s1 != NULL, FALSE);
1071 g_return_val_if_fail (s2 != NULL, FALSE);
1072 g_return_val_if_fail (n1 > 0, FALSE);
1073 g_return_val_if_fail (n2 > 0, FALSE);
1075 casefold = g_utf8_casefold (s1, n1);
1076 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1079 casefold = g_utf8_casefold (s2, n2);
1080 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1083 len_s1 = strlen (normalized_s1);
1084 len_s2 = strlen (normalized_s2);
1086 if (len_s1 < len_s2)
1089 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
1092 g_free (normalized_s1);
1093 g_free (normalized_s2);
1099 forward_chars_with_skipping (GtkTextIter *iter,
1101 gboolean skip_invisible,
1102 gboolean skip_nontext,
1103 gboolean skip_decomp)
1107 g_return_if_fail (count >= 0);
1113 gboolean ignored = FALSE;
1115 /* minimal workaround to avoid the infinite loop of bug #168247.
1116 * It doesn't fix the problemjust the symptom...
1118 if (gtk_text_iter_is_end (iter))
1121 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
1124 if (!ignored && skip_invisible &&
1125 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
1128 if (!ignored && skip_decomp)
1130 /* being UTF8 correct sucks; this accounts for extra
1131 offsets coming from canonical decompositions of
1132 UTF8 characters (e.g. accented characters) which
1133 g_utf8_normalize () performs */
1138 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
1139 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
1140 i -= (g_utf8_strlen (normal, -1) - 1);
1144 gtk_text_iter_forward_char (iter);
1152 lines_match (const GtkTextIter *start,
1153 const gchar **lines,
1154 gboolean visible_only,
1156 GtkTextIter *match_start,
1157 GtkTextIter *match_end)
1164 if (*lines == NULL || **lines == '\0')
1167 *match_start = *start;
1169 *match_end = *start;
1174 gtk_text_iter_forward_line (&next);
1176 /* No more text in buffer, but *lines is nonempty */
1177 if (gtk_text_iter_equal (start, &next))
1183 line_text = gtk_text_iter_get_visible_slice (start, &next);
1185 line_text = gtk_text_iter_get_slice (start, &next);
1190 line_text = gtk_text_iter_get_visible_text (start, &next);
1192 line_text = gtk_text_iter_get_text (start, &next);
1195 if (match_start) /* if this is the first line we're matching */
1197 found = g_utf8_strcasestr (line_text, *lines);
1201 /* If it's not the first line, we have to match from the
1202 * start of the line.
1204 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1217 /* Get offset to start of search string */
1218 offset = g_utf8_strlen (line_text, found - line_text);
1222 /* If match start needs to be returned, set it to the
1223 * start of the search string.
1225 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1228 *match_start = next;
1231 /* Go to end of search string */
1232 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1241 /* pass NULL for match_start, since we don't need to find the
1244 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
1247 /* strsplit () that retains the delimiter as part of the string. */
1249 strbreakup (const char *string,
1250 const char *delimiter,
1253 GSList *string_list = NULL, *slist;
1254 gchar **str_array, *s, *casefold, *new_string;
1257 g_return_val_if_fail (string != NULL, NULL);
1258 g_return_val_if_fail (delimiter != NULL, NULL);
1261 max_tokens = G_MAXINT;
1263 s = strstr (string, delimiter);
1266 guint delimiter_len = strlen (delimiter);
1272 len = s - string + delimiter_len;
1273 new_string = g_new (gchar, len + 1);
1274 strncpy (new_string, string, len);
1275 new_string[len] = 0;
1276 casefold = g_utf8_casefold (new_string, -1);
1277 g_free (new_string);
1278 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1280 string_list = g_slist_prepend (string_list, new_string);
1282 string = s + delimiter_len;
1283 s = strstr (string, delimiter);
1284 } while (--max_tokens && s);
1290 casefold = g_utf8_casefold (string, -1);
1291 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1293 string_list = g_slist_prepend (string_list, new_string);
1296 str_array = g_new (gchar*, n);
1300 str_array[i--] = NULL;
1301 for (slist = string_list; slist; slist = slist->next)
1302 str_array[i--] = slist->data;
1304 g_slist_free (string_list);
1310 empathy_text_iter_forward_search (const GtkTextIter *iter,
1312 GtkTextIter *match_start,
1313 GtkTextIter *match_end,
1314 const GtkTextIter *limit)
1316 gchar **lines = NULL;
1318 gboolean retval = FALSE;
1320 gboolean visible_only;
1323 g_return_val_if_fail (iter != NULL, FALSE);
1324 g_return_val_if_fail (str != NULL, FALSE);
1326 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
1330 /* If we can move one char, return the empty string there */
1333 if (gtk_text_iter_forward_char (&match)) {
1334 if (limit && gtk_text_iter_equal (&match, limit)) {
1339 *match_start = match;
1350 visible_only = TRUE;
1353 /* locate all lines */
1354 lines = strbreakup (str, "\n", -1);
1359 /* This loop has an inefficient worst-case, where
1360 * gtk_text_iter_get_text () is called repeatedly on
1365 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1369 if (lines_match (&search, (const gchar**)lines,
1370 visible_only, slice, &match, &end)) {
1371 if (limit == NULL ||
1372 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1376 *match_start = match;
1384 } while (gtk_text_iter_forward_line (&search));
1386 g_strfreev ((gchar **) lines);
1391 static const gchar *
1392 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1396 const gchar *ret = NULL;
1399 gchar *caseless_haystack;
1402 g_return_val_if_fail (haystack != NULL, NULL);
1403 g_return_val_if_fail (needle != NULL, NULL);
1405 casefold = g_utf8_casefold (haystack, -1);
1406 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1409 needle_len = g_utf8_strlen (needle, -1);
1410 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1412 if (needle_len == 0)
1414 ret = (gchar *) haystack;
1418 if (haystack_len < needle_len)
1424 i = haystack_len - needle_len;
1425 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1426 needle_len = strlen (needle);
1428 while (p >= caseless_haystack)
1430 if (strncmp (p, needle, needle_len) == 0)
1432 ret = pointer_from_offset_skipping_decomp (haystack, i);
1436 p = g_utf8_prev_char (p);
1441 g_free (caseless_haystack);
1447 backward_lines_match (const GtkTextIter *start,
1448 const gchar **lines,
1449 gboolean visible_only,
1451 GtkTextIter *match_start,
1452 GtkTextIter *match_end)
1454 GtkTextIter line, next;
1459 if (*lines == NULL || **lines == '\0')
1462 *match_start = *start;
1464 *match_end = *start;
1468 line = next = *start;
1469 if (gtk_text_iter_get_line_offset (&next) == 0)
1471 if (!gtk_text_iter_backward_line (&next))
1475 gtk_text_iter_set_line_offset (&next, 0);
1480 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1482 line_text = gtk_text_iter_get_slice (&next, &line);
1487 line_text = gtk_text_iter_get_visible_text (&next, &line);
1489 line_text = gtk_text_iter_get_text (&next, &line);
1492 if (match_start) /* if this is the first line we're matching */
1494 found = g_utf8_strrcasestr (line_text, *lines);
1498 /* If it's not the first line, we have to match from the
1499 * start of the line.
1501 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1514 /* Get offset to start of search string */
1515 offset = g_utf8_strlen (line_text, found - line_text);
1517 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1519 /* If match start needs to be returned, set it to the
1520 * start of the search string.
1524 *match_start = next;
1527 /* Go to end of search string */
1528 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1537 /* try to match the rest of the lines forward, passing NULL
1538 * for match_start so lines_match will try to match the entire
1540 return lines_match (&next, lines, visible_only,
1541 slice, NULL, match_end);
1545 empathy_text_iter_backward_search (const GtkTextIter *iter,
1547 GtkTextIter *match_start,
1548 GtkTextIter *match_end,
1549 const GtkTextIter *limit)
1551 gchar **lines = NULL;
1553 gboolean retval = FALSE;
1555 gboolean visible_only;
1558 g_return_val_if_fail (iter != NULL, FALSE);
1559 g_return_val_if_fail (str != NULL, FALSE);
1561 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1566 /* If we can move one char, return the empty string there */
1569 if (gtk_text_iter_backward_char (&match))
1571 if (limit && gtk_text_iter_equal (&match, limit))
1575 *match_start = match;
1586 visible_only = TRUE;
1589 /* locate all lines */
1590 lines = strbreakup (str, "\n", -1);
1596 /* This loop has an inefficient worst-case, where
1597 * gtk_text_iter_get_text () is called repeatedly on
1602 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1605 if (backward_lines_match (&search, (const gchar**)lines,
1606 visible_only, slice, &match, &end))
1608 if (limit == NULL || (limit &&
1609 gtk_text_iter_compare (&end, limit) > 0))
1614 *match_start = match;
1621 if (gtk_text_iter_get_line_offset (&search) == 0)
1623 if (!gtk_text_iter_backward_line (&search))
1628 gtk_text_iter_set_line_offset (&search, 0);
1632 g_strfreev ((gchar **) lines);
1637 /* Takes care of moving the window to the current workspace. */
1639 empathy_window_present_with_time (GtkWindow *window,
1642 GdkWindow *gdk_window;
1644 g_return_if_fail (GTK_IS_WINDOW (window));
1646 /* Move the window to the current workspace before trying to show it.
1647 * This is the behaviour people expect when clicking on the statusbar icon. */
1648 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1653 /* Has no effect if the WM has viewports, like compiz */
1654 gdk_x11_window_move_to_current_desktop (gdk_window);
1656 /* If window is still off-screen, hide it to force it to
1657 * reposition on the current workspace. */
1658 gtk_window_get_position (window, &x, &y);
1659 gtk_window_get_size (window, &w, &h);
1660 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1661 gtk_widget_hide (GTK_WIDGET (window));
1664 if (timestamp == GDK_CURRENT_TIME)
1665 gtk_window_present (window);
1667 gtk_window_present_with_time (window, timestamp);
1671 empathy_window_present (GtkWindow *window)
1673 empathy_window_present_with_time (window, gtk_get_current_event_time ());
1677 empathy_get_toplevel_window (GtkWidget *widget)
1679 GtkWidget *toplevel;
1681 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1683 toplevel = gtk_widget_get_toplevel (widget);
1684 if (GTK_IS_WINDOW (toplevel) &&
1685 gtk_widget_is_toplevel (toplevel)) {
1686 return GTK_WINDOW (toplevel);
1692 /** empathy_make_absolute_url_len:
1696 * Same as #empathy_make_absolute_url but for a limited string length
1699 empathy_make_absolute_url_len (const gchar *url,
1702 g_return_val_if_fail (url != NULL, NULL);
1704 if (g_str_has_prefix (url, "help:") ||
1705 g_str_has_prefix (url, "mailto:") ||
1706 strstr (url, ":/")) {
1707 return g_strndup (url, len);
1710 if (strstr (url, "@")) {
1711 return g_strdup_printf ("mailto:%.*s", len, url);
1714 return g_strdup_printf ("http://%.*s", len, url);
1717 /** empathy_make_absolute_url:
1720 * The URL opening code can't handle schemeless strings, so we try to be
1721 * smart and add http if there is no scheme or doesn't look like a mail
1722 * address. This should work in most cases, and let us click on strings
1723 * like "www.gnome.org".
1725 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1726 * g_free when your are done with it
1729 empathy_make_absolute_url (const gchar *url)
1731 return empathy_make_absolute_url_len (url, strlen (url));
1735 empathy_url_show (GtkWidget *parent,
1739 GError *error = NULL;
1741 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1742 g_return_if_fail (url != NULL);
1744 real_url = empathy_make_absolute_url (url);
1746 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1747 gtk_get_current_event_time (), &error);
1752 dialog = gtk_message_dialog_new (NULL, 0,
1753 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1754 _("Unable to open URI"));
1755 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1756 "%s", error->message);
1758 g_signal_connect (dialog, "response",
1759 G_CALLBACK (gtk_widget_destroy),
1761 gtk_window_present (GTK_WINDOW (dialog));
1763 g_clear_error (&error);
1770 empathy_send_file (EmpathyContact *contact, GFile *file)
1772 EmpathyFTFactory *factory;
1773 GtkRecentManager *manager;
1776 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1777 g_return_if_fail (G_IS_FILE (file));
1779 factory = empathy_ft_factory_dup_singleton ();
1781 empathy_ft_factory_new_transfer_outgoing (factory, contact, file,
1782 empathy_get_current_action_time ());
1784 uri = g_file_get_uri (file);
1785 manager = gtk_recent_manager_get_default ();
1786 gtk_recent_manager_add_item (manager, uri);
1789 g_object_unref (factory);
1793 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1798 /* Only handle a single file for now. It would be wicked cool to be
1799 able to do multiple files, offering to zip them or whatever like
1800 nautilus-sendto does. Note that text/uri-list is defined to have
1801 each line terminated by \r\n, but we can be tolerant of applications
1802 that only use \n or don't terminate single-line entries.
1804 nl = strstr (uri_list, "\r\n");
1806 nl = strchr (uri_list, '\n');
1809 gchar *uri = g_strndup (uri_list, nl - uri_list);
1810 file = g_file_new_for_uri (uri);
1814 file = g_file_new_for_uri (uri_list);
1817 empathy_send_file (contact, file);
1819 g_object_unref (file);
1823 file_manager_send_file_response_cb (GtkDialog *widget,
1825 EmpathyContact *contact)
1829 if (response_id == GTK_RESPONSE_OK) {
1830 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1832 empathy_send_file (contact, file);
1834 g_object_unref (file);
1837 g_object_unref (contact);
1838 gtk_widget_destroy (GTK_WIDGET (widget));
1842 filter_cb (const GtkFileFilterInfo *filter_info,
1845 /* filter out socket files */
1846 return tp_strdiff (filter_info->mime_type, "inode/socket");
1849 static GtkFileFilter *
1850 create_file_filter (void)
1852 GtkFileFilter *filter;
1854 filter = gtk_file_filter_new ();
1856 gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_MIME_TYPE, filter_cb,
1863 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1868 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1870 DEBUG ("Creating selection file chooser");
1872 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1874 GTK_FILE_CHOOSER_ACTION_OPEN,
1876 GTK_RESPONSE_CANCEL,
1880 button = gtk_button_new_with_mnemonic (_("_Send"));
1881 gtk_button_set_image (GTK_BUTTON (button),
1882 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1883 GTK_ICON_SIZE_BUTTON));
1884 gtk_widget_show (button);
1885 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1887 gtk_widget_set_can_default (button, TRUE);
1888 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1891 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1893 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1896 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (widget),
1897 create_file_filter ());
1899 g_signal_connect (widget, "response",
1900 G_CALLBACK (file_manager_send_file_response_cb),
1901 g_object_ref (contact));
1903 gtk_widget_show (widget);
1907 file_manager_receive_file_response_cb (GtkDialog *dialog,
1908 GtkResponseType response,
1909 EmpathyFTHandler *handler)
1911 EmpathyFTFactory *factory;
1914 if (response == GTK_RESPONSE_OK) {
1917 guint64 free_space, file_size;
1918 GError *error = NULL;
1920 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1921 parent = g_file_get_parent (file);
1922 info = g_file_query_filesystem_info (parent,
1923 G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
1926 g_object_unref (parent);
1928 if (error != NULL) {
1929 g_warning ("Error: %s", error->message);
1931 g_object_unref (file);
1935 free_space = g_file_info_get_attribute_uint64 (info,
1936 G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
1937 file_size = empathy_ft_handler_get_total_bytes (handler);
1939 g_object_unref (info);
1941 if (file_size > free_space) {
1942 GtkWidget *message = gtk_message_dialog_new (
1943 GTK_WINDOW (dialog),
1947 _("Insufficient free space to save file"));
1948 char *file_size_str, *free_space_str;
1950 file_size_str = g_format_size (file_size);
1951 free_space_str = g_format_size (free_space);
1953 gtk_message_dialog_format_secondary_text (
1954 GTK_MESSAGE_DIALOG (message),
1955 _("%s of free space are required to save this "
1956 "file, but only %s is available. Please "
1957 "choose another location."),
1958 file_size_str, free_space_str);
1960 gtk_dialog_run (GTK_DIALOG (message));
1962 g_free (file_size_str);
1963 g_free (free_space_str);
1964 gtk_widget_destroy (message);
1966 g_object_unref (file);
1971 factory = empathy_ft_factory_dup_singleton ();
1973 empathy_ft_factory_set_destination_for_incoming_handler (
1974 factory, handler, file);
1976 g_object_unref (factory);
1977 g_object_unref (file);
1979 /* unref the handler, as we dismissed the file chooser,
1980 * and refused the transfer.
1982 g_object_unref (handler);
1985 gtk_widget_destroy (GTK_WIDGET (dialog));
1989 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1993 EmpathyContact *contact;
1996 contact = empathy_ft_handler_get_contact (handler);
1997 g_assert (contact != NULL);
1999 title = g_strdup_printf (_("Incoming file from %s"),
2000 empathy_contact_get_alias (contact));
2002 widget = gtk_file_chooser_dialog_new (title,
2004 GTK_FILE_CHOOSER_ACTION_SAVE,
2006 GTK_RESPONSE_CANCEL,
2010 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
2011 empathy_ft_handler_get_filename (handler));
2012 gtk_file_chooser_set_do_overwrite_confirmation
2013 (GTK_FILE_CHOOSER (widget), TRUE);
2015 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
2017 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
2018 dir = g_get_home_dir ();
2020 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
2022 g_signal_connect (widget, "response",
2023 G_CALLBACK (file_manager_receive_file_response_cb), handler);
2025 gtk_widget_show (widget);
2030 empathy_make_color_whiter (GdkRGBA *color)
2032 const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
2034 color->red = (color->red + white.red) / 2;
2035 color->green = (color->green + white.green) / 2;
2036 color->blue = (color->blue + white.blue) / 2;
2040 menu_deactivate_cb (GtkMenu *menu,
2043 /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
2044 g_signal_handlers_disconnect_by_func (menu,
2045 menu_deactivate_cb, user_data);
2047 gtk_menu_detach (menu);
2050 /* Convenient function to create a GtkMenu attached to @attach_to and detach
2051 * it when the menu is not displayed any more. This is useful when creating a
2052 * context menu that we want to get rid as soon as it as been displayed. */
2054 empathy_context_menu_new (GtkWidget *attach_to)
2058 menu = gtk_menu_new ();
2060 gtk_menu_attach_to_widget (GTK_MENU (menu), attach_to, NULL);
2062 /* menu is initially unowned but gtk_menu_attach_to_widget () taked its
2063 * floating ref. We can either wait that @attach_to releases its ref when
2064 * it will be destroyed (when leaving Empathy most of the time) or explicitely
2065 * detach the menu when it's not displayed any more.
2066 * We go for the latter as we don't want to keep useless menus in memory
2067 * during the whole lifetime of Empathy. */
2068 g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb), NULL);
2074 empathy_get_current_action_time (void)
2076 return (tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
2079 /* @words = empathy_live_search_strip_utf8_string (@text);
2081 * User has to pass both so we don't have to compute @words ourself each time
2082 * this function is called. */
2084 empathy_individual_match_string (FolksIndividual *individual,
2091 gboolean retval = FALSE;
2093 /* check alias name */
2094 str = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual));
2096 if (empathy_live_search_match_words (str, words))
2099 personas = folks_individual_get_personas (individual);
2101 /* check contact id, remove the @server.com part */
2102 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2103 while (retval == FALSE && gee_iterator_next (iter))
2105 FolksPersona *persona = gee_iterator_get (iter);
2108 if (empathy_folks_persona_is_interesting (persona))
2110 str = folks_persona_get_display_id (persona);
2112 /* Accept the persona if @text is a full prefix of his ID; that allows
2113 * user to find, say, a jabber contact by typing his JID. */
2114 if (g_str_has_prefix (str, text))
2120 gchar *dup_str = NULL;
2123 p = strstr (str, "@");
2125 str = dup_str = g_strndup (str, p - str);
2127 visible = empathy_live_search_match_words (str, words);
2133 g_clear_object (&persona);
2135 g_clear_object (&iter);
2137 /* FIXME: Add more rules here, we could check phone numbers in
2138 * contact's vCard for example. */
2143 empathy_launch_program (const gchar *dir,
2147 GdkDisplay *display;
2148 GError *error = NULL;
2151 GdkAppLaunchContext *context = NULL;
2153 /* Try to run from source directory if possible */
2154 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
2157 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2160 path = g_build_filename (dir, name, NULL);
2164 cmd = g_strconcat (path, " ", args, NULL);
2166 cmd = g_strdup (path);
2168 app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
2169 if (app_info == NULL)
2171 DEBUG ("Failed to create app info: %s", error->message);
2172 g_error_free (error);
2176 display = gdk_display_get_default ();
2177 context = gdk_display_get_app_launch_context (display);
2179 if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
2182 g_warning ("Failed to launch %s: %s", name, error->message);
2183 g_error_free (error);
2188 tp_clear_object (&app_info);
2189 tp_clear_object (&context);
2194 /* Most of the workspace manipulation code has been copied from libwnck
2195 * Copyright (C) 2001 Havoc Pennington
2196 * Copyright (C) 2005-2007 Vincent Untz
2199 _wnck_activate_workspace (Screen *screen,
2200 int new_active_space,
2207 display = DisplayOfScreen (screen);
2208 root = RootWindowOfScreen (screen);
2210 xev.xclient.type = ClientMessage;
2211 xev.xclient.serial = 0;
2212 xev.xclient.send_event = True;
2213 xev.xclient.display = display;
2214 xev.xclient.window = root;
2215 xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_CURRENT_DESKTOP");
2216 xev.xclient.format = 32;
2217 xev.xclient.data.l[0] = new_active_space;
2218 xev.xclient.data.l[1] = timestamp;
2219 xev.xclient.data.l[2] = 0;
2220 xev.xclient.data.l[3] = 0;
2221 xev.xclient.data.l[4] = 0;
2223 gdk_error_trap_push ();
2224 XSendEvent (display, root, False,
2225 SubstructureRedirectMask | SubstructureNotifyMask,
2227 XSync (display, False);
2228 gdk_error_trap_pop_ignored ();
2232 _wnck_get_cardinal (Screen *screen,
2245 display = DisplayOfScreen (screen);
2249 gdk_error_trap_push ();
2251 result = XGetWindowProperty (display, xwindow, atom,
2252 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems,
2253 &bytes_after, (void *) &num);
2254 err = gdk_error_trap_pop ();
2255 if (err != Success ||
2259 if (type != XA_CARDINAL)
2273 window_get_workspace (Screen *xscreen,
2278 if (!_wnck_get_cardinal (xscreen, win,
2279 gdk_x11_get_xatom_by_name ("_NET_WM_DESKTOP"), &number))
2285 /* Ask X to move to the desktop on which @window currently is
2286 * and the present @window. */
2288 empathy_move_to_window_desktop (GtkWindow *window,
2293 GdkWindow *gdk_window;
2296 screen = gtk_window_get_screen (window);
2297 xscreen = gdk_x11_screen_get_xscreen (screen);
2298 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
2300 workspace = window_get_workspace (xscreen,
2301 gdk_x11_window_get_xid (gdk_window));
2302 if (workspace == -1)
2305 _wnck_activate_workspace (xscreen, workspace, timestamp);
2308 gtk_window_present_with_time (window, timestamp);
2312 empathy_set_css_provider (GtkWidget *widget)
2314 GtkCssProvider *provider;
2316 GError *error = NULL;
2319 filename = empathy_file_lookup ("empathy.css", "data");
2321 provider = gtk_css_provider_new ();
2323 if (!gtk_css_provider_load_from_path (provider, filename, &error))
2325 g_warning ("Failed to load css file '%s': %s", filename, error->message);
2326 g_error_free (error);
2331 screen = gtk_widget_get_screen (widget);
2333 screen = gdk_screen_get_default ();
2335 gtk_style_context_add_provider_for_screen (screen,
2336 GTK_STYLE_PROVIDER (provider),
2337 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
2341 g_object_unref (provider);