1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
4
* Copyright (C) Philip Withnall 2008 <philip@tecnocode.co.uk>
6
* Almanah is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* Almanah is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with Almanah. If not, see <http://www.gnu.org/licenses/>.
22
#include "event-manager.h"
23
#include "event-factory.h"
24
#include "event-factory-builtins.h"
27
AlmanahEventFactoryType type_id;
28
GType (*type_function) (void);
31
/* TODO: This is still a little hacky */
36
const EventFactoryType event_factory_types[] = {
37
{ ALMANAH_EVENT_FACTORY_CALENDAR, almanah_calendar_event_factory_get_type }
40
static void almanah_event_manager_init (AlmanahEventManager *self);
41
static void almanah_event_manager_dispose (GObject *object);
42
static void events_updated_cb (AlmanahEventFactory *factory, AlmanahEventManager *self);
44
struct _AlmanahEventManagerPrivate {
45
AlmanahEventFactory **factories;
49
SIGNAL_EVENTS_UPDATED,
53
static guint event_manager_signals[LAST_SIGNAL] = { 0, };
55
G_DEFINE_TYPE (AlmanahEventManager, almanah_event_manager, G_TYPE_OBJECT)
56
#define ALMANAH_EVENT_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ALMANAH_TYPE_EVENT_MANAGER, AlmanahEventManagerPrivate))
59
almanah_event_manager_class_init (AlmanahEventManagerClass *klass)
61
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
63
g_type_class_add_private (klass, sizeof (AlmanahEventManagerPrivate));
65
gobject_class->dispose = almanah_event_manager_dispose;
67
event_manager_signals[SIGNAL_EVENTS_UPDATED] = g_signal_new ("events-updated",
68
G_TYPE_FROM_CLASS (klass),
71
g_cclosure_marshal_VOID__ENUM,
72
G_TYPE_NONE, 1, ALMANAH_TYPE_EVENT_FACTORY_TYPE);
76
almanah_event_manager_init (AlmanahEventManager *self)
80
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_EVENT_MANAGER, AlmanahEventManagerPrivate);
82
/* Set up the list of AlmanahEventFactories */
83
self->priv->factories = g_new (AlmanahEventFactory*, G_N_ELEMENTS (event_factory_types) + 1);
84
for (i = 0; i < G_N_ELEMENTS (event_factory_types); i++) {
85
self->priv->factories[i] = g_object_new (event_factory_types[i].type_function (), NULL);
86
g_signal_connect (self->priv->factories[i], "events-updated", G_CALLBACK (events_updated_cb), self);
88
self->priv->factories[i] = NULL;
92
almanah_event_manager_dispose (GObject *object)
95
AlmanahEventManagerPrivate *priv = ALMANAH_EVENT_MANAGER_GET_PRIVATE (object);
97
/* Free the factories */
98
if (priv->factories != NULL) {
99
for (i = 0; priv->factories[i] != NULL; i++)
100
g_object_unref (priv->factories[i]);
101
g_free (priv->factories);
103
priv->factories = NULL;
105
/* Chain up to the parent class */
106
G_OBJECT_CLASS (almanah_event_manager_parent_class)->dispose (object);
109
AlmanahEventManager *
110
almanah_event_manager_new (void)
112
return g_object_new (ALMANAH_TYPE_EVENT_MANAGER, NULL);
116
events_updated_cb (AlmanahEventFactory *factory, AlmanahEventManager *self)
118
g_signal_emit (self, event_manager_signals[SIGNAL_EVENTS_UPDATED], 0, almanah_event_factory_get_type_id (factory));
122
almanah_event_manager_query_events (AlmanahEventManager *self, AlmanahEventFactoryType type_id, GDate *date)
124
AlmanahEventManagerPrivate *priv = ALMANAH_EVENT_MANAGER_GET_PRIVATE (self);
127
if (almanah->debug == TRUE)
128
g_debug ("almanah_event_manager_query_events called for factory %u and date %u-%u-%u.", type_id, g_date_get_year (date), g_date_get_month (date), g_date_get_day (date));
130
if (type_id != ALMANAH_EVENT_FACTORY_UNKNOWN) {
131
/* Just query that factory */
132
for (i = 0; priv->factories[i] != NULL; i++) {
133
if (almanah_event_factory_get_type_id (priv->factories[i]) == type_id)
134
almanah_event_factory_query_events (priv->factories[i], date);
140
/* Otherwise, query all factories */
141
for (i = 0; priv->factories[i] != NULL; i++)
142
almanah_event_factory_query_events (priv->factories[i], date);
146
almanah_event_manager_get_events (AlmanahEventManager *self, AlmanahEventFactoryType type_id, GDate *date)
148
AlmanahEventManagerPrivate *priv = ALMANAH_EVENT_MANAGER_GET_PRIVATE (self);
149
GSList *list = NULL, *end = NULL;
152
if (almanah->debug == TRUE)
153
g_debug ("almanah_event_manager_get_events called for factory %u and date %u-%u-%u.", type_id, g_date_get_year (date), g_date_get_month (date), g_date_get_day (date));
155
if (type_id != ALMANAH_EVENT_FACTORY_UNKNOWN) {
156
/* Just return the events for the specified event factory */
157
for (i = 0; priv->factories[i] != NULL; i++) {
158
if (almanah_event_factory_get_type_id (priv->factories[i]) == type_id)
159
return almanah_event_factory_get_events (priv->factories[i], date);
165
/* Otherwise, return a concatenation of all factories' events */
166
for (i = 0; priv->factories[i] != NULL; i++) {
169
end2 = almanah_event_factory_get_events (priv->factories[i], date);
170
end = g_slist_concat (end, end2); /* assignment's only to shut gcc up */