~tristan957/harvest-almanac

ref: 7bb9d430e827c8a5c2410d0873024b18f4c1394a harvest-almanac/harvest-glib/harvest-response.c -rw-r--r-- 2.9 KiB
7bb9d430Tristan Partin harvest-glib: made use of more enums in GValus 1 year, 10 months ago
                                                                                
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
#include "config.h"

#define G_LOG_DOMAIN "HarvestResponse"

#include <glib-object.h>
#include <glib/gi18n-lib.h>

#include "harvest-enum-types.h"
#include "harvest-http.h"
#include "harvest-response.h"

G_DEFINE_TYPE(HarvestResponse, harvest_response, G_TYPE_OBJECT)

enum HarvestResponseProps
{
	PROP_0,
	PROP_BODY,
	PROP_ERROR,
	PROP_STATUS_CODE,
	N_PROPS,
};

static GParamSpec *obj_properties[N_PROPS];

static void
harvest_response_finalize(GObject *obj)
{
	HarvestResponse *self = HARVEST_RESPONSE(obj);

	if (self->body != NULL)
		g_value_unset(self->body);
	if (self->err != NULL)
		g_error_free(self->err);

	G_OBJECT_CLASS(harvest_response_parent_class)->finalize(obj);
}

static void
harvest_response_get_property(GObject *obj, guint prop_id, GValue *val, GParamSpec *pspec)
{
	HarvestResponse *self = HARVEST_RESPONSE(obj);

	switch (prop_id) {
	case PROP_BODY:
		g_value_set_boxed(val, self->body);
		break;
	case PROP_ERROR:
		g_value_set_boxed(val, self->err);
		break;
	case PROP_STATUS_CODE:
		g_value_set_enum(val, (gint) self->status_code);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
harvest_response_set_property(GObject *obj, guint prop_id, const GValue *val, GParamSpec *pspec)
{
	HarvestResponse *self = HARVEST_RESPONSE(obj);

	switch (prop_id) {
	case PROP_BODY:
		if (self->body != NULL)
			g_value_unset(self->body);
		self->body = g_value_dup_boxed(val);
		break;
	case PROP_ERROR:
		if (self->err != NULL)
			g_error_free(self->err);
		self->err = g_value_dup_boxed(val);
		break;
	case PROP_STATUS_CODE:
		self->status_code = (HttpStatusCode) g_value_get_enum(val);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
harvest_response_class_init(HarvestResponseClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize		= harvest_response_finalize;
	obj_class->get_property = harvest_response_get_property;
	obj_class->set_property = harvest_response_set_property;

	obj_properties[PROP_BODY] = g_param_spec_boxed("body", _("Body"), _("Response body."),
		G_TYPE_VALUE, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_ERROR]
		= g_param_spec_boxed("error", _("Error"), _("Why the request errored out."), G_TYPE_ERROR,
			G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_STATUS_CODE] = g_param_spec_enum("status-code", _("Status Code"),
		_("Status code the response came back with."), HTTP_TYPE_STATUS_CODE, HTTP_STATUS_OK,
		G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPS, obj_properties);
}

static void
harvest_response_init(G_GNUC_UNUSED HarvestResponse *self)
{}

HarvestResponse *
harvest_response_new(GValue *body, const HttpStatusCode code, GError *err)
{
	return g_object_new(
		HARVEST_TYPE_RESPONSE, "body", body, "status-code", code, "error", err, NULL);
}