Use shared pointers to fix incompatibility with new Qt

This commit is contained in:
Petr Mrázek 2013-06-22 23:34:33 +02:00
parent 3b38e5f924
commit 929698ff15
4 changed files with 46 additions and 36 deletions

View File

@ -28,6 +28,8 @@
#include "instance.h" #include "instance.h"
#include "libmmc_config.h" #include "libmmc_config.h"
class FileToDownload;
typedef QSharedPointer<FileToDownload> PtrFileToDownload;
class FileToDownload : public QObject class FileToDownload : public QObject
{ {
@ -43,9 +45,11 @@ class FileToDownload : public QObject
* This path is relative to the instance's root directory. * This path is relative to the instance's root directory.
*/ */
Q_PROPERTY(QString path READ path WRITE setPath) Q_PROPERTY(QString path READ path WRITE setPath)
public:
private:
FileToDownload(const QUrl &url, const QString &path, QObject *parent = 0); FileToDownload(const QUrl &url, const QString &path, QObject *parent = 0);
FileToDownload(const FileToDownload &other); public:
static PtrFileToDownload Create(const QUrl &url, const QString &path, QObject *parent = 0);
virtual QUrl url() const { return m_dlURL; } virtual QUrl url() const { return m_dlURL; }
virtual void setURL(const QUrl &url) { m_dlURL = url; } virtual void setURL(const QUrl &url) { m_dlURL = url; }
@ -58,6 +62,8 @@ private:
QString m_dlPath; QString m_dlPath;
}; };
/*! /*!
* The game update task is the task that handles downloading instances' files. * The game update task is the task that handles downloading instances' files.
*/ */
@ -86,7 +92,7 @@ public:
virtual void executeTask(); virtual void executeTask();
virtual bool downloadFile(const FileToDownload &file); virtual bool downloadFile(const PtrFileToDownload file);
////////////////////// //////////////////////
@ -149,7 +155,7 @@ private:
//////////////////////// ////////////////////////
// List of URLs that the game updater will need to download. // List of URLs that the game updater will need to download.
QList<FileToDownload> m_downloadList; QList<PtrFileToDownload> m_downloadList;
int m_currentDownload; int m_currentDownload;

View File

@ -18,7 +18,7 @@
#include <QObject> #include <QObject>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QSharedPointer>
#include <QUrl> #include <QUrl>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
@ -26,6 +26,9 @@
#include "libmmc_config.h" #include "libmmc_config.h"
class LWJGLVersion;
typedef QSharedPointer<LWJGLVersion> PtrLWJGLVersion;
class LIBMULTIMC_EXPORT LWJGLVersion : public QObject class LIBMULTIMC_EXPORT LWJGLVersion : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -39,12 +42,15 @@ class LIBMULTIMC_EXPORT LWJGLVersion : public QObject
* The URL for this version of LWJGL. * The URL for this version of LWJGL.
*/ */
Q_PROPERTY(QUrl url READ url) Q_PROPERTY(QUrl url READ url)
public:
LWJGLVersion(const QString &name, const QUrl &url, QObject *parent = 0) : LWJGLVersion(const QString &name, const QUrl &url, QObject *parent = 0) :
QObject(parent), m_name(name), m_url(url) { } QObject(parent), m_name(name), m_url(url) { }
public:
LWJGLVersion(const LWJGLVersion &other) : static PtrLWJGLVersion Create(const QString &name, const QUrl &url, QObject *parent = 0)
QObject(other.parent()), m_name(other.name()), m_url(other.url()) { } {
return PtrLWJGLVersion(new LWJGLVersion(name, url, parent));
};
QString name() const { return m_name; } QString name() const { return m_name; }
@ -65,9 +71,9 @@ public:
bool isLoaded() { return m_vlist.length() > 0; } bool isLoaded() { return m_vlist.length() > 0; }
const LWJGLVersion *getVersion(const QString &versionName); const PtrLWJGLVersion getVersion(const QString &versionName);
LWJGLVersion &at(int index) { return m_vlist[index]; } PtrLWJGLVersion at(int index) { return m_vlist[index]; }
const LWJGLVersion &at(int index) const { return m_vlist[index]; } const PtrLWJGLVersion at(int index) const { return m_vlist[index]; }
int count() const { return m_vlist.length(); } int count() const { return m_vlist.length(); }
@ -104,7 +110,7 @@ signals:
void loadListFailed(QString msg); void loadListFailed(QString msg);
private: private:
QList<LWJGLVersion> m_vlist; QList<PtrLWJGLVersion> m_vlist;
QNetworkReply *m_netReply; QNetworkReply *m_netReply;

View File

