mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 04:57:23 +02:00
Posting Image to forum
This commit is contained in:
parent
cec3ce78b9
commit
d93480458f
3 changed files with 102 additions and 48 deletions
|
@ -3663,7 +3663,7 @@ void Application::takeSnapshot() {
|
||||||
if (!_snapshotShareDialog) {
|
if (!_snapshotShareDialog) {
|
||||||
_snapshotShareDialog = new SnapshotShareDialog(fileName, _glWidget);
|
_snapshotShareDialog = new SnapshotShareDialog(fileName, _glWidget);
|
||||||
}
|
}
|
||||||
_snapshotShareDialog->exec();
|
_snapshotShareDialog->show();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::urlGoTo(int argc, const char * constArgv[]) {
|
void Application::urlGoTo(int argc, const char * constArgv[]) {
|
||||||
|
|
|
@ -1,28 +1,41 @@
|
||||||
//
|
//
|
||||||
// SnapshotShareDialog.cpp
|
// SnapshotShareDialog.cpp
|
||||||
// hifi
|
// interface/src/ui
|
||||||
//
|
//
|
||||||
// Created by Stojce Slavkovski on 2/16/14.
|
// Created by Stojce Slavkovski on 2/16/14.
|
||||||
|
// Copyright 2014 High Fidelity, Inc.
|
||||||
//
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "SnapshotShareDialog.h"
|
#include "SnapshotShareDialog.h"
|
||||||
#include "AccountManager.h"
|
#include "AccountManager.h"
|
||||||
|
|
||||||
#include <QString>
|
#include <QFile>
|
||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
#include <QtNetwork/QNetworkRequest>
|
|
||||||
#include <QHttpMultiPart>
|
#include <QHttpMultiPart>
|
||||||
|
|
||||||
const int NARROW_SNAPSHOT_DIALOG_SIZE = 500;
|
const int NARROW_SNAPSHOT_DIALOG_SIZE = 500;
|
||||||
const int WIDE_SNAPSHOT_DIALOG_WIDTH = 650;
|
const int WIDE_SNAPSHOT_DIALOG_WIDTH = 650;
|
||||||
const QString FORUM_POST_URL = "http://localhost:4000";
|
|
||||||
|
|
||||||
SnapshotShareDialog::SnapshotShareDialog(QString fileName, QWidget* parent) : QDialog(parent), _fileName(fileName) {
|
const QString FORUM_URL = "http://localhost:4000";
|
||||||
|
const QString FORUM_UPLOADS_URL = FORUM_URL + "/uploads";
|
||||||
|
const QString FORUM_POST_URL = FORUM_URL + "/posts";
|
||||||
|
const QString FORUM_REPLY_TO_TOPIC = "64";
|
||||||
|
const QString FORUM_POST_TEMPLATE = "<img src='%1'/><p>%2</p>";
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(QNetworkAccessManager::Operation)
|
||||||
|
|
||||||
|
SnapshotShareDialog::SnapshotShareDialog(QString fileName, QWidget* parent) :
|
||||||
|
QDialog(parent),
|
||||||
|
_fileName(fileName),
|
||||||
|
_networkAccessManager(NULL)
|
||||||
|
{
|
||||||
|
|
||||||
setAttribute(Qt::WA_DeleteOnClose);
|
setAttribute(Qt::WA_DeleteOnClose);
|
||||||
|
|
||||||
ui.setupUi(this);
|
_ui.setupUi(this);
|
||||||
|
|
||||||
QPixmap snaphsotPixmap(fileName);
|
QPixmap snaphsotPixmap(fileName);
|
||||||
float snapshotRatio = static_cast<float>(snaphsotPixmap.size().width()) / snaphsotPixmap.size().height();
|
float snapshotRatio = static_cast<float>(snaphsotPixmap.size().width()) / snaphsotPixmap.size().height();
|
||||||
|
@ -30,70 +43,104 @@ SnapshotShareDialog::SnapshotShareDialog(QString fileName, QWidget* parent) : QD
|
||||||
// narrow snapshot
|
// narrow snapshot
|
||||||
if (snapshotRatio > 1) {
|
if (snapshotRatio > 1) {
|
||||||
setFixedWidth(WIDE_SNAPSHOT_DIALOG_WIDTH);
|
setFixedWidth(WIDE_SNAPSHOT_DIALOG_WIDTH);
|
||||||
ui.snapshotWidget->setFixedWidth(WIDE_SNAPSHOT_DIALOG_WIDTH);
|
_ui.snapshotWidget->setFixedWidth(WIDE_SNAPSHOT_DIALOG_WIDTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
float labelRatio = static_cast<float>(ui.snapshotWidget->size().width()) / ui.snapshotWidget->size().height();
|
float labelRatio = static_cast<float>(_ui.snapshotWidget->size().width()) / _ui.snapshotWidget->size().height();
|
||||||
|
|
||||||
// set the same aspect ratio of label as of snapshot
|
// set the same aspect ratio of label as of snapshot
|
||||||
if (snapshotRatio > labelRatio) {
|
if (snapshotRatio > labelRatio) {
|
||||||
int oldHeight = ui.snapshotWidget->size().height();
|
int oldHeight = _ui.snapshotWidget->size().height();
|
||||||
ui.snapshotWidget->setFixedHeight(ui.snapshotWidget->size().width() / snapshotRatio);
|
_ui.snapshotWidget->setFixedHeight((int) (_ui.snapshotWidget->size().width() / snapshotRatio));
|
||||||
|
|
||||||
// if height is less then original, resize the window as well
|
// if height is less then original, resize the window as well
|
||||||
if (ui.snapshotWidget->size().height() < NARROW_SNAPSHOT_DIALOG_SIZE) {
|
if (_ui.snapshotWidget->size().height() < NARROW_SNAPSHOT_DIALOG_SIZE) {
|
||||||
setFixedHeight(size().height() - (oldHeight - ui.snapshotWidget->size().height()));
|
setFixedHeight(size().height() - (oldHeight - _ui.snapshotWidget->size().height()));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ui.snapshotWidget->setFixedWidth(ui.snapshotWidget->size().height() * snapshotRatio);
|
_ui.snapshotWidget->setFixedWidth((int) (_ui.snapshotWidget->size().height() * snapshotRatio));
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.snapshotWidget->setPixmap(snaphsotPixmap);
|
_ui.snapshotWidget->setPixmap(snaphsotPixmap);
|
||||||
ui.snapshotWidget->adjustSize();
|
_ui.snapshotWidget->adjustSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnapshotShareDialog::accept() {
|
void SnapshotShareDialog::accept() {
|
||||||
|
uploadSnapshot();
|
||||||
|
sendForumPost("/uploads/default/25/b607c8faea6de9c3.jpg");
|
||||||
close();
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
// post to Discourse forum
|
void SnapshotShareDialog::uploadSnapshot() {
|
||||||
// AccountManager& accountManager = AccountManager::getInstance();
|
|
||||||
QNetworkAccessManager* _networkAccessManager = NULL;
|
|
||||||
|
|
||||||
if (!_networkAccessManager) {
|
if (!_networkAccessManager) {
|
||||||
_networkAccessManager = new QNetworkAccessManager(this);
|
_networkAccessManager = new QNetworkAccessManager(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
QNetworkRequest request;
|
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||||||
|
|
||||||
QUrl grantURL(FORUM_POST_URL);
|
QHttpPart apiKeyPart;
|
||||||
grantURL.setPath("/posts");
|
apiKeyPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"api_key\""));
|
||||||
|
apiKeyPart.setBody("9168f53930b2fc69ec278414d6ff04fed723ef717867a25954143150d3e2dfe8");
|
||||||
|
// apiKeyPart.setBody(AccountManager::getInstance().getAccountInfo().getDiscourseApiKey().toLatin1());
|
||||||
|
|
||||||
|
QFile *file = new QFile(_fileName);
|
||||||
|
file->open(QIODevice::ReadOnly);
|
||||||
|
|
||||||
QByteArray postData;
|
QHttpPart imagePart;
|
||||||
// postData.append("api_key=" + accountManager.getAccountInfo().getDiscourseApiKey() + "&");
|
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
|
||||||
postData.append("api_key=9168f53930b2fc69ec278414d6ff04fed723ef717867a25954143150d3e2dfe8&");
|
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,
|
||||||
postData.append("topic_id=64&");
|
QVariant("form-data; name=\"file\"; filename=\"" + file->fileName() +"\""));
|
||||||
postData.append("raw=" + QUrl::toPercentEncoding(ui.textEdit->toPlainText()));
|
imagePart.setBodyDevice(file);
|
||||||
|
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
|
||||||
|
|
||||||
request.setUrl(grantURL);
|
multiPart->append(apiKeyPart);
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
multiPart->append(imagePart);
|
||||||
|
|
||||||
QNetworkReply* requestReply = _networkAccessManager->post(request, postData);
|
QUrl url(FORUM_UPLOADS_URL);
|
||||||
connect(_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
|
QNetworkRequest request(url);
|
||||||
|
|
||||||
connect(requestReply, &QNetworkReply::finished, this, &SnapshotShareDialog::requestFinished);
|
QNetworkReply *reply = _networkAccessManager->post(request, multiPart);
|
||||||
connect(requestReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));
|
bool check;
|
||||||
|
Q_UNUSED(check);
|
||||||
|
|
||||||
|
check = connect(reply, SIGNAL(finished()), this, SLOT(requestFinished()));
|
||||||
|
Q_ASSERT(check);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnapshotShareDialog::serviceRequestFinished(QNetworkReply* reply) {
|
void SnapshotShareDialog::sendForumPost(QString snapshotPath) {
|
||||||
qDebug() << reply->errorString();
|
|
||||||
|
if (!_networkAccessManager) {
|
||||||
|
_networkAccessManager = new QNetworkAccessManager(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// post to Discourse forum
|
||||||
|
QNetworkRequest request;
|
||||||
|
QUrl forumUrl(FORUM_POST_URL);
|
||||||
|
|
||||||
|
QUrlQuery query;
|
||||||
|
// query.addQueryItem("api_key", accountManager.getAccountInfo().getDiscourseApiKey();
|
||||||
|
query.addQueryItem("api_key", "9168f53930b2fc69ec278414d6ff04fed723ef717867a25954143150d3e2dfe8");
|
||||||
|
query.addQueryItem("topic_id", FORUM_REPLY_TO_TOPIC);
|
||||||
|
query.addQueryItem("raw", FORUM_POST_TEMPLATE.arg(snapshotPath, _ui.textEdit->toPlainText()));
|
||||||
|
forumUrl.setQuery(query);
|
||||||
|
|
||||||
|
QByteArray postData = forumUrl.toEncoded(QUrl::RemoveFragment);
|
||||||
|
request.setUrl(forumUrl);
|
||||||
|
QNetworkReply* requestReply = _networkAccessManager->post(request, postData);
|
||||||
|
|
||||||
|
bool check;
|
||||||
|
Q_UNUSED(check);
|
||||||
|
|
||||||
|
check = connect(requestReply, &QNetworkReply::finished, this, &SnapshotShareDialog::requestFinished);
|
||||||
|
Q_ASSERT(check);
|
||||||
|
|
||||||
|
check = connect(requestReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));
|
||||||
|
Q_ASSERT(check);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SnapshotShareDialog::requestFinished() {
|
void SnapshotShareDialog::requestFinished() {
|
||||||
|
|
||||||
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
|
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
|
||||||
|
|
||||||
qDebug() << requestReply->errorString();
|
qDebug() << requestReply->errorString();
|
||||||
delete requestReply;
|
delete requestReply;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,22 @@
|
||||||
//
|
//
|
||||||
// SnapshotShareDialog.h
|
// SnapshotShareDialog.h
|
||||||
// hifi
|
// interface/src/ui
|
||||||
//
|
//
|
||||||
// Created by Stojce Slavkovski on 2/16/14.
|
// Created by Stojce Slavkovski on 2/16/14.
|
||||||
|
// Copyright 2014 High Fidelity, Inc.
|
||||||
//
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifndef __hifi__snapshotShareDialog__
|
#ifndef hifi_snapshotShareDialog
|
||||||
#define __hifi__snapshotShareDialog__
|
#define hifi_snapshotShareDialog
|
||||||
|
|
||||||
#include "ui_shareSnapshot.h"
|
#include "ui_shareSnapshot.h"
|
||||||
|
|
||||||
|
#include <QUrl>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
|
#include <QNetworkAccessManager>
|
||||||
|
|
||||||
class SnapshotShareDialog : public QDialog {
|
class SnapshotShareDialog : public QDialog {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -20,15 +26,16 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString _fileName;
|
QString _fileName;
|
||||||
Ui_SnapshotShareDialog ui;
|
QNetworkAccessManager* _networkAccessManager;
|
||||||
|
Ui_SnapshotShareDialog _ui;
|
||||||
|
|
||||||
|
void uploadSnapshot();
|
||||||
|
void sendForumPost(QString snapshotPath);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void requestFinished();
|
void requestFinished();
|
||||||
void requestError(QNetworkReply::NetworkError error);
|
void requestError(QNetworkReply::NetworkError error);
|
||||||
void serviceRequestFinished(QNetworkReply* reply);
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void accept();
|
void accept();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* defined(__hifi__snapshotShareDialog__) */
|
#endif // hifi_snapshotShareDialog
|
||||||
|
|
Loading…
Reference in a new issue