From bd22a6ed5906520a5acb41ce544596e5dfda82e7 Mon Sep 17 00:00:00 2001 From: xclaesse Date: Fri, 30 Jan 2009 17:07:35 +0000 Subject: W.I.P. for notifications on chat window. git-svn-id: svn+ssh://svn.gnome.org/svn/empathy/trunk@2285 4ee84921-47dd-4033-b63a-18d7a039a3e4 --- libempathy-gtk/empathy-ui-utils.c | 10 ++++- libempathy-gtk/empathy-ui-utils.h | 2 +- src/empathy-chat-window.c | 85 +++++++++++++++++++++++++++++++++++++++ src/empathy-event-manager.c | 3 +- src/empathy-status-icon.c | 9 +++-- 5 files changed, 101 insertions(+), 8 deletions(-) diff --git a/libempathy-gtk/empathy-ui-utils.c b/libempathy-gtk/empathy-ui-utils.c index 7b55587ea..7c6166839 100644 --- a/libempathy-gtk/empathy-ui-utils.c +++ b/libempathy-gtk/empathy-ui-utils.c @@ -1647,7 +1647,7 @@ empathy_sound_play (GtkWidget *widget, } gboolean -empathy_notification_should_show (void) +empathy_notification_should_show (gboolean check_focus) { EmpathyConf *conf; gboolean res; @@ -1670,6 +1670,12 @@ empathy_notification_should_show (void) } } - return TRUE; + if (check_focus) { + empathy_conf_get_bool (conf, + EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res); + return res; + } else { + return TRUE; + } } diff --git a/libempathy-gtk/empathy-ui-utils.h b/libempathy-gtk/empathy-ui-utils.h index b3b26f0bb..455ddd7a2 100644 --- a/libempathy-gtk/empathy-ui-utils.h +++ b/libempathy-gtk/empathy-ui-utils.h @@ -132,7 +132,7 @@ void empathy_sound_play (GtkWidget *widge EmpathySound sound_id); /* Notifications */ -gboolean empathy_notification_should_show (void); +gboolean empathy_notification_should_show (gboolean check_focus); G_END_DECLS diff --git a/src/empathy-chat-window.c b/src/empathy-chat-window.c index 0ce90ac4a..2f9d59641 100644 --- a/src/empathy-chat-window.c +++ b/src/empathy-chat-window.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -70,6 +71,7 @@ typedef struct { EmpathyChatroomManager *chatroom_manager; GtkWidget *dialog; GtkWidget *notebook; + NotifyNotification *notification; /* Menu items. */ GtkWidget *menu_conv_clear; @@ -833,6 +835,79 @@ chat_window_set_urgency_hint (EmpathyChatWindow *window, gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent); } +static gboolean +notification_closed_idle_cb (EmpathyChatWindow *window) +{ + EmpathyChatWindowPriv *priv = GET_PRIV (window); + + gtk_widget_grab_focus (priv->dialog); + + return FALSE; +} + +static void +chat_window_notification_closed_cb (NotifyNotification *notify, + EmpathyChatWindow *window) +{ + EmpathyChatWindowPriv *priv = GET_PRIV (window); + int reason; + + reason = notify_notification_get_closed_reason (notify); + + if (priv->notification) { + g_object_unref (priv->notification); + priv->notification = NULL; + } + + if (reason == 2) { + g_idle_add ((GSourceFunc) notification_closed_idle_cb, window); + } +} + +static void +show_or_update_notification (EmpathyMessage *message, + EmpathyChatWindow *window) +{ + EmpathyContact *sender; + char *header; + const char *body; + GdkPixbuf *pixbuf; + EmpathyChatWindowPriv *priv = GET_PRIV (window); + + if (!empathy_notification_should_show (TRUE)) { + return; + } + + 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); + pixbuf = empathy_pixbuf_avatar_from_contact_scaled (sender, 48, 48); + if (pixbuf == NULL) { + /* we use INVALID here so we fall pack to 48px */ + pixbuf = empathy_pixbuf_from_icon_name (EMPATHY_IMAGE_NEW_MESSAGE, + GTK_ICON_SIZE_INVALID); + } + + if (priv->notification != NULL) { + notify_notification_update (priv->notification, + header, body, NULL); + notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf); + } else { + priv->notification = notify_notification_new (header, body, NULL, priv->dialog); + 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), window); + } + + notify_notification_show (priv->notification, NULL); + + g_object_unref (pixbuf); + g_free (header); +} + static void chat_window_new_message_cb (EmpathyChat *chat, EmpathyMessage *message, @@ -866,6 +941,10 @@ chat_window_new_message_cb (EmpathyChat *chat, } } + if (!has_focus) { + show_or_update_notification (message, window); + } + if (has_focus && priv->current_chat == chat) { return; } @@ -1163,6 +1242,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); diff --git a/src/empathy-event-manager.c b/src/empathy-event-manager.c index 253067ff1..f4e774b84 100644 --- a/src/empathy-event-manager.c +++ b/src/empathy-event-manager.c @@ -230,7 +230,6 @@ event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat, /* try to update the event if it's referring to a chat which is already in the * queue. */ - event = event_lookup_by_approval (approval->manager, approval); if (event != NULL && event->inhibit) @@ -248,7 +247,7 @@ event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat, channel = empathy_tp_chat_get_channel (tp_chat); if (event != NULL) - event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg); + event_update (approval->manager, event, EMPATHY_IMAGE_NEW_MESSAGE, header, msg); else event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, header, msg, approval, event_channel_process_func, NULL); diff --git a/src/empathy-status-icon.c b/src/empathy-status-icon.c index 9ea211784..4c7964566 100644 --- a/src/empathy-status-icon.c +++ b/src/empathy-status-icon.c @@ -140,6 +140,8 @@ status_icon_update_notification (EmpathyStatusIcon *icon) g_signal_connect (priv->notification, "closed", G_CALLBACK (status_icon_notification_closed_cb), icon); + + g_object_unref (pixbuf); } notify_notification_show (priv->notification, NULL); @@ -223,7 +225,7 @@ status_icon_event_added_cb (EmpathyEventManager *manager, status_icon_update_icon (icon); status_icon_update_tooltip (icon); - if (empathy_notification_should_show ()) { + if (empathy_notification_should_show (FALSE)) { status_icon_update_notification (icon); } @@ -272,7 +274,7 @@ status_icon_event_updated_cb (EmpathyEventManager *manager, return; } - if (empathy_notification_should_show ()) { + if (empathy_notification_should_show (FALSE)) { status_icon_update_notification (icon); } @@ -340,7 +342,7 @@ status_icon_idle_notify_cb (EmpathyStatusIcon *icon) status_icon_update_icon (icon); status_icon_update_tooltip (icon); - if (!empathy_notification_should_show ()) { + if (!empathy_notification_should_show (FALSE)) { /* dismiss the outstanding notification if present */ if (priv->notification) { @@ -504,6 +506,7 @@ status_icon_finalize (GObject *object) if (priv->notification) { notify_notification_close (priv->notification, NULL); g_object_unref (priv->notification); + priv->notification = NULL; } g_object_unref (priv->icon); -- cgit v1.2.3