From 33819d2eb9b09bde8b72027adb5476eae68e1c74 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 19 Apr 2024 10:14:53 +0200 Subject: [PATCH] Remove deprecated g_type_init --- CMakeLists.txt | 2 +- slack-timedate.cpp | 84 ++++++++++++++++++++++++---------------------- slack-timedate.h | 1 + 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cf19ec..304a366 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_CXX_STANDARD 17) find_package(PkgConfig) -pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1) +pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4) find_program(SED sed) add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service diff --git a/slack-timedate.cpp b/slack-timedate.cpp index 9c07ebe..607736f 100644 --- a/slack-timedate.cpp +++ b/slack-timedate.cpp @@ -16,6 +16,7 @@ * */ +#include #include #include #include @@ -117,18 +118,14 @@ static void on_timedate_lost (GDBusConnection *connection, const gchar *name, gp exit (1); } -gboolean timeout_callback (gpointer loop2quit) { - g_main_loop_quit ((GMainLoop *)loop2quit); - return FALSE; +gboolean timeout_callback (Glib::RefPtr loop2quit) { + loop2quit->quit(); + return false; } -int main (int argc, char **argv) { - guint owner_id; - GMainLoop *loop; - - g_type_init (); - - owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, +int main(int argc, char **argv) +{ + guint owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, BUS_NAME, static_cast(G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE), on_timedate_acquired, @@ -136,10 +133,10 @@ int main (int argc, char **argv) { on_timedate_lost, NULL, NULL); + Glib::RefPtr loop = Glib::MainLoop::create(false); - loop = g_main_loop_new (NULL, FALSE); - g_timeout_add_seconds (DEFAULT_EXIT_SEC, timeout_callback , loop); - g_main_loop_run (loop); + Glib::signal_timeout().connect_seconds(sigc::bind(&timeout_callback, loop), DEFAULT_EXIT_SEC); + loop->run(); g_bus_unown_name (owner_id); @@ -164,7 +161,8 @@ gboolean slack_set_timezone (gchar *zone) { GFile *etc_localtime, *localtime_link, *g_zone_file; zone_file = g_strconcat ("/usr/share/zoneinfo/", zone, NULL); - if (g_file_test (zone_file, G_FILE_TEST_IS_REGULAR)) { + if (Glib::file_test(zone_file, Glib::FILE_TEST_IS_REGULAR)) + { etc_localtime = g_file_new_for_path ("/etc/localtime"); localtime_link = g_file_new_for_path ("/etc/localtime-copied-from"); g_zone_file = g_file_new_for_path (zone_file); @@ -184,10 +182,10 @@ gboolean slack_set_timezone (gchar *zone) { g_object_unref (etc_localtime); g_object_unref (localtime_link); g_object_unref (g_zone_file); - } else { - return FALSE; + + return true; } - return TRUE; + return false; } gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative) @@ -237,32 +235,36 @@ gboolean slack_get_is_localtime () { return is_localtime; } -gboolean slack_get_ntp () { - if (g_file_test ("/etc/rc.d/rc.ntpd", G_FILE_TEST_IS_EXECUTABLE)) - return TRUE; - else return FALSE; +gboolean slack_get_ntp() +{ + return Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_EXECUTABLE); } -gboolean slack_set_ntp (gboolean xntp) { - mode_t rc_mode; +gboolean slack_set_ntp(gboolean xntp) +{ + std::filesystem::perms rc_mode = std::filesystem::perms::owner_read | std::filesystem::perms::owner_write + | std::filesystem::perms::group_read | std::filesystem::perms::others_read; - rc_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; + if (xntp) + { + rc_mode |= std::filesystem::perms::owner_exec + | std::filesystem::perms::group_exec | std::filesystem::perms::others_exec; + g_message("Please don't forget to configure the NTP daemon"); - if (xntp) { - rc_mode |= S_IXUSR | S_IXGRP | S_IXOTH; - g_message ("Please don't forget to configure the NTP daemon"); - - /* It doesn't matter if fails. - * The ntpdate command is considered obsolete, but "orthodox" ntpd -g - * will fail if your system clock is off for more than half an hour. */ - g_spawn_command_line_async ("/usr/sbin/ntpdate pool.ntp.org", NULL); - } - - if (g_file_test ("/etc/rc.d/rc.ntpd", G_FILE_TEST_IS_REGULAR)) { - if (chmod ("/etc/rc.d/rc.ntpd", rc_mode)) return FALSE; - else return TRUE; - } else { - g_error ("The NTP daemon isn't installed"); - return FALSE; - } + /* It doesn't matter if fails. + * The ntpdate command is considered obsolete, but "orthodox" ntpd -g + * will fail if your system clock is off for more than half an hour. */ + g_spawn_command_line_async("/usr/sbin/ntpdate pool.ntp.org", NULL); + } + if (Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_REGULAR)) + { + std::error_code ec; + std::filesystem::permissions("/etc/rc.d/rc.ntpd", rc_mode, ec); + return !ec; + } + else + { + g_error("The NTP daemon isn't installed"); + return false; + } } diff --git a/slack-timedate.h b/slack-timedate.h index 49f0209..7154336 100644 --- a/slack-timedate.h +++ b/slack-timedate.h @@ -17,6 +17,7 @@ */ #include +#include #include #define BUS_NAME "org.freedesktop.timedate1"