Commit f83eb83f authored by Jan Möbius's avatar Jan Möbius

Basic update checker

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@2866 383ad7c9-94d9-4d36-a494-682f7c89f535
parent d8368d22
......@@ -35,8 +35,6 @@
#include "optionsWidget.hh"
#include <iostream>
#include <OpenFlipper/common/GlobalOptions.hh>
#include <QtNetwork>
OptionsWidget::OptionsWidget(std::vector<PluginInfo>& _plugins, std::vector<KeyBinding>& _core, QWidget *parent)
: QWidget(parent),
......@@ -48,10 +46,22 @@ OptionsWidget::OptionsWidget(std::vector<PluginInfo>& _plugins, std::vector<KeyB
connect(applyButton,SIGNAL(clicked()),this,SLOT(slotApply()));
connect(cancelButton,SIGNAL(clicked()),this,SLOT(slotCancel()));
// connect(checkUpdateButton,SIGNAL(clicked()),this,SLOT(slotCheckUpdates()));
connect(checkUpdateButton,SIGNAL(clicked()),this,SLOT(slotCheckUpdates()));
http = new QHttp(this);
// http specific connections
connect(http, SIGNAL(requestFinished(int, bool)),
this, SLOT(httpRequestFinished(int, bool)));
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
connect(http, SIGNAL(dataReadProgress(int, int)),
this, SLOT(updateDataReadProgress(int, int)));
progressDialog = new QProgressDialog(this);
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
}
void OptionsWidget::showEvent ( QShowEvent * event ) {
......@@ -147,95 +157,137 @@ void OptionsWidget::slotCancel() {
hide();
}
// void OptionsWidget::slotCheckUpdates() {
// // QUrl url("https://www.graphics.rwth-aachen.de/restricted/OpenFlipper-SIL/Versions.txt");
// // url.setUserName("");
// // url.setPassord("");
//
// QString ServerMainURL = updateURL->text() + "Versions.txt";
// QUrl url(ServerMainURL);
//
// if ( ! updateUser->text().isEmpty() )
// url.setUserName(updateUser->text());
//
// if ( ! updatePass->text().isEmpty() )
// url.setPassword(updatePass->text());
//
// QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
// ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + "ServerVersions.txt");
// QString fileName = fileInfo.filePath();
//
// if (QFile::exists(fileName)) {
// QFile::remove(fileName);
// std::cerr << "File exists, removing it" << std::endl;
// }
//
// file = new QFile(fileName);
// if (!file->open(QIODevice::WriteOnly)) {
// std::cerr << "Unable to Open local file for writing" << std::endl;
// delete file;
// file = 0;
// } else {
// QHttp* http = new QHttp;
// QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
// http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
//
// if (!url.userName().isEmpty())
// http->setUser(url.userName(), url.password());
//
// // httpRequestAborted = false;
// QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
// if (path.isEmpty())
// path = "/";
//
// int httpGetId = http->get(path, file);
//
// // !!! Does not work because get is non blocking
// file->close();
//
// delete file;
//
// // progressDialog->setWindowTitle(tr("HTTP"));
// // progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
// // downloadButton->setEnabled(false);
// }
// }
//
// void HttpWindow::httpRequestFinished(int requestId, bool error)
// {
// if (requestId != httpGetId)
// return;
// if (httpRequestAborted) {
// if (file) {
// file->close();
// file->remove();
// delete file;
// file = 0;
// }
//
// progressDialog->hide();
// return;
// }
//
// if (requestId != httpGetId)
// return;
//
// progressDialog->hide();
// file->close();
//
// if (error) {
// file->remove();
// QMessageBox::information(this, tr("HTTP"),
// tr("Download failed: %1.")
// .arg(http->errorString()));
// } else {
// QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
// statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName));
// }
//
// downloadButton->setEnabled(true);
// delete file;
// file = 0;
// }
void OptionsWidget::slotCheckUpdates() {
// http://www.graphics.rwth-aachen.de/restricted/OpenFlipper-SIL/
downloadType = VERSIONS_FILE;
QString ServerMainURL = updateURL->text() + "Versions.txt";
QUrl url(ServerMainURL);
if ( ! updateUser->text().isEmpty() )
url.setUserName(updateUser->text());
if ( ! updatePass->text().isEmpty() )
url.setPassword(updatePass->text());
QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
".OpenFlipper" + OpenFlipper::Options::dirSeparator() + "ServerVersions.txt");
QString fileName = fileInfo.filePath();
if (QFile::exists(fileName)) {
QFile::remove(fileName);
}
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
std::cerr << "Unable to Open local file for writing" << std::endl;
delete file;
file = 0;
} else {
QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
if (!url.userName().isEmpty())
http->setUser(url.userName(), url.password());
httpRequestAborted = false;
QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
if (path.isEmpty())
path = "/";
httpGetId = http->get(path, file);
statusLabel->setText("Getting Versions file from " + ServerMainURL);
progressDialog->setWindowTitle(tr("HTTP"));
progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
checkUpdateButton->setEnabled(false);
}
}
void OptionsWidget::slotGetUpdates() {
}
void OptionsWidget::httpRequestFinished(int requestId, bool error)
{
if (requestId != httpGetId)
return;
if (httpRequestAborted) {
if (file) {
file->close();
file->remove();
delete file;
file = 0;
}
progressDialog->hide();
return;
}
if (requestId != httpGetId)
return;
progressDialog->hide();
file->close();
if (error) {
file->remove();
QMessageBox::information(this, tr("HTTP"),
tr("Download failed: %1.")
.arg(http->errorString()));
statusLabel->setText(tr("Download failed: %1.").arg(http->errorString()));
} else {
QString fileName = QFileInfo(QUrl(updateURL->text()).path()).fileName();
statusLabel->setText(tr("Downloaded %1.").arg(fileName));
}
checkUpdateButton->setEnabled(true);
delete file;
file = 0;
}
void OptionsWidget::readResponseHeader(const QHttpResponseHeader &responseHeader)
{
switch (responseHeader.statusCode()) {
case 200: // Ok
case 301: // Moved Permanently
case 302: // Found
case 303: // See Other
case 307: // Temporary Redirect
// these are not error conditions
break;
default:
QMessageBox::information(this, tr("HTTP"),
tr("Download failed: %1.")
.arg(responseHeader.reasonPhrase()));
statusLabel->setText("Download failed: " + responseHeader.reasonPhrase());
httpRequestAborted = true;
progressDialog->hide();
http->abort();
}
}
void OptionsWidget::cancelDownload()
{
statusLabel->setText(tr("download canceled."));
httpRequestAborted = true;
http->abort();
checkUpdateButton->setEnabled(true);
}
void OptionsWidget::updateDataReadProgress(int bytesRead, int totalBytes)
{
if (httpRequestAborted)
return;
progressDialog->setMaximum(totalBytes);
progressDialog->setValue(bytesRead);
}
......@@ -38,6 +38,7 @@
#include "ui_optionsWidget.hh"
#include <QtGui>
#include <QStringList>
#include <QtNetwork>
#include <OpenFlipper/Core/PluginInfo.hh>
......@@ -60,7 +61,10 @@ class OptionsWidget : public QWidget, public Ui::OptionsWidget
void slotCancel();
/// Checks for updates
// void slotCheckUpdates();
void slotCheckUpdates();
/// Download updates
void slotGetUpdates();
protected:
void showEvent ( QShowEvent * event );
......@@ -70,6 +74,42 @@ class OptionsWidget : public QWidget, public Ui::OptionsWidget
std::vector<PluginInfo>& plugins_;
std::vector<KeyBinding>& coreKeys_;
// flag indicating if something went wrong and the request has to be aborted
bool httpRequestAborted;
// Id of the current request
int httpGetId;
// Request variable
QHttp *http;
// File for http downloads
QFile *file;
// ProgressDialog for Downloads
QProgressDialog *progressDialog;
// What type of download is currently active
enum DOWNLOAD {
NONE,
VERSIONS_FILE,
WINDOWS_SETUP
} downloadType;
private slots:
// This slot is called when a http request has been finished
void httpRequestFinished(int requestId, bool error);
// Parses the response and gives feedback
void readResponseHeader(const QHttpResponseHeader &responseHeader);
// Updates the progress Dialog while downloading
void updateDataReadProgress(int bytesRead, int totalBytes);
// Progress dialog callback for cancel
void cancelDownload();
};
#endif //OPTIONSWIDGET_HH
......@@ -264,6 +264,14 @@
</layout>
</widget>
<widget class="QWidget" name="Updates" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>545</width>
<height>401</height>
</rect>
</property>
<attribute name="title" >
<string>Updates</string>
</attribute>
......@@ -326,8 +334,32 @@
</property>
<property name="sizeHint" stdset="0" >
<size>
<width>40</width>
<height>20</height>
<width>198</width>
<height>27</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5" >
<item>
<widget class="QLabel" name="statusLabel" >
<property name="text" >
<string>Check for Updates?</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3" >
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0" >
<size>
<width>13</width>
<height>17</height>
</size>
</property>
</spacer>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment