Posting Image to forum

This commit is contained in:
Stojce Slavkovski 2014-05-04 18:30:58 +02:00
parent cec3ce78b9
commit d93480458f
3 changed files with 102 additions and 48 deletions

View file

@ -3663,7 +3663,7 @@ void Application::takeSnapshot() {
if (!_snapshotShareDialog) {
_snapshotShareDialog = new SnapshotShareDialog(fileName, _glWidget);
}
_snapshotShareDialog->exec();
_snapshotShareDialog->show();
}
void Application::urlGoTo(int argc, const char * constArgv[]) {

View file

@ -1,28 +1,41 @@
//
// SnapshotShareDialog.cpp
// hifi
// interface/src/ui
//
// 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 "AccountManager.h"
#include <QString>
#include <QFile>
#include <QUrlQuery>
#include <QtNetwork/QNetworkRequest>
#include <QHttpMultiPart>
const int NARROW_SNAPSHOT_DIALOG_SIZE = 500;
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);
ui.setupUi(this);
_ui.setupUi(this);
QPixmap snaphsotPixmap(fileName);
float snapshotRatio = static_cast<float>(snaphsotPixmap.size().width()) / snaphsotPixmap.size().height();
@ -30,70 +43,104 @@ SnapshotShareDialog::SnapshotShareDialog(QString fileName, QWidget* parent) : QD
// narrow snapshot
if (snapshotRatio > 1) {
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
if (snapshotRatio > labelRatio) {
int oldHeight = ui.snapshotWidget->size().height();
ui.snapshotWidget->setFixedHeight(ui.snapshotWidget->size().width() / snapshotRatio);
int oldHeight = _ui.snapshotWidget->size().height();
_ui.snapshotWidget->setFixedHeight((int) (_ui.snapshotWidget->size().width() / snapshotRatio));
// if height is less then original, resize the window as well
if (ui.snapshotWidget->size().height() < NARROW_SNAPSHOT_DIALOG_SIZE) {
setFixedHeight(size().height() - (oldHeight - ui.snapshotWidget->size().height()));
if (_ui.snapshotWidget->size().height() < NARROW_SNAPSHOT_DIALOG_SIZE) {
setFixedHeight(size().height() - (oldHeight - _ui.snapshotWidget->size().height()));
}
} else {
ui.snapshotWidget->setFixedWidth(ui.snapshotWidget->size().height() * snapshotRatio);
_ui.snapshotWidget->setFixedWidth((int) (_ui.snapshotWidget->size().height() * snapshotRatio));
}
ui.snapshotWidget->setPixmap(snaphsotPixmap);
ui.snapshotWidget->adjustSize();
_ui.snapshotWidget->setPixmap(snaphsotPixmap);
_ui.snapshotWidget->adjustSize();
}
void SnapshotShareDialog::accept() {
uploadSnapshot();
sendForumPost("/uploads/default/25/b607c8faea6de9c3.jpg");
close();
// post to Discourse forum
// AccountManager& accountManager = AccountManager::getInstance();
QNetworkAccessManager* _networkAccessManager = NULL;
}
void SnapshotShareDialog::uploadSnapshot() {
if (!_networkAccessManager) {
_networkAccessManager = new QNetworkAccessManager(this);
}
QNetworkRequest request;
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QUrl grantURL(FORUM_POST_URL);
grantURL.setPath("/posts");
QHttpPart apiKeyPart;
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;
// postData.append("api_key=" + accountManager.getAccountInfo().getDiscourseApiKey() + "&");
postData.append("api_key=9168f53930b2fc69ec278414d6ff04fed723ef717867a25954143150d3e2dfe8&");
postData.append("topic_id=64&");
postData.append("raw=" + QUrl::toPercentEncoding(ui.textEdit->toPlainText()));
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"file\"; filename=\"" + file->fileName() +"\""));
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
request.setUrl(grantURL);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
multiPart->append(apiKeyPart);
multiPart->append(imagePart);
QNetworkReply* requestReply = _networkAccessManager->post(request, postData);
connect(_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
QUrl url(FORUM_UPLOADS_URL);
QNetworkRequest request(url);
connect(requestReply, &QNetworkReply::finished, this, &SnapshotShareDialog::requestFinished);
connect(requestReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));
QNetworkReply *reply = _networkAccessManager->post(request, multiPart);
bool check;
Q_UNUSED(check);
check = connect(reply, SIGNAL(finished()), this, SLOT(requestFinished()));
Q_ASSERT(check);
}
void SnapshotShareDialog::serviceRequestFinished(QNetworkReply* reply) {
qDebug() << reply->errorString();
void SnapshotShareDialog::sendForumPost(QString snapshotPath) {
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() {
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
qDebug() << requestReply->errorString();
delete requestReply;
}
@ -101,4 +148,4 @@ void SnapshotShareDialog::requestFinished() {
void SnapshotShareDialog::requestError(QNetworkReply::NetworkError error) {
// TODO: error handling
qDebug() << "AccountManager requestError - " << error;
}
}

View file

@ -1,16 +1,22 @@
//
// SnapshotShareDialog.h
// hifi
// interface/src/ui
//
// 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__
#define __hifi__snapshotShareDialog__
#ifndef hifi_snapshotShareDialog
#define hifi_snapshotShareDialog
#include "ui_shareSnapshot.h"
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkAccessManager>
class SnapshotShareDialog : public QDialog {
Q_OBJECT
@ -20,15 +26,16 @@ public:
private:
QString _fileName;
Ui_SnapshotShareDialog ui;
QNetworkAccessManager* _networkAccessManager;
Ui_SnapshotShareDialog _ui;
void uploadSnapshot();
void sendForumPost(QString snapshotPath);
public slots:
void requestFinished();
void requestError(QNetworkReply::NetworkError error);
void serviceRequestFinished(QNetworkReply* reply);
private slots:
void accept();
};
#endif /* defined(__hifi__snapshotShareDialog__) */
#endif // hifi_snapshotShareDialog