• 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
  • filewatch
metadatamover.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE Project
2  Copyright (c) 2009-2011 Sebastian Trueg <trueg@kde.org>
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 version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "metadatamover.h"
20 #include "nepomukfilewatch.h"
21 #include "datamanagement.h"
22 #include "resourcemanager.h"
23 
24 #include <QtCore/QTimer>
25 
26 #include <Soprano/Model>
27 #include <Soprano/Node>
28 #include <Soprano/QueryResultIterator>
29 
30 #include "nie.h"
31 
32 #include <KDebug>
33 #include <KJob>
34 #include <KLocale>
35 
36 using namespace Nepomuk2::Vocabulary;
37 
38 Nepomuk2::MetadataMover::MetadataMover( Soprano::Model* model, QObject* parent )
39  : QObject( parent ),
40  m_queueMutex(QMutex::Recursive),
41  m_model( model )
42 {
43  // setup the main update queue timer
44  m_queueTimer = new QTimer(this);
45  connect(m_queueTimer, SIGNAL(timeout()),
46  this, SLOT(slotWorkUpdateQueue()),
47  Qt::DirectConnection);
48 
49 }
50 
51 
52 Nepomuk2::MetadataMover::~MetadataMover()
53 {
54 }
55 
56 
57 void Nepomuk2::MetadataMover::moveFileMetadata( const KUrl& from, const KUrl& to )
58 {
59 // kDebug() << from << to;
60  Q_ASSERT( !from.path().isEmpty() && from.path() != "/" );
61  Q_ASSERT( !to.path().isEmpty() && to.path() != "/" );
62 
63  QMutexLocker lock(&m_queueMutex);
64 
65  UpdateRequest req( from, to );
66  if ( !m_updateQueue.contains( req ) )
67  m_updateQueue.enqueue( req );
68 
69  QTimer::singleShot(0, this, SLOT(slotStartUpdateTimer()));
70 }
71 
72 
73 void Nepomuk2::MetadataMover::removeFileMetadata( const KUrl& file )
74 {
75  Q_ASSERT( !file.path().isEmpty() && file.path() != "/" );
76  removeFileMetadata( KUrl::List() << file );
77 }
78 
79 
80 void Nepomuk2::MetadataMover::removeFileMetadata( const KUrl::List& files )
81 {
82  kDebug() << files;
83  QMutexLocker lock(&m_queueMutex);
84 
85  foreach( const KUrl& file, files ) {
86  UpdateRequest req( file );
87  if ( !m_updateQueue.contains( req ) )
88  m_updateQueue.enqueue( req );
89  }
90 
91  QTimer::singleShot(0, this, SLOT(slotStartUpdateTimer()));
92 }
93 
94 
95 void Nepomuk2::MetadataMover::slotWorkUpdateQueue()
96 {
97  // lock for initial iteration
98  QMutexLocker lock(&m_queueMutex);
99 
100  emit metadataUpdateStarted();
101 
102  // work the queue
103  if( !m_updateQueue.isEmpty() ) {
104  UpdateRequest updateRequest = m_updateQueue.dequeue();
105 
106  // unlock after queue utilization
107  lock.unlock();
108 
109 // kDebug() << "========================= handling" << updateRequest.source() << updateRequest.target();
110 
111  // an empty second url means deletion
112  if( updateRequest.target().isEmpty() ) {
113 
114  emit statusMessage( i18n("Remove metadata from %1",updateRequest.source().prettyUrl()) );
115  removeMetadata( updateRequest.source() );
116  }
117  else {
118  const KUrl from = updateRequest.source();
119  const KUrl to = updateRequest.target();
120 
121  emit statusMessage( i18n("Move metadata from %1 to %2",from.prettyUrl(),to.prettyUrl()) );
122 
123  // We do NOT get deleted messages for overwritten files! Thus, we
124  // have to remove all metadata for overwritten files first.
125  removeMetadata( to );
126 
127  // and finally update the old statements
128  updateMetadata( from, to );
129  }
130 
131 // kDebug() << "========================= done with" << updateRequest.source() << updateRequest.target();
132  }
133  else {
134  //kDebug() << "All update requests handled. Stopping timer.";
135 
136  emit metadataUpdateStopped();
137  m_queueTimer->stop();
138  }
139 }
140 
141 
142 namespace {
143  QUrl fetchUriFromUrl(const QUrl& nieUrl) {
144  if( nieUrl.isEmpty() ) {
145  return QUrl();
146  }
147 
148  QString query = QString::fromLatin1("select ?r where { ?r nie:url %1 . } LIMIT 1")
149  .arg( Soprano::Node::resourceToN3(nieUrl) );
150 
151  Soprano::Model* model = Nepomuk2::ResourceManager::instance()->mainModel();
152  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
153  if( it.next() )
154  return it[0].uri();
155 
156  return QUrl();
157  }
158 }
159 
160 void Nepomuk2::MetadataMover::removeMetadata( const KUrl& url )
161 {
162  if( url.isEmpty() ) {
163  kDebug() << "empty path. Looks like a bug somewhere...";
164  return;
165  }
166 
167  const QUrl uri = fetchUriFromUrl(url);
168  if( uri.isEmpty() )
169  return;
170 
171  // When the url is a folder we do not remove the metadata for all
172  // the files in that folder, since inotify gives us a delete event
173  // for each of those files
174  KJob* job = Nepomuk2::removeResources( QList<QUrl>() << uri );
175  job->exec();
176  if( job->error() )
177  kError() << job->errorString();
178 }
179 
180 
181 void Nepomuk2::MetadataMover::updateMetadata( const KUrl& from, const KUrl& to )
182 {
183  kDebug() << from << "->" << to;
184  if( from.isEmpty() || to.isEmpty() ) {
185  kError() << "Paths Empty - File a bug" << from << to;
186  return;
187  }
188 
189  const QUrl uri = fetchUriFromUrl(from);
190  if( !uri.isEmpty() ) {
191  KJob* job = Nepomuk2::setProperty(QList<QUrl>() << uri, NIE::url(), QVariantList() << to);
192  job->exec();
193  if( job->error() )
194  kError() << job->errorString();
195  }
196  else {
197  //
198  // If we have no metadata yet we need to tell the file indexer (if running) so it can
199  // create the metadata in case the target folder is configured to be indexed.
200  //
201  emit movedWithoutData( to.path() );
202  }
203 }
204 
205 
206 
207 // start the timer in the update thread
208 void Nepomuk2::MetadataMover::slotStartUpdateTimer()
209 {
210  if(!m_queueTimer->isActive()) {
211  m_queueTimer->start();
212  }
213 }
214 
215 #include "metadatamover.moc"
Nepomuk2::UpdateRequest::target
KUrl target() const
Definition: updaterequest.h:49
metadatamover.h
Nepomuk2::MetadataMover::moveFileMetadata
void moveFileMetadata(const KUrl &from, const KUrl &to)
Definition: metadatamover.cpp:57
QObject
Nepomuk2::MetadataMover::MetadataMover
MetadataMover(Soprano::Model *model, QObject *parent=0)
Definition: metadatamover.cpp:38
Nepomuk2::MetadataMover::~MetadataMover
~MetadataMover()
Definition: metadatamover.cpp:52
Nepomuk2::UpdateRequest
One update request with a timestamp.
Definition: updaterequest.h:32
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
datamanagement.h
Nepomuk2::removeResources
KJob * removeResources(const QList< QUrl > &resources, Nepomuk2::RemovalFlags flags=Nepomuk2::NoRemovalFlags, const KComponentData &component=KGlobal::mainComponent())
Completely remove resources from the database.
Nepomuk2::setProperty
KJob * setProperty(const QList< QUrl > &resources, const QUrl &property, const QVariantList &values, const KComponentData &component=KGlobal::mainComponent())
Set the values of a property for one or more resources.
Definition: datamanagement.cpp:48
Nepomuk2::MetadataMover::removeFileMetadata
void removeFileMetadata(const KUrl &file)
Definition: metadatamover.cpp:73
nepomukfilewatch.h
Nepomuk2::ResourceManager::mainModel
Soprano::Model * mainModel()
Retrieve the main data storage model.
Definition: resourcemanager.cpp:363
KJob
Nepomuk2::UpdateRequest::source
KUrl source() const
Definition: updaterequest.h:48
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 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