[Kde-cygwin-cvs] CVS: tools/installer installer.cpp,NONE,1.1 installer.h,NONE,1.1 installer.pro,NONE
Status: Inactive
Brought to you by:
habacker
Update of /cvsroot/kde-cygwin/tools/installer In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2517/installer Added Files: installer.cpp installer.h installer.pro main.cpp package.cpp package.h packagelist.cpp packagelist.h packages.h Log Message: version 0.1 --- NEW FILE: installer.cpp --- /**************************************************************************** ** ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sa...@tr.... ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include "installer.h" #include "package.h" #include "packagelist.h" #include <QtCore> #include <QtGui> #include <QtNetwork> #include <QStringListModel> QString packageListURL = "http://sourceforge.net/project/showfiles.php?group_id=23617"; QString packageListInputFile = "packages.html"; QString packageListOutputFile = "packages.txt"; Installer::Installer(QWidget *parent) : QDialog(parent) { /* QComboBox *downloadMirror = new QComboBox(this); downloadMirror->addItem("aleron (US)"); downloadMirror->addItem("belnet (BE)"); downloadMirror->addItem("easynews (US)"); downloadMirror->addItem("heanet (IE)"); downloadMirror->addItem("internap (US)"); downloadMirror->addItem("jaist (JP)"); downloadMirror->addItem("keihanna (JP)"); downloadMirror->addItem("kent (UK)"); downloadMirror->addItem("mesh (DE)"); downloadMirror->addItem("optusnet (AU)"); downloadMirror->addItem("ovh (FR)"); downloadMirror->addItem("puzzle (CH)"); downloadMirror->addItem("switch (CH)"); downloadMirror->addItem("umn (US)"); downloadMirror->addItem("unc (US)"); downloadMirror->addItem("voxel (US)"); */ urlLineEdit = new QLineEdit("http://sourceforge.net/project/showfiles.php?group_id=23617"); urlLabel = new QLabel(tr("&URL:")); urlLabel->setBuddy(urlLineEdit); statusLabel = new QLabel(tr("Please enter the URL of a file you want to " "download.")); downloadPackageListButton = new QPushButton(tr("Download Package List")); downloadPackageListButton->setDefault(true); quitButton = new QPushButton(tr("Quit")); DownloadPackageButton = new QPushButton(tr("Download")); installButton = new QPushButton(tr("Install")); parseButton = new QPushButton(tr("Parse")); progressDialog = new QProgressDialog(this); http = new QHttp(this); connect(urlLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableDownloadPackageButton())); connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool))); connect(http, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int))); connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &))); connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); connect(downloadPackageListButton, SIGNAL(clicked()), this, SLOT(downloadPackageList())); connect(DownloadPackageButton, SIGNAL(clicked()), this, SLOT(downloadFiles())); connect(parseButton, SIGNAL(clicked()), this, SLOT(parsePackageList())); connect(installButton, SIGNAL(clicked()), this, SLOT(installFiles())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); QHBoxLayout *topLayout = new QHBoxLayout; topLayout->addWidget(urlLabel); topLayout->addWidget(urlLineEdit); // topLayout->addWidget(downloadMirror); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(downloadPackageListButton); buttonLayout->addWidget(parseButton); buttonLayout->addWidget(DownloadPackageButton); buttonLayout->addWidget(installButton); buttonLayout->addWidget(quitButton); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(topLayout); mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout); // mainLayout->addWidget(parseOutput); setLayout(mainLayout); setWindowTitle(tr("HTTP")); urlLineEdit->setFocus(); } void Installer::downloadPackageList() { QUrl url(packageListURL); QString fileName = packageListInputFile; if (QFile::exists(fileName)) { QMessageBox::information(this, tr("HTTP"), tr("There already exists a file called %1 in " "the current directory.") .arg(fileName)); return; } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("HTTP"), tr("Unable to save the file %1: %2.") .arg(fileName).arg(file->errorString())); delete file; file = 0; return; } http->setHost(url.host(), url.port() != -1 ? url.port() : 80); if (!url.userName().isEmpty()) http->setUser(url.userName(), url.password()); httpRequestAborted = false; QByteArray query = url.encodedQuery(); httpGetId = http->get(url.path() + (!query.isEmpty() ? "?" + url.encodedQuery() : ""), file); progressDialog->setWindowTitle(tr("HTTP")); progressDialog->setLabelText(tr("Downloading %1.").arg(fileName)); DownloadPackageButton->setEnabled(false); } void Installer::cancelDownload() { statusLabel->setText(tr("Download canceled.")); httpRequestAborted = true; http->abort(); DownloadPackageButton->setEnabled(true); } void Installer::httpRequestFinished(int requestId, bool error) { 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)); } DownloadPackageButton->setEnabled(true); delete file; file = 0; } void Installer::readResponseHeader(const QHttpResponseHeader &responseHeader) { if (responseHeader.statusCode() != 200) { QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.") .arg(responseHeader.reasonPhrase())); httpRequestAborted = true; progressDialog->hide(); http->abort(); return; } } void Installer::updateDataReadProgress(int bytesRead, int totalBytes) { if (httpRequestAborted) return; progressDialog->setMaximum(totalBytes); progressDialog->setValue(bytesRead); } void Installer::enableDownloadPackageButton() { DownloadPackageButton->setEnabled(!urlLineEdit->text().isEmpty()); } void Installer::parsePackageList() { parseButton->setEnabled(false); QString fileName = packageListInputFile; QFile pkglist(fileName); if (!pkglist.exists()) { QMessageBox::information(this, tr("HTTP"), tr("could not open: %1") .arg(fileName) ); parseButton->setEnabled(true); return; } pkglist.open(QIODevice::ReadOnly); Package pkg; while (!pkglist.atEnd()) { QByteArray line = pkglist.readLine(); Package::parsePackageListLine(line); } PackageList::listPackages(); PackageList::writeToFile("packages.txt"); parseButton->setEnabled(true); } void Installer::downloadFiles() { QStringList list = PackageList::getPackageFiles("unzip"); for (int i = 0; i < list.size(); ++i) downloadSingleFile(list.at(i)); } void Installer::installFiles() { Package pkg = PackageList::getPackage("unzip"); pkg.install(pkg.binFileName(),"test"); } void Installer::downloadSingleFile(QString _url) { qWarning("downloading " + _url.toLatin1()); QUrl url(_url); QMessageBox::information(this, tr("HTTP"), tr("%1 " ".") .arg(url.toString())); QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); if (QFile::exists(fileName)) { QMessageBox::information(this, tr("HTTP"), tr("There already exists a file called %1 in " "the current directory.") .arg(fileName)); return; } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("HTTP"), tr("Unable to save the file %1: %2.") .arg(fileName).arg(file->errorString())); delete file; file = 0; return; } http->setHost(url.host(), url.port() != -1 ? url.port() : 80); if (!url.userName().isEmpty()) http->setUser(url.userName(), url.password()); httpRequestAborted = false; QByteArray query = url.encodedQuery(); httpGetId = http->get(url.path() + (!query.isEmpty() ? "?" + url.encodedQuery() : ""), file); } --- NEW FILE: installer.h --- /**************************************************************************** ** ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sa...@tr.... ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #ifndef INSTALLER_H #define INSTALLER_H #include <QDialog> class QFile; class QHttp; class QHttpResponseHeader; class QLabel; class QLineEdit; class QProgressDialog; class QPushButton; class QListView; class QAbstractItemModel; class Installer : public QDialog { Q_OBJECT public: Installer(QWidget *parent = 0); void downloadSingleFile(QString url); private slots: void downloadPackageList(); void parsePackageList(); void downloadFiles(); void installFiles(); void cancelDownload(); void httpRequestFinished(int requestId, bool error); void readResponseHeader(const QHttpResponseHeader &responseHeader); void updateDataReadProgress(int bytesRead, int totalBytes); void enableDownloadPackageButton(); private: QLabel *statusLabel; QLabel *urlLabel; QLineEdit *urlLineEdit; QProgressDialog *progressDialog; QPushButton *quitButton; QPushButton *downloadPackageListButton; QPushButton *DownloadPackageButton; QPushButton *parseButton; QPushButton *installButton; QHttp *http; QFile *file; int httpGetId; bool httpRequestAborted; }; #endif --- NEW FILE: installer.pro --- HEADERS += installer.h \ package.h \ packagelist.h SOURCES += installer.cpp \ package.cpp \ packagelist.cpp \ main.cpp QT += network # install target.path = $$[QT_INSTALL_EXAMPLES]/network/http sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS http.pro sources.path = $$[QT_INSTALL_EXAMPLES]/network/http INSTALLS += target sources --- NEW FILE: main.cpp --- /**************************************************************************** ** ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sa...@tr.... ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include <QApplication> #include "Installer.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Installer httpWin; httpWin.show(); return httpWin.exec(); } --- NEW FILE: package.cpp --- /**************************************************************************** ** ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sa...@tr.... ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include "package.h" #include "packagelist.h" #include <QtGui> #include <QtNetwork> QString Package::baseURL = "http://mesh.dl.sourceforge.net/sourceforge/gnuwin32/"; void Package::parsePackageListLine(const QByteArray &line) { static Package pkg; if (line.contains("<td><a href=\"/project/showfiles.php?group_id=23617")) { int a = line.indexOf("\">") + 2; int b = line.indexOf("</a>"); QByteArray value = line.mid(a,b-a); if (line.indexOf("release_id") > -1) { pkg.setVersion(value); PackageList::addPackage(pkg); } else pkg.setName(value); } } void Package::install(const QString &fileName,const QString &destdir) { QString cmd = "unzip -o"; if (destdir != "") cmd += " -d " + destdir; cmd += " " + fileName; qDebug() << cmd; QProcess unzip; unzip.setReadChannelMode(QProcess::MergedChannels); unzip.start(cmd); if (!unzip.waitForFinished()) qDebug() << "unzip failed:" << unzip.errorString(); else qDebug() << "unzip output:" << unzip.readAll(); } void Package::logOutput() { } --- NEW FILE: package.h --- /**************************************************************************** ** ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sa...@tr.... ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #ifndef PACKAGE_H #define PACKAGE_H #include <QtGui> #include <QtNetwork> class Package { public: Package() {} Package(QString const &_name, QString const &_version) { name = _name; version = _version; } void setName(QString const &_name) { name = _name; } QString &Name() { return name; } void setVersion(QString const &_version) { version = _version; } static void parsePackageListLine(const QByteArray &line); QString toString() { return name + "-" + version; } const QString binURL() { return baseURL + name + "-" + version + "-bin.zip"; } const QString libURL() { return baseURL + name + "-" + version + "-lib.zip"; } const QString docURL() { return baseURL + name + "-" + version + "-doc.zip"; } const QString srcURL() { return baseURL + name + "-" + version + "-src.zip"; } const QString binFileName() { return name + "-" + version + "-bin.zip"; } const QString libFileName() { return name + "-" + version + "-lib.zip"; } const QString docFileName() { return name + "-" + version + "-doc.zip"; } const QString srcFileName() { return name + "-" + version + "-src.zip"; } static QString baseURL; void install(const QString &fileName,const QString &destdir=0); private slots: void logOutput(); private: QString name; QString version; }; #endif --- NEW FILE: packagelist.cpp --- /**************************************************************************** ** ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sa...@tr.... ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include <QtGui> #include <QtNetwork> #include "packagelist.h" QList<Package> PackageList::packageList; void PackageList::addPackage(Package const &package) { packageList.append(package); } void PackageList::listPackages(void) { QList<Package>::iterator i; for (i = packageList.begin(); i != packageList.end(); ++i) qWarning(i->toString().toLatin1()); } bool PackageList::writeToFile(QString const &fileName) { QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false; QTextStream out(&file); out << "# package list" << "\n"; QList<Package>::iterator i; for (i = packageList.begin(); i != packageList.end(); ++i) out << i->toString().toLatin1() << "\n"; return true; } QStringList PackageList::getPackageFiles(QString const &pkgName) { QList<Package>::iterator i; QStringList list; for (i = packageList.begin(); i != packageList.end(); ++i) { if (i->Name() == pkgName) { list << i->binURL(); list << i->libURL(); list << i->docURL(); list << i->srcURL(); } } return list; } Package PackageList::getPackage(QString const &pkgName) { QList<Package>::iterator i; for (i = packageList.begin(); i != packageList.end(); ++i) if (i->Name() == pkgName) return *i; return Package(); } --- NEW FILE: packagelist.h --- /**************************************************************************** ** ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sa...@tr.... ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #ifndef PACKAGELIST_H #define PACKAGELIST_H #include "package.h" class PackageList { public: static void addPackage(Package const &package); static void listPackages(void); static bool writeToFile(QString const &fileName); // static bool downloadPackage(QString const &pkgName); static QStringList getPackageFiles(QString const &pkgName); static Package getPackage(QString const &pkgName); private: static QList<Package> packageList; }; #endif --- NEW FILE: packages.h --- /**************************************************************************** ** ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sa...@tr.... ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include <QtGui> #include <QtNetwork> #include <QStringListModel> #include "Installer.h" class Package { public: Package() {} Package(QString const &_name, QString const &_version) { name = _name; version = _version; } void setName(QString const &_name) { name = _name; } void setVersion(QString const &_version) { version = _version; } QString toString() { return name + "-" + version; } const QString binURL() { return baseURL + name + "-" + version + "-bin.zip"; } const QString libURL() { return baseURL + name + "-" + version + "-lib.zip"; } const QString docURL() { return baseURL + name + "-" + version + "-doc.zip"; } const QString srcURL() { return baseURL + name + "-" + version + "-src.zip"; } static QString baseURL; private: QString name; QString version; }; QString Package::baseURL = "http://mesh.dl.sourceforge.net/sourceforge/gnuwin32/"; QList<Package> packageList; class PackageList { public: static void addPackage(Package const &package); static void listPackages(void); static bool writeToFile(QString const &fileName); private: // QList<Package> packageList; }; void PackageList::addPackage(Package const &package) { packageList << package; } void PackageList::listPackages(void) { QList<Package>::iterator i; for (i = packageList.begin(); i != packageList.end(); ++i) qWarning(i->toString().toLatin1()); } bool PackageList::writeToFile(QString const &fileName) { QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false; QTextStream out(&file); out << "# package list" << "\n"; QList<Package>::iterator i; for (i = packageList.begin(); i != packageList.end(); ++i) out << i->toString().toLatin1() << "\n"; return true; } Installer::Installer(QWidget *parent) : QDialog(parent) { /* QListWidget *downloadMirror = new QListWidget(this); downloadMirror->addItem("aleron (US)"); downloadMirror->addItem("belnet (BE)"); downloadMirror->addItem("easynews (US)"); downloadMirror->addItem("heanet (IE)"); downloadMirror->addItem("internap (US)"); downloadMirror->addItem("jaist (JP)"); downloadMirror->addItem("keihanna (JP)"); downloadMirror->addItem("kent (UK)"); downloadMirror->addItem("mesh (DE)"); downloadMirror->addItem("optusnet (AU)"); downloadMirror->addItem("ovh (FR)"); downloadMirror->addItem("puzzle (CH)"); downloadMirror->addItem("switch (CH)"); downloadMirror->addItem("umn (US)"); downloadMirror->addItem("unc (US)"); downloadMirror->addItem("voxel (US)"); */ urlLineEdit = new QLineEdit("http://sourceforge.net/project/showfiles.php?group_id=23617"); urlLabel = new QLabel(tr("&URL:")); urlLabel->setBuddy(urlLineEdit); statusLabel = new QLabel(tr("Please enter the URL of a file you want to " "download.")); quitButton = new QPushButton(tr("Quit")); DownloadPackageButton = new QPushButton(tr("Download")); DownloadPackageButton->setDefault(true); parseButton = new QPushButton(tr("Parse")); QListView *parseOutput = new QListView(this); progressDialog = new QProgressDialog(this); http = new QHttp(this); connect(urlLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableDownloadPackageButton())); connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool))); connect(http, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int))); connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &))); connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); connect(DownloadPackageButton, SIGNAL(clicked()), this, SLOT(downloadFile())); connect(parseButton, SIGNAL(clicked()), this, SLOT(parseFile())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); QHBoxLayout *topLayout = new QHBoxLayout; topLayout->addWidget(urlLabel); topLayout->addWidget(urlLineEdit); // topLayout->addWidget(downloadMirror); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(DownloadPackageButton); buttonLayout->addWidget(parseButton); buttonLayout->addWidget(quitButton); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(topLayout); mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout); mainLayout->addWidget(parseOutput); setLayout(mainLayout); setWindowTitle(tr("HTTP")); urlLineEdit->setFocus(); } void Installer::_downloadFile() { QUrl url(urlLineEdit->text()); // QString a = "http://" + downloadMirror->currentItem()->text() + ".dl.sourceforge.net/sourceforge/gnuwin32/" ; //QMessageBox::information(this, tr("HTTP"),"http://" + downloadMirror->currentItem()->text() + ".dl.sourceforge.net/sourceforge/gnuwin32/" ); // QUrl url("http://" + downloadMirror->currentItem()->text() + ".dl.sourceforge.net/sourceforge/gnuwin32/"); QMessageBox::information(this, tr("HTTP"), tr("%1 " ".") .arg(url.toString())); QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); if (QFile::exists(fileName)) { QMessageBox::information(this, tr("HTTP"), tr("There already exists a file called %1 in " "the current directory.") .arg(fileName)); return; } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("HTTP"), tr("Unable to save the file %1: %2.") .arg(fileName).arg(file->errorString())); delete file; file = 0; return; } http->setHost(url.host(), url.port() != -1 ? url.port() : 80); if (!url.userName().isEmpty()) http->setUser(url.userName(), url.password()); httpRequestAborted = false; QByteArray query = url.encodedQuery(); httpGetId = http->get(url.path() + (!query.isEmpty() ? "?" + url.encodedQuery() : ""), file); progressDialog->setWindowTitle(tr("HTTP")); progressDialog->setLabelText(tr("Downloading %1.").arg(fileName)); DownloadPackageButton->setEnabled(false); } void Installer::cancelDownload() { statusLabel->setText(tr("Download canceled.")); httpRequestAborted = true; http->abort(); DownloadPackageButton->setEnabled(true); } void Installer::httpRequestFinished(int requestId, bool error) { 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)); } DownloadPackageButton->setEnabled(true); delete file; file = 0; } void Installer::readResponseHeader(const QHttpResponseHeader &responseHeader) { if (responseHeader.statusCode() != 200) { QMessageBox::information(this, tr("HTTP"), tr("Download failed: %1.") .arg(responseHeader.reasonPhrase())); httpRequestAborted = true; progressDialog->hide(); http->abort(); return; } } void Installer::updateDataReadProgress(int bytesRead, int totalBytes) { if (httpRequestAborted) return; progressDialog->setMaximum(totalBytes); progressDialog->setValue(bytesRead); } void Installer::enableDownloadPackageButton() { DownloadPackageButton->setEnabled(!urlLineEdit->text().isEmpty()); } void Installer::parseFile() { parseButton->setEnabled(false); // numbers << "test"; // model = new QStringListModel(numbers); // parseOutput->setModel(model); QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName(); QMessageBox::information(this, tr("HTTP"), tr("try to open: %1.") .arg(fileName) ); QFile file(fileName); if (!file.exists()) { QMessageBox::information(this, tr("HTTP"), tr("could not open: %1.") .arg(fileName) ); return; } file.open(QIODevice::ReadOnly); Package pkg; while (!file.atEnd()) { QByteArray line = file.readLine(); // parseOutput->addItem(line.data()); if (line.contains("<td><a href=\"/project/showfiles.php?group_id=23617")) { int a = line.indexOf("\">") + 2; int b = line.indexOf("</a>"); QByteArray value = line.mid(a,b-a); if (line.indexOf("release_id") > -1) { pkg.setVersion(value); PackageList::addPackage(pkg); } else pkg.setName(value); } } PackageList::listPackages(); // QList<Package>::iterator i; // for (i = packageList.begin(); i != packageList.end(); ++i) // qWarning(i->toString().toLatin1()); PackageList::writeToFile("packages.txt"); parseButton->setEnabled(true); } void Installer::downloadFile() { QList<Package>::iterator i; i = packageList.begin(); downloadSingleFile(i->binURL()); downloadSingleFile(i->libURL()); downloadSingleFile(i->docURL()); downloadSingleFile(i->srcURL()); } void Installer::downloadSingleFile(QString _url) { QUrl url(_url); QMessageBox::information(this, tr("HTTP"), tr("%1 " ".") .arg(url.toString())); QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); if (QFile::exists(fileName)) { QMessageBox::information(this, tr("HTTP"), tr("There already exists a file called %1 in " "the current directory.") .arg(fileName)); return; } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("HTTP"), tr("Unable to save the file %1: %2.") .arg(fileName).arg(file->errorString())); delete file; file = 0; return; } http->setHost(url.host(), url.port() != -1 ? url.port() : 80); if (!url.userName().isEmpty()) http->setUser(url.userName(), url.password()); httpRequestAborted = false; QByteArray query = url.encodedQuery(); httpGetId = http->get(url.path() + (!query.isEmpty() ? "?" + url.encodedQuery() : ""), file); } |