changeset 31:7ce29c196f36

misc: add libnotify support (optional)
author David Demelier <markand@malikania.fr>
date Fri, 27 Apr 2018 17:03:00 +0200
parents 23d4de57e583
children 404be85de083
files CMakeLists.txt INSTALL.md TODO.md dmenu-background/main.cpp dmenu-mpc/main.cpp dmenu-ssh/main.cpp libdmenutools/CMakeLists.txt libdmenutools/dmenu/dmenu.cpp libdmenutools/dmenu/dmenu.hpp libdmenutools/dmenu/sysconfig.hpp.in
diffstat 10 files changed, 126 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri Apr 27 12:14:44 2018 +0200
+++ b/CMakeLists.txt	Fri Apr 27 17:03:00 2018 +0200
@@ -33,7 +33,10 @@
 add_subdirectory(dmenu-ssh)
 
 message("")
-message("dmenutools configuration")
+message("dmenutools features")
+message("    libnotify:         ${WITH_NOTIFY_MSG}")
+message("")
+message("dmenutools applications")
 message("    dmenu-background:  ${WITH_BACKGROUND_MSG}")
 message("    dmenu-mpc:         ${WITH_MPC_MSG}")
 message("    dmenu-ssh:         ${WITH_SSH_MSG}")
--- a/INSTALL.md	Fri Apr 27 12:14:44 2018 +0200
+++ b/INSTALL.md	Fri Apr 27 17:03:00 2018 +0200
@@ -7,11 +7,16 @@
 ------------
 
   - Boost,
-  - CMake.
+  - CMake,
+  - libnotify (Optional, for error reporting).
 
 For `dmenu-background`:
 
-  - feh (by default, can be changed)
+  - feh (by default, can be changed).
+
+For `dmenu-mpc`:
+
+  - libmpdclient.
 
 Basic installation
 ------------------
@@ -23,3 +28,19 @@
     cmake .. -DCMAKE\_BUILD\_TYPE=Release
     make
     sudo make install
+
+Options
+-------
+
+The following options are available:
+
+  - `WITH_NOTIFY`, disable libnotify support.
+
+Applications
+------------
+
+The following options are available to disable applications:
+
+  - `WITH_BACKGROUND`, disable dmenu-background,
+  - `WITH_MPC`, disable dmenu-mpc,
+  - `WITH_SSH`, disable dmenu-ssh,
--- a/TODO.md	Fri Apr 27 12:14:44 2018 +0200
+++ b/TODO.md	Fri Apr 27 17:03:00 2018 +0200
@@ -6,9 +6,8 @@
 libdmenutools
 -------------
 
-  - Create convenient and powerful `dmenu::browse` API.
+  - Create convenient and powerful `dmenu::browse` API,
   - Create convenient `dmenu::exec` API.
-  - Add `dmenu::fatal` that creates a notification error.
 
 dmenu-mpc
 ---------
--- a/dmenu-background/main.cpp	Fri Apr 27 12:14:44 2018 +0200
+++ b/dmenu-background/main.cpp	Fri Apr 27 17:03:00 2018 +0200
@@ -135,7 +135,7 @@
         if (!selection.empty())
             apply(conf.command, selection);
     } catch (const std::exception& ex) {
-        std::cerr << "abort: " << ex.what() << std::endl;
+        dmenu::error("background", ex.what());
         return 1;
     }
 }
--- a/dmenu-mpc/main.cpp	Fri Apr 27 12:14:44 2018 +0200
+++ b/dmenu-mpc/main.cpp	Fri Apr 27 17:03:00 2018 +0200
@@ -328,7 +328,7 @@
                 clear(conn.get());
         }
     } catch (const std::exception& ex) {
-        std::cerr << "abort: " << ex.what() << std::endl;
+        dmenu::error("mpc", ex.what());
         return 1;
     }
 }
--- a/dmenu-ssh/main.cpp	Fri Apr 27 12:14:44 2018 +0200
+++ b/dmenu-ssh/main.cpp	Fri Apr 27 17:03:00 2018 +0200
@@ -96,7 +96,7 @@
         if (!selection.empty())
             exec(selection);
     } catch (const std::exception& ex) {
-        std::cerr << "abort: " << ex.what() << std::endl;
+        dmenu::error("ssh", ex.what());
         return 1;
     }
 }
--- a/libdmenutools/CMakeLists.txt	Fri Apr 27 12:14:44 2018 +0200
+++ b/libdmenutools/CMakeLists.txt	Fri Apr 27 17:03:00 2018 +0200
@@ -18,6 +18,19 @@
 
 project(libdmenutools)
 
