Compare commits

..

No commits in common. "ed6c87638f68331fece92aef4646ee6544eb6d41" and "9d20748c5c49e11ec02379b4f35c4a55dabe8399" have entirely different histories.

3 changed files with 110 additions and 126 deletions

View File

@ -1,5 +1,5 @@
find_package(PkgConfig) find_package(PkgConfig)
pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4 giomm-2.4) pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4)
add_executable(slack-timedate add_executable(slack-timedate
slack-timedate.cpp slack-timedate.h slack-timedate.cpp slack-timedate.h

View File

@ -27,168 +27,159 @@
#include "slack-timedate.h" #include "slack-timedate.h"
static void slack_method_call(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& sender, static void slack_method_call (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) {
const Glib::ustring& object_path, const Glib::ustring& interface_name, const Glib::ustring& method_name, gchar *timezone, *response;
const Glib::VariantContainerBase& parameters, const Glib::RefPtr<Gio::DBus::MethodInvocation>& invocation) { gboolean user_interaction, relative, is_localtime, use_ntp;
gchar *timezone; gint64 usec_utc;
gboolean user_interaction, relative, is_localtime, use_ntp; auto timedate1 = reinterpret_cast<dlackware::timedate::timedate1 *>(user_data);
gint64 usec_utc;
auto parameters_copy = parameters.gobj_copy();
// Set time zone // Set time zone
if (method_name == "SetTimezone") if (g_strcmp0(method_name, "SetTimezone") == 0)
{ {
g_variant_get(parameters_copy, "(&sb)", &timezone, &user_interaction); g_variant_get(parameters, "(&sb)", &timezone, &user_interaction);
std::error_code error_code; std::error_code error_code;
if (dlackware::timedate::set_timezone(timezone, error_code)) if (timedate1->set_timezone(timezone, error_code))
{ {
g_dbus_method_invocation_return_value(invocation->gobj(), NULL); g_dbus_method_invocation_return_value(invocation, NULL);
} }
else else
{ {
g_dbus_method_invocation_return_error(invocation->gobj(), G_IO_ERROR, G_IO_ERROR_FAILED, g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED,
"Set time zone: %s", "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");
} }
g_free (timezone); g_free (timezone);
} }
else if (method_name == "SetTime") else if (g_strcmp0(method_name, "SetTime") == 0)
{ {
g_variant_get(parameters_copy, "(xbb)", &usec_utc, &relative, &user_interaction); g_variant_get (parameters, "(xbb)", &usec_utc, &relative, &user_interaction);
// Set time // Set time
//if (!slack_set_time (usec_utc, slack_get_is_localtime ())) { //if (!slack_set_time (usec_utc, slack_get_is_localtime ())) {
if (slack_set_time(usec_utc, relative)) if (slack_set_time(usec_utc, relative))
{ {
g_dbus_method_invocation_return_value(invocation->gobj(), NULL); g_dbus_method_invocation_return_value(invocation, NULL);
} }
else else
{ {
g_dbus_method_invocation_return_error(invocation->gobj(), G_IO_ERROR, G_IO_ERROR_FAILED, g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to set system clock"); "Failed to set system clock");
} }
} }
else if (method_name == "SetNTP") else if (g_strcmp0(method_name, "SetNTP") == 0)
{ {
g_variant_get(parameters_copy, "(bb)", &use_ntp, &user_interaction); g_variant_get (parameters, "(bb)", &use_ntp, &user_interaction);
// Enable NTP // Enable NTP
if (slack_set_ntp (use_ntp)) if (slack_set_ntp (use_ntp))
{ {
g_dbus_method_invocation_return_value(invocation->gobj(), NULL); g_dbus_method_invocation_return_value (invocation, NULL);
} }
else else
{ {
g_dbus_method_invocation_return_error(invocation->gobj(), g_dbus_method_invocation_return_error (invocation,
G_IO_ERROR, G_IO_ERROR,
G_IO_ERROR_FAILED, G_IO_ERROR_FAILED,
"Error enabling NTP"); "Error enabling NTP");
} }
} }
else if (method_name == "ListTimezones") else if (g_strcmp0(method_name, "ListTimezones") == 0)
{ {
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->gobj(), return_tuple.gobj()); g_dbus_method_invocation_return_value(invocation, return_tuple.gobj());
} }
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, g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED,
"List time zones: %s", exception.what()); "List time zones: %s", exception.what());
} }
} }
g_free(parameters_copy);
} }
static void slack_get_property(Glib::VariantBase& result, const Glib::RefPtr<Gio::DBus::Connection>& connection, 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) {
const Glib::ustring& sender, const Glib::ustring& object_path, const Glib::ustring& interface_name, auto timedate1 = reinterpret_cast<dlackware::timedate::timedate1 *>(user_data);
const Glib::ustring& prop_name)
{ if (g_strcmp0 ("Timezone", prop_name) == 0) {
if (prop_name == "Timezone") return Glib::Variant<Glib::ustring>::create(timedate1->timezone()).gobj_copy();
{ } if (g_strcmp0 ("LocalRTC", prop_name) == 0) {
result = Glib::Variant<Glib::ustring>::create(dlackware::timedate::timezone()); return g_variant_new_boolean (slack_get_is_localtime ());
} } if (g_strcmp0 ("NTP", prop_name) == 0) {
else if (prop_name == "LocalRTC") return g_variant_new_boolean (slack_get_ntp ());
{ }
result = Glib::Variant<bool>::create(slack_get_is_localtime()); return NULL;
}
else if (prop_name == "NTP")
{
result = Glib::Variant<bool>::create(slack_get_ntp());
}
} }
gboolean timeout_callback(Glib::RefPtr<Glib::MainLoop> loop2quit) static void on_timedate_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) {
{ guint registration_id;
loop2quit->quit(); GDBusNodeInfo *introspection_data;
return false; GError *slack_err;
const GDBusInterfaceVTable interface_vtable = {
slack_method_call,
slack_get_property,
NULL
};
slack_err = NULL;
introspection_data = g_dbus_node_info_new_for_xml (INTROSPECTION_XML, &slack_err);
if (introspection_data == NULL)
g_error ("Failed to parse D-Bus introspection XML: %s\n", slack_err->message);
slack_err = NULL;
registration_id = g_dbus_connection_register_object (connection,
BUS_PATH,
introspection_data->interfaces[0],
&interface_vtable,
user_data,
NULL,
&slack_err);
if (registration_id <= 0) {
g_critical ("Failed to register callbacks for the exported object with the D-Bus interface: %s\n", slack_err->message);
g_error_free (slack_err);
}
g_dbus_node_info_unref (introspection_data);
}
static void on_timedate_lost (GDBusConnection *connection, const gchar *name, gpointer user_data) {
g_warning ("Failed to acquire the service %s.\n", name);
exit (1);
}
gboolean timeout_callback (Glib::RefPtr<Glib::MainLoop> loop2quit) {
loop2quit->quit();
return false;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Gio::init(); auto timedate1 = new dlackware::timedate::timedate1();
auto timedate1 = std::make_unique<dlackware::timedate::timedate1>(); guint owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
BUS_NAME,
guint owner_id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SYSTEM, BUS_NAME, static_cast<GBusNameOwnerFlags>(G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE),
std::bind(&dlackware::timedate::timedate1::on_bus_acquired, timedate1.get(), on_timedate_acquired,
std::placeholders::_1, std::placeholders::_2), NULL,
Gio::DBus::SlotNameAcquired(), on_timedate_lost,
std::bind(&dlackware::timedate::timedate1::on_name_lost, timedate1.get(), timedate1,
std::placeholders::_1, std::placeholders::_2), [](void *memory) { delete reinterpret_cast<dlackware::timedate::timedate1 *>(memory); });
Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE);
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); g_bus_unown_name (owner_id);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
namespace dlackware::timedate namespace dlackware::timedate
{ {
timedate1::timedate1() void timedate1::list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator)
: interface_vtable{ &slack_method_call, &slack_get_property }
{
}
void timedate1::on_name_lost(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& name)
{
g_warning("Failed to acquire the service %s.\n", name.data());
exit(1);
}
void timedate1::on_bus_acquired(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& name)
{
Glib::RefPtr<Gio::DBus::NodeInfo> introspection_data;
try
{
introspection_data = Gio::DBus::NodeInfo::create_for_xml(INTROSPECTION_XML);
}
catch (Glib::Error& slack_err)
{
g_error("Failed to parse D-Bus introspection XML: %s\n", slack_err.what().data());
}
try
{
guint registration_id = connection->register_object(BUS_PATH,
introspection_data->lookup_interface(), this->interface_vtable);
}
catch (Glib::Error& slack_err)
{
g_critical("Failed to register callbacks for the exported object with the D-Bus interface: %s\n",
slack_err.what().data());
}
}
void list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator)
{ {
auto zoneinfo_path = std::filesystem::path(zoneinfo_database) / prefix; auto zoneinfo_path = std::filesystem::path(zoneinfo_database) / prefix;
@ -208,7 +199,7 @@ 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;
@ -216,7 +207,7 @@ namespace dlackware::timedate
return Glib::Variant<std::vector<Glib::ustring>>::create(result); return Glib::Variant<std::vector<Glib::ustring>>::create(result);
} }
Glib::ustring timezone() Glib::ustring timedate1::timezone()
{ {
std::unique_ptr<gchar[], decltype(&g_free)> zone_copied_from( std::unique_ptr<gchar[], decltype(&g_free)> zone_copied_from(
g_file_read_link("/etc/localtime", NULL), &g_free); g_file_read_link("/etc/localtime", NULL), &g_free);
@ -228,7 +219,7 @@ 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 timedate1::set_timezone(gchar *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;

View File

@ -18,7 +18,6 @@
#include <dbus/dbus.h> #include <dbus/dbus.h>
#include <glibmm.h> #include <glibmm.h>
#include <giomm.h>
#include <gio/gio.h> #include <gio/gio.h>
#define BUS_NAME "org.freedesktop.timedate1" #define BUS_NAME "org.freedesktop.timedate1"
@ -64,26 +63,20 @@ namespace dlackware::timedate
{ {
constexpr const char *zoneinfo_database = "/usr/share/zoneinfo"; constexpr const char *zoneinfo_database = "/usr/share/zoneinfo";
// Returns the timezones available on the system.
Glib::Variant<std::vector<Glib::ustring>> list_timezones();
void list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator);
// 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
bool set_timezone(gchar *, std::error_code& ec);
class timedate1 class timedate1
{ {
const Gio::DBus::InterfaceVTable interface_vtable; void list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator);
public: public:
timedate1(); // Returns the timezones available on the system.
Glib::Variant<std::vector<Glib::ustring>> list_timezones();
void on_bus_acquired(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& name); // Returns the system time zone.
void on_name_lost(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& name); Glib::ustring timezone();
// Sets the system time zone to the one passed by the argument
// Returns true on success, false otherwise
bool set_timezone(gchar *, std::error_code& ec);
}; };
} }