Use glibmm API for variants
All checks were successful
Build / build (push) Successful in 16s

This commit is contained in:
Eugen Wissner 2024-06-10 16:42:09 +02:00
parent ed6c87638f
commit 8d43caadcc
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 34 additions and 37 deletions

View File

@ -29,61 +29,59 @@
static void slack_method_call(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& sender, static void slack_method_call(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& sender,
const Glib::ustring& object_path, const Glib::ustring& interface_name, const Glib::ustring& method_name, const Glib::ustring& object_path, const Glib::ustring& interface_name, const Glib::ustring& method_name,
const Glib::VariantContainerBase& parameters, const Glib::RefPtr<Gio::DBus::MethodInvocation>& invocation) { const Glib::VariantContainerBase& parameters, const Glib::RefPtr<Gio::DBus::MethodInvocation>& invocation)
gchar *timezone; {
gboolean user_interaction, relative, is_localtime, use_ntp;
gint64 usec_utc;
auto parameters_copy = parameters.gobj_copy();
// Set time zone // Set time zone
if (method_name == "SetTimezone") if (method_name == "SetTimezone")
{ {
g_variant_get(parameters_copy, "(&sb)", &timezone, &user_interaction); Glib::Variant<Glib::ustring> timezone;
std::error_code error_code; std::error_code error_code;
if (dlackware::timedate::set_timezone(timezone, error_code)) parameters.get_child(timezone, 0);
if (dlackware::timedate::set_timezone(timezone.get(), error_code))
{ {
g_dbus_method_invocation_return_value(invocation->gobj(), NULL); invocation->return_value(Glib::VariantContainerBase());
} }
else else
{ {
g_dbus_method_invocation_return_error(invocation->gobj(), G_IO_ERROR, G_IO_ERROR_FAILED, Gio::DBus::Error error{ Gio::DBus::Error::FAILED,
"Set time zone: %s", error_code ? error_code.message().c_str() : "Zone info is not a regular file" };
error_code ? error_code.message().c_str() : "Zone info is not a regular file"); invocation->return_error(error);
} }
g_free (timezone);
} }
else if (method_name == "SetTime") else if (method_name == "SetTime")
{ {
g_variant_get(parameters_copy, "(xbb)", &usec_utc, &relative, &user_interaction); Glib::Variant<gint64> usec_utc;
Glib::Variant<bool> relative;
// Set time parameters.get_child(usec_utc, 0);
//if (!slack_set_time (usec_utc, slack_get_is_localtime ())) { parameters.get_child(relative, 1);
if (slack_set_time(usec_utc, relative))
if (slack_set_time(usec_utc.get(), relative.get()))
{ {
g_dbus_method_invocation_return_value(invocation->gobj(), NULL); invocation->return_value(Glib::VariantContainerBase());
} }
else else
{ {
g_dbus_method_invocation_return_error(invocation->gobj(), G_IO_ERROR, G_IO_ERROR_FAILED, Gio::DBus::Error error{ Gio::DBus::Error::FAILED, "Failed to set system clock" };
"Failed to set system clock"); invocation->return_error(error);
} }
} }
else if (method_name == "SetNTP") else if (method_name == "SetNTP")
{ {
g_variant_get(parameters_copy, "(bb)", &use_ntp, &user_interaction); Glib::Variant<bool> use_ntp;
parameters.get_child(use_ntp, 0);
// Enable NTP // Enable NTP
if (slack_set_ntp (use_ntp)) if (slack_set_ntp(use_ntp.get()))
{ {
g_dbus_method_invocation_return_value(invocation->gobj(), NULL); invocation->return_value(Glib::VariantContainerBase());
} }
else else
{ {
g_dbus_method_invocation_return_error(invocation->gobj(), Gio::DBus::Error error{ Gio::DBus::Error::FAILED, "Error enabling NTP" };
G_IO_ERROR, invocation->return_error(error);
G_IO_ERROR_FAILED,
"Error enabling NTP");
} }
} }
else if (method_name == "ListTimezones") else if (method_name == "ListTimezones")
@ -93,15 +91,14 @@ static void slack_method_call(const Glib::RefPtr<Gio::DBus::Connection>& connect
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() dlackware::timedate::list_timezones()
}); });
g_dbus_method_invocation_return_value(invocation->gobj(), return_tuple.gobj()); invocation->return_value(return_tuple);
} }
catch (const std::exception& exception) catch (const std::exception& exception)
{ {
g_dbus_method_invocation_return_error(invocation->gobj(), G_IO_ERROR, G_IO_ERROR_FAILED, Gio::DBus::Error error{ Gio::DBus::Error::FAILED, exception.what() };
"List time zones: %s", exception.what()); invocation->return_error(error);
} }
} }
g_free(parameters_copy);
} }
static void slack_get_property(Glib::VariantBase& result, const Glib::RefPtr<Gio::DBus::Connection>& connection, static void slack_get_property(Glib::VariantBase& result, const Glib::RefPtr<Gio::DBus::Connection>& connection,
@ -144,11 +141,11 @@ int main(int argc, char **argv)
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);
loop->run(); loop->run();
Gio::DBus::unown_name(owner_id); Gio::DBus::unown_name(owner_id);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
namespace dlackware::timedate namespace dlackware::timedate
@ -228,10 +225,10 @@ namespace dlackware::timedate
return Glib::ustring{ zone_copied_from.get() + strlen(zoneinfo_database) + 1 }; return Glib::ustring{ zone_copied_from.get() + strlen(zoneinfo_database) + 1 };
} }
bool set_timezone(gchar *zone, std::error_code& ec) bool set_timezone(const Glib::ustring& zone, std::error_code& ec)
{ {
ec.clear(); ec.clear();
auto zone_file = std::filesystem::path("/usr/share/zoneinfo") / zone; auto zone_file = std::filesystem::path("/usr/share/zoneinfo") / zone.data();
if (!std::filesystem::is_regular_file(zone_file, ec) || ec) if (!std::filesystem::is_regular_file(zone_file, ec) || ec)
{ {
@ -297,7 +294,7 @@ gboolean slack_get_is_localtime()
gboolean slack_get_ntp() gboolean slack_get_ntp()
{ {
return Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_EXECUTABLE); return Glib::file_test("/etc/rc.d/rc.ntpd", Glib::FileTest::FILE_TEST_IS_EXECUTABLE);
} }
gboolean slack_set_ntp(gboolean xntp) gboolean slack_set_ntp(gboolean xntp)

View File

@ -73,7 +73,7 @@ namespace dlackware::timedate
// 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
bool set_timezone(gchar *, std::error_code& ec); bool set_timezone(const Glib::ustring& zone, std::error_code& ec);
class timedate1 class timedate1
{ {