~ubuntu-branches/ubuntu/maverick/almanah/maverick

« back to all changes in this revision

Viewing changes to src/event-manager.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2009-05-16 15:49:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090516154921-2uhvlj8btb4qygmd
Tags: 0.6.1-0ubuntu1
* New upstream release (LP: #368078)
* debian/control:
  - Bump Standards-Version to 3.8.1
  - Add intltool, libedataserver1.2-dev, libedataserverui1.2-dev, 
    libecal1.2-dev, libcryptui-dev as build dependency
  - Remove build dependency on autotools-dev and chrpath
* debian/{control/compat/rules}: Move to mimalistic dh7 style
* Update debian/watch
* Update copyright information

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
 * Almanah
 
4
 * Copyright (C) Philip Withnall 2008 <philip@tecnocode.co.uk>
 
5
 * 
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include <glib.h>
 
21
 
 
22
#include "event-manager.h"
 
23
#include "event-factory.h"
 
24
#include "event-factory-builtins.h"
 
25
 
 
26
typedef struct {
 
27
        AlmanahEventFactoryType type_id;
 
28
        GType (*type_function) (void);
 
29
} EventFactoryType;
 
30
 
 
31
/* TODO: This is still a little hacky */
 
32
 
 
33
#include "calendar.h"
 
34
#include "main.h"
 
35
 
 
36
const EventFactoryType event_factory_types[] = {
 
37
        { ALMANAH_EVENT_FACTORY_CALENDAR, almanah_calendar_event_factory_get_type }
 
38
};
 
39
 
 
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);
 
43
 
 
44
struct _AlmanahEventManagerPrivate {
 
45
        AlmanahEventFactory **factories;
 
46
};
 
47
 
 
48
enum {
 
49
        SIGNAL_EVENTS_UPDATED,
 
50
        LAST_SIGNAL
 
51
};
 
52
 
 
53
static guint event_manager_signals[LAST_SIGNAL] = { 0, };
 
54
 
 
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))
 
57
 
 
58
static void
 
59
almanah_event_manager_class_init (AlmanahEventManagerClass *klass)
 
60
{
 
61
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
62
 
 
63
        g_type_class_add_private (klass, sizeof (AlmanahEventManagerPrivate));
 
64
 
 
65
        gobject_class->dispose = almanah_event_manager_dispose;
 
66
 
 
67
        event_manager_signals[SIGNAL_EVENTS_UPDATED] = g_signal_new ("events-updated",
 
68
                                G_TYPE_FROM_CLASS (klass),
 
69
                                G_SIGNAL_RUN_LAST,
 
70
                                0, NULL, NULL,
 
71
                                g_cclosure_marshal_VOID__ENUM,
 
72
                                G_TYPE_NONE, 1, ALMANAH_TYPE_EVENT_FACTORY_TYPE);
 
73
}
 
74
 
 
75
static void
 
76
almanah_event_manager_init (AlmanahEventManager *self)
 
77
{
 
78
        guint i;
 
79
 
 
80
        self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_EVENT_MANAGER, AlmanahEventManagerPrivate);
 
81
 
 
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);
 
87
        }
 
88
        self->priv->factories[i] = NULL;
 
89
}
 
90
 
 
91
static void
 
92
almanah_event_manager_dispose (GObject *object)
 
93
{
 
94
        guint i = 0;
 
95
        AlmanahEventManagerPrivate *priv = ALMANAH_EVENT_MANAGER_GET_PRIVATE (object);
 
96
 
 
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);
 
102
        }
 
103
        priv->factories = NULL;
 
104
 
 
105
        /* Chain up to the parent class */
 
106
        G_OBJECT_CLASS (almanah_event_manager_parent_class)->dispose (object);
 
107
}
 
108
 
 
109
AlmanahEventManager *
 
110
almanah_event_manager_new (void)
 
111
{
 
112
        return g_object_new (ALMANAH_TYPE_EVENT_MANAGER, NULL);
 
113
}
 
114
 
 
115
static void
 
116
events_updated_cb (AlmanahEventFactory *factory, AlmanahEventManager *self)
 
117
{
 
118
        g_signal_emit (self, event_manager_signals[SIGNAL_EVENTS_UPDATED], 0, almanah_event_factory_get_type_id (factory));
 
119
}
 
120
 
 
121
void
 
122
almanah_event_manager_query_events (AlmanahEventManager *self, AlmanahEventFactoryType type_id, GDate *date)
 
123
{
 
124
        AlmanahEventManagerPrivate *priv = ALMANAH_EVENT_MANAGER_GET_PRIVATE (self);
 
125
        guint i;
 
126
 
 
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));
 
129
 
 
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);
 
135
                }
 
136
 
 
137
                return;
 
138
        }
 
139
 
 
140
        /* Otherwise, query all factories */
 
141
        for (i = 0; priv->factories[i] != NULL; i++)
 
142
                almanah_event_factory_query_events (priv->factories[i], date);
 
143
}
 
144
 
 
145
GSList *
 
146
almanah_event_manager_get_events (AlmanahEventManager *self, AlmanahEventFactoryType type_id, GDate *date)
 
147
{
 
148
        AlmanahEventManagerPrivate *priv = ALMANAH_EVENT_MANAGER_GET_PRIVATE (self);
 
149
        GSList *list = NULL, *end = NULL;
 
150
        guint i;
 
151
 
 
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));
 
154
 
 
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);
 
160
                }
 
161
 
 
162
                return NULL;
 
163
        }
 
164
 
 
165
        /* Otherwise, return a concatenation of all factories' events */
 
166
        for (i = 0; priv->factories[i] != NULL; i++) {
 
167
                GSList *end2;
 
168
 
 
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 */
 
171
                end = end2;
 
172
 
 
173
                if (list == NULL)
 
174
                        list = end;
 
175
        }
 
176
 
 
177
        return list;
 
178
}