From 0f363b5f4424cc4cdf0e36d7aa5b62b8e8ea52bc Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Fri, 24 Nov 2017 00:10:58 +0200 Subject: Send read receipts Automatically dismiss unread notifications when the window regains focus. fixes #111 fixes #68 --- src/TimelineView.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/TimelineView.cc') diff --git a/src/TimelineView.cc b/src/TimelineView.cc index 267fbbff..44f3b9d8 100644 --- a/src/TimelineView.cc +++ b/src/TimelineView.cc @@ -379,6 +379,9 @@ TimelineView::addEvents(const Timeline &timeline) if (!timeline.events().isEmpty() && scroll_layout_->count() > 1) notifyForLastEvent(); + if (isActiveWindow() && isVisible() && timeline.events().size() > 0) + readLastEvent(); + return message_count; } @@ -648,3 +651,52 @@ TimelineView::paintEvent(QPaintEvent *) QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); } + +void +TimelineView::readLastEvent() const +{ + const auto eventId = getLastEventId(); + + if (!eventId.isEmpty()) + client_->readEvent(room_id_, eventId); +} + +QString +TimelineView::getLastEventId() const +{ + auto index = scroll_layout_->count(); + + // Search backwards for the first event that has a valid event id. + while (index > 0) { + --index; + + auto lastItem = scroll_layout_->itemAt(index); + auto *lastTimelineItem = qobject_cast(lastItem->widget()); + + if (lastTimelineItem && !lastTimelineItem->eventId().isEmpty()) + return lastTimelineItem->eventId(); + } + + return QString(""); +} + +void +TimelineView::showEvent(QShowEvent *event) +{ + readLastEvent(); + + QWidget::showEvent(event); +} + +bool +TimelineView::event(QEvent *event) +{ + if (event->type() == QEvent::WindowActivate) { + QTimer::singleShot(1000, this, [=]() { + emit clearUnreadMessageCount(room_id_); + readLastEvent(); + }); + } + + return QWidget::event(event); +} -- cgit 1.5.1