From 422443fc5c653bfd68aad90a2556a62f1edf1333 Mon Sep 17 00:00:00 2001 From: Mike Gabriel Date: Mon, 12 Mar 2018 14:34:44 +0100 Subject: Make URL dispatcher build-dependency optional and handle battery settings request appropriately. --- CMakeLists.txt | 10 +++++-- debian/control | 2 +- src/CMakeLists.txt | 9 ++++-- src/notifier.c | 5 ++-- src/service.c | 56 ++-------------------------------- src/utils.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 26 ++++++++++++++++ tests/CMakeLists.txt | 2 +- 8 files changed, 133 insertions(+), 62 deletions(-) create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index db80c6f..ac0cca6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,11 +41,17 @@ pkg_check_modules(SERVICE_DEPS REQUIRED glib-2.0>=2.36 gio-2.0>=2.36 gio-unix-2.0>=2.36 - libnotify>=0.7.6 - url-dispatcher-1>=1) + libnotify>=0.7.6) include_directories (SYSTEM ${SERVICE_DEPS_INCLUDE_DIRS}) +set(URL_DISPATCHER_1_REQUIRED_VERSION 1) +pkg_check_modules( + URLDISPATCHER + url-dispatcher-1>=${URL_DISPATCHER_1_REQUIRED_VERSION} +) +include_directories(${URLDISPATCHER_INCLUDE_DIRS}) + ## ## custom targets ## diff --git a/debian/control b/debian/control index d5725aa..cc4ba11 100644 --- a/debian/control +++ b/debian/control @@ -5,7 +5,7 @@ Maintainer: Mike Gabriel Build-Depends: cmake, libnotify-dev (>= 0.7.6), libglib2.0-dev (>= 2.36), - liburl-dispatcher1-dev, + liburl-dispatcher1-dev | hello, python:any, # for packaging debhelper (>= 9), diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2faedd6..d803f1e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,10 @@ set (SERVICE_EXEC "ayatana-indicator-power-service") add_definitions(-DG_LOG_DOMAIN="ayatana-indicator-power") +if(URLDISPATCHER_FOUND) + add_definitions( -DHAS_URLDISPATCHER ) +endif() + # handwritten sources set(SERVICE_MANUAL_SOURCES brightness.c @@ -12,7 +16,8 @@ set(SERVICE_MANUAL_SOURCES device.c notifier.c testing.c - service.c) + service.c + utils.c) # generated sources include(GdbusCodegen) @@ -51,5 +56,5 @@ link_directories(${SERVICE_DEPS_LIBRARY_DIRS}) # the executable: lib + main() add_executable (${SERVICE_EXEC} main.c) set_source_files_properties(${SERVICE_SOURCES} main.c PROPERTIES COMPILE_FLAGS "${C_WARNING_ARGS} -g -std=c99") -target_link_libraries (${SERVICE_EXEC} ${SERVICE_LIB} ${SERVICE_DEPS_LIBRARIES} ${GCOV_LIBS}) +target_link_libraries (${SERVICE_EXEC} ${SERVICE_LIB} ${SERVICE_DEPS_LIBRARIES} ${GCOV_LIBS} ${URLDISPATCHER_LIBRARIES}) install (TARGETS ${SERVICE_EXEC} RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_PKGLIBEXECDIR}) diff --git a/src/notifier.c b/src/notifier.c index f51032e..123ee6f 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -20,8 +20,7 @@ #include "dbus-battery.h" #include "dbus-shared.h" #include "notifier.h" - -#include +#include "utils.h" #include @@ -172,7 +171,7 @@ on_battery_settings_clicked(NotifyNotification * nn G_GNUC_UNUSED, char * action G_GNUC_UNUSED, gpointer user_data G_GNUC_UNUSED) { - url_dispatch_send("settings:///system/battery", NULL, NULL); + utils_handle_settings_request(); } static void diff --git a/src/service.c b/src/service.c index cf7d808..a1f6793 100644 --- a/src/service.c +++ b/src/service.c @@ -20,7 +20,6 @@ #include #include -#include #include "brightness.h" #include "dbus-shared.h" @@ -28,6 +27,7 @@ #include "device-provider.h" #include "notifier.h" #include "service.h" +#include "utils.h" #define BUS_NAME "org.ayatana.indicator.power" #define BUS_PATH "/org/ayatana/indicator/power" @@ -792,62 +792,12 @@ create_menu (IndicatorPowerService * self, int profile) **** GActions ***/ -/* Run a particular program based on an activation */ -static void -execute_command (const gchar * cmd) -{ - GError * err = NULL; - - g_debug ("Issuing command '%s'", cmd); - - if (!g_spawn_command_line_async (cmd, &err)) - { - g_warning ("Unable to start %s: %s", cmd, err->message); - g_error_free (err); - } -} - static void on_settings_activated (GSimpleAction * a G_GNUC_UNUSED, GVariant * param G_GNUC_UNUSED, gpointer gself G_GNUC_UNUSED) { - static const gchar *control_center_cmd = NULL; - - if (control_center_cmd == NULL) - { - if (g_getenv ("MIR_SOCKET") != NULL) - { - url_dispatch_send("settings:///system/battery", NULL, NULL); - return; - } - else if ((!g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "xubuntu")) || (!g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "xfce"))) - { - control_center_cmd = "xfce4-power-manager-settings"; - } - else if (!g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "mate")) - { - control_center_cmd = "mate-power-preferences"; - } - else if (!g_strcmp0 (g_getenv ("XDG_CURRENT_DESKTOP"), "Pantheon")) - { - control_center_cmd = "switchboard --open-plug system-pantheon-power"; - } - else - { - gchar *path; - - path = g_find_program_in_path ("unity-control-center"); - if (path != NULL) - control_center_cmd = "unity-control-center power"; - else - control_center_cmd = "gnome-control-center power"; - - g_free (path); - } - } - - execute_command (control_center_cmd); + utils_handle_settings_request(); } static void @@ -876,7 +826,7 @@ on_phone_settings_activated (GSimpleAction * a G_GNUC_UNUSED, GVariant * param G_GNUC_UNUSED, gpointer gself G_GNUC_UNUSED) { - url_dispatch_send("settings:///system/battery", NULL, NULL); + utils_handle_settings_request(); } /*** diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..6b2aeb3 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,85 @@ +/* + * Copyright 2013 Canonical Ltd. + * Copytight 2018 Mike Gabriel + * + * Authors (@Canonical): + * Charles Kerr + * Ted Gould + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "utils.h" + +#ifdef HAS_URLDISPATCHER +# include +#endif + +/* Run a particular program based on an activation */ +void +execute_command (const gchar * cmd) +{ + GError * err = NULL; + + g_debug ("Issuing command '%s'", cmd); + + if (!g_spawn_command_line_async (cmd, &err)) + { + g_warning ("Unable to start %s: %s", cmd, err->message); + g_error_free (err); + } +} + +void +utils_handle_settings_request (void) +{ + static const gchar *control_center_cmd = NULL; + + if (control_center_cmd == NULL) + { +#ifdef HAS_URLDISPATCHER + if (g_getenv ("MIR_SOCKET") != NULL) + { + url_dispatch_send("settings:///system/battery", NULL, NULL); + return; + } + else +#endif + if ((!g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "xubuntu")) || (!g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "xfce"))) + { + control_center_cmd = "xfce4-power-manager-settings"; + } + else if (!g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "mate")) + { + control_center_cmd = "mate-power-preferences"; + } + else if (!g_strcmp0 (g_getenv ("XDG_CURRENT_DESKTOP"), "Pantheon")) + { + control_center_cmd = "switchboard --open-plug system-pantheon-power"; + } + else + { + gchar *path; + + path = g_find_program_in_path ("unity-control-center"); + if (path != NULL) + control_center_cmd = "unity-control-center power"; + else + control_center_cmd = "gnome-control-center power"; + + g_free (path); + } + } + + execute_command (control_center_cmd); +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..744536c --- /dev/null +++ b/src/utils.h @@ -0,0 +1,26 @@ +/* + * Copytight 2018 Mike Gabriel + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __INDICATOR_POWER_UTILS_H__ +#define __INDICATOR_POWER_UTILS_H__ + +#include +#include + +void execute_command (const gchar * cmd); +void utils_handle_settings_request(void); + +#endif /* __INDICATOR_POWER_UTILS_H__ */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dc2dcab..bf53ee3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -49,7 +49,7 @@ function(add_test_by_name name) add_executable (${TEST_NAME} ${TEST_NAME}.cc) add_test (${TEST_NAME} ${TEST_NAME}) add_dependencies (${TEST_NAME} ayatanaindicatorpowerservice gschemas-compiled) - target_link_libraries (${TEST_NAME} ayatanaindicatorpowerservice gtest ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS}) + target_link_libraries (${TEST_NAME} ayatanaindicatorpowerservice gtest ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS} ${URLDISPATCHER_LIBRARIES}) endfunction() add_test_by_name(test-notify) add_test(NAME dear-reader-the-next-test-takes-80-seconds COMMAND true) -- cgit v1.2.3