2019-07-29 06:14:10 +03:00
|
|
|
#include <QTabWidget>
|
2019-07-17 05:36:55 +03:00
|
|
|
#include <QTimer>
|
|
|
|
|
2019-08-13 05:09:40 +03:00
|
|
|
#include "Cache.h"
|
|
|
|
#include "ChatPage.h"
|
2019-08-20 01:11:38 +03:00
|
|
|
#include "Logging.h"
|
2019-07-17 05:36:55 +03:00
|
|
|
#include "UserMentions.h"
|
|
|
|
#include "timeline/TimelineItem.h"
|
|
|
|
|
2019-08-10 06:34:44 +03:00
|
|
|
using namespace popups;
|
2019-07-17 05:36:55 +03:00
|
|
|
|
|
|
|
UserMentions::UserMentions(QWidget *parent)
|
2019-07-17 05:50:23 +03:00
|
|
|
: QWidget{parent}
|
2019-07-17 05:36:55 +03:00
|
|
|
{
|
2019-08-13 05:09:40 +03:00
|
|
|
setAttribute(Qt::WA_ShowWithoutActivating, true);
|
2019-08-20 01:11:38 +03:00
|
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
|
2019-08-13 05:09:40 +03:00
|
|
|
|
2019-07-29 06:14:10 +03:00
|
|
|
tab_layout_ = new QTabWidget(this);
|
|
|
|
|
2019-07-17 05:36:55 +03:00
|
|
|
top_layout_ = new QVBoxLayout(this);
|
|
|
|
top_layout_->setSpacing(0);
|
|
|
|
top_layout_->setMargin(0);
|
|
|
|
|
2019-07-29 06:14:10 +03:00
|
|
|
local_scroll_area_ = new QScrollArea(this);
|
|
|
|
local_scroll_area_->setWidgetResizable(true);
|
|
|
|
local_scroll_area_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
|
|
|
local_scroll_widget_ = new QWidget(this);
|
|
|
|
local_scroll_widget_->setObjectName("local_scroll_widget");
|
|
|
|
|
|
|
|
all_scroll_area_ = new QScrollArea(this);
|
|
|
|
all_scroll_area_->setWidgetResizable(true);
|
|
|
|
all_scroll_area_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2019-07-17 05:36:55 +03:00
|
|
|
|
2019-07-29 06:14:10 +03:00
|
|
|
all_scroll_widget_ = new QWidget(this);
|
|
|
|
all_scroll_widget_->setObjectName("all_scroll_widget");
|
2019-07-17 05:36:55 +03:00
|
|
|
|
|
|
|
// Height of the typing display.
|
|
|
|
QFont f;
|
|
|
|
f.setPointSizeF(f.pointSizeF() * 0.9);
|
|
|
|
const int bottomMargin = QFontMetrics(f).height() + 6;
|
|
|
|
|
2019-07-29 06:14:10 +03:00
|
|
|
local_scroll_layout_ = new QVBoxLayout(local_scroll_widget_);
|
|
|
|
local_scroll_layout_->setContentsMargins(4, 0, 15, bottomMargin);
|
|
|
|
local_scroll_layout_->setSpacing(0);
|
2019-08-13 05:09:40 +03:00
|
|
|
local_scroll_layout_->setObjectName("localscrollarea");
|
2019-07-17 05:36:55 +03:00
|
|
|
|
2019-07-29 06:14:10 +03:00
|
|
|
all_scroll_layout_ = new QVBoxLayout(all_scroll_widget_);
|
|
|
|
all_scroll_layout_->setContentsMargins(4, 0, 15, bottomMargin);
|
|
|
|
all_scroll_layout_->setSpacing(0);
|
2019-08-13 05:09:40 +03:00
|
|
|
all_scroll_layout_->setObjectName("allscrollarea");
|
2019-07-17 05:36:55 +03:00
|
|
|
|
2019-07-29 06:14:10 +03:00
|
|
|
local_scroll_area_->setWidget(local_scroll_widget_);
|
|
|
|
local_scroll_area_->setAlignment(Qt::AlignBottom);
|
|
|
|
|
|
|
|
all_scroll_area_->setWidget(all_scroll_widget_);
|
|
|
|
all_scroll_area_->setAlignment(Qt::AlignBottom);
|
|
|
|
|
|
|
|
tab_layout_->addTab(local_scroll_area_, tr("This Room"));
|
|
|
|
tab_layout_->addTab(all_scroll_area_, tr("All Rooms"));
|
|
|
|
top_layout_->addWidget(tab_layout_);
|
2019-07-17 05:36:55 +03:00
|
|
|
|
|
|
|
setLayout(top_layout_);
|
|
|
|
}
|
|
|
|
|
2019-08-10 06:34:44 +03:00
|
|
|
void
|
2019-08-13 05:09:40 +03:00
|
|
|
UserMentions::initializeMentions(const QMap<QString, mtx::responses::Notifications> ¬ifs)
|
2019-08-10 06:34:44 +03:00
|
|
|
{
|
2019-08-13 05:09:40 +03:00
|
|
|
nhlog::ui()->debug("Initializing " + std::to_string(notifs.size()) + " notifications.");
|
2019-08-20 01:11:38 +03:00
|
|
|
|
2019-08-13 05:09:40 +03:00
|
|
|
for (const auto &item : notifs) {
|
|
|
|
for (const auto notif : item.notifications) {
|
|
|
|
const auto event_id = QString::fromStdString(utils::event_id(notif.event));
|
|
|
|
|
|
|
|
try {
|
|
|
|
const auto room_id = QString::fromStdString(notif.room_id);
|
|
|
|
const auto user_id = utils::event_sender(notif.event);
|
|
|
|
const auto body = utils::event_body(notif.event);
|
|
|
|
|
|
|
|
pushItem(event_id,
|
|
|
|
user_id,
|
|
|
|
body,
|
|
|
|
room_id,
|
|
|
|
ChatPage::instance()->currentRoom());
|
|
|
|
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
nhlog::db()->warn("error while sending desktop notification: {}",
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UserMentions::showPopup()
|
|
|
|
{
|
2019-08-20 01:11:38 +03:00
|
|
|
for (auto widget : all_scroll_layout_->findChildren<QWidget *>()) {
|
|
|
|
delete widget;
|
|
|
|
}
|
|
|
|
for (auto widget : local_scroll_layout_->findChildren<QWidget *>()) {
|
|
|
|
delete widget;
|
|
|
|
}
|
|
|
|
|
2019-08-13 05:09:40 +03:00
|
|
|
auto notifs = cache::client()->getTimelineMentions();
|
|
|
|
|
|
|
|
initializeMentions(notifs);
|
|
|
|
show();
|
2019-08-10 06:34:44 +03:00
|
|
|
}
|
|
|
|
|
2019-07-17 05:36:55 +03:00
|
|
|
void
|
2019-07-17 05:50:23 +03:00
|
|
|
UserMentions::pushItem(const QString &event_id,
|
|
|
|
const QString &user_id,
|
|
|
|
const QString &body,
|
2019-07-29 06:14:10 +03:00
|
|
|
const QString &room_id,
|
|
|
|
const QString ¤t_room_id)
|
2019-07-17 05:50:23 +03:00
|
|
|
{
|
2019-07-29 06:14:10 +03:00
|
|
|
setUpdatesEnabled(false);
|
|
|
|
|
|
|
|
// Add to the 'all' section
|
2019-07-17 05:50:23 +03:00
|
|
|
TimelineItem *view_item = new TimelineItem(
|
2019-07-29 06:14:10 +03:00
|
|
|
mtx::events::MessageType::Text, user_id, body, true, room_id, all_scroll_widget_);
|
2019-07-17 05:50:23 +03:00
|
|
|
view_item->setEventId(event_id);
|
|
|
|
view_item->hide();
|
|
|
|
|
2019-07-29 06:14:10 +03:00
|
|
|
all_scroll_layout_->addWidget(view_item);
|
2019-07-17 05:50:23 +03:00
|
|
|
QTimer::singleShot(0, this, [view_item, this]() {
|
|
|
|
view_item->show();
|
|
|
|
view_item->adjustSize();
|
|
|
|
setUpdatesEnabled(true);
|
|
|
|
});
|
2019-07-29 06:14:10 +03:00
|
|
|
|
|
|
|
// if it matches the current room... add it to the current room as well.
|
|
|
|
if (QString::compare(room_id, current_room_id, Qt::CaseInsensitive) == 0) {
|
|
|
|
// Add to the 'local' section
|
|
|
|
TimelineItem *local_view_item = new TimelineItem(mtx::events::MessageType::Text,
|
|
|
|
user_id,
|
|
|
|
body,
|
|
|
|
true,
|
|
|
|
room_id,
|
|
|
|
local_scroll_widget_);
|
|
|
|
local_view_item->setEventId(event_id);
|
|
|
|
local_view_item->hide();
|
|
|
|
|
|
|
|
local_scroll_layout_->addWidget(local_view_item);
|
|
|
|
|
2019-07-29 22:37:21 +03:00
|
|
|
QTimer::singleShot(0, this, [local_view_item]() {
|
2019-07-29 06:14:10 +03:00
|
|
|
local_view_item->show();
|
|
|
|
local_view_item->adjustSize();
|
|
|
|
});
|
|
|
|
}
|
2019-07-17 05:36:55 +03:00
|
|
|
}
|