-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paththumbnailloader.cpp
157 lines (126 loc) · 4.16 KB
/
thumbnailloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright (C) 2014 Sialan Labs
http://labs.sialan.org
Limoo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Limoo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define THUMBNAILS_PATH QDir::homePath() + "/.thumbnails/limoo"
#define CACHE_COUNT 10
#include "thumbnailloader.h"
#include "thumbnailloaderitem.h"
#include "limoo_macros.h"
#include <QSet>
#include <QQueue>
#include <QHash>
#include <QTimerEvent>
#include <QDebug>
#include <QFileInfo>
#include <QCryptographicHash>
#include <QMimeDatabase>
#include <QDateTime>
#include <QDir>
typedef QPair<QString,QString> QueueItem;
class ThumbnailLoaderPrivate
{
public:
QSet<ThumbnailLoaderItem*> items;
QList<ThumbnailLoaderItem*> inactiveItems;
QQueue<QueueItem> queue;
QMimeDatabase db;
QHash<ThumbnailLoaderItem*,QString> paths;
};
ThumbnailLoader::ThumbnailLoader(QObject *parent) :
QObject(parent)
{
p = new ThumbnailLoaderPrivate;
QDir().mkpath(THUMBNAILS_PATH);
reset();
}
void ThumbnailLoader::load(const QString &path)
{
QFileInfo file( path );
QString suffix = p->db.mimeTypeForFile(path).preferredSuffix();
if( suffix == "svg" || suffix == "svgz" )
{
emit loaded( path, file.filePath() );
return;
}
else
if( file.suffix() == PATH_HANDLER_LLOCK_SUFFIX )
{
QString thumb_path = file.path() + "/" + file.completeBaseName() + "." + PATH_HANDLER_LLOCK_SUFFIX_THUMB;
emit loaded( path, thumb_path );
return;
}
QString hidden_text = file.path() + " " +
QString::number(file.size()) + " " +
file.created().toString("yyyy/MM/dd hh:mm:ss:zzz") + " " +
file.lastModified().toString("yyyy/MM/dd hh:mm:ss:zzz");
QString md5 = HASH_MD5(hidden_text);
QString thumbnail = THUMBNAILS_PATH + "/" + md5 + "." + suffix;
if( QFileInfo(thumbnail).exists() )
{
emit loaded( path, thumbnail );
return;
}
const QueueItem item = QueueItem(path,thumbnail);
if( p->queue.contains(item) ) {
p->queue.prepend( p->queue.takeAt(p->queue.indexOf(item)) );
return;
}
p->queue.append( item );
next();
}
void ThumbnailLoader::reset()
{
foreach( ThumbnailLoaderItem *item, p->items ) {
disconnect( item, SIGNAL(completed(ThumbnailLoaderItem*,QString)), this, SLOT(loaded_private(ThumbnailLoaderItem*,QString)) );
connect( item, SIGNAL(finished()), item, SLOT(deleteLater()), Qt::QueuedConnection );
if( item->isFinished() )
item->deleteLater();
}
p->items.clear();
p->queue.clear();
p->inactiveItems.clear();
p->paths.clear();
for( int i=0; i<CACHE_COUNT; i++ )
{
ThumbnailLoaderItem *item = new ThumbnailLoaderItem(this);
p->items.insert( item );
p->inactiveItems.append( item );
connect( item, SIGNAL(completed(ThumbnailLoaderItem*,QString)), this, SLOT(loaded_private(ThumbnailLoaderItem*,QString)) );
}
}
void ThumbnailLoader::next()
{
if( p->inactiveItems.isEmpty() )
return;
if( p->queue.isEmpty() )
return;
const QueueItem & path = p->queue.takeFirst();
ThumbnailLoaderItem *item = p->inactiveItems.takeFirst();
p->paths.insert(item,path.first);
item->start( path.first, path.second );
}
void ThumbnailLoader::loaded_private(ThumbnailLoaderItem *item, const QString &thumbnail)
{
if( !p->items.contains(item) )
return;
p->inactiveItems.append(item);
const QString path = p->paths.value(item);
p->paths.remove(item);
emit loaded( path, thumbnail );
next();
}
ThumbnailLoader::~ThumbnailLoader()
{
delete p;
}