Commit 18f11b32 authored by scuq's avatar scuq

file upload

parent a99ccb53
Pipeline #396 failed with stage
in 3 minutes and 41 seconds
......@@ -14,6 +14,7 @@ dialogChat::dialogChat(QWidget *parent, ImSession *session, manipulator *manipul
longestUsernameLen = 0;
this->historicMsgPrintCount = 0;
clip = QApplication::clipboard();
this->session = session;
this->m_mp = manipulator;
this->m_contacts = this->session->getIMContacts();
......@@ -25,6 +26,7 @@ dialogChat::dialogChat(QWidget *parent, ImSession *session, manipulator *manipul
this->setChatDetails();
this->restoreWindowPositionState();
this->loadMessageHistrory();
this->initShortcuts();
this->setupSignals();
this->ui->plainTextEditChatInput->installEventFilter(this);
......@@ -51,6 +53,13 @@ void dialogChat::setChatDetails()
}
void dialogChat::initShortcuts()
{
m_UploadFile = new QShortcut(this);
m_UploadFile->setKey(QKeySequence(Qt::CTRL + Qt::Key_Right));
connect(m_UploadFile, SIGNAL(activated()), this, SLOT(slUploadFile()));
}
void dialogChat::loadMessageHistrory()
{
......@@ -272,9 +281,30 @@ bool dialogChat::eventFilter(QObject *obj, QEvent *event)
return true; // do not process this event further
}
}
}
}
if(event->type() == QKeyEvent::KeyPress) {
QKeyEvent * ke = static_cast<QKeyEvent*>(event);
if (ke->matches(QKeySequence::Paste)) {
qDebug() << "clip:";
qDebug() << this->clip->mimeData();
qDebug() << this->clip->text();
if (this->clip->image().sizeInBytes() > 0)
{
QByteArray arr;
QBuffer buffer(&arr);
buffer.open(QIODevice::WriteOnly);
this->clip->image().save(&buffer);
this->session->uploadFile(this->contact->getDirectChannelId(),"test",arr);
}
}
}
......@@ -764,6 +794,13 @@ void dialogChat::slOnBlinkTimerRefresh()
}
void dialogChat::slUploadFile()
{
qDebug() << "slUploadFile";
//this->session->uploadFile(this->contact->getDirectChannelId(),"test");
}
QColor dialogChat::getColorChatNick() const
{
return m_colorchatnick;
......@@ -791,7 +828,6 @@ void dialogChat::setColorChatLink(const QColor &colorchatlink)
style += " table { margin: 0px; padding: 0px; border-collapse: collapse; } ";
style += " * { margin: 0; padding: 0; } ";
style = style.arg(colorchatlink.red()).arg(colorchatlink.green()).arg(colorchatlink.blue());
this->ui->textBrowserChatOutput->document()->setDefaultStyleSheet(style);
......
......@@ -10,6 +10,8 @@
#include <QPainter>
#include <QCloseEvent>
#include <QMessageBox>
#include <QClipboard>
#include <QShortcut>
#include "imsession.h"
#include "imcontact.h"
#include "dialogfileviewer.h"
......@@ -73,6 +75,7 @@ private slots:
void on_textBrowserChatOutput_anchorClicked(const QUrl &arg1);
void slOnBlinkTimerRefresh();
void slUploadFile();
signals:
void siOnRead(QString channelid);
......@@ -90,7 +93,10 @@ private:
int historicMsgPrintCount;
QFont chatFont;
QMap<QString, ImContact*> m_contacts;
QShortcut *m_UploadFile;
QClipboard *clip;
void setChatDetails();
void initShortcuts();
void loadMessageHistrory();
void loadSettings();
void sendMessage(QString message);
......
......@@ -86,6 +86,9 @@
<height>40</height>
</size>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
......
......@@ -304,6 +304,63 @@ bool ImSession::newChannelPost(QString channelid, QString message)
}
}
bool ImSession::uploadFile(QString channelid, QString filename, QByteArray content)
{
QUrl url ( this->m_account->getServer() + QString("/api/v4/files") );
// QString boundary = QString("---------------------------%1_%2").arg(channelid,filename);
QNetworkRequest req ( url );
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"channel_id\""));
textPart.setBody(channelid.toUtf8());/* toto is the name I give to my file in the server */
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"files\"; filename=\"%1\"").arg(filename)));
QBuffer *contentbuffer = new QBuffer();
contentbuffer->setData(content);
contentbuffer->open(QIODevice::ReadOnly);
//QFile *file = new QFile("c:/aic_alt.png");
//file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(contentbuffer);
contentbuffer->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(textPart);
multiPart->append(filePart);
// req.setRawHeader("Content-Type", "multipart/form-data");
// req.setRawHeader("X-Requested-With", "XMLHttpRequest");
req.setRawHeader("Authorization", "Bearer "+this->getAuthtoken().toUtf8());
qDebug() << QString("Uploading file %1 to %2 for channel %3").arg(filename, url.toString(), channelid);
QNetworkReply *reply = m_WebCtrlProgress.post(req,multiPart);
multiPart->setParent(reply);
netReplies[reply] = QString("mmapi::uploadFile__%1").arg( channelid );
return true;
}
void ImSession::requestRemoteViewChannel(QString channelid)
{
if ( (this->getNetworkConnection()) && this->isAuthenticated() ) {
......@@ -429,6 +486,12 @@ void ImSession::setupSignals()
connect( &m_WebCtrl, SIGNAL (finished(QNetworkReply*)),
this, SLOT (parseNetworkResponse(QNetworkReply*))
);
connect( &m_WebCtrlProgress, SIGNAL (finished(QNetworkReply*)),
this, SLOT (parseNetworkResponse(QNetworkReply*))
);
connect( &m_WebCtrlProgress, SIGNAL (uploadProgress(qint64, qint64)),
this, SLOT (slUploadProgress(qint64, qint64))
);
connect( this, SIGNAL (siMmAuthenticationSuccessful(const QJsonDocument)),
this, SLOT (slOnMmAuthenticationSuccessful(const QJsonDocument))
......@@ -767,6 +830,12 @@ void ImSession::parseNetworkResponse(QNetworkReply *pReply)
QString _channelid = callingFuncName.split("__")[1];
emit siChannelPosted( QJsonDocument::fromJson(pReply->readAll()), _channelid );
}
if (callingFuncName.startsWith( "mmapi::uploadFile" )) {
qDebug() << "mmapi::uploadFile response!!!";
QString _channelid = callingFuncName.split("__")[1];
qDebug().noquote() << "getChannelPosts: " << QJsonDocument::fromJson( pReply->readAll() ).toJson(QJsonDocument::Indented);
//emit siChannelPosted( QJsonDocument::fromJson(pReply->readAll()), _channelid );
}
if (callingFuncName.startsWith( "mmapi::requestUnreadCount" )) {
//qDebug().noquote() << "getChannelPosts: " << QJsonDocument::fromJson( pReply->readAll() ).toJson(QJsonDocument::Indented);
QString _channelid = callingFuncName.split("__")[1];
......@@ -823,6 +892,10 @@ void ImSession::parseNetworkResponse(QNetworkReply *pReply)
if (callingFuncName == "mmapi::newChannelPost") {
qWarning() << pReply->readAll();
}
if (callingFuncName == "mmapi::uploadFile") {
qDebug() << "mmapi::uploadFile response err!!!";
qWarning() << pReply->readAll();
}
if (callingFuncName.startsWith( "mmapi::requestUnreadCount" )) {
qWarning() << pReply->readAll();
}
......@@ -1185,3 +1258,8 @@ void ImSession::slOnDisconnected()
qDebug() << "slOnDisconnected";
}
void ImSession::slUploadProgress(qint64 read, qint64 total)
{
qDebug() << read << " " << total;
}
......@@ -10,11 +10,14 @@
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QBuffer>
#include <QJsonValue>
#include <QThread>
#include <QEventLoop>
#include <QApplication>
#include <QMessageBox>
#include <QHttpMultiPart>
#include <QFile>
#include <QTimer>
#include "imcontact.h"
#include "account.h"
......@@ -43,6 +46,7 @@ public:
void getChannelsForUser();
void getChannelPosts(QString channelid);
bool newChannelPost(QString channelid, QString message);
bool uploadFile(QString channelid, QString filename, QByteArray content);
void requestRemoteViewChannel(QString channelid);
void getRemoteFile(QString fileid, bool preview);
bool isAuthenticated();
......@@ -77,6 +81,7 @@ public:
private:
void setupSignals();
QNetworkAccessManager m_WebCtrl;
QNetworkAccessManager m_WebCtrlProgress;
QWebSocket m_webSocket;
QEventLoop m_eventLoop;
QByteArray m_StatusData;
......@@ -155,6 +160,7 @@ private slots:
void slNetworkReplyTimeout(QString url);
void slOnConnected();
void slOnDisconnected();
void slUploadProgress(qint64 read, qint64 total);
};
#endif // IMSESSION_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment