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