summaryrefslogtreecommitdiff
path: root/lib/packagekit-glib2/pk-error.c
blob: 901c842ea445c8c002293eb7d56432f0870bb073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

/**
 * SECTION:pk-error
 * @short_description: Error object
 *
 * This GObject represents a error_code from a transaction.
 * These objects represent single items of data from the transaction, and are
 * often present in lists (#PkResults) or just refcounted in client programs.
 */

#include "config.h"

#include <glib-object.h>

#include <packagekit-glib2/pk-error.h>
#include <packagekit-glib2/pk-enum.h>
#include <packagekit-glib2/pk-enum-types.h>

static void     pk_error_finalize	(GObject     *object);

#define PK_ERROR_CODE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PK_TYPE_ERROR_CODE, PkErrorPrivate))

/**
 * PkErrorPrivate:
 *
 * Private #PkError data
 **/
struct _PkErrorPrivate
{
	PkErrorEnum			 code;
	gchar				*details;
};

enum {
	PROP_0,
	PROP_CODE,
	PROP_DETAILS,
	PROP_LAST
};

G_DEFINE_TYPE (PkError, pk_error, PK_TYPE_SOURCE)

/*
 * pk_error_get_property:
 **/
static void
pk_error_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	PkError *error_code = PK_ERROR_CODE (object);
	PkErrorPrivate *priv = error_code->priv;

	switch (prop_id) {
	case PROP_CODE:
		g_value_set_enum (value, priv->code);
		break;
	case PROP_DETAILS:
		g_value_set_string (value, priv->details);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

/*
 * pk_error_set_property:
 **/
static void
pk_error_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	PkError *error_code = PK_ERROR_CODE (object);
	PkErrorPrivate *priv = error_code->priv;

	switch (prop_id) {
	case PROP_CODE:
		priv->code = g_value_get_enum (value);
		break;
	case PROP_DETAILS:
		g_free (priv->details);
		priv->details = g_strdup (g_value_get_string (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

/**
 * pk_error_get_code:
 * @error_code: a valid #PkError instance
 *
 * Get the error code for this error.
 *
 * Return value: a #PkErrorEnum
 *
 * Since: 0.5.5
 **/
PkErrorEnum
pk_error_get_code (PkError *error_code)
{
	g_return_val_if_fail (PK_IS_ERROR_CODE (error_code), 0);
	return error_code->priv->code;
}

/**
 * pk_error_get_details:
 * @error_code: a valid #PkError instance
 *
 * Get details for this error.
 *
 * Return value: an string description
 *
 * Since: 0.5.5
 **/
const gchar *
pk_error_get_details (PkError *error_code)
{
	g_return_val_if_fail (PK_IS_ERROR_CODE (error_code), NULL);
	return error_code->priv->details;
}

/*
 * pk_error_class_init:
 **/
static void
pk_error_class_init (PkErrorClass *klass)
{
	GParamSpec *pspec;
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = pk_error_finalize;
	object_class->get_property = pk_error_get_property;
	object_class->set_property = pk_error_set_property;

	/**
	 * PkError:code:
	 *
	 * Since: 0.5.5
	 */
	pspec = g_param_spec_enum ("code", NULL, NULL,
				   PK_TYPE_ERROR_ENUM, PK_ERROR_ENUM_UNKNOWN,
				   G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_CODE, pspec);

	/**
	 * PkError:details:
	 *
	 * Since: 0.5.5
	 */
	pspec = g_param_spec_string ("details", NULL, NULL,
				     NULL,
				     G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_DETAILS, pspec);

	g_type_class_add_private (klass, sizeof (PkErrorPrivate));
}

/*
 * pk_error_init:
 **/
static void
pk_error_init (PkError *error_code)
{
	error_code->priv = PK_ERROR_CODE_GET_PRIVATE (error_code);
}

/*
 * pk_error_finalize:
 **/
static void
pk_error_finalize (GObject *object)
{
	PkError *error_code = PK_ERROR_CODE (object);
	PkErrorPrivate *priv = error_code->priv;

	g_free (priv->details);

	G_OBJECT_CLASS (pk_error_parent_class)->finalize (object);
}

/**
 * pk_error_new:
 *
 * Return value: a new #PkError object.
 *
 * Since: 0.5.5
 **/
PkError *
pk_error_new (void)
{
	PkError *error_code;
	error_code = g_object_new (PK_TYPE_ERROR_CODE, NULL);
	return PK_ERROR_CODE (error_code);
}