Commit cfeffc36 authored by Nicolas Jager's avatar Nicolas Jager

fix: incoming chat message notification

- notify user of new chat messages only when the conversation is
  not selected.

Change-Id: I205ea6bc1df10f26d71bac5f0fcdc34f8195e864
parent 5b32612c
......@@ -43,7 +43,6 @@
#include <personmodel.h>
#include <fallbackpersoncollection.h>
#include <localhistorycollection.h>
#include <media/text.h>
#include <numbercategorymodel.h>
#include <globalinstances.h>
#include <profilemodel.h>
......@@ -52,7 +51,6 @@
#include <localprofilecollection.h>
#include <accountmodel.h>
#include <smartinfohub.h>
#include <media/textrecording.h>
#include <media/recordingmodel.h>
#include <availableaccountmodel.h>
......@@ -122,7 +120,6 @@ struct _RingClientPrivate {
/* notifications */
QMetaObject::Connection call_notification;
QMetaObject::Connection chat_notification;
};
/* this union is used to pass ints as pointers and vice versa for GAction parameters*/
......@@ -473,23 +470,6 @@ call_notifications_toggled(RingClient *self)
}
}
static void
chat_notifications_toggled(RingClient *self)
{
auto priv = RING_CLIENT_GET_PRIVATE(self);
if (g_settings_get_boolean(priv->settings, "enable-chat-notifications")) {
priv->chat_notification = QObject::connect(
&Media::RecordingModel::instance(),
&Media::RecordingModel::newTextMessage,
[self] (Media::TextRecording* t, ContactMethod* cm)
{ ring_notify_message(cm, t, self); }
);
} else {
QObject::disconnect(priv->chat_notification);
}
}
static void
ring_client_startup(GApplication *app)
{
......@@ -611,9 +591,7 @@ ring_client_startup(GApplication *app)
/* enable notifications based on settings */
ring_notify_init();
call_notifications_toggled(client);
chat_notifications_toggled(client);
g_signal_connect_swapped(priv->settings, "changed::enable-call-notifications", G_CALLBACK(call_notifications_toggled), client);
g_signal_connect_swapped(priv->settings, "changed::enable-chat-notifications", G_CALLBACK(chat_notifications_toggled), client);
#if USE_LIBNM
/* monitor the network using libnm to notify the daemon about connectivity chagnes */
......@@ -637,7 +615,6 @@ ring_client_shutdown(GApplication *app)
QObject::disconnect(priv->uam_updated);
QObject::disconnect(priv->call_notification);
QObject::disconnect(priv->chat_notification);
/* free the QCoreApplication, which will destroy all libRingClient models
* and thus send the Unregister signal over dbus to dring */
......
......@@ -36,6 +36,9 @@
#include <api/newcallmodel.h>
#include <api/behaviorcontroller.h>
#include "accountcontainer.h"
#include <media/textrecording.h>
#include <media/recordingmodel.h>
#include <media/text.h>
// Ring client
#include "accountview.h"
......@@ -52,6 +55,7 @@
#include "ringwelcomeview.h"
#include "utils/accounts.h"
#include "utils/files.h"
#include "ringnotify.h"
//==============================================================================
......@@ -213,6 +217,7 @@ public:
QMetaObject::Connection newConversationConnection_;
QMetaObject::Connection conversationRemovedConnection_;
QMetaObject::Connection accountStatusChangedConnection_;
QMetaObject::Connection chat_notification_;
private:
CppImpl() = delete;
......@@ -748,6 +753,7 @@ CppImpl::~CppImpl()
QObject::disconnect(showCallViewConnection_);
QObject::disconnect(modelSortedConnection_);
QObject::disconnect(accountStatusChangedConnection_);
QObject::disconnect(chat_notification_);
g_clear_object(&widgets->welcome_view);
g_clear_object(&widgets->webkit_chat_container);
......@@ -768,6 +774,8 @@ CppImpl::changeView(GType type, lrc::api::conversation::Info conversation)
} else if (g_type_is_a(CHAT_VIEW_TYPE, type)) {
new_view = displayChatView(conversation);
} else {
chatViewConversation_.reset(nullptr);
// TODO select first conversation?
new_view = widgets->welcome_view;
......@@ -1389,6 +1397,24 @@ CppImpl::slotShowIncomingCallView(const std::string& id, lrc::api::conversation:
//==============================================================================
static void
chat_notifications(RingMainWindow *win)
{
auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
priv->cpp->chat_notification_ = QObject::connect(
&Media::RecordingModel::instance(),
&Media::RecordingModel::newTextMessage,
[win, priv] (Media::TextRecording* t, ContactMethod* cm) {
if ((priv->cpp->chatViewConversation_
&& priv->cpp->chatViewConversation_->participants[0] == cm->uri().toStdString())
|| not g_settings_get_boolean(priv->settings, "enable-chat-notifications"))
return;
ring_notify_message(cm, t);
}
);
}
static void
ring_main_window_init(RingMainWindow *win)
{
......@@ -1398,6 +1424,9 @@ ring_main_window_init(RingMainWindow *win)
// CppImpl ctor
priv->cpp = new details::CppImpl {*win};
priv->cpp->init();
// setup chat notification
chat_notifications(win);
}
static void
......
......@@ -406,36 +406,22 @@ delete_idx(QModelIndex *idx)
void
ring_notify_message(
#if !USE_LIBNOTIFY
ContactMethod*, Media::TextRecording*, RingClient*)
ContactMethod*, Media::TextRecording*)
#else
ContactMethod *cm, Media::TextRecording *t, RingClient *client)
ContactMethod *cm, Media::TextRecording *t)
#endif
{
#if USE_LIBNOTIFY
g_return_if_fail(cm && t && client);
g_return_if_fail(cm && t);
// get the message
auto model = t->instantMessagingModel();
auto msg_idx = model->index(model->rowCount()-1, 0);
// make sure its a text message, or else there is nothing to do
if (msg_idx.data(static_cast<int>(Media::TextRecording::Role::HasText)).toBool()) {
auto main_window = ring_client_get_main_window(client);
if ( main_window && gtk_window_is_active(main_window)) {
/* in this case we only want to show the notification if the message is not marked as
* read; this will only possibly be done after the the chatview has displayed it in
* response to this or another signal; so we must check for the read status after the
* chat view handler has completed, we do so via a g_idle function.
*/
auto new_idx = new QModelIndex(msg_idx);
g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, (GSourceFunc)show_message_if_unread, new_idx, (GDestroyNotify)delete_idx);
} else {
/* always show a notification if the window is not active/visible */
ring_notify_show_text_message(cm, msg_idx);
}
ring_notify_show_text_message(cm, msg_idx);
}
return;
#endif
}
......
......@@ -21,7 +21,7 @@
#define RING_NOTIFY_H_
#include <gtk/gtk.h>
#include "ring_client.h"
#include "ringmainwindow.h"
class Call;
class ContactMethod;
......@@ -35,7 +35,7 @@ void ring_notify_init();
void ring_notify_uninit();
gboolean ring_notify_is_initted();
gboolean ring_notify_incoming_call(Call*);
void ring_notify_message(ContactMethod*, Media::TextRecording*, RingClient*);
void ring_notify_message(ContactMethod*, Media::TextRecording*);
gboolean ring_notify_close_chat_notification(ContactMethod*);
G_END_DECLS
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment