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

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 @@ ...@@ -35,8 +35,6 @@
#include "optionsWidget.hh" #include "optionsWidget.hh"
#include <iostream> #include <iostream>
#include <OpenFlipper/common/GlobalOptions.hh> #include <OpenFlipper/common/GlobalOptions.hh>
#include <QtNetwork>
OptionsWidget::OptionsWidget(std::vector<PluginInfo>& _plugins, std::vector<KeyBinding>& _core, QWidget *parent) OptionsWidget::OptionsWidget(std::vector<PluginInfo>& _plugins, std::vector<KeyBinding>& _core, QWidget *parent)
: QWidget(parent), : QWidget(parent),
...@@ -48,10 +46,22 @@ OptionsWidget::OptionsWidget(std::vector<PluginInfo>& _plugins, std::vector<KeyB ...@@ -48,10 +46,22 @@ OptionsWidget::OptionsWidget(std::vector<PluginInfo>& _plugins, std::vector<KeyB
connect(applyButton,SIGNAL(clicked()),this,SLOT(slotApply())); connect(applyButton,SIGNAL(clicked()),this,SLOT(slotApply()));
connect(cancelButton,SIGNAL(clicked()),this,SLOT(slotCancel())); 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 ) { void OptionsWidget::showEvent ( QShowEvent * event ) {
...@@ -147,95 +157,137 @@ void OptionsWidget::slotCancel() { ...@@ -147,95 +157,137 @@ void OptionsWidget::slotCancel() {
hide(); hide();
} }
// void OptionsWidget::slotCheckUpdates() { void OptionsWidget::slotCheckUpdates() {
// // QUrl url("https://www.graphics.rwth-aachen.de/restricted/OpenFlipper-SIL/Versions.txt");
// // url.setUserName(""); // http://www.graphics.rwth-aachen.de/restricted/OpenFlipper-SIL/
// // url.setPassord("");
// downloadType = VERSIONS_FILE;
// QString ServerMainURL = updateURL->text() + "Versions.txt";
// QUrl url(ServerMainURL); QString ServerMainURL = updateURL->text() + "Versions.txt";
// QUrl url(ServerMainURL);
// if ( ! updateUser->text().isEmpty() )
// url.setUserName(updateUser->text()); if ( ! updateUser->text().isEmpty() )
// url.setUserName(updateUser->text());
// if ( ! updatePass->text().isEmpty() )
// url.setPassword(updatePass->text()); if ( ! updatePass->text().isEmpty() )
// url.setPassword(updatePass->text());
// QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
// ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + "ServerVersions.txt"); QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
// QString fileName = fileInfo.filePath(); ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + "ServerVersions.txt");
// QString fileName = fileInfo.filePath();
// if (QFile::exists(fileName)) {
// QFile::remove(fileName); if (QFile::exists(fileName)) {
// std::cerr << "File exists, removing it" << std::endl; QFile::remove(fileName);
// } }
//
// file = new QFile(fileName); file = new QFile(fileName);
// if (!file->open(QIODevice::WriteOnly)) { if (!file->open(QIODevice::WriteOnly)) {
// std::cerr << "Unable to Open local file for writing" << std::endl; std::cerr << "Unable to Open local file for writing" << std::endl;
// delete file; delete file;
// file = 0; file = 0;
// } else { } else {
// QHttp* http = new QHttp; QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
// QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp; http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
// http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
// if (!url.userName().isEmpty())
// if (!url.userName().isEmpty()) http->setUser(url.userName(), url.password());
// http->setUser(url.userName(), url.password());
// httpRequestAborted = false;
// // httpRequestAborted = false; QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
// QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/"); if (path.isEmpty())
// if (path.isEmpty()) path = "/";
// path = "/";
// httpGetId = http->get(path, file);
// int httpGetId = http->get(path, file);
// statusLabel->setText("Getting Versions file from " + ServerMainURL);
// // !!! Does not work because get is non blocking
// file->close(); progressDialog->setWindowTitle(tr("HTTP"));
// progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
// delete file; checkUpdateButton->setEnabled(false);
// }
// // progressDialog->setWindowTitle(tr("HTTP")); }
// // progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
// // downloadButton->setEnabled(false); void OptionsWidget::slotGetUpdates() {
// } }
// }
// void OptionsWidget::httpRequestFinished(int requestId, bool error)
// void HttpWindow::httpRequestFinished(int requestId, bool error) {
// { if (requestId != httpGetId)
// if (requestId != httpGetId) return;
// return; if (httpRequestAborted) {
// if (httpRequestAborted) { if (file) {
// if (file) { file->close();
// file->close(); file->remove();
// file->remove(); delete file;
// delete file; file = 0;
// file = 0; }
// }
// progressDialog->hide();
// progressDialog->hide(); return;
// return; }
// }
// if (requestId != httpGetId)
// if (requestId != httpGetId) return;
// return;
// progressDialog->hide();
// progressDialog->hide(); file->close();
// file->close();
// if (error) {
// if (error) { file->remove();
// file->remove(); QMessageBox::information(this, tr("HTTP"),
// QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.")
// tr("Download failed: %1.") .arg(http->errorString()));
// .arg(http->errorString())); statusLabel->setText(tr("Download failed: %1.").arg(http->errorString()));
// } else { } else {
// QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName(); QString fileName = QFileInfo(QUrl(updateURL->text()).path()).fileName();
// statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName)); statusLabel->setText(tr("Downloaded %1.").arg(fileName));
// } }
//
// downloadButton->setEnabled(true); checkUpdateButton->setEnabled(true);
// delete file; delete file;
// file = 0; 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 @@ ...@@ -38,6 +38,7 @@
#include "ui_optionsWidget.hh" #include "ui_optionsWidget.hh"
#include <QtGui> #include <QtGui>
#include <QStringList> #include <QStringList>
#include <QtNetwork>
#include <OpenFlipper/Core/PluginInfo.hh> #include <OpenFlipper/Core/PluginInfo.hh>
...@@ -60,7 +61,10 @@ class OptionsWidget : public QWidget, public Ui::OptionsWidget ...@@ -60,7 +61,10 @@ class OptionsWidget : public QWidget, public Ui::OptionsWidget
void slotCancel(); void slotCancel();
/// Checks for updates /// Checks for updates
// void slotCheckUpdates(); void slotCheckUpdates();
/// Download updates
void slotGetUpdates();
protected: protected:
void showEvent ( QShowEvent * event ); void showEvent ( QShowEvent * event );
...@@ -70,6 +74,42 @@ class OptionsWidget : public QWidget, public Ui::OptionsWidget ...@@ -70,6 +74,42 @@ class OptionsWidget : public QWidget, public Ui::OptionsWidget
std::vector<PluginInfo>& plugins_; std::vector<PluginInfo>& plugins_;
std::vector<KeyBinding>& coreKeys_; 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 #endif //OPTIONSWIDGET_HH
...@@ -264,6 +264,14 @@ ...@@ -264,6 +264,14 @@
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="Updates" > <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" > <attribute name="title" >
<string>Updates</string> <string>Updates</string>
</attribute> </attribute>
...@@ -326,8 +334,32 @@ ...@@ -326,8 +334,32 @@
</property> </property>
<property name="sizeHint" stdset="0" > <property name="sizeHint" stdset="0" >
<size> <size>
<width>40</width> <width>198</width>
<height>20</height> <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> </size>
</property> </property>
</spacer> </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