Commit 8a3e5d96 authored by Raphael Kubo da Costa's avatar Raphael Kubo da Costa
Browse files

Use a QScopedPointer to keep only one Archive instance alive at a time.

Even though having more than one Archive instance around is harmless, it
wastes resources with no purpose.

We thus guard the Archive pointer in Part::openFile() with a
QScopedPointer so that if one is trying to create an archive whose type
is read-only for Ark (ie. .gz files) and then chooses another mimetype,
the first created instance is deleted right away, and not when
ArchiveModel is destroyed.

ArchiveModel::m_archive is now also a QScopedPointer, so that we do not
need to manually delete the current archive when
ArchiveModel::setArchive() is called.
parent 24da06e7
...@@ -262,7 +262,7 @@ int ArchiveNode::row() const ...@@ -262,7 +262,7 @@ int ArchiveNode::row() const
} }
ArchiveModel::ArchiveModel(QObject *parent) ArchiveModel::ArchiveModel(QObject *parent)
: QAbstractItemModel(parent), m_archive(0), : QAbstractItemModel(parent),
m_rootNode(new ArchiveDirNode(0, ArchiveEntry())) m_rootNode(new ArchiveDirNode(0, ArchiveEntry()))
{ {
} }
...@@ -852,13 +852,13 @@ void ArchiveModel::insertNode(ArchiveNode *node, InsertBehaviour behaviour) ...@@ -852,13 +852,13 @@ void ArchiveModel::insertNode(ArchiveNode *node, InsertBehaviour behaviour)
Kerfuffle::Archive* ArchiveModel::archive() const Kerfuffle::Archive* ArchiveModel::archive() const
{ {
return m_archive; return m_archive.data();
} }
KJob* ArchiveModel::setArchive(Kerfuffle::Archive *archive) KJob* ArchiveModel::setArchive(Kerfuffle::Archive *archive)
{ {
delete m_archive; m_archive.reset(archive);
m_archive = archive;
m_rootNode->clear(); m_rootNode->clear();
s_previousMatch = 0; s_previousMatch = 0;
s_previousPieces->clear(); s_previousPieces->clear();
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#define ARCHIVEMODEL_H #define ARCHIVEMODEL_H
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QScopedPointer>
#include <kjobtrackerinterface.h> #include <kjobtrackerinterface.h>
#include "kerfuffle/archive.h" #include "kerfuffle/archive.h"
...@@ -115,7 +117,7 @@ private: ...@@ -115,7 +117,7 @@ private:
QList<Kerfuffle::ArchiveEntry> m_newArchiveEntries; // holds entries from opening a new archive until it's totally open QList<Kerfuffle::ArchiveEntry> m_newArchiveEntries; // holds entries from opening a new archive until it's totally open
QList<int> m_showColumns; QList<int> m_showColumns;
Kerfuffle::Archive *m_archive; QScopedPointer<Kerfuffle::Archive> m_archive;
ArchiveDirNode *m_rootNode; ArchiveDirNode *m_rootNode;
}; };
......
...@@ -60,6 +60,7 @@ ...@@ -60,6 +60,7 @@
#include <QMenu> #include <QMenu>
#include <QMimeData> #include <QMimeData>
#include <QMouseEvent> #include <QMouseEvent>
#include <QScopedPointer>
#include <QSplitter> #include <QSplitter>
#include <QTimer> #include <QTimer>
#include <QVBoxLayout> #include <QVBoxLayout>
...@@ -397,7 +398,7 @@ bool Part::openFile() ...@@ -397,7 +398,7 @@ bool Part::openFile()
} }
} }
Kerfuffle::Archive *archive = Kerfuffle::Archive::create(localFile, m_model); QScopedPointer<Kerfuffle::Archive> archive(Kerfuffle::Archive::create(localFile, m_model));
if ((!archive) || ((creatingNewArchive) && (archive->isReadOnly()))) { if ((!archive) || ((creatingNewArchive) && (archive->isReadOnly()))) {
QStringList mimeTypeList; QStringList mimeTypeList;
...@@ -440,7 +441,7 @@ bool Part::openFile() ...@@ -440,7 +441,7 @@ bool Part::openFile()
return false; return false;
} }
archive = Kerfuffle::Archive::create(localFile, mimeTypes.key(item), m_model); archive.reset(Kerfuffle::Archive::create(localFile, mimeTypes.key(item), m_model));
} }
if (!archive) { if (!archive) {
...@@ -448,7 +449,7 @@ bool Part::openFile() ...@@ -448,7 +449,7 @@ bool Part::openFile()
return false; return false;
} }
KJob *job = m_model->setArchive(archive); KJob *job = m_model->setArchive(archive.take());
registerJob(job); registerJob(job);
job->start(); job->start();
m_infoPanel->setIndex(QModelIndex()); m_infoPanel->setIndex(QModelIndex());
......
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