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

NepomukDaemons

strigicontroller.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE Project
00002    Copyright (c) 2007 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 "strigicontroller.h"
00020 
00021 #include <QtDBus/QDBusConnection>
00022 #include <QtDBus/QDBusMessage>
00023 #include <QtCore/QTimer>
00024 #include <QtCore/QFile>
00025 #include <QtCore/QDir>
00026 
00027 #include <strigi/qtdbus/strigiclient.h>
00028 
00029 #include <KDebug>
00030 #include <KProcess>
00031 #include <KStandardDirs>
00032 #include <KMessageBox>
00033 #include <KLocale>
00034 
00035 
00036 Nepomuk::StrigiController::StrigiController( QObject* parent )
00037     : QObject( parent ),
00038       m_strigiProcess( 0 ),
00039       m_running5Minutes( false ),
00040       m_state( Idle )
00041 {
00042 }
00043 
00044 
00045 Nepomuk::StrigiController::~StrigiController()
00046 {
00047     shutdown();
00048 }
00049 
00050 
00051 Nepomuk::StrigiController::State Nepomuk::StrigiController::state() const
00052 {
00053     return m_state;
00054 }
00055 
00056 
00057 bool Nepomuk::StrigiController::start()
00058 {
00059     kDebug(300002) << "(Nepomuk::StrigiController::start)";
00060     if ( !m_strigiProcess ) {
00061         m_strigiProcess = new KProcess( this );
00062         m_strigiProcess->setOutputChannelMode( KProcess::ForwardedChannels );
00063         connect( m_strigiProcess, SIGNAL( finished( int, QProcess::ExitStatus) ),
00064                  this, SLOT( slotProcessFinished( int, QProcess::ExitStatus) ) );
00065     }
00066 
00067     m_strigiProcess->clearProgram();
00068     *m_strigiProcess << KStandardDirs::findExe( "strigidaemon" );
00069 
00070     if ( m_strigiProcess->state() == QProcess::NotRunning ) {
00071         m_running5Minutes = false;
00072         m_state = StartingUp;
00073         m_strigiProcess->start();
00074         if ( m_strigiProcess->waitForStarted() ) {
00075             m_state = Running;
00076             QTimer::singleShot( 50000, this, SLOT( slotRunning5Minutes() ) );
00077 
00078             kDebug(300002) << "Strigi started successfully.";
00079 
00080             // Strigi might refuse to start properly for some reason (invalid config, lock file invalid, whatever.)
00081             // In that case the dbus client would hang. Thus, we wait for 5 seconds before starting the indexing
00082             // (Which is ugly anyway since Strigi should do that automatically)
00083             QTimer::singleShot( 5000, this, SLOT( slotStartStrigiIndexing() ) );
00084 
00085             return true;
00086         }
00087         else {
00088             kDebug(300002) << "Failed to start strigidaemon.";
00089             m_state = Idle;
00090             return false;
00091         }
00092     }
00093     else {
00094         kDebug(300002) << "strigidaemon already running.";
00095         return false;
00096     }
00097 }
00098 
00099 
00100 void Nepomuk::StrigiController::shutdown()
00101 {
00102     kDebug(300002) << "(Nepomuk::StrigiController::shutdown)";
00103 
00104     StrigiClient strigiClient;
00105 
00106     m_state = ShuttingDown;
00107 
00108     if ( isRunning() ) {
00109         strigiClient.stopDaemon();
00110     }
00111 
00112     if ( state() == Running ) {
00113         kDebug(300002) << "We started Strigi ourselves. Trying to shut it down gracefully.";
00114         if ( !m_strigiProcess->waitForFinished(60000) ) {
00115             kDebug(300002) << "strigidaemon does not terminate properly. Killing process...";
00116             m_strigiProcess->terminate();
00117         }
00118         m_state = Idle;
00119     }
00120 }
00121 
00122 
00123 void Nepomuk::StrigiController::slotProcessFinished( int exitCode, QProcess::ExitStatus exitStatus )
00124 {
00125     if ( m_state == Running ) {
00126         kDebug(300002) << "strigidaemon shut down unexpectedly with exit code:" << exitCode;
00127 
00128         m_state = Idle;
00129 
00130         if ( exitStatus == QProcess::CrashExit ) {
00131             kDebug(300002) << "strigidaemon crashed.";
00132             if ( m_running5Minutes ) {
00133                 kDebug(300002) << "restarting strigidaemon...";
00134                 start();
00135             }
00136             else {
00137                 kDebug(300002) << "looks like a recurring crash!";
00138                 KMessageBox::error( 0,
00139                                     i18n( "Strigi (the desktop file indexer) crashed repeatedly. It will not be started again." ),
00140                                     i18n( "Strigi Desktop Search" ) );
00141             }
00142         }
00143     }
00144 }
00145 
00146 
00147 void Nepomuk::StrigiController::slotRunning5Minutes()
00148 {
00149     m_running5Minutes = true;
00150 }
00151 
00152 
00153 bool Nepomuk::StrigiController::isRunning()
00154 {
00155     return QDBusConnection::sessionBus().interface()->isServiceRegistered( "vandenoever.strigi" );
00156 }
00157 
00158 
00159 void Nepomuk::StrigiController::slotStartStrigiIndexing()
00160 {
00161     if ( isRunning() ) {
00162         StrigiClient strigiClient;
00163         strigiClient.startIndexing();
00164     }
00165 }
00166 
00167 #include "strigicontroller.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.4
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