~ubuntu-branches/ubuntu/oneiric/almanah/oneiric

« back to all changes in this revision

Viewing changes to src/definition.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
#include <glib/gi18n.h>
 
22
#include <math.h>
 
23
#include <string.h>
 
24
 
 
25
#include "definition.h"
 
26
#include "definition-builtins.h"
 
27
 
 
28
typedef struct {
 
29
        AlmanahDefinitionType type_id;
 
30
        GType (*type_function) (void);
 
31
} DefinitionType;
 
32
 
 
33
/* TODO: This is still a little hacky */
 
34
 
 
35
#include "file.h"
 
36
#include "note.h"
 
37
#include "uri.h"
 
38
 
 
39
const DefinitionType definition_types[] = {
 
40
        { ALMANAH_DEFINITION_FILE, almanah_file_definition_get_type },
 
41
        { ALMANAH_DEFINITION_NOTE, almanah_note_definition_get_type },
 
42
        { ALMANAH_DEFINITION_URI, almanah_uri_definition_get_type }
 
43
};
 
44
 
 
45
static void almanah_definition_init (AlmanahDefinition *self);
 
46
static void almanah_definition_finalize (GObject *object);
 
47
static void almanah_definition_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
 
48
static void almanah_definition_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
 
49
 
 
50
struct _AlmanahDefinitionPrivate {
 
51
        gchar *text;
 
52
        gchar *value;
 
53
        gchar *value2;
 
54
};
 
55
 
 
56
enum {
 
57
        PROP_TYPE_ID = 1,
 
58
        PROP_NAME,
 
59
        PROP_DESCRIPTION,
 
60
        PROP_ICON_NAME,
 
61
        PROP_TEXT,
 
62
        PROP_VALUE,
 
63
        PROP_VALUE2
 
64
};
 
65
 
 
66
G_DEFINE_ABSTRACT_TYPE (AlmanahDefinition, almanah_definition, G_TYPE_OBJECT)
 
67
#define ALMANAH_DEFINITION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ALMANAH_TYPE_DEFINITION, AlmanahDefinitionPrivate))
 
68
 
 
69
static void
 
70
almanah_definition_class_init (AlmanahDefinitionClass *klass)
 
