00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "eventmonitor.h"
00020 #include "config.h"
00021 #include "indexscheduler.h"
00022 #include "filesystemwatcher.h"
00023
00024 #include <KDebug>
00025 #include <KPassivePopup>
00026 #include <KLocale>
00027 #include <KDiskFreeSpaceInfo>
00028 #include <KStandardDirs>
00029 #include <KNotification>
00030 #include <KIcon>
00031
00032 #include <Solid/PowerManagement>
00033
00034 #include <QtDBus/QDBusInterface>
00035
00036
00037 namespace {
00038 void sendEvent( const QString& event, const QString& text, const QString& iconName ) {
00039 KNotification::event( event, text, KIcon( iconName ).pixmap( 32, 32 ) );
00040 }
00041 }
00042
00043
00044 Nepomuk::EventMonitor::EventMonitor( IndexScheduler* scheduler, QObject* parent )
00045 : QObject( parent ),
00046 m_indexScheduler( scheduler ),
00047 m_pauseState( NotPaused )
00048 {
00049
00050 m_fsWatcher = new FileSystemWatcher( this );
00051 m_fsWatcher->setWatchRecursively( true );
00052 connect( m_fsWatcher, SIGNAL( dirty( QString ) ),
00053 this, SLOT( slotDirDirty( QString ) ) );
00054
00055
00056 connect( Config::self(), SIGNAL( configChanged() ),
00057 this, SLOT( updateWatches() ) );
00058
00059
00060 updateWatches();
00061
00062
00063
00064 connect( &m_periodicUpdateTimer, SIGNAL( timeout() ),
00065 m_indexScheduler, SLOT( updateAll() ) );
00066 m_periodicUpdateTimer.setInterval( 2*60*60*1000 );
00067
00068
00069 connect( Solid::PowerManagement::notifier(), SIGNAL( appShouldConserveResourcesChanged( bool ) ),
00070 this, SLOT( slotPowerManagementStatusChanged( bool ) ) );
00071
00072
00073 connect( &m_availSpaceTimer, SIGNAL( timeout() ),
00074 this, SLOT( slotCheckAvailableSpace() ) );
00075 m_availSpaceTimer.start( 20*1000 );
00076
00077 if ( Config::self()->isInitialRun() ) {
00078
00079
00080 m_initialIndexTime.start();
00081
00082
00083 sendEvent( "initialIndexingStarted",
00084 i18n( "Strigi file indexing started. Indexing all files for fast desktop searches may take a while." ),
00085 "nepomuk" );
00086
00087
00088 connect( m_indexScheduler, SIGNAL( indexingStopped() ),
00089 this, SLOT( slotIndexingStopped() ) );
00090 }
00091 else {
00092 m_periodicUpdateTimer.start();
00093 }
00094
00095 slotPowerManagementStatusChanged( Solid::PowerManagement::appShouldConserveResources() );
00096 }
00097
00098
00099 Nepomuk::EventMonitor::~EventMonitor()
00100 {
00101 }
00102
00103
00104 void Nepomuk::EventMonitor::updateWatches()
00105 {
00106
00107 QStringList folders = Config::self()->folders();
00108 if ( folders != m_fsWatcher->folders() ) {
00109 m_fsWatcher->setFolders( Config::self()->folders() );
00110 m_fsWatcher->setInterval( 2*60 );
00111 m_fsWatcher->start();
00112 }
00113 }
00114
00115
00116 void Nepomuk::EventMonitor::slotDirDirty( const QString& path )
00117 {
00118 if ( Config::self()->shouldFolderBeIndexed( path ) ) {
00119 m_indexScheduler->updateDir( path );
00120 }
00121 }
00122
00123
00124 void Nepomuk::EventMonitor::slotPowerManagementStatusChanged( bool conserveResources )
00125 {
00126 if ( !conserveResources && m_pauseState == PausedDueToPowerManagement ) {
00127 kDebug() << "Resuming indexer due to power management";
00128 m_pauseState = NotPaused;
00129 m_indexScheduler->resume();
00130 sendEvent( "indexingResumed", i18n("Resuming Strigi file indexing."), "solid" );
00131 }
00132 else if ( m_indexScheduler->isRunning() &&
00133 !m_indexScheduler->isSuspended() ) {
00134 kDebug() << "Pausing indexer due to power management";
00135 m_pauseState = PausedDueToPowerManagement;
00136 m_indexScheduler->suspend();
00137 sendEvent( "indexingSuspended", i18n("Suspending Strigi file indexing to preserve resources."), "solid" );
00138 }
00139 }
00140
00141
00142 void Nepomuk::EventMonitor::slotCheckAvailableSpace()
00143 {
00144 KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo( KStandardDirs::locateLocal( "data", "nepomuk/repository/", false ) );
00145 if ( info.isValid() ) {
00146 if ( info.available() <= Config::self()->minDiskSpace() ) {
00147 if ( m_indexScheduler->isRunning() &&
00148 !m_indexScheduler->isSuspended() ) {
00149 m_pauseState = PausedDueToAvailSpace;
00150 m_indexScheduler->suspend();
00151 sendEvent( "indexingSuspended",
00152 i18n("Local disk space is running low (%1 left). Suspending Strigi file indexing.",
00153 KIO::convertSize( info.available() ) ),
00154 "drive-harddisk" );
00155 }
00156 }
00157 else if ( m_pauseState == PausedDueToAvailSpace ) {
00158 kDebug() << "Resuming indexer due to disk space";
00159 m_pauseState = NotPaused;
00160 m_indexScheduler->resume();
00161 sendEvent( "indexingResumed", i18n("Resuming Strigi file indexing."), "drive-harddisk" );
00162 }
00163 }
00164 else {
00165
00166 m_availSpaceTimer.stop();
00167 }
00168 }
00169
00170
00171 void Nepomuk::EventMonitor::slotIndexingStopped()
00172 {
00173
00174 if ( !m_indexScheduler->isSuspended() ) {
00175 kDebug() << "initial indexing took" << m_initialIndexTime.elapsed();
00176 sendEvent( "initialIndexingFinished",
00177 i18nc( "@info %1 is a duration formatted using KLocale::formatDuration",
00178 "Initial Desktop Search file indexing finished in %1",
00179 KGlobal::locale()->formatDuration( m_initialIndexTime.elapsed() ) ),
00180 "nepomuk" );
00181 m_indexScheduler->disconnect( this );
00182
00183
00184 QDBusInterface( "org.kde.nepomuk.services.nepomukstorage", "/nepomukstorage", "org.kde.nepomuk.Storage" ).call( "optimize", "main" );
00185
00186
00187 m_periodicUpdateTimer.start();
00188 }
00189 }
00190
00191 #include "eventmonitor.moc"