]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kloadmetadatathread.cpp
ba4ebeda55ee4f121bc6341282c2aca4a6be9bdb
1 /*****************************************************************************
2 * Copyright (C) 2009-2010 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 "kmetadatamodel.h"
29 #include <kprotocolinfo.h>
31 #include <nepomuk/resource.h>
32 #include <nepomuk/resourcemanager.h>
34 KLoadMetaDataThread::KLoadMetaDataThread(KMetaDataModel
* model
) :
43 KLoadMetaDataThread::~KLoadMetaDataThread()
47 void KLoadMetaDataThread::load(const KUrl::List
& urls
)
54 QMap
<KUrl
, Nepomuk::Variant
> KLoadMetaDataThread::data() const
59 void KLoadMetaDataThread::cancel()
61 // Setting m_canceled to true will cancel KLoadMetaDataThread::run()
62 // as soon as run() gets the chance to check m_cancel.
66 void KLoadMetaDataThread::cancelAndDelete()
69 Q_ASSERT(!isRunning());
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.
76 // Afterwards the thread will delete itself
77 // asynchronously inside slotFinished().
81 void KLoadMetaDataThread::run()
83 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
84 KConfigGroup settings
= config
.group("Show");
86 unsigned int rating
= 0;
88 QList
<Nepomuk::Tag
> tags
;
91 foreach (const KUrl
& url
, m_urls
) {
96 Nepomuk::Resource
file(url
);
97 if (!file
.isValid()) {
101 if (!first
&& (rating
!= file
.rating())) {
102 rating
= 0; // reset rating
104 rating
= file
.rating();
107 if (!first
&& (comment
!= file
.description())) {
108 comment
.clear(); // reset comment
110 comment
= file
.description();
113 if (!first
&& (tags
!= file
.tags())) {
114 tags
.clear(); // reset tags
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()));
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
));
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
);
155 QList
<Nepomuk::Variant
> tagVariants
;
156 foreach (const Nepomuk::Tag
& tag
, tags
) {
157 tagVariants
.append(Nepomuk::Variant(tag
));
159 m_data
.insert(KUrl("kfileitem#tags"), tagVariants
);
162 m_data
.unite(m_model
->loadData());
165 void KLoadMetaDataThread::slotFinished()
170 QString
KLoadMetaDataThread::formatValue(const Nepomuk::Variant
& value
)
172 if (value
.isDateTime()) {
173 return KGlobal::locale()->formatDateTime(value
.toDateTime(), KLocale::FancyLongDate
);
176 if (value
.isResource() || value
.isResourceList()) {
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());
184 links
<< res
.genericLabel();
187 return links
.join(QLatin1String(";\n"));
190 return value
.toString();
193 #include "kloadmetadatathread_p.moc"