]>
cloud.milkyroute.net Git - dolphin.git/blob - 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> *
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. *
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. *
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 *****************************************************************************/
20 #include "kloadmetadatathread_p.h"
23 #include <kconfiggroup.h>
24 #include <kfilemetainfo.h>
25 #include <kfilemetainfoitem.h>
28 #include "knfotranslator_p.h"
29 #include <kprotocolinfo.h>
31 #include <nepomuk/resource.h>
33 KLoadMetaDataThread::KLoadMetaDataThread() :
44 KLoadMetaDataThread::~KLoadMetaDataThread()
48 void KLoadMetaDataThread::load(const KUrl::List
& urls
)
55 void KLoadMetaDataThread::cancel()
57 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
58 // as soon as run() gets the chance to check m_cancel.
62 void KLoadMetaDataThread::cancelAndDelete()
65 Q_ASSERT(!isRunning());
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.
72 // Afterwards the thread will delete itself
73 // asynchronously inside slotFinished().
77 void KLoadMetaDataThread::run()
79 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
80 KConfigGroup settings
= config
.group("Show");
83 foreach (const KUrl
& url
, m_urls
) {
88 Nepomuk::Resource
file(url
);
89 if (!file
.isValid()) {
93 m_files
.insert(url
, file
);
95 if (!first
&& (m_rating
!= file
.rating())) {
96 m_rating
= 0; // reset rating
98 m_rating
= file
.rating();
101 if (!first
&& (m_comment
!= file
.description())) {
102 m_comment
.clear(); // reset comment
104 m_comment
= file
.description();
107 if (!first
&& (m_tags
!= file
.tags())) {
108 m_tags
.clear(); // reset tags
110 m_tags
= file
.tags();
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)) {
123 item
.name
= uriString
;
124 item
.label
= nfo
.translation(prop
.uri());
125 item
.value
= formatValue(it
.value());
126 m_items
.append(item
);
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)) {
140 item
.name
= uriString
;
141 item
.label
= nfo
.translation(metaInfoItem
.name());
142 item
.value
= metaInfoItem
.value().toString();
143 m_items
.append(item
);
153 int KLoadMetaDataThread::rating() const
158 QString
KLoadMetaDataThread::comment() const
163 QList
<Nepomuk::Tag
> KLoadMetaDataThread::tags() const
168 QList
<KLoadMetaDataThread::Item
> KLoadMetaDataThread::items() const
173 QMap
<KUrl
, Nepomuk::Resource
> KLoadMetaDataThread::files() const
178 void KLoadMetaDataThread::slotFinished()
183 QString
KLoadMetaDataThread::formatValue(const Nepomuk::Variant
& value
)
185 if (value
.isDateTime()) {
186 return KGlobal::locale()->formatDateTime(value
.toDateTime(), KLocale::FancyLongDate
);
189 if (value
.isResource() || value
.isResourceList()) {
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());
197 links
<< res
.genericLabel();
200 return links
.join(QLatin1String(";\n"));
203 return value
.toString();
206 #include "kloadmetadatathread_p.moc"