Last active
October 9, 2018 17:53
-
-
Save thefloweringash/a4c36057f33204037e12498ff7be9fed to your computer and use it in GitHub Desktop.
accounts-service-consumer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.11) | |
project(accounts-service-consumer LANGUAGES C) | |
find_package(PkgConfig) | |
pkg_check_modules(Glib IMPORTED_TARGET REQUIRED glib-2.0) | |
pkg_check_modules(AccountsService IMPORTED_TARGET REQUIRED accountsservice) | |
add_executable(accounts-service-consumer main.c) | |
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}" -g) | |
target_link_libraries(accounts-service-consumer | |
PkgConfig::Glib | |
PkgConfig::AccountsService | |
) | |
install( | |
TARGETS accounts-service-consumer | |
RUNTIME DESTINATION bin | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with (import <nixpkgs> {}); | |
stdenv.mkDerivation { | |
name = "accounts-service-consumer"; | |
src = lib.cleanSource ./.; | |
nativeBuildInputs = [ cmake pkgconfig ]; | |
buildInputs = [ glib accountsservice ]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#include <act/act.h> | |
// Most of this is verbatim from the gio documentation. | |
static void on_signal(GDBusProxy *proxy, | |
gchar *sender_name, | |
gchar *signal_name, | |
GVariant *parameters, | |
gpointer user_data) { | |
gchar *parameters_str; | |
parameters_str = g_variant_print(parameters, TRUE); | |
g_print(" *** Received Signal: %s: %s\n", | |
signal_name, | |
parameters_str); | |
g_free(parameters_str); | |
} | |
static void on_properties_changed(GDBusProxy *proxy, | |
GVariant *changed_properties, | |
const gchar *const *invalidated_properties, | |
gpointer user_data) { | |
/* Note that we are guaranteed that changed_properties and | |
* invalidated_properties are never NULL | |
*/ | |
if (g_variant_n_children(changed_properties) > 0) { | |
GVariantIter *iter; | |
const gchar *key; | |
GVariant *value; | |
g_print(" *** Properties Changed:\n"); | |
g_variant_get(changed_properties, | |
"a{sv}", | |
&iter); | |
while (g_variant_iter_loop(iter, "{&sv}", &key, &value)) { | |
gchar *value_str; | |
value_str = g_variant_print(value, TRUE); | |
g_print(" %s -> %s\n", key, value_str); | |
g_free(value_str); | |
} | |
g_variant_iter_free(iter); | |
} | |
if (g_strv_length((GStrv) invalidated_properties) > 0) { | |
guint n; | |
g_print(" *** Properties Invalidated:\n"); | |
for (n = 0; invalidated_properties[n] != NULL; n++) { | |
const gchar *key = invalidated_properties[n]; | |
g_print(" %s\n", key); | |
} | |
} | |
} | |
void print_user(void *x, void *userdata) { | |
ActUser *user = (ActUser *) x; | |
printf("user: %s\n", act_user_get_user_name(user)); | |
} | |
int main(int argc, char **argv) { | |
GMainLoop *loop = g_main_loop_new(NULL, false); | |
GError *error = NULL; | |
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync( | |
G_BUS_TYPE_SYSTEM, | |
G_DBUS_PROXY_FLAGS_NONE, | |
NULL, /* GDBusInterfaceInfo */ | |
"org.freedesktop.Accounts", | |
"/org/freedesktop/Accounts/User1000", | |
"org.freedesktop.Accounts.User", | |
NULL, /* GCancellable */ | |
&error); | |
if (!proxy || error) { | |
abort(); | |
} | |
g_signal_connect( | |
proxy, | |
"g-properties-changed", | |
G_CALLBACK(on_properties_changed), | |
NULL); | |
g_signal_connect( | |
proxy, | |
"g-signal", | |
G_CALLBACK(on_signal), | |
NULL); | |
ActUserManager *manager = act_user_manager_get_default(); | |
GSList *user_list = act_user_manager_list_users(manager); | |
g_slist_foreach(user_list, &print_user, NULL); | |
g_slist_free(user_list); | |
g_main_loop_run(loop); | |
g_free(loop); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment