]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
fc353c4935a10318b9141948aa6a1b2207f3d706
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"
22 #include "nfotranslator.h"
25 #include <kconfiggroup.h>
26 #include <kfilemetainfo.h>
27 #include <kfilemetainfoitem.h>
30 #include <kprotocolinfo.h>
32 #include <nepomuk/resource.h>
34 KLoadMetaDataThread::KLoadMetaDataThread() :
45 KLoadMetaDataThread::~KLoadMetaDataThread()
49 void KLoadMetaDataThread::load(const KUrl::List
& urls
)
56 void KLoadMetaDataThread::cancel()
58 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
59 // as soon as run() gets the chance to check m_cancel.
63 void KLoadMetaDataThread::cancelAndDelete()
66 Q_ASSERT(!isRunning());
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.
73 // Afterwards the thread will delete itself
74 // asynchronously inside slotFinished().
78 void KLoadMetaDataThread::run()
80 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
81 KConfigGroup settings
= config
.group("Show");
84 foreach (const KUrl
& url
, m_urls
) {
89 Nepomuk::Resource
file(url
);
90 if (!file
.isValid()) {
94 m_files
.insert(url
, file
);
96 if (!first
&& (m_rating
!= file
.rating())) {
97 m_rating
= 0; // reset rating
99 m_rating
= file
.rating();
102 if (!first
&& (m_comment
!= file
.description())) {
103 m_comment
.clear(); // reset comment
105 m_comment
= file
.description();
108 if (!first
&& (m_tags
!= file
.tags())) {
109 m_tags
.clear(); // reset tags
111 m_tags
= file
.tags();
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)) {
124 item
.name
= uriString
;
125 item
.label
= nfo
.translation(prop
.uri());
126 item
.value
= formatValue(it
.value());
127 m_items
.append(item
);
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)) {
141 item
.name
= uriString
;
142 item
.label
= nfo
.translation(metaInfoItem
.name());
143 item
.value
= metaInfoItem
.value().toString();
144 m_items
.append(item
);
154 int KLoadMetaDataThread::rating() const
159 QString
KLoadMetaDataThread::comment() const
164 QList
<Nepomuk::Tag
> KLoadMetaDataThread::tags() const
169 QList
<KLoadMetaDataThread::Item
> KLoadMetaDataThread::items() const
174 QMap
<KUrl
, Nepomuk::Resource
> KLoadMetaDataThread::files() const
179 void KLoadMetaDataThread::slotFinished()
184 QString
KLoadMetaDataThread::formatValue(const Nepomuk::Variant
& value
)
186 if (value
.isDateTime()) {
187 return KGlobal::locale()->formatDateTime(value
.toDateTime(), KLocale::FancyLongDate
);
190 if (value
.isResource() || value
.isResourceList()) {
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());
198 links
<< res
.genericLabel();
201 return links
.join(QLatin1String(";\n"));
204 return value
.toString();
207 #include "kloadmetadatathread_p.moc"