~xavi-garcia-mena/ubuntu/vivid/upower/percentages-power-off

« back to all changes in this revision

Viewing changes to libupower-glib/up-stats-item.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2010-02-16 10:16:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100216101624-2cmwqsr1ndftdd87
Tags: upstream-0.9.0+git20100216.72bb2
ImportĀ upstreamĀ versionĀ 0.9.0+git20100216.72bb2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
/**
 
23
 * SECTION:up-stats-item
 
24
 * @short_description: Helper object representing one item of statistics data.
 
25
 *
 
26
 * This object represents one item of data which may be returned from the
 
27
 * daemon in response to a query.
 
28
 *
 
29
 * See also: #UpDevice, #UpClient
 
30
 */
 
31
 
 
32
#include "config.h"
 
33
 
 
34
#include <glib.h>
 
35
 
 
36
#include "up-stats-item.h"
 
37
 
 
38
static void     up_stats_item_class_init        (UpStatsItemClass       *klass);
 
39
static void     up_stats_item_init              (UpStatsItem            *stats_item);
 
40
static void     up_stats_item_finalize          (GObject                *object);
 
41
 
 
42
#define UP_STATS_ITEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_STATS_ITEM, UpStatsItemPrivate))
 
43
 
 
44
struct UpStatsItemPrivate
 
45
{
 
46
        gdouble                  value;
 
47
        gdouble                  accuracy;
 
48
};
 
49
 
 
50
enum {
 
51
        PROP_0,
 
52
        PROP_VALUE,
 
53
        PROP_ACCURACY,
 
54
        PROP_LAST
 
55
};
 
56
 
 
57
G_DEFINE_TYPE (UpStatsItem, up_stats_item, G_TYPE_OBJECT)
 
58
 
 
59
/**
 
60
 * up_stats_item_set_value:
 
61
 *
 
62
 * Sets the item value.
 
63
 *
 
64
 * Since: 0.9.0
 
65
 **/
 
66
void
 
67
up_stats_item_set_value (UpStatsItem *stats_item, gdouble value)
 
68
{
 
69
        g_return_if_fail (UP_IS_STATS_ITEM (stats_item));
 
70
        stats_item->priv->value = value;
 
71
        g_object_notify (G_OBJECT(stats_item), "value");
 
72
}
 
73
 
 
74
/**
 
75
 * up_stats_item_get_value:
 
76
 *
 
77
 * Gets the item value.
 
78
 *
 
79
 * Since: 0.9.0
 
80
 **/
 
81
gdouble
 
82
up_stats_item_get_value (UpStatsItem *stats_item)
 
83
{
 
84
        g_return_val_if_fail (UP_IS_STATS_ITEM (stats_item), G_MAXDOUBLE);
 
85
        return stats_item->priv->value;
 
86
}
 
87
 
 
88
/**
 
89
 * up_stats_item_set_accuracy:
 
90
 *
 
91
 * Sets the item accuracy.
 
92
 *
 
93
 * Since: 0.9.0
 
94
 **/
 
95
void
 
96
up_stats_item_set_accuracy (UpStatsItem *stats_item, gdouble accuracy)
 
97
{
 
98
        g_return_if_fail (UP_IS_STATS_ITEM (stats_item));
 
99
        stats_item->priv->accuracy = accuracy;
 
100
        g_object_notify (G_OBJECT(stats_item), "accuracy");
 
101
}
 
102
 
 
103
/**
 
104
 * up_stats_item_get_accuracy:
 
105
 *
 
106
 * Gets the item accuracy.
 
107
 *
 
108
 * Since: 0.9.0
 
109
 **/
 
110
gdouble
 
111
up_stats_item_get_accuracy (UpStatsItem *stats_item)
 
112
{
 
113
        g_return_val_if_fail (UP_IS_STATS_ITEM (stats_item), G_MAXDOUBLE);
 
114
        return stats_item->priv->accuracy;
 
115
}
 
116
 
 
117
/**
 
118
 * up_stats_item_set_property:
 
119
 **/
 
120
static void
 
121
up_stats_item_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
 
122
{
 
123
        UpStatsItem *stats_item = UP_STATS_ITEM (object);
 
124
 
 
125
        switch (prop_id) {
 
126
        case PROP_VALUE:
 
127
                stats_item->priv->value = g_value_get_double (value);
 
128
                break;
 
129
        case PROP_ACCURACY:
 
130
                stats_item->priv->accuracy = g_value_get_double (value);
 
131
                break;
 
132
        default:
 
133
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
134
                break;
 
135
        }
 
136
}
 
137
 
 
138
/**
 
139
 * up_stats_item_get_property:
 
140
 **/
 
141
static void
 
142
up_stats_item_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
 
143
{
 
144
        UpStatsItem *stats_item = UP_STATS_ITEM (object);
 
145
 
 
146
        switch (prop_id) {
 
147
        case PROP_VALUE:
 
148
                g_value_set_double (value, stats_item->priv->value);
 
149
                break;
 
150
        case PROP_ACCURACY:
 
151
                g_value_set_double (value, stats_item->priv->accuracy);
 
152
                break;
 
153
        default:
 
154
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
155
                break;
 
156
    }
 
157
}
 
158
 
 
159
/**
 
160
 * up_stats_item_class_init:
 
161
 * @klass: The UpStatsItemClass
 
162
 **/
 
163
static void
 
164
up_stats_item_class_init (UpStatsItemClass *klass)
 
165
{
 
166
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
167
        object_class->finalize = up_stats_item_finalize;
 
168
        object_class->set_property = up_stats_item_set_property;
 
169
        object_class->get_property = up_stats_item_get_property;
 
170
 
 
171
        /**
 
172
         * UpStatsItem:value:
 
173
         *
 
174
         * Since: 0.9.0
 
175
         **/
 
176
        g_object_class_install_property (object_class,
 
177
                                         PROP_VALUE,
 
178
                                         g_param_spec_double ("value", NULL, NULL,
 
179
                                                              0.0, G_MAXDOUBLE, 0.0,
 
180
                                                              G_PARAM_READWRITE));
 
181
        /**
 
182
         * UpStatsItem:accuracy:
 
183
         *
 
184
         * Since: 0.9.0
 
185
         **/
 
186
        g_object_class_install_property (object_class,
 
187
                                         PROP_ACCURACY,
 
188
                                         g_param_spec_double ("accuracy", NULL, NULL,
 
189
                                                              0.0, G_MAXDOUBLE, 0.0,
 
190
                                                              G_PARAM_READWRITE));
 
191
 
 
192
        g_type_class_add_private (klass, sizeof (UpStatsItemPrivate));
 
193
}
 
194
 
 
195
/**
 
196
 * up_stats_item_init:
 
197
 * @stats_item: This class instance
 
198
 **/
 
199
static void
 
200
up_stats_item_init (UpStatsItem *stats_item)
 
201
{
 
202
        stats_item->priv = UP_STATS_ITEM_GET_PRIVATE (stats_item);
 
203
        stats_item->priv->value = 0.0f;
 
204
        stats_item->priv->accuracy = 0.0f;
 
205
}
 
206
 
 
207
/**
 
208
 * up_stats_item_finalize:
 
209
 * @object: The object to finalize
 
210
 **/
 
211
static void
 
212
up_stats_item_finalize (GObject *object)
 
213
{
 
214
        g_return_if_fail (UP_IS_STATS_ITEM (object));
 
215
        G_OBJECT_CLASS (up_stats_item_parent_class)->finalize (object);
 
216
}
 
217
 
 
218
/**
 
219
 * up_stats_item_new:
 
220
 *
 
221
 * Return value: a new UpStatsItem object.
 
222
 *
 
223
 * Since: 0.9.0
 
224
 **/
 
225
UpStatsItem *
 
226
up_stats_item_new (void)
 
227
{
 
228
        UpStatsItem *stats_item;
 
229
        stats_item = g_object_new (UP_TYPE_STATS_ITEM, NULL);
 
230
        return UP_STATS_ITEM (stats_item);
 
231
}
 
232