Take the timezone link from /etc/localtime
All checks were successful
Build / build (push) Successful in 18s

This commit is contained in:
Eugen Wissner 2024-06-04 14:56:11 +02:00
parent 263d91efb5
commit 9c95b1f864
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 32 additions and 22 deletions

View File

@ -31,6 +31,7 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
gchar *timezone, *response; gchar *timezone, *response;
gboolean user_interaction, relative, is_localtime, use_ntp; gboolean user_interaction, relative, is_localtime, use_ntp;
gint64 usec_utc; gint64 usec_utc;
auto timedate1 = reinterpret_cast<dlackware::timedate::timedate1 *>(user_data);
// Set time zone // Set time zone
if (g_strcmp0(method_name, "SetTimezone") == 0) if (g_strcmp0(method_name, "SetTimezone") == 0)
@ -88,7 +89,7 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
try try
{ {
auto return_tuple = Glib::Variant<std::vector<Glib::ustring>>::create_tuple({ auto return_tuple = Glib::Variant<std::vector<Glib::ustring>>::create_tuple({
dlackware::timedate::list_timezones() timedate1->list_timezones()
}); });
g_dbus_method_invocation_return_value(invocation, return_tuple.gobj()); 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) { 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<dlackware::timedate::timedate1 *>(user_data);
if (g_strcmp0 ("Timezone", prop_name) == 0) { if (g_strcmp0 ("Timezone", prop_name) == 0) {
return g_variant_new_string (slack_get_timezone ()); return Glib::Variant<Glib::ustring>::create(timedate1->timezone()).gobj_copy();
} if (g_strcmp0 ("LocalRTC", prop_name) == 0) { } if (g_strcmp0 ("LocalRTC", prop_name) == 0) {
return g_variant_new_boolean (slack_get_is_localtime ()); return g_variant_new_boolean (slack_get_is_localtime ());
} if (g_strcmp0 ("NTP", prop_name) == 0) { } if (g_strcmp0 ("NTP", prop_name) == 0) {
@ -131,7 +134,7 @@ static void on_timedate_acquired (GDBusConnection *connection, const gchar *name
BUS_PATH, BUS_PATH,
introspection_data->interfaces[0], introspection_data->interfaces[0],
&interface_vtable, &interface_vtable,
NULL, user_data,
NULL, NULL,
&slack_err); &slack_err);
@ -155,14 +158,15 @@ gboolean timeout_callback (Glib::RefPtr<Glib::MainLoop> loop2quit) {
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
auto timedate1 = new dlackware::timedate::timedate1();
guint owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, guint owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
BUS_NAME, BUS_NAME,
static_cast<GBusNameOwnerFlags>(G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE), static_cast<GBusNameOwnerFlags>(G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE),
on_timedate_acquired, on_timedate_acquired,
NULL, NULL,
on_timedate_lost, on_timedate_lost,
NULL, timedate1,
NULL); [](void *memory) { delete reinterpret_cast<dlackware::timedate::timedate1 *>(memory); });
Glib::RefPtr<Glib::MainLoop> loop = Glib::MainLoop::create(false); Glib::RefPtr<Glib::MainLoop> loop = Glib::MainLoop::create(false);
Glib::signal_timeout().connect_seconds(sigc::bind(&timeout_callback, loop), DEFAULT_EXIT_SEC); 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 namespace dlackware::timedate
{ {
void list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator) void timedate1::list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator)
{ {
constexpr Glib::FileTest is_directory = Glib::FILE_TEST_IS_DIR; constexpr Glib::FileTest is_directory = Glib::FILE_TEST_IS_DIR;
constexpr Glib::FileTest is_regular = Glib::FILE_TEST_IS_REGULAR; constexpr Glib::FileTest is_regular = Glib::FILE_TEST_IS_REGULAR;
@ -198,25 +202,25 @@ namespace dlackware::timedate
} }
} }
Glib::Variant<std::vector<Glib::ustring>> list_timezones() Glib::Variant<std::vector<Glib::ustring>> timedate1::list_timezones()
{ {
std::vector<Glib::ustring> result; std::vector<Glib::ustring> result;
list_timezones("", result); list_timezones("", result);
return Glib::Variant<std::vector<Glib::ustring>>::create(result); return Glib::Variant<std::vector<Glib::ustring>>::create(result);
} }
Glib::ustring timedate1::timezone()
{
std::unique_ptr<gchar[], decltype(&g_free)> zone_copied_from(
g_file_read_link("/etc/localtime", NULL), &g_free);
if (zone_copied_from == nullptr)
{
return nullptr;
}
return Glib::ustring{ zone_copied_from.get() + strlen(zoneinfo_database) + 1 };
} }
gchar *slack_get_timezone () {
gchar *zone_copied_from, *zone;
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;
} }
gboolean slack_set_timezone (gchar *zone) { gboolean slack_set_timezone (gchar *zone) {

View File

@ -63,12 +63,18 @@ namespace dlackware::timedate
{ {
constexpr const char *zoneinfo_database = "/usr/share/zoneinfo"; constexpr const char *zoneinfo_database = "/usr/share/zoneinfo";
class timedate1
{
void list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator); void list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator);
Glib::Variant<std::vector<Glib::ustring>> list_timezones();
}
// Returns the system time zone public:
gchar *slack_get_timezone (); // Returns the timezones available on the system.
Glib::Variant<std::vector<Glib::ustring>> list_timezones();
// Returns the system time zone.
Glib::ustring timezone();
};
}
// Sets the system time zone to the one passed by the argument // Sets the system time zone to the one passed by the argument
// Returns true on success, false otherwise // Returns true on success, false otherwise