Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "./toonzqt/updatechecker.h"
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
#include <qnetworkreply></qnetworkreply>
Toshihiro Shimizu 890ddd
#include <qeventloop></qeventloop>
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// UpdateChecker
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
UpdateChecker::UpdateChecker(const QString &requestToServer)
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	:
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	: QHttp(),
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	  m_webPageUrl()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
	QUrl url(requestToServer);
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	QString urlTemp = requestToServer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList urlList = urlTemp.split('?');
Toshihiro Shimizu 890ddd
	if (urlList.count() <= 1) {
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList paramList = urlList.at(1).split('&');
Toshihiro Shimizu 890ddd
	//if (!url.userName().isEmpty())
Toshihiro Shimizu 890ddd
	//	setUser(url.userName(), url.password());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_httpRequestAborted = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString param;
Toshihiro Shimizu 890ddd
	param = QString("?");
Toshihiro Shimizu 890ddd
	for (i = 0; i < paramList.size(); i++) {
Toshihiro Shimizu 890ddd
		param += paramList.at(i);
Toshihiro Shimizu 890ddd
		if (i < paramList.size() - 1)
Toshihiro Shimizu 890ddd
			param += QString("&");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QNetworkAccessManager manager;
Toshihiro Shimizu 890ddd
	QNetworkReply *reply = manager.get(QNetworkRequest(url.path() + param));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QEventLoop loop;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	connect(reply, SIGNAL(requestStarted(int)), &loop, SLOT(httpRequestStarted(int)));
Toshihiro Shimizu 890ddd
	connect(reply, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)), &loop, SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
Toshihiro Shimizu 890ddd
	connect(reply, SIGNAL(stateChanged(int)), &loop, SLOT(httpStateChanged(int)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	connect(&manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(httpRequestFinished(QNetworkReply *)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	loop.exec();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	//connect(this, SIGNAL(readyRead(const QHttpResponseHeader &)), this,
Toshihiro Shimizu 890ddd
	//	SLOT(readyReadexec(const QHttpResponseHeader &)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(requestFinished(int, bool)), this,
Toshihiro Shimizu 890ddd
			SLOT(httpRequestFinished(int, bool)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(requestStarted(int)), this,
Toshihiro Shimizu 890ddd
			SLOT(httpRequestStarted(int)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this,
Toshihiro Shimizu 890ddd
			SLOT(readResponseHeader(const QHttpResponseHeader &)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)), this,
Toshihiro Shimizu 890ddd
			SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(stateChanged(int)), this,
Toshihiro Shimizu 890ddd
			SLOT(httpStateChanged(int)));
Toshihiro Shimizu 890ddd
	QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
Toshihiro Shimizu 890ddd
	QString urlTemp = requestToServer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList urlList = urlTemp.split('?');
Toshihiro Shimizu 890ddd
	if (urlList.count() <= 1) {
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList paramList = urlList.at(1).split('&');
Toshihiro Shimizu 890ddd
	if (!url.userName().isEmpty())
Toshihiro Shimizu 890ddd
		setUser(url.userName(), url.password());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_httpRequestAborted = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString param;
Toshihiro Shimizu 890ddd
	param = QString("?");
Toshihiro Shimizu 890ddd
	for (i = 0; i < paramList.size(); i++) {
Toshihiro Shimizu 890ddd
		param += paramList.at(i);
Toshihiro Shimizu 890ddd
		if (i < paramList.size() - 1)
Toshihiro Shimizu 890ddd
			param += QString("&");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/*param = QString("?")+paramList.at(0)+QString("&")
Toshihiro Shimizu 890ddd
		+paramList.at(1) + QString("&") + paramList.at(2);*/
Toshihiro Shimizu 890ddd
	m_httpGetId = get(url.path() + param); //, file);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if QT_VERSION < 0x050000
Toshihiro Shimizu 890ddd
void UpdateChecker::readResponseHeader(const QHttpResponseHeader &responseHeader)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int err = responseHeader.statusCode();
Toshihiro Shimizu 890ddd
	if (err != 200 && err != 502) {
Toshihiro Shimizu 890ddd
		m_httpRequestAborted = true;
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void UpdateChecker::httpStateChanged(int status)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::string stateStr;
Toshihiro Shimizu 890ddd
	switch (status) {
Toshihiro Shimizu 890ddd
	case 1:
Toshihiro Shimizu 890ddd
		stateStr = "A host name lookup is in progress...";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 2:
Toshihiro Shimizu 890ddd
		stateStr = "Connecting...";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 3:
Toshihiro Shimizu 890ddd
		stateStr = "Sending informations...";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 4:
Toshihiro Shimizu 890ddd
		stateStr = "Reading informations...";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 5:
Toshihiro Shimizu 890ddd
		stateStr = "Connected.";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 6:
Toshihiro Shimizu 890ddd
		stateStr = "The connection is closing down, but is not yet closed. (The state will be Unconnected when the connection is closed.)";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		stateStr = "There is no connection to the host.";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#if QT_VERSION < 0x050000
Toshihiro Shimizu 890ddd
	status = state();
Toshihiro Shimizu 890ddd
	qDebug("Status: %d : %s", status, stateStr.c_str());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
void UpdateChecker::httpRequestFinished(QNetworkReply *reply)
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
void UpdateChecker::httpRequestFinished(int requestId, bool error)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QByteArray arr;
Toshihiro Shimizu 890ddd
	std::string webPageString;
Toshihiro Shimizu 890ddd
	std::string dateString;
Toshihiro Shimizu 890ddd
	QString qstr;
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
	if (reply->error() != QNetworkReply::NoError)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	arr = reply->readAll();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	if (requestId != m_httpGetId) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (error || m_httpRequestAborted) {
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	arr = readAll();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	qstr = QString(arr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int startIndex = qstr.indexOf("Startdate");
Toshihiro Shimizu 890ddd
	int endIndex = qstr.indexOf("Enddate");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((endIndex - startIndex - 9) <= 0) {
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dateString = qstr.toStdString();
Toshihiro Shimizu 890ddd
	dateString = dateString.substr(startIndex + 9, endIndex - startIndex - 9);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString qDateString = QString::fromStdString(dateString);
Toshihiro Shimizu 890ddd
	qDateString.remove("\"");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Make sure that the format is exactly MM/dd/yyyy
Toshihiro Shimizu 890ddd
	if (qDateString.size() != 10) {
Toshihiro Shimizu 890ddd
		QStringList fields = qDateString.split("/");
Toshihiro Shimizu 890ddd
		if (fields.size() == 3) {
Toshihiro Shimizu 890ddd
			QString month = fields.at(0);
Toshihiro Shimizu 890ddd
			QString day = fields.at(1);
Toshihiro Shimizu 890ddd
			QString year = fields.at(2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (day.size() == 1)
Toshihiro Shimizu 890ddd
				day.prepend("0");
Toshihiro Shimizu 890ddd
			if (month.size() == 1)
Toshihiro Shimizu 890ddd
				month.prepend("0");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			qDateString = month + "/" + day + "/" + year;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_updateDate = QDate::fromString(qDateString, "MM/dd/yyyy");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	startIndex = qstr.indexOf("Starturl");
Toshihiro Shimizu 890ddd
	endIndex = qstr.indexOf("Endurl");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	webPageString = qstr.toStdString();
Toshihiro Shimizu 890ddd
	webPageString = webPageString.substr(startIndex + 9, endIndex - startIndex - 9);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString qWebPageString = QString::fromStdString(webPageString);
Toshihiro Shimizu 890ddd
	qWebPageString.remove("\"");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QUrl webPageUrl(qWebPageString);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (webPageUrl.isValid())
Toshihiro Shimizu 890ddd
		m_webPageUrl = webPageUrl;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void UpdateChecker::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(false);
Toshihiro Shimizu 890ddd
}