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

« back to all changes in this revision

Viewing changes to src/add-definition-dialog.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 <config.h>
 
21
#include <glib.h>
 
22
#include <glib/gi18n.h>
 
23
#include <gtk/gtk.h>
 
24
 
 
25
#include "add-definition-dialog.h"
 
26
#include "definition.h"
 
27
#include "interface.h"
 
28
#include "main.h"
 
29
#include "storage-manager.h"
 
30
 
 
31
static void almanah_add_definition_dialog_init (AlmanahAddDefinitionDialog *self);
 
32
static void almanah_add_definition_dialog_dispose (GObject *object);
 
33
static void almanah_add_definition_dialog_finalize (GObject *object);
 
34
static void almanah_add_definition_dialog_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
 
35
static void almanah_add_definition_dialog_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
 
36
static void response_cb (GtkDialog *dialog, gint response_id, AlmanahAddDefinitionDialog *self);
 
37
 
 
38
struct _AlmanahAddDefinitionDialogPrivate {
 
39
        GtkComboBox *type_combo_box;
 
40
        GtkVBox *vbox;
 
41
        GtkListStore *type_store;
 
42
        AlmanahDefinition *definition;
 
43
        gchar *text;
 
44
};
 
45
 
 
46
enum {
 
47
        PROP_TEXT = 1
 
48
};
 
49
 
 
50
G_DEFINE_TYPE (AlmanahAddDefinitionDialog, almanah_add_definition_dialog, GTK_TYPE_DIALOG)
 
51
#define ALMANAH_ADD_DEFINITION_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ALMANAH_TYPE_ADD_DEFINITION_DIALOG, AlmanahAddDefinitionDialogPrivate))
 
52
 
 
53
static void
 
54
almanah_add_definition_dialog_class_init (AlmanahAddDefinitionDialogClass *klass)
 
55
{
 
56
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
57
 
 
58
        g_type_class_add_private (klass, sizeof (AlmanahAddDefinitionDialogPrivate));
 
59
 
 
60
        gobject_class->set_property = almanah_add_definition_dialog_set_property;
 
61
        gobject_class->get_property = almanah_add_definition_dialog_get_property;
 
62
        gobject_class->dispose = almanah_add_definition_dialog_dispose;
 
63
        gobject_class->finalize = almanah_add_definition_dialog_finalize;
 
64
 
 
65
        g_object_class_install_property (gobject_class, PROP_TEXT,
 
66
                                g_param_spec_string ("text",
 
67
                                        "Text", "The text for which this dialog is getting a definition.",
 
68
                                        NULL,
 
69
                                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
70
}
 
71
 
 
72
static void
 
73
almanah_add_definition_dialog_init (AlmanahAddDefinitionDialog *self)
 
74
{
 
75
        self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_ADD_DEFINITION_DIALOG, AlmanahAddDefinitionDialogPrivate);
 
76
 
 
77
        g_signal_connect (self, "response", G_CALLBACK (response_cb), self);
 
78
        gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
 
79
        gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
 
80
        gtk_window_set_title (GTK_WINDOW (self), _("Add Definition"));
 
81
        gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (almanah->main_window));
 
82
}
 
83
 
 
84
static void
 
85
almanah_add_definition_dialog_dispose (GObject *object)
 
86
{
 
87
        AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
 
88
 
 
89
        if (priv->definition != NULL)
 
90
                g_object_unref (priv->definition);
 
91
        priv->definition = NULL;
 
92
 
 
93
        /* Chain up to the parent class */
 
94
        G_OBJECT_CLASS (almanah_add_definition_dialog_parent_class)->dispose (object);
 
95
}
 
96
 
 
97
static void
 
98
almanah_add_definition_dialog_finalize (GObject *object)
 
99
{
 
100
        AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
 
101
 
 
102
        g_free (priv->text);
 
103
 
 
104
        /* Chain up to the parent class */
 
105
        G_OBJECT_CLASS (almanah_add_definition_dialog_parent_class)->finalize (object);
 
106
}
 
107
 
 
108
static void
 
109
almanah_add_definition_dialog_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
 
110
{
 
111
        AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
 
112
 
 
113
        switch (property_id) {
 
114
                case PROP_TEXT:
 
115
                        g_value_set_string (value, priv->text);
 
116
                        break;
 
117
                default:
 
118
                        /* We don't have any other property... */
 
119
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
120
                        break;
 
121
        }
 
122
}
 
123
 
 
124
static void
 
125
almanah_add_definition_dialog_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
 
126
{
 
127
        AlmanahAddDefinitionDialog *self = ALMANAH_ADD_DEFINITION_DIALOG (object);
 
128
 
 
129
        switch (property_id) {
 
130
                case PROP_TEXT:
 
131
                        almanah_add_definition_dialog_set_text (self, g_value_get_string (value));
 
132
                        break;
 
133
                default:
 
134
                        /* We don't have any other property... */
 
135
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
136
                        break;
 
137
        }
 
138
}
 
139
 
 
140
AlmanahAddDefinitionDialog *
 
141
almanah_add_definition_dialog_new (void)
 
142
{
 
143
        GtkBuilder *builder;
 
144
        AlmanahAddDefinitionDialog *add_definition_dialog;
 
145
        AlmanahAddDefinitionDialogPrivate *priv;
 
146
        GError *error = NULL;
 
147
        const gchar *interface_filename = almanah_get_interface_filename ();
 
148
        const gchar *object_names[] = {
 
149
                "almanah_add_definition_dialog",
 
150
                "almanah_add_type_store",
 
151
                NULL
 
152
        };
 
153
 
 
154
        builder = gtk_builder_new ();
 
155
 
 
156
        if (gtk_builder_add_objects_from_file (builder, interface_filename, (gchar**) object_names, &error) == FALSE) {
 
157
                /* Show an error */
 
158
                GtkWidget *dialog = gtk_message_dialog_new (NULL,
 
159
                                GTK_DIALOG_MODAL,
 
160
                                GTK_MESSAGE_ERROR,
 
161
                                GTK_BUTTONS_OK,
 
162
                                _("UI file \"%s\" could not be loaded"), interface_filename);
 
163
                gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
 
164
                gtk_dialog_run (GTK_DIALOG (dialog));
 
165
                gtk_widget_destroy (dialog);
 
166
 
 
167
                g_error_free (error);
 
168
                g_object_unref (builder);
 
169
 
 
170
                return NULL;
 
171
        }
 
172
 
 
173
        gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
 
174
        add_definition_dialog = ALMANAH_ADD_DEFINITION_DIALOG (gtk_builder_get_object (builder, "almanah_add_definition_dialog"));
 
175
        gtk_builder_connect_signals (builder, add_definition_dialog);
 
176
 
 
177
        if (add_definition_dialog == NULL) {
 
178
                g_object_unref (builder);
 
179
                return NULL;
 
180
        }
 
181
 
 
182
        priv = add_definition_dialog->priv;
 
183
 
 
184
        /* Grab our child widgets */
 
185
        priv->type_combo_box = GTK_COMBO_BOX (gtk_builder_get_object (builder, "almanah_add_type_combo_box"));
 
186
        priv->vbox = GTK_VBOX (gtk_builder_get_object (builder, "almanah_add_vbox"));
 
187
        priv->type_store = GTK_LIST_STORE (gtk_builder_get_object (builder, "almanah_add_type_store"));
 
188
 
 
189
        almanah_definition_populate_model (priv->type_store, 1, 0, 2);
 
190
        gtk_combo_box_set_active (priv->type_combo_box, 0);
 
191
 
 
192
        g_object_unref (builder);
 
193
 
 
194
        return add_definition_dialog;
 
195
}
 
196
 
 
197
static void
 
198
destroy_extra_widgets (AlmanahAddDefinitionDialog *self)
 
199
{
 
200
        gtk_container_foreach (GTK_CONTAINER (self->priv->vbox), (GtkCallback) gtk_widget_destroy, NULL);
 
201
}
 
202
 
 
203
static void
 
204
response_cb (GtkDialog *dialog, gint response_id, AlmanahAddDefinitionDialog *self)
 
205
{
 
206
        AlmanahAddDefinitionDialogPrivate *priv = self->priv;
 
207
 
 
208
        /* Save the entered data */
 
209
        g_assert (priv->definition != NULL);
 
210
        almanah_definition_close_dialog (priv->definition, priv->vbox);
 
211
 
 
212
        /* Make sure to remove all the custom widgets for the currently-selected definition type */
 
213
        gtk_widget_hide_all (GTK_WIDGET (dialog));
 
214
        destroy_extra_widgets (self);
 
215
}
 
216
 
 
217
void
 
218
add_type_combo_box_changed_cb (GtkComboBox *combo_box, AlmanahAddDefinitionDialog *self)
 
219
{
 
220
        GtkTreeIter iter;
 
221
        AlmanahDefinitionType type_id;
 
222
        AlmanahAddDefinitionDialogPrivate *priv = self->priv;
 
223
 
 
224
        destroy_extra_widgets (self);
 
225
 
 
226
        if (gtk_combo_box_get_active_iter (combo_box, &iter) == FALSE)
 
227
                return;
 
228
        gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter, 1, &type_id, -1);
 
229
 
 
230
        /* Create a new definition of the correct type if necessary */
 
231
        if (priv->definition == NULL || almanah_definition_get_type_id (priv->definition) != type_id) {
 
232
                if (priv->definition != NULL)
 
233
                        g_object_unref (priv->definition);
 
234
 
 
235
                priv->definition = almanah_definition_new (type_id);
 
236
        }
 
237
 
 
238
        almanah_definition_build_dialog (priv->definition, priv->vbox);
 
239
        if (priv->text != NULL)
 
240
                almanah_definition_parse_text (priv->definition, priv->text);
 
241
}
 
242
 
 
243
const gchar *
 
244
almanah_add_definition_dialog_get_text (AlmanahAddDefinitionDialog *self)
 
245
{
 
246
        return self->priv->text;
 
247
}
 
248
 
 
249
void
 
250
almanah_add_definition_dialog_set_text (AlmanahAddDefinitionDialog *self, const gchar *text)
 
251
{
 
252
        AlmanahAddDefinitionDialogPrivate *priv = self->priv;
 
253
        AlmanahDefinition *definition;
 
254
 
 
255
        g_free (self->priv->text);
 
256
        self->priv->text = g_strdup (text);
 
257
 
 
258
        /* See if a definition for this text already exists... */
 
259
        definition = almanah_storage_manager_get_definition (almanah->storage_manager, text);
 
260
        if (definition != NULL) {
 
261
                /* Use the definition we've got from the DB */
 
262
                if (priv->definition != NULL)
 
263
                        g_object_unref (priv->definition);
 
264
                priv->definition = definition;
 
265
 
 
266
                /* Rebuild the UI so it contains all the data from the new definition */
 
267
                gtk_combo_box_set_active (priv->type_combo_box, -1); /* just to make sure the selection does actually change */
 
268
                gtk_combo_box_set_active (priv->type_combo_box, almanah_definition_get_type_id (definition) - 1);
 
269
        } else {
 
270
                /* Select a default definition type */
 
271
                gtk_combo_box_set_active (priv->type_combo_box, -1); /* just to make sure the selection does actually change */
 
272
                gtk_combo_box_set_active (priv->type_combo_box, 0);
 
273
        }
 
274
 
 
275
        g_object_notify (G_OBJECT (self), "text");
 
276
}
 
277
 
 
278
AlmanahDefinition *
 
279
almanah_add_definition_dialog_get_definition (AlmanahAddDefinitionDialog *self)
 
280
{
 
281
        g_assert (self->priv->text != NULL);
 
282
        return self->priv->definition;
 
283
}