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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*****************************************************************************
* Copyright (C) 2009 by Peter Penz <[email protected]> *
* Copyright (C) 2009 by Sebastian Trueg <[email protected]> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License version 2 as published by the Free Software Foundation. *
* *
* This library 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 *
* Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public License *
* along with this library; see the file COPYING.LIB. If not, write to *
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301, USA. *
*****************************************************************************/
#include "kloadmetadatathread_p.h"
#include "nfotranslator.h"
#include <kconfig.h>
#include <kconfiggroup.h>
#include <kfilemetainfo.h>
#include <kfilemetainfoitem.h>
#include <kglobal.h>
#include <klocale.h>
#include <kprotocolinfo.h>
#include <nepomuk/resource.h>
KLoadMetaDataThread::KLoadMetaDataThread() :
m_rating(0),
m_comment(),
m_tags(),
m_items(),
m_files(),
m_urls(),
m_canceled(false)
{
}
KLoadMetaDataThread::~KLoadMetaDataThread()
{
}
void KLoadMetaDataThread::load(const KUrl::List& urls)
{
m_urls = urls;
m_canceled = false;
start();
}
void KLoadMetaDataThread::cancelAndDelete()
{
connect(this, SIGNAL(finished()), this, SLOT(slotFinished()));
m_canceled = true;
// Setting m_canceled to true will cancel KLoadMetaDataThread::run()
// as soon as possible. Afterwards the thread will delete itself
// asynchronously inside slotFinished().
}
void KLoadMetaDataThread::run()
{
KConfig config("kmetainformationrc", KConfig::NoGlobals);
KConfigGroup settings = config.group("Show");
bool first = true;
foreach (const KUrl& url, m_urls) {
if (m_canceled) {
return;
}
Nepomuk::Resource file(url);
if (!file.isValid()) {
continue;
}
m_files.insert(url, file);
if (!first && (m_rating != file.rating())) {
m_rating = 0; // reset rating
} else if (first) {
m_rating = file.rating();
}
if (!first && (m_comment != file.description())) {
m_comment.clear(); // reset comment
} else if (first) {
m_comment = file.description();
}
if (!first && (m_tags != file.tags())) {
m_tags.clear(); // reset tags
} else if (first) {
m_tags = file.tags();
}
NfoTranslator& nfo = NfoTranslator::instance();
if (first && (m_urls.count() == 1)) {
// get cached meta data by checking the indexed files
QHash<QUrl, Nepomuk::Variant> variants = file.properties();
QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin();
while (it != variants.constEnd()) {
Nepomuk::Types::Property prop(it.key());
if (settings.readEntry(prop.name(), true)) {
Item item;
item.name = prop.name();
item.label = nfo.translation(prop.uri());
item.value = formatValue(it.value());
m_items.append(item);
}
++it;
}
if (variants.isEmpty()) {
// the file has not been indexed, query the meta data
// directly from the file
KFileMetaInfo metaInfo(m_urls.first());
const QHash<QString, KFileMetaInfoItem> metaInfoItems = metaInfo.items();
foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) {
Item item;
item.name = metaInfoItem.name();
item.label = nfo.translation(metaInfoItem.name());
item.value = metaInfoItem.value().toString();
m_items.append(item);
}
}
}
first = false;
}
}
int KLoadMetaDataThread::rating() const
{
return m_rating;
}
QString KLoadMetaDataThread::comment() const
{
return m_comment;
}
QList<Nepomuk::Tag> KLoadMetaDataThread::tags() const
{
return m_tags;
}
QList<KLoadMetaDataThread::Item> KLoadMetaDataThread::items() const
{
return m_items;
}
QMap<KUrl, Nepomuk::Resource> KLoadMetaDataThread::files() const
{
return m_files;
}
void KLoadMetaDataThread::slotFinished()
{
deleteLater();
}
QString KLoadMetaDataThread::formatValue(const Nepomuk::Variant& value)
{
if (value.isDateTime()) {
return KGlobal::locale()->formatDateTime(value.toDateTime(), KLocale::FancyLongDate);
}
if (value.isResource() || value.isResourceList()) {
QStringList links;
foreach(const Nepomuk::Resource& res, value.toResourceList()) {
if (KProtocolInfo::isKnownProtocol(res.resourceUri())) {
links << QString::fromLatin1("<a href=\"%1\">%2</a>")
.arg(KUrl(res.resourceUri()).url())
.arg(res.genericLabel());
} else {
links << res.genericLabel();
}
}
return links.join(QLatin1String(";\n"));
}
return value.toString();
}
#include "kloadmetadatathread_p.moc"
|