• 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
  • libnepomukcore
  • service
service2.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE Project
2  Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
3  Copyright (c) 2013 Vishesh Handa <me@vhanda.in>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "service2.h"
21 #include "servicecontrol2.h"
22 #include "nepomukversion.h"
23 #include "../../servicestub/priority.h"
24 
25 #include <QtCore/QTimer>
26 #include <QtDBus/QDBusConnection>
27 #include <QtDBus/QDBusConnectionInterface>
28 
29 #include <KAboutData>
30 #include <KDebug>
31 #include <KService>
32 
33 #include <signal.h>
34 
35 namespace {
36 #ifndef Q_OS_WIN
37  void signalHandler( int signal )
38  {
39  switch( signal ) {
40  case SIGHUP:
41  case SIGQUIT:
42  case SIGINT:
43  QCoreApplication::exit( 0 );
44  }
45  }
46 #endif
47 
48  void installSignalHandler() {
49 #ifndef Q_OS_WIN
50  struct sigaction sa;
51  ::memset( &sa, 0, sizeof( sa ) );
52  sa.sa_handler = signalHandler;
53  sigaction( SIGHUP, &sa, 0 );
54  sigaction( SIGINT, &sa, 0 );
55  sigaction( SIGQUIT, &sa, 0 );
56 #endif
57  }
58 }
59 
60 class Nepomuk2::Service2::Private
61 {
62 public:
63  Private( Service2* s ) : q(s) {}
64 
65  Service2* q;
66  ServiceControl2* m_serviceControl;
67 
68  bool delayedInitialization;
69 
70  QString serviceName;
71  QString readableName;
72  QString description;
73 
74  bool loadDetails();
75  void configurePriority();
76  bool createDBusInterfaces();
77 };
78 
79 
80 Nepomuk2::Service2::Service2( QObject* parent, bool delayedInitialization )
81  : QObject( parent ),
82  d( new Private(this) )
83 {
84  d->m_serviceControl = 0;
85  d->delayedInitialization = delayedInitialization;
86 }
87 
88 
89 Nepomuk2::Service2::~Service2()
90 {
91  delete d;
92 }
93 
94 
95 QString Nepomuk2::Service2::name()
96 {
97  return d->readableName;
98 }
99 
100 QString Nepomuk2::Service2::description()
101 {
102  return d->description;
103 }
104 
105 
106 void Nepomuk2::Service2::setServiceInitialized( bool success )
107 {
108  QMetaObject::invokeMethod( d->m_serviceControl,
109  "setServiceInitialized",
110  Qt::QueuedConnection, // needs to be queued to give the service time to register with DBus
111  Q_ARG(bool, success) );
112 }
113 
114 bool Nepomuk2::Service2::Private::loadDetails()
115 {
116  KService::Ptr servicePtr = KService::serviceByDesktopName( serviceName );
117  if( servicePtr.isNull() )
118  return false;
119 
120  readableName = servicePtr->name();
121  description = servicePtr->comment();
122 
123  return true;
124 }
125 
126 void Nepomuk2::Service2::Private::configurePriority()
127 {
128  // Lower our priority by default which makes sense for most services since Nepomuk
129  // does not want to get in the way of the user
130  // TODO: make it configurable
131  // ====================================
132  if ( !lowerPriority() )
133  kDebug() << "Failed to lower priority.";
134  if ( !lowerSchedulingPriority() )
135  kDebug() << "Failed to lower scheduling priority.";
136  if ( !lowerIOPriority() )
137  kDebug() << "Failed to lower io priority.";
138 }
139 
140 bool Nepomuk2::Service2::Private::createDBusInterfaces()
141 {
142  QTextStream s( stderr );
143 
144  // register the service
145  // ====================================
146  QDBusConnection bus = QDBusConnection::sessionBus();
147  if( !bus.registerService( q->dbusServiceName() ) ) {
148  s << "Failed to register dbus service " << q->dbusServiceName() << "." << endl;
149  return false;
150  }
151 
152  bus.registerObject( '/' + serviceName, q,
153  QDBusConnection::ExportScriptableSlots |
154  QDBusConnection::ExportScriptableSignals |
155  QDBusConnection::ExportScriptableProperties |
156  QDBusConnection::ExportAdaptors);
157 
158  m_serviceControl = new ServiceControl2( q );
159  if( m_serviceControl->failedToStart() )
160  return false;
161 
162  return true;
163 }
164 
165 QString Nepomuk2::Service2::dbusServiceName()
166 {
167  return dbusServiceName( d->serviceName );
168 }
169 
170 // static
171 QString Nepomuk2::Service2::dbusServiceName(const QString& serviceName)
172 {
173  return QString("org.kde.nepomuk.services.%1").arg(serviceName.toLower());
174 }
175 
176 bool Nepomuk2::Service2::initCommon(const QString& name)
177 {
178  d->serviceName = name;
179 
180  if( !d->loadDetails() )
181  return false;
182 
183  d->configurePriority();
184 
185  if( !d->createDBusInterfaces() )
186  return false;
187 
188  if ( !d->delayedInitialization )
189  setServiceInitialized( true );
190 
191  installSignalHandler();
192 
193  return true;
194 }
195 
196 
197 
198 #include "service2.moc"
Nepomuk2::Service2::setServiceInitialized
void setServiceInitialized(bool success)
A Nepomuk service can make use of a warmup phase in which it is not usable yet.
Definition: service2.cpp:106
Nepomuk2::Service2::initCommon
bool initCommon(const QString &name)
Definition: service2.cpp:176
Nepomuk2::Service2::dbusServiceName
QString dbusServiceName()
Returns the DBus Service Name.
Definition: service2.cpp:165
Nepomuk2::Service2::Service2
Service2(QObject *parent=0, bool delayedInitialization=false)
Create a new Service.
Definition: service2.cpp:80
lowerSchedulingPriority
bool lowerSchedulingPriority()
Definition: priority.cpp:95
Nepomuk2::Service2::name
QString name()
Return the generic service name.
Definition: service2.cpp:95
QObject
Nepomuk2::Service2::description
QString description()
Returns the description of the service.
Definition: service2.cpp:100
service2.h
Nepomuk2::Service2::~Service2
virtual ~Service2()
Destructor.
Definition: service2.cpp:89
lowerPriority
bool lowerPriority()
Definition: priority.cpp:84
servicecontrol2.h
lowerIOPriority
bool lowerIOPriority()
Definition: priority.cpp:67
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 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