• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • servicestub
servicestub/main.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE Project
2  Copyright (c) 2008-2011 Sebastian Trueg <trueg@kde.org>
3 
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.
7 
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.
12 
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.
17 */
18 
19 
20 #include <KComponentData>
21 #include <KCmdLineArgs>
22 #include <KAboutData>
23 #include <KService>
24 #include <KServiceTypeTrader>
25 #include <KDebug>
26 #include <KApplication>
27 
28 #include <QtCore/QTextStream>
29 #include <QtCore/QTimer>
30 #include <QtGui/QApplication>
31 #include <QtDBus/QDBusConnection>
32 #include <QtDBus/QDBusConnectionInterface>
33 
34 #include <signal.h>
35 #include <stdio.h>
36 
37 #include "nepomukversion.h"
38 
39 #include "servicecontrol.h"
40 #include "priority.h"
41 
42 namespace {
43  Nepomuk2::ServiceControl* s_control = 0;
44 
45 #ifndef Q_OS_WIN
46  void signalHandler( int signal )
47  {
48  switch( signal ) {
49  case SIGHUP:
50  case SIGQUIT:
51  case SIGINT:
52  if( s_control ) {
53  delete s_control;
54  s_control = 0;
55  }
56  QCoreApplication::exit( 0 );
57  }
58  }
59 #endif
60 
61  void installSignalHandler() {
62 #ifndef Q_OS_WIN
63  struct sigaction sa;
64  ::memset( &sa, 0, sizeof( sa ) );
65  sa.sa_handler = signalHandler;
66  sigaction( SIGHUP, &sa, 0 );
67  sigaction( SIGINT, &sa, 0 );
68  sigaction( SIGQUIT, &sa, 0 );
69 #endif
70  }
71 }
72 
73 
74 int main( int argc, char** argv )
75 {
76  KAboutData aboutData( "nepomukservicestub",
77  "nepomukservicestub",
78  ki18n("Nepomuk Service Stub"),
79  NEPOMUK_VERSION_STRING,
80  ki18n("Nepomuk Service Stub"),
81  KAboutData::License_GPL,
82  ki18n("(c) 2008-2011, Sebastian Trüg"),
83  KLocalizedString(),
84  "http://nepomuk.kde.org" );
85  aboutData.setProgramIconName( "nepomuk" );
86  aboutData.addAuthor(ki18n("Sebastian Trüg"),ki18n("Maintainer"), "trueg@kde.org");
87 
88  KCmdLineOptions options;
89  options.add("+servicename", ki18nc("@info:shell", "Service to start"));
90  KCmdLineArgs::addCmdLineOptions( options );
91 
92  KCmdLineArgs::init( argc, argv, &aboutData );
93 
94  // FIXME: set the proper KConfig rc name using the service name
95 
96  KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
97 
98  if( args->count() != 1 ) {
99  // create dummy componentData for the locale
100  KComponentData dummy( "___nepomukdummyservicename___" );
101  KCmdLineArgs::usageError( i18n("No service name specified") );
102  }
103 
104  QTextStream s( stderr );
105 
106  QString serviceName = args->arg(0);
107  args->clear();
108 
109  aboutData.setAppName( serviceName.toLocal8Bit() );
110  KApplication app( /*GuiEnabeled - Required for KIdleTime*/ true );
111  app.disableSessionManagement();
112 
113  // We explicitly remove the MainApplication object cause it inteferes with the ability to
114  // properly shut down the nepomuk services
115  QDBusConnection con = QDBusConnection::sessionBus();
116  con.unregisterObject( QLatin1String("/MainApplication"), QDBusConnection::UnregisterNode );
117 
118  installSignalHandler();
119  KGlobal::locale()->insertCatalog( serviceName );
120 
121 
122  // check if NepomukServer is running
123  // ====================================
124 // if( !QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.NepomukServer" ) ) {
125 // s << "Nepomuk server not running." << endl;
126 // return ErrorMissingDependency;
127 // }
128 
129 
130  // search the service
131  // ====================================
132  KService::List services = KServiceTypeTrader::self()->query( "NepomukService", "DesktopEntryName == '" + serviceName + '\'' );
133  if( services.isEmpty() ) {
134  s << i18n( "Unknown service name:") << " " << serviceName << endl;
135  return Nepomuk2::ServiceControl::ErrorUnknownServiceName;
136  }
137  KService::Ptr service = services.first();
138 
139 
140  // Check if this service is already running
141  // ====================================
142  if( QDBusConnection::sessionBus().interface()->isServiceRegistered( Nepomuk2::ServiceControl::dbusServiceName( serviceName ) ) ) {
143  s << "Service " << serviceName << " already running." << endl;
144  return Nepomuk2::ServiceControl::ErrorServiceAlreadyRunning;
145  }
146 
147 
148  // Check the service dependencies
149  // ====================================
150  QStringList dependencies = service->property( "X-KDE-Nepomuk-dependencies", QVariant::StringList ).toStringList();
151  foreach( const QString &dep, dependencies ) {
152  if( !QDBusConnection::sessionBus().interface()->isServiceRegistered( Nepomuk2::ServiceControl::dbusServiceName( dep ) ) ) {
153  s << "Missing dependency " << dep << endl;
154  return Nepomuk2::ServiceControl::ErrorMissingDependency;
155  }
156  }
157 
158 
159  // Lower our priority by default which makes sense for most services since Nepomuk
160  // does not want to get in the way of the user
161  // TODO: make it configurable
162  // ====================================
163  if ( !lowerPriority() )
164  kDebug() << "Failed to lower priority.";
165  if ( !lowerSchedulingPriority() )
166  kDebug() << "Failed to lower scheduling priority.";
167  if ( !lowerIOPriority() )
168  kDebug() << "Failed to lower io priority.";
169 
170 
171  // register the service control
172  // ====================================
173  s_control = new Nepomuk2::ServiceControl( serviceName, service, &app );
174 
175 
176  // start the service (queued since we need an event loop)
177  // ====================================
178  QTimer::singleShot( 0, s_control, SLOT( start() ) );
179 
180  return app.exec();
181 }
Nepomuk2::ServiceControl::ErrorMissingDependency
Definition: servicecontrol.h:43
lowerSchedulingPriority
bool lowerSchedulingPriority()
Definition: priority.cpp:95
Nepomuk2::ServiceControl::dbusServiceName
static QString dbusServiceName(const QString &serviceName)
Definition: servicecontrol.cpp:121
Nepomuk2::ServiceControl::ErrorServiceAlreadyRunning
Definition: servicecontrol.h:41
Nepomuk2::ServiceControl::ErrorUnknownServiceName
Definition: servicecontrol.h:40
Nepomuk2::ServiceControl
Definition: servicecontrol.h:29
lowerPriority
bool lowerPriority()
Definition: priority.cpp:84
main
int main(int argc, char **argv)
Definition: servicestub/main.cpp:74
priority.h
servicecontrol.h
lowerIOPriority
bool lowerIOPriority()
Definition: priority.cpp:67
start
void start()
Definition: ontologydownloadjob.cpp:13
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal