diff options
-rw-r--r-- | CMakeLists.txt | 40 | ||||
-rw-r--r-- | src/CMakeLists.txt | 23 | ||||
-rw-r--r-- | src/libayatana-common.pc.in | 10 | ||||
-rw-r--r-- | src/utils.c | 167 | ||||
-rw-r--r-- | src/utils.h | 38 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 14 | ||||
-rw-r--r-- | tests/tst_utils.cpp | 74 |
7 files changed, 366 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bf34c7c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,40 @@ +project (libayatana-common C CXX) +cmake_minimum_required (VERSION 2.8.9) + +set(PROJECT_VERSION "0.8.1") +set(PACKAGE ${CMAKE_PROJECT_NAME}) + +# Options +option(DISABLE_TESTS "Disable tests" off) + +# GNU standard installation directories +include (GNUInstallDirs) +set (CMAKE_INSTALL_PKGLIBEXECDIR "${CMAKE_INSTALL_LIBEXECDIR}/${CMAKE_PROJECT_NAME}") +set (CMAKE_INSTALL_FULL_PKGLIBEXECDIR "${CMAKE_INSTALL_FULL_LIBEXECDIR}/${CMAKE_PROJECT_NAME}") + +# Check for prerequisites +find_package (PkgConfig REQUIRED) +include (FindPkgConfig) + +pkg_check_modules(GLIB REQUIRED + glib-2.0>=2.36 +) +include_directories (${GLIB_INCLUDE_DIRS}) + +pkg_check_modules( + URLDISPATCHER + url-dispatcher-1>=1 +) +include_directories(${URLDISPATCHER_INCLUDE_DIRS}) + +set(CC_WARNING_ARGS " -Wall -pedantic -Wextra -Wno-missing-field-initializers") + +add_subdirectory(src) + +if (DISABLE_TESTS) + message(STATUS "Tests disabled") +else() + include(CTest) + enable_testing() + add_subdirectory(tests) +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..5dce006 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,23 @@ + +add_library(ayatana-common SHARED + utils.c +) + +target_link_libraries(ayatana-common + ${GLIB_LIBRARIES} + ${URLDISPATCHER_LIBRARIES} +) + +if(URLDISPATCHER_FOUND) + add_definitions( -DHAS_URLDISPATCHER ) +endif() + +set(PUBLIC_HEADERS + utils.h +) + +configure_file(libayatana-common.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libayatana-common.pc @ONLY) + +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libayatana-common.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) +install(FILES ${PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ayatana/common) +install(TARGETS ayatana-common DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/src/libayatana-common.pc.in b/src/libayatana-common.pc.in new file mode 100644 index 0000000..2a1ac48 --- /dev/null +++ b/src/libayatana-common.pc.in @@ -0,0 +1,10 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include/ayatana + +Name: Ayatana common +Description: Ayatana common library +Version: @PROJECT_VERSION@ +Libs: -L${libdir} -layatana-common +Cflags: -I${includedir} diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..82f9cff --- /dev/null +++ b/src/utils.c @@ -0,0 +1,167 @@ +/* + * Copyright 2021 Marius Gripsgard <marius@ubports.com> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include "utils.h" + +#include <string.h> + +#ifdef HAS_URLDISPATCHER +# include <url-dispatcher.h> +#endif + +// TODO: make case insensitive +gboolean +is_xdg_current_desktop (const gchar* desktop) +{ + const gchar *xdg_current_desktop; + gchar **desktop_names; + int i; + + xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP"); + if (xdg_current_desktop != NULL) { + desktop_names = g_strsplit (xdg_current_desktop, ":", 0); + for (i = 0; desktop_names[i]; ++i) { + if (!g_strcmp0 (desktop_names[i], desktop)) { + g_strfreev (desktop_names); + return TRUE; + } + } + g_strfreev (desktop_names); + } + return FALSE; +} + +gboolean +is_lomiri () +{ + // For legacy reasons keep the MIR_SOCKET hack + return (g_getenv ("MIR_SOCKET") != NULL || + is_xdg_current_desktop(DESKTOP_LOMIRI)); +} + +gboolean +is_gnome () +{ + return is_xdg_current_desktop(DESKTOP_GNOME); +} + +gboolean +is_unity () +{ + return is_xdg_current_desktop(DESKTOP_UNITY); +} + +gboolean +is_mate () +{ + return is_xdg_current_desktop(DESKTOP_MATE); +} + +gboolean +is_xfce () +{ + return is_xdg_current_desktop(DESKTOP_XFCE); +} + +gboolean +is_pantheon () +{ + return is_xdg_current_desktop(DESKTOP_PANTHEON); +} + +// Bit of a hacky way? should use xdg open +char * +find_browser () +{ + static char * browser_path = NULL; + char* tmp_browser_path; + gchar **browser_names; + + int i; + + if (browser_path == NULL) + { + browser_names = g_strsplit ("x-www-browser,google-chrome,firefox,chromium", ",", 0); + + for (i = 0; browser_names[i]; ++i) { + tmp_browser_path = g_find_program_in_path (browser_names[i]); + + if (tmp_browser_path) { + browser_path = g_strdup (tmp_browser_path); + g_free (tmp_browser_path); + g_strfreev (browser_names); + break; + } + } + } + + return browser_path; +} + +gboolean +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); + return FALSE; + } + + return TRUE; +} + +gboolean +open_url (const gchar * url) +{ + char * browser = NULL; + + if (is_lomiri()) + { +#ifdef HAS_URLDISPATCHER + url_dispatch_send("settings:///system/battery", NULL, NULL); + return TRUE; +#else + g_warning("Built without url-dispatcher, is not able to open url"); +#endif + } + + if (browser == NULL) + browser = find_browser(); + + if (browser != NULL) + return execute_command(g_strdup_printf("%s '%s'", browser, url)); + else + return FALSE; + +} + +gboolean +have_program (const gchar * program) +{ + gchar *path; + gboolean have; + + path = g_find_program_in_path(program); + have = path != NULL; + g_free(path); + + return have; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..52e491f --- /dev/null +++ b/src/utils.h @@ -0,0 +1,38 @@ +/* + * Copyright 2021 Marius Gripsgard <marius@ubports.com> + * + * 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 <http://www.gnu.org/licenses/>. + */ + + +#pragma once + +#include <glib.h> + +#define DESKTOP_LOMIRI "Lomiri" +#define DESKTOP_UNITY "Unity" +#define DESKTOP_MATE "MATE" +#define DESKTOP_GNOME "GNOME" +#define DESKTOP_XFCE "XFCE" +#define DESKTOP_PANTHEON "PANTHEON" + +gboolean is_lomiri(); +gboolean is_unity(); +gboolean is_gnome(); +gboolean is_mate(); +gboolean is_xfce(); +gboolean is_pantheon(); + +gboolean execute_command(const gchar * cmd); +gboolean open_url(const gchar * url); +gboolean have_program(const gchar * program); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..d4c068c --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,14 @@ +find_package(GMock) + +include_directories( + ${CMAKE_SOURCE_DIR}/src +) + +add_executable(tst_utils tst_utils.cpp) +target_link_libraries(tst_utils + ayatana-common + + ${GTEST_LIBRARIES} + ${GTEST_BOTH_LIBRARIES} +) +add_test(TstUtils tst_utils) diff --git a/tests/tst_utils.cpp b/tests/tst_utils.cpp new file mode 100644 index 0000000..d7a4946 --- /dev/null +++ b/tests/tst_utils.cpp @@ -0,0 +1,74 @@ +/* + * Copyright 2021 Marius Gripsgard <marius@ubports.com> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <gtest/gtest.h> +#include <memory> + +extern "C" { + #include "utils.h" +} + +class XdgCurrentDesktopUtilsTest : public ::testing::Test +{ +public: + XdgCurrentDesktopUtilsTest() {} + + void SetUp() { + unsetenv("XDG_CURRENT_DESKTOP"); + } + + void TearDown() { + unsetenv("XDG_CURRENT_DESKTOP"); + } +}; + +TEST_F(XdgCurrentDesktopUtilsTest, isLomiri) +{ + EXPECT_FALSE(is_lomiri()); + setenv("XDG_CURRENT_DESKTOP", "Lomiri", 1); + EXPECT_TRUE(is_lomiri()); +} + +TEST_F(XdgCurrentDesktopUtilsTest, isGnome) +{ + EXPECT_FALSE(is_gnome()); + setenv("XDG_CURRENT_DESKTOP", "GNOME", 1); + EXPECT_TRUE(is_gnome()); +} +TEST_F(XdgCurrentDesktopUtilsTest, isUnity) +{ + EXPECT_FALSE(is_unity()); + setenv("XDG_CURRENT_DESKTOP", "Unity", 1); + EXPECT_TRUE(is_unity()); +} +TEST_F(XdgCurrentDesktopUtilsTest, isMate) +{ + EXPECT_FALSE(is_mate()); + setenv("XDG_CURRENT_DESKTOP", "MATE", 1); + EXPECT_TRUE(is_mate()); +} +TEST_F(XdgCurrentDesktopUtilsTest, isXfce) +{ + EXPECT_FALSE(is_xfce()); + setenv("XDG_CURRENT_DESKTOP", "XFCE", 1); + EXPECT_TRUE(is_xfce()); +} +TEST_F(XdgCurrentDesktopUtilsTest, isPantheon) +{ + EXPECT_FALSE(is_pantheon()); + setenv("XDG_CURRENT_DESKTOP", "PANTHEON", 1); + EXPECT_TRUE(is_pantheon()); +} |