• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

NepomukDaemons

eventmonitor.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE Project
00002    Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
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     // monitor the file system
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     // update the watches if the config changes
00056     connect( Config::self(), SIGNAL( configChanged() ),
00057              this, SLOT( updateWatches() ) );
00058 
00059     // start watching the index folders
00060     updateWatches();
00061 
00062     // FileSystemWatcher does not catch changes to files, only new and removed files
00063     // thus, we also do periodic updates of the whole index every two hours
00064     connect( &m_periodicUpdateTimer, SIGNAL( timeout() ),
00065              m_indexScheduler, SLOT( updateAll() ) );
00066     m_periodicUpdateTimer.setInterval( 2*60*60*1000 );
00067 
00068     // monitor the powermanagement to not drain the battery
00069     connect( Solid::PowerManagement::notifier(), SIGNAL( appShouldConserveResourcesChanged( bool ) ),
00070              this, SLOT( slotPowerManagementStatusChanged( bool ) ) );
00071 
00072     // setup the avail disk usage monitor
00073     connect( &m_availSpaceTimer, SIGNAL( timeout() ),
00074              this, SLOT( slotCheckAvailableSpace() ) );
00075     m_availSpaceTimer.start( 20*1000 ); // every 20 seconds should be enough
00076 
00077     if ( Config::self()->isInitialRun() ) {
00078         // TODO: add actions to this notification
00079 
00080         m_initialIndexTime.start();
00081 
00082         // inform the user about the initial indexing
00083         sendEvent( "initialIndexingStarted",
00084                    i18n( "Strigi file indexing started. Indexing all files for fast desktop searches may take a while." ),
00085                    "nepomuk" );
00086 
00087         // connect to get the end of initial indexing
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     // the hard way since the KDirWatch API is too simple
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 ); // check every 2 minutes
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         // if it does not work once, it will probably never work
00166         m_availSpaceTimer.stop();
00167     }
00168 }
00169 
00170 
00171 void Nepomuk::EventMonitor::slotIndexingStopped()
00172 {
00173     // inform the user about the end of initial indexing. This will only be called once
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         // after this much index work, it makes sense to optimize the full text index in the main model
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"

NepomukDaemons

Skip menu "NepomukDaemons"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal