• 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
  • tools
  • nepomukctl
tools/nepomukctl/main.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013 Gabriel Poesia <gabriel.poesia@gmail.com>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License as
6  published by the Free Software Foundation; either version 2 of
7  the License or (at your option) version 3 or any later version
8  accepted by the membership of KDE e.V. (or its successor approved
9  by the membership of KDE e.V.), which shall act as a proxy
10  defined in Section 14 of version 3 of the license.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <cstdlib>
22 #include <iostream>
23 
24 #if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
25  #include <unistd.h>
26 #elif defined _WIN32
27  #include <windows.h>
28  static inline void sleep(unsigned int x) { Sleep(1000 * x); };
29 #else
30  #define sleep(x)
31 #endif
32 
33 #include <QProcess>
34 #include <QCoreApplication>
35 #include <QDBusConnection>
36 #include <QDBusConnectionInterface>
37 #include <QDBusInterface>
38 #include <QDBusReply>
39 #include <QThread>
40 #include <KAboutData>
41 #include <KCmdLineArgs>
42 #include <KService>
43 #include <KServiceTypeTrader>
44 
45 KService::List allServices;
46 
47 void loadAllServiceNames();
48 void startNepomukServer();
49 void stopNepomukServer();
50 void showStatus();
51 bool isServerRunning();
52 
53 bool isServiceRunning( const KService::Ptr& ptr );
54 void startService( const KService::Ptr& ptr );
55 void stopService( const KService::Ptr& ptr );
56 
57 int main( int argc, char* argv[] )
58 {
59  QCoreApplication app(argc, argv);
60  KAboutData aboutData( "nepomukctl",
61  "nepomukctl",
62  ki18n( "Nepomuk server manipulation tool" ),
63  "1.0",
64  ki18n( "Starts, stops or lists running Nepomuk services" ),
65  KAboutData::License_GPL,
66  ki18n( "(c) 2013, Gabriel Poesia" ),
67  KLocalizedString(),
68  "http://nepomuk.kde.org" );
69 
70  aboutData.addAuthor( ki18n( "Gabriel Poesia" ),
71  ki18n( "Developer" ),
72  "gabriel.poesia@gmail.com" );
73  aboutData.setProgramIconName( "nepomuk" );
74 
75  KCmdLineOptions options;
76 
77  options.add( "+command", ki18n( "One of the available commands (start, stop, restart or status)" ) );
78  options.add( "+[service]", ki18n( "Name of a Nepomuk service (filewatch, fileindexer or storage)\n"
79  "When a service name isn't given, the command will be applied to the Nepomuk Server" ) );
80 
81  KCmdLineArgs::init( argc, argv, &aboutData );
82  KCmdLineArgs::addCmdLineOptions( options );
83  KCmdLineArgs::addStdCmdLineOptions();
84 
85  KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
86 
87  loadAllServiceNames();
88 
89  if( args->count() == 0 )
90  KCmdLineArgs::usage();
91 
92  QString serviceName;
93  bool specificService = false;
94 
95  KService::Ptr service;
96  if( args->count() == 2 ) {
97  serviceName = args->arg( 1 ).toLower();
98 
99  foreach( const KService::Ptr servicePtr, allServices ) {
100  QString name = servicePtr->desktopEntryName().toLower();
101  if( name == serviceName || name == QLatin1String("nepomuk") + serviceName ) {
102  service = servicePtr;
103  specificService = true;
104  }
105  }
106 
107  if( service.isNull() ) {
108  std::cerr << "Error: service '" << qPrintable( serviceName )
109  << "' does not exist." << std::endl;
110  KCmdLineArgs::usage();
111  }
112  }
113 
114  QString command = args->arg( 0 );
115 
116  bool start = command == "start" || command == "restart";
117  bool stop = command == "stop" || command == "restart";
118 
119  if( stop ) {
120  if( specificService )
121  stopService( service );
122  else
123  stopNepomukServer();
124  }
125 
126  if( start ) {
127  if( stop ) {
128  while( ( specificService && isServiceRunning( service ) ) ||
129  ( !specificService && isServerRunning() ) ) {
130  sleep(1);
131  }
132  }
133 
134  if( specificService )
135  startService( service );
136  else
137  startNepomukServer();
138  }
139 
140  if( !( start || stop ) ) {
141  if( command == "status" )
142  showStatus();
143  else {
144  std::cerr << "Error: unrecognized command '"
145  << qPrintable( command ) << "'." << std::endl;
146  KCmdLineArgs::usage();
147  }
148  }
149 
150  return EXIT_SUCCESS;
151 }
152 
153 void loadAllServiceNames()
154 {
155  allServices.clear();
156  allServices << KServiceTypeTrader::self()->query( "NepomukService2" );
157  allServices << KServiceTypeTrader::self()->query( "NepomukService" );
158 }
159 
160 bool isServiceRunning( const KService::Ptr& ptr )
161 {
162  QString serviceName = ptr->desktopEntryName();
163  return QDBusConnection::sessionBus().interface()->isServiceRegistered(
164  QLatin1String( "org.kde.nepomuk.services." ) + serviceName );
165 }
166 
167 bool isServerRunning()
168 {
169  return QDBusConnection::sessionBus().interface()->isServiceRegistered(
170  QLatin1String( "org.kde.NepomukServer" ) );
171 }
172 
173 void startNepomukServer()
174 {
175  if( isServerRunning() )
176  std::cerr << "Nepomuk Server already running." << std::endl;
177  else if( QProcess::startDetached( "nepomukserver" ) )
178  std::cerr << "Nepomuk Server started successfully." << std::endl;
179  else
180  std::cerr << "Could not start the Nepomuk Server." << std::endl;
181 }
182 
183 void stopNepomukServer()
184 {
185  QDBusInterface interface( "org.kde.NepomukServer", "/nepomukserver" );
186 
187  QDBusReply<void> reply = interface.call( "quit" );
188 
189  if( reply.isValid() )
190  std::cerr << "Nepomuk Server stopped succesfully." << std::endl;
191  else
192  std::cerr << "Couldn't stop the Nepomuk Server: "
193  << qPrintable( interface.lastError().message() )
194  << std::endl;
195 }
196 
197 void startService(const KService::Ptr& ptr)
198 {
199  if( !isServerRunning() ) {
200  std::cerr << "Nepomuk Server is not running. Starting it..."
201  << std::endl;
202  startNepomukServer();
203  return;
204  }
205 
206  if( isServiceRunning( ptr ) ) {
207  std::cerr << qPrintable( ptr->desktopEntryName() )
208  << " already running." << std::endl;
209  }
210  else {
211  QString program = ptr->exec();
212  QStringList args;
213 
214  if( program.isEmpty() ) {
215  program = QLatin1String("nepomukservicestub");
216  args << ptr->desktopEntryName();
217  }
218 
219  if( QProcess::startDetached( program, args ) )
220  std::cerr << qPrintable( ptr->desktopEntryName() )
221  << " started successfully." << std::endl;
222  else
223  std::cerr << qPrintable( ptr->desktopEntryName() )
224  << " could not be started." << std::endl;
225  }
226 }
227 
228 
229 void stopService( const KService::Ptr& ptr )
230 {
231  QString serviceName = ptr->desktopEntryName();
232  QDBusInterface interface( "org.kde.nepomuk.services." + serviceName,
233  "/servicecontrol" );
234 
235  QDBusReply<void> reply = interface.call( "shutdown" );
236 
237  if( reply.isValid() )
238  std::cerr << qPrintable( serviceName )
239  << " could not be started." << std::endl;
240  else
241  std::cerr << "Couldn't stop Nepomuk " << qPrintable( serviceName )
242  << ": " << qPrintable( interface.lastError().message() )
243  << std::endl;
244 }
245 
246 void showStatus()
247 {
248  if( isServerRunning() ) {
249  std::cerr << "Nepomuk Server is running." << std::endl;
250 
251  foreach( const KService::Ptr& ptr, allServices ) {
252  if( isServiceRunning( ptr ) ) {
253  QString name = ptr->desktopEntryName();
254  if( name.startsWith(QLatin1String("nepomuk")) ) {
255  name = name.mid( 7 );
256  }
257  std::cerr << "Service " << qPrintable( name ) << " is running."
258  << std::endl;
259  }
260  }
261  }
262  else
263  std::cerr << "Nepomuk Server is not running." << std::endl;
264 }
main
int main(int argc, char *argv[])
Definition: tools/nepomukctl/main.cpp:57
isServerRunning
bool isServerRunning()
Definition: tools/nepomukctl/main.cpp:167
stopService
void stopService(const KService::Ptr &ptr)
Definition: tools/nepomukctl/main.cpp:229
stopNepomukServer
void stopNepomukServer()
Definition: tools/nepomukctl/main.cpp:183
startService
void startService(const KService::Ptr &ptr)
Definition: tools/nepomukctl/main.cpp:197
allServices
KService::List allServices
Definition: tools/nepomukctl/main.cpp:45
loadAllServiceNames
void loadAllServiceNames()
Definition: tools/nepomukctl/main.cpp:153
startNepomukServer
void startNepomukServer()
Definition: tools/nepomukctl/main.cpp:173
isServiceRunning
bool isServiceRunning(const KService::Ptr &ptr)
Definition: tools/nepomukctl/main.cpp:160
showStatus
void showStatus()
Definition: tools/nepomukctl/main.cpp:246
start
void start()
Definition: ontologydownloadjob.cpp:13
sleep
#define sleep(x)
Definition: tools/nepomukctl/main.cpp:30
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