00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "nepomukfilewatch.h"
00020
00021 #include <QtCore/QTimer>
00022 #include <QtCore/QDir>
00023 #include <QtCore/QRegExp>
00024 #include <QtCore/QFileInfo>
00025 #include <QtDBus/QDBusConnection>
00026
00027 #include <kdebug.h>
00028 #include <KUrl>
00029 #include <kmessagebox.h>
00030 #include <klocale.h>
00031 #include <KPluginFactory>
00032 #include <kio/netaccess.h>
00033
00034 #include <Soprano/Model>
00035 #include <Soprano/StatementIterator>
00036 #include <Soprano/Statement>
00037 #include <Soprano/Node>
00038 #include <Soprano/NodeIterator>
00039 #include <Soprano/QueryResultIterator>
00040 #include <Soprano/Vocabulary/Xesam>
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 using namespace Soprano;
00055
00056
00057 NEPOMUK_EXPORT_SERVICE( Nepomuk::FileWatch, "nepomukfilewatch")
00058
00059
00060 namespace {
00061 Soprano::QueryResultIterator queryChildren( Model* model, const QString& path )
00062 {
00063
00064 QString regexpPath( path );
00065 if ( regexpPath[regexpPath.length()-1] != '/' ) {
00066 regexpPath += '/';
00067 }
00068 regexpPath.replace( QRegExp( "([\\.\\?\\*\\\\+\\(\\)\\\\\\|\\[\\]{}])" ), "\\\\\\1" );
00069
00070
00071
00072
00073 return model->executeQuery( QString( "prefix xesam: <http://freedesktop.org/standards/xesam/1.0/core#> "
00074 "select ?r ?p where { ?r xesam:url ?p . FILTER(REGEX(STR(?p), '^%1')) . }" ).arg( regexpPath ),
00075 Soprano::Query::QueryLanguageSparql );
00076 }
00077 }
00078
00079
00080
00081 Nepomuk::FileWatch::FileWatch( QObject* parent, const QList<QVariant>& )
00082 : Service( parent ),
00083 m_strigiParentUrlUri( "http://strigi.sf.net/ontologies/0.9#parentUrl" )
00084 {
00085
00086 QDBusConnection::sessionBus().connect( QString(), QString(), "org.kde.KDirNotify", "FileMoved",
00087 this, SIGNAL( fileMoved( const QString&, const QString& ) ) );
00088 QDBusConnection::sessionBus().connect( QString(), QString(), "org.kde.KDirNotify", "FilesRemoved",
00089 this, SIGNAL( filesDeleted( const QStringList& ) ) );
00090
00091
00092
00093 connect( this, SIGNAL( fileMoved( QString, QString ) ), this, SLOT( slotFileMoved( QString, QString ) ), Qt::QueuedConnection );
00094 connect( this, SIGNAL( filesDeleted( QStringList ) ), this, SLOT( slotFilesDeleted( QStringList ) ), Qt::QueuedConnection );
00095 }
00096
00097
00098 Nepomuk::FileWatch::~FileWatch()
00099 {
00100 }
00101
00102
00103 void Nepomuk::FileWatch::slotFileMoved( const QString& urlFrom, const QString& urlTo )
00104 {
00105 KUrl from( urlFrom );
00106 KUrl to( urlTo );
00107
00108 kDebug() << from << to;
00109
00110 if ( from.isEmpty() || to.isEmpty() ) {
00111 kDebug() << "empty path. Looks like a bug somewhere...";
00112 return;
00113 }
00114
00115 if ( mainModel() ) {
00116
00117
00118 removeMetaData( to );
00119
00120
00121 updateMetaData( from, to );
00122
00123
00124 QString fromPath = from.path();
00125 QList<Soprano::BindingSet> children = queryChildren( mainModel(), fromPath ).allBindings();
00126 foreach( const Soprano::BindingSet& bs, children ) {
00127 QString path = to.path();
00128 if ( !path.endsWith( '/' ) )
00129 path += '/';
00130 path += bs[1].toString().mid( fromPath.endsWith( '/' ) ? fromPath.length() : fromPath.length()+1 );
00131 updateMetaData( bs[1].toString(), path );
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 }
00143 else {
00144 kDebug() << "Could not contact Nepomuk server.";
00145 }
00146 }
00147
00148
00149 void Nepomuk::FileWatch::slotFilesDeleted( const QStringList& paths )
00150 {
00151 foreach( const QString& path, paths ) {
00152 slotFileDeleted( path );
00153 }
00154 }
00155
00156
00157 void Nepomuk::FileWatch::slotFileDeleted( const QString& urlString )
00158 {
00159 KUrl url( urlString );
00160
00161 kDebug() << url;
00162
00163 if ( mainModel() ) {
00164 removeMetaData( url );
00165
00166
00167 foreach( Soprano::Node node, queryChildren( mainModel(), url.path() ).iterateBindings( 0 ).allNodes() ) {
00168 mainModel()->removeAllStatements( Statement( node, Node(), Node() ) );
00169 }
00170 }
00171 else {
00172 kDebug() << "Could not contact Nepomuk server.";
00173 }
00174 }
00175
00176
00177 void Nepomuk::FileWatch::removeMetaData( const KUrl& url )
00178 {
00179 kDebug() << url;
00180
00181 if ( url.isEmpty() ) {
00182 kDebug() << "empty path. Looks like a bug somewhere...";
00183 return;
00184 }
00185
00186 mainModel()->removeAllStatements( Statement( url, Node(), Node() ) );
00187
00188
00189 }
00190
00191
00192 void Nepomuk::FileWatch::updateMetaData( const KUrl& from, const KUrl& to )
00193 {
00194 kDebug() << from << "->" << to;
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 Soprano::Node oldResource = from;
00205 Soprano::Node newResource = to;
00206
00207
00208
00209 if ( mainModel()->containsAnyStatement( Soprano::Statement( oldResource, Soprano::Node(), Soprano::Node() ) ) ) {
00210
00211 QList<Soprano::Statement> sl = mainModel()->listStatements( Soprano::Statement( oldResource,
00212 Soprano::Node(),
00213 Soprano::Node() ) ).allStatements();
00214 Q_FOREACH( Soprano::Statement s, sl ) {
00215 if ( s.predicate() == Soprano::Vocabulary::Xesam::url() ) {
00216 mainModel()->addStatement( Soprano::Statement( newResource,
00217 s.predicate(),
00218 Soprano::LiteralValue( to.path() ),
00219 s.context() ) );
00220 }
00221 else if ( s.predicate() == m_strigiParentUrlUri ) {
00222 mainModel()->addStatement( Soprano::Statement( newResource,
00223 s.predicate(),
00224 Soprano::LiteralValue( to.directory( KUrl::IgnoreTrailingSlash ) ),
00225 s.context() ) );
00226 }
00227 else {
00228 mainModel()->addStatement( Soprano::Statement( newResource,
00229 s.predicate(),
00230 s.object(),
00231 s.context() ) );
00232 }
00233 }
00234
00235 mainModel()->removeStatements( sl );
00236
00237
00238
00239
00240
00241 sl = mainModel()->listStatements( Statement( Node(),
00242 Node(),
00243 oldResource ) ).allStatements();
00244 Q_FOREACH( Soprano::Statement s, sl ) {
00245 mainModel()->addStatement( Soprano::Statement( s.subject(),
00246 s.predicate(),
00247 newResource,
00248 s.context() ) );
00249 }
00250 mainModel()->removeStatements( sl );
00251
00252 }
00253 }
00254
00255 #include "nepomukfilewatch.moc"