delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / nepomuk / services / strigi / eventmonitor.cpp
blob7fac5efc5604a80a2510e4d8a7c4e2958b91fb32
1 /* This file is part of the KDE Project
2 Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "eventmonitor.h"
20 #include "config.h"
21 #include "indexscheduler.h"
22 #include "filesystemwatcher.h"
24 #include <KDebug>
25 #include <KPassivePopup>
26 #include <KLocale>
27 #include <KDiskFreeSpaceInfo>
28 #include <KStandardDirs>
29 #include <KNotification>
30 #include <KIcon>
32 #include <Solid/PowerManagement>
34 #include <QtDBus/QDBusInterface>
37 namespace {
38 void sendEvent( const QString& event, const QString& text, const QString& iconName ) {
39 KNotification::event( event, text, KIcon( iconName ).pixmap( 32, 32 ) );
44 Nepomuk::EventMonitor::EventMonitor( IndexScheduler* scheduler, QObject* parent )
45 : QObject( parent ),
46 m_indexScheduler( scheduler ),
47 m_pauseState( NotPaused )
49 // monitor the file system
50 m_fsWatcher = new FileSystemWatcher( this );
51 m_fsWatcher->setWatchRecursively( true );
52 connect( m_fsWatcher, SIGNAL( dirty( QString ) ),
53 this, SLOT( slotDirDirty( QString ) ) );
55 // update the watches if the config changes
56 connect( Config::self(), SIGNAL( configChanged() ),
57 this, SLOT( updateWatches() ) );
59 // start watching the index folders
60 updateWatches();
62 // FileSystemWatcher does not catch changes to files, only new and removed files
63 // thus, we also do periodic updates of the whole index every two hours
64 connect( &m_periodicUpdateTimer, SIGNAL( timeout() ),
65 m_indexScheduler, SLOT( updateAll() ) );
66 m_periodicUpdateTimer.setInterval( 2*60*60*1000 );
68 // monitor the powermanagement to not drain the battery
69 connect( Solid::PowerManagement::notifier(), SIGNAL( appShouldConserveResourcesChanged( bool ) ),
70 this, SLOT( slotPowerManagementStatusChanged( bool ) ) );
72 // setup the avail disk usage monitor
73 connect( &m_availSpaceTimer, SIGNAL( timeout() ),
74 this, SLOT( slotCheckAvailableSpace() ) );
75 m_availSpaceTimer.start( 20*1000 ); // every 20 seconds should be enough
77 if ( Config::self()->isInitialRun() ) {
78 // TODO: add actions to this notification
80 m_initialIndexTime.start();
82 // inform the user about the initial indexing
83 sendEvent( "initialIndexingStarted",
84 i18n( "Strigi file indexing started. Indexing all files for fast desktop searches may take a while." ),
85 "nepomuk" );
87 // connect to get the end of initial indexing
88 connect( m_indexScheduler, SIGNAL( indexingStopped() ),
89 this, SLOT( slotIndexingStopped() ) );
91 else {
92 m_periodicUpdateTimer.start();
95 slotPowerManagementStatusChanged( Solid::PowerManagement::appShouldConserveResources() );
99 Nepomuk::EventMonitor::~EventMonitor()
104 void Nepomuk::EventMonitor::updateWatches()
106 // the hard way since the KDirWatch API is too simple
107 QStringList folders = Config::self()->folders();
108 if ( folders != m_fsWatcher->folders() ) {
109 m_fsWatcher->setFolders( Config::self()->folders() );
110 m_fsWatcher->setInterval( 2*60 ); // check every 2 minutes
111 m_fsWatcher->start();
116 void Nepomuk::EventMonitor::slotDirDirty( const QString& path )
118 if ( Config::self()->shouldFolderBeIndexed( path ) ) {
119 m_indexScheduler->updateDir( path );
124 void Nepomuk::EventMonitor::slotPowerManagementStatusChanged( bool conserveResources )
126 if ( !conserveResources && m_pauseState == PausedDueToPowerManagement ) {
127 kDebug() << "Resuming indexer due to power management";
128 m_pauseState = NotPaused;
129 m_indexScheduler->resume();
130 sendEvent( "indexingResumed", i18n("Resuming Strigi file indexing."), "solid" );
132 else if ( m_indexScheduler->isRunning() &&
133 !m_indexScheduler->isSuspended() ) {
134 kDebug() << "Pausing indexer due to power management";
135 m_pauseState = PausedDueToPowerManagement;
136 m_indexScheduler->suspend();
137 sendEvent( "indexingSuspended", i18n("Suspending Strigi file indexing to preserve resources."), "solid" );
142 void Nepomuk::EventMonitor::slotCheckAvailableSpace()
144 KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo( KStandardDirs::locateLocal( "data", "nepomuk/repository/", false ) );
145 if ( info.isValid() ) {
146 if ( info.available() <= Config::self()->minDiskSpace() ) {
147 if ( m_indexScheduler->isRunning() &&
148 !m_indexScheduler->isSuspended() ) {
149 m_pauseState = PausedDueToAvailSpace;
150 m_indexScheduler->suspend();
151 sendEvent( "indexingSuspended",
152 i18n("Local disk space is running low (%1 left). Suspending Strigi file indexing.",
153 KIO::convertSize( info.available() ) ),
154 "drive-harddisk" );
157 else if ( m_pauseState == PausedDueToAvailSpace ) {
158 kDebug() << "Resuming indexer due to disk space";
159 m_pauseState = NotPaused;
160 m_indexScheduler->resume();
161 sendEvent( "indexingResumed", i18n("Resuming Strigi file indexing."), "drive-harddisk" );
164 else {
165 // if it does not work once, it will probably never work
166 m_availSpaceTimer.stop();
171 void Nepomuk::EventMonitor::slotIndexingStopped()
173 // inform the user about the end of initial indexing. This will only be called once
174 if ( !m_indexScheduler->isSuspended() ) {
175 kDebug() << "initial indexing took" << m_initialIndexTime.elapsed();
176 sendEvent( "initialIndexingFinished",
177 i18nc( "@info %1 is a duration formatted using KLocale::formatDuration",
178 "Initial Desktop Search file indexing finished in %1",
179 KGlobal::locale()->formatDuration( m_initialIndexTime.elapsed() ) ),
180 "nepomuk" );
181 m_indexScheduler->disconnect( this );
183 // after this much index work, it makes sense to optimize the full text index in the main model
184 QDBusInterface( "org.kde.nepomuk.services.nepomukstorage", "/nepomukstorage", "org.kde.nepomuk.Storage" ).call( "optimize", "main" );
187 m_periodicUpdateTimer.start();
191 #include "eventmonitor.moc"