list.c 3.97 KB
Newer Older
1
/*
Bastien Nocera's avatar
Bastien Nocera committed
2
 * fprintd example to list enrolled fingerprints
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus-glib-bindings.h>
#include "manager-dbus-glue.h"
#include "device-dbus-glue.h"

static DBusGProxy *manager = NULL;
static DBusGConnection *connection = NULL;

static void create_manager(void)
{
	GError *error = NULL;

	connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
34 35 36 37
	if (connection == NULL) {
		g_print("Failed to connect to session bus: %s\n", error->message);
		exit (1);
	}
38 39 40 41 42 43 44 45 46

	manager = dbus_g_proxy_new_for_name(connection,
		"net.reactivated.Fprint", "/net/reactivated/Fprint/Manager",
		"net.reactivated.Fprint.Manager");
}

static void list_fingerprints(DBusGProxy *dev, const char *username)
{
	GError *error = NULL;
47
	char **fingers;
48
	GHashTable *props;
49
	DBusGProxy *p;
50 51
	guint i;

52
	if (!net_reactivated_Fprint_Device_list_enrolled_fingers(dev, username, &fingers, &error)) {
53 54 55 56
		if (dbus_g_error_has_name (error, "net.reactivated.Fprint.Error.NoEnrolledPrints") == FALSE) {
			g_print("ListEnrolledFingers failed: %s\n", error->message);
			exit (1);
		} else {
57
			fingers = NULL;
58
		}
59 60
	}

61
	p = dbus_g_proxy_new_from_proxy(dev, "org.freedesktop.DBus.Properties", NULL);
62
	if (!dbus_g_proxy_call (p, "GetAll", &error, G_TYPE_STRING, "net.reactivated.Fprint.Device", G_TYPE_INVALID,
63 64 65 66
			   dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props, G_TYPE_INVALID)) {
		g_print("GetAll on the Properties interface failed: %s\n", error->message);
		exit (1);
	}
67

68 69 70 71 72
	if (fingers == NULL || g_strv_length (fingers) == 0) {
		g_print("User %s has no fingers enrolled for %s.\n", username, g_value_get_string (g_hash_table_lookup (props, "name")));
		return;
	}

73 74
	g_print("Fingerprints for user %s on %s (%s):\n",
		username,
75 76
		g_value_get_string (g_hash_table_lookup (props, "name")),
		g_value_get_string (g_hash_table_lookup (props, "scan-type")));
77
	g_hash_table_destroy (props);
78
	g_object_unref (p);
79 80 81

	for (i = 0; fingers[i] != NULL; i++) {
		g_print(" - #%d: %s\n", i, fingers[i]);
82 83
	}

84
	g_strfreev (fingers);
85 86
}

87 88 89 90 91 92 93
static void process_devices(char **argv)
{
	GError *error = NULL;
	GPtrArray *devices;
	char *path;
	guint i;

94 95 96 97
	if (!net_reactivated_Fprint_Manager_get_devices(manager, &devices, &error)) {
		g_print("list_devices failed: %s\n", error->message);
		exit (1);
	}
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	
	if (devices->len == 0) {
		g_print("No devices found\n");
		exit(1);
	}

	g_print("found %d devices\n", devices->len);
	for (i = 0; i < devices->len; i++) {
		path = g_ptr_array_index(devices, i);
		g_print("Device at %s\n", path);
	}

	for (i = 0; i < devices->len; i++) {
		guint j;
		DBusGProxy *dev;

114
		path = g_ptr_array_index(devices, i);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		g_print("Using device %s\n", path);

		/* FIXME use for_name_owner?? */
		dev = dbus_g_proxy_new_for_name(connection, "net.reactivated.Fprint",
						path, "net.reactivated.Fprint.Device");

		for (j = 1; argv[j] != NULL; j++)
			list_fingerprints (dev, argv[j]);

		g_object_unref (dev);
	}
	
	g_ptr_array_foreach(devices, (GFunc) g_free, NULL);
	g_ptr_array_free(devices, TRUE);
}

131 132
int main(int argc, char **argv)
{
133
#if !GLIB_CHECK_VERSION (2, 36, 0)
134
	g_type_init();
135 136
#endif

137 138 139 140 141 142 143
	create_manager();

	if (argc < 2) {
		g_print ("Usage: %s <username> [usernames...]\n", argv[0]);
		return 1;
	}

144
	process_devices (argv);
145 146 147 148

	return 0;
}