NepomukDaemons
main.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
00110
00111
00112
00113
00114
00115
00116
00117
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
00129
00130 if( QDBusConnection::sessionBus().interface()->isServiceRegistered( dbusServiceName( serviceName ) ) ) {
00131 s << "Service " << serviceName << " already running." << endl;
00132 return ErrorServiceAlreadyRunning;
00133 }
00134
00135
00136
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
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
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
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 }