|
8 | 8 |
|
9 | 9 | namespace accounts { |
10 | 10 |
|
11 | | -namespace { |
12 | | - |
13 | | -std::optional<GVariant *> dbusUserProperty(GDBusConnection *connection, |
14 | | - const char *userPath, |
15 | | - const char *propertyName) { |
16 | | - GError *error = nullptr; |
17 | | - GVariant *result = g_dbus_connection_call_sync( |
18 | | - connection, "org.freedesktop.Accounts", userPath, |
19 | | - "org.freedesktop.DBus.Properties", "Get", |
20 | | - g_variant_new("(ss)", "org.freedesktop.Accounts.User", propertyName), |
21 | | - G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE, 5000, nullptr, &error); |
22 | | - if (result == nullptr) { |
23 | | - if (error != nullptr) { |
24 | | - g_error_free(error); |
| 11 | + namespace { |
| 12 | + |
| 13 | + std::optional<GVariant*> |
| 14 | + dbusUserProperty(GDBusConnection* connection, const char* userPath, const char* propertyName) { |
| 15 | + GError* error = nullptr; |
| 16 | + GVariant* result = g_dbus_connection_call_sync( |
| 17 | + connection, "org.freedesktop.Accounts", userPath, "org.freedesktop.DBus.Properties", "Get", |
| 18 | + g_variant_new("(ss)", "org.freedesktop.Accounts.User", propertyName), G_VARIANT_TYPE("(v)"), |
| 19 | + G_DBUS_CALL_FLAGS_NONE, 5000, nullptr, &error |
| 20 | + ); |
| 21 | + if (result == nullptr) { |
| 22 | + if (error != nullptr) { |
| 23 | + g_error_free(error); |
| 24 | + } |
| 25 | + return std::nullopt; |
| 26 | + } |
| 27 | + |
| 28 | + GVariant* wrapped = nullptr; |
| 29 | + g_variant_get(result, "(v)", &wrapped); |
| 30 | + g_variant_unref(result); |
| 31 | + return wrapped; |
25 | 32 | } |
26 | | - return std::nullopt; |
27 | | - } |
28 | 33 |
|
29 | | - GVariant *wrapped = nullptr; |
30 | | - g_variant_get(result, "(v)", &wrapped); |
31 | | - g_variant_unref(result); |
32 | | - return wrapped; |
33 | | -} |
34 | | - |
35 | | -std::optional<std::string> dbusUserStringProperty(GDBusConnection *connection, |
36 | | - const char *userPath, |
37 | | - const char *propertyName) { |
38 | | - const auto property = dbusUserProperty(connection, userPath, propertyName); |
39 | | - if (!property.has_value()) { |
40 | | - return std::nullopt; |
41 | | - } |
| 34 | + std::optional<std::string> |
| 35 | + dbusUserStringProperty(GDBusConnection* connection, const char* userPath, const char* propertyName) { |
| 36 | + const auto property = dbusUserProperty(connection, userPath, propertyName); |
| 37 | + if (!property.has_value()) { |
| 38 | + return std::nullopt; |
| 39 | + } |
| 40 | + |
| 41 | + std::optional<std::string> value; |
| 42 | + if (g_variant_is_of_type(*property, G_VARIANT_TYPE_STRING)) { |
| 43 | + const char* text = g_variant_get_string(*property, nullptr); |
| 44 | + if (text != nullptr && text[0] != '\0') { |
| 45 | + value = text; |
| 46 | + } |
| 47 | + } |
| 48 | + g_variant_unref(*property); |
| 49 | + return value; |
| 50 | + } |
42 | 51 |
|
43 | | - std::optional<std::string> value; |
44 | | - if (g_variant_is_of_type(*property, G_VARIANT_TYPE_STRING)) { |
45 | | - const char *text = g_variant_get_string(*property, nullptr); |
46 | | - if (text != nullptr && text[0] != '\0') { |
47 | | - value = text; |
| 52 | + std::optional<std::uint64_t> |
| 53 | + dbusUserUint64Property(GDBusConnection* connection, const char* userPath, const char* propertyName) { |
| 54 | + const auto property = dbusUserProperty(connection, userPath, propertyName); |
| 55 | + if (!property.has_value()) { |
| 56 | + return std::nullopt; |
| 57 | + } |
| 58 | + |
| 59 | + std::optional<std::uint64_t> value; |
| 60 | + if (g_variant_is_of_type(*property, G_VARIANT_TYPE_UINT64)) { |
| 61 | + value = g_variant_get_uint64(*property); |
| 62 | + } |
| 63 | + g_variant_unref(*property); |
| 64 | + return value; |
48 | 65 | } |
49 | | - } |
50 | | - g_variant_unref(*property); |
51 | | - return value; |
52 | | -} |
53 | | - |
54 | | -std::optional<std::uint64_t> dbusUserUint64Property(GDBusConnection *connection, |
55 | | - const char *userPath, |
56 | | - const char *propertyName) { |
57 | | - const auto property = dbusUserProperty(connection, userPath, propertyName); |
58 | | - if (!property.has_value()) { |
59 | | - return std::nullopt; |
60 | | - } |
61 | 66 |
|
62 | | - std::optional<std::uint64_t> value; |
63 | | - if (g_variant_is_of_type(*property, G_VARIANT_TYPE_UINT64)) { |
64 | | - value = g_variant_get_uint64(*property); |
65 | | - } |
66 | | - g_variant_unref(*property); |
67 | | - return value; |
68 | | -} |
69 | | - |
70 | | -[[nodiscard]] bool iconPathReadable(const std::string &path) { |
71 | | - std::error_code ec; |
72 | | - return !path.empty() && std::filesystem::is_regular_file(path, ec) && !ec; |
73 | | -} |
74 | | - |
75 | | -} // namespace |
76 | | - |
77 | | -std::optional<std::string> iconFileForUid(const uid_t uid) { |
78 | | - GError *error = nullptr; |
79 | | - GDBusConnection *connection = |
80 | | - g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error); |
81 | | - if (connection == nullptr) { |
82 | | - if (error != nullptr) { |
83 | | - g_error_free(error); |
| 67 | + [[nodiscard]] bool iconPathReadable(const std::string& path) { |
| 68 | + std::error_code ec; |
| 69 | + return !path.empty() && std::filesystem::is_regular_file(path, ec) && !ec; |
84 | 70 | } |
85 | | - return std::nullopt; |
86 | | - } |
87 | 71 |
|
88 | | - GVariant *result = g_dbus_connection_call_sync( |
89 | | - connection, "org.freedesktop.Accounts", "/org/freedesktop/Accounts", |
90 | | - "org.freedesktop.Accounts", "FindUserById", |
91 | | - g_variant_new("(x)", static_cast<gint64>(uid)), G_VARIANT_TYPE("(o)"), |
92 | | - G_DBUS_CALL_FLAGS_NONE, 5000, nullptr, &error); |
93 | | - if (result == nullptr) { |
94 | | - if (error != nullptr) { |
95 | | - g_error_free(error); |
| 72 | + } // namespace |
| 73 | + |
| 74 | + std::optional<std::string> iconFileForUid(const uid_t uid) { |
| 75 | + GError* error = nullptr; |
| 76 | + GDBusConnection* connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error); |
| 77 | + if (connection == nullptr) { |
| 78 | + if (error != nullptr) { |
| 79 | + g_error_free(error); |
| 80 | + } |
| 81 | + return std::nullopt; |
96 | 82 | } |
97 | | - g_object_unref(connection); |
98 | | - return std::nullopt; |
99 | | - } |
100 | 83 |
|
101 | | - const char *userPath = nullptr; |
102 | | - g_variant_get(result, "(&o)", &userPath); |
103 | | - const std::string userObjectPath = userPath != nullptr ? userPath : ""; |
104 | | - g_variant_unref(result); |
105 | | - if (userObjectPath.empty()) { |
106 | | - g_object_unref(connection); |
107 | | - return std::nullopt; |
108 | | - } |
| 84 | + GVariant* result = g_dbus_connection_call_sync( |
| 85 | + connection, "org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts", "FindUserById", |
| 86 | + g_variant_new("(x)", static_cast<gint64>(uid)), G_VARIANT_TYPE("(o)"), G_DBUS_CALL_FLAGS_NONE, 5000, nullptr, |
| 87 | + &error |
| 88 | + ); |
| 89 | + if (result == nullptr) { |
| 90 | + if (error != nullptr) { |
| 91 | + g_error_free(error); |
| 92 | + } |
| 93 | + g_object_unref(connection); |
| 94 | + return std::nullopt; |
| 95 | + } |
109 | 96 |
|
110 | | - const std::optional<std::uint64_t> reportedUid = |
111 | | - dbusUserUint64Property(connection, userObjectPath.c_str(), "Uid"); |
112 | | - if (!reportedUid.has_value() || static_cast<uid_t>(*reportedUid) != uid) { |
113 | | - g_object_unref(connection); |
114 | | - return std::nullopt; |
115 | | - } |
| 97 | + const char* userPath = nullptr; |
| 98 | + g_variant_get(result, "(&o)", &userPath); |
| 99 | + const std::string userObjectPath = userPath != nullptr ? userPath : ""; |
| 100 | + g_variant_unref(result); |
| 101 | + if (userObjectPath.empty()) { |
| 102 | + g_object_unref(connection); |
| 103 | + return std::nullopt; |
| 104 | + } |
116 | 105 |
|
117 | | - const std::optional<std::string> iconFile = |
118 | | - dbusUserStringProperty(connection, userObjectPath.c_str(), "IconFile"); |
119 | | - g_object_unref(connection); |
| 106 | + const std::optional<std::uint64_t> reportedUid = dbusUserUint64Property(connection, userObjectPath.c_str(), "Uid"); |
| 107 | + if (!reportedUid.has_value() || static_cast<uid_t>(*reportedUid) != uid) { |
| 108 | + g_object_unref(connection); |
| 109 | + return std::nullopt; |
| 110 | + } |
120 | 111 |
|
121 | | - if (!iconFile.has_value() || !iconPathReadable(*iconFile)) { |
122 | | - return std::nullopt; |
| 112 | + const std::optional<std::string> iconFile = dbusUserStringProperty(connection, userObjectPath.c_str(), "IconFile"); |
| 113 | + g_object_unref(connection); |
| 114 | + |
| 115 | + if (!iconFile.has_value() || !iconPathReadable(*iconFile)) { |
| 116 | + return std::nullopt; |
| 117 | + } |
| 118 | + return iconFile; |
123 | 119 | } |
124 | | - return iconFile; |
125 | | -} |
126 | 120 |
|
127 | 121 | } // namespace accounts |
0 commit comments