summaryrefslogtreecommitdiffstats
path: root/smglobals.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'smglobals.cpp')
-rw-r--r--smglobals.cpp126
1 files changed, 3 insertions, 123 deletions
diff --git a/smglobals.cpp b/smglobals.cpp
index 6ef4ceb..9240bf2 100644
--- a/smglobals.cpp
+++ b/smglobals.cpp
@@ -29,6 +29,7 @@
#include "pictureviewer2.h"
#include "picfilesmodel.h"
#include "configurationdialog.h"
+#include "framecache.h"
SmGlobals *SmGlobals::mInstance = 0;
@@ -115,9 +116,9 @@ PictureViewer2 *SmGlobals::pictureViewer() {
return mPictureViewer;
}
-SmGlobals::FrameCache *SmGlobals::frameCache() {
+FrameCache *SmGlobals::frameCache() {
if(!mFrameCache){
- mFrameCache = new SmGlobals::FrameCache;
+ mFrameCache = new FrameCache;
}
return mFrameCache;
}
@@ -170,124 +171,3 @@ SmGlobals::SmGlobals() : mPictureViewer(0), mFrameCache(0){
mIcons.insert("Clean tampon", ":/clean_tampon.png");
mDvdSize = Q_INT64_C(4707319808) - 20 * 1024 *1024;
}
-
-//FrameCache
-SmGlobals::FrameCache::FrameCache(QObject *parent) : QObject(parent), mMagic(0xDEADBEEF), mCacheSubDir(".frameCache"), mCacheFileName("cache") {
- readConfig();
- readCache();
-}
-
-SmGlobals::FrameCache::~FrameCache(){
- QFile outfile(mCacheFile);
- outfile.open(QIODevice::WriteOnly | QIODevice::Truncate);
- QDataStream ds(&outfile);
- ds << (qint32)mMagic;
- for(QHash<QPair<QString, QString>, QString>::const_iterator it = mFrameCache.constBegin(); it != mFrameCache.constEnd(); ++it){
- ds << it.key().first << it.key().second << it.value();
- }
- outfile.close();
-}
-
-void SmGlobals::FrameCache::readConfig(){
- QSettings s;
- QString archive = s.value("paths/archivedir").toString();
- if(archive.isEmpty()){
- return;
- }
- QDir archiveDir(archive);
- if(!archiveDir.exists(mCacheSubDir)){
- archiveDir.mkdir(mCacheSubDir);
- }
- mCacheDir = QString("%1/%2").arg(archive).arg(mCacheSubDir);
- mCacheFile = QString("%1/%2").arg(mCacheDir).arg(mCacheFileName);
- mWhen = s.value("ui/grabframe", "00:00:00").toString();
- mFfMpegPath = s.value("paths/ffmpeg").toString();
-}
-
-const QPixmap SmGlobals::FrameCache::entry(const QString &sourcePath, const QString &when){
- const QPair<QString, QString> source = prepFrame(sourcePath, when);
- return QPixmap(mFrameCache.value(source));
-}
-
-const QString SmGlobals::FrameCache::entryPath(const QString &sourcePath, const QString &when){
- const QPair<QString, QString> source = prepFrame(sourcePath, when);
- return mFrameCache.value(source);
-}
-
-const QPair<QString, QString> SmGlobals::FrameCache::prepFrame(const QString &sourceFile, QString when){
- if(when.isEmpty()){
- when = mWhen;
- }
- const QString fileName = QFileInfo(sourceFile).fileName();
- const QPair<QString, QString> source(fileName, when);
- if(!mFrameCache.contains(source)){
- grabFrame(sourceFile, when);
- }
- return source;
-}
-
-void SmGlobals::FrameCache::grabFrame(const QString &sourceFile, QString when){
- QFileInfo sourceInfo(sourceFile);
- if(!sourceInfo.exists()){
- return;
- }
- QString tmpTemplate = QString("%1/%2_%3-XXXXXX.png").arg(mCacheDir).arg(sourceInfo.fileName()).arg(when);
- QTemporaryFile tempFile(tmpTemplate);
- tempFile.setAutoRemove(false);
- if(tempFile.open()){
- QStringList ffMpegArgs = QStringList() << "-vframes" << "1" << "-ss" << when << "-i" << sourceFile << "-y" << tempFile.fileName();
- QProcess ffmpeg;
- ffmpeg.start(mFfMpegPath, ffMpegArgs);
- if(!ffmpeg.waitForStarted()){
- return;
- }
- ffmpeg.waitForFinished();
- QPair<QString, QString> pair(sourceInfo.fileName(), when);
- mFrameCache.insert(pair, tempFile.fileName());
- }
-}
-
-void SmGlobals::FrameCache::readCache(){
- QFile cache(mCacheFile);
- cache.open(QIODevice::ReadOnly);
- QDataStream ds(&cache);
- qint32 magic;
- ds >> magic;
- if(magic != mMagic){
- return;
- }
- while(!ds.atEnd()){
- QString source, pos, cacheFile;
- ds >> source >> pos >> cacheFile;
- QFileInfo fi(cacheFile);
- if(fi.size() == 0){
- QFile::remove(fi.absoluteFilePath());
- continue;
- }
- QPair<QString, QString> pair(source, pos);
- mFrameCache.insert(pair, cacheFile);
- }
- cleanup();
-}
-
-/* cleanup function for fucked up framecache:
- Key was full path until recently, not just the filename, so
- duplicates showed up when hovering over the same in different
- paths, eg, archive or filesystem. Should be removed in the future
-*/
-void SmGlobals::FrameCache::cleanup(){
- QHash<QPair<QString, QString>, QString> newFrameCache;
- QHash<QPair<QString, QString>, QString>::const_iterator it = mFrameCache.constBegin();
- while(it != mFrameCache.constEnd()){
- QPair<QString, QString> key = it.key();
- QFileInfo fi(key.first);
- QPair<QString, QString> newEntry(fi.fileName(), key.second);
- if(!newFrameCache.contains(newEntry)){
- newFrameCache.insert(newEntry, it.value());
- }else{
- QFile::remove(it.value());
- }
- ++it;
- }
- mFrameCache = newFrameCache;
-}