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

NepomukDaemons

main.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 
00020 #include <KComponentData>
00021 #include <KCmdLineArgs>
00022 #include <KAboutData>
00023 #include <KService>
00024 #include <KServiceTypeTrader>
00025 #include <KDebug>
00026 #include <Nepomuk/Service>
00027 
00028 #include <QtCore/QCoreApplication>
00029 #include <QtCore/QTextStream>
00030 #include <QtDBus/QDBusConnection>
00031 #include <QtDBus/QDBusConnectionInterface>
00032 
00033 #include <signal.h>
00034 #include <stdio.h>
00035 
00036 #include "servicecontrol.h"
00037 #include "servicecontroladaptor.h"
00038 
00039 namespace {
00040     QString dbusServiceName( const QString& serviceName ) {
00041         return QString("org.kde.nepomuk.services.%1").arg(serviceName);
00042     }
00043 
00044     enum Errors {
00045         ErrorUnknownServiceName = -9,
00046         ErrorServiceAlreadyRunning = -10,
00047         ErrorFailedToStart = -11,
00048         ErrorMissingDependency = -12
00049     };
00050 
00051 #ifndef Q_OS_WIN
00052     void signalHandler( int signal )
00053     {
00054         switch( signal ) {
00055         case SIGHUP:
00056         case SIGQUIT:
00057         case SIGINT:
00058             QCoreApplication::exit( 0 );
00059         }
00060     }
00061 #endif
00062 
00063     void installSignalHandler() {
00064 #ifndef Q_OS_WIN
00065         struct sigaction sa;
00066         ::memset( &sa, 0, sizeof( sa ) );
00067         sa.sa_handler = signalHandler;
00068         sigaction( SIGHUP, &sa, 0 );
00069         sigaction( SIGINT, &sa, 0 );
00070         sigaction( SIGQUIT, &sa, 0 );
00071 #endif
00072     }
00073 }
00074 
00075 
00076 int main( int argc, char** argv )
00077 {
00078     KAboutData aboutData( "nepomukservicestub", "nepomuk",
00079                           ki18n("Nepomuk Service Stub"),
00080                           "0.1",
00081                           ki18n("Nepomuk Service Stub"),
00082                           KAboutData::License_GPL,
00083                           ki18n("(c) 2008, Sebastian Trüg"),
00084                           KLocalizedString(),
00085                           "http://nepomuk.kde.org" );
00086     aboutData.addAuthor(ki18n("Sebastian Trüg"),ki18n("Maintainer"), "trueg@kde.org");
00087 
00088     KCmdLineOptions options;
00089     options.add("+servicename", ki18nc("@info:shell", "Service to start"));
00090     KCmdLineArgs::addCmdLineOptions( options );
00091 
00092     KCmdLineArgs::init( argc, argv, &aboutData );
00093 
00094     QCoreApplication app( argc, argv );
00095     installSignalHandler();
00096     KComponentData compData( aboutData );
00097 
00098     // FIXME: set the proper KConfig rc name using the service name
00099 
00100     KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00101 
00102     if( args->count() != 1 ) {
00103         KCmdLineArgs::usageError( i18n("No service name specified") );
00104     }
00105 
00106     QTextStream s( stderr );
00107 
00108 
00109     // check if NepomukServer is running
00110     // ====================================
00111 //     if( !QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.NepomukServer" ) ) {
00112 //         s << "Nepomuk server not running." << endl;
00113 //         return ErrorMissingDependency;
00114 //     }
00115 
00116 
00117     // search the service
00118     // ====================================
00119     QString serviceName = args->arg(0);
00120     KService::List services = KServiceTypeTrader::self()->query( "NepomukService", "DesktopEntryName == '" + serviceName + "'" );
00121     if( services.isEmpty() ) {
00122         s << i18n( "Unknown service name:") << " " <<  serviceName << endl;
00123         return ErrorUnknownServiceName;
00124     }
00125     KService::Ptr service = services.first();
00126 
00127 
00128     // Check if this service is already running
00129     // ====================================
00130     if( QDBusConnection::sessionBus().interface()->isServiceRegistered( dbusServiceName( serviceName ) ) ) {
00131         s << "Service " << serviceName << " already running." << endl;
00132         return ErrorServiceAlreadyRunning;
00133     }
00134 
00135 
00136     // Check the service dependencies
00137     // ====================================
00138     QStringList dependencies = service->property( "X-KDE-Nepomuk-dependencies", QVariant::StringList ).toStringList();
00139     foreach( const QString &dep, dependencies ) {
00140         if( !QDBusConnection::sessionBus().interface()->isServiceRegistered( dbusServiceName( dep ) ) ) {
00141             s << "Missing dependency " << dep << endl;
00142             return ErrorMissingDependency;
00143         }
00144     }
00145 
00146 
00147     // register the service control
00148     // ====================================
00149     Nepomuk::ServiceControl* control = new Nepomuk::ServiceControl( &app );
00150     (void)new ServiceControlAdaptor( control );
00151     if( !QDBusConnection::sessionBus().registerObject( "/servicecontrol", control ) ) {
00152         s << "Failed to register dbus service " << dbusServiceName( serviceName ) << "." << endl;
00153         return ErrorFailedToStart;
00154     }
00155 
00156     // start the service
00157     // ====================================
00158     Nepomuk::Service* module = service->createInstance<Nepomuk::Service>( control );
00159     if( !module ) {
00160         s << "Failed to start service " << serviceName << "." << endl;
00161         return ErrorFailedToStart;
00162     }
00163 
00164     // register the service interface
00165     // ====================================
00166     if( !QDBusConnection::sessionBus().registerService( dbusServiceName( serviceName ) ) ) {
00167         s << "Failed to register dbus service " << dbusServiceName( serviceName ) << "." << endl;
00168         return ErrorFailedToStart;
00169     }
00170 
00171     QDBusConnection::sessionBus().registerObject( '/' + serviceName,
00172                                                   module,
00173                                                   QDBusConnection::ExportScriptableSlots |
00174                                                   QDBusConnection::ExportScriptableProperties |
00175                                                   QDBusConnection::ExportAdaptors);
00176 
00177     return app.exec();
00178 }

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