23 #include "resourcemanager_p.h"
37 #include <Soprano/Node>
38 #include <Soprano/Statement>
39 #include <Soprano/Vocabulary/RDF>
40 #include <Soprano/StatementIterator>
41 #include <Soprano/QueryResultIterator>
43 #include <QtCore/QFileInfo>
44 #include <QtCore/QMutex>
45 #include <QtCore/QMutexLocker>
46 #include <QtCore/QUuid>
47 #include <QtCore/QMutableHashIterator>
48 #include <QtCore/QCoreApplication>
49 #include <QtDBus/QDBusConnection>
50 #include <QtDBus/QDBusConnectionInterface>
51 #include <QtDBus/QDBusServiceWatcher>
52 #include <QtDBus/QDBusMetaType>
54 using namespace Soprano;
55 using namespace Soprano::Vocabulary;
56 using namespace Nepomuk2::Vocabulary;
60 Nepomuk2::ResourceManagerPrivate::ResourceManagerPrivate( ResourceManager* manager )
63 mutex(QMutex::Recursive),
73 if ( uri.isEmpty() ) {
75 return new ResourceData( QUrl(), QUrl(), type,
this );
79 if( uri.scheme().isEmpty() ) {
80 const QString uriString = uri.toString();
81 if( uriString[0] ==
'/' && QFile::exists(uriString) ) {
82 newUri.setScheme(QLatin1String(
"file"));
86 if( ResourceData* data = findData( newUri ) ) {
90 if( uri.scheme() != QLatin1String(
"nepomuk") )
91 return new ResourceData( QUrl(), newUri, type, this );
93 return new ResourceData( newUri, QUrl(), type,
this );
100 if ( !uriOrId.isEmpty() ) {
102 if( uriOrId[0] ==
'/' && QFile::exists(uriOrId) ) {
103 url.setScheme(
"file");
105 return data( url, type );
108 return new ResourceData( QUrl(), QUrl(), type,
this );
113 Nepomuk2::ResourceData* Nepomuk2::ResourceManagerPrivate::dataForResourceUri(
const QUrl& uri,
const QUrl& type )
115 if ( uri.isEmpty() ) {
117 return new ResourceData( QUrl(), QUrl(), type,
this );
120 if( ResourceData* data = findData( uri ) ) {
124 return new ResourceData( uri, QUrl(), type,
this );
129 QSet<Nepomuk2::ResourceData*> Nepomuk2::ResourceManagerPrivate::allResourceData()
131 return m_identifierKickOff.values().toSet() + m_urlKickOff.values().toSet() + m_initializedData.values().toSet();
134 bool Nepomuk2::ResourceManagerPrivate::shouldBeDeleted( ResourceData * rd )
const
141 void Nepomuk2::ResourceManagerPrivate::_k_storageServiceInitialized(
bool success )
144 kDebug() <<
"Nepomuk Storage service up and initialized.";
146 emit m_manager->nepomukSystemStarted();
149 _k_dbusServiceUnregistered( QLatin1String(
"org.kde.NepomukStorage") );
154 void Nepomuk2::ResourceManagerPrivate::_k_dbusServiceUnregistered(
const QString& serviceName )
156 if( serviceName == QLatin1String(
"org.kde.NepomukStorage") ) {
157 kDebug() <<
"Nepomuk Storage service went down.";
158 if( m_manager->d->mainModel && m_manager->d->mainModel->isValid() ){
159 m_manager->d->mainModel->disconnect();
161 emit m_manager->nepomukSystemStopped();
169 if ( !uri.isEmpty() ) {
170 QMutexLocker lock( &mutex );
172 if( uri.scheme() == QLatin1String(
"nepomuk") ) {
173 ResourceDataHash::iterator it = m_initializedData.find( uri );
174 if( it != m_initializedData.end() )
177 else if( uri.scheme().isEmpty() ) {
178 QString identifier = uri.toString();
180 if( it != m_identifierKickOff.end() )
185 if( it != m_urlKickOff.end() )
194 Nepomuk2::ResourceManager::ResourceManager()
196 d( new ResourceManagerPrivate( this ) )
200 QDBusConnection::sessionBus().connect( QLatin1String(
"org.kde.NepomukStorage"),
201 QLatin1String(
"/servicecontrol"),
202 QLatin1String(
"org.kde.nepomuk.ServiceControl"),
203 QLatin1String(
"serviceInitialized"),
205 SLOT(_k_storageServiceInitialized(
bool)) );
209 QDBusServiceWatcher *watcher =
new QDBusServiceWatcher( QLatin1String(
"org.kde.NepomukStorage"),
210 QDBusConnection::sessionBus(),
211 QDBusServiceWatcher::WatchForUnregistration,
213 connect( watcher, SIGNAL(serviceUnregistered(QString)),
214 this, SLOT(_k_dbusServiceUnregistered(QString)) );
217 QDBusMessage result = QDBusConnection::sessionBus().call(
218 QDBusMessage::createMethodCall(
219 QLatin1String(
"org.kde.NepomukStorage"),
220 QLatin1String(
"/servicecontrol"),
221 QLatin1String(
"org.kde.nepomuk.ServiceControl"),
222 QLatin1String(
"isInitialized")
225 if ( result.type() == QDBusMessage::ReplyMessage ) {
226 if ( result.arguments().count() > 0 ) {
227 const bool initialized = result.arguments()[0].toBool();
228 d->_k_storageServiceInitialized( initialized );
234 void Nepomuk2::ResourceManager::cleanupResources()
244 QMutexLocker lock( &d->mutex );
245 QList<ResourceData*> rdList = d->m_identifierKickOff.values() + d->m_urlKickOff.values() +
246 d->m_initializedData.values();
247 foreach(
const ResourceData* rd, rdList.toSet() ) {
248 foreach(Resource* res, rd->resources())
254 Nepomuk2::ResourceManager::~ResourceManager()
257 Q_ASSERT( d->m_identifierKickOff.isEmpty() );
258 Q_ASSERT( d->m_urlKickOff.isEmpty() );
259 Q_ASSERT( d->m_initializedData.isEmpty() );
264 if(s_instance ==
this) {
276 QCoreApplication* app = QCoreApplication::instance();
281 s_instance->setParent( app );
282 connect( app, SIGNAL(aboutToQuit()), s_instance, SLOT(cleanupResources()) );
290 QMutexLocker lock( &d->initMutex );
292 if( d->overrideModel ) {
296 if( !d->mainModel ) {
300 d->mainModel->init();
302 return d->mainModel->isValid() ? 0 : -1;
308 QMutexLocker lock( &d->initMutex );
309 if( d->overrideModel )
312 return d->mainModel && d->mainModel->isValid();
324 kDebug() <<
"(Nepomuk2::ResourceManager) error: " << uri <<
" " << errorCode;
325 emit error( uri, errorCode );
332 QString type = QLatin1String(
"res");
335 if(name == QLatin1String(
"ctx")) {
339 Soprano::Model* model = mainModel();
342 QString uuid = QUuid::createUuid().toString();
343 uuid = uuid.mid(1, uuid.length()-2);
344 QString uriString = QLatin1String(
"nepomuk:/") + type + QLatin1String(
"/") + uuid;
345 QString query = QString::fromLatin1(
"select iri_to_id( '%1', 0 )").arg( uriString );
347 Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageUser,
348 QLatin1String(
"sql") );
351 if( model->lastError() ) {
355 if( it[0].literal().toString().isEmpty() ) {
356 return QUrl( uriString );
366 if ( !d->overrideModel && !initialized() ) {
370 if( d->overrideModel )
371 return d->overrideModel;
377 void Nepomuk2::ResourceManager::slotPropertyAdded(
const Resource &res,
const Types::Property &prop,
const QVariant &value)
379 QMutexLocker lock( &d->mutex );
380 ResourceDataHash::iterator it = d->m_initializedData.find(res.
uri());
381 if(it != d->m_initializedData.end()) {
387 void Nepomuk2::ResourceManager::slotPropertyRemoved(
const Resource &res,
const Types::Property &prop,
const QVariant &value_)
389 QMutexLocker lock( &d->mutex );
390 ResourceDataHash::iterator it = d->m_initializedData.find(res.uri());
391 if(it != d->m_initializedData.end()) {
392 ResourceData* data = *it;
393 data->propertyRemoved(prop, value_);
397 void Nepomuk2::ResourceManager::slotResourceRemoved(
const QUrl& uri,
const QList<QUrl>& )
399 QMutexLocker lock( &d->mutex );
400 ResourceDataHash::iterator it = d->m_initializedData.find(uri);
401 if(it != d->m_initializedData.end()) {
402 ResourceData* data = *it;
405 d->m_initializedData.remove(uri);
411 QMutexLocker lock( &d->mutex );
413 if( model != d->mainModel ) {
414 d->overrideModel = model;
423 void Nepomuk2::ResourceManagerPrivate::addToWatcher(
const QUrl& uri )
431 QMutexLocker lock( &mutex );
438 QObject::connect( m_watcher, SIGNAL(resourceRemoved(QUrl,QList<QUrl>)),
439 m_manager, SLOT(slotResourceRemoved(QUrl, QList<QUrl>)) );
441 m_watcher->addResource( uri );
443 if( m_watcher->resourceCount() <= 1 ) {
448 void Nepomuk2::ResourceManagerPrivate::removeFromWatcher(
const QUrl& uri )
453 QMutexLocker lock( &mutex );
457 m_watcher->removeResource( uri );
459 if( !m_watcher->resourceCount() ) {
464 #include "resourcemanager.moc"
void remove()
Remove this resource completely.
The main Nepomuk data storage model.
void removeResource(const QString &uri)
Remove the resource denoted by uri completely.
void setOverrideMainModel(Soprano::Model *model)
Override the main model used for all storage.
void propertyAdded(const Types::Property &prop, const QVariant &value)
void notifyError(const QString &uri, int errorCode)
A property is a resource of type rdf:Property which relates a domain with a range.
static ResourceManager * instance()
Selectively monitor the nepomuk repository for changes.
int init()
Initialize the Nepomuk framework.
The ResourceManager is the central Nepomuk configuration point.
Resource is the central object type in Nepomuk.
QUrl uri() const
The URI of the resource, uniquely identifying it.
Soprano::Model * mainModel()
Retrieve the main data storage model.
QUrl generateUniqueUri(const QString &label)
Generates a unique URI that is not used in the store yet.