Debian Codegraph @

lib/packagekit-glib2/pk-error.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2009 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 details.
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-error
24 * @short_description: Error object
25 *
26 * This GObject represents a error_code 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-error.h>
36 #include <packagekit-glib2/pk-enum.h>
37 #include <packagekit-glib2/pk-enum-types.h>
38
39 static void pk_error_finalize (GObject *object);
40
41 /**
42 * PkErrorPrivate:
43 *
44 * Private #PkError data
45 **/
46 struct _PkErrorPrivate
47 {
48 PkErrorEnum code;
49 gchar *details;
50 };
51
52 enum {
53 PROP_0,
54 PROP_CODE,
55 PROP_DETAILS,
56 PROP_LAST
57 };
58
59 G_DEFINE_TYPE_WITH_PRIVATE (PkError, pk_error, PK_TYPE_SOURCE)
60 #define GET_PRIVATE(o) (pk_error_get_instance_private (o))
61
62 /*
63 * pk_error_get_property:
64 **/
65 static void
66 pk_error_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
67 {
68 PkError *error_code = PK_ERROR_CODE (object);
69 PkErrorPrivate *priv = GET_PRIVATE(error_code);
70
71 switch (prop_id) {
72 case PROP_CODE:
73 g_value_set_enum (value, priv->code);
74 break;
75 case PROP_DETAILS:
76 g_value_set_string (value, priv->details);
77 break;
78 default:
79 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
80 break;
81 }
82 }
83
84 /*
85 * pk_error_set_property:
86 **/
87 static void
88 pk_error_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
89 {
90 PkError *error_code = PK_ERROR_CODE (object);
91 PkErrorPrivate *priv = GET_PRIVATE(error_code);
92
93 switch (prop_id) {
94 case PROP_CODE:
95 priv->code = g_value_get_enum (value);
96 break;
97 case PROP_DETAILS:
98 g_free (priv->details);
99 priv->details = g_value_dup_string (value);
100 break;
101 default:
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103 break;
104 }
105 }
106
107 /**
108 * pk_error_get_code:
109 * @error_code: a valid #PkError instance
110 *
111 * Get the error code for this error.
112 *
113 * Return value: a #PkErrorEnum
114 *
115 * Since: 0.5.5
116 **/
117 PkErrorEnum
118 pk_error_get_code (PkError *error_code)
119 {
120 PkErrorPrivate *priv = GET_PRIVATE(error_code);
121
122 g_return_val_if_fail (PK_IS_ERROR_CODE (error_code), 0);
123
124 return priv->code;
125 }
126
127 /**
128 * pk_error_get_details:
129 * @error_code: a valid #PkError instance
130 *
131 * Get details for this error.
132 *
133 * Return value: an string description
134 *
135 * Since: 0.5.5
136 **/
137 const gchar *
138 pk_error_get_details (PkError *error_code)
139 {
140 PkErrorPrivate *priv = GET_PRIVATE(error_code);
141
142 g_return_val_if_fail (PK_IS_ERROR_CODE (error_code), NULL);
143
144 return priv->details;
145 }
146
147 /*
148 * pk_error_class_init:
149 **/
150 static void
151 pk_error_class_init (PkErrorClass *klass)
152 {
153 GParamSpec *pspec;
154 GObjectClass *object_class = G_OBJECT_CLASS (klass);
155 object_class->finalize = pk_error_finalize;
156 object_class->get_property = pk_error_get_property;
157 object_class->set_property = pk_error_set_property;
158
159 /**
160 * PkError:code:
161 *
162 * Since: 0.5.5
163 */
164 pspec = g_param_spec_enum ("code", NULL, NULL,
165 PK_TYPE_ERROR_ENUM, PK_ERROR_ENUM_UNKNOWN,
166 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
167 g_object_class_install_property (object_class, PROP_CODE, pspec);
168
169 /**
170 * PkError:details:
171 *
172 * Since: 0.5.5
173 */
174 pspec = g_param_spec_string ("details", NULL, NULL,
175 NULL,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
177 g_object_class_install_property (object_class, PROP_DETAILS, pspec);
178 }
179
180 /*
181 * pk_error_init:
182 **/
183 static void
184 pk_error_init (PkError *error_code)
185 {
186 error_code->priv = GET_PRIVATE(error_code);
187 }
188
189 /*
190 * pk_error_finalize:
191 **/
192 static void
193 pk_error_finalize (GObject *object)
194 {
195 PkError *error_code = PK_ERROR_CODE (object);
196 PkErrorPrivate *priv = GET_PRIVATE(error_code);
197
198 g_clear_pointer (&priv->details, g_free);
199
200 G_OBJECT_CLASS (pk_error_parent_class)->finalize (object);
201 }
202
203 /**
204 * pk_error_new:
205 *
206 * Return value: a new #PkError object.
207 *
208 * Since: 0.5.5
209 **/
210 PkError *
211 pk_error_new (void)
212 {
213 PkError *error_code;
214 error_code = g_object_new (PK_TYPE_ERROR_CODE, NULL);
215 return PK_ERROR_CODE (error_code);
216 }