@ -78,7 +78,7 @@ void GameUpdateTask::executeTask()
QUrl mcJarURL = targetVersion->downloadURL() + jarFilename + ".jar"; QUrl mcJarURL = targetVersion->downloadURL() + jarFilename + ".jar";
qDebug() << mcJarURL.toString(); qDebug() << mcJarURL.toString();
m_downloadList.append(FileToDownload(mcJarURL, PathCombine(m_inst->minecraftDir(), "bin/minecraft.jar"))); m_downloadList.append(FileToDownload::Create(mcJarURL, PathCombine(m_inst->minecraftDir(), "bin/minecraft.jar")));
@ -111,10 +111,10 @@ void GameUpdateTask::executeTask()
emit gameUpdateComplete(m_response); emit gameUpdateComplete(m_response);
} }
bool GameUpdateTask::downloadFile(const FileToDownload &file) bool GameUpdateTask::downloadFile( const PtrFileToDownload file )
{ {
setSubStatus("Downloading " + file.url().toString()); setSubStatus("Downloading " + file->url().toString());
QNetworkReply *reply = netMgr->get(QNetworkRequest(file.url())); QNetworkReply *reply = netMgr->get(QNetworkRequest(file->url()));
this->connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this->connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
SLOT(updateDownloadProgress(qint64,qint64))); SLOT(updateDownloadProgress(qint64,qint64)));
@ -123,16 +123,17 @@ bool GameUpdateTask::downloadFile(const FileToDownload &file)
if (reply->error() == QNetworkReply::NoError) if (reply->error() == QNetworkReply::NoError)
{ {
QFile outFile = file.path(); QString filePath = file->path();
QFile outFile(filePath);
if (outFile.exists() && !outFile.remove()) if (outFile.exists() && !outFile.remove())
{ {
error("Can't delete old file " + file.path() + ": " + outFile.errorString()); error("Can't delete old file " + file->path() + ": " + outFile.errorString());
return false; return false;
} }
if (!outFile.open(QIODevice::WriteOnly)) if (!outFile.open(QIODevice::WriteOnly))
{ {
error("Can't write to " + file.path() + ": " + outFile.errorString()); error("Can't write to " + file->path() + ": " + outFile.errorString());
return false; return false;
} }
@ -141,7 +142,7 @@ bool GameUpdateTask::downloadFile(const FileToDownload &file)
} }
else else
{ {
error("Can't download " + file.url().toString() + ": " + reply->errorString()); error("Can't download " + file->url().toString() + ": " + reply->errorString());
return false; return false;
} }
@ -230,16 +231,13 @@ void GameUpdateTask::updateDownloadProgress(qint64 current, qint64 total)
setProgress((int)(overallDLProgress * 100)); setProgress((int)(overallDLProgress * 100));
} }
PtrFileToDownload FileToDownload::Create(const QUrl &url, const QString &path, QObject *parent)
{
return PtrFileToDownload(new FileToDownload (url, path, parent));
}
FileToDownload::FileToDownload(const QUrl &url, const QString &path, QObject *parent) : FileToDownload::FileToDownload(const QUrl &url, const QString &path, QObject *parent) :
QObject(parent), m_dlURL(url), m_dlPath(path) QObject(parent), m_dlURL(url), m_dlPath(path)
{ {
} }
FileToDownload::FileToDownload(const FileToDownload &other) :
QObject(other.parent()), m_dlURL(other.m_dlURL), m_dlPath(other.m_dlPath)
{
}

View File

@ -45,15 +45,15 @@ QVariant LWJGLVersionList::data(const QModelIndex &index, int role) const
if (index.row() > count()) if (index.row() > count())
return QVariant(); return QVariant();
const LWJGLVersion &version = at(index.row()); const PtrLWJGLVersion version = at(index.row());
switch (role) switch (role)
{ {
case Qt::DisplayRole: case Qt::DisplayRole:
return version.name(); return version->name();
case Qt::ToolTipRole: case Qt::ToolTipRole:
return version.url().toString(); return version->url().toString();
default: default:
return QVariant(); return QVariant();
@ -125,7 +125,7 @@ void LWJGLVersionList::netRequestComplete()
QDomNodeList items = doc.elementsByTagName("item"); QDomNodeList items = doc.elementsByTagName("item");
QList<LWJGLVersion> tempList; QList<PtrLWJGLVersion> tempList;
for (int i = 0; i < items.length(); i++) for (int i = 0; i < items.length(); i++)
{ {
@ -155,7 +155,7 @@ void LWJGLVersionList::netRequestComplete()
continue; continue;
} }
tempList.append(LWJGLVersion(name, link)); tempList.append(LWJGLVersion::Create(name, link));
} }
} }
@ -175,14 +175,14 @@ void LWJGLVersionList::netRequestComplete()
reply->deleteLater(); reply->deleteLater();
} }
const LWJGLVersion *LWJGLVersionList::getVersion(const QString &versionName) const PtrLWJGLVersion LWJGLVersionList::getVersion(const QString &versionName)
{ {
for (int i = 0; i < count(); i++) for (int i = 0; i < count(); i++)
{ {
if (at(i).name() == versionName) if (at(i)->name() == versionName)
return &at(i); return at(i);
} }
return NULL; return PtrLWJGLVersion();
} }