• 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
  • storage
storage.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE Project
2  Copyright (c) 2007-10 Sebastian Trueg <trueg@kde.org>
3  Copyright (c) 2013 Vishesh Handa <me@vhanda.in>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "storage.h"
21 #include "repository.h"
22 #include "query/queryservice.h"
23 #include "backup/backupmanager.h"
24 #include "resourcemanager.h"
25 #include "graphmigrationjob.h"
26 
27 #include <QtCore/QFile>
28 #include <QtCore/QCoreApplication>
29 #include <QtCore/QTimer>
30 #include <QtCore/QDir>
31 #include <QtCore/QThread>
32 
33 #include <KDebug>
34 #include <KGlobal>
35 #include <KStandardDirs>
36 #include <KConfigGroup>
37 #include <KProcess>
38 #include <kdbusconnectionpool.h>
39 #include <Soprano/QueryResultIterator>
40 
41 namespace {
42  static const char s_repositoryName[] = "main";
43 }
44 
45 
46 Nepomuk2::Storage::Storage()
47  : Service2( 0, true /* delayed initialization */ )
48  , m_queryService( 0 )
49  , m_backupManager( 0 )
50  , m_resetInProgress( false )
51 {
52  // register the fancier name for this important service
53  QDBusConnection con = KDBusConnectionPool::threadConnection();
54  con.registerService( "org.kde.NepomukStorage" );
55 
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() ) );
62 }
63 
64 
65 Nepomuk2::Storage::~Storage()
66 {
67  slotRepositoryClosed();
68 
69  delete m_repository;
70  m_repository = 0;
71 }
72 
73 void Nepomuk2::Storage::slotRepositoryLoaded(Nepomuk2::Repository* repo, bool success)
74 {
75  if( !success ) {
76  setServiceInitialized( false );
77  return;
78  }
79 
80  // FIXME: ResourceManager::instance() will result in a another VirtuosoModel being made
81  // and then removed. Is that good?
82  //
83  // We overide the main model cause certain classes utilize the Resource class, and we
84  // don't want them using a different model
85  ResourceManager::instance()->setOverrideMainModel( repo );
86 
87  // Backup Service
88  if( !m_backupManager )
89  m_backupManager = new BackupManager( this );
90 
91  if( m_resetInProgress ) {
92  m_resetInProgress = false;
93  emit resetRepositoryDone( m_oldPath, m_newPath );
94  return;
95  }
96 
97  if( !dataMigrationRequired() ) {
98  openPublicInterfaces();
99  }
100  else {
101  KProcess::execute( "nepomukmigrator" );
102  setServiceInitialized( false );
103  }
104 }
105 
106 void Nepomuk2::Storage::openPublicInterfaces()
107 {
108  if( !m_queryService )
109  m_queryService = new Query::QueryService( m_repository, this );
110 
111  // DataManagement interface
112  m_repository->openPublicInterface();
113 
114  setServiceInitialized( true );
115  kDebug() << "Registered QueryService and DataManagement interface";
116 }
117 
118 void Nepomuk2::Storage::closePublicInterfaces()
119 {
120  setServiceInitialized( false );
121 
122  m_repository->closePublicInterface();
123 
124  delete m_queryService;
125  m_queryService = 0;
126 }
127 
128 void Nepomuk2::Storage::slotRepositoryClosed()
129 {
130  closePublicInterfaces();
131 
132  delete m_backupManager;
133  m_backupManager = 0;
134 }
135 
136 namespace {
137 
138  bool removeDir(const QString & dirName)
139  {
140  bool result = false;
141  QDir dir(dirName);
142 
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) {
147  if( info.isDir() )
148  result = removeDir(info.absoluteFilePath());
149  else
150  result = QFile::remove(info.absoluteFilePath());
151 
152  if( !result ) {
153  return result;
154  }
155  }
156  result = dir.rmdir(dirName);
157  }
158  return result;
159  }
160 }
161 
162 void Nepomuk2::Storage::resetRepository()
163 {
164  closePublicInterfaces();
165  m_resetInProgress = true;
166  m_repository->disconnect( this );
167 
168  connect( m_repository, SIGNAL(closed(Repository*)),
169  this, SLOT(slotRepositoryClosedAfterReset()), Qt::QueuedConnection );
170  m_repository->close();
171 }
172 
173 void Nepomuk2::Storage::slotRepositoryClosedAfterReset()
174 {
175  // Remove the damn repo
176  m_oldPath = m_repository->storagePath();
177  m_newPath = m_oldPath + QDateTime::currentDateTime().toString(Qt::ISODate);
178 
179  QFile::rename( m_oldPath, m_newPath );
180 
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() ) );
186 
187  m_repository->open();
188 }
189 
190 bool Nepomuk2::Storage::hasMigrationData()
191 {
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 . } }");
194 
195  bool mg = m_repository->executeQuery( query, Soprano::Query::QueryLanguageSparql ).boolValue();
196  kDebug() << "HAS MIGRATION DATA:" << mg;
197  return mg;
198 }
199 
200 void Nepomuk2::Storage::migrateGraphsByBackup()
201 {
202  closePublicInterfaces();
203 
204  if( !dataMigrationRequired() ) {
205  emit migrateGraphsDone();
206  return;
207  }
208 
209  if( !hasMigrationData() ) {
210  connect( this, SIGNAL(resetRepositoryDone(QString, QString)), this, SLOT(slotMigrationResetDone(QString, QString)) );
211  emit migrateGraphsPercent( -1 );
212  resetRepository();
213  return;
214  }
215 
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()) );
220 
221  m_backupManager->backupTagsAndRatings( backupLocation );
222  emit migrateGraphsPercent( 3 );
223  }
224  else {
225  slotMigrationBackupDone();
226  emit migrateGraphsPercent( 53 );
227  }
228 }
229 
230 void Nepomuk2::Storage::slotMigrationResetDone(const QString&, const QString& newPath)
231 {
232  disconnect( this, SIGNAL(resetRepositoryDone(QString, QString)), this, SLOT(slotMigrationResetDone(QString, QString)) );
233 
234  removeDir( newPath );
235  slotMigrationDone();
236 }
237 
238 void Nepomuk2::Storage::slotMigrationBackupDone()
239 {
240  disconnect( m_backupManager, SIGNAL(backupPercent(int)), this, SLOT(slotMigrationBackupProgress(int)) );
241  disconnect( m_backupManager, SIGNAL(backupDone()), this, SLOT(slotMigrationBackupDone()) );
242 
243  connect( m_backupManager, SIGNAL(restorePercent(int)), this, SLOT(slotMigrationRestoreProgress(int)) );
244  connect( m_backupManager, SIGNAL(restoreDone()), this, SLOT(slotMigrationRestoreDone()) );
245 
246  QString backupLocation = KStandardDirs::locateLocal( "data", "nepomuk/migrationBackup" );
247  m_backupManager->restore( backupLocation );
248 }
249 
250 void Nepomuk2::Storage::slotMigrationRestoreDone()
251 {
252  disconnect( m_backupManager, SIGNAL(restorePercent(int)), this, SLOT(slotMigrationRestoreProgress(int)) );
253  disconnect( m_backupManager, SIGNAL(restoreDone()), this, SLOT(slotMigrationRestoreDone()) );
254 
255  setDataMigrated();
256  emit migrateGraphsDone();
257 }
258 
259 void Nepomuk2::Storage::slotMigrationBackupProgress(int percent)
260 {
261  emit migrateGraphsPercent( 3 + percent/2 );
262 }
263 
264 void Nepomuk2::Storage::slotMigrationRestoreProgress(int percent)
265 {
266  emit migrateGraphsPercent( qMin( 53, 53 + percent/2 ) );
267 }
268 
269 void Nepomuk2::Storage::migrateGraphs()
270 {
271  closePublicInterfaces();
272 
273  KJob* job = new GraphMigrationJob( model() );
274 
275  QThread* migrationThread = new QThread( this );
276  job->moveToThread( migrationThread );
277  migrationThread->start();
278 
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 );
283  job->start();
284 }
285 
286 void Nepomuk2::Storage::slotMigrationDone()
287 {
288  setDataMigrated();
289  openPublicInterfaces();
290  emit migrateGraphsDone();
291 }
292 
293 void Nepomuk2::Storage::slotMigrationPercent(KJob* , ulong percent)
294 {
295  kDebug() << percent;
296  emit migrateGraphsPercent( percent );
297 }
298 
299 QString Nepomuk2::Storage::usedSopranoBackend() const
300 {
301  return m_repository->usedSopranoBackend();
302 }
303 
304 Soprano::Model* Nepomuk2::Storage::model()
305 {
306  return m_repository;
307 }
308 
309 bool Nepomuk2::Storage::dataMigrationRequired()
310 {
311  KConfigGroup group = KSharedConfig::openConfig("nepomukserverrc")->group( m_repository->name() + " Settings" );
312  return group.readEntry( "GraphMigrationRequired", true );
313 }
314 
315 void Nepomuk2::Storage::setDataMigrated()
316 {
317  KConfigGroup group = KSharedConfig::openConfig("nepomukserverrc")->group( m_repository->name() + " Settings" );
318  group.writeEntry( "GraphMigrationRequired", false );
319 
320  group = KSharedConfig::openConfig("nepomukstrigirc")->group( "General" );
321  group.writeEntry( "first run", true );
322 }
323 
324 
325 int main( int argc, char **argv ) {
326  KAboutData aboutData( "nepomukstorage",
327  "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"),
333  KLocalizedString(),
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");
337 
338  Nepomuk2::Service2::initUI<Nepomuk2::Storage>( argc, argv, aboutData );
339 }
340 
341 #include "storage.moc"
Nepomuk2::Storage::usedSopranoBackend
Q_SCRIPTABLE QString usedSopranoBackend() const
Definition: storage.cpp:299
Nepomuk2::Storage::openPublicInterfaces
Q_SCRIPTABLE void openPublicInterfaces()
Definition: storage.cpp:106
Nepomuk2::Storage::migrateGraphsByBackup
Q_SCRIPTABLE void migrateGraphsByBackup()
Definition: storage.cpp:200
Nepomuk2::Storage::model
Soprano::Model * model()
Definition: storage.cpp:304
backupmanager.h
Nepomuk2::ResourceManager::setOverrideMainModel
void setOverrideMainModel(Soprano::Model *model)
Override the main model used for all storage.
Definition: resourcemanager.cpp:409
Nepomuk2::Service2
New Base class for all Nepomuk services.
Definition: service2.h:88
queryservice.h
repository.h
Nepomuk2::Repository
Represents the main Nepomuk model.
Definition: repository.h:53
Nepomuk2::Storage::~Storage
~Storage()
Definition: storage.cpp:65
Nepomuk2::Storage::resetRepository
void resetRepository()
Switches off the Repository and renames the database directory to a new name.
Definition: storage.cpp:162
storage.h
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
resourcemanager.h
Nepomuk2::Storage::Storage
Storage()
Definition: storage.cpp:46
main
int main(int argc, char **argv)
Definition: storage.cpp:325
Nepomuk2::Storage::migrateGraphs
Q_SCRIPTABLE void migrateGraphs()
Definition: storage.cpp:269
Nepomuk2::Storage::closePublicInterfaces
Q_SCRIPTABLE void closePublicInterfaces()
Definition: storage.cpp:118
Nepomuk2::BackupManager
org::kde::nepomuk::BackupManager BackupManager
Definition: backupwizardpages.h:42
Nepomuk2::GraphMigrationJob
Definition: graphmigrationjob.h:35
graphmigrationjob.h
KJob
Nepomuk2::Query::QueryService
Definition: queryservice.h:43
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 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