KIO
kloadfilemetadatathread.cpp
Go to the documentation of this file.
00001 /***************************************************************************** 00002 * Copyright (C) 2009-2010 by Peter Penz <peter.penz@gmx.at> * 00003 * Copyright (C) 2009-2010 by Sebastian Trueg <trueg@kde.org> * 00004 * * 00005 * This library is free software; you can redistribute it and/or * 00006 * modify it under the terms of the GNU Library General Public * 00007 * License as published by the Free Software Foundation; either * 00008 * version 2 of the License, or (at your option) any later version. * 00009 * * 00010 * This library is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00013 * Library General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU Library General Public License * 00016 * along with this library; see the file COPYING.LIB. If not, write to * 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * 00018 * Boston, MA 02110-1301, USA. * 00019 *****************************************************************************/ 00020 00021 #include "kloadfilemetadatathread_p.h" 00022 00023 #include <kfilemetainfo.h> 00024 #include <kfilemetainfoitem.h> 00025 #include <kglobal.h> 00026 #include <klocale.h> 00027 #include "kfilemetadataprovider_p.h" 00028 #include <kprotocolinfo.h> 00029 00030 #include "resource.h" 00031 #include "thing.h" 00032 #include "variant.h" 00033 #include "resourcemanager.h" 00034 00035 #include "config-nepomuk.h" 00036 00037 #include "query/filequery.h" 00038 #include "query/comparisonterm.h" 00039 #include "query/andterm.h" 00040 #include "query/resourceterm.h" 00041 #include "query/resourcetypeterm.h" 00042 #include "query/optionalterm.h" 00043 #include "utils/utils.h" 00044 00045 #include <Soprano/Model> 00046 #include <Soprano/QueryResultIterator> 00047 #include <Soprano/NodeIterator> 00048 00049 #include <QMutexLocker> 00050 00051 KLoadFileMetaDataThread::KLoadFileMetaDataThread() 00052 : m_canceled(false) 00053 { 00054 connect(this, SIGNAL(finished()), this, SLOT(slotLoadingFinished())); 00055 } 00056 00057 KLoadFileMetaDataThread::~KLoadFileMetaDataThread() 00058 { 00059 } 00060 00061 void KLoadFileMetaDataThread::load(const KUrl::List& urls) 00062 { 00063 QMutexLocker locker(&m_mutex); 00064 m_urls = urls; 00065 m_canceled = false; 00066 start(); 00067 } 00068 00069 QHash<KUrl, Nepomuk::Variant> KLoadFileMetaDataThread::data() const 00070 { 00071 QMutexLocker locker(&m_mutex); 00072 return m_data; 00073 } 00074 00075 void KLoadFileMetaDataThread::cancel() 00076 { 00077 // Setting m_canceled to true will cancel KLoadFileMetaDataThread::run() 00078 // as soon as run() gets the chance to check m_cancel. 00079 m_canceled = true; 00080 } 00081 00082 void KLoadFileMetaDataThread::run() 00083 { 00084 QMutexLocker locker(&m_mutex); 00085 const KUrl::List urls = m_urls; 00086 locker.unlock(); // no shared member is accessed until locker.relock() 00087 00088 unsigned int rating = 0; 00089 QString comment; 00090 QList<Nepomuk::Tag> tags; 00091 QHash<KUrl, Nepomuk::Variant> data; 00092 00093 bool first = true; 00094 foreach (const KUrl& url, urls) { 00095 if (m_canceled) { 00096 return; 00097 } 00098 00099 Nepomuk::Resource file(url); 00100 if (!file.isValid()) { 00101 continue; 00102 } 00103 00104 if (!first && (rating != file.rating())) { 00105 rating = 0; // reset rating 00106 } else if (first) { 00107 rating = file.rating(); 00108 } 00109 00110 if (!first && (comment != file.description())) { 00111 comment.clear(); // reset comment 00112 } else if (first) { 00113 comment = file.description(); 00114 } 00115 00116 if (!first && (tags != file.tags())) { 00117 tags.clear(); // reset tags 00118 } else if (first) { 00119 tags = file.tags(); 00120 } 00121 00122 if (first && (urls.count() == 1)) { 00123 // get cached meta data by checking the indexed files 00124 QHash<QUrl, Nepomuk::Variant> variants = file.properties(); 00125 QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin(); 00126 while (it != variants.constEnd()) { 00127 Nepomuk::Types::Property prop(it.key()); 00128 data.insert(prop.uri(), Nepomuk::Utils::formatPropertyValue(prop, it.value(), QList<Nepomuk::Resource>() << file, Nepomuk::Utils::WithKioLinks)); 00129 ++it; 00130 } 00131 00132 if (variants.isEmpty()) { 00133 // The file has not been indexed, query the meta data 00134 // directly from the file 00135 00136 // TODO: Some Strigi analyzers (used in KFileMetaInfo) are not reentrant. 00137 // As workaround the access is protected by a mutex here. To reproduce 00138 // the issue run kfilemetainfotest with helgrind. 00139 static QMutex metaInfoMutex; 00140 metaInfoMutex.lock(); 00141 00142 const QString path = urls.first().toLocalFile(); 00143 KFileMetaInfo metaInfo(path, QString(), KFileMetaInfo::Fastest); 00144 const QHash<QString, KFileMetaInfoItem> metaInfoItems = metaInfo.items(); 00145 foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) { 00146 const QString uriString = metaInfoItem.name(); 00147 const Nepomuk::Variant value(metaInfoItem.value()); 00148 data.insert(uriString, Nepomuk::Utils::formatPropertyValue(Nepomuk::Types::Property(), value)); 00149 } 00150 00151 metaInfoMutex.unlock(); 00152 } 00153 } 00154 00155 first = false; 00156 } 00157 00158 if (Nepomuk::ResourceManager::instance()->initialized()) { 00159 data.insert(KUrl("kfileitem#rating"), rating); 00160 data.insert(KUrl("kfileitem#comment"), comment); 00161 00162 QList<Nepomuk::Variant> tagVariants; 00163 foreach (const Nepomuk::Tag& tag, tags) { 00164 tagVariants.append(Nepomuk::Variant(tag)); 00165 } 00166 data.insert(KUrl("kfileitem#tags"), tagVariants); 00167 } 00168 00169 locker.relock(); 00170 m_data = data; 00171 } 00172 00173 void KLoadFileMetaDataThread::slotLoadingFinished() 00174 { 00175 emit finished(this); 00176 } 00177 00178 #include "kloadfilemetadatathread_p.moc"
KDE 4.6 API Reference