Debian Codegraph @

lib/packagekit-glib2/pk-item-progress.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2012 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more item_progress.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; 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:pk-item-progress
24 * @short_description: ItemProgress object
25 *
26 * This GObject represents a item_progress from a transaction.
27 * These objects represent single items of data from the transaction, and are
28 * often present in lists (#PkResults) or just refcounted in client programs.
29 */
30
31 #include "config.h"
32
33 #include <glib-object.h>
34
35 #include <packagekit-glib2/pk-item-progress.h>
36 #include <packagekit-glib2/pk-enum.h>
37
38 static void pk_item_progress_finalize (GObject *object);
39
40 /**
41 * PkItemProgressPrivate:
42 *
43 * Private #PkItemProgress data
44 **/
45 struct _PkItemProgressPrivate
46 {
47 gchar *package_id;
48 PkStatusEnum status;
49 guint percentage;
50 };
51
52 enum {
53 PROP_0,
54 PROP_PACKAGE_ID,
55 PROP_STATUS,
56 PROP_PERCENTAGE,
57 PROP_LAST
58 };
59
60 G_DEFINE_TYPE_WITH_PRIVATE (PkItemProgress, pk_item_progress, PK_TYPE_SOURCE)
61 #define GET_PRIVATE(o) (pk_item_progress_get_instance_private (o))
62
63 /**
64 * pk_item_progress_get_status:
65 * @item_progress: a valid #PkItemProgress instance
66 *
67 * Get the status of this item.
68 *
69 * Return value: a #PkStatusEnum
70 **/
71 PkStatusEnum
72 pk_item_progress_get_status (PkItemProgress *item_progress)
73 {
74 PkItemProgressPrivate *priv = GET_PRIVATE(item_progress);
75
76 g_return_val_if_fail (PK_IS_ITEM_PROGRESS (item_progress), PK_STATUS_ENUM_UNKNOWN);
77
78 return priv->status;
79 }
80
81 /**
82 * pk_item_progress_get_percentage:
83 * @item_progress: a valid #PkItemProgress instance
84 *
85 * Get the percentage complete of this item.
86 *
87 * Return value: a progress percentage (0-100)
88 **/
89 guint
90 pk_item_progress_get_percentage (PkItemProgress *item_progress)
91 {
92 PkItemProgressPrivate *priv = GET_PRIVATE(item_progress);
93
94 g_return_val_if_fail (PK_IS_ITEM_PROGRESS (item_progress), 0);
95
96 return priv->percentage;
97 }
98
99 /**
100 * pk_item_progress_get_package_id:
101 * @item_progress: a valid #PkItemProgress instance
102 *
103 * Get the package ID this item is working on.
104 *
105 * Return value: a package ID
106 **/
107 const gchar *
108 pk_item_progress_get_package_id (PkItemProgress *item_progress)
109 {
110 PkItemProgressPrivate *priv = GET_PRIVATE(item_progress);
111
112 g_return_val_if_fail (PK_IS_ITEM_PROGRESS (item_progress), NULL);
113
114 return priv->package_id;
115 }
116
117 /*
118 * pk_item_progress_get_property:
119 **/
120 static void
121 pk_item_progress_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
122 {
123 PkItemProgress *item_progress = PK_ITEM_PROGRESS (object);
124 PkItemProgressPrivate *priv = GET_PRIVATE(item_progress);
125
126 switch (prop_id) {
127 case PROP_PACKAGE_ID:
128 g_value_set_string (value, priv->package_id);
129 break;
130 case PROP_PERCENTAGE:
131 g_value_set_uint (value, priv->percentage);
132 break;
133 case PROP_STATUS:
134 g_value_set_uint (value, priv->status);
135 break;
136 default:
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 break;
139 }
140 }
141
142 /*
143 * pk_item_progress_set_property:
144 **/
145 static void
146 pk_item_progress_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
147 {
148 PkItemProgress *item_progress = PK_ITEM_PROGRESS (object);
149 PkItemProgressPrivate *priv = GET_PRIVATE(item_progress);
150
151 switch (prop_id) {
152 case PROP_PACKAGE_ID:
153 g_free (priv->package_id);
154 priv->package_id = g_value_dup_string (value);
155 break;
156 case PROP_STATUS:
157 priv->status = g_value_get_uint (value);
158 break;
159 case PROP_PERCENTAGE:
160 priv->percentage = g_value_get_uint (value);
161 break;
162 default:
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164 break;
165 }
166 }
167
168 /*
169 * pk_item_progress_class_init:
170 **/
171 static void
172 pk_item_progress_class_init (PkItemProgressClass *klass)
173 {
174 GParamSpec *pspec;
175 GObjectClass *object_class = G_OBJECT_CLASS (klass);
176 object_class->finalize = pk_item_progress_finalize;
177 object_class->get_property = pk_item_progress_get_property;
178 object_class->set_property = pk_item_progress_set_property;
179
180 /**
181 * PkItemProgress:package-id:
182 *
183 * Since: 0.8.1
184 */
185 pspec = g_param_spec_string ("package-id", NULL, NULL,
186 NULL,
187 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
188 g_object_class_install_property (object_class, PROP_PACKAGE_ID, pspec);
189
190 /**
191 * PkItemProgress:status:
192 *
193 * Since: 0.8.2
194 */
195 pspec = g_param_spec_uint ("status", NULL, NULL,
196 0, G_MAXUINT, 0,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
198 g_object_class_install_property (object_class, PROP_STATUS, pspec);
199
200 /**
201 * PkItemProgress:percentage:
202 *
203 * Since: 0.8.1
204 */
205 pspec = g_param_spec_uint ("percentage", NULL, NULL,
206 0, G_MAXUINT, 0,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
208 g_object_class_install_property (object_class, PROP_PERCENTAGE, pspec);
209 }
210
211 /*
212 * pk_item_progress_init:
213 **/
214 static void
215 pk_item_progress_init (PkItemProgress *item_progress)
216 {
217 item_progress->priv = GET_PRIVATE(item_progress);
218 }
219
220 /*
221 * pk_item_progress_finalize:
222 **/
223 static void
224 pk_item_progress_finalize (GObject *object)
225 {
226 PkItemProgress *item_progress = PK_ITEM_PROGRESS (object);
227 PkItemProgressPrivate *priv = GET_PRIVATE(item_progress);
228
229 g_clear_pointer (&priv->package_id, g_free);
230
231 G_OBJECT_CLASS (pk_item_progress_parent_class)->finalize (object);
232 }
233
234 /**
235 * pk_item_progress_new:
236 *
237 * An object containing item inside a #PkProgress.
238 *
239 * Return value: a new #PkItemProgress object.
240 *
241 * Since: 0.8.1
242 **/
243 PkItemProgress *
244 pk_item_progress_new (void)
245 {
246 PkItemProgress *item_progress;
247 item_progress = g_object_new (PK_TYPE_ITEM_PROGRESS, NULL);
248 return PK_ITEM_PROGRESS (item_progress);
249 }