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

KIO

  • sources
  • kde-4.12
  • kdelibs
  • kio
  • kfile
kfilemetadatareaderprocess.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3  * *
4  * This library is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU Library General Public *
6  * License as published by the Free Software Foundation; either *
7  * version 2 of the License, or (at your option) any later version. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12  * Library General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Library General Public License *
15  * along with this library; see the file COPYING.LIB. If not, write to *
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17  * Boston, MA 02110-1301, USA. *
18  *****************************************************************************/
19 
20 #include <iostream>
21 
22 #include <kaboutdata.h>
23 #include <kcmdlineargs.h>
24 #include <kfilemetainfo.h>
25 #include <kcomponentdata.h>
26 #include <kconfiggroup.h>
27 #include <klocale.h>
28 
29 #include <QtCore/QByteArray>
30 #include <QtCore/QCoreApplication>
31 #include <QtCore/QDataStream>
32 #include <QtCore/QHash>
33 #include <QtCore/QString>
34 #include <QtCore/QTimer>
35 
36 #define DISABLE_NEPOMUK_LEGACY
37 #include "config-nepomuk.h"
38 
39 #include <nepomuk/query/filequery.h>
40 #include <nepomuk/query/comparisonterm.h>
41 #include <nepomuk/query/andterm.h>
42 #include <nepomuk/query/resourceterm.h>
43 #include <nepomuk/query/resourcetypeterm.h>
44 #include <nepomuk/query/optionalterm.h>
45 #include <nepomuk/utils/utils.h>
46 #include <nepomuk/types/property.h>
47 #include <nepomuk/core/tag.h>
48 #include <nepomuk/core/variant.h>
49 #include <nepomuk/core/resourcemanager.h>
50 
51 using namespace std;
52 
53 class KFileMetaDataReaderApplication : public QCoreApplication
54 {
55  Q_OBJECT
56 
57 public:
58  KFileMetaDataReaderApplication(int& argc, char** argv);
59 
60 private Q_SLOTS:
61  void readAndSendMetaData();
62 
63 private:
64  void sendMetaData(const QHash<KUrl, Nepomuk::Variant>& data);
65  QHash<KUrl, Nepomuk::Variant> readFileMetaData(const QList<KUrl>& urls) const;
66  QHash<KUrl, Nepomuk::Variant> readFileAndContextMetaData(const QList<KUrl>& urls) const;
67 };
68 
69 
70 
71 KFileMetaDataReaderApplication::KFileMetaDataReaderApplication(int& argc, char** argv) :
72  QCoreApplication(argc, argv)
73 {
74  QTimer::singleShot(0, this, SLOT(readAndSendMetaData()));
75 }
76 
77 void KFileMetaDataReaderApplication::readAndSendMetaData()
78 {
79  const KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
80 
81  KUrl::List urls;
82  for (int i = 0; i < args->count(); ++i) {
83  urls.append(KUrl(args->arg(i)));
84  }
85 
86  QHash<KUrl, Nepomuk::Variant> metaData;
87  if (args->isSet("file")) {
88  metaData = readFileMetaData(urls);
89  } else {
90  metaData = readFileAndContextMetaData(urls);
91  }
92 
93  sendMetaData(metaData);
94 
95  quit();
96 }
97 
98 void KFileMetaDataReaderApplication::sendMetaData(const QHash<KUrl, Nepomuk::Variant>& data)
99 {
100  QByteArray byteArray;
101  QDataStream out(&byteArray, QIODevice::WriteOnly);
102 
103  QHashIterator<KUrl, Nepomuk::Variant> it(data);
104  while (it.hasNext()) {
105  it.next();
106 
107  out << it.key();
108 
109  // Unlike QVariant no streaming operators are implemented for Nepomuk::Variant.
110  // So it is required to manually encode the variant for the stream.
111  // The decoding counterpart is located in KFileMetaDataReader.
112  const Nepomuk::Variant& variant = it.value();
113  if (variant.isList()) {
114  out << 0 << variant.toStringList();
115  } else if (variant.isResource()) {
116  out << 1 << variant.toString();
117  } else {
118  out << 2 << variant.variant();
119  }
120  }
121 
122  cout << byteArray.toBase64().constData();
123 }
124 
125 QHash<KUrl, Nepomuk::Variant> KFileMetaDataReaderApplication::readFileMetaData(const QList<KUrl>& urls) const
126 {
127  QHash<KUrl, Nepomuk::Variant> data;
128 
129  // Currently only the meta-data of one file is supported.
130  // It might be an option to read all meta-data and show
131  // ranges for each key.
132  if (urls.count() == 1) {
133  const QString path = urls.first().toLocalFile();
134  KFileMetaInfo metaInfo(path, QString(), KFileMetaInfo::Fastest);
135  const QHash<QString, KFileMetaInfoItem> metaInfoItems = metaInfo.items();
136  foreach (const KFileMetaInfoItem& metaInfoItem, metaInfoItems) {
137  const QString uriString = metaInfoItem.name();
138  const Nepomuk::Variant value(metaInfoItem.value());
139  data.insert(uriString,
140  Nepomuk::Utils::formatPropertyValue(Nepomuk::Types::Property(), value));
141  }
142  }
143 
144  return data;
145 }
146 
147 QHash<KUrl, Nepomuk::Variant> KFileMetaDataReaderApplication::readFileAndContextMetaData(const QList<KUrl>& urls) const
148 {
149  QHash<KUrl, Nepomuk::Variant> metaData;
150 
151  bool isNepomukIndexerActive = false;
152  if (Nepomuk::ResourceManager::instance()->initialized()) {
153  KConfig config("nepomukserverrc");
154  isNepomukIndexerActive = config.group("Service-nepomukfileindexer").readEntry("autostart", false);
155  } else {
156  // No context meta data can be read without enabled Nepomuk
157  return readFileMetaData(urls);
158  }
159 
160  unsigned int rating = 0;
161  QString comment;
162  QList<Nepomuk::Tag> tags;
163 
164  if (urls.count() == 1) {
165  // Read the metadata of the file that are provided as properties
166  // (e.g. image-size, artist, album, ...)
167  bool useReadFromFileFallback = true;
168 
169  Nepomuk::Resource file(urls.first());
170  if (file.isValid() && !file.resourceUri().isEmpty()) {
171  QHash<QUrl, Nepomuk::Variant> variants = file.properties();
172  QHash<QUrl, Nepomuk::Variant>::const_iterator it = variants.constBegin();
173  while (it != variants.constEnd()) {
174  Nepomuk::Types::Property prop(it.key());
175  metaData.insert(prop.uri(), Nepomuk::Utils::formatPropertyValue(prop, it.value(),
176  QList<Nepomuk::Resource>() << file,
177  Nepomuk::Utils::WithKioLinks));
178  ++it;
179  }
180  useReadFromFileFallback = !isNepomukIndexerActive || variants.isEmpty();
181 
182  rating = file.rating();
183  comment = file.description();
184  tags = file.tags();
185  }
186 
187  if (useReadFromFileFallback) {
188  // No metadata could be received with Nepomuk. Parse the file
189  // itself as fallback to extract metadata.
190  metaData = readFileMetaData(QList<KUrl>() << urls.first());
191  }
192  } else {
193  // Read the data for rating, comment and tags
194  bool first = true;
195  foreach (const KUrl& url, urls) {
196  Nepomuk::Resource file(url);
197  if (!file.isValid()) {
198  continue;
199  }
200 
201  if (!first && rating != file.rating()) {
202  rating = 0; // Reset rating
203  } else if (first) {
204  rating = file.rating();
205  }
206 
207  if (!first && comment != file.description()) {
208  comment.clear(); // Reset comment
209  } else if (first) {
210  comment = file.description();
211  }
212 
213  if (!first && tags != file.tags()) {
214  tags.clear(); // Reset tags
215  } else if (first) {
216  tags = file.tags();
217  }
218 
219  first = false;
220  }
221  }
222 
223  metaData.insert(KUrl("kfileitem#rating"), rating);
224  metaData.insert(KUrl("kfileitem#comment"), comment);
225 
226  QList<Nepomuk::Variant> tagVariants;
227  foreach (const Nepomuk::Tag& tag, tags) {
228  tagVariants.append(Nepomuk::Variant(tag));
229  }
230  metaData.insert(KUrl("kfileitem#tags"), tagVariants);
231 
232  return metaData;
233 }
234 
235 int main(int argc, char *argv[])
236 {
237  KAboutData aboutData("kfilemetadatareader", "kio4", ki18n("KFileMetaDataReader"),
238  "1.0",
239  ki18n("KFileMetaDataReader can be used to read metadata from a file"),
240  KAboutData::License_GPL,
241  ki18n("(C) 2011, Peter Penz"));
242  aboutData.addAuthor(ki18n("Peter Penz"), ki18n("Current maintainer"), "peter.penz19@gmail.com");
243  KComponentData compData(&aboutData);
244 
245  KCmdLineArgs::init(argc, argv, &aboutData);
246 
247  KCmdLineOptions options;
248  options.add("file", ki18n("Only the meta data that is part of the file is read"));
249  options.add("+[arg]", ki18n("List of URLs where the meta-data should be read from"));
250 
251  KCmdLineArgs::addCmdLineOptions(options);
252 
253  KFileMetaDataReaderApplication app(argc, argv);
254  return app.exec();
255 }
256 
257 #include "kfilemetadatareaderprocess.moc"
KCmdLineArgs::addCmdLineOptions
static void addCmdLineOptions(const KCmdLineOptions &options, const KLocalizedString &name=KLocalizedString(), const QByteArray &id=QByteArray(), const QByteArray &afterId=QByteArray())
KFileMetaInfoItem
Definition: kfilemetainfoitem.h:34
KAboutData::addAuthor
KAboutData & addAuthor(const KLocalizedString &name, const KLocalizedString &task=KLocalizedString(), const QByteArray &emailAddress=QByteArray(), const QByteArray &webAddress=QByteArray())
KFileMetaInfo::Fastest
do the fastest possible read and omit all items that might need a significantly longer time than the ...
Definition: kfilemetainfo.h:66
KCmdLineOptions::add
KCmdLineOptions & add(const QByteArray &name, const KLocalizedString &description=KLocalizedString(), const QByteArray &defaultValue=QByteArray())
ki18n
KLocalizedString ki18n(const char *msg)
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
KCmdLineArgs
main
int main(int argc, char *argv[])
Definition: kfilemetadatareaderprocess.cpp:235
QString
QHash< KUrl, Nepomuk::Variant >
KCmdLineArgs::arg
QString arg(int n) const
klocale.h
KCmdLineArgs::isSet
bool isSet(const QByteArray &option) const
KUrl
KCmdLineArgs::count
int count() const
config
KSharedConfigPtr config()
KFileMetaInfo
KFileMetaInfo provides metadata extracted from a file or other resource.
Definition: kfilemetainfo.h:56
kcmdlineargs.h
KAboutData
KUrl::List
KConfig
kfilemetainfo.h
KCmdLineArgs::init
static void init(int argc, char **argv, const QByteArray &appname, const QByteArray &catalog, const KLocalizedString &programName, const QByteArray &version, const KLocalizedString &description=KLocalizedString(), StdCmdLineArgs stdargs=StdCmdLineArgs(CmdLineArgQt|CmdLineArgKDE))
KAboutData::License_GPL
quit
KAction * quit(const QObject *recvr, const char *slot, QObject *parent)
KFileMetaInfoItem::name
const QString & name() const
Localized name of the predicate.
Definition: kfilemetainfoitem.cpp:54
kaboutdata.h
kcomponentdata.h
KCmdLineOptions
KComponentData
kconfiggroup.h
QList< KUrl >
KFileMetaInfoItem::value
const QVariant & value() const
Retrieve the current value of this item.
Definition: kfilemetainfoitem.cpp:66
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

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 API Reference

Skip menu "kdelibs API Reference"
  • 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal