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