2017-04-06 02:06:42 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QDebug>
|
2017-05-07 17:15:38 +03:00
|
|
|
#include <QJsonArray>
|
2017-04-06 02:06:42 +03:00
|
|
|
#include <QScrollBar>
|
2017-04-13 04:11:22 +03:00
|
|
|
#include <QSettings>
|
2017-04-06 02:06:42 +03:00
|
|
|
#include <QtWidgets/QLabel>
|
|
|
|
#include <QtWidgets/QSpacerItem>
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
#include "Event.h"
|
|
|
|
#include "MessageEvent.h"
|
|
|
|
#include "MessageEventContent.h"
|
|
|
|
|
2017-04-28 14:56:45 +03:00
|
|
|
#include "ImageItem.h"
|
2017-04-25 14:37:54 +03:00
|
|
|
#include "TimelineItem.h"
|
|
|
|
#include "TimelineView.h"
|
|
|
|
#include "TimelineViewManager.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
namespace events = matrix::events;
|
|
|
|
namespace msgs = matrix::events::messages;
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
TimelineView::TimelineView(const Timeline &timeline,
|
|
|
|
QSharedPointer<MatrixClient> client,
|
|
|
|
const QString &room_id,
|
|
|
|
QWidget *parent)
|
2017-04-06 02:06:42 +03:00
|
|
|
: QWidget(parent)
|
2017-05-12 15:43:35 +03:00
|
|
|
, room_id_{room_id}
|
2017-04-28 14:56:45 +03:00
|
|
|
, client_{client}
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-05-12 15:43:35 +03:00
|
|
|
QSettings settings;
|
|
|
|
local_user_ = settings.value("auth/user_id").toString();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
|
|
|
init();
|
2017-05-12 15:43:35 +03:00
|
|
|
addEvents(timeline);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-07-29 11:49:00 +03:00
|
|
|
TimelineView::TimelineView(QSharedPointer<MatrixClient> client, const QString &room_id, QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
, room_id_{room_id}
|
|
|
|
, client_{client}
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
local_user_ = settings.value("auth/user_id").toString();
|
|
|
|
|
|
|
|
init();
|
|
|
|
client_->messages(room_id_, "");
|
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
void TimelineView::sliderRangeChanged(int min, int max)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
|
|
|
Q_UNUSED(min);
|
2017-05-12 15:43:35 +03:00
|
|
|
|
2017-08-05 15:59:24 +03:00
|
|
|
if (!scroll_area_->verticalScrollBar()->isVisible()) {
|
|
|
|
scroll_area_->verticalScrollBar()->setValue(max);
|
2017-05-12 15:43:35 +03:00
|
|
|
return;
|
2017-08-05 15:59:24 +03:00
|
|
|
}
|
2017-05-12 15:43:35 +03:00
|
|
|
|
|
|
|
if (max - scroll_area_->verticalScrollBar()->value() < SCROLL_BAR_GAP)
|
|
|
|
scroll_area_->verticalScrollBar()->setValue(max);
|
2017-06-05 19:21:19 +03:00
|
|
|
|
|
|
|
if (isPaginationScrollPending_) {
|
|
|
|
isPaginationScrollPending_ = false;
|
|
|
|
|
|
|
|
int currentHeight = scroll_widget_->size().height();
|
|
|
|
int diff = currentHeight - oldHeight_;
|
2017-07-29 11:49:00 +03:00
|
|
|
int newPosition = oldPosition_ + diff;
|
|
|
|
|
|
|
|
// Keep the scroll bar to the bottom if we are coming from
|
|
|
|
// an scrollbar without height i.e scrollbar->value() == 0
|
|
|
|
if (oldPosition_ == 0)
|
|
|
|
newPosition = max;
|
|
|
|
|
|
|
|
scroll_area_->verticalScrollBar()->setValue(newPosition);
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 19:21:19 +03:00
|
|
|
|
2017-07-29 11:49:00 +03:00
|
|
|
void TimelineView::fetchHistory()
|
|
|
|
{
|
|
|
|
bool hasEnoughMessages = scroll_area_->verticalScrollBar()->value() != 0;
|
|
|
|
|
2017-08-05 15:59:24 +03:00
|
|
|
if (!hasEnoughMessages && !isTimelineFinished) {
|
2017-07-29 11:49:00 +03:00
|
|
|
isPaginationInProgress_ = true;
|
|
|
|
client_->messages(room_id_, prev_batch_token_);
|
2017-08-05 15:59:24 +03:00
|
|
|
paginationTimer_->start(500);
|
|
|
|
return;
|
2017-06-05 19:21:19 +03:00
|
|
|
}
|
2017-08-05 15:59:24 +03:00
|
|
|
|
|
|
|
paginationTimer_->stop();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
void TimelineView::scrollDown()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-05-12 15:43:35 +03:00
|
|
|
int current = scroll_area_->verticalScrollBar()->value();
|
|
|
|
int max = scroll_area_->verticalScrollBar()->maximum();
|
|
|
|
|
|
|
|
// The first time we enter the room move the scroll bar to the bottom.
|
|
|
|
if (!isInitialized) {
|
2017-06-05 19:54:45 +03:00
|
|
|
scroll_area_->verticalScrollBar()->setValue(max);
|
2017-05-12 15:43:35 +03:00
|
|
|
isInitialized = true;
|
|
|
|
return;
|
|
|
|
}
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
// If the gap is small enough move the scroll bar down. e.g when a new message appears.
|
|
|
|
if (max - current < SCROLL_BAR_GAP)
|
|
|
|
scroll_area_->verticalScrollBar()->setValue(max);
|
|
|
|
}
|
2017-04-15 02:56:04 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
void TimelineView::sliderMoved(int position)
|
|
|
|
{
|
|
|
|
if (!scroll_area_->verticalScrollBar()->isVisible())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The scrollbar is high enough so we can start retrieving old events.
|
|
|
|
if (position < SCROLL_BAR_GAP) {
|
|
|
|
if (isTimelineFinished)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Prevent user from moving up when there is pagination in progress.
|
2017-06-05 19:21:19 +03:00
|
|
|
// TODO: Keep a map of the event ids to filter out duplicates.
|
|
|
|
if (isPaginationInProgress_)
|
2017-05-12 15:43:35 +03:00
|
|
|
return;
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
isPaginationInProgress_ = true;
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
// FIXME: Maybe move this to TimelineViewManager to remove the extra calls?
|
2017-06-05 19:21:19 +03:00
|
|
|
client_->messages(room_id_, prev_batch_token_);
|
2017-05-12 15:43:35 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
void TimelineView::addBackwardsEvents(const QString &room_id, const RoomMessages &msgs)
|
|
|
|
{
|
|
|
|
if (room_id_ != room_id)
|
|
|
|
return;
|
2017-04-15 02:56:04 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
if (msgs.chunk().count() == 0) {
|
|
|
|
isTimelineFinished = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isTimelineFinished = false;
|
|
|
|
last_sender_backwards_.clear();
|
|
|
|
QList<TimelineItem *> items;
|
|
|
|
|
|
|
|
// Parse in reverse order to determine where we should not show sender's name.
|
|
|
|
auto it = msgs.chunk().constEnd();
|
|
|
|
while (it != msgs.chunk().constBegin()) {
|
|
|
|
--it;
|
|
|
|
|
|
|
|
TimelineItem *item = parseMessageEvent((*it).toObject(), TimelineDirection::Top);
|
|
|
|
|
|
|
|
if (item != nullptr)
|
|
|
|
items.push_back(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse again to render them.
|
|
|
|
std::reverse(items.begin(), items.end());
|
|
|
|
|
2017-06-05 19:21:19 +03:00
|
|
|
oldPosition_ = scroll_area_->verticalScrollBar()->value();
|
|
|
|
oldHeight_ = scroll_widget_->size().height();
|
|
|
|
|
2017-08-05 15:59:24 +03:00
|
|
|
for (const auto &item : items)
|
2017-05-12 15:43:35 +03:00
|
|
|
addTimelineItem(item, TimelineDirection::Top);
|
2017-04-28 14:56:45 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
prev_batch_token_ = msgs.end();
|
|
|
|
isPaginationInProgress_ = false;
|
2017-06-05 19:21:19 +03:00
|
|
|
isPaginationScrollPending_ = true;
|
2017-05-12 15:43:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TimelineItem *TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection direction)
|
|
|
|
{
|
|
|
|
events::EventType ty = events::extractEventType(event);
|
|
|
|
|
|
|
|
if (ty == events::EventType::RoomMessage) {
|
|
|
|
events::MessageEventType msg_type = events::extractMessageEventType(event);
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
if (msg_type == events::MessageEventType::Text) {
|
|
|
|
events::MessageEvent<msgs::Text> text;
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
try {
|
|
|
|
text.deserialize(event);
|
|
|
|
} catch (const DeserializationException &e) {
|
|
|
|
qWarning() << e.what() << event;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-07-29 11:49:00 +03:00
|
|
|
if (isDuplicate(text.eventId()))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
eventIds_[text.eventId()] = true;
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
if (isPendingMessage(text, local_user_)) {
|
|
|
|
removePendingMessage(text);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
auto with_sender = isSenderRendered(text.sender(), direction);
|
|
|
|
updateLastSender(text.sender(), direction);
|
2017-04-28 14:56:45 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
auto color = TimelineViewManager::getUserColor(text.sender());
|
|
|
|
last_sender_ = text.sender();
|
2017-04-28 14:56:45 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
return createTimelineItem(text, color, with_sender);
|
|
|
|
} else if (msg_type == events::MessageEventType::Notice) {
|
|
|
|
events::MessageEvent<msgs::Notice> notice;
|
2017-04-28 14:56:45 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
try {
|
|
|
|
notice.deserialize(event);
|
|
|
|
} catch (const DeserializationException &e) {
|
|
|
|
qWarning() << e.what() << event;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-04-28 14:56:45 +03:00
|
|
|
|
2017-07-29 11:49:00 +03:00
|
|
|
if (isDuplicate(notice.eventId()))
|
|
|
|
return nullptr;
|
|
|
|
;
|
|
|
|
|
|
|
|
eventIds_[notice.eventId()] = true;
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
auto with_sender = isSenderRendered(notice.sender(), direction);
|
|
|
|
updateLastSender(notice.sender(), direction);
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
auto color = TimelineViewManager::getUserColor(notice.sender());
|
|
|
|
last_sender_ = notice.sender();
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
return createTimelineItem(notice, color, with_sender);
|
|
|
|
} else if (msg_type == events::MessageEventType::Image) {
|
|
|
|
events::MessageEvent<msgs::Image> img;
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
try {
|
|
|
|
img.deserialize(event);
|
|
|
|
} catch (const DeserializationException &e) {
|
|
|
|
qWarning() << e.what() << event;
|
|
|
|
return nullptr;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
2017-05-12 15:43:35 +03:00
|
|
|
|
2017-07-29 11:49:00 +03:00
|
|
|
if (isDuplicate(img.eventId()))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
eventIds_[img.eventId()] = true;
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
auto with_sender = isSenderRendered(img.sender(), direction);
|
|
|
|
updateLastSender(img.sender(), direction);
|
|
|
|
|
|
|
|
auto color = TimelineViewManager::getUserColor(img.sender());
|
|
|
|
last_sender_ = img.sender();
|
|
|
|
|
|
|
|
return createTimelineItem(img, color, with_sender);
|
|
|
|
} else if (msg_type == events::MessageEventType::Unknown) {
|
|
|
|
qWarning() << "Unknown message type" << event;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TimelineView::addEvents(const Timeline &timeline)
|
|
|
|
{
|
|
|
|
int message_count = 0;
|
|
|
|
|
2017-06-01 18:55:42 +03:00
|
|
|
QSettings settings;
|
|
|
|
QString localUser = settings.value("auth/user_id").toString();
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
for (const auto &event : timeline.events()) {
|
|
|
|
TimelineItem *item = parseMessageEvent(event.toObject(), TimelineDirection::Bottom);
|
2017-06-01 18:55:42 +03:00
|
|
|
auto sender = event.toObject().value("sender").toString();
|
2017-05-12 15:43:35 +03:00
|
|
|
|
|
|
|
if (item != nullptr) {
|
|
|
|
addTimelineItem(item, TimelineDirection::Bottom);
|
2017-06-01 18:55:42 +03:00
|
|
|
|
|
|
|
if (sender != localUser)
|
|
|
|
message_count += 1;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-15 02:56:04 +03:00
|
|
|
|
2017-06-05 19:54:45 +03:00
|
|
|
if (isInitialSync) {
|
|
|
|
prev_batch_token_ = timeline.previousBatch();
|
|
|
|
isInitialSync = false;
|
|
|
|
|
|
|
|
client_->messages(room_id_, prev_batch_token_);
|
|
|
|
}
|
|
|
|
|
2017-04-15 02:56:04 +03:00
|
|
|
return message_count;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
void TimelineView::init()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
|
|
|
top_layout_ = new QVBoxLayout(this);
|
|
|
|
top_layout_->setSpacing(0);
|
|
|
|
top_layout_->setMargin(0);
|
|
|
|
|
|
|
|
scroll_area_ = new QScrollArea(this);
|
|
|
|
scroll_area_->setWidgetResizable(true);
|
2017-04-10 18:49:24 +03:00
|
|
|
scroll_area_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-05-24 22:45:13 +03:00
|
|
|
scrollbar_ = new ScrollBar(scroll_area_);
|
|
|
|
scroll_area_->setVerticalScrollBar(scrollbar_);
|
|
|
|
|
2017-04-06 02:06:42 +03:00
|
|
|
scroll_widget_ = new QWidget();
|
|
|
|
|
|
|
|
scroll_layout_ = new QVBoxLayout();
|
|
|
|
scroll_layout_->addStretch(1);
|
|
|
|
scroll_layout_->setSpacing(0);
|
|
|
|
|
|
|
|
scroll_widget_->setLayout(scroll_layout_);
|
|
|
|
|
|
|
|
scroll_area_->setWidget(scroll_widget_);
|
|
|
|
|
|
|
|
top_layout_->addWidget(scroll_area_);
|
|
|
|
|
|
|
|
setLayout(top_layout_);
|
|
|
|
|
2017-08-05 15:59:24 +03:00
|
|
|
paginationTimer_ = new QTimer(this);
|
|
|
|
connect(paginationTimer_, &QTimer::timeout, this, &TimelineView::fetchHistory);
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
connect(client_.data(), &MatrixClient::messagesRetrieved, this, &TimelineView::addBackwardsEvents);
|
|
|
|
|
|
|
|
connect(scroll_area_->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(sliderMoved(int)));
|
|
|
|
connect(scroll_area_->verticalScrollBar(), SIGNAL(rangeChanged(int, int)), this, SLOT(sliderRangeChanged(int, int)));
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
void TimelineView::updateLastSender(const QString &user_id, TimelineDirection direction)
|
|
|
|
{
|
|
|
|
if (direction == TimelineDirection::Bottom)
|
|
|
|
last_sender_ = user_id;
|
|
|
|
else
|
|
|
|
last_sender_backwards_ = user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TimelineView::isSenderRendered(const QString &user_id, TimelineDirection direction)
|
|
|
|
{
|
|
|
|
if (direction == TimelineDirection::Bottom)
|
|
|
|
return last_sender_ != user_id;
|
|
|
|
else
|
|
|
|
return last_sender_backwards_ != user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimelineItem *TimelineView::createTimelineItem(const events::MessageEvent<msgs::Image> &event, const QString &color, bool with_sender)
|
2017-04-28 14:56:45 +03:00
|
|
|
{
|
2017-05-07 17:15:38 +03:00
|
|
|
auto image = new ImageItem(client_, event);
|
2017-04-28 14:56:45 +03:00
|
|
|
|
|
|
|
if (with_sender) {
|
|
|
|
auto item = new TimelineItem(image, event, color, scroll_widget_);
|
2017-05-12 15:43:35 +03:00
|
|
|
return item;
|
2017-04-28 14:56:45 +03:00
|
|
|
}
|
2017-05-12 15:43:35 +03:00
|
|
|
|
|
|
|
auto item = new TimelineItem(image, event, scroll_widget_);
|
|
|
|
return item;
|
2017-04-28 14:56:45 +03:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
TimelineItem *TimelineView::createTimelineItem(const events::MessageEvent<msgs::Notice> &event, const QString &color, bool with_sender)
|
2017-05-07 17:15:38 +03:00
|
|
|
{
|
|
|
|
TimelineItem *item = new TimelineItem(event, with_sender, color, scroll_widget_);
|
2017-05-12 15:43:35 +03:00
|
|
|
return item;
|
2017-05-07 17:15:38 +03:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:43:35 +03:00
|
|
|
TimelineItem *TimelineView::createTimelineItem(const events::MessageEvent<msgs::Text> &event, const QString &color, bool with_sender)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-04-25 14:37:54 +03:00
|
|
|
TimelineItem *item = new TimelineItem(event, with_sender, color, scroll_widget_);
|
2017-05-12 15:43:35 +03:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction)
|
|
|
|
{
|
|
|
|
if (direction == TimelineDirection::Bottom)
|
|
|
|
scroll_layout_->addWidget(item);
|
|
|
|
else
|
2017-07-30 14:02:15 +03:00
|
|
|
scroll_layout_->insertWidget(1, item);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
void TimelineView::updatePendingMessage(int txn_id, QString event_id)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
|
|
|
for (auto &msg : pending_msgs_) {
|
|
|
|
if (msg.txn_id == txn_id) {
|
|
|
|
msg.event_id = event_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
bool TimelineView::isPendingMessage(const events::MessageEvent<msgs::Text> &e, const QString &local_userid)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
2017-05-07 17:15:38 +03:00
|
|
|
if (e.sender() != local_userid)
|
2017-04-13 04:11:22 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const auto &msg : pending_msgs_) {
|
2017-05-07 17:15:38 +03:00
|
|
|
if (msg.event_id == e.eventId() || msg.body == e.content().body())
|
2017-04-13 04:11:22 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
void TimelineView::removePendingMessage(const events::MessageEvent<msgs::Text> &e)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
|
|
|
for (auto it = pending_msgs_.begin(); it != pending_msgs_.end(); it++) {
|
|
|
|
int index = std::distance(pending_msgs_.begin(), it);
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
if (it->event_id == e.eventId() || it->body == e.content().body()) {
|
2017-04-13 04:11:22 +03:00
|
|
|
pending_msgs_.removeAt(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
void TimelineView::addUserTextMessage(const QString &body, int txn_id)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
auto user_id = settings.value("auth/user_id").toString();
|
|
|
|
|
|
|
|
auto with_sender = last_sender_ != user_id;
|
2017-04-25 14:37:54 +03:00
|
|
|
auto color = TimelineViewManager::getUserColor(user_id);
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
TimelineItem *view_item;
|
2017-04-13 04:11:22 +03:00
|
|
|
|
|
|
|
if (with_sender)
|
2017-04-25 14:37:54 +03:00
|
|
|
view_item = new TimelineItem(user_id, color, body, scroll_widget_);
|
2017-04-13 04:11:22 +03:00
|
|
|
else
|
2017-04-25 14:37:54 +03:00
|
|
|
view_item = new TimelineItem(body, scroll_widget_);
|
2017-04-13 04:11:22 +03:00
|
|
|
|
|
|
|
scroll_layout_->addWidget(view_item);
|
|
|
|
|
|
|
|
last_sender_ = user_id;
|
|
|
|
|
2017-04-13 04:32:18 +03:00
|
|
|
PendingMessage message(txn_id, body, "", view_item);
|
2017-04-13 04:11:22 +03:00
|
|
|
|
|
|
|
pending_msgs_.push_back(message);
|
|
|
|
}
|