NepomukDaemons
nepomukmainmodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "nepomukmainmodel.h"
00022
00023 #include <Soprano/Node>
00024 #include <Soprano/Statement>
00025 #include <Soprano/StatementIterator>
00026 #include <Soprano/NodeIterator>
00027 #include <Soprano/QueryResultIterator>
00028 #include <Soprano/Client/DBusModel>
00029 #include <Soprano/Query/QueryLanguage>
00030 #include <Soprano/Util/DummyModel>
00031 #include <Soprano/Util/MutexModel>
00032
00033 #include <QtCore/QTimer>
00034 #include <QtCore/QDebug>
00035 #include <QtCore/QDir>
00036
00037 #include <stdlib.h>
00038
00039
00040
00041
00042
00043 using namespace Soprano;
00044
00045 namespace {
00046 QString nepomukServerSocketPath() {
00047 QString kdeHome = getenv( "KDEHOME" );
00048 if ( kdeHome.isEmpty() ) {
00049 kdeHome = QDir::homePath() + "/.kde4";
00050 }
00051 return kdeHome + "/share/apps/nepomuk/socket";
00052 }
00053 }
00054
00055
00056 Nepomuk::MainModel::MainModel( QObject* parent )
00057 : Soprano::Model(),
00058 m_dbusClient( "org.kde.NepomukStorage" ),
00059 m_dbusModel( 0 ),
00060 m_localSocketModel( 0 ),
00061 m_mutexModel( 0 ),
00062 m_socketConnectFailed( false )
00063 {
00064 setParent( parent );
00065 init();
00066 }
00067
00068
00069 Nepomuk::MainModel::~MainModel()
00070 {
00071 delete m_mutexModel;
00072 delete m_localSocketModel;
00073 delete m_dbusModel;
00074 }
00075
00076
00077
00078 void Nepomuk::MainModel::init()
00079 {
00080 if ( !m_dbusModel ) {
00081 m_dbusModel = m_dbusClient.createModel( "main" );
00082 }
00083
00084 if ( !m_mutexModel ) {
00085 m_mutexModel = new Soprano::Util::MutexModel( Soprano::Util::MutexModel::ReadWriteMultiThreading );
00086 }
00087
00088
00089
00090
00091
00092 if ( !m_socketConnectFailed && !m_localSocketClient.isConnected() ) {
00093 delete m_localSocketModel;
00094 QString socketName = nepomukServerSocketPath();
00095 if ( m_localSocketClient.connect( socketName ) ) {
00096 m_localSocketModel = m_localSocketClient.createModel( "main" );
00097 }
00098 else {
00099 m_socketConnectFailed = true;
00100 qDebug() << "Failed to connect to Nepomuk server via local socket" << socketName;
00101 }
00102 }
00103 }
00104
00105
00106 Soprano::Model* Nepomuk::MainModel::model() const
00107 {
00108 const_cast<MainModel*>(this)->init();
00109
00110
00111 if ( m_localSocketModel ) {
00112 if ( m_mutexModel->parentModel() != m_localSocketModel ) {
00113 m_mutexModel->setParentModel( m_localSocketModel );
00114 }
00115 }
00116 else if ( m_dbusModel ) {
00117 if ( m_mutexModel->parentModel() != m_dbusModel ) {
00118 m_mutexModel->setParentModel( m_dbusModel );
00119 }
00120 }
00121
00122 return m_mutexModel;
00123 }
00124
00125
00126 bool Nepomuk::MainModel::isValid() const
00127 {
00128 return m_dbusClient.isValid() || m_localSocketClient.isConnected();
00129 }
00130
00131
00132 Soprano::StatementIterator Nepomuk::MainModel::listStatements( const Statement& partial ) const
00133 {
00134 Soprano::StatementIterator it = model()->listStatements( partial );
00135 setError( model()->lastError() );
00136 return it;
00137 }
00138
00139
00140 Soprano::NodeIterator Nepomuk::MainModel::listContexts() const
00141 {
00142 Soprano::NodeIterator it = model()->listContexts();
00143 setError( model()->lastError() );
00144 return it;
00145 }
00146
00147
00148 Soprano::QueryResultIterator Nepomuk::MainModel::executeQuery( const QString& query,
00149 Soprano::Query::QueryLanguage language,
00150 const QString& userQueryLanguage ) const
00151 {
00152 Soprano::QueryResultIterator it = model()->executeQuery( query, language, userQueryLanguage );
00153 setError( model()->lastError() );
00154 return it;
00155 }
00156
00157
00158 bool Nepomuk::MainModel::containsStatement( const Statement& statement ) const
00159 {
00160 bool b = model()->containsStatement( statement );
00161 setError( model()->lastError() );
00162 return b;
00163 }
00164
00165
00166 bool Nepomuk::MainModel::containsAnyStatement( const Statement &statement ) const
00167 {
00168 bool b = model()->containsAnyStatement( statement );
00169 setError( model()->lastError() );
00170 return b;
00171 }
00172
00173
00174 bool Nepomuk::MainModel::isEmpty() const
00175 {
00176 bool b = model()->isEmpty();
00177 setError( model()->lastError() );
00178 return b;
00179 }
00180
00181
00182 int Nepomuk::MainModel::statementCount() const
00183 {
00184 int c = model()->statementCount();
00185 setError( model()->lastError() );
00186 return c;
00187 }
00188
00189
00190 Soprano::Error::ErrorCode Nepomuk::MainModel::addStatement( const Statement& statement )
00191 {
00192 Soprano::Error::ErrorCode c = model()->addStatement( statement );
00193 setError( model()->lastError() );
00194 return c;
00195 }
00196
00197
00198 Soprano::Error::ErrorCode Nepomuk::MainModel::removeStatement( const Statement& statement )
00199 {
00200 Soprano::Error::ErrorCode c = model()->removeStatement( statement );
00201 setError( model()->lastError() );
00202 return c;
00203 }
00204
00205
00206 Soprano::Error::ErrorCode Nepomuk::MainModel::removeAllStatements( const Statement& statement )
00207 {
00208 Soprano::Error::ErrorCode c = model()->removeAllStatements( statement );
00209 setError( model()->lastError() );
00210 return c;
00211 }
00212
00213
00214 Soprano::Node Nepomuk::MainModel::createBlankNode()
00215 {
00216 Soprano::Node n = model()->createBlankNode();
00217 setError( model()->lastError() );
00218 return n;
00219 }
00220
00221 #include "nepomukmainmodel.moc"