1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
#include <glib/gi18n.h>
30
#include "egg-debug.h"
32
#include "up-backend.h"
33
#include "up-daemon.h"
34
#include "up-marshal.h"
35
#include "up-device.h"
37
static void up_backend_class_init (UpBackendClass *klass);
38
static void up_backend_init (UpBackend *backend);
39
static void up_backend_finalize (GObject *object);
41
#define UP_BACKEND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_BACKEND, UpBackendPrivate))
43
struct UpBackendPrivate
47
UpDeviceList *device_list; /* unused */
53
SIGNAL_DEVICE_REMOVED,
57
static guint signals [SIGNAL_LAST] = { 0 };
59
G_DEFINE_TYPE (UpBackend, up_backend, G_TYPE_OBJECT)
62
* up_backend_changed_time_cb:
65
up_backend_changed_time_cb (UpBackend *backend)
74
g_get_current_time (&timeval);
75
g_object_set (device, "update-time", (guint64) timeval.tv_sec, NULL);
83
up_backend_add_cb (UpBackend *backend)
88
ret = up_device_coldplug (backend->priv->device, backend->priv->daemon, backend->priv->native);
90
egg_warning ("failed to coldplug");
95
g_signal_emit (backend, signals[SIGNAL_DEVICE_ADDED], 0, backend->priv->native, backend->priv->device);
98
g_timeout_add_seconds (2, (GSourceFunc) up_backend_changed_time_cb, backend);
104
* up_backend_coldplug:
105
* @backend: The %UpBackend class instance
106
* @daemon: The %UpDaemon controlling instance
108
* Finds all the devices already plugged in, and emits device-add signals for
111
* Return value: %TRUE for success
114
up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
116
backend->priv->daemon = g_object_ref (daemon);
117
backend->priv->device_list = up_daemon_get_device_list (daemon);
119
/* small delay until first device is added */
120
g_timeout_add_seconds (1, (GSourceFunc) up_backend_add_cb, backend);
126
* up_backend_class_init:
127
* @klass: The UpBackendClass
130
up_backend_class_init (UpBackendClass *klass)
132
GObjectClass *object_class = G_OBJECT_CLASS (klass);
133
object_class->finalize = up_backend_finalize;
135
signals [SIGNAL_DEVICE_ADDED] =
136
g_signal_new ("device-added",
137
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
138
G_STRUCT_OFFSET (UpBackendClass, device_added),
139
NULL, NULL, up_marshal_VOID__POINTER_POINTER,
140
G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
141
signals [SIGNAL_DEVICE_REMOVED] =
142
g_signal_new ("device-removed",
143
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
144
G_STRUCT_OFFSET (UpBackendClass, device_removed),
145
NULL, NULL, up_marshal_VOID__POINTER_POINTER,
146
G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
148
g_type_class_add_private (klass, sizeof (UpBackendPrivate));
155
up_backend_init (UpBackend *backend)
157
backend->priv = UP_BACKEND_GET_PRIVATE (backend);
158
backend->priv->daemon = NULL;
159
backend->priv->device_list = NULL;
160
backend->priv->native = g_object_new (UP_TYPE_DEVICE, NULL);
161
backend->priv->device = up_device_new ();
164
g_object_set (backend->priv->device,
165
"native-path", "/hal/blows/goats",
169
"type", UP_DEVICE_KIND_BATTERY,
171
"power-supply", TRUE,
173
"is-rechargeable", TRUE,
174
"has-history", FALSE,
175
"has-statistics", FALSE,
176
"state", UP_DEVICE_STATE_DISCHARGING,
178
"energy-empty", 0.0f,
179
"energy-full", 10.0f,
180
"energy-full-design", 10.0f,
187
* up_backend_finalize:
190
up_backend_finalize (GObject *object)
194
g_return_if_fail (UP_IS_BACKEND (object));
196
backend = UP_BACKEND (object);
198
if (backend->priv->daemon != NULL)
199
g_object_unref (backend->priv->daemon);
200
if (backend->priv->device_list != NULL)
201
g_object_unref (backend->priv->device_list);
203
g_object_unref (backend->priv->native);
204
g_object_unref (backend->priv->device);
206
G_OBJECT_CLASS (up_backend_parent_class)->finalize (object);
212
* Return value: a new %UpBackend object.
215
up_backend_new (void)
218
backend = g_object_new (UP_TYPE_BACKEND, NULL);
219
return UP_BACKEND (backend);
222
/***************************************************************************
223
*** MAKE CHECK TESTS ***
224
***************************************************************************/
226
#include "egg-test.h"
229
up_backend_test (gpointer user_data)
231
EggTest *test = (EggTest *) user_data;
234
if (!egg_test_start (test, "UpBackend"))
237
/************************************************************/
238
egg_test_title (test, "get instance");
239
backend = up_backend_new ();
240
egg_test_assert (test, backend != NULL);
243
g_object_unref (backend);