Changeset View
Changeset View
Standalone View
Standalone View
src/service/Application.cpp
Show First 20 Lines • Show All 284 Lines • ▼ Show 20 Line(s) | |||||
285 | } | 285 | } | ||
286 | 286 | | |||
287 | #include "Version.h" | 287 | #include "Version.h" | ||
288 | QString Application::serviceVersion() const | 288 | QString Application::serviceVersion() const | ||
289 | { | 289 | { | ||
290 | return KACTIVITIES_VERSION_STRING; | 290 | return KACTIVITIES_VERSION_STRING; | ||
291 | } | 291 | } | ||
292 | 292 | | |||
293 | // Leaving object oriented world :) | | |||
294 | | ||||
295 | namespace { | | |||
296 | template <typename Return> | | |||
297 | Return callOnRunningService(const QString &method) | | |||
298 | { | | |||
299 | static QDBusInterface remote(KAMD_DBUS_SERVICE, QStringLiteral("/ActivityManager"), | | |||
300 | QStringLiteral("org.kde.ActivityManager.Application")); | | |||
301 | QDBusReply<Return> reply = remote.call(method); | | |||
302 | | ||||
303 | return (Return)reply; | | |||
304 | } | | |||
305 | | ||||
306 | QString runningServiceVersion() | | |||
307 | { | | |||
308 | return callOnRunningService<QString>(QStringLiteral("serviceVersion")); | | |||
309 | } | | |||
310 | | ||||
311 | bool isServiceRunning() | | |||
312 | { | | |||
313 | return QDBusConnection::sessionBus().interface()->isServiceRegistered( | | |||
314 | KAMD_DBUS_SERVICE); | | |||
315 | } | | |||
316 | } | | |||
317 | | ||||
318 | int main(int argc, char **argv) | 293 | int main(int argc, char **argv) | ||
319 | { | 294 | { | ||
320 | // Disable session management for this process | 295 | // Disable session management for this process | ||
321 | qunsetenv("SESSION_MANAGER"); | 296 | qunsetenv("SESSION_MANAGER"); | ||
322 | 297 | | |||
323 | QGuiApplication::setDesktopSettingsAware(false); | 298 | QGuiApplication::setDesktopSettingsAware(false); | ||
324 | 299 | | |||
325 | Application application(argc, argv); | 300 | Application application(argc, argv); | ||
326 | application.setApplicationName(QStringLiteral("ActivityManager")); | 301 | application.setApplicationName(QStringLiteral("ActivityManager")); | ||
327 | application.setOrganizationDomain(QStringLiteral("kde.org")); | 302 | application.setOrganizationDomain(QStringLiteral("kde.org")); | ||
328 | | ||||
329 | // KAboutData about("kactivitymanagerd", nullptr, ki18n("KDE Activity Manager"), "3.0", | | |||
330 | // ki18n("KDE Activity Management Service"), | | |||
331 | // KAboutData::License_GPL, | | |||
332 | // ki18n("(c) 2010, 2011, 2012 Ivan Cukic"), KLocalizedString(), | | |||
333 | // "http://www.kde.org/"); | | |||
334 | | ||||
335 | // KCmdLineArgs::init(argc, argv, &about); | | |||
336 | | ||||
337 | const auto arguments = application.arguments(); | | |||
338 | | ||||
339 | if (arguments.size() == 0) { | | |||
340 | QCoreApplication::exit(EXIT_FAILURE); | | |||
341 | | ||||
342 | } else if (arguments.size() != 1 && (arguments.size() != 2 || arguments[1] == QLatin1String("--help"))) { | | |||
343 | | ||||
344 | QTextStream(stdout) | | |||
345 | << "start\tStarts the service\n" | | |||
346 | << "stop\tStops the server\n" | | |||
347 | << "status\tPrints basic server information\n" | | |||
348 | << "start-daemon\tStarts the service without forking (use with caution)\n" | | |||
349 | << "--help\tThis help message\n"; | | |||
350 | | ||||
351 | QCoreApplication::exit(EXIT_SUCCESS); | | |||
352 | | ||||
353 | } else if (arguments.size() == 1 || arguments[1] == QLatin1String("start")) { | | |||
354 | | ||||
355 | // Checking whether the service is already running | | |||
356 | if (isServiceRunning()) { | | |||
357 | QTextStream(stdout) << "Already running\n"; | | |||
358 | QCoreApplication::exit(EXIT_SUCCESS); | | |||
359 | } | | |||
360 | | ||||
361 | // Creating the watcher, but not on the wall | | |||
362 | | ||||
363 | QDBusServiceWatcher watcher(KAMD_DBUS_SERVICE, | | |||
364 | QDBusConnection::sessionBus(), | | |||
365 | QDBusServiceWatcher::WatchForRegistration); | | |||
366 | | ||||
367 | QObject::connect(&watcher, &QDBusServiceWatcher::serviceRegistered, | | |||
368 | [] (const QString &service) { | | |||
369 | QTextStream(stdout) | | |||
370 | << "Service started, version: " << runningServiceVersion() | | |||
371 | << "\n"; | | |||
372 | | ||||
373 | QCoreApplication::exit(EXIT_SUCCESS); | | |||
374 | }); | | |||
375 | | ||||
376 | // Starting the dameon | | |||
377 | | ||||
378 | QProcess::startDetached( | | |||
379 | QLatin1String(KAMD_FULL_BIN_DIR "/kactivitymanagerd"), | | |||
380 | QStringList{QStringLiteral("start-daemon")} | | |||
381 | ); | | |||
382 | | ||||
383 | return application.exec(); | | |||
384 | | ||||
385 | } else if (arguments[1] == QLatin1String("stop")) { | | |||
386 | if (!isServiceRunning()) { | | |||
387 | QTextStream(stdout) << "Service not running\n"; | | |||
388 | QCoreApplication::exit(EXIT_SUCCESS); | | |||
389 | } | | |||
390 | | ||||
391 | callOnRunningService<void>(QStringLiteral("quit")); | | |||
392 | | ||||
393 | QTextStream(stdout) << "Service stopped\n"; | | |||
394 | | ||||
395 | return EXIT_SUCCESS; | | |||
396 | | ||||
397 | } else if (arguments[1] == QLatin1String("status")) { | | |||
398 | | ||||
399 | // Checking whether the service is already running | | |||
400 | if (isServiceRunning()) { | | |||
401 | QTextStream(stdout) << "The service is running, version: " | | |||
402 | << runningServiceVersion() << "\n"; | | |||
403 | } else { | | |||
404 | QTextStream(stdout) << "The service is not running\n"; | | |||
405 | | ||||
406 | } | | |||
407 | | ||||
408 | return EXIT_SUCCESS; | | |||
409 | | ||||
410 | } else if (arguments[1] == QLatin1String("start-daemon")) { | | |||
411 | // Really starting the activity manager | | |||
412 | | ||||
413 | KDBusService service(KDBusService::Unique); | 303 | KDBusService service(KDBusService::Unique); | ||
414 | 304 | | |||
415 | application.init(); | 305 | application.init(); | ||
416 | 306 | | |||
417 | return application.exec(); | 307 | return application.exec(); | ||
418 | | ||||
419 | } else { | | |||
420 | QTextStream(stdout) << "Unrecognized command: " << arguments[1] << '\n'; | | |||
421 | | ||||
422 | return EXIT_FAILURE; | | |||
423 | } | | |||
424 | } | 308 | } | ||
425 | 309 | | |||
426 | QStringList Application::loadedPlugins() const | 310 | QStringList Application::loadedPlugins() const | ||
427 | { | 311 | { | ||
428 | return d->pluginIds; | 312 | return d->pluginIds; | ||
429 | } | 313 | } | ||
430 | 314 | |