~ubuntu-branches/ubuntu/utopic/almanah/utopic

« back to all changes in this revision

Viewing changes to src/add-definition-dialog.c

  • Committer: Package Import Robot
  • Author(s): Angel Abad
  • Date: 2012-10-29 10:14:18 UTC
  • mfrom: (1.4.1) (10.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20121029101418-k2c27xb0aku22zeg
Tags: 0.10.0-1~exp1
* Imported Upstream version 0.10.0
* debian/control:
  - Bump evolution Build-Depends ( >=3.6.0)
  - debian/control: Depends on evolution-common (>= 3.6.0)
* Bump Standards-Version to 3.9.4 (no changes)

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