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/>.
|
|
|
|
*/
|
|
|
|
|
2017-04-11 22:48:02 +03:00
|
|
|
#include <random>
|
|
|
|
|
2017-04-16 22:34:57 +03:00
|
|
|
#include <QApplication>
|
2017-04-06 02:06:42 +03:00
|
|
|
#include <QDebug>
|
2017-09-10 12:58:00 +03:00
|
|
|
#include <QFileInfo>
|
2017-04-13 04:11:22 +03:00
|
|
|
#include <QSettings>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "MatrixClient.h"
|
2017-11-30 14:53:28 +03:00
|
|
|
|
|
|
|
#include "timeline/TimelineView.h"
|
|
|
|
#include "timeline/TimelineViewManager.h"
|
2017-12-01 18:33:49 +03:00
|
|
|
#include "timeline/widgets/AudioItem.h"
|
2017-11-30 14:53:28 +03:00
|
|
|
#include "timeline/widgets/FileItem.h"
|
|
|
|
#include "timeline/widgets/ImageItem.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
TimelineViewManager::TimelineViewManager(QSharedPointer<MatrixClient> client, QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QStackedWidget(parent)
|
|
|
|
, client_(client)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-11-09 00:09:15 +03:00
|
|
|
setStyleSheet("border: none;");
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-09-03 11:43:45 +03:00
|
|
|
connect(
|
|
|
|
client_.data(), &MatrixClient::messageSent, this, &TimelineViewManager::messageSent);
|
2017-11-15 19:38:50 +03:00
|
|
|
|
2017-11-15 19:42:21 +03:00
|
|
|
connect(client_.data(),
|
|
|
|
&MatrixClient::messageSendFailed,
|
|
|
|
this,
|
|
|
|
&TimelineViewManager::messageSendFailed);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TimelineViewManager::messageSent(const QString &event_id, const QString &roomid, int txn_id)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
// We save the latest valid transaction ID for later use.
|
|
|
|
QSettings settings;
|
|
|
|
settings.setValue("client/transaction_id", txn_id + 1);
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
auto view = views_[roomid];
|
|
|
|
view->updatePendingMessage(txn_id, event_id);
|
2017-04-13 04:11:22 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-11-15 19:38:50 +03:00
|
|
|
TimelineViewManager::messageSendFailed(const QString &roomid, int txn_id)
|
|
|
|
{
|
|
|
|
auto view = views_[roomid];
|
|
|
|
view->handleFailedMessage(txn_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimelineViewManager::queueTextMessage(const QString &msg)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
2018-02-02 20:50:02 +03:00
|
|
|
if (active_room_.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
auto room_id = active_room_;
|
|
|
|
auto view = views_[room_id];
|
2017-04-13 04:11:22 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
view->addUserMessage(mtx::events::MessageType::Text, msg);
|
2017-09-03 11:43:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-15 19:38:50 +03:00
|
|
|
TimelineViewManager::queueEmoteMessage(const QString &msg)
|
2017-09-03 11:43:45 +03:00
|
|
|
{
|
2018-02-02 20:50:02 +03:00
|
|
|
if (active_room_.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2017-09-03 11:43:45 +03:00
|
|
|
auto room_id = active_room_;
|
|
|
|
auto view = views_[room_id];
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
view->addUserMessage(mtx::events::MessageType::Emote, msg);
|
2017-04-13 04:11:22 +03:00
|
|
|
}
|
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
void
|
2017-11-15 19:38:50 +03:00
|
|
|
TimelineViewManager::queueImageMessage(const QString &roomid,
|
2018-01-10 10:52:59 +03:00
|
|
|
const QSharedPointer<QIODevice> data,
|
2017-11-15 19:42:21 +03:00
|
|
|
const QString &filename,
|
|
|
|
const QString &url)
|
2017-09-10 12:58:00 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!timelineViewExists(roomid)) {
|
2017-09-10 12:58:00 +03:00
|
|
|
qDebug() << "Cannot send m.image message to a non-managed view";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto view = views_[roomid];
|
|
|
|
|
2018-01-12 12:27:24 +03:00
|
|
|
view->addUserMessage<ImageItem, mtx::events::MessageType::Image>(url, filename, data);
|
2017-11-30 00:39:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimelineViewManager::queueFileMessage(const QString &roomid,
|
|
|
|
const QString &filename,
|
|
|
|
const QString &url)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!timelineViewExists(roomid)) {
|
2017-11-30 00:39:35 +03:00
|
|
|
qDebug() << "Cannot send m.file message to a non-managed view";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto view = views_[roomid];
|
|
|
|
|
2018-01-12 12:27:24 +03:00
|
|
|
view->addUserMessage<FileItem, mtx::events::MessageType::File>(url, filename);
|
2017-09-10 12:58:00 +03:00
|
|
|
}
|
|
|
|
|
2017-12-01 18:33:49 +03:00
|
|
|
void
|
|
|
|
TimelineViewManager::queueAudioMessage(const QString &roomid,
|
|
|
|
const QString &filename,
|
|
|
|
const QString &url)
|
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!timelineViewExists(roomid)) {
|
2017-12-01 18:33:49 +03:00
|
|
|
qDebug() << "Cannot send m.audio message to a non-managed view";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto view = views_[roomid];
|
|
|
|
|
2018-01-12 12:27:24 +03:00
|
|
|
view->addUserMessage<AudioItem, mtx::events::MessageType::Audio>(url, filename);
|
2017-12-01 18:33:49 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-12-04 19:41:19 +03:00
|
|
|
TimelineViewManager::initialize(const mtx::responses::Rooms &rooms)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
for (auto it = rooms.join.cbegin(); it != rooms.join.cend(); ++it) {
|
|
|
|
addRoom(it->second, QString::fromStdString(it->first));
|
2017-08-26 11:33:26 +03:00
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-01-24 21:46:37 +03:00
|
|
|
TimelineViewManager::initialize(const std::vector<QString> &rooms)
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
for (const auto &roomid : rooms)
|
2017-10-01 19:49:36 +03:00
|
|
|
addRoom(roomid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-04 19:41:19 +03:00
|
|
|
TimelineViewManager::addRoom(const mtx::responses::JoinedRoom &room, const QString &room_id)
|
2017-10-01 19:49:36 +03:00
|
|
|
{
|
|
|
|
// Create a history view with the room events.
|
2017-12-04 19:41:19 +03:00
|
|
|
TimelineView *view = new TimelineView(room.timeline, client_, room_id);
|
2018-01-24 21:46:37 +03:00
|
|
|
views_.emplace(room_id, QSharedPointer<TimelineView>(view));
|
2017-08-26 11:33:26 +03:00
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
connect(view,
|
|
|
|
&TimelineView::updateLastTimelineMessage,
|
|
|
|
this,
|
|
|
|
&TimelineViewManager::updateRoomsLastMessage);
|
2017-11-24 01:10:58 +03:00
|
|
|
connect(view,
|
|
|
|
&TimelineView::clearUnreadMessageCount,
|
|
|
|
this,
|
|
|
|
&TimelineViewManager::clearRoomMessageCount);
|
2017-08-26 11:33:26 +03:00
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
// Add the view in the widget stack.
|
|
|
|
addWidget(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimelineViewManager::addRoom(const QString &room_id)
|
|
|
|
{
|
|
|
|
// Create a history view without any events.
|
|
|
|
TimelineView *view = new TimelineView(client_, room_id);
|
2018-01-24 21:46:37 +03:00
|
|
|
views_.emplace(room_id, QSharedPointer<TimelineView>(view));
|
2017-10-01 19:49:36 +03:00
|
|
|
|
|
|
|
connect(view,
|
|
|
|
&TimelineView::updateLastTimelineMessage,
|
|
|
|
this,
|
|
|
|
&TimelineViewManager::updateRoomsLastMessage);
|
2017-11-24 01:10:58 +03:00
|
|
|
connect(view,
|
|
|
|
&TimelineView::clearUnreadMessageCount,
|
|
|
|
this,
|
|
|
|
&TimelineViewManager::clearRoomMessageCount);
|
2017-10-01 19:49:36 +03:00
|
|
|
|
|
|
|
// Add the view in the widget stack.
|
|
|
|
addWidget(view);
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-12-04 19:41:19 +03:00
|
|
|
TimelineViewManager::sync(const mtx::responses::Rooms &rooms)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
for (auto it = rooms.join.cbegin(); it != rooms.join.cend(); ++it) {
|
|
|
|
auto roomid = QString::fromStdString(it->first);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!timelineViewExists(roomid)) {
|
2017-08-26 11:33:26 +03:00
|
|
|
qDebug() << "Ignoring event from unknown room" << roomid;
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
auto view = views_.at(roomid);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
int msgs_added = view->addEvents(it->second.timeline);
|
2017-04-15 02:56:04 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
if (msgs_added > 0) {
|
|
|
|
// TODO: When the app window gets active the current
|
|
|
|
// unread count (if any) should be cleared.
|
|
|
|
auto isAppActive = QApplication::activeWindow() != nullptr;
|
2017-04-16 22:34:57 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
if (roomid != active_room_ || !isAppActive)
|
|
|
|
emit unreadMessages(roomid, msgs_added);
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
TimelineViewManager::setHistoryView(const QString &room_id)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!timelineViewExists(room_id)) {
|
2017-08-26 11:33:26 +03:00
|
|
|
qDebug() << "Room ID from RoomList is not present in ViewManager" << room_id;
|
|
|
|
return;
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
active_room_ = room_id;
|
2018-01-24 21:46:37 +03:00
|
|
|
auto view = views_.at(room_id);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
setCurrentWidget(view.data());
|
2017-06-05 19:54:45 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
view->fetchHistory();
|
|
|
|
view->scrollDown();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
2017-04-11 22:48:02 +03:00
|
|
|
|
2018-01-24 21:46:37 +03:00
|
|
|
std::map<QString, QString> TimelineViewManager::DISPLAY_NAMES;
|
2017-04-11 22:48:02 +03:00
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QString
|
|
|
|
TimelineViewManager::chooseRandomColor()
|
2017-04-11 22:48:02 +03:00
|
|
|
{
|
2017-08-26 11:33:26 +03:00
|
|
|
std::random_device random_device;
|
2017-11-06 00:04:55 +03:00
|
|
|
std::mt19937 engine{random_device()};
|
2017-08-26 11:33:26 +03:00
|
|
|
std::uniform_real_distribution<float> dist(0, 1);
|
|
|
|
|
|
|
|
float hue = dist(engine);
|
|
|
|
float saturation = 0.9;
|
|
|
|
float value = 0.7;
|
|
|
|
|
|
|
|
int hue_i = hue * 6;
|
|
|
|
|
|
|
|
float f = hue * 6 - hue_i;
|
|
|
|
|
|
|
|
float p = value * (1 - saturation);
|
|
|
|
float q = value * (1 - f * saturation);
|
|
|
|
float t = value * (1 - (1 - f) * saturation);
|
|
|
|
|
|
|
|
float r = 0;
|
|
|
|
float g = 0;
|
|
|
|
float b = 0;
|
|
|
|
|
|
|
|
if (hue_i == 0) {
|
|
|
|
r = value;
|
|
|
|
g = t;
|
|
|
|
b = p;
|
|
|
|
} else if (hue_i == 1) {
|
|
|
|
r = q;
|
|
|
|
g = value;
|
|
|
|
b = p;
|
|
|
|
} else if (hue_i == 2) {
|
|
|
|
r = p;
|
|
|
|
g = value;
|
|
|
|
b = t;
|
|
|
|
} else if (hue_i == 3) {
|
|
|
|
r = p;
|
|
|
|
g = q;
|
|
|
|
b = value;
|
|
|
|
} else if (hue_i == 4) {
|
|
|
|
r = t;
|
|
|
|
g = p;
|
|
|
|
b = value;
|
|
|
|
} else if (hue_i == 5) {
|
|
|
|
r = value;
|
|
|
|
g = p;
|
|
|
|
b = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ri = r * 256;
|
|
|
|
int gi = g * 256;
|
|
|
|
int bi = b * 256;
|
|
|
|
|
|
|
|
QColor color(ri, gi, bi);
|
|
|
|
|
|
|
|
return color.name();
|
2017-04-13 04:11:22 +03:00
|
|
|
}
|
2017-05-08 19:44:01 +03:00
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QString
|
|
|
|
TimelineViewManager::displayName(const QString &userid)
|
2017-05-08 19:44:01 +03:00
|
|
|
{
|
2018-01-24 21:46:37 +03:00
|
|
|
if (DISPLAY_NAMES.find(userid) != DISPLAY_NAMES.end())
|
|
|
|
return DISPLAY_NAMES.at(userid);
|
2017-05-08 19:44:01 +03:00
|
|
|
|
2017-08-26 11:33:26 +03:00
|
|
|
return userid;
|
2017-05-08 19:44:01 +03:00
|
|
|
}
|
2017-10-07 20:50:32 +03:00
|
|
|
|
|
|
|
bool
|
|
|
|
TimelineViewManager::hasLoaded() const
|
|
|
|
{
|
|
|
|
for (const auto &view : views_)
|
2018-01-24 21:46:37 +03:00
|
|
|
if (!view.second->hasLoaded())
|
2017-10-07 20:50:32 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|