]> git.0d.be Git - empathy.git/blobdiff - src/empathy-chat-window.c
Add en_GB in gitignore
[empathy.git] / src / empathy-chat-window.c
index 7ae404e096f82bf70b2d8c2d02121a61a14b04d3..d7c1a398fd3440247182c2ee998b38963390c4b7 100644 (file)
@@ -33,6 +33,7 @@
 #include <gdk/gdkkeysyms.h>
 #include <glade/glade.h>
 #include <glib/gi18n.h>
+#include <libnotify/notification.h>
 
 #include <telepathy-glib/util.h>
 #include <libmissioncontrol/mission-control.h>
 #include <libempathy-gtk/empathy-contact-dialogs.h>
 #include <libempathy-gtk/empathy-log-window.h>
 #include <libempathy-gtk/empathy-geometry.h>
+#include <libempathy-gtk/empathy-smiley-manager.h>
 #include <libempathy-gtk/empathy-ui-utils.h>
 
 #include "empathy-chat-window.h"
 #include "empathy-about-dialog.h"
+#include "empathy-misc.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
 #include <libempathy/empathy-debug.h>
@@ -69,6 +72,7 @@ typedef struct {
        EmpathyChatroomManager *chatroom_manager;
        GtkWidget   *dialog;
        GtkWidget   *notebook;
+       NotifyNotification *notification;
 
        /* Menu items. */
        GtkWidget   *menu_conv_clear;
@@ -263,23 +267,6 @@ chat_window_create_label (EmpathyChatWindow *window,
        return hbox;
 }
 
-static const gchar *
-chat_window_get_chat_name (EmpathyChat *chat)
-{
-       EmpathyContact *remote_contact = NULL;
-       const gchar    *name = NULL;
-
-       name = empathy_chat_get_name (chat);
-       if (!name) {
-               remote_contact = empathy_chat_get_remote_contact (chat);
-               if (remote_contact) {
-                       name = empathy_contact_get_name (remote_contact);
-               }
-       }
-
-       return name ? name : _("Conversation");
-}
-
 static void
 chat_window_update (EmpathyChatWindow *window)
 {
@@ -304,7 +291,7 @@ chat_window_update (EmpathyChatWindow *window)
        first_page = (page_num == 0);
        last_page = (page_num == (num_pages - 1));
        is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
-       name = chat_window_get_chat_name (priv->current_chat);
+       name = empathy_chat_get_name (priv->current_chat);
        n_chats = g_list_length (priv->chats);
 
        DEBUG ("Update window");
@@ -341,7 +328,10 @@ chat_window_update (EmpathyChatWindow *window)
                        remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
                        icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
                        gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
-                       g_object_unref (icon);
+
+                       if (icon != NULL) {
+                               g_object_unref (icon);
+                       }
                } else {
                        gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
                }
@@ -369,10 +359,11 @@ chat_window_update_chat_tab (EmpathyChat *chat)
        EmpathyChatWindowPriv *priv;
        EmpathyContact        *remote_contact;
        const gchar           *name;
+       McAccount             *account;
        const gchar           *subject;
        GtkWidget             *widget;
        GString               *tooltip;
-       gchar                 *str;
+       gchar                 *markup;
        const gchar           *icon_name;
 
        window = chat_window_find_chat (chat);
@@ -382,12 +373,13 @@ chat_window_update_chat_tab (EmpathyChat *chat)
        priv = GET_PRIV (window);
 
        /* Get information */
-       name = chat_window_get_chat_name (chat);
+       name = empathy_chat_get_name (chat);
+       account = empathy_chat_get_account (chat);
        subject = empathy_chat_get_subject (chat);
        remote_contact = empathy_chat_get_remote_contact (chat);
 
-       DEBUG ("Updating chat tab, name=%s, subject=%s, remote_contact=%p",
-               name, subject, remote_contact);
+       DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
+               name, mc_account_get_unique_name (account), subject, remote_contact);
 
        /* Update tab image */
        if (g_list_find (priv->chats_new_msg, chat)) {
@@ -406,24 +398,37 @@ chat_window_update_chat_tab (EmpathyChat *chat)
 
        /* Update tab tooltip */
        tooltip = g_string_new (NULL);
+
        if (remote_contact) {
-               g_string_append_printf (tooltip, "%s\n%s",
-                                       empathy_contact_get_id (remote_contact),
-                                       empathy_contact_get_status (remote_contact));
+               markup = g_markup_printf_escaped ("<b>%s</b><small> (%s)</small>\n%s",
+                                                 empathy_contact_get_id (remote_contact),
+                                                 mc_account_get_display_name (account),
+                                                 empathy_contact_get_status (remote_contact));
+               g_string_append (tooltip, markup);
+               g_free (markup);
        }
        else {
-               g_string_append (tooltip, name);
+               markup = g_markup_printf_escaped ("<b>%s</b><small>  (%s)</small>", name,
+                                                 mc_account_get_display_name (account));
+               g_string_append (tooltip, markup);
+               g_free (markup);
        }
+
        if (subject) {
-               g_string_append_printf (tooltip, "\n%s %s", _("Topic:"), subject);
+               markup = g_markup_printf_escaped ("\n<b>%s</b> %s", _("Topic:"), subject);
+               g_string_append (tooltip, markup);
+               g_free (markup);
        }
        if (g_list_find (priv->chats_composing, chat)) {
-               g_string_append_printf (tooltip, "\n%s", _("Typing a message."));
+               markup = g_markup_printf_escaped ("\n%s", _("Typing a message."));
+               g_string_append (tooltip, markup);
+               g_free (markup);
        }
-       str = g_string_free (tooltip, FALSE);
+
+       markup = g_string_free (tooltip, FALSE);
        widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
-       gtk_widget_set_tooltip_text (widget, str);
-       g_free (str);
+       gtk_widget_set_tooltip_markup (widget, markup);
+       g_free (markup);
 
        /* Update tab label */
        widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
@@ -467,25 +472,20 @@ chat_window_chat_notify_cb (EmpathyChat *chat)
 }
 
 static void
-chat_window_insert_smiley_activate_cb (GtkWidget         *menuitem,
-                                      EmpathyChatWindow *window)
+chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
+                                      EmpathySmiley        *smiley,
+                                      gpointer              window)
 {
-       EmpathyChatWindowPriv *priv;
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
        EmpathyChat           *chat;
-       GtkTextBuffer        *buffer;
-       GtkTextIter           iter;
-       const gchar          *smiley;
-
-       priv = GET_PRIV (window);
+       GtkTextBuffer         *buffer;
+       GtkTextIter            iter;
 
        chat = priv->current_chat;
 
-       smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
-
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
        gtk_text_buffer_get_end_iter (buffer, &iter);
-       gtk_text_buffer_insert (buffer, &iter,
-                               smiley, -1);
+       gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
 }
 
 static void
@@ -658,14 +658,13 @@ chat_window_edit_activate_cb (GtkWidget        *menuitem,
        }
 
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
-       if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
+       if (gtk_text_buffer_get_has_selection (buffer)) {
                gtk_widget_set_sensitive (priv->menu_edit_copy, TRUE);
                gtk_widget_set_sensitive (priv->menu_edit_cut, TRUE);
        } else {
                gboolean selection;
 
-               selection = empathy_chat_view_get_selection_bounds (priv->current_chat->view, 
-                                                                  NULL, NULL);
+               selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
 
                gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
                gtk_widget_set_sensitive (priv->menu_edit_copy, selection);
@@ -777,7 +776,7 @@ static void
 chat_window_help_contents_cb (GtkWidget         *menuitem,
                              EmpathyChatWindow *window)
 {
-       empathy_url_show ("ghelp:empathy?chat");
+       empathy_url_show (menuitem, "ghelp:empathy?chat");
 }
 
 static void
@@ -837,6 +836,86 @@ chat_window_set_urgency_hint (EmpathyChatWindow *window,
        gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
 }
 
+typedef struct {
+       EmpathyChatWindow *window;
+       EmpathyChat *chat;
+} NotificationData;
+
+static void
+chat_window_notification_closed_cb (NotifyNotification *notify,
+                                   NotificationData *cb_data)
+{
+       EmpathyNotificationClosedReason reason = 0;
+       EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
+
+#ifdef notify_notification_get_closed_reason
+       reason = notify_notification_get_closed_reason (notify);
+#endif
+       if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
+               empathy_chat_window_present_chat (cb_data->chat);
+       }
+
+       g_object_unref (notify);
+       priv->notification = NULL;
+       g_object_unref (cb_data->chat);
+       g_slice_free (NotificationData, cb_data);
+}
+
+static void
+chat_window_show_or_update_notification (EmpathyChatWindow *window,
+                                        EmpathyMessage *message,
+                                        EmpathyChat *chat)
+{
+       EmpathyContact *sender;
+       char *header, *escaped;
+       const char *body;
+       GdkPixbuf *pixbuf;
+       NotificationData *cb_data;
+       EmpathyChatWindowPriv *priv = GET_PRIV (window);
+       gboolean res;
+
+       if (!empathy_notification_is_enabled ()) {
+               return;
+       } else {
+               empathy_conf_get_bool (empathy_conf_get (),
+                                      EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
+               if (!res) {
+                       return;
+               }
+       }
+
+       cb_data = g_slice_new0 (NotificationData);
+       cb_data->chat = g_object_ref (chat);
+       cb_data->window = window;
+
+       sender = empathy_message_get_sender (message);
+       header = g_strdup_printf (_("New message from %s"),
+                                 empathy_contact_get_name (sender));
+       body = empathy_message_get_body (message);
+       escaped = g_markup_escape_text (body, -1);
+
+       pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
+
+       if (priv->notification != NULL) {
+               notify_notification_update (priv->notification,
+                                           header, escaped, NULL);
+               notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
+       } else {
+               priv->notification = notify_notification_new (header, escaped, NULL, NULL);
+               notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
+               notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
+
+               g_signal_connect (priv->notification, "closed",
+                                 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
+       }
+
+       notify_notification_show (priv->notification, NULL);
+
+       g_object_unref (pixbuf);
+       g_free (header);
+       g_free (escaped);
+}
+
 static void
 chat_window_new_message_cb (EmpathyChat       *chat,
                            EmpathyMessage    *message,
@@ -845,11 +924,35 @@ chat_window_new_message_cb (EmpathyChat       *chat,
        EmpathyChatWindowPriv *priv;
        gboolean              has_focus;
        gboolean              needs_urgency;
+       EmpathyContact        *sender;
 
        priv = GET_PRIV (window);
 
        has_focus = empathy_chat_window_has_focus (window);
 
+       /* - if we're the sender, we play the sound if it's specified in the
+        *   preferences and we're not away.
+        * - if we receive a message, we play the sound if it's specified in the
+        *   prefereces and the window does not have focus on the chat receiving
+        *   the message.
+        */
+
+       sender = empathy_message_get_sender (message);
+
+       if (empathy_contact_is_user (sender) != FALSE) {
+               empathy_sound_play (GTK_WIDGET (priv->dialog),
+                                   EMPATHY_SOUND_MESSAGE_OUTGOING);
+       } else {
+               if ((!has_focus || priv->current_chat != chat)) {
+                       empathy_sound_play (GTK_WIDGET (priv->dialog),
+                                           EMPATHY_SOUND_MESSAGE_INCOMING);
+               }
+       }
+
+       if (!has_focus) {
+               chat_window_show_or_update_notification (window, message, chat);
+       }
+
        if (has_focus && priv->current_chat == chat) {
                return;
        }
@@ -1065,7 +1168,7 @@ chat_window_drag_data_received (GtkWidget        *widget,
                chat = empathy_chat_window_find_chat (account, strv[1]);
 
                if (!chat) {
-                       empathy_dispatcher_chat_with_contact_id (account, strv[2]);
+                       empathy_dispatcher_chat_with_contact_id (account, strv[2], NULL, NULL);
                        g_object_unref (account);
                        g_strfreev (strv);
                        return;
@@ -1147,6 +1250,12 @@ chat_window_finalize (GObject *object)
                g_source_remove (priv->save_geometry_id);
        }
 
+       if (priv->notification != NULL) {
+               notify_notification_close (priv->notification, NULL);
+               g_object_unref (priv->notification);
+               priv->notification = NULL;
+       }
+
        chat_windows = g_list_remove (chat_windows, window);
        gtk_widget_destroy (priv->dialog);
 
@@ -1186,6 +1295,7 @@ empathy_chat_window_init (EmpathyChatWindow *window)
        gint                   i;
        GtkWidget             *chat_vbox;
        gchar                 *filename;
+       EmpathySmileyManager  *smiley_manager;
        EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
                EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
 
@@ -1235,7 +1345,7 @@ empathy_chat_window_init (EmpathyChatWindow *window)
 
        g_object_unref (glade);
 
-       priv->chatroom_manager = empathy_chatroom_manager_new (NULL);
+       priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
 
        priv->notebook = gtk_notebook_new ();
        gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow"); 
@@ -1260,10 +1370,13 @@ empathy_chat_window_init (EmpathyChatWindow *window)
        g_object_unref (accel_group);
 
        /* Set up smiley menu */
-       menu = empathy_chat_view_get_smiley_menu (
-               G_CALLBACK (chat_window_insert_smiley_activate_cb),
-               window);
-       gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_insert_smiley), menu);
+       smiley_manager = empathy_smiley_manager_dup_singleton ();
+       menu = empathy_smiley_menu_new (smiley_manager,
+                                       chat_window_insert_smiley_activate_cb,
+                                       window);
+       gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_insert_smiley),
+                                  menu);
+       g_object_unref (smiley_manager);
 
        /* Set up signals we can't do with glade since we may need to
         * block/unblock them at some later stage.
@@ -1536,7 +1649,7 @@ empathy_chat_window_find_chat (McAccount   *account,
        GList *l;
 
        g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
-       g_return_val_if_fail (!G_STR_EMPTY (id), NULL);
+       g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
 
        for (l = chat_windows; l; l = l->next) {
                EmpathyChatWindowPriv *priv;