+pkg_check_modules(libnotify IMPORTED_TARGET libnotify)
+
+option(WITH_NOTIFY "Enable libnotify support" On)
+
+if (NOT WITH_NOTIFY)
+    set(WITH_NOTIFY_MSG "No (disabled by user)" CACHE INTERNAL "" FORCE)
+elseif (NOT libnotify_FOUND)
+    set(WITH_NOTIFY_MSG "No (libnotify not found)" CACHE INTERNAL "" FORCE)
+else ()
+    set(WITH_NOTIFY_MSG "Yes" CACHE INTERNAL "" FORCE)
+    set(HAVE_NOTIFY On)
+endif ()
+
 set(
     SOURCES
     ${libdmenutools_SOURCE_DIR}/dmenu/dmenu.cpp
@@ -27,7 +40,25 @@
     ${libdmenutools_SOURCE_DIR}/dmenu/xdg.hpp
 )
 
+configure_file(
+    ${libdmenutools_SOURCE_DIR}/dmenu/sysconfig.hpp.in
+    ${libdmenutools_BINARY_DIR}/dmenu/sysconfig.hpp
+)
+
 add_library(libdmenutools STATIC ${SOURCES})
 set_target_properties(libdmenutools PROPERTIES PREFIX "")
-target_link_libraries(libdmenutools Boost::filesystem Boost::system)
-target_include_directories(libdmenutools PUBLIC ${libdmenutools_SOURCE_DIR})
+
+target_link_libraries(
+    libdmenutools
+    Boost::filesystem
+    Boost::system
+    $<$<BOOL:${HAVE_NOTIFY}>:PkgConfig::libnotify>
+)
+
+target_include_directories(
+    libdmenutools
+    PUBLIC
+        ${libdmenutools_SOURCE_DIR}
+    PRIVATE
+        ${libdmenutools_BINARY_DIR}/dmenu
+)
--- a/libdmenutools/dmenu/dmenu.cpp	Fri Apr 27 12:14:44 2018 +0200
+++ b/libdmenutools/dmenu/dmenu.cpp	Fri Apr 27 17:03:00 2018 +0200
@@ -16,12 +16,14 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <iostream>
 #include <sstream>
 
 #include <boost/filesystem.hpp>
 #include <boost/process.hpp>
 
 #include "dmenu.hpp"
+#include "sysconfig.hpp"
 #include "xdg.hpp"
 
 namespace fs = boost::filesystem;
@@ -82,4 +84,26 @@
     return ini::read_file(path).get(section);
 }
 
+void error(const std::string& summary, const std::string& message)
+{
+#if defined(HAVE_NOTIFY)
+    static bool is_initialized;
+
+    if (!is_initialized) {
+        notify_init("dmenu");
+        is_initialized = true;
+    }
+
+    NotifyNotification* n = notify_notification_new(summary.c_str(), message.c_str(), nullptr);
+    GError* error = nullptr;
+
+    if (!notify_notification_show(n, &error))
+        std::cerr << error->message << std::endl;
+
+    g_free(error);
+#else
+    std::cerr << summary << ": " << message << std::endl;
+#endif
+}
+
 } // !dmenu
--- a/libdmenutools/dmenu/dmenu.hpp	Fri Apr 27 12:14:44 2018 +0200
+++ b/libdmenutools/dmenu/dmenu.hpp	Fri Apr 27 17:03:00 2018 +0200
@@ -49,6 +49,16 @@
  */
 ini::section config(const std::string& section);
 
+/**
+ * Show a notification error.
+ *
+ * If notification support is disabled, error is printed to std::cerr.
+ *
+ * \param summary the summary
+ * \param message the message content
+ */
+void error(const std::string& summary, const std::string& message);
+
 } // !dmenu
 
 #endif // !DMENUTOOLS_DMENU_HPP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libdmenutools/dmenu/sysconfig.hpp.in	Fri Apr 27 17:03:00 2018 +0200
@@ -0,0 +1,28 @@
+/*
+ * sysconfig.hpp -- configuration file for dmenutools
+ *
+ * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef DMENUTOOLS_SYSCONFIG_HPP
+#define DMENUTOOLS_SYSCONFIG_HPP
+
+#cmakedefine HAVE_NOTIFY
+
+#if defined(HAVE_NOTIFY)
+#   include <libnotify/notify.h>
+#endif
+
+#endif // !DMENUTOOLS_SYSCONFIG_HPP