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

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • services
  • fileindexer
  • indexer
simpleindexer.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Nepomuk KDE project.
3  Copyright (C) 2012 Vishesh Handa <me@vhanda.in>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) version 3, or any
9  later version accepted by the membership of KDE e.V. (or its
10  successor approved by the membership of KDE e.V.), which shall
11  act as a proxy defined in Section 6 of version 3 of the license.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "simpleindexer.h"
23 #include "datamanagement.h"
24 #include "storeresourcesjob.h"
25 
26 #include "nfo.h"
27 #include "nie.h"
28 #include "kext.h"
29 
30 #include <Soprano/Vocabulary/NRL>
31 #include <Soprano/Vocabulary/RDF>
32 
33 #include <QtCore/QFileInfo>
34 #include <QtCore/QDateTime>
35 
36 #include <KMimeType>
37 #include <KDebug>
38 #include <KJob>
39 #include <kde_file.h>
40 
41 using namespace Nepomuk2::Vocabulary;
42 using namespace Soprano::Vocabulary;
43 
44 Nepomuk2::SimpleIndexingJob::SimpleIndexingJob(const QUrl& fileUrl, QObject* parent)
45  : KJob( parent )
46  , m_nieUrl( fileUrl )
47 {
48 }
49 
50 Nepomuk2::SimpleIndexingJob::SimpleIndexingJob(const QUrl& fileUrl, const QString& mimeType, QObject* parent)
51  : KJob(parent)
52  , m_nieUrl( fileUrl )
53  , m_mimeType( mimeType )
54 {
55 }
56 
57 void Nepomuk2::SimpleIndexingJob::start()
58 {
59  SimpleResource m_res = createSimpleResource( m_nieUrl, &m_mimeType );
60  m_resUri = m_res.uri();
61 
62  // Indexing Level
63  m_res.setProperty(KExt::indexingLevel(), 1);
64 
65  QHash<QUrl, QVariant> additionalMetadata;
66  additionalMetadata.insert( RDF::type(), NRL::DiscardableInstanceBase() );
67 
68  SimpleResourceGraph graph;
69  graph << m_res;
70 
71  // In order to be compatibile with earlier releases we keep the old app name
72  KComponentData component = KGlobal::mainComponent();
73  if( component.componentName() != QLatin1String("nepomukindexer") ) {
74  component = KComponentData( QByteArray("nepomukindexer"),
75  QByteArray(), KComponentData::SkipMainComponentRegistration );
76  }
77  StoreResourcesJob* job( Nepomuk2::storeResources( graph, IdentifyNew,
78  NoStoreResourcesFlags, additionalMetadata, component ) );
79 
80  connect( job, SIGNAL(finished(KJob*)), this, SLOT(slotJobFinished(KJob*)) );
81 }
82 
83 void Nepomuk2::SimpleIndexingJob::slotJobFinished(KJob* job_)
84 {
85  StoreResourcesJob* job = dynamic_cast<StoreResourcesJob*>(job_);
86  if( job->error() ) {
87  kError() << "SimpleIndexError: " << job->errorString();
88 
89  setError( job->error() );
90  setErrorText( job->errorString() );
91  }
92 
93  m_resUri = job->mappings().value( m_resUri );
94  emitResult();
95 }
96 
97 // static
98 
99 Nepomuk2::SimpleResource Nepomuk2::SimpleIndexingJob::createSimpleResource(const KUrl& fileUrl, QString* mimeType)
100 {
101  SimpleResource res;
102 
103  res.addProperty(NIE::url(), fileUrl);
104  res.addProperty(NFO::fileName(), fileUrl.fileName());
105 
106  res.addType(NFO::FileDataObject());
107  res.addType(NIE::InformationElement());
108 
109  QFileInfo fileInfo(fileUrl.toLocalFile());
110  if( fileInfo.isDir() ) {
111  res.addType(NFO::Folder());
112  }
113  else {
114  res.addProperty(NFO::fileSize(), fileInfo.size());
115  }
116 
117  // Types by mime type
118  QString mime;
119  if( mimeType )
120  mime = *mimeType;
121 
122  if( mime.isEmpty() ) {
123  mime = KMimeType::findByUrl( fileUrl )->name();
124  if( mimeType )
125  *mimeType = mime;
126  }
127  QSet<QUrl> types = typesForMimeType( mime );
128  foreach(const QUrl& type, types)
129  res.addType( type );
130 
131  res.addProperty(NIE::mimeType(), mime );
132 
133  res.setProperty(NIE::created(), fileInfo.created());
134  res.setProperty(NIE::lastModified(), fileInfo.lastModified());
135 
136  return res;
137 }
138 
139 // static
140 QSet<QUrl> Nepomuk2::SimpleIndexingJob::typesForMimeType(const QString& mimeType)
141 {
142  QSet<QUrl> types;
143 
144  // Basic types
145  if( mimeType.contains(QLatin1String("audio")) )
146  types << NFO::Audio();
147  if( mimeType.contains(QLatin1String("video")) )
148  types << NFO::Video();
149  if( mimeType.contains(QLatin1String("image")) )
150  types << NFO::Image();
151  if( mimeType.contains(QLatin1String("text")) )
152  types << NFO::PlainTextDocument();
153  if( mimeType.contains(QLatin1String("document")) )
154  types << NFO::Document();
155  if( mimeType.contains(QLatin1String("font")) )
156  types << NFO::Font();
157 
158  // Presentation
159  if( mimeType.contains(QLatin1String("powerpoint") ) )
160  types << NFO::Presentation();
161  // Spreadsheet
162  if( mimeType.contains(QLatin1String("excel")) )
163  types << NFO::Spreadsheet();
164  // Html
165  if( mimeType.contains(QLatin1String("text/html") ) )
166  types << NFO::HtmlDocument();
167 
168  static QHash<QString, QUrl> typeMapper;
169  if( typeMapper.isEmpty() ) {
170  // Microsoft
171  typeMapper.insert( QLatin1String("application/msword"), NFO::PaginatedTextDocument() );
172  typeMapper.insert( QLatin1String("application/vnd.ms-powerpoint"), NFO::Presentation() );
173  typeMapper.insert( QLatin1String("application/vnd.ms-excel"), NFO::Spreadsheet() );
174 
175  // Office 2007
176  typeMapper.insert( QLatin1String("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
177  NFO::PaginatedTextDocument() );
178  typeMapper.insert( QLatin1String("application/vnd.openxmlformats-officedocument.presentationml.presentation"),
179  NFO::Presentation() );
180  typeMapper.insert( QLatin1String("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
181  NFO::Spreadsheet() );
182 
183  // Open document formats - http://en.wikipedia.org/wiki/OpenDocument_technical_specification
184  typeMapper.insert( QLatin1String("application/vnd.oasis.opendocument.text"), NFO::PaginatedTextDocument() );
185  typeMapper.insert( QLatin1String("application/vnd.oasis.opendocument.presentation"), NFO::Presentation() );
186  typeMapper.insert( QLatin1String("application/vnd.oasis.opendocument.spreadsheet"), NFO::Spreadsheet() );
187 
188  // Others
189  typeMapper.insert( QLatin1String("application/pdf"), NFO::PaginatedTextDocument() );
190  typeMapper.insert( QLatin1String("application/postscript"), NFO::PaginatedTextDocument() );
191  typeMapper.insert( QLatin1String("application/x-dvi"), NFO::PaginatedTextDocument() );
192  typeMapper.insert( QLatin1String("application/rtf"), NFO::PaginatedTextDocument() );
193 
194  // Ebooks
195  typeMapper.insert( QLatin1String("application/epub+zip"), NFO::PaginatedTextDocument() );
196  typeMapper.insert( QLatin1String("application/x-mobipocket-ebook"), NFO::PaginatedTextDocument() );
197 
198  // Archives - http://en.wikipedia.org/wiki/List_of_archive_formats
199  typeMapper.insert( QLatin1String("application/x-tar"), NFO::Archive() );
200  typeMapper.insert( QLatin1String("application/x-bzip2"), NFO::Archive() );
201  typeMapper.insert( QLatin1String("application/x-gzip"), NFO::Archive() );
202  typeMapper.insert( QLatin1String("application/x-lzip"), NFO::Archive() );
203  typeMapper.insert( QLatin1String("application/x-lzma"), NFO::Archive() );
204  typeMapper.insert( QLatin1String("application/x-lzop"), NFO::Archive() );
205  typeMapper.insert( QLatin1String("application/x-compress"), NFO::Archive() );
206  typeMapper.insert( QLatin1String("application/x-7z-compressed"), NFO::Archive() );
207  typeMapper.insert( QLatin1String("application/x-ace-compressed"), NFO::Archive() );
208  typeMapper.insert( QLatin1String("application/x-astrotite-afa"), NFO::Archive() );
209  typeMapper.insert( QLatin1String("application/x-alz-compressed"), NFO::Archive() );
210  typeMapper.insert( QLatin1String("application/vnd.android.package-archive"), NFO::Archive() );
211  typeMapper.insert( QLatin1String("application/x-arj"), NFO::Archive() );
212  typeMapper.insert( QLatin1String("application/vnd.ms-cab-compressed"), NFO::Archive() );
213  typeMapper.insert( QLatin1String("application/x-cfs-compressed"), NFO::Archive() );
214  typeMapper.insert( QLatin1String("application/x-dar"), NFO::Archive() );
215  typeMapper.insert( QLatin1String("application/x-lzh"), NFO::Archive() );
216  typeMapper.insert( QLatin1String("application/x-lzx"), NFO::Archive() );
217  typeMapper.insert( QLatin1String("application/x-rar-compressed"), NFO::Archive() );
218  typeMapper.insert( QLatin1String("application/x-stuffit"), NFO::Archive() );
219  typeMapper.insert( QLatin1String("application/x-stuffitx"), NFO::Archive() );
220  typeMapper.insert( QLatin1String("application/x-gtar"), NFO::Archive() );
221  typeMapper.insert( QLatin1String("application/zip"), NFO::Archive() );
222 
223  // Special images
224  typeMapper.insert( QLatin1String("image/vnd.microsoft.icon"), NFO::Icon() );
225  typeMapper.insert( QLatin1String("image/svg+xml"), NFO::VectorImage() );
226 
227  // Fonts
228  typeMapper.insert( QLatin1String("application/vnd.ms-fontobject"), NFO::Font() );
229  typeMapper.insert( QLatin1String("application/vnd.ms-opentype"), NFO::Font() );
230  }
231 
232  QHash< QString, QUrl >::const_iterator it = typeMapper.constFind( mimeType );
233  if( it != typeMapper.constEnd() )
234  types << it.value();
235 
236  return types;
237 }
238 
239 QString Nepomuk2::SimpleIndexingJob::mimeType()
240 {
241  return m_mimeType;
242 }
243 
244 QUrl Nepomuk2::SimpleIndexingJob::uri()
245 {
246  return m_resUri;
247 }
248 
storeresourcesjob.h
Nepomuk2::SimpleResource::setProperty
void setProperty(const QUrl &property, const QVariant &value)
Set a property overwriting existing values.
Definition: simpleresource.cpp:186
Nepomuk2::SimpleIndexingJob::SimpleIndexingJob
SimpleIndexingJob(const QUrl &fileUrl, QObject *parent=0)
Definition: simpleindexer.cpp:44
Nepomuk2::SimpleResource
Represents a snapshot of one Nepomuk resource.
Definition: simpleresource.h:46
QHash
QObject
Nepomuk2::SimpleIndexingJob::createSimpleResource
static SimpleResource createSimpleResource(const KUrl &fileUrl, QString *mimeType)
Fills a new SimpleResource with the basic data of the fileUrl such as type, mimetype, and other stat info.
Definition: simpleindexer.cpp:99
Nepomuk2::SimpleResource::addProperty
void addProperty(const QUrl &property, const QVariant &value)
Add a property.
Definition: simpleresource.cpp:206
Nepomuk2::SimpleResourceGraph
Definition: simpleresourcegraph.h:48
Nepomuk2::SimpleIndexingJob::mimeType
QString mimeType()
Definition: simpleindexer.cpp:239
Nepomuk2::storeResources
StoreResourcesJob * storeResources(const Nepomuk2::SimpleResourceGraph &resources, Nepomuk2::StoreIdentificationMode identificationMode=Nepomuk2::IdentifyNew, Nepomuk2::StoreResourcesFlags flags=Nepomuk2::NoStoreResourcesFlags, const QHash< QUrl, QVariant > &additionalMetadata=QHash< QUrl, QVariant >(), const KComponentData &component=KGlobal::mainComponent())
Store many resources at once.
Definition: datamanagement.cpp:144
Nepomuk2::SimpleIndexingJob::uri
QUrl uri()
Definition: simpleindexer.cpp:244
Nepomuk2::IdentifyNew
This is the default mode.
Definition: datamanagement.h:352
Nepomuk2::StoreResourcesJob::mappings
QHash< QUrl, QUrl > mappings() const
Definition: storeresourcesjob.cpp:78
datamanagement.h
Nepomuk2::SimpleIndexingJob::start
virtual void start()
Definition: simpleindexer.cpp:57
Nepomuk2::SimpleResource::addType
void addType(const QUrl &type)
A convenience method which adds a property of type rdf:type.
Definition: simpleresource.cpp:257
simpleindexer.h
Nepomuk2::SimpleResource::uri
QUrl uri() const
Definition: simpleresource.cpp:90
Nepomuk2::StoreResourcesJob
Definition: storeresourcesjob.h:34
Nepomuk2::NoStoreResourcesFlags
No flags - default behaviour.
Definition: datamanagement.h:364
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • 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