71
{
 
72
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
73
 
 
74
        g_type_class_add_private (klass, sizeof (AlmanahDefinitionPrivate));
 
75
 
 
76
        gobject_class->set_property = almanah_definition_set_property;
 
77
        gobject_class->get_property = almanah_definition_get_property;
 
78
        gobject_class->finalize = almanah_definition_finalize;
 
79
 
 
80
        g_object_class_install_property (gobject_class, PROP_TYPE_ID,
 
81
                                g_param_spec_enum ("type-id",
 
82
                                        "Type ID", "The type ID of this definition.",
 
83
                                        ALMANAH_TYPE_DEFINITION_TYPE, ALMANAH_DEFINITION_UNKNOWN,
 
84
                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
85
        g_object_class_install_property (gobject_class, PROP_NAME,
 
86
                                g_param_spec_string ("name",
 
87
                                        "Name", "The human-readable name for this definition type.",
 
88
                                        NULL,
 
89
                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
90
        g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
 
91
                                g_param_spec_string ("description",
 
92
                                        "Description", "The human-readable description for this definition type.",
 
93
                                        NULL,
 
94
                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
95
        g_object_class_install_property (gobject_class, PROP_ICON_NAME,
 
96
                                g_param_spec_string ("icon-name",
 
97
                                        "Icon Name", "The icon name for this definition type.",
 
98
                                        NULL,
 
99
                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
100
        g_object_class_install_property (gobject_class, PROP_TEXT,
 
101
                                g_param_spec_string ("text",
 
102
                                        "Text", "The text this definition defines.",
 
103
                                        NULL,
 
104
                                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
105
        g_object_class_install_property (gobject_class, PROP_VALUE,
 
106
                                g_param_spec_string ("value",
 
107
                                        "Value", "The first value of this definition.",
 
108
                                        NULL,
 
109
                                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
110
        g_object_class_install_property (gobject_class, PROP_VALUE2,
 
111
                                g_param_spec_string ("value2",
 
112
                                        "Value 2", "The second value of this definition.",
 
113
                                        NULL,
 
114
                                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
115
}
 
116
 
 
117
static void
 
118
almanah_definition_init (AlmanahDefinition *self)
 
119
{
 
120
        self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_DEFINITION, AlmanahDefinitionPrivate);
 
121
}
 
122
 
 
123
static void
 
124
almanah_definition_finalize (GObject *object)
 
125
{
 
126
        AlmanahDefinitionPrivate *priv = ALMANAH_DEFINITION (object)->priv;
 
127
 
 
128
        g_free (priv->value);
 
129
        g_free (priv->value2);
 
130
 
 
131
        /* Chain up to the parent class */
 
132
        G_OBJECT_CLASS (almanah_definition_parent_class)->finalize (object);
 
133
}
 
134
 
 
135
static void
 
136
almanah_definition_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
 
137
{
 
138
        AlmanahDefinitionPrivate *priv = ALMANAH_DEFINITION (object)->priv;
 
139
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (object);
 
140
 
 
141
        switch (property_id) {
 
142
                case PROP_TYPE_ID:
 
143
                        g_value_set_enum (value, klass->type_id);
 
144
                        break;
 
145
                case PROP_NAME:
 
146
                        g_value_set_string (value, klass->name);
 
147
                        break;
 
148
                case PROP_DESCRIPTION:
 
149
                        g_value_set_string (value, klass->description);
 
150
                        break;
 
151
                case PROP_ICON_NAME:
 
152
                        g_value_set_string (value, klass->icon_name);
 
153
                        break;
 
154
                case PROP_VALUE:
 
155
                        g_value_set_string (value, priv->value);
 
156
                        break;
 
157
                case PROP_VALUE2:
 
158
                        g_value_set_string (value, priv->value2);
 
159
                        break;
 
160
                default:
 
161
                        /* We don't have any other property... */
 
162
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
163
                        break;
 
164
        }
 
165
}
 
166
 
 
167
static void
 
168
almanah_definition_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
 
169
{
 
170
        AlmanahDefinition *self = ALMANAH_DEFINITION (object);
 
171
 
 
172
        switch (property_id) {
 
173
                case PROP_VALUE:
 
174
                        almanah_definition_set_value (self, g_value_get_string (value));
 
175
                        break;
 
176
                case PROP_VALUE2:
 
177
                        almanah_definition_set_value2 (self, g_value_get_string (value));
 
178
                        break;
 
179
                default:
 
180
                        /* We don't have any other property... */
 
181
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
182
                        break;
 
183
        }
 
184
}
 
185
 
 
186
AlmanahDefinition *
 
187
almanah_definition_new (AlmanahDefinitionType type_id)
 
188
{
 
189
        guint i;
 
190
 
 
191
        for (i = 0; i < G_N_ELEMENTS (definition_types); i++) {
 
192
                if (definition_types[i].type_id == type_id)
 
193
                        return g_object_new (definition_types[i].type_function (), NULL);
 
194
        }
 
195
 
 
196
        return NULL;
 
197
}
 
198
 
 
199
AlmanahDefinitionType
 
200
almanah_definition_get_type_id (AlmanahDefinition *self)
 
201
{
 
202
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
203
        return klass->type_id;
 
204
}
 
205
 
 
206
const gchar *
 
207
almanah_definition_get_name (AlmanahDefinition *self)
 
208
{
 
209
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
210
        return klass->name;
 
211
}
 
212
 
 
213
const gchar *
 
214
almanah_definition_get_description (AlmanahDefinition *self)
 
215
{
 
216
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
217
        return klass->description;
 
218
}
 
219
 
 
220
const gchar *
 
221
almanah_definition_get_icon_name (AlmanahDefinition *self)
 
222
{
 
223
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
224
        return klass->icon_name;
 
225
}
 
226
 
 
227
gboolean
 
228
almanah_definition_view (AlmanahDefinition *self)
 
229
{
 
230
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
231
        g_assert (klass->view != NULL);
 
232
        return klass->view (self);
 
233
}
 
234
 
 
235
void
 
236
almanah_definition_build_dialog (AlmanahDefinition *self, GtkVBox *parent_vbox)
 
237
{
 
238
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
239
        g_assert (klass->build_dialog != NULL);
 
240
        return klass->build_dialog (self, parent_vbox);
 
241
}
 
242
 
 
243
void
 
244
almanah_definition_close_dialog (AlmanahDefinition *self, GtkVBox *parent_vbox)
 
245
{
 
246
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
247
        g_assert (klass->close_dialog != NULL);
 
248
        return klass->close_dialog (self, parent_vbox);
 
249
}
 
250
 
 
251
void
 
252
almanah_definition_parse_text (AlmanahDefinition *self, const gchar *text)
 
253
{
 
254
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
255
 
 
256
        almanah_definition_set_text (self, text);
 
257
 
 
258
        g_assert (klass->parse_text != NULL);
 
259
        return klass->parse_text (self, text);
 
260
}
 
261
 
 
262
gchar *
 
263
almanah_definition_get_blurb (AlmanahDefinition *self)
 
264
{
 
265
        AlmanahDefinitionClass *klass = ALMANAH_DEFINITION_GET_CLASS (self);
 
266
        g_assert (klass->get_blurb != NULL);
 
267
        return klass->get_blurb (self);
 
268
}
 
269
 
 
270
const gchar *
 
271
almanah_definition_get_text (AlmanahDefinition *self)
 
272
{
 
273
        return self->priv->text;
 
274
}
 
275
 
 
276
void
 
277
almanah_definition_set_text (AlmanahDefinition *self, const gchar *text)
 
278
{
 
279
        g_free (self->priv->text);
 
280
        self->priv->text = g_strdup (text);
 
281
        g_object_notify (G_OBJECT (self), "text");
 
282
}
 
283
 
 
284
const gchar *
 
285
almanah_definition_get_value (AlmanahDefinition *self)
 
286
{
 
287
        return self->priv->value;
 
288
}
 
289
 
 
290
void
 
291
almanah_definition_set_value (AlmanahDefinition *self, const gchar *value)
 
292
{
 
293
        g_free (self->priv->value);
 
294
        self->priv->value = g_strdup (value);
 
295
        g_object_notify (G_OBJECT (self), "value");
 
296
}
 
297
 
 
298
const gchar *
 
299
almanah_definition_get_value2 (AlmanahDefinition *self)
 
300
{
 
301
        return self->priv->value2;
 
302
}
 
303
 
 
304
void
 
305
almanah_definition_set_value2 (AlmanahDefinition *self, const gchar *value)
 
306
{
 
307
        g_free (self->priv->value2);
 
308
        self->priv->value2 = g_strdup (value);
 
309
        g_object_notify (G_OBJECT (self), "value2");
 
310
}
 
311
 
 
312
void
 
313
almanah_definition_populate_model (GtkListStore *list_store, guint type_id_column, guint name_column, guint icon_name_column)
 
314
{
 
315
        GtkTreeIter iter;
 
316
        guint i;
 
317
 
 
318
        for (i = 0; i < G_N_ELEMENTS (definition_types); i++) {
 
319
                AlmanahDefinition *definition = g_object_new (definition_types[i].type_function (), NULL);
 
320
 
 
321
                if (definition == NULL)
 
322
                        continue;
 
323
 
 
324
                gtk_list_store_append (list_store, &iter);
 
325
                gtk_list_store_set (list_store, &iter,
 
326
                                    type_id_column, definition_types[i].type_id,
 
327
                                    name_column, almanah_definition_get_name (definition),
 
328
                                    icon_name_column, almanah_definition_get_icon_name (definition),
 
329
                                    -1);
 
330
        }
 
331
}