Skip to content

Commit

Permalink
Retrieving message/error from server
Browse files Browse the repository at this point in the history
  • Loading branch information
hrobeers committed Nov 27, 2015
1 parent 4e732d5 commit fbc1f5f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/version_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define MINOR_VERSION 1
#define REVISION 0

#define BUILD_NUMBER 465
#define COMMIT_HASH "0bb741c6003958b0a8383b1f6bef424bd5812eae"
#define BUILD_NUMBER 466
#define COMMIT_HASH "4e732d589fdfcbfa742528741d05fcb91d2e05f1"

#endif // VERSION_AUTOGEN_H
47 changes: 32 additions & 15 deletions src/web/exportdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
#include "exportdialog.hpp"
#include "ui_exportdialog.h"

#include <QNetworkReply>

using namespace web;

ExportDialog::ExportDialog(const foillogic::Foil *toExport, const hrlib::Version *version, QWidget *parent) :
QDialog(parent),
_ui(new Ui::ExportDialog),
_toExport(toExport),
_stlExport(new StlExport(version))
_stlExport(new StlExport(version)),
_msgReply(nullptr), _stlReply(nullptr)
{
_ui->setupUi(this);

Expand All @@ -38,30 +41,44 @@ ExportDialog::ExportDialog(const foillogic::Foil *toExport, const hrlib::Version
_ui->progressBar->setMaximum(0);

connect(_ui->exportButton, &QPushButton::clicked, this, &ExportDialog::exportClicked);
connect(_ui->cancelButton, &QPushButton::clicked, this, &ExportDialog::cancelClicked);
connect(_ui->closeButton, &QPushButton::clicked, this, &ExportDialog::closeClicked);

_ui->webView->setHtml("<h2>Connection to the internet is required for this functionality</h2>");

connect(_stlExport.get(), &StlExport::finished, this, &ExportDialog::exportFinished);

_msgReply.reset(_stlExport->getMessage());
}

ExportDialog::~ExportDialog() {}

bool clicked = false;
void ExportDialog::exportClicked()
{
if (clicked)
{
clicked = false;
close();
}
else
{
_ui->progressBar->show();
_stlExport->generateSTL(_toExport);
clicked = true;
}
// TODO choose file to save to
_stlReply.reset(_stlExport->generateSTL(_toExport));
_ui->progressBar->show();
}

void ExportDialog::cancelClicked()
void ExportDialog::closeClicked()
{
close();
}

void ExportDialog::exportFinished(QNetworkReply *reply)
{
if (_msgReply.get() == reply)
{
_ui->webView->setHtml(QString::fromUtf8(reply->readAll()));

if (reply->error() == QNetworkReply::NoError)
_ui->exportButton->setDisabled(false);
_msgReply.reset();
}
else if (_stlReply.get() == reply)
{
// TODO download (progressbar) & save
_ui->webView->setHtml(QString::fromUtf8(reply->readAll()));
_stlReply.reset();
// close();
}
}
6 changes: 5 additions & 1 deletion src/web/exportdialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QDialog>
#include <memory>
#include "stlexport.hpp"
#include "qmemory.hpp"

namespace Ui {
class ExportDialog;
Expand All @@ -45,9 +46,12 @@ namespace web {
std::unique_ptr<StlExport> _stlExport;
const foillogic::Foil* _toExport;

qunique_ptr<QNetworkReply> _msgReply, _stlReply;

private slots:
void exportClicked();
void cancelClicked();
void closeClicked();
void exportFinished(QNetworkReply *reply);
};
}

Expand Down
7 changes: 5 additions & 2 deletions src/web/exportdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@
</item>
<item>
<widget class="QPushButton" name="exportButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Export</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelButton">
<widget class="QPushButton" name="closeButton">
<property name="text">
<string>Cancel</string>
<string>Close</string>
</property>
</widget>
</item>
Expand Down
32 changes: 20 additions & 12 deletions src/web/stlexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,46 @@ using namespace hrlib;
StlExport::StlExport(const Version *version, QObject *parent) :
QObject(parent),
_manager(new QNetworkAccessManager()),
_fileName("finfoil_")
_fileName("finfoil_"), // TODO use real fileName if possible
_messageName("finfoil_v")
{
_fileName.append(QString::number(version->Major()));
_fileName.append("_");
_fileName.append(QString::number(version->Minor()));
_fileName.append(".foil");

_messageName.append(QString::number(version->Major()));
_messageName.append(".");
_messageName.append(QString::number(version->Minor()));
_messageName.append(".html");
}

QString StlExport::generateSTL(const Foil *foil)
QNetworkReply* StlExport::getMessage()
{
QUrl url("http://127.0.0.1:4000/messages/" + _messageName);
QNetworkRequest request(url);

connect(_manager.get(), &QNetworkAccessManager::finished, this, &StlExport::finished);

return _manager->get(request);
}

QNetworkReply* StlExport::generateSTL(const Foil *foil)
{
QUrl url("http://127.0.0.1:4000/stl/" + _fileName);
QNetworkRequest request(url);

request.setHeader(QNetworkRequest::ContentTypeHeader, "application/foil");

QObject::connect(_manager.get(), SIGNAL(finished(QNetworkReply *)), this, SLOT(stlExportFinished(QNetworkReply *)));
connect(_manager.get(), &QNetworkAccessManager::finished, this, &StlExport::finished);

// Serialize foil object
QJsonDocument json(JenSON::serialize(foil));
// Trim json floats
std::string long_utf8 = json.toJson(QJsonDocument::Compact).toStdString();
std::string short_utf8 = trim_json_floats(long_utf8);

_manager->post(request, QByteArray(short_utf8.c_str()));

return "";
return _manager->post(request, QByteArray(short_utf8.c_str()));
}

StlExport::~StlExport() { }

void StlExport::stlExportFinished(QNetworkReply *reply)
{
auto r = reply->readAll();
qDebug() << r;
}
9 changes: 4 additions & 5 deletions src/web/stlexport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,19 @@ namespace web
public:
explicit StlExport(const hrlib::Version *version, QObject *parent = 0);

QString generateSTL(const foillogic::Foil *foil);
QNetworkReply* getMessage();
QNetworkReply* generateSTL(const foillogic::Foil *foil);

virtual ~StlExport();

signals:
void finished(QNetworkReply* reply);

public slots:

private:
std::unique_ptr<QNetworkAccessManager> _manager;
QString _fileName;

private slots:
void stlExportFinished(QNetworkReply *reply);
QString _fileName, _messageName;
};
}

Expand Down

0 comments on commit fbc1f5f

Please sign in to comment.