KIO
kfilemetadatareader.cpp
Go to the documentation of this file.
00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> * 00003 * * 00004 * This library is free software; you can redistribute it and/or * 00005 * modify it under the terms of the GNU Library General Public * 00006 * License as published by the Free Software Foundation; either * 00007 * version 2 of the License, or (at your option) any later version. * 00008 * * 00009 * This library is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00012 * Library General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU Library General Public License * 00015 * along with this library; see the file COPYING.LIB. If not, write to * 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * 00017 * Boston, MA 02110-1301, USA. * 00018 *****************************************************************************/ 00019 00020 #include "kfilemetadatareader_p.h" 00021 00022 #include <kprocess.h> 00023 #include <kstandarddirs.h> 00024 00025 class KFileMetaDataReader::Private 00026 { 00027 public: 00028 Private(KFileMetaDataReader* parent); 00029 ~Private(); 00030 00031 void slotLoadingFinished(int exitCode, QProcess::ExitStatus exitStatus); 00032 00033 bool m_readContextData; 00034 KProcess* m_process; 00035 QHash<KUrl, Nepomuk::Variant> m_metaData; 00036 00037 private: 00038 KFileMetaDataReader* const q; 00039 }; 00040 00041 KFileMetaDataReader::Private::Private(KFileMetaDataReader* parent) : 00042 m_readContextData(true), 00043 m_process(new KProcess()), 00044 m_metaData(), 00045 q(parent) 00046 { 00047 } 00048 00049 KFileMetaDataReader::Private::~Private() 00050 { 00051 delete m_process; 00052 } 00053 00054 void KFileMetaDataReader::Private::slotLoadingFinished(int exitCode, QProcess::ExitStatus exitStatus) 00055 { 00056 Q_UNUSED(exitCode); 00057 Q_UNUSED(exitStatus); 00058 00059 QDataStream in(QByteArray::fromBase64(m_process->readLine())); 00060 00061 KUrl key; 00062 Nepomuk::Variant value; 00063 while (!in.atEnd()) { 00064 in >> key; 00065 00066 // Unlike QVariant no streaming operators are implemented for Nepomuk::Variant. 00067 // So it is required to manually decode the variant from the stream. See 00068 // function sendMetaData() in kfilemetadatareaderprocess.cpp for the encoding 00069 // counterpart. 00070 int streamType; 00071 in >> streamType; 00072 00073 switch (streamType) { 00074 case 0: { 00075 QStringList stringList; 00076 in >> stringList; 00077 value = stringList; 00078 break; 00079 } 00080 case 1: { 00081 QString resource; 00082 in >> resource; 00083 value = resource; 00084 break; 00085 } 00086 00087 default: 00088 QVariant variant; 00089 in >> variant; 00090 value = Nepomuk::Variant(variant); 00091 } 00092 00093 m_metaData.insert(key, value); 00094 } 00095 00096 emit q->finished(); 00097 } 00098 00099 KFileMetaDataReader::KFileMetaDataReader(const QList<KUrl>& urls, QObject* parent) : 00100 QObject(parent), 00101 d(new Private(this)) 00102 { 00103 const QString fileMetaDataReaderExe = KStandardDirs::findExe(QLatin1String("kfilemetadatareader")); 00104 (*d->m_process) << fileMetaDataReaderExe; 00105 00106 foreach (const KUrl& url, urls) { 00107 (*d->m_process) << url.url(); 00108 } 00109 00110 d->m_process->setOutputChannelMode(KProcess::OnlyStdoutChannel); 00111 d->m_process->setNextOpenMode(QIODevice::ReadOnly); 00112 connect(d->m_process, SIGNAL(finished(int, QProcess::ExitStatus)), 00113 this, SLOT(slotLoadingFinished(int, QProcess::ExitStatus))); 00114 } 00115 00116 KFileMetaDataReader::~KFileMetaDataReader() 00117 { 00118 delete d; 00119 } 00120 00121 void KFileMetaDataReader::setReadContextData(bool read) 00122 { 00123 d->m_readContextData = read; 00124 } 00125 00126 bool KFileMetaDataReader::readContextData() const 00127 { 00128 return d->m_readContextData; 00129 } 00130 00131 void KFileMetaDataReader::start() 00132 { 00133 if (d->m_process->state() == QProcess::NotRunning) { 00134 if (!d->m_readContextData) { 00135 (*d->m_process) << "--file"; 00136 } 00137 d->m_process->start(); 00138 } 00139 } 00140 00141 QHash<KUrl, Nepomuk::Variant> KFileMetaDataReader::metaData() const 00142 { 00143 return d->m_metaData; 00144 } 00145 00146 #include "kfilemetadatareader_p.moc"
KDE 4.7 API Reference