24 #if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
28 static inline void sleep(
unsigned int x) { Sleep(1000 * x); };
34 #include <QCoreApplication>
35 #include <QDBusConnection>
36 #include <QDBusConnectionInterface>
37 #include <QDBusInterface>
41 #include <KCmdLineArgs>
43 #include <KServiceTypeTrader>
57 int main(
int argc,
char* argv[] )
59 QCoreApplication app(argc, argv);
60 KAboutData aboutData(
"nepomukctl",
62 ki18n(
"Nepomuk server manipulation tool" ),
64 ki18n(
"Starts, stops or lists running Nepomuk services" ),
65 KAboutData::License_GPL,
66 ki18n(
"(c) 2013, Gabriel Poesia" ),
68 "http://nepomuk.kde.org" );
70 aboutData.addAuthor( ki18n(
"Gabriel Poesia" ),
72 "gabriel.poesia@gmail.com" );
73 aboutData.setProgramIconName(
"nepomuk" );
75 KCmdLineOptions options;
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" ) );
81 KCmdLineArgs::init( argc, argv, &aboutData );
82 KCmdLineArgs::addCmdLineOptions( options );
83 KCmdLineArgs::addStdCmdLineOptions();
85 KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
89 if( args->count() == 0 )
90 KCmdLineArgs::usage();
93 bool specificService =
false;
95 KService::Ptr service;
96 if( args->count() == 2 ) {
97 serviceName = args->arg( 1 ).toLower();
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;
107 if( service.isNull() ) {
108 std::cerr <<
"Error: service '" << qPrintable( serviceName )
109 <<
"' does not exist." << std::endl;
110 KCmdLineArgs::usage();
114 QString command = args->arg( 0 );
116 bool start = command ==
"start" || command ==
"restart";
117 bool stop = command ==
"stop" || command ==
"restart";
120 if( specificService )
134 if( specificService )
140 if( !( start || stop ) ) {
141 if( command ==
"status" )
144 std::cerr <<
"Error: unrecognized command '"
145 << qPrintable( command ) <<
"'." << std::endl;
146 KCmdLineArgs::usage();
156 allServices << KServiceTypeTrader::self()->query(
"NepomukService2" );
157 allServices << KServiceTypeTrader::self()->query(
"NepomukService" );
162 QString serviceName = ptr->desktopEntryName();
163 return QDBusConnection::sessionBus().interface()->isServiceRegistered(
164 QLatin1String(
"org.kde.nepomuk.services." ) + serviceName );
169 return QDBusConnection::sessionBus().interface()->isServiceRegistered(
170 QLatin1String(
"org.kde.NepomukServer" ) );
176 std::cerr <<
"Nepomuk Server already running." << std::endl;
177 else if( QProcess::startDetached(
"nepomukserver" ) )
178 std::cerr <<
"Nepomuk Server started successfully." << std::endl;
180 std::cerr <<
"Could not start the Nepomuk Server." << std::endl;
185 QDBusInterface interface(
"org.kde.NepomukServer",
"/nepomukserver" );
187 QDBusReply<void> reply = interface.call(
"quit" );
189 if( reply.isValid() )
190 std::cerr <<
"Nepomuk Server stopped succesfully." << std::endl;
192 std::cerr <<
"Couldn't stop the Nepomuk Server: "
193 << qPrintable( interface.lastError().message() )
200 std::cerr <<
"Nepomuk Server is not running. Starting it..."
207 std::cerr << qPrintable( ptr->desktopEntryName() )
208 <<
" already running." << std::endl;
211 QString program = ptr->exec();
214 if( program.isEmpty() ) {
215 program = QLatin1String(
"nepomukservicestub");
216 args << ptr->desktopEntryName();
219 if( QProcess::startDetached( program, args ) )
220 std::cerr << qPrintable( ptr->desktopEntryName() )
221 <<
" started successfully." << std::endl;
223 std::cerr << qPrintable( ptr->desktopEntryName() )
224 <<
" could not be started." << std::endl;
231 QString serviceName = ptr->desktopEntryName();
232 QDBusInterface interface(
"org.kde.nepomuk.services." + serviceName,
235 QDBusReply<void> reply = interface.call(
"shutdown" );
237 if( reply.isValid() )
238 std::cerr << qPrintable( serviceName )
239 <<
" could not be started." << std::endl;
241 std::cerr <<
"Couldn't stop Nepomuk " << qPrintable( serviceName )
242 <<
": " << qPrintable( interface.lastError().message() )
249 std::cerr <<
"Nepomuk Server is running." << std::endl;
253 QString name = ptr->desktopEntryName();
254 if( name.startsWith(QLatin1String(
"nepomuk")) ) {
255 name = name.mid( 7 );
257 std::cerr <<
"Service " << qPrintable( name ) <<
" is running."
263 std::cerr <<
"Nepomuk Server is not running." << std::endl;
int main(int argc, char *argv[])
void stopService(const KService::Ptr &ptr)
void startService(const KService::Ptr &ptr)
KService::List allServices
void loadAllServiceNames()
void startNepomukServer()
bool isServiceRunning(const KService::Ptr &ptr)