27 #include <QtCore/QFile> 
   28 #include <QtCore/QCoreApplication> 
   29 #include <QtCore/QTimer> 
   30 #include <QtCore/QDir> 
   31 #include <QtCore/QThread> 
   35 #include <KStandardDirs> 
   36 #include <KConfigGroup> 
   38 #include <kdbusconnectionpool.h> 
   39 #include <Soprano/QueryResultIterator> 
   42     static const char s_repositoryName[] = 
"main";
 
   49     , m_backupManager( 0 )
 
   50     , m_resetInProgress( false )
 
   53     QDBusConnection con = KDBusConnectionPool::threadConnection();
 
   54     con.registerService( 
"org.kde.NepomukStorage" );
 
   56     m_repository = 
new Repository( QLatin1String( s_repositoryName ) );
 
   57     connect( m_repository, SIGNAL( loaded( 
Repository*, 
bool ) ),
 
   58                 this, SLOT( slotRepositoryLoaded( 
Repository*, 
bool ) ) );
 
   59     connect( m_repository, SIGNAL( closed( 
Repository* ) ),
 
   60                 this, SLOT( slotRepositoryClosed() ) );
 
   61     QTimer::singleShot( 0, m_repository, SLOT( open() ) );
 
   67     slotRepositoryClosed();
 
   76         setServiceInitialized( 
false );
 
   88     if( !m_backupManager )
 
   91     if( m_resetInProgress ) {
 
   92         m_resetInProgress = 
false;
 
   93         emit resetRepositoryDone( m_oldPath, m_newPath );
 
   97     if( !dataMigrationRequired() ) {
 
   98         openPublicInterfaces();
 
  101         KProcess::execute( 
"nepomukmigrator" );
 
  102         setServiceInitialized( 
false );
 
  108     if( !m_queryService )
 
  112     m_repository->openPublicInterface();
 
  114     setServiceInitialized( 
true );
 
  115     kDebug() << 
"Registered QueryService and DataManagement interface";
 
  120     setServiceInitialized( 
false );
 
  122     m_repository->closePublicInterface();
 
  124     delete m_queryService;
 
  128 void Nepomuk2::Storage::slotRepositoryClosed()
 
  130     closePublicInterfaces();
 
  132     delete m_backupManager;
 
  138     bool removeDir(
const QString & dirName)
 
  143         if (dir.exists(dirName)) {
 
  144             QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden |
 
  145                                                    QDir::AllDirs | QDir::Files, QDir::DirsFirst);
 
  146             foreach(QFileInfo info, list) {
 
  148                     result = removeDir(info.absoluteFilePath());
 
  150                     result = QFile::remove(info.absoluteFilePath());
 
  156             result = dir.rmdir(dirName);
 
  164     closePublicInterfaces();
 
  165     m_resetInProgress = 
true;
 
  166     m_repository->disconnect( 
this );
 
  168     connect( m_repository, SIGNAL(closed(
Repository*)),
 
  169              this, SLOT(slotRepositoryClosedAfterReset()), Qt::QueuedConnection );
 
  170     m_repository->close();
 
  173 void Nepomuk2::Storage::slotRepositoryClosedAfterReset()
 
  176     m_oldPath = m_repository->storagePath();
 
  177     m_newPath = m_oldPath + QDateTime::currentDateTime().toString(Qt::ISODate);
 
  179     QFile::rename( m_oldPath, m_newPath );
 
  181     m_repository->disconnect( 
this );
 
  182     connect( m_repository, SIGNAL( loaded( 
Repository*, 
bool ) ),
 
  183                 this, SLOT( slotRepositoryLoaded( 
Repository*, 
bool ) ) );
 
  184     connect( m_repository, SIGNAL( closed( 
Repository* ) ),
 
  185                 this, SLOT( slotRepositoryClosed() ) );
 
  187     m_repository->open();
 
  190 bool Nepomuk2::Storage::hasMigrationData()
 
  192     QString query = QString::fromLatin1(
"ask where { graph ?g { ?r rdf:type ?t . FILTER(?t in (nao:Tag, nfo:FileDataObject)) . }" 
  193                                         "FILTER NOT EXISTS { ?g a nrl:DiscardableInstanceBase . } }");
 
  195     bool mg = m_repository->executeQuery( query, Soprano::Query::QueryLanguageSparql ).boolValue();
 
  196     kDebug() << 
"HAS MIGRATION DATA:" << mg;
 
  202     closePublicInterfaces();
 
  204     if( !dataMigrationRequired() ) {
 
  205         emit migrateGraphsDone();
 
  209     if( !hasMigrationData() ) {
 
  210         connect( 
this, SIGNAL(resetRepositoryDone(QString, QString)), 
this, SLOT(slotMigrationResetDone(QString, QString)) );
 
  211         emit migrateGraphsPercent( -1 );
 
  216     QString backupLocation = KStandardDirs::locateLocal( 
"data", 
"nepomuk/migrationBackup" );
 
  217     if( !QFile::exists(backupLocation) ) {
 
  218         connect( m_backupManager, SIGNAL(backupPercent(
int)), 
this, SLOT(slotMigrationBackupProgress(
int)) );
 
  219         connect( m_backupManager, SIGNAL(backupDone()), 
this, SLOT(slotMigrationBackupDone()) );
 
  221         m_backupManager->backupTagsAndRatings( backupLocation );
 
  222         emit migrateGraphsPercent( 3 );
 
  225         slotMigrationBackupDone();
 
  226         emit migrateGraphsPercent( 53 );
 
  230 void Nepomuk2::Storage::slotMigrationResetDone(
const QString&, 
const QString& newPath)
 
  232     disconnect( 
this, SIGNAL(resetRepositoryDone(QString, QString)), 
this, SLOT(slotMigrationResetDone(QString, QString)) );
 
  234     removeDir( newPath );
 
  238 void Nepomuk2::Storage::slotMigrationBackupDone()
 
  240     disconnect( m_backupManager, SIGNAL(backupPercent(
int)), 
this, SLOT(slotMigrationBackupProgress(
int)) );
 
  241     disconnect( m_backupManager, SIGNAL(backupDone()), 
this, SLOT(slotMigrationBackupDone()) );
 
  243     connect( m_backupManager, SIGNAL(restorePercent(
int)), 
this, SLOT(slotMigrationRestoreProgress(
int)) );
 
  244     connect( m_backupManager, SIGNAL(restoreDone()), 
this, SLOT(slotMigrationRestoreDone()) );
 
  246     QString backupLocation = KStandardDirs::locateLocal( 
"data", 
"nepomuk/migrationBackup" );
 
  247     m_backupManager->restore( backupLocation );
 
  250 void Nepomuk2::Storage::slotMigrationRestoreDone()
 
  252     disconnect( m_backupManager, SIGNAL(restorePercent(
int)), 
this, SLOT(slotMigrationRestoreProgress(
int)) );
 
  253     disconnect( m_backupManager, SIGNAL(restoreDone()), 
this, SLOT(slotMigrationRestoreDone()) );
 
  256     emit migrateGraphsDone();
 
  259 void Nepomuk2::Storage::slotMigrationBackupProgress(
int percent)
 
  261     emit migrateGraphsPercent( 3 + percent/2 );
 
  264 void Nepomuk2::Storage::slotMigrationRestoreProgress(
int percent)
 
  266     emit migrateGraphsPercent( qMin( 53, 53 + percent/2 ) );
 
  271     closePublicInterfaces();
 
  275     QThread* migrationThread = 
new QThread( 
this );
 
  276     job->moveToThread( migrationThread );
 
  277     migrationThread->start();
 
  279     connect( job, SIGNAL(finished(
KJob*)), migrationThread, SLOT(quit()), Qt::QueuedConnection );
 
  280     connect( migrationThread, SIGNAL(finished()), migrationThread, SLOT(deleteLater()) );
 
  281     connect( job, SIGNAL(finished(
KJob*)), 
this, SLOT(slotMigrationDone()), Qt::QueuedConnection );
 
  282     connect( job, SIGNAL(percent(
KJob*,ulong)), 
this, SLOT(slotMigrationPercent(
KJob*,ulong)), Qt::QueuedConnection );
 
  286 void Nepomuk2::Storage::slotMigrationDone()
 
  289     openPublicInterfaces();
 
  290     emit migrateGraphsDone();
 
  293 void Nepomuk2::Storage::slotMigrationPercent(
KJob* , ulong percent)
 
  296     emit migrateGraphsPercent( percent );
 
  301     return m_repository->usedSopranoBackend();
 
  309 bool Nepomuk2::Storage::dataMigrationRequired()
 
  311     KConfigGroup group = KSharedConfig::openConfig(
"nepomukserverrc")->group( m_repository->name() + 
" Settings" );
 
  312     return group.readEntry( 
"GraphMigrationRequired", 
true );
 
  315 void Nepomuk2::Storage::setDataMigrated()
 
  317     KConfigGroup group = KSharedConfig::openConfig(
"nepomukserverrc")->group( m_repository->name() + 
" Settings" );
 
  318     group.writeEntry( 
"GraphMigrationRequired", 
false );
 
  320     group = KSharedConfig::openConfig(
"nepomukstrigirc")->group( 
"General" );
 
  321     group.writeEntry( 
"first run", 
true );
 
  325 int main( 
int argc, 
char **argv ) {
 
  326     KAboutData aboutData( 
"nepomukstorage",
 
  328                           ki18n(
"Nepomuk Storage"),
 
  329                           NEPOMUK_VERSION_STRING,
 
  330                           ki18n(
"Nepomuk Storage"),
 
  331                           KAboutData::License_GPL,
 
  332                           ki18n(
"(c) 2008-2013, Sebastian Trüg"),
 
  334                           "http://nepomuk.kde.org" );
 
  335     aboutData.addAuthor(ki18n(
"Sebastian Trüg"),ki18n(
"Developer"), 
"trueg@kde.org");
 
  336     aboutData.addAuthor(ki18n(
"Vishesh Handa"),ki18n(
"Maintainer"), 
"me@vhanda.in");
 
  338     Nepomuk2::Service2::initUI<Nepomuk2::Storage>( argc, argv, aboutData );
 
  341 #include "storage.moc" 
Q_SCRIPTABLE QString usedSopranoBackend() const 
 
Q_SCRIPTABLE void openPublicInterfaces()
 
Q_SCRIPTABLE void migrateGraphsByBackup()
 
void setOverrideMainModel(Soprano::Model *model)
Override the main model used for all storage. 
 
New Base class for all Nepomuk services. 
 
Represents the main Nepomuk model. 
 
void resetRepository()
Switches off the Repository and renames the database directory to a new name. 
 
static ResourceManager * instance()
 
int main(int argc, char **argv)
 
Q_SCRIPTABLE void migrateGraphs()
 
Q_SCRIPTABLE void closePublicInterfaces()
 
org::kde::nepomuk::BackupManager BackupManager