From 9c95b1f8644943c52ed1b8361ddb1686d469ce2a Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 4 Jun 2024 14:56:11 +0200 Subject: [PATCH] Take the timezone link from /etc/localtime --- src/slack-timedate.cpp | 38 +++++++++++++++++++++----------------- src/slack-timedate.h | 16 +++++++++++----- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/slack-timedate.cpp b/src/slack-timedate.cpp index 5752f79..b054ade 100644 --- a/src/slack-timedate.cpp +++ b/src/slack-timedate.cpp @@ -31,6 +31,7 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender, gchar *timezone, *response; gboolean user_interaction, relative, is_localtime, use_ntp; gint64 usec_utc; + auto timedate1 = reinterpret_cast(user_data); // Set time zone if (g_strcmp0(method_name, "SetTimezone") == 0) @@ -88,7 +89,7 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender, try { auto return_tuple = Glib::Variant>::create_tuple({ - dlackware::timedate::list_timezones() + timedate1->list_timezones() }); g_dbus_method_invocation_return_value(invocation, return_tuple.gobj()); } @@ -101,8 +102,10 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender, } static GVariant *slack_get_property (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *prop_name, GError **slack_err, gpointer user_data) { + auto timedate1 = reinterpret_cast(user_data); + if (g_strcmp0 ("Timezone", prop_name) == 0) { - return g_variant_new_string (slack_get_timezone ()); + return Glib::Variant::create(timedate1->timezone()).gobj_copy(); } if (g_strcmp0 ("LocalRTC", prop_name) == 0) { return g_variant_new_boolean (slack_get_is_localtime ()); } if (g_strcmp0 ("NTP", prop_name) == 0) { @@ -131,7 +134,7 @@ static void on_timedate_acquired (GDBusConnection *connection, const gchar *name BUS_PATH, introspection_data->interfaces[0], &interface_vtable, - NULL, + user_data, NULL, &slack_err); @@ -155,14 +158,15 @@ gboolean timeout_callback (Glib::RefPtr loop2quit) { int main(int argc, char **argv) { + auto timedate1 = new dlackware::timedate::timedate1(); 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, NULL, on_timedate_lost, - NULL, - NULL); + timedate1, + [](void *memory) { delete reinterpret_cast(memory); }); Glib::RefPtr loop = Glib::MainLoop::create(false); Glib::signal_timeout().connect_seconds(sigc::bind(&timeout_callback, loop), DEFAULT_EXIT_SEC); @@ -175,7 +179,7 @@ int main(int argc, char **argv) namespace dlackware::timedate { - void list_timezones(const std::string& prefix, std::vector& accumulator) + void timedate1::list_timezones(const std::string& prefix, std::vector& accumulator) { constexpr Glib::FileTest is_directory = Glib::FILE_TEST_IS_DIR; constexpr Glib::FileTest is_regular = Glib::FILE_TEST_IS_REGULAR; @@ -198,25 +202,25 @@ namespace dlackware::timedate } } - Glib::Variant> list_timezones() + Glib::Variant> timedate1::list_timezones() { std::vector result; list_timezones("", result); return Glib::Variant>::create(result); } -} -gchar *slack_get_timezone () { - gchar *zone_copied_from, *zone; + Glib::ustring timedate1::timezone() + { + std::unique_ptr zone_copied_from( + g_file_read_link("/etc/localtime", NULL), &g_free); - zone_copied_from = g_file_read_link ("/etc/localtime-copied-from", NULL); - if (zone_copied_from == NULL) return NULL; - - zone = g_strdup (zone_copied_from + strlen ("/usr/share/zoneinfo/") * sizeof (gchar)); - g_free (zone_copied_from); - - return zone; + if (zone_copied_from == nullptr) + { + return nullptr; + } + return Glib::ustring{ zone_copied_from.get() + strlen(zoneinfo_database) + 1 }; + } } gboolean slack_set_timezone (gchar *zone) { diff --git a/src/slack-timedate.h b/src/slack-timedate.h index f833387..c80cbf2 100644 --- a/src/slack-timedate.h +++ b/src/slack-timedate.h @@ -63,12 +63,18 @@ namespace dlackware::timedate { constexpr const char *zoneinfo_database = "/usr/share/zoneinfo"; - void list_timezones(const std::string& prefix, std::vector& accumulator); - Glib::Variant> list_timezones(); -} + class timedate1 + { + void list_timezones(const std::string& prefix, std::vector& accumulator); -// Returns the system time zone -gchar *slack_get_timezone (); + public: + // Returns the timezones available on the system. + Glib::Variant> list_timezones(); + + // Returns the system time zone. + Glib::ustring timezone(); + }; +} // Sets the system time zone to the one passed by the argument // Returns true on success, false otherwise