Added https support to the Windows version.

This commit is contained in:
Leonardo Zide 2017-09-16 12:20:17 -07:00
parent ecdcd50239
commit 943b93a514
3 changed files with 127 additions and 11 deletions

View file

@ -20,7 +20,7 @@ win32 {
QMAKE_LFLAGS += /INCREMENTAL QMAKE_LFLAGS += /INCREMENTAL
PRECOMPILED_SOURCE = common/lc_global.cpp PRECOMPILED_SOURCE = common/lc_global.cpp
RC_FILE = qt/leocad.rc RC_FILE = qt/leocad.rc
LIBS += -ladvapi32 -lshell32 -lopengl32 LIBS += -ladvapi32 -lshell32 -lopengl32 -lwininet.lib
} else { } else {
PRECOMPILED_HEADER = common/lc_global.h PRECOMPILED_HEADER = common/lc_global.h
LIBS += -lz LIBS += -lz

View file

@ -3,6 +3,61 @@
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#ifdef Q_OS_WIN
lcHttpReply::lcHttpReply(QObject* Parent, const QString& URL)
: QThread(Parent)
{
mError = true;
mAbort = false;
mURL = URL;
}
void lcHttpReply::run()
{
HINTERNET Session = nullptr;
HINTERNET Request = nullptr;
if (sizeof(wchar_t) != sizeof(QChar))
return;
Session = InternetOpen(L"LeoCAD", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!Session)
return;
Request = InternetOpenUrl(Session, (WCHAR*)mURL.data(), NULL, 0, 0, 0);
if (!Request)
{
InternetCloseHandle(Session);
return;
}
for (;;)
{
char Buffer[1024];
DWORD BytesRead;
if (mAbort)
break;
if (!InternetReadFile(Request, Buffer, sizeof(Buffer), &BytesRead))
break;
if (BytesRead)
mBuffer.append(Buffer, BytesRead);
else
{
mError = false;
break;
}
}
InternetCloseHandle(Request);
InternetCloseHandle(Session);
}
#endif
lcSetsDatabaseDialog::lcSetsDatabaseDialog(QWidget* Parent) lcSetsDatabaseDialog::lcSetsDatabaseDialog(QWidget* Parent)
: QDialog(Parent), : QDialog(Parent),
ui(new Ui::lcSetsDatabaseDialog) ui(new Ui::lcSetsDatabaseDialog)
@ -12,9 +67,11 @@ lcSetsDatabaseDialog::lcSetsDatabaseDialog(QWidget* Parent)
connect(ui->SetsTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(accept())); connect(ui->SetsTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(accept()));
connect(this, SIGNAL(finished(int)), this, SLOT(Finished(int))); connect(this, SIGNAL(finished(int)), this, SLOT(Finished(int)));
connect(&mNetworkManager, SIGNAL(finished(QNetworkReply*)), SLOT(DownloadFinished(QNetworkReply*))); #ifndef Q_OS_WIN
connect(&mNetworkManager, SIGNAL(finished(QNetworkReply*)), SLOT(DownloadFinished(lcHttpReply*)));
#endif
mKeyListReply = mNetworkManager.get(QNetworkRequest(QUrl("http://www.leocad.org/rebrickable.json"))); mKeyListReply = RequestURL("http://www.leocad.org/rebrickable.json");
} }
lcSetsDatabaseDialog::~lcSetsDatabaseDialog() lcSetsDatabaseDialog::~lcSetsDatabaseDialog()
@ -22,6 +79,18 @@ lcSetsDatabaseDialog::~lcSetsDatabaseDialog()
delete ui; delete ui;
} }
lcHttpReply* lcSetsDatabaseDialog::RequestURL(const QString& URL)
{
#ifdef Q_OS_WIN
lcHttpReply* Reply = new lcHttpReply(this, URL);
connect(Reply, &QThread::finished, [this, Reply] { DownloadFinished(Reply); });
Reply->start();
return Reply;
#else
return mNetworkManager.get(QNetworkRequest(QUrl(URL)));
#endif
}
QString lcSetsDatabaseDialog::GetSetName() const QString lcSetsDatabaseDialog::GetSetName() const
{ {
QTreeWidgetItem* Current = ui->SetsTree->currentItem(); QTreeWidgetItem* Current = ui->SetsTree->currentItem();
@ -74,7 +143,7 @@ void lcSetsDatabaseDialog::accept()
int KeyIndex = QTime::currentTime().msec() % mKeys.size(); int KeyIndex = QTime::currentTime().msec() % mKeys.size();
QString DownloadUrl = QString("https://rebrickable.com/api/v3/lego/sets/%1/parts/?key=%2").arg(SetNum, mKeys[KeyIndex]); QString DownloadUrl = QString("https://rebrickable.com/api/v3/lego/sets/%1/parts/?key=%2").arg(SetNum, mKeys[KeyIndex]);
mInventoryReply = mNetworkManager.get(QNetworkRequest(QUrl(DownloadUrl))); mInventoryReply = RequestURL(DownloadUrl);
while (mInventoryReply) while (mInventoryReply)
{ {
@ -135,7 +204,7 @@ void lcSetsDatabaseDialog::on_SearchButton_clicked()
int KeyIndex = QTime::currentTime().msec() % mKeys.size(); int KeyIndex = QTime::currentTime().msec() % mKeys.size();
QString SearchUrl = QString("https://rebrickable.com/api/v3/lego/sets/?search=%1&key=%2").arg(Keyword, mKeys[KeyIndex]); QString SearchUrl = QString("https://rebrickable.com/api/v3/lego/sets/?search=%1&key=%2").arg(Keyword, mKeys[KeyIndex]);
mSearchReply = mNetworkManager.get(QNetworkRequest(QUrl(SearchUrl))); mSearchReply = RequestURL(SearchUrl);
while (mSearchReply) while (mSearchReply)
{ {
@ -151,7 +220,7 @@ void lcSetsDatabaseDialog::on_SearchButton_clicked()
} }
} }
void lcSetsDatabaseDialog::DownloadFinished(QNetworkReply* Reply) void lcSetsDatabaseDialog::DownloadFinished(lcHttpReply* Reply)
{ {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
if (Reply == mKeyListReply) if (Reply == mKeyListReply)

View file

@ -7,6 +7,48 @@ namespace Ui {
class lcSetsDatabaseDialog; class lcSetsDatabaseDialog;
} }
#ifdef Q_OS_WIN
#include <wininet.h>
class lcHttpReply : public QThread
{
Q_OBJECT
public:
lcHttpReply(QObject* Parent, const QString& URL);
void run();
bool error() const
{
return mError;
}
void abort()
{
mAbort = true;
}
QByteArray readAll() const
{
return mBuffer;
}
protected:
bool mError;
bool mAbort;
QByteArray mBuffer;
QString mURL;
};
#else
typedef QNetworkReply lcHttpReply;
#endif
class lcSetsDatabaseDialog : public QDialog class lcSetsDatabaseDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT
@ -26,16 +68,21 @@ public:
virtual bool eventFilter(QObject* Object, QEvent* Event) override; virtual bool eventFilter(QObject* Object, QEvent* Event) override;
public slots: public slots:
void DownloadFinished(QNetworkReply* Reply); void DownloadFinished(lcHttpReply* Reply);
void on_SearchButton_clicked(); void on_SearchButton_clicked();
void accept() override; void accept() override;
void Finished(int Result); void Finished(int Result);
private: protected:
lcHttpReply* RequestURL(const QString& URL);
#ifndef Q_OS_WIN
QNetworkAccessManager mNetworkManager; QNetworkAccessManager mNetworkManager;
QNetworkReply* mKeyListReply; #endif
QNetworkReply* mSearchReply;
QNetworkReply* mInventoryReply; lcHttpReply* mKeyListReply;
lcHttpReply* mSearchReply;
lcHttpReply* mInventoryReply;
QStringList mKeys; QStringList mKeys;
QByteArray mInventory; QByteArray mInventory;