add more spacing
[personal-kdebase.git] / runtime / nepomuk / servicestub / main.cpp
blob482b8430608679ecb6506f1b24cf9a34f57b1ff3
1 /* This file is part of the KDE Project
2 Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include <KComponentData>
21 #include <KCmdLineArgs>
22 #include <KAboutData>
23 #include <KService>
24 #include <KServiceTypeTrader>
25 #include <KDebug>
27 #include <QtCore/QTextStream>
28 #include <QtCore/QTimer>
29 #include <QtGui/QApplication>
30 #include <QtDBus/QDBusConnection>
31 #include <QtDBus/QDBusConnectionInterface>
33 #include <signal.h>
34 #include <stdio.h>
36 #include "servicecontrol.h"
38 namespace {
39 #ifndef Q_OS_WIN
40 void signalHandler( int signal )
42 switch( signal ) {
43 case SIGHUP:
44 case SIGQUIT:
45 case SIGINT:
46 QCoreApplication::exit( 0 );
49 #endif
51 void installSignalHandler() {
52 #ifndef Q_OS_WIN
53 struct sigaction sa;
54 ::memset( &sa, 0, sizeof( sa ) );
55 sa.sa_handler = signalHandler;
56 sigaction( SIGHUP, &sa, 0 );
57 sigaction( SIGINT, &sa, 0 );
58 sigaction( SIGQUIT, &sa, 0 );
59 #endif
64 int main( int argc, char** argv )
66 KAboutData aboutData( "nepomukservicestub", "nepomuk",
67 ki18n("Nepomuk Service Stub"),
68 "0.2",
69 ki18n("Nepomuk Service Stub"),
70 KAboutData::License_GPL,
71 ki18n("(c) 2008, Sebastian Trüg"),
72 KLocalizedString(),
73 "http://nepomuk.kde.org" );
74 aboutData.addAuthor(ki18n("Sebastian Trüg"),ki18n("Maintainer"), "trueg@kde.org");
76 KCmdLineOptions options;
77 options.add("+servicename", ki18nc("@info:shell", "Service to start"));
78 KCmdLineArgs::addCmdLineOptions( options );
80 KCmdLineArgs::init( argc, argv, &aboutData );
82 QApplication app( argc, argv );
83 installSignalHandler();
84 QApplication::setQuitOnLastWindowClosed( false );
86 // FIXME: set the proper KConfig rc name using the service name
88 KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
90 if( args->count() != 1 ) {
91 KCmdLineArgs::usageError( i18n("No service name specified") );
94 QTextStream s( stderr );
96 QString serviceName = args->arg(0);
97 args->clear();
99 aboutData.setAppName( serviceName.toLocal8Bit() );
100 KComponentData compData( aboutData );
103 // check if NepomukServer is running
104 // ====================================
105 // if( !QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.NepomukServer" ) ) {
106 // s << "Nepomuk server not running." << endl;
107 // return ErrorMissingDependency;
108 // }
111 // search the service
112 // ====================================
113 KService::List services = KServiceTypeTrader::self()->query( "NepomukService", "DesktopEntryName == '" + serviceName + "'" );
114 if( services.isEmpty() ) {
115 s << i18n( "Unknown service name:") << " " << serviceName << endl;
116 return Nepomuk::ServiceControl::ErrorUnknownServiceName;
118 KService::Ptr service = services.first();
121 // Check if this service is already running
122 // ====================================
123 if( QDBusConnection::sessionBus().interface()->isServiceRegistered( Nepomuk::ServiceControl::dbusServiceName( serviceName ) ) ) {
124 s << "Service " << serviceName << " already running." << endl;
125 return Nepomuk::ServiceControl::ErrorServiceAlreadyRunning;
129 // Check the service dependencies
130 // ====================================
131 QStringList dependencies = service->property( "X-KDE-Nepomuk-dependencies", QVariant::StringList ).toStringList();
132 foreach( const QString &dep, dependencies ) {
133 if( !QDBusConnection::sessionBus().interface()->isServiceRegistered( Nepomuk::ServiceControl::dbusServiceName( dep ) ) ) {
134 s << "Missing dependency " << dep << endl;
135 return Nepomuk::ServiceControl::ErrorMissingDependency;
140 // register the service control
141 // ====================================
142 Nepomuk::ServiceControl* control = new Nepomuk::ServiceControl( serviceName, service, &app );
145 // start the service (queued since we need an event loop)
146 // ====================================
147 QTimer::singleShot( 0, control, SLOT( start() ) );
149 return app.exec();