]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
ba4ebeda55ee4f121bc6341282c2aca4a6be9bdb
[dolphin.git] / src / panels / information / kloadmetadatathread.cpp
1 /*****************************************************************************
2 * Copyright (C) 2009-2010 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2009 by Sebastian Trueg <trueg@kde.org> *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License version 2 as published by the Free Software Foundation. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Library General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Library General Public License *
15 * along with this library; see the file COPYING.LIB. If not, write to *
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301, USA. *
18 *****************************************************************************/
19
20 #include "kloadmetadatathread_p.h"
21
22 #include <kconfig.h>
23 #include <kconfiggroup.h>
24 #include <kfilemetainfo.h>
25 #include <kfilemetainfoitem.h>
26 #include <kglobal.h>
27 #include <klocale.h>
28 #include "kmetadatamodel.h"
29 #include <kprotocolinfo.h>
30
31 #include <nepomuk/resource.h>
32 #include <nepomuk/resourcemanager.h>
33
34 KLoadMetaDataThread::KLoadMetaDataThread(KMetaDataModel* model) :
35 m_model(model),
36 m_data(),
37 m_urls(),
38 m_canceled(false)
39 {
40 Q_ASSERT(model != 0);
41 }
42
43 KLoadMetaDataThread::~KLoadMetaDataThread()
44 {
45 }
46
47 void KLoadMetaDataThread::load(const KUrl::List& urls)
48 {
49 m_urls = urls;
50 m_canceled = false;
51 start();
52 }
53
54 QMap<KUrl, Nepomuk::Variant> KLoadMetaDataThread::data() const
55 {
56 return m_data;
57 }
58
59 void KLoadMetaDataThread::cancel()
60 {
61 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
62 // as soon as run() gets the chance to check m_cancel.
63 m_canceled = true;
64 }
65
66 void KLoadMetaDataThread::cancelAndDelete()
67 {
68 if (isFinished()) {
69 Q_ASSERT(!isRunning());
70 deleteLater();
71 } else {
72 connect(this, SIGNAL(finished()), this, SLOT(slotFinished()));
73 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
74 // as soon as run() gets the chance to check m_cancel.
75 m_canceled = true;
76 // Afterwards the thread will delete itself
77 // asynchronously inside slotFinished().
78 }
79 }
80
81 void KLoadMetaDataThread::run()
82 {
83 KConfig config("kmetainformationrc", KConfig::NoGlobals);
84 KConfigGroup settings = config.group("Show");
85
86 unsigned int rating = 0;
87 QString comment;
88 QList<Nepomuk::Tag> tags;
89
90 bool first = true;
91 foreach (const KUrl& url, m_urls) {
92 if (m_canceled) {
93 return;
94 }
95
96 Nepomuk::Resource file(url);
97 if (!file.isValid()) {
98 continue;
99 }
100
101 if (!first && (rating != file.rating())) {
102 rating = 0; // reset rating
103 } else if (first) {
104 rating = file.rating();
105 }
106
107 if (!first && (comment != file.description())) {
108 comment.clear(); // reset comment
109 } else if (first) {
110 comment = file.description();
111 }
112
113 if (!first && (tags != file.tags())) {
114 tags.clear(); // reset tags
115 } else if (first) {
116 tags = file.tags();
117 }
118
119 if (first && (m_urls.count() == 1)) {
120 // get cached meta data by checking the indexed files
121 QHash<QUrl, Nepomuk::Variant> variants = file.properties();
122 QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin();
123 while (it != variants.constEnd()) {
124 Nepomuk::Types::Property prop(it.key());
125 const QString uriString = prop.uri().toString();
126 if (settings.readEntry(uriString, true)) {
127 m_data.insert(uriString, formatValue(it.value()));
128 }
129 ++it;
130 }
131
132 if (variants.isEmpty()) {
133 // the file has not been indexed, query the meta data
134 // directly from the file
135 KFileMetaInfo metaInfo(m_urls.first());
136 const QHash<QString, KFileMetaInfoItem> metaInfoItems = metaInfo.items();
137 foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) {
138 const QString uriString = metaInfoItem.name();
139 if (settings.readEntry(uriString, true)) {
140 const Nepomuk::Variant value(metaInfoItem.value());
141 m_data.insert(uriString, formatValue(value));
142 }
143 }
144 }
145 }
146
147 first = false;
148 }
149
150 const bool isNepomukActivated = (Nepomuk::ResourceManager::instance()->init() == 0);
151 if (isNepomukActivated) {
152 m_data.insert(KUrl("kfileitem#rating"), rating);
153 m_data.insert(KUrl("kfileitem#comment"), comment);
154
155 QList<Nepomuk::Variant> tagVariants;
156 foreach (const Nepomuk::Tag& tag, tags) {
157 tagVariants.append(Nepomuk::Variant(tag));
158 }
159 m_data.insert(KUrl("kfileitem#tags"), tagVariants);
160 }
161
162 m_data.unite(m_model->loadData());
163 }
164
165 void KLoadMetaDataThread::slotFinished()
166 {
167 deleteLater();
168 }
169
170 QString KLoadMetaDataThread::formatValue(const Nepomuk::Variant& value)
171 {
172 if (value.isDateTime()) {
173 return KGlobal::locale()->formatDateTime(value.toDateTime(), KLocale::FancyLongDate);
174 }
175
176 if (value.isResource() || value.isResourceList()) {
177 QStringList links;
178 foreach(const Nepomuk::Resource& res, value.toResourceList()) {
179 if (KProtocolInfo::isKnownProtocol(res.resourceUri())) {
180 links << QString::fromLatin1("<a href=\"%1\">%2</a>")
181 .arg(KUrl(res.resourceUri()).url())
182 .arg(res.genericLabel());
183 } else {
184 links << res.genericLabel();
185 }
186 }
187 return links.join(QLatin1String(";\n"));
188 }
189
190 return value.toString();
191 }
192
193 #include "kloadmetadatathread_p.moc"