]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
fc353c4935a10318b9141948aa6a1b2207f3d706
[dolphin.git] / src / panels / information / kloadmetadatathread.cpp
1 /*****************************************************************************
2 * Copyright (C) 2009 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 "nfotranslator.h"
23
24 #include <kconfig.h>
25 #include <kconfiggroup.h>
26 #include <kfilemetainfo.h>
27 #include <kfilemetainfoitem.h>
28 #include <kglobal.h>
29 #include <klocale.h>
30 #include <kprotocolinfo.h>
31
32 #include <nepomuk/resource.h>
33
34 KLoadMetaDataThread::KLoadMetaDataThread() :
35 m_rating(0),
36 m_comment(),
37 m_tags(),
38 m_items(),
39 m_files(),
40 m_urls(),
41 m_canceled(false)
42 {
43 }
44
45 KLoadMetaDataThread::~KLoadMetaDataThread()
46 {
47 }
48
49 void KLoadMetaDataThread::load(const KUrl::List& urls)
50 {
51 m_urls = urls;
52 m_canceled = false;
53 start();
54 }
55
56 void KLoadMetaDataThread::cancel()
57 {
58 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
59 // as soon as run() gets the chance to check m_cancel.
60 m_canceled = true;
61 }
62
63 void KLoadMetaDataThread::cancelAndDelete()
64 {
65 if (isFinished()) {
66 Q_ASSERT(!isRunning());
67 deleteLater();
68 } else {
69 connect(this, SIGNAL(finished()), this, SLOT(slotFinished()));
70 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
71 // as soon as run() gets the chance to check m_cancel.
72 m_canceled = true;
73 // Afterwards the thread will delete itself
74 // asynchronously inside slotFinished().
75 }
76 }
77
78 void KLoadMetaDataThread::run()
79 {
80 KConfig config("kmetainformationrc", KConfig::NoGlobals);
81 KConfigGroup settings = config.group("Show");
82
83 bool first = true;
84 foreach (const KUrl& url, m_urls) {
85 if (m_canceled) {
86 return;
87 }
88
89 Nepomuk::Resource file(url);
90 if (!file.isValid()) {
91 continue;
92 }
93
94 m_files.insert(url, file);
95
96 if (!first && (m_rating != file.rating())) {
97 m_rating = 0; // reset rating
98 } else if (first) {
99 m_rating = file.rating();
100 }
101
102 if (!first && (m_comment != file.description())) {
103 m_comment.clear(); // reset comment
104 } else if (first) {
105 m_comment = file.description();
106 }
107
108 if (!first && (m_tags != file.tags())) {
109 m_tags.clear(); // reset tags
110 } else if (first) {
111 m_tags = file.tags();
112 }
113
114 const NfoTranslator& nfo = NfoTranslator::instance();
115 if (first && (m_urls.count() == 1)) {
116 // get cached meta data by checking the indexed files
117 QHash<QUrl, Nepomuk::Variant> variants = file.properties();
118 QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin();
119 while (it != variants.constEnd()) {
120 Nepomuk::Types::Property prop(it.key());
121 const QString uriString = prop.uri().toString();
122 if (settings.readEntry(uriString, true)) {
123 Item item;
124 item.name = uriString;
125 item.label = nfo.translation(prop.uri());
126 item.value = formatValue(it.value());
127 m_items.append(item);
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 Item item;
141 item.name = uriString;
142 item.label = nfo.translation(metaInfoItem.name());
143 item.value = metaInfoItem.value().toString();
144 m_items.append(item);
145 }
146 }
147 }
148 }
149
150 first = false;
151 }
152 }
153
154 int KLoadMetaDataThread::rating() const
155 {
156 return m_rating;
157 }
158
159 QString KLoadMetaDataThread::comment() const
160 {
161 return m_comment;
162 }
163
164 QList<Nepomuk::Tag> KLoadMetaDataThread::tags() const
165 {
166 return m_tags;
167 }
168
169 QList<KLoadMetaDataThread::Item> KLoadMetaDataThread::items() const
170 {
171 return m_items;
172 }
173
174 QMap<KUrl, Nepomuk::Resource> KLoadMetaDataThread::files() const
175 {
176 return m_files;
177 }
178
179 void KLoadMetaDataThread::slotFinished()
180 {
181 deleteLater();
182 }
183
184 QString KLoadMetaDataThread::formatValue(const Nepomuk::Variant& value)
185 {
186 if (value.isDateTime()) {
187 return KGlobal::locale()->formatDateTime(value.toDateTime(), KLocale::FancyLongDate);
188 }
189
190 if (value.isResource() || value.isResourceList()) {
191 QStringList links;
192 foreach(const Nepomuk::Resource& res, value.toResourceList()) {
193 if (KProtocolInfo::isKnownProtocol(res.resourceUri())) {
194 links << QString::fromLatin1("<a href=\"%1\">%2</a>")
195 .arg(KUrl(res.resourceUri()).url())
196 .arg(res.genericLabel());
197 } else {
198 links << res.genericLabel();
199 }
200 }
201 return links.join(QLatin1String(";\n"));
202 }
203
204 return value.toString();
205 }
206
207 #include "kloadmetadatathread_p.moc"