From 34f968972060e830cda1e24bc46438aed243aeff Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Mon, 11 Aug 2014 22:04:11 +0200 Subject: [PATCH] added a command line interface --- src/CMakeLists.txt | 3 +- src/main.cpp | 13 ++++-- src/snore-send/main.cpp | 96 +++++++++++++++++++++++++++++++++++++++++ src/snorenotify.cpp | 3 -- 4 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 src/snore-send/main.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5903b92..56dd407 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/core) add_subdirectory(core) +add_subdirectory(snore-send) if(WITH_SNORE_DEAMON) @@ -10,8 +11,6 @@ if(WITH_SNORE_DEAMON) target_link_libraries( snorenotify KF5::CoreAddons KF5::I18n) endif() - add_dependencies(snorenotify libsnore) - install(TARGETS snorenotify RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) diff --git a/src/main.cpp b/src/main.cpp index 3069c16..16ca4e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,28 @@ #include "snorenotify.h" +#include "core/log.h" +#include "core/version.h" #include + + #ifdef HAVE_KDE5 -#include "core/version.h" #include #include #endif + + int main ( int argc, char *argv[] ) { - QApplication app ( argc, argv ); + app.setApplicationName("SnoreNotify"); + app.setOrganizationName("SnoreNotify"); + app.setApplicationVersion(Snore::Version::version()); #ifdef HAVE_KDE5 - KAboutData about("SnoreNotify",i18n("SnoreNotify"),Snore::Version::version(), + KAboutData about("SnoreNotify","SnoreNotify",Snore::Version::version(), i18n("A notification deamon."),KAboutLicense::LGPL_V3, i18n("Copyright (c) 2010-2014 Patrick von Reth ")); KAboutData::setApplicationData(about); diff --git a/src/snore-send/main.cpp b/src/snore-send/main.cpp new file mode 100644 index 0000000..8e90411 --- /dev/null +++ b/src/snore-send/main.cpp @@ -0,0 +1,96 @@ + +#include "core/snore.h" +#include "core/notification/notification.h" +#include "core/log.h" +#include "core/version.h" + +#include +#include + + +#ifdef HAVE_KDE5 +#include +#include +#endif + +#include + +using namespace Snore; + + +int main ( int argc, char *argv[] ) +{ + QApplication app ( argc, argv ); + app.setApplicationName("snore-send"); + app.setOrganizationName("snore-send"); + app.setApplicationVersion(Snore::Version::version()); + +#ifdef HAVE_KDE5 + KAboutData about("snore-send", "snore-send",Snore::Version::version(), + i18n("A command line interface for Snorenotify."),KAboutLicense::LGPL_V3, i18n("Copyright (c) 2014 Patrick von Reth ")); + + KAboutData::setApplicationData(about); +#endif + + QCommandLineParser parser; + parser.setApplicationDescription("A command line interface for Snorenotify."); + parser.addHelpOption(); + parser.addVersionOption(); + + QCommandLineOption title(QStringList() << "t" << "title", "Set the notification title.", "title"); + parser.addOption(title); + + QCommandLineOption message(QStringList() << "m" << "message", "Set the notification body.", "message"); + parser.addOption(message); + + QCommandLineOption applicationName(QStringList() << "a" << "application", "Set the notification applicattion.", "application", app.applicationName()); + parser.addOption(applicationName); + + + QCommandLineOption alertName(QStringList() << "c" << "alert", "Set the notification alert class.","alert", "Default"); + parser.addOption(alertName); + + QCommandLineOption iconPath(QStringList() << "i" << "icon", "Set the notification icon.","icon", ":/root/snore.png"); + parser.addOption(iconPath); + + QCommandLineOption backend(QStringList() << "b" << "backend", "Set the notification backend.","backend"); + parser.addOption(backend); + + + parser.process(app); + snoreDebug( SNORE_DEBUG ) << parser.positionalArguments(); + if(parser.isSet(title) && parser.isSet(message)) + { + SnoreCore core; + + core.loadPlugins(SnorePlugin::BACKEND); + if(parser.isSet(backend)?!core.setPrimaryNotificationBackend(parser.value(backend)):!core.setPrimaryNotificationBackend()) + { + std::cout << "Failed to set backend: " << qPrintable(parser.value(backend)) << " avalible backends: " << qPrintable(core.notificationBackends().join(", ")) << std::endl; + return 1; + } + Icon icon(parser.value(iconPath)); + Application application(parser.value(applicationName),icon); + Alert alert(parser.value(alertName),icon); + application.addAlert(alert); + + core.registerApplication(application); + + Notification n(application, alert, parser.value(title), parser.value(message),icon); + + core.broadcastNotification(n); + int returnCode = -1; + app.connect(&core, &SnoreCore::notificationClosed, [&returnCode](Notification noti){ + QString reason; + QDebug(&reason) << noti.closeReason(); + std::cout << qPrintable(reason) << std::endl; + returnCode = noti.closeReason(); + }); + app.connect(&core, &SnoreCore::notificationClosed, &app, &QApplication::quit); + app.exec(); + return returnCode; + } + parser.showHelp(1); +} + + diff --git a/src/snorenotify.cpp b/src/snorenotify.cpp index 3f6ec54..26e0a6d 100644 --- a/src/snorenotify.cpp +++ b/src/snorenotify.cpp @@ -29,7 +29,6 @@ #include #include - #include #include @@ -38,8 +37,6 @@ using namespace Snore; SnoreNotify::SnoreNotify(): m_settings("SnoreNotify","SnoreNotify") { - qApp->setApplicationName("SnoreNotify"); - qApp->setOrganizationName("SnoreNotify"); m_trayIcon = new TrayIcon(); m_snore = new SnoreCore(m_trayIcon->trayIcon()); m_snore->loadPlugins(SnorePlugin::ALL);