~tristan957/harvest-almanac

ref: f48b10b7c275e961a2d5a64bc9346898e8121d4a harvest-almanac/harvest-almanac/hal-context.c -rw-r--r-- 3.2 KiB
f48b10b7Tristan Partin harvest-alamnac: spacing 1 year, 8 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
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
#include "config.h"

#include <gio/gio.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <harvest-glib/harvest.h>

#include "hal-context.h"

HalContext *CONTEXT;

struct _HalContext
{
	GObject parent_instance;

	// The currently logged in user
	HarvestUser *user;
	// The company of the currently logged in user
	HarvestCompany *company;
};

G_DEFINE_TYPE(HalContext, hal_context, G_TYPE_OBJECT)

enum HalContextProps
{
	PROP_0,
	PROP_USER,
	PROP_COMPANY,
	N_PROPS,
};

static GParamSpec *obj_properties[N_PROPS];

static void
hal_context_finalize(GObject *obj)
{
	HalContext *self = HAL_CONTEXT(obj);

	/**
	 * Using g_clear_object here due to the ownership model. CONTEXT will be the last thing to be
	 * garbage collected before the application itself.
	 */
	g_clear_object(&self->user);
	g_clear_object(&self->company);

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

static void
hal_context_get_property(GObject *obj, guint prop_id, GValue *val, GParamSpec *pspec)
{
	HalContext *self = HAL_CONTEXT(obj);

	switch (prop_id) {
	case PROP_USER:
		g_value_set_object(val, self->user);
		break;
	case PROP_COMPANY:
		g_value_set_object(val, self->company);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
hal_context_set_property(GObject *obj, guint prop_id, const GValue *val, GParamSpec *pspec)
{
	HalContext *self = HAL_CONTEXT(obj);

	switch (prop_id) {
	case PROP_USER:
		if (self->user != NULL)
			g_object_unref(self->user);
		self->user = g_value_dup_object(val);
		break;
	case PROP_COMPANY:
		if (self->company != NULL)
			g_object_unref(self->company);
		self->company = g_value_dup_object(val);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
hal_context_class_init(HalContextClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize		= hal_context_finalize;
	obj_class->get_property = hal_context_get_property;
	obj_class->set_property = hal_context_set_property;

	obj_properties[PROP_USER]	 = g_param_spec_object("user", _("User"),
		   _("Currently logged in user of the application."), HARVEST_TYPE_USER,
		   G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_COMPANY] = g_param_spec_object("company", _("Company"),
		_("Company of the currently logged in user."), HARVEST_TYPE_COMPANY,
		G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPS, obj_properties);
}

static void
hal_context_init(G_GNUC_UNUSED HalContext *self)
{}

void
hal_context_initialize()
{
	CONTEXT = g_object_new(HAL_TYPE_CONTEXT, NULL);
}

void
hal_context_set_user(HarvestUser *user)
{
	g_return_if_fail(CONTEXT != NULL && HARVEST_IS_USER(user));

	CONTEXT->user = user;

	g_object_notify_by_pspec(G_OBJECT(CONTEXT), obj_properties[PROP_USER]);
}

HarvestUser *
hal_context_get_user()
{
	return CONTEXT->user;
}

void
hal_context_set_company(HarvestCompany *company)
{
	g_return_if_fail(CONTEXT != NULL && HARVEST_IS_COMPANY(company));

	CONTEXT->company = company;

	g_object_notify_by_pspec(G_OBJECT(CONTEXT), obj_properties[PROP_COMPANY]);
}

HarvestCompany *
hal_context_get_company()
{
	return CONTEXT->company;
}