New attachment mechanism. Also add image from clipboard.

This commit is contained in:
Black Hat
2019-05-19 21:58:54 +08:00
parent ae5154fd35
commit 603cb33042
16 changed files with 279 additions and 67 deletions

View File

@@ -31,13 +31,9 @@
#include <QtNetwork/QAuthenticator>
#include <QtNetwork/QNetworkReply>
Controller::Controller(QObject* parent)
: QObject(parent), notificationsManager(this) {
Controller::Controller(QObject* parent) : QObject(parent) {
QApplication::setQuitOnLastWindowClosed(false);
connect(&notificationsManager, &NotificationsManager::notificationClicked,
this, &Controller::notificationClicked);
Connection::setRoomType<SpectralRoom>();
Connection::setUserType<SpectralUser>();
@@ -232,10 +228,6 @@ void Controller::createDirectChat(Connection* c, const QString& userID) {
});
}
void Controller::copyToClipboard(const QString& text) {
m_clipboard->setText(text);
}
void Controller::playAudio(QUrl localFile) {
QMediaPlayer* player = new QMediaPlayer;
player->setMedia(localFile);
@@ -243,16 +235,6 @@ void Controller::playAudio(QUrl localFile) {
connect(player, &QMediaPlayer::stateChanged, [=] { player->deleteLater(); });
}
void Controller::postNotification(const QString& roomId,
const QString& eventId,
const QString& roomName,
const QString& senderName,
const QString& text,
const QImage& icon) {
notificationsManager.postNotification(roomId, eventId, roomName, senderName,
text, icon);
}
int Controller::dpi() {
return SettingsGroup("Interface").value("dpi", 100).toInt();
}

View File

@@ -67,8 +67,6 @@ class Controller : public QObject {
}
private:
QClipboard* m_clipboard = QApplication::clipboard();
NotificationsManager notificationsManager;
QVector<Connection*> m_connections;
QPointer<Connection> m_connection;
@@ -99,14 +97,7 @@ class Controller : public QObject {
void joinRoom(Connection* c, const QString& alias);
void createRoom(Connection* c, const QString& name, const QString& topic);
void createDirectChat(Connection* c, const QString& userID);
void copyToClipboard(const QString& text);
void playAudio(QUrl localFile);
void postNotification(const QString& roomId,
const QString& eventId,
const QString& roomName,
const QString& senderName,
const QString& text,
const QImage& icon);
};
#endif // CONTROLLER_H

26
src/imageclipboard.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "imageclipboard.h"
#include <QGuiApplication>
#include <QUrl>
ImageClipboard::ImageClipboard(QObject* parent)
: QObject(parent), m_clipboard(QGuiApplication::clipboard()) {
connect(m_clipboard, &QClipboard::changed, this,
&ImageClipboard::imageChanged);
}
bool ImageClipboard::hasImage() {
return !image().isNull();
}
QImage ImageClipboard::image() {
return m_clipboard->image();
}
void ImageClipboard::saveImage(const QUrl& localPath) {
auto i = image();
if (i.isNull()) return;
i.save(localPath.toString());
}

29
src/imageclipboard.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef IMAGECLIPBOARD_H
#define IMAGECLIPBOARD_H
#include <QClipboard>
#include <QImage>
#include <QObject>
class ImageClipboard : public QObject {
Q_OBJECT
Q_PROPERTY(bool hasImage READ hasImage NOTIFY imageChanged)
Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
public:
explicit ImageClipboard(QObject* parent = nullptr);
bool hasImage();
QImage image();
private:
QClipboard* m_clipboard;
signals:
void imageChanged();
public slots:
void saveImage(const QUrl& localPath);
};
#endif // IMAGECLIPBOARD_H

View File

@@ -8,8 +8,10 @@
#include "accountlistmodel.h"
#include "controller.h"
#include "emojimodel.h"
#include "imageprovider.h"
#include "imageclipboard.h"
#include "matriximageprovider.h"
#include "messageeventmodel.h"
#include "notifications/manager.h"
#include "room.h"
#include "roomlistmodel.h"
#include "spectralroom.h"
@@ -55,6 +57,9 @@ int main(int argc, char* argv[]) {
qmlRegisterType<UserListModel>("Spectral", 0, 1, "UserListModel");
qmlRegisterType<MessageEventModel>("Spectral", 0, 1, "MessageEventModel");
qmlRegisterType<EmojiModel>("Spectral", 0, 1, "EmojiModel");
qmlRegisterType<NotificationsManager>("Spectral", 0, 1,
"NotificationsManager");
qmlRegisterType<ImageClipboard>("Spectral", 0, 1, "ImageClipboard");
qmlRegisterUncreatableType<RoomMessageEvent>("Spectral", 0, 1,
"RoomMessageEvent", "ENUM");
qmlRegisterUncreatableType<RoomType>("Spectral", 0, 1, "RoomType", "ENUM");
@@ -72,9 +77,10 @@ int main(int argc, char* argv[]) {
QQmlApplicationEngine engine;
engine.addImportPath("qrc:/imports");
ImageProvider* m_provider = new ImageProvider();
engine.rootContext()->setContextProperty("imageProvider", m_provider);
engine.addImageProvider(QLatin1String("mxc"), m_provider);
MatrixImageProvider* matrixImageProvider = new MatrixImageProvider();
engine.rootContext()->setContextProperty("imageProvider",
matrixImageProvider);
engine.addImageProvider(QLatin1String("mxc"), matrixImageProvider);
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
if (engine.rootObjects().isEmpty())

View File

@@ -1,4 +1,4 @@
#include "imageprovider.h"
#include "matriximageprovider.h"
#include <QDir>
#include <QFileInfo>
@@ -108,7 +108,7 @@ void ThumbnailResponse::cancel() {
Qt::QueuedConnection);
}
QQuickImageResponse* ImageProvider::requestImageResponse(
QQuickImageResponse* MatrixImageProvider::requestImageResponse(
const QString& id,
const QSize& requestedSize) {
return new ThumbnailResponse(m_connection.load(), id, requestedSize);

View File

@@ -1,5 +1,5 @@
#ifndef IMAGEPROVIDER_H
#define IMAGEPROVIDER_H
#ifndef MatrixImageProvider_H
#define MatrixImageProvider_H
#pragma once
#include <QtQuick/QQuickAsyncImageProvider>
@@ -43,12 +43,12 @@ class ThumbnailResponse : public QQuickImageResponse {
void cancel() override;
};
class ImageProvider : public QObject, public QQuickAsyncImageProvider {
class MatrixImageProvider : public QObject, public QQuickAsyncImageProvider {
Q_OBJECT
Q_PROPERTY(QMatrixClient::Connection* connection READ connection WRITE
setConnection NOTIFY connectionChanged)
public:
explicit ImageProvider() = default;
explicit MatrixImageProvider() = default;
QQuickImageResponse* requestImageResponse(
const QString& id,
@@ -67,4 +67,4 @@ class ImageProvider : public QObject, public QQuickAsyncImageProvider {
QAtomicPointer<QMatrixClient::Connection> m_connection;
};
#endif // IMAGEPROVIDER_H
#endif // MatrixImageProvider_H

View File

@@ -19,11 +19,7 @@ struct roomEventId {
class NotificationsManager : public QObject {
Q_OBJECT
public:
NotificationsManager(QObject *parent = nullptr);
void postNotification(const QString &roomId, const QString &eventId,
const QString &roomName, const QString &senderName,
const QString &text, const QImage &icon);
NotificationsManager(QObject* parent = nullptr);
signals:
void notificationClicked(const QString roomId, const QString eventId);
@@ -31,7 +27,8 @@ class NotificationsManager : public QObject {
private:
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
QDBusInterface dbus;
uint showNotification(const QString summary, const QString text,
uint showNotification(const QString summary,
const QString text,
const QImage image);
#endif
@@ -43,9 +40,16 @@ class NotificationsManager : public QObject {
public slots:
void actionInvoked(uint id, QString action);
void notificationClosed(uint id, uint reason);
void postNotification(const QString& roomId,
const QString& eventId,
const QString& roomName,
const QString& senderName,
const QString& text,
const QImage& icon);
};
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
QDBusArgument &operator<<(QDBusArgument &arg, const QImage &image);
const QDBusArgument &operator>>(const QDBusArgument &arg, QImage &);
QDBusArgument& operator<<(QDBusArgument& arg, const QImage& image);
const QDBusArgument& operator>>(const QDBusArgument& arg, QImage&);
#endif

View File

@@ -44,18 +44,11 @@ inline QSize getImageSize(const QUrl& imageUrl) {
return reader.size();
}
void SpectralRoom::chooseAndUploadFile() {
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
if (!localFile.isEmpty()) {
uploadFile(localFile);
}
}
void SpectralRoom::uploadFile(const QUrl& url) {
void SpectralRoom::uploadFile(const QUrl& url, const QString& body) {
if (url.isEmpty())
return;
QString txnID = postFile(url.fileName(), url, false);
QString txnID = postFile(body.isEmpty() ? url.fileName() : body, url, false);
setHasFileUploading(true);
connect(this, &Room::fileTransferCompleted,
[=](QString id, QUrl localFile, QUrl mxcUrl) {

View File

@@ -254,8 +254,7 @@ class SpectralRoom : public Room {
void fileUploadingProgressChanged();
public slots:
void chooseAndUploadFile();
void uploadFile(const QUrl& url);
void uploadFile(const QUrl& url, const QString& body = "");
void acceptInvitation();
void forget();
void sendTypingNotification(bool isTyping);