KIO
kfilemetadatareaderprocess.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 <iostream> 00021 00022 #include <kaboutdata.h> 00023 #include <kcmdlineargs.h> 00024 #include <kfilemetainfo.h> 00025 #include <kcomponentdata.h> 00026 #include <kconfiggroup.h> 00027 #include <klocale.h> 00028 00029 #include <QtCore/QByteArray> 00030 #include <QtCore/QCoreApplication> 00031 #include <QtCore/QDataStream> 00032 #include <QtCore/QHash> 00033 #include <QtCore/QString> 00034 #include <QtCore/QTimer> 00035 00036 #define DISABLE_NEPOMUK_LEGACY 00037 #include "config-nepomuk.h" 00038 00039 #include <nepomuk/query/filequery.h> 00040 #include <nepomuk/query/comparisonterm.h> 00041 #include <nepomuk/query/andterm.h> 00042 #include <nepomuk/query/resourceterm.h> 00043 #include <nepomuk/query/resourcetypeterm.h> 00044 #include <nepomuk/query/optionalterm.h> 00045 #include <nepomuk/utils/utils.h> 00046 #include <nepomuk/types/property.h> 00047 #include <nepomuk/core/tag.h> 00048 #include <nepomuk/core/variant.h> 00049 #include <nepomuk/core/resourcemanager.h> 00050 00051 using namespace std; 00052 00053 class KFileMetaDataReaderApplication : public QCoreApplication 00054 { 00055 Q_OBJECT 00056 00057 public: 00058 KFileMetaDataReaderApplication(int& argc, char** argv); 00059 00060 private Q_SLOTS: 00061 void readAndSendMetaData(); 00062 00063 private: 00064 void sendMetaData(const QHash<KUrl, Nepomuk::Variant>& data); 00065 QHash<KUrl, Nepomuk::Variant> readFileMetaData(const QList<KUrl>& urls) const; 00066 QHash<KUrl, Nepomuk::Variant> readFileAndContextMetaData(const QList<KUrl>& urls) const; 00067 }; 00068 00069 00070 00071 KFileMetaDataReaderApplication::KFileMetaDataReaderApplication(int& argc, char** argv) : 00072 QCoreApplication(argc, argv) 00073 { 00074 QTimer::singleShot(0, this, SLOT(readAndSendMetaData())); 00075 } 00076 00077 void KFileMetaDataReaderApplication::readAndSendMetaData() 00078 { 00079 const KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); 00080 00081 KUrl::List urls; 00082 for (int i = 0; i < args->count(); ++i) { 00083 urls.append(KUrl(args->arg(i))); 00084 } 00085 00086 QHash<KUrl, Nepomuk::Variant> metaData; 00087 if (args->isSet("file")) { 00088 metaData = readFileMetaData(urls); 00089 } else { 00090 metaData = readFileAndContextMetaData(urls); 00091 } 00092 00093 sendMetaData(metaData); 00094 00095 quit(); 00096 } 00097 00098 void KFileMetaDataReaderApplication::sendMetaData(const QHash<KUrl, Nepomuk::Variant>& data) 00099 { 00100 QByteArray byteArray; 00101 QDataStream out(&byteArray, QIODevice::WriteOnly); 00102 00103 QHashIterator<KUrl, Nepomuk::Variant> it(data); 00104 while (it.hasNext()) { 00105 it.next(); 00106 00107 out << it.key(); 00108 00109 // Unlike QVariant no streaming operators are implemented for Nepomuk::Variant. 00110 // So it is required to manually encode the variant for the stream. 00111 // The decoding counterpart is located in KFileMetaDataReader. 00112 const Nepomuk::Variant& variant = it.value(); 00113 if (variant.isList()) { 00114 out << 0 << variant.toStringList(); 00115 } else if (variant.isResource()) { 00116 out << 1 << variant.toString(); 00117 } else { 00118 out << 2 << variant.variant(); 00119 } 00120 } 00121 00122 cout << byteArray.toBase64().constData(); 00123 } 00124 00125 QHash<KUrl, Nepomuk::Variant> KFileMetaDataReaderApplication::readFileMetaData(const QList<KUrl>& urls) const 00126 { 00127 QHash<KUrl, Nepomuk::Variant> data; 00128 00129 // Currently only the meta-data of one file is supported. 00130 // It might be an option to read all meta-data and show 00131 // ranges for each key. 00132 if (urls.count() == 1) { 00133 const QString path = urls.first().toLocalFile(); 00134 KFileMetaInfo metaInfo(path, QString(), KFileMetaInfo::Fastest); 00135 const QHash<QString, KFileMetaInfoItem> metaInfoItems = metaInfo.items(); 00136 foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) { 00137 const QString uriString = metaInfoItem.name(); 00138 const Nepomuk::Variant value(metaInfoItem.value()); 00139 data.insert(uriString, 00140 Nepomuk::Utils::formatPropertyValue(Nepomuk::Types::Property(), value)); 00141 } 00142 } 00143 00144 return data; 00145 } 00146 00147 QHash<KUrl, Nepomuk::Variant> KFileMetaDataReaderApplication::readFileAndContextMetaData(const QList<KUrl>& urls) const 00148 { 00149 QHash<KUrl, Nepomuk::Variant> metaData; 00150 00151 bool isNepomukIndexerActive = false; 00152 if (Nepomuk::ResourceManager::instance()->initialized()) { 00153 KConfig config("nepomukserverrc"); 00154 isNepomukIndexerActive = config.group("Service-nepomukfileindexer").readEntry("autostart", false); 00155 } else { 00156 // No context meta data can be read without enabled Nepomuk 00157 return readFileMetaData(urls); 00158 } 00159 00160 unsigned int rating = 0; 00161 QString comment; 00162 QList<Nepomuk::Tag> tags; 00163 00164 if (urls.count() == 1) { 00165 // Read the metadata of the file that are provided as properties 00166 // (e.g. image-size, artist, album, ...) 00167 bool useReadFromFileFallback = true; 00168 00169 Nepomuk::Resource file(urls.first()); 00170 if (file.isValid() && !file.resourceUri().isEmpty()) { 00171 QHash<QUrl, Nepomuk::Variant> variants = file.properties(); 00172 QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin(); 00173 while (it != variants.constEnd()) { 00174 Nepomuk::Types::Property prop(it.key()); 00175 metaData.insert(prop.uri(), Nepomuk::Utils::formatPropertyValue(prop, it.value(), 00176 QList<Nepomuk::Resource>() << file, 00177 Nepomuk::Utils::WithKioLinks)); 00178 ++it; 00179 } 00180 useReadFromFileFallback = !isNepomukIndexerActive || variants.isEmpty(); 00181 00182 rating = file.rating(); 00183 comment = file.description(); 00184 tags = file.tags(); 00185 } 00186 00187 if (useReadFromFileFallback) { 00188 // No metadata could be received with Nepomuk. Parse the file 00189 // itself as fallback to extract metadata. 00190 metaData = readFileMetaData(QList<KUrl>() << urls.first()); 00191 } 00192 } else { 00193 // Read the data for rating, comment and tags 00194 bool first = true; 00195 foreach (const KUrl& url, urls) { 00196 Nepomuk::Resource file(url); 00197 if (!file.isValid()) { 00198 continue; 00199 } 00200 00201 if (!first && rating != file.rating()) { 00202 rating = 0; // Reset rating 00203 } else if (first) { 00204 rating = file.rating(); 00205 } 00206 00207 if (!first && comment != file.description()) { 00208 comment.clear(); // Reset comment 00209 } else if (first) { 00210 comment = file.description(); 00211 } 00212 00213 if (!first && tags != file.tags()) { 00214 tags.clear(); // Reset tags 00215 } else if (first) { 00216 tags = file.tags(); 00217 } 00218 00219 first = false; 00220 } 00221 } 00222 00223 metaData.insert(KUrl("kfileitem#rating"), rating); 00224 metaData.insert(KUrl("kfileitem#comment"), comment); 00225 00226 QList<Nepomuk::Variant> tagVariants; 00227 foreach (const Nepomuk::Tag& tag, tags) { 00228 tagVariants.append(Nepomuk::Variant(tag)); 00229 } 00230 metaData.insert(KUrl("kfileitem#tags"), tagVariants); 00231 00232 return metaData; 00233 } 00234 00235 int main(int argc, char *argv[]) 00236 { 00237 KAboutData aboutData("kfilemetadatareader", "kio4", ki18n("KFileMetaDataReader"), 00238 "1.0", 00239 ki18n("KFileMetaDataReader can be used to read metadata from a file"), 00240 KAboutData::License_GPL, 00241 ki18n("(C) 2011, Peter Penz")); 00242 aboutData.addAuthor(ki18n("Peter Penz"), ki18n("Current maintainer"), "peter.penz19@gmail.com"); 00243 KComponentData compData(&aboutData); 00244 00245 KCmdLineArgs::init(argc, argv, &aboutData); 00246 00247 KCmdLineOptions options; 00248 options.add("file", ki18n("Only the meta data that is part of the file is read")); 00249 options.add("+[arg]", ki18n("List of URLs where the meta-data should be read from")); 00250 00251 KCmdLineArgs::addCmdLineOptions(options); 00252 00253 KFileMetaDataReaderApplication app(argc, argv); 00254 return app.exec(); 00255 } 00256 00257 #include "kfilemetadatareaderprocess.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:21:10 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:21:10 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.