From 6213db49c8281ed9cb84e32b1cee261202f9ad73 Mon Sep 17 00:00:00 2001 From: Samuli Piippo Date: Tue, 19 Nov 2013 16:22:42 +0200 Subject: Move TestRepository from sdk to own file in installer lib Moved the class to installer lib so it can be reused in ProductKeyCheck as well. Change-Id: I8b207f4816505671fa05ebf602895978e04d13e9 Reviewed-by: Karsten Heimrich --- src/libs/installer/installer.pro | 6 +- src/libs/installer/testrepository.cpp | 168 ++++++++++++++++++++++++++++++++++ src/libs/installer/testrepository.h | 95 +++++++++++++++++++ 3 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 src/libs/installer/testrepository.cpp create mode 100644 src/libs/installer/testrepository.h (limited to 'src/libs') diff --git a/src/libs/installer/installer.pro b/src/libs/installer/installer.pro index 1595773f2..197e853c3 100644 --- a/src/libs/installer/installer.pro +++ b/src/libs/installer/installer.pro @@ -102,7 +102,8 @@ HEADERS += packagemanagercore.h \ applyproductkeyoperation.h \ globals.h \ graph.h \ - settingsoperation.h + settingsoperation.h \ + testrepository.h SOURCES += packagemanagercore.cpp \ packagemanagercore_p.cpp \ @@ -165,7 +166,8 @@ HEADERS += packagemanagercore.h \ packagemanagercoredata.cpp \ applyproductkeyoperation.cpp \ globals.cpp \ - settingsoperation.cpp + settingsoperation.cpp \ + testrepository.cpp RESOURCES += resources/patch_file_lists.qrc \ resources/installer.qrc diff --git a/src/libs/installer/testrepository.cpp b/src/libs/installer/testrepository.cpp new file mode 100644 index 000000000..a39c499a1 --- /dev/null +++ b/src/libs/installer/testrepository.cpp @@ -0,0 +1,168 @@ +/************************************************************************** +** +** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.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 the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +**************************************************************************/ +#include "testrepository.h" + +#include +#include + +#include + +using namespace QInstaller; + +TestRepository::TestRepository(QObject *parent) + : KDJob(parent) + , m_downloader(0) +{ + setTimeout(10000); + setAutoDelete(false); + setCapabilities(Cancelable); +} + +TestRepository::~TestRepository() +{ + if (m_downloader) + m_downloader->deleteLater(); +} + +Repository TestRepository::repository() const +{ + return m_repository; +} + +void TestRepository::setRepository(const Repository &repository) +{ + cancel(); + + setError(NoError); + setErrorString(QString()); + m_repository = repository; +} + +void TestRepository::doStart() +{ + if (m_downloader) + m_downloader->deleteLater(); + + const QUrl url = m_repository.url(); + if (url.isEmpty()) { + emitFinishedWithError(InvalidUrl, tr("Empty repository URL.")); + return; + } + + m_downloader = KDUpdater::FileDownloaderFactory::instance().create(url.scheme(), this); + if (!m_downloader) { + emitFinishedWithError(InvalidUrl, tr("URL scheme not supported: %1 (%2).") + .arg(url.scheme(), url.toString())); + return; + } + + QAuthenticator auth; + auth.setUser(m_repository.username()); + auth.setPassword(m_repository.password()); + m_downloader->setAuthenticator(auth); + + connect(m_downloader, SIGNAL(downloadCompleted()), this, SLOT(downloadCompleted())); + connect(m_downloader, SIGNAL(downloadAborted(QString)), this, SLOT(downloadAborted(QString)), + Qt::QueuedConnection); + connect(m_downloader, SIGNAL(authenticatorChanged(QAuthenticator)), this, + SLOT(onAuthenticatorChanged(QAuthenticator))); + + m_downloader->setAutoRemoveDownloadedFile(true); + m_downloader->setUrl(QUrl(url.toString() + QString::fromLatin1("/Updates.xml"))); + + m_downloader->download(); +} + +void TestRepository::doCancel() +{ + if (m_downloader) { + QString errorString = m_downloader->errorString(); + if (errorString.isEmpty()) + errorString = tr("Got a timeout while testing: '%1'").arg(m_repository.displayname()); + // at the moment the download sends downloadCompleted() if we cancel it, so just + disconnect(m_downloader, 0, this, 0); + m_downloader->cancelDownload(); + emitFinishedWithError(KDJob::Canceled, errorString); + } +} + +void TestRepository::downloadCompleted() +{ + QString errorMsg; + int error = DownloadError; + + if (m_downloader->isDownloaded()) { + QFile file(m_downloader->downloadedFileName()); + if (file.exists() && file.open(QIODevice::ReadOnly)) { + QDomDocument doc; + QString errorMsg; + if (!doc.setContent(&file, &errorMsg)) { + error = InvalidUpdatesXml; + errorMsg = tr("Could not parse Updates.xml! Error: %1.").arg(errorMsg); + } else { + error = NoError; + } + } else { + errorMsg = tr("Updates.xml could not be opened for reading!"); + } + } else { + errorMsg = tr("Updates.xml could not be found on server!"); + } + + if (error > NoError) + emitFinishedWithError(error, errorMsg); + else + emitFinished(); + + m_downloader->deleteLater(); + m_downloader = 0; +} + +void TestRepository::downloadAborted(const QString &reason) +{ + emitFinishedWithError(DownloadError, reason); +} + +void TestRepository::onAuthenticatorChanged(const QAuthenticator &authenticator) +{ + m_repository.setUsername(authenticator.user()); + m_repository.setPassword(authenticator.password()); +} diff --git a/src/libs/installer/testrepository.h b/src/libs/installer/testrepository.h new file mode 100644 index 000000000..c1ecc039c --- /dev/null +++ b/src/libs/installer/testrepository.h @@ -0,0 +1,95 @@ +/************************************************************************** +** +** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.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 the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +**************************************************************************/ +#ifndef TESTREPOSITORY_H +#define TESTREPOSITORY_H + +#include "qinstallerglobal.h" + +#include +#include + +#include + +QT_BEGIN_NAMESPACE +class QAuthenticator; +class QLocale; +class QVariant; +QT_END_NAMESPACE + +namespace KDUpdater { + class FileDownloader; +} + +namespace QInstaller { + class PackageManagerCore; +} + +namespace QInstaller { + +class INSTALLER_EXPORT TestRepository : public KDJob +{ + Q_OBJECT + +public: + + explicit TestRepository(QObject *parent = 0); + ~TestRepository(); + + QInstaller::Repository repository() const; + void setRepository(const QInstaller::Repository &repository); + +private: + void doStart(); + void doCancel(); + +private Q_SLOTS: + void downloadCompleted(); + void downloadAborted(const QString &reason); + void onAuthenticatorChanged(const QAuthenticator &authenticator); + +private: + QInstaller::Repository m_repository; + KDUpdater::FileDownloader *m_downloader; +}; + +} //namespace QInstaller + +#endif // TESTREPOSITORY_H -- cgit v1.2.3