mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2026-01-06 10:30:09 +01:00
switch from using GPtrArray to EggObjList as this provides us with auto array cleanup for different objects
This commit is contained in:
parent
7fdcc278dc
commit
38a050b01f
10 changed files with 429 additions and 69 deletions
|
|
@ -14,6 +14,8 @@ libdevkit_power_la_SOURCES = \
|
|||
egg-debug.h \
|
||||
egg-string.c \
|
||||
egg-string.h \
|
||||
egg-obj-list.c \
|
||||
egg-obj-list.h \
|
||||
dkp-client.c \
|
||||
dkp-client.h \
|
||||
dkp-client-device.c \
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@
|
|||
#include <dbus/dbus-glib.h>
|
||||
|
||||
#include "egg-debug.h"
|
||||
#include "egg-obj-list.h"
|
||||
|
||||
#include "dkp-client-device.h"
|
||||
#include "dkp-object.h"
|
||||
#include "dkp-stats-obj.h"
|
||||
|
|
@ -199,7 +201,7 @@ static gboolean
|
|||
dkp_client_device_print_history (const DkpClientDevice *device, const gchar *type)
|
||||
{
|
||||
guint i;
|
||||
GPtrArray *array;
|
||||
EggObjList *array;
|
||||
DkpHistoryObj *obj;
|
||||
gboolean ret = FALSE;
|
||||
|
||||
|
|
@ -211,11 +213,10 @@ dkp_client_device_print_history (const DkpClientDevice *device, const gchar *typ
|
|||
/* pretty print */
|
||||
g_print (" Statistics (%s):\n", type);
|
||||
for (i=0; i<array->len; i++) {
|
||||
obj = (DkpHistoryObj *) g_ptr_array_index (array, i);
|
||||
obj = (DkpHistoryObj *) egg_obj_list_index (array, i);
|
||||
g_print (" %i\t%.3f\t%s\n", obj->time, obj->value, dkp_device_state_to_text (obj->state));
|
||||
}
|
||||
g_ptr_array_foreach (array, (GFunc) dkp_history_obj_free, NULL);
|
||||
g_ptr_array_free (array, TRUE);
|
||||
g_object_unref (array);
|
||||
ret = TRUE;
|
||||
out:
|
||||
return ret;
|
||||
|
|
@ -268,7 +269,7 @@ out:
|
|||
*
|
||||
* Returns an array of %DkpHistoryObj's
|
||||
**/
|
||||
GPtrArray *
|
||||
EggObjList *
|
||||
dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type, guint timespec)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
|
@ -278,7 +279,7 @@ dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type,
|
|||
GValue *gv;
|
||||
guint i;
|
||||
DkpHistoryObj *obj;
|
||||
GPtrArray *array = NULL;
|
||||
EggObjList *array = NULL;
|
||||
gboolean ret;
|
||||
|
||||
g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
|
||||
|
|
@ -309,7 +310,10 @@ dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type,
|
|||
goto out;
|
||||
|
||||
/* convert */
|
||||
array = g_ptr_array_sized_new (gvalue_ptr_array->len);
|
||||
array = egg_obj_list_new ();
|
||||
egg_obj_list_set_copy (array, (EggObjListCopyFunc) dkp_history_obj_copy);
|
||||
egg_obj_list_set_free (array, (EggObjListFreeFunc) dkp_history_obj_free);
|
||||
|
||||
for (i=0; i<gvalue_ptr_array->len; i++) {
|
||||
gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
|
||||
obj = dkp_history_obj_new ();
|
||||
|
|
@ -325,7 +329,8 @@ dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type,
|
|||
gv = g_value_array_get_nth (gva, 2);
|
||||
obj->state = dkp_device_state_from_text (g_value_get_string (gv));
|
||||
g_value_unset (gv);
|
||||
g_ptr_array_add (array, obj);
|
||||
egg_obj_list_add (array, obj);
|
||||
dkp_history_obj_free (obj);
|
||||
g_value_array_free (gva);
|
||||
}
|
||||
|
||||
|
|
@ -340,7 +345,7 @@ out:
|
|||
*
|
||||
* Returns an array of %DkpStatsObj's
|
||||
**/
|
||||
GPtrArray *
|
||||
EggObjList *
|
||||
dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *type)
|
||||
{
|
||||
GError *error = NULL;
|
||||
|
|
@ -350,7 +355,7 @@ dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *ty
|
|||
GValue *gv;
|
||||
guint i;
|
||||
DkpStatsObj *obj;
|
||||
GPtrArray *array = NULL;
|
||||
EggObjList *array = NULL;
|
||||
gboolean ret;
|
||||
|
||||
g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
|
||||
|
|
@ -379,7 +384,10 @@ dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *ty
|
|||
goto out;
|
||||
|
||||
/* convert */
|
||||
array = g_ptr_array_sized_new (gvalue_ptr_array->len);
|
||||
array = egg_obj_list_new ();
|
||||
egg_obj_list_set_copy (array, (EggObjListCopyFunc) dkp_stats_obj_copy);
|
||||
egg_obj_list_set_free (array, (EggObjListFreeFunc) dkp_stats_obj_free);
|
||||
|
||||
for (i=0; i<gvalue_ptr_array->len; i++) {
|
||||
gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
|
||||
obj = dkp_stats_obj_new ();
|
||||
|
|
@ -392,7 +400,8 @@ dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *ty
|
|||
obj->accuracy = g_value_get_double (gv);
|
||||
g_value_unset (gv);
|
||||
/* 2 */
|
||||
g_ptr_array_add (array, obj);
|
||||
egg_obj_list_add (array, obj);
|
||||
dkp_stats_obj_free (obj);
|
||||
g_value_array_free (gva);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
#include <glib-object.h>
|
||||
#include <dkp-enum.h>
|
||||
#include <dkp-object.h>
|
||||
#include "egg-obj-list.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
|
|
@ -62,10 +63,10 @@ gboolean dkp_client_device_set_object_path (DkpClientDevice *device,
|
|||
|
||||
gboolean dkp_client_device_print (const DkpClientDevice *device);
|
||||
gboolean dkp_client_device_refresh (DkpClientDevice *device);
|
||||
GPtrArray *dkp_client_device_get_history (const DkpClientDevice *device,
|
||||
EggObjList *dkp_client_device_get_history (const DkpClientDevice *device,
|
||||
const gchar *type,
|
||||
guint timespec);
|
||||
GPtrArray *dkp_client_device_get_statistics (const DkpClientDevice *device,
|
||||
EggObjList *dkp_client_device_get_statistics (const DkpClientDevice *device,
|
||||
const gchar *type);
|
||||
|
||||
G_END_DECLS
|
||||
|
|
|
|||
252
libdevkit-power/egg-obj-list.c
Normal file
252
libdevkit-power/egg-obj-list.c
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
||||
*
|
||||
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
|
||||
*
|
||||
* Licensed under the GNU General Public License Version 2
|
||||
*
|
||||
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <glib/gi18n.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "egg-obj-list.h"
|
||||
|
||||
#define EGG_OBJ_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_OBJ_LIST, EggObjListPrivate))
|
||||
|
||||
struct EggObjListPrivate
|
||||
{
|
||||
EggObjListNewFunc func_new;
|
||||
EggObjListCopyFunc func_copy;
|
||||
EggObjListFreeFunc func_free;
|
||||
GPtrArray *array;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (EggObjList, egg_obj_list, G_TYPE_OBJECT)
|
||||
|
||||
/**
|
||||
* egg_obj_list_set_new:
|
||||
* @list: a valid #EggObjList instance
|
||||
* @func: typedef'd function
|
||||
*
|
||||
* Adds a creation func
|
||||
**/
|
||||
void
|
||||
egg_obj_list_set_new (EggObjList *list, EggObjListNewFunc func)
|
||||
{
|
||||
g_return_if_fail (EGG_IS_OBJ_LIST (list));
|
||||
list->priv->func_new = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_set_copy:
|
||||
* @list: a valid #EggObjList instance
|
||||
* @func: typedef'd function
|
||||
*
|
||||
* Adds a copy func
|
||||
**/
|
||||
void
|
||||
egg_obj_list_set_copy (EggObjList *list, EggObjListCopyFunc func)
|
||||
{
|
||||
g_return_if_fail (EGG_IS_OBJ_LIST (list));
|
||||
list->priv->func_copy = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_set_free:
|
||||
* @list: a valid #EggObjList instance
|
||||
* @func: typedef'd function
|
||||
*
|
||||
* Adds a free func
|
||||
**/
|
||||
void
|
||||
egg_obj_list_set_free (EggObjList *list, EggObjListFreeFunc func)
|
||||
{
|
||||
g_return_if_fail (EGG_IS_OBJ_LIST (list));
|
||||
list->priv->func_free = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_clear:
|
||||
* @list: a valid #EggObjList instance
|
||||
*
|
||||
* Clears the package list
|
||||
**/
|
||||
void
|
||||
egg_obj_list_clear (EggObjList *list)
|
||||
{
|
||||
guint i;
|
||||
gpointer obj;
|
||||
GPtrArray *array;
|
||||
EggObjListFreeFunc func_free;
|
||||
|
||||
g_return_if_fail (EGG_IS_OBJ_LIST (list));
|
||||
|
||||
array = list->priv->array;
|
||||
func_free = list->priv->func_free;
|
||||
for (i=0; i<array->len; i++) {
|
||||
obj = g_ptr_array_index (array, i);
|
||||
if (func_free != NULL)
|
||||
func_free (obj);
|
||||
g_ptr_array_remove (array, obj);
|
||||
}
|
||||
list->len = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_add:
|
||||
* @list: a valid #EggObjList instance
|
||||
* @obj: a valid #gpointer object
|
||||
*
|
||||
* Adds a copy of the object to the list
|
||||
**/
|
||||
void
|
||||
egg_obj_list_add (EggObjList *list, const gpointer obj)
|
||||
{
|
||||
gpointer obj_new;
|
||||
|
||||
g_return_if_fail (EGG_IS_OBJ_LIST (list));
|
||||
g_return_if_fail (obj != NULL);
|
||||
g_return_if_fail (list->priv->func_copy != NULL);
|
||||
|
||||
/* TODO: are we already in the list? */
|
||||
obj_new = list->priv->func_copy (obj);
|
||||
g_ptr_array_add (list->priv->array, obj_new);
|
||||
list->len = list->priv->array->len;
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_index:
|
||||
* @list: a valid #EggObjList instance
|
||||
* @id: A #EggPackageId of the item to match
|
||||
*
|
||||
* Gets an object from the list
|
||||
**/
|
||||
const gpointer
|
||||
egg_obj_list_index (EggObjList *list, guint i)
|
||||
{
|
||||
gpointer obj;
|
||||
|
||||
g_return_val_if_fail (EGG_IS_OBJ_LIST (list), NULL);
|
||||
|
||||
obj = g_ptr_array_index (list->priv->array, i);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_finalize:
|
||||
* @object: a valid #EggObjList instance
|
||||
**/
|
||||
static void
|
||||
egg_obj_list_finalize (GObject *object)
|
||||
{
|
||||
EggObjListFreeFunc func_free;
|
||||
gpointer obj;
|
||||
guint i;
|
||||
EggObjList *list;
|
||||
GPtrArray *array;
|
||||
g_return_if_fail (EGG_IS_OBJ_LIST (object));
|
||||
list = EGG_OBJ_LIST (object);
|
||||
|
||||
array = list->priv->array;
|
||||
func_free = list->priv->func_free;
|
||||
for (i=0; i<array->len; i++) {
|
||||
obj = g_ptr_array_index (array, i);
|
||||
if (func_free != NULL)
|
||||
func_free (obj);
|
||||
}
|
||||
g_ptr_array_free (array, TRUE);
|
||||
|
||||
G_OBJECT_CLASS (egg_obj_list_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_class_init:
|
||||
* @klass: a valid #EggObjListClass instance
|
||||
**/
|
||||
static void
|
||||
egg_obj_list_class_init (EggObjListClass *klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||
object_class->finalize = egg_obj_list_finalize;
|
||||
g_type_class_add_private (klass, sizeof (EggObjListPrivate));
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_init:
|
||||
* @list: a valid #EggObjList instance
|
||||
*
|
||||
* Initializes the obj_list class.
|
||||
**/
|
||||
static void
|
||||
egg_obj_list_init (EggObjList *list)
|
||||
{
|
||||
list->priv = EGG_OBJ_LIST_GET_PRIVATE (list);
|
||||
list->priv->func_new = NULL;
|
||||
list->priv->func_copy = NULL;
|
||||
list->priv->func_free = NULL;
|
||||
list->priv->array = g_ptr_array_new ();
|
||||
list->len = list->priv->array->len;
|
||||
}
|
||||
|
||||
/**
|
||||
* egg_obj_list_new:
|
||||
*
|
||||
* Return value: A new list class instance.
|
||||
**/
|
||||
EggObjList *
|
||||
egg_obj_list_new (void)
|
||||
{
|
||||
EggObjList *list;
|
||||
list = g_object_new (EGG_TYPE_OBJ_LIST, NULL);
|
||||
return EGG_OBJ_LIST (list);
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
*** MAKE CHECK TESTS ***
|
||||
***************************************************************************/
|
||||
#ifdef EGG_BUILD_TESTS
|
||||
#include <libselftest.h>
|
||||
|
||||
void
|
||||
libst_obj_list (LibSelfTest *test)
|
||||
{
|
||||
EggObjList *list;
|
||||
gchar *text;
|
||||
gint value;
|
||||
|
||||
if (libst_start (test, "EggObjList", CLASS_AUTO) == FALSE) {
|
||||
return;
|
||||
}
|
||||
|
||||
/************************************************************/
|
||||
libst_title (test, "get an instance");
|
||||
list = egg_obj_list_new ();
|
||||
if (list != NULL) {
|
||||
libst_success (test, NULL);
|
||||
} else {
|
||||
libst_failed (test, NULL);
|
||||
}
|
||||
|
||||
g_object_unref (list);
|
||||
|
||||
libst_end (test);
|
||||
}
|
||||
#endif
|
||||
|
||||
71
libdevkit-power/egg-obj-list.h
Normal file
71
libdevkit-power/egg-obj-list.h
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
||||
*
|
||||
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
|
||||
*
|
||||
* Licensed under the GNU General Public License Version 2
|
||||
*
|
||||
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __EGG_OBJ_LIST_H
|
||||
#define __EGG_OBJ_LIST_H
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define EGG_TYPE_OBJ_LIST (egg_obj_list_get_type ())
|
||||
#define EGG_OBJ_LIST(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EGG_TYPE_OBJ_LIST, EggObjList))
|
||||
#define EGG_OBJ_LIST_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EGG_TYPE_OBJ_LIST, EggObjListClass))
|
||||
#define EGG_IS_OBJ_LIST(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EGG_TYPE_OBJ_LIST))
|
||||
#define EGG_IS_OBJ_LIST_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EGG_TYPE_OBJ_LIST))
|
||||
#define EGG_OBJ_LIST_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EGG_TYPE_OBJ_LIST, EggObjListClass))
|
||||
|
||||
typedef struct EggObjListPrivate EggObjListPrivate;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GObject parent;
|
||||
EggObjListPrivate *priv;
|
||||
guint len;
|
||||
} EggObjList;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GObjectClass parent_class;
|
||||
} EggObjListClass;
|
||||
|
||||
typedef gpointer (*EggObjListNewFunc) (void);
|
||||
typedef gpointer (*EggObjListCopyFunc) (const gpointer data);
|
||||
typedef void (*EggObjListFreeFunc) (gpointer data);
|
||||
|
||||
GType egg_obj_list_get_type (void) G_GNUC_CONST;
|
||||
EggObjList *egg_obj_list_new (void);
|
||||
|
||||
void egg_obj_list_set_new (EggObjList *list,
|
||||
EggObjListNewFunc func);
|
||||
void egg_obj_list_set_copy (EggObjList *list,
|
||||
EggObjListCopyFunc func);
|
||||
void egg_obj_list_set_free (EggObjList *list,
|
||||
EggObjListFreeFunc func);
|
||||
void egg_obj_list_clear (EggObjList *list);
|
||||
void egg_obj_list_add (EggObjList *list,
|
||||
const gpointer data);
|
||||
const gpointer egg_obj_list_index (EggObjList *list,
|
||||
guint index);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __EGG_OBJ_LIST_H */
|
||||
|
|
@ -36,6 +36,8 @@
|
|||
|
||||
#include "sysfs-utils.h"
|
||||
#include "egg-debug.h"
|
||||
#include "egg-obj-list.h"
|
||||
|
||||
#include "dkp-supply.h"
|
||||
#include "dkp-device.h"
|
||||
#include "dkp-device.h"
|
||||
|
|
@ -315,7 +317,7 @@ dkp_device_get_statistics (DkpDevice *device, const gchar *type, DBusGMethodInvo
|
|||
{
|
||||
DkpDeviceClass *klass = DKP_DEVICE_GET_CLASS (device);
|
||||
GError *error;
|
||||
GPtrArray *array;
|
||||
EggObjList *array = NULL;
|
||||
GPtrArray *complex;
|
||||
const DkpStatsObj *obj;
|
||||
GValue *value;
|
||||
|
|
@ -348,7 +350,7 @@ dkp_device_get_statistics (DkpDevice *device, const gchar *type, DBusGMethodInvo
|
|||
/* copy data to dbus struct */
|
||||
complex = g_ptr_array_sized_new (array->len);
|
||||
for (i=0; i<array->len; i++) {
|
||||
obj = (const DkpStatsObj *) g_ptr_array_index (array, i);
|
||||
obj = (const DkpStatsObj *) egg_obj_list_index (array, i);
|
||||
value = g_new0 (GValue, 1);
|
||||
g_value_init (value, DKP_DBUS_STRUCT_DOUBLE_DOUBLE);
|
||||
g_value_take_boxed (value, dbus_g_type_specialized_construct (DKP_DBUS_STRUCT_DOUBLE_DOUBLE));
|
||||
|
|
@ -357,9 +359,10 @@ dkp_device_get_statistics (DkpDevice *device, const gchar *type, DBusGMethodInvo
|
|||
g_free (value);
|
||||
}
|
||||
|
||||
g_ptr_array_free (array, TRUE);
|
||||
dbus_g_method_return (context, complex);
|
||||
out:
|
||||
if (array != NULL)
|
||||
g_object_unref (array);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
@ -371,7 +374,7 @@ dkp_device_get_history (DkpDevice *device, const gchar *type, guint timespan, DB
|
|||
{
|
||||
DkpDeviceClass *klass = DKP_DEVICE_GET_CLASS (device);
|
||||
GError *error;
|
||||
GPtrArray *array;
|
||||
EggObjList *array = NULL;
|
||||
GPtrArray *complex;
|
||||
const DkpHistoryObj *obj;
|
||||
GValue *value;
|
||||
|
|
@ -397,7 +400,7 @@ dkp_device_get_history (DkpDevice *device, const gchar *type, guint timespan, DB
|
|||
/* copy data to dbus struct */
|
||||
complex = g_ptr_array_sized_new (array->len);
|
||||
for (i=0; i<array->len; i++) {
|
||||
obj = (const DkpHistoryObj *) g_ptr_array_index (array, i);
|
||||
obj = (const DkpHistoryObj *) egg_obj_list_index (array, i);
|
||||
value = g_new0 (GValue, 1);
|
||||
g_value_init (value, DKP_DBUS_STRUCT_UINT_DOUBLE_STRING);
|
||||
g_value_take_boxed (value, dbus_g_type_specialized_construct (DKP_DBUS_STRUCT_UINT_DOUBLE_STRING));
|
||||
|
|
@ -406,9 +409,10 @@ dkp_device_get_history (DkpDevice *device, const gchar *type, guint timespan, DB
|
|||
g_free (value);
|
||||
}
|
||||
|
||||
g_ptr_array_free (array, TRUE);
|
||||
dbus_g_method_return (context, complex);
|
||||
out:
|
||||
if (array != NULL)
|
||||
g_object_unref (array);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,8 +26,10 @@
|
|||
#include <polkit-dbus/polkit-dbus.h>
|
||||
#include <devkit-gobject.h>
|
||||
#include <dbus/dbus-glib.h>
|
||||
#include "dkp-object.h"
|
||||
|
||||
#include "egg-obj-list.h"
|
||||
|
||||
#include "dkp-object.h"
|
||||
#include "dkp-daemon.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
|
@ -59,10 +61,10 @@ typedef struct
|
|||
gboolean *on_battery);
|
||||
gboolean (*get_low_battery) (DkpDevice *device,
|
||||
gboolean *low_battery);
|
||||
GPtrArray *(*get_history) (DkpDevice *device,
|
||||
EggObjList *(*get_history) (DkpDevice *device,
|
||||
const gchar *type,
|
||||
guint timespan);
|
||||
GPtrArray *(*get_stats) (DkpDevice *device,
|
||||
EggObjList *(*get_stats) (DkpDevice *device,
|
||||
const gchar *type);
|
||||
} DkpDeviceClass;
|
||||
|
||||
|
|
|
|||
|
|
@ -46,10 +46,10 @@ struct DkpHistoryPrivate
|
|||
gint64 time_empty_last;
|
||||
gdouble percentage_last;
|
||||
DkpDeviceState state;
|
||||
GPtrArray *data_rate;
|
||||
GPtrArray *data_charge;
|
||||
GPtrArray *data_time_full;
|
||||
GPtrArray *data_time_empty;
|
||||
EggObjList *data_rate;
|
||||
EggObjList *data_charge;
|
||||
EggObjList *data_time_full;
|
||||
EggObjList *data_time_empty;
|
||||
guint save_id;
|
||||
};
|
||||
|
||||
|
|
@ -61,33 +61,45 @@ enum {
|
|||
G_DEFINE_TYPE (DkpHistory, dkp_history, G_TYPE_OBJECT)
|
||||
#define DKP_HISTORY_FILE_HEADER "PackageKit Profile"
|
||||
|
||||
/**
|
||||
* dkp_history_get_history_list:
|
||||
**/
|
||||
static EggObjList *
|
||||
dkp_history_new_history_list (void)
|
||||
{
|
||||
EggObjList *list;
|
||||
list = egg_obj_list_new ();
|
||||
egg_obj_list_set_new (list, (EggObjListNewFunc) dkp_history_obj_new);
|
||||
egg_obj_list_set_copy (list, (EggObjListCopyFunc) dkp_history_obj_copy);
|
||||
egg_obj_list_set_free (list, (EggObjListFreeFunc) dkp_history_obj_free);
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_history_copy_array_timespan:
|
||||
**/
|
||||
static GPtrArray *
|
||||
dkp_history_copy_array_timespan (GPtrArray *array, guint timespan)
|
||||
static EggObjList *
|
||||
dkp_history_copy_array_timespan (EggObjList *array, guint timespan)
|
||||
{
|
||||
guint i;
|
||||
const DkpHistoryObj *obj;
|
||||
DkpHistoryObj *obj_new;
|
||||
GPtrArray *array_new;
|
||||
EggObjList *array_new;
|
||||
guint start;
|
||||
|
||||
/* no data */
|
||||
if (array->len == 0)
|
||||
return NULL;
|
||||
|
||||
array_new = g_ptr_array_new ();
|
||||
/* new data */
|
||||
array_new = dkp_history_new_history_list ();
|
||||
|
||||
/* treat the timespan like a range, and search backwards */
|
||||
obj = (const DkpHistoryObj *) g_ptr_array_index (array, array->len-1);
|
||||
obj = (const DkpHistoryObj *) egg_obj_list_index (array, array->len-1);
|
||||
start = obj->time;
|
||||
for (i=array->len-1; i>0; i--) {
|
||||
obj = (const DkpHistoryObj *) g_ptr_array_index (array, i);
|
||||
if (start - obj->time < timespan) {
|
||||
obj_new = dkp_history_obj_copy (obj);
|
||||
g_ptr_array_add (array_new, obj_new);
|
||||
}
|
||||
obj = (const DkpHistoryObj *) egg_obj_list_index (array, i);
|
||||
if (start - obj->time < timespan)
|
||||
egg_obj_list_add (array_new, (const gpointer) obj);
|
||||
}
|
||||
|
||||
return array_new;
|
||||
|
|
@ -96,10 +108,10 @@ dkp_history_copy_array_timespan (GPtrArray *array, guint timespan)
|
|||
/**
|
||||
* dkp_history_get_charge_data:
|
||||
**/
|
||||
GPtrArray *
|
||||
EggObjList *
|
||||
dkp_history_get_charge_data (DkpHistory *history, guint timespan)
|
||||
{
|
||||
GPtrArray *array;
|
||||
EggObjList *array;
|
||||
|
||||
g_return_val_if_fail (DKP_IS_HISTORY (history), NULL);
|
||||
|
||||
|
|
@ -112,10 +124,10 @@ dkp_history_get_charge_data (DkpHistory *history, guint timespan)
|
|||
/**
|
||||
* dkp_history_get_rate_data:
|
||||
**/
|
||||
GPtrArray *
|
||||
EggObjList *
|
||||
dkp_history_get_rate_data (DkpHistory *history, guint timespan)
|
||||
{
|
||||
GPtrArray *array;
|
||||
EggObjList *array;
|
||||
|
||||
g_return_val_if_fail (DKP_IS_HISTORY (history), NULL);
|
||||
|
||||
|
|
@ -128,10 +140,10 @@ dkp_history_get_rate_data (DkpHistory *history, guint timespan)
|
|||
/**
|
||||
* dkp_history_get_time_full_data:
|
||||
**/
|
||||
GPtrArray *
|
||||
EggObjList *
|
||||
dkp_history_get_time_full_data (DkpHistory *history, guint timespan)
|
||||
{
|
||||
GPtrArray *array;
|
||||
EggObjList *array;
|
||||
|
||||
g_return_val_if_fail (DKP_IS_HISTORY (history), NULL);
|
||||
|
||||
|
|
@ -144,10 +156,10 @@ dkp_history_get_time_full_data (DkpHistory *history, guint timespan)
|
|||
/**
|
||||
* dkp_history_get_time_empty_data:
|
||||
**/
|
||||
GPtrArray *
|
||||
EggObjList *
|
||||
dkp_history_get_time_empty_data (DkpHistory *history, guint timespan)
|
||||
{
|
||||
GPtrArray *array;
|
||||
EggObjList *array;
|
||||
|
||||
g_return_val_if_fail (DKP_IS_HISTORY (history), NULL);
|
||||
|
||||
|
|
@ -176,7 +188,7 @@ dkp_history_get_filename (DkpHistory *history, const gchar *type)
|
|||
* dkp_history_save_data_array:
|
||||
**/
|
||||
static gboolean
|
||||
dkp_history_save_data_array (const gchar *filename, GPtrArray *array)
|
||||
dkp_history_save_data_array (const gchar *filename, EggObjList *array)
|
||||
{
|
||||
guint i;
|
||||
const DkpHistoryObj *obj;
|
||||
|
|
@ -189,7 +201,7 @@ dkp_history_save_data_array (const gchar *filename, GPtrArray *array)
|
|||
/* generate data */
|
||||
string = g_string_new ("");
|
||||
for (i=0; i<array->len; i++) {
|
||||
obj = (const DkpHistoryObj *) g_ptr_array_index (array, i);
|
||||
obj = (const DkpHistoryObj *) egg_obj_list_index (array, i);
|
||||
part = dkp_history_obj_to_string (obj);
|
||||
if (part == NULL) {
|
||||
ret = FALSE;
|
||||
|
|
@ -290,7 +302,7 @@ dkp_history_is_low_power (DkpHistory *history)
|
|||
return FALSE;
|
||||
|
||||
/* get the last saved charge object */
|
||||
obj = (const DkpHistoryObj *) g_ptr_array_index (history->priv->data_charge, length-1);
|
||||
obj = (const DkpHistoryObj *) egg_obj_list_index (history->priv->data_charge, length-1);
|
||||
if (obj->state != DKP_DEVICE_STATE_DISCHARGING)
|
||||
return FALSE;
|
||||
|
||||
|
|
@ -338,7 +350,7 @@ dkp_history_schedule_save (DkpHistory *history)
|
|||
* dkp_history_load_data_array:
|
||||
**/
|
||||
static gboolean
|
||||
dkp_history_load_data_array (const gchar *filename, GPtrArray *array)
|
||||
dkp_history_load_data_array (const gchar *filename, EggObjList *array)
|
||||
{
|
||||
gboolean ret;
|
||||
GFile *file = NULL;
|
||||
|
|
@ -378,7 +390,8 @@ dkp_history_load_data_array (const gchar *filename, GPtrArray *array)
|
|||
for (i=0; i<length-1; i++) {
|
||||
obj = dkp_history_obj_from_string (parts[i]);
|
||||
if (obj != NULL)
|
||||
g_ptr_array_add (array, obj);
|
||||
egg_obj_list_add (array, obj);
|
||||
dkp_history_obj_free (obj);
|
||||
}
|
||||
|
||||
out:
|
||||
|
|
@ -476,7 +489,8 @@ dkp_history_set_charge_data (DkpHistory *history, gdouble percentage)
|
|||
|
||||
/* add to array and schedule save file */
|
||||
obj = dkp_history_obj_create (percentage, history->priv->state);
|
||||
g_ptr_array_add (history->priv->data_charge, obj);
|
||||
egg_obj_list_add (history->priv->data_charge, obj);
|
||||
dkp_history_obj_free (obj);
|
||||
dkp_history_schedule_save (history);
|
||||
|
||||
/* save last value */
|
||||
|
|
@ -504,7 +518,8 @@ dkp_history_set_rate_data (DkpHistory *history, gdouble rate)
|
|||
|
||||
/* add to array and schedule save file */
|
||||
obj = dkp_history_obj_create (rate, history->priv->state);
|
||||
g_ptr_array_add (history->priv->data_rate, obj);
|
||||
egg_obj_list_add (history->priv->data_rate, obj);
|
||||
dkp_history_obj_free (obj);
|
||||
dkp_history_schedule_save (history);
|
||||
|
||||
/* save last value */
|
||||
|
|
@ -534,7 +549,8 @@ dkp_history_set_time_full_data (DkpHistory *history, gint64 time)
|
|||
|
||||
/* add to array and schedule save file */
|
||||
obj = dkp_history_obj_create ((gdouble) time, history->priv->state);
|
||||
g_ptr_array_add (history->priv->data_time_full, obj);
|
||||
egg_obj_list_add (history->priv->data_time_full, obj);
|
||||
dkp_history_obj_free (obj);
|
||||
dkp_history_schedule_save (history);
|
||||
|
||||
/* save last value */
|
||||
|
|
@ -564,7 +580,8 @@ dkp_history_set_time_empty_data (DkpHistory *history, gint64 time)
|
|||
|
||||
/* add to array and schedule save file */
|
||||
obj = dkp_history_obj_create ((gdouble) time, history->priv->state);
|
||||
g_ptr_array_add (history->priv->data_time_empty, obj);
|
||||
egg_obj_list_add (history->priv->data_time_empty, obj);
|
||||
dkp_history_obj_free (obj);
|
||||
dkp_history_schedule_save (history);
|
||||
|
||||
/* save last value */
|
||||
|
|
@ -597,10 +614,10 @@ dkp_history_init (DkpHistory *history)
|
|||
history->priv->rate_last = 0;
|
||||
history->priv->percentage_last = 0;
|
||||
history->priv->state = DKP_DEVICE_STATE_UNKNOWN;
|
||||
history->priv->data_rate = g_ptr_array_new ();
|
||||
history->priv->data_charge = g_ptr_array_new ();
|
||||
history->priv->data_time_full = g_ptr_array_new ();
|
||||
history->priv->data_time_empty = g_ptr_array_new ();
|
||||
history->priv->data_rate = dkp_history_new_history_list ();
|
||||
history->priv->data_charge = dkp_history_new_history_list ();
|
||||
history->priv->data_time_full = dkp_history_new_history_list ();
|
||||
history->priv->data_time_empty = dkp_history_new_history_list ();
|
||||
history->priv->save_id = 0;
|
||||
}
|
||||
|
||||
|
|
@ -620,10 +637,10 @@ dkp_history_finalize (GObject *object)
|
|||
/* save */
|
||||
dkp_history_save_data (history);
|
||||
|
||||
g_ptr_array_free (history->priv->data_rate, TRUE);
|
||||
g_ptr_array_free (history->priv->data_charge, TRUE);
|
||||
g_ptr_array_free (history->priv->data_time_full, TRUE);
|
||||
g_ptr_array_free (history->priv->data_time_empty, TRUE);
|
||||
g_object_unref (history->priv->data_rate);
|
||||
g_object_unref (history->priv->data_charge);
|
||||
g_object_unref (history->priv->data_time_full);
|
||||
g_object_unref (history->priv->data_time_empty);
|
||||
g_free (history->priv->id);
|
||||
|
||||
g_return_if_fail (history->priv != NULL);
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@
|
|||
#include <glib-object.h>
|
||||
#include <dkp-enum.h>
|
||||
|
||||
#include "egg-obj-list.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define DKP_TYPE_HISTORY (dkp_history_get_type ())
|
||||
|
|
@ -51,13 +53,13 @@ typedef struct
|
|||
|
||||
GType dkp_history_get_type (void) G_GNUC_CONST;
|
||||
DkpHistory *dkp_history_new (void);
|
||||
GPtrArray *dkp_history_get_charge_data (DkpHistory *history,
|
||||
EggObjList *dkp_history_get_charge_data (DkpHistory *history,
|
||||
guint timespan);
|
||||
GPtrArray *dkp_history_get_rate_data (DkpHistory *history,
|
||||
EggObjList *dkp_history_get_rate_data (DkpHistory *history,
|
||||
guint timespan);
|
||||
GPtrArray *dkp_history_get_time_full_data (DkpHistory *history,
|
||||
EggObjList *dkp_history_get_time_full_data (DkpHistory *history,
|
||||
guint timespan);
|
||||
GPtrArray *dkp_history_get_time_empty_data (DkpHistory *history,
|
||||
EggObjList *dkp_history_get_time_empty_data (DkpHistory *history,
|
||||
guint timespan);
|
||||
|
||||
gboolean dkp_history_set_id (DkpHistory *history,
|
||||
|
|
|
|||
|
|
@ -361,11 +361,11 @@ dkp_supply_poll_battery (DkpSupply *supply)
|
|||
/**
|
||||
* dkp_supply_get_history:
|
||||
**/
|
||||
static GPtrArray *
|
||||
static EggObjList *
|
||||
dkp_supply_get_history (DkpDevice *device, const gchar *type, guint timespan)
|
||||
{
|
||||
DkpSupply *supply = DKP_SUPPLY (device);
|
||||
GPtrArray *array = NULL;
|
||||
EggObjList *array = NULL;
|
||||
|
||||
g_return_val_if_fail (DKP_IS_SUPPLY (supply), FALSE);
|
||||
g_return_val_if_fail (type != NULL, FALSE);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue