diff options
Diffstat (limited to 'src/slack-timedate.cpp')
| -rw-r--r-- | src/slack-timedate.cpp | 76 |
1 files changed, 29 insertions, 47 deletions
diff --git a/src/slack-timedate.cpp b/src/slack-timedate.cpp index b054ade..45b8e50 100644 --- a/src/slack-timedate.cpp +++ b/src/slack-timedate.cpp @@ -36,18 +36,18 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender, // Set time zone if (g_strcmp0(method_name, "SetTimezone") == 0) { - g_variant_get (parameters, "(&sb)", &timezone, &user_interaction); + g_variant_get(parameters, "(&sb)", &timezone, &user_interaction); + std::error_code error_code; - if (slack_set_timezone(timezone)) + if (timedate1->set_timezone(timezone, error_code)) { - g_dbus_method_invocation_return_value (invocation, NULL); + g_dbus_method_invocation_return_value(invocation, NULL); } else { - g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_FAILED, - "Write operation failed"); + g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED, + "Set time zone: %s", + error_code ? error_code.message().c_str() : "Zone info is not a regular file"); } g_free (timezone); } @@ -181,23 +181,20 @@ namespace dlackware::timedate { 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_regular = Glib::FILE_TEST_IS_REGULAR; auto zoneinfo_path = std::filesystem::path(zoneinfo_database) / prefix; - auto zoneinfo_directory = Glib::Dir(zoneinfo_path); - for (auto zoneinfo_entry : zoneinfo_directory) + for (auto zoneinfo_entry : std::filesystem::directory_iterator(zoneinfo_path)) { - auto entry_path = zoneinfo_path / zoneinfo_entry; + auto new_prefix = prefix + zoneinfo_entry.path().filename().string(); - if (Glib::file_test(entry_path, is_directory)) + if (zoneinfo_entry.is_directory()) { - list_timezones(prefix + zoneinfo_entry + "/", accumulator); + list_timezones(new_prefix + "/", accumulator); } - else if (Glib::file_test(entry_path, is_regular) && !entry_path.has_extension() - && zoneinfo_entry != "leapseconds") + else if (zoneinfo_entry.is_regular_file() && !zoneinfo_entry.path().has_extension() + && zoneinfo_entry.path().filename() != "leapseconds") { - accumulator.push_back(prefix + zoneinfo_entry); + accumulator.emplace_back(new_prefix); } } } @@ -221,38 +218,23 @@ namespace dlackware::timedate } return Glib::ustring{ zone_copied_from.get() + strlen(zoneinfo_database) + 1 }; } -} -gboolean slack_set_timezone (gchar *zone) { - gchar *zone_file; - GFile *etc_localtime, *localtime_link, *g_zone_file; - - zone_file = g_strconcat ("/usr/share/zoneinfo/", zone, NULL); - if (Glib::file_test(zone_file, Glib::FILE_TEST_IS_REGULAR)) + bool timedate1::set_timezone(gchar *zone, std::error_code& ec) { - 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); - - if (!g_file_copy (g_zone_file, etc_localtime, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, NULL)) { - return FALSE; - } - - if (!g_file_delete (localtime_link, NULL, NULL)) { - return FALSE; - } - if (!g_file_make_symbolic_link (localtime_link, zone_file, NULL, NULL)) { - return FALSE; - } - - g_free (zone_file); - g_object_unref (etc_localtime); - g_object_unref (localtime_link); - g_object_unref (g_zone_file); - - return true; - } - return false; + ec.clear(); + auto zone_file = std::filesystem::path("/usr/share/zoneinfo") / zone; + + if (!std::filesystem::is_regular_file(zone_file, ec) || ec) + { + return false; + } + std::filesystem::path etc_localtime = "/etc/localtime"; + + std::filesystem::remove(etc_localtime); + std::filesystem::create_symlink(zone_file, etc_localtime, ec); + + return !ec; + } } gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative) |
