diff --git a/src/ring_client.cpp b/src/ring_client.cpp index 219961ab75ef74d27d3d306276b3a0fcb29ce1c9..c1b5233743add84dc88d4ebf8b48ed5861c02ee6 100644 --- a/src/ring_client.cpp +++ b/src/ring_client.cpp @@ -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 */ diff --git a/src/ringmainwindow.cpp b/src/ringmainwindow.cpp index 635d55c0e8cfdfc29d5c554bf3b6966e6ee924c6..e831d1b318054f6b7fe4a70140e27a3b75febbce 100644 --- a/src/ringmainwindow.cpp +++ b/src/ringmainwindow.cpp @@ -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 diff --git a/src/ringnotify.cpp b/src/ringnotify.cpp index 0013a126ffb8c05272c8735991407e9094a04f83..7b24664ab0458f0a3102273f2b241c9e63c1f289 100644 --- a/src/ringnotify.cpp +++ b/src/ringnotify.cpp @@ -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 } diff --git a/src/ringnotify.h b/src/ringnotify.h index e16ef0b9b90c02fafdf94bf02289057cf96845fe..db402f3a3b091aded29d65d2394bfa305a643fab 100644 --- a/src/ringnotify.h +++ b/src/ringnotify.h @@ -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