Add copy link to room context menu (#1101)

This commit is contained in:
brausepulver 2022-06-24 01:44:50 +02:00 committed by GitHub
parent f189fbc98d
commit 09c646d3fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 12 deletions

View file

@ -148,13 +148,18 @@ Page {
onTriggered: TimelineManager.openLeaveRoomDialog(roomContextMenu.roomid) onTriggered: TimelineManager.openLeaveRoomDialog(roomContextMenu.roomid)
} }
Platform.MenuItem {
text: qsTr("Copy room link")
onTriggered: Rooms.copyLink(roomContextMenu.roomid)
}
Platform.MenuSeparator { Platform.MenuSeparator {
text: qsTr("Tag room as:") text: qsTr("Tag room as:")
} }
Instantiator { Instantiator {
model: Communities.tagsWithDefault model: Communities.tagsWithDefault
onObjectAdded: roomContextMenu.insertItem(index + 3, object) onObjectAdded: roomContextMenu.insertItem(index + 4, object)
onObjectRemoved: roomContextMenu.removeItem(object) onObjectRemoved: roomContextMenu.removeItem(object)
delegate: Platform.MenuItem { delegate: Platform.MenuItem {

View file

@ -5,6 +5,9 @@
#include "RoomlistModel.h" #include "RoomlistModel.h"
#include <QClipboard>
#include <QGuiApplication>
#include "Cache.h" #include "Cache.h"
#include "Cache_p.h" #include "Cache_p.h"
#include "ChatPage.h" #include "ChatPage.h"
@ -1081,6 +1084,14 @@ FilteredRoomlistModel::toggleTag(QString roomid, QString tag, bool on)
} }
} }
void
FilteredRoomlistModel::copyLink(QString roomid)
{
auto link = QStringLiteral("%1?%2").arg(TimelineModel::getBareRoomLink(roomid),
TimelineModel::getRoomVias(roomid));
QGuiApplication::clipboard()->setText(link);
}
void void
FilteredRoomlistModel::nextRoomWithActivity() FilteredRoomlistModel::nextRoomWithActivity()
{ {

View file

@ -178,6 +178,7 @@ public slots:
void declineInvite(QString roomid) { roomlistmodel->declineInvite(roomid); } void declineInvite(QString roomid) { roomlistmodel->declineInvite(roomid); }
void leave(QString roomid, QString reason = "") { roomlistmodel->leave(roomid, reason); } void leave(QString roomid, QString reason = "") { roomlistmodel->leave(roomid, reason); }
void toggleTag(QString roomid, QString tag, bool on); void toggleTag(QString roomid, QString tag, bool on);
void copyLink(QString roomid);
TimelineModel *currentRoom() const { return roomlistmodel->currentRoom(); } TimelineModel *currentRoom() const { return roomlistmodel->currentRoom(); }
RoomPreview currentRoomPreview() const { return roomlistmodel->currentRoomPreview(); } RoomPreview currentRoomPreview() const { return roomlistmodel->currentRoomPreview(); }

View file

@ -1752,13 +1752,11 @@ TimelineModel::requestKeyForEvent(const QString &id)
} }
} }
void QString
TimelineModel::copyLinkToEvent(const QString &eventId) const TimelineModel::getBareRoomLink(const QString &roomId)
{ {
QStringList vias;
auto alias = auto alias =
cache::client()->getStateEvent<mtx::events::state::CanonicalAlias>(room_id_.toStdString()); cache::client()->getStateEvent<mtx::events::state::CanonicalAlias>(roomId.toStdString());
QString room; QString room;
if (alias) { if (alias) {
room = QString::fromStdString(alias->content.alias); room = QString::fromStdString(alias->content.alias);
@ -1768,11 +1766,19 @@ TimelineModel::copyLinkToEvent(const QString &eventId) const
} }
if (room.isEmpty()) if (room.isEmpty())
room = room_id_; room = roomId;
return QStringLiteral("https://matrix.to/#/%1").arg(QString(QUrl::toPercentEncoding(room)));
}
QString
TimelineModel::getRoomVias(const QString &roomId)
{
QStringList vias;
vias.push_back(QStringLiteral("via=%1").arg(QString( vias.push_back(QStringLiteral("via=%1").arg(QString(
QUrl::toPercentEncoding(QString::fromStdString(http::client()->user_id().hostname()))))); QUrl::toPercentEncoding(QString::fromStdString(http::client()->user_id().hostname())))));
auto members = cache::getMembers(room_id_.toStdString(), 0, 100); auto members = cache::getMembers(roomId.toStdString(), 0, 100);
for (const auto &m : members) { for (const auto &m : members) {
if (vias.size() >= 4) if (vias.size() >= 4)
break; break;
@ -1785,11 +1791,16 @@ TimelineModel::copyLinkToEvent(const QString &eventId) const
vias.push_back(server); vias.push_back(server);
} }
auto link = QStringLiteral("https://matrix.to/#/%1/%2?%3") return vias.join("&");
.arg(QString(QUrl::toPercentEncoding(room)), }
QString(QUrl::toPercentEncoding(eventId)),
vias.join('&'));
void
TimelineModel::copyLinkToEvent(const QString &eventId) const
{
auto link = QStringLiteral("%1/%2?%3")
.arg(getBareRoomLink(room_id_),
QString(QUrl::toPercentEncoding(eventId)),
getRoomVias(room_id_));
QGuiApplication::clipboard()->setText(link); QGuiApplication::clipboard()->setText(link);
} }

View file

@ -249,6 +249,9 @@ public:
bool canFetchMore(const QModelIndex &) const override; bool canFetchMore(const QModelIndex &) const override;
void fetchMore(const QModelIndex &) override; void fetchMore(const QModelIndex &) override;
static QString getBareRoomLink(const QString &);
static QString getRoomVias(const QString &);
Q_INVOKABLE QString displayName(const QString &id) const; Q_INVOKABLE QString displayName(const QString &id) const;
Q_INVOKABLE QString avatarUrl(const QString &id) const; Q_INVOKABLE QString avatarUrl(const QString &id) const;
Q_INVOKABLE QString formatDateSeparator(QDate date) const; Q_INVOKABLE QString formatDateSeparator(QDate date) const;