• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • Contact Us
 

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

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal