Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Add support for pasting images into a room (#180)
Browse files Browse the repository at this point in the history
fixes #132
  • Loading branch information
christarazi authored and mujx committed Jan 10, 2018
1 parent 53f6700 commit ddfce13
Show file tree
Hide file tree
Showing 22 changed files with 388 additions and 73 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ set(SRC_FILES
# Dialogs
src/dialogs/CreateRoom.cc
src/dialogs/ImageOverlay.cc
src/dialogs/PreviewImageOverlay.cc
src/dialogs/InviteUsers.cc
src/dialogs/JoinRoom.cc
src/dialogs/LeaveRoom.cc
Expand Down Expand Up @@ -229,6 +230,7 @@ qt5_wrap_cpp(MOC_HEADERS
# Dialogs
include/dialogs/CreateRoom.h
include/dialogs/ImageOverlay.h
include/dialogs/PreviewImageOverlay.h
include/dialogs/InviteUsers.h
include/dialogs/JoinRoom.h
include/dialogs/LeaveRoom.h
Expand Down
19 changes: 14 additions & 5 deletions include/MatrixClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ class MatrixClient : public QNetworkAccessManager
void downloadImage(const QString &event_id, const QUrl &url);
void downloadFile(const QString &event_id, const QUrl &url);
void messages(const QString &room_id, const QString &from_token, int limit = 30) noexcept;
void uploadImage(const QString &roomid, const QString &filename);
void uploadFile(const QString &roomid, const QString &filename);
void uploadAudio(const QString &roomid, const QString &filename);
void uploadImage(const QString &roomid,
const QSharedPointer<QIODevice> data,
const QString &filename);
void uploadFile(const QString &roomid,
const QSharedPointer<QIODevice> data,
const QString &filename);
void uploadAudio(const QString &roomid,
const QSharedPointer<QIODevice> data,
const QString &filename);
void joinRoom(const QString &roomIdOrAlias);
void leaveRoom(const QString &roomId);
void sendTypingNotification(const QString &roomid, int timeoutInMillis = 20000);
Expand Down Expand Up @@ -98,7 +104,10 @@ public slots:
const QString &homeserver,
const QString &token);
void versionSuccess();
void imageUploaded(const QString &roomid, const QString &filename, const QString &url);
void imageUploaded(const QString &roomid,
const QSharedPointer<QIODevice> data,
const QString &filename,
const QString &url);
void fileUploaded(const QString &roomid, const QString &filename, const QString &url);
void audioUploaded(const QString &roomid, const QString &filename, const QString &url);

Expand Down Expand Up @@ -131,7 +140,7 @@ public slots:
void roomCreationFailed(const QString &msg);

private:
QNetworkReply *makeUploadRequest(const QString &filename);
QNetworkReply *makeUploadRequest(QSharedPointer<QIODevice> iodev);

// Client API prefix.
QString clientApiUrl_;
Expand Down
18 changes: 15 additions & 3 deletions include/TextInputWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
#include "FlatButton.h"
#include "LoadingIndicator.h"

#include "dialogs/PreviewImageOverlay.h"

#include "emoji/PickButton.h"

namespace dialogs {
class PreviewImageOverlay;
}

class FilteredTextEdit : public QTextEdit
{
Q_OBJECT
Expand All @@ -48,16 +54,22 @@ class FilteredTextEdit : public QTextEdit
void stoppedTyping();
void message(QString);
void command(QString name, QString args);
void image(const QSharedPointer<QIODevice> iodev, const QString &img_name);

protected:
void keyPressEvent(QKeyEvent *event) override;
bool canInsertFromMimeData(const QMimeData *source) const override;
void insertFromMimeData(const QMimeData *source) override;

private:
std::deque<QString> true_history_, working_history_;
size_t history_index_;
QTimer *typingTimer_;

dialogs::PreviewImageOverlay previewDialog_;

void textChanged();
void receiveImage(const QByteArray img, const QString &img_name);
void afterCompletion(int);
};

Expand All @@ -83,9 +95,9 @@ private slots:
void sendTextMessage(QString msg);
void sendEmoteMessage(QString msg);

void uploadImage(QString filename);
void uploadFile(QString filename);
void uploadAudio(QString filename);
void uploadImage(QSharedPointer<QIODevice> data, const QString &filename);
void uploadFile(QSharedPointer<QIODevice> data, const QString &filename);
void uploadAudio(QSharedPointer<QIODevice> data, const QString &filename);

void sendJoinRoomRequest(const QString &room);

Expand Down
57 changes: 57 additions & 0 deletions include/dialogs/PreviewImageOverlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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/>.
*/

#pragma once

#include <QLabel>
#include <QLineEdit>
#include <QPixmap>
#include <QWidget>

#include "FlatButton.h"

class QMimeData;

namespace dialogs {

class PreviewImageOverlay : public QWidget
{
Q_OBJECT
public:
PreviewImageOverlay(QWidget *parent = nullptr);

void setImageAndCreate(const QByteArray data, const QString &type);
void setImageAndCreate(const QString &path);

signals:
void confirmImageUpload(const QByteArray data, const QString &img_name);

private:
void init();

QPixmap image_;
QByteArray imageData_;
QString imagePath_;

QLabel titleLabel_;
QLabel imageLabel_;
QLineEdit imageName_;

FlatButton upload_;
FlatButton cancel_;
};
} // dialogs
10 changes: 7 additions & 3 deletions include/timeline/TimelineView.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ class TimelineView : public QWidget
void addUserMessage(mtx::events::MessageType ty, const QString &msg);

template<class Widget, mtx::events::MessageType MsgType>
void addUserMessage(const QString &url, const QString &filename);
void addUserMessage(const QString &url,
const QSharedPointer<QIODevice> data,
const QString &filename);
void updatePendingMessage(int txn_id, QString event_id);
void scrollDown();
void addDateSeparator(QDateTime datetime, int position);
Expand Down Expand Up @@ -216,11 +218,13 @@ private slots:

template<class Widget, mtx::events::MessageType MsgType>
void
TimelineView::addUserMessage(const QString &url, const QString &filename)
TimelineView::addUserMessage(const QString &url,
const QSharedPointer<QIODevice> data,
const QString &filename)
{
auto with_sender = lastSender_ != local_user_;

auto widget = new Widget(client_, url, filename, this);
auto widget = new Widget(client_, url, data, filename, this);

TimelineItem *view_item =
new TimelineItem(widget, local_user_, with_sender, scroll_widget_);
Expand Down
7 changes: 6 additions & 1 deletion include/timeline/TimelineViewManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <mtx.hpp>

class QFile;

class MatrixClient;
class RoomInfoListItem;
class TimelineView;
Expand Down Expand Up @@ -64,7 +66,10 @@ public slots:
void setHistoryView(const QString &room_id);
void queueTextMessage(const QString &msg);
void queueEmoteMessage(const QString &msg);
void queueImageMessage(const QString &roomid, const QString &filename, const QString &url);
void queueImageMessage(const QString &roomid,
const QSharedPointer<QIODevice> data,
const QString &filename,
const QString &url);
void queueFileMessage(const QString &roomid, const QString &filename, const QString &url);
void queueAudioMessage(const QString &roomid, const QString &filename, const QString &url);

Expand Down
1 change: 1 addition & 0 deletions include/timeline/widgets/AudioItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AudioItem : public QWidget

AudioItem(QSharedPointer<MatrixClient> client,
const QString &url,
const QSharedPointer<QIODevice> data,
const QString &filename,
QWidget *parent = nullptr);

Expand Down
1 change: 1 addition & 0 deletions include/timeline/widgets/FileItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class FileItem : public QWidget

FileItem(QSharedPointer<MatrixClient> client,
const QString &url,
const QSharedPointer<QIODevice> data,
const QString &filename,
QWidget *parent = nullptr);

Expand Down
1 change: 1 addition & 0 deletions include/timeline/widgets/ImageItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ImageItem : public QWidget

ImageItem(QSharedPointer<MatrixClient> client,
const QString &url,
const QSharedPointer<QIODevice> data,
const QString &filename,
QWidget *parent = nullptr);

Expand Down
1 change: 1 addition & 0 deletions include/timeline/widgets/VideoItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class VideoItem : public QWidget

VideoItem(QSharedPointer<MatrixClient> client,
const QString &url,
const QSharedPointer<QIODevice> data,
const QString &filename,
QWidget *parent = nullptr);

Expand Down
9 changes: 3 additions & 6 deletions resources/styles/nheko-dark.qss
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,13 @@ dialogs--LeaveRoom,
dialogs--CreateRoom,
dialogs--InviteUsers,
dialogs--ReadReceipts,
dialogs--JoinRoom {
background-color: #383c4a;
color: #caccd1;
}

QListWidget {
dialogs--JoinRoom,
dialogs--PreviewImageOverlay {
background-color: #383c4a;
color: #caccd1;
}

QListWidget,
WelcomePage,
LoginPage,
RegisterPage {
Expand Down
7 changes: 2 additions & 5 deletions resources/styles/nheko.qss
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,8 @@ dialogs--LeaveRoom,
dialogs--CreateRoom,
dialogs--InviteUsers,
dialogs--ReadReceipts,
dialogs--JoinRoom {
background-color: white;
color: #333;
}

dialogs--JoinRoom,
dialogs--PreviewImageOverlay,
QListWidget {
background-color: white;
color: #333;
Expand Down
31 changes: 20 additions & 11 deletions src/ChatPage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,27 +228,36 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client,
client_.data(),
&MatrixClient::joinRoom);

connect(text_input_, &TextInputWidget::uploadImage, this, [=](QString filename) {
client_->uploadImage(current_room_, filename);
});
connect(text_input_,
&TextInputWidget::uploadImage,
this,
[=](QSharedPointer<QIODevice> data, const QString &fn) {
client_->uploadImage(current_room_, data, fn);
});

connect(text_input_, &TextInputWidget::uploadFile, this, [=](QString filename) {
client_->uploadFile(current_room_, filename);
});
connect(text_input_,
&TextInputWidget::uploadFile,
this,
[=](QSharedPointer<QIODevice> data, const QString &fn) {
client_->uploadFile(current_room_, data, fn);
});

connect(text_input_, &TextInputWidget::uploadAudio, this, [=](QString filename) {
client_->uploadAudio(current_room_, filename);
});
connect(text_input_,
&TextInputWidget::uploadAudio,
this,
[=](QSharedPointer<QIODevice> data, const QString &fn) {
client_->uploadAudio(current_room_, data, fn);
});

connect(
client_.data(), &MatrixClient::roomCreationFailed, this, &ChatPage::showNotification);
connect(client_.data(), &MatrixClient::joinFailed, this, &ChatPage::showNotification);
connect(client_.data(),
&MatrixClient::imageUploaded,
this,
[=](QString roomid, QString filename, QString url) {
[=](QString roomid, QSharedPointer<QIODevice> data, QString filename, QString url) {
text_input_->hideUploadSpinner();
view_manager_->queueImageMessage(roomid, filename, url);
view_manager_->queueImageMessage(roomid, data, filename, url);
});
connect(client_.data(),
&MatrixClient::fileUploaded,
Expand Down
Loading

0 comments on commit ddfce13

Please sign in to comment.