Commit 017f7704 authored by Bastien Nocera's avatar Bastien Nocera Committed by Daniel Drake
Browse files

Clean up object creation code

Make all of FPrintManager's struct members private,
warn of errors using a _get_error() function, as object
creation can never fail.

The only error possible shouldn't really be an error though,
as it only means that no devices will be enumerated, but it
could tell us about newly plugged devices instead.
parent 3ad569b6
......@@ -45,15 +45,14 @@ typedef enum {
/* Manager */
#define FPRINT_TYPE_MANAGER (fprint_manager_get_type())
#define FPRINT_MANAGER(object) (G_TYPE_CHECK_INSTANCE_CAST((object), FPRINT_MANAGER_TYPE, FprintManager))
#define FPRINT_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), FPRINT_MANAGER_TYPE, FprintManagerClass))
#define FPRINT_IS_MANAGER(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), FPRINT_MANAGER_TYPE))
#define FPRINT_MANAGER(object) (G_TYPE_CHECK_INSTANCE_CAST((object), FPRINT_TYPE_MANAGER, FprintManager))
#define FPRINT_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), FPRINT_TYPE_MANAGER, FprintManagerClass))
#define FPRINT_IS_MANAGER(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), FPRINT_TYPE_MANAGER))
#define FPRINT_IS_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), FPRINT_TYPE_MANAGER))
#define FPRINT_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), FPRINT_TYPE_MANAGER, FprintManagerClass))
struct FprintManager {
GObject parent;
GSList *dev_registry;
};
struct FprintManagerClass {
......@@ -64,6 +63,7 @@ typedef struct FprintManager FprintManager;
typedef struct FprintManagerClass FprintManagerClass;
FprintManager *fprint_manager_new(void);
GError *fprint_manager_get_error(FprintManager *manager);
GType fprint_manager_get_type(void);
/* Device */
......
......@@ -237,6 +237,12 @@ int main(int argc, char **argv)
/* create the one instance of the Manager object to be shared between
* all fprintd users */
manager = fprint_manager_new();
error = fprint_manager_get_error (manager);
if (error != NULL) {
g_error("Couldn't create manager object: %s", error->message);
g_error_free (error);
return 1;
}
driver_proxy = dbus_g_proxy_new_for_name(fprintd_dbus_conn,
DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
......
......@@ -31,41 +31,37 @@ static gboolean fprint_manager_get_devices(FprintManager *manager,
static GObjectClass *parent_class = NULL;
static void manager_finalize(GObject *object)
G_DEFINE_TYPE(FprintManager, fprint_manager, G_TYPE_OBJECT);
typedef struct
{
GError *last_error;
GSList *dev_registry;
} FprintManagerPrivate;
#define FPRINT_MANAGER_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), FPRINT_TYPE_MANAGER, FprintManagerPrivate))
static void fprint_manager_finalize(GObject *object)
{
FprintManager *self = (FprintManager *) object;
g_slist_free(self->dev_registry);
FprintManagerPrivate *priv = FPRINT_MANAGER_GET_PRIVATE (object);
g_slist_free(priv->dev_registry);
if (priv->last_error)
g_error_free (priv->last_error);
G_OBJECT_CLASS(parent_class)->finalize(object);
}
static void manager_class_init(FprintManagerClass *klass)
static void fprint_manager_class_init(FprintManagerClass *klass)
{
dbus_g_object_type_install_info(FPRINT_TYPE_MANAGER,
&dbus_glib_fprint_manager_object_info);
G_OBJECT_CLASS(klass)->finalize = manager_finalize;
parent_class = g_type_class_peek_parent(klass);
}
g_type_class_add_private ((GObjectClass *) klass, sizeof (FprintManagerPrivate));
GType fprint_manager_get_type(void)
{
static GType type = 0;
if (type == 0) {
static const GTypeInfo info = {
sizeof(FprintManagerClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) manager_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof(FprintManager),
0, /* n_preallocs */
NULL,
};
type = g_type_register_static(G_TYPE_OBJECT, "FprintManagerType",
&info, 0);
}
return type;
G_OBJECT_CLASS(klass)->finalize = fprint_manager_finalize;
parent_class = g_type_class_peek_parent(klass);
}
static gchar *get_device_path(FprintDevice *rdev)
......@@ -74,20 +70,19 @@ static gchar *get_device_path(FprintDevice *rdev)
_fprint_device_get_id(rdev));
}
FprintManager *fprint_manager_new(void)
static void
fprint_manager_init (FprintManager *manager)
{
FprintManager *manager;
FprintManagerPrivate *priv = FPRINT_MANAGER_GET_PRIVATE (manager);
struct fp_dscv_dev **discovered_devs = fp_discover_devs();
struct fp_dscv_dev *ddev;
int i = 0;
/* FIXME some or all of this should probably be moved into FprintManager
* ctor. however i'm not sure how to raise errors from a constructor... */
if (!discovered_devs)
return NULL;
if (!discovered_devs) {
priv->last_error = g_error_new (0, 0, "NO DEVICES AVAILABLE: FIXME");
return;
}
manager = g_object_new(FPRINT_TYPE_MANAGER, NULL);
dbus_g_connection_register_g_object(fprintd_dbus_conn,
"/net/reactivated/Fprint/Manager", G_OBJECT(manager));
......@@ -95,20 +90,31 @@ FprintManager *fprint_manager_new(void)
FprintDevice *rdev = fprint_device_new(ddev);
gchar *path;
manager->dev_registry = g_slist_prepend(manager->dev_registry, rdev);
priv->dev_registry = g_slist_prepend(priv->dev_registry, rdev);
path = get_device_path(rdev);
dbus_g_connection_register_g_object(fprintd_dbus_conn, path,
G_OBJECT(rdev));
g_free(path);
}
}
FprintManager *fprint_manager_new(void)
{
return g_object_new(FPRINT_TYPE_MANAGER, NULL);
}
GError *fprint_manager_get_error(FprintManager *manager)
{
FprintManagerPrivate *priv = FPRINT_MANAGER_GET_PRIVATE (manager);
return manager;
return g_error_copy (priv->last_error);
}
static gboolean fprint_manager_get_devices(FprintManager *manager,
GPtrArray **devices, GError **error)
{
GSList *elem = manager->dev_registry;
FprintManagerPrivate *priv = FPRINT_MANAGER_GET_PRIVATE (manager);
GSList *elem = priv->dev_registry;
int num_open = g_slist_length(elem);
GPtrArray *devs = g_ptr_array_sized_new(num_open);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment