diff options
author | Arno <arno@disconnect.de> | 2018-01-02 23:10:08 +0100 |
---|---|---|
committer | Arno <arno@disconnect.de> | 2018-01-02 23:10:08 +0100 |
commit | aab351093e3ba713f1179a797d0f5929bdb4c92c (patch) | |
tree | 512b6858491e209f4d7be1f9fe46a0921c8523df | |
parent | b5dc8c10367537aec8ce7c061f608ca896ec9f36 (diff) | |
download | ShemovCleaner-aab351093e3ba713f1179a797d0f5929bdb4c92c.tar.gz ShemovCleaner-aab351093e3ba713f1179a797d0f5929bdb4c92c.tar.bz2 ShemovCleaner-aab351093e3ba713f1179a797d0f5929bdb4c92c.zip |
Save CachedFileData to disk
Since we already have the data, save it to speed up the start.
Surprisingly, the hardest part was to figure out how to use
QStandardPaths to find a location where to save the file. I went with
the semi-hardcoded path HomeLocation/.shemovcleaner, even though we're
on windows.
-rw-r--r-- | cachedfiledata.cpp | 10 | ||||
-rw-r--r-- | cachedfiledata.h | 4 | ||||
-rw-r--r-- | filewidget.cpp | 27 | ||||
-rw-r--r-- | filewidget.h | 2 |
4 files changed, 42 insertions, 1 deletions
diff --git a/cachedfiledata.cpp b/cachedfiledata.cpp index 6f51edb..773f411 100644 --- a/cachedfiledata.cpp +++ b/cachedfiledata.cpp @@ -1,3 +1,13 @@ #include "cachedfiledata.h" CachedFileData::CachedFileData() : seconds(0), size(0), attr(-1), copied(false) {} + +QDataStream &operator<<(QDataStream &out, const CachedFileData &cfd){ + out << cfd.fullPath << cfd.name << cfd.mimeType << cfd.duration << cfd.md5Sum << cfd.seconds << cfd.size << cfd.attr << cfd.copied; + return out; +} + +QDataStream &operator>>(QDataStream &in, CachedFileData &cfd){ + in >> cfd.fullPath >> cfd.name >> cfd.mimeType >> cfd.duration >> cfd.md5Sum >> cfd.seconds >> cfd.size >> cfd.attr >> cfd.copied; + return in; +} diff --git a/cachedfiledata.h b/cachedfiledata.h index 9b5fe48..4aa1e20 100644 --- a/cachedfiledata.h +++ b/cachedfiledata.h @@ -2,6 +2,7 @@ #define CACHEDFILEDATA_H #include <QString> +#include <QDataStream> struct CachedFileData { explicit CachedFileData(); @@ -16,4 +17,7 @@ struct CachedFileData { bool copied; }; +QDataStream &operator<<(QDataStream &out, const CachedFileData &cfd); +QDataStream &operator>>(QDataStream &in, CachedFileData &cfd); + #endif // CACHEDFILEDATA_H diff --git a/filewidget.cpp b/filewidget.cpp index e2be4e1..458dd45 100644 --- a/filewidget.cpp +++ b/filewidget.cpp @@ -31,6 +31,7 @@ #include <QApplication> #include <QContextMenuEvent> #include <QDirIterator> +#include <QStandardPaths> #include "filewidget.h" #include "filesorter.h" @@ -47,6 +48,19 @@ FileWidget::FileWidget(QWidget *parent) : QWidget(parent), mCopyToMenu(0) { mFileCopier = new FileCopier(this); mCopyProgress = new ProgressDialog; mFileCache.setMaxCost(500); + QString cacheDir = QStandardPaths::locate(QStandardPaths::HomeLocation, ".shemovcleaner", QStandardPaths::LocateDirectory); + if(!cacheDir.isEmpty()){ + QString cacheFile = QString("%1/%2").arg(cacheDir).arg("cfd.dat"); + QFile f(cacheFile); + if(f.open(QIODevice::ReadOnly)){ + QDataStream in(&f); + while(!in.atEnd()){ + CachedFileData *fd = new CachedFileData; + in >> *fd; + mFileCache.insert(fd->fullPath, fd); + } + } + } connect(mFileCopier, SIGNAL(newFile(QString,QString,qint64)), this, SLOT(setupProgress(QString,QString,qint64))); connect(mFileCopier, SIGNAL(bytesRead(qint64)), this, SLOT(setCopyProgress(qint64))); connect(mFileCopier, SIGNAL(bytesReadIntval(qint64,qint64)), this, SLOT(setCopySummary(qint64,qint64))); @@ -59,6 +73,19 @@ FileWidget::FileWidget(QWidget *parent) : QWidget(parent), mCopyToMenu(0) { FileWidget::~FileWidget(){ writeHeaderData(); writeSettings(); + QString cacheDir = QStandardPaths::locate(QStandardPaths::HomeLocation, ".shemovcleaner", QStandardPaths::LocateDirectory); + if(!cacheDir.isEmpty()){ + QString cacheFile = QString("%1/%2").arg(cacheDir).arg("cfd.dat"); + QFile f(cacheFile); + if(f.open(QIODevice::WriteOnly)){ + QDataStream out(&f); + QList<QString> entries = mFileCache.keys(); + for(const QString &e : entries){ + CachedFileData *entry = mFileCache[e]; + out << *entry; + } + } + } } void FileWidget::setupGui(){ diff --git a/filewidget.h b/filewidget.h index 9c466fa..468821f 100644 --- a/filewidget.h +++ b/filewidget.h @@ -41,7 +41,7 @@ class FileWidget : public QWidget { QToolBar *toolBar() { return mToolBar; } const QString currentDir() const { return mDir->text(); } void setProgressBar(QProgressBar *progressBar) { mProgressBar = progressBar; } - ~FileWidget(); + virtual ~FileWidget(); signals: void statusMessage(const QString &msg); |