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 <glib/gi18n.h>
25
#include "add-definition-dialog.h"
26
#include "definition.h"
27
#include "interface.h"
29
#include "storage-manager.h"
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);
38
struct _AlmanahAddDefinitionDialogPrivate {
39
GtkComboBox *type_combo_box;
41
GtkListStore *type_store;
42
AlmanahDefinition *definition;
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))
54
almanah_add_definition_dialog_class_init (AlmanahAddDefinitionDialogClass *klass)
56
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
58
g_type_class_add_private (klass, sizeof (AlmanahAddDefinitionDialogPrivate));
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;
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.",
69
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
73
almanah_add_definition_dialog_init (AlmanahAddDefinitionDialog *self)
75
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_ADD_DEFINITION_DIALOG, AlmanahAddDefinitionDialogPrivate);
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));
85
almanah_add_definition_dialog_dispose (GObject *object)
87
AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
89
if (priv->definition != NULL)
90
g_object_unref (priv->definition);
91
priv->definition = NULL;
93
/* Chain up to the parent class */
94
G_OBJECT_CLASS (almanah_add_definition_dialog_parent_class)->dispose (object);
98
almanah_add_definition_dialog_finalize (GObject *object)
100
AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
104
/* Chain up to the parent class */
105
G_OBJECT_CLASS (almanah_add_definition_dialog_parent_class)->finalize (object);
109
almanah_add_definition_dialog_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
111
AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
113
switch (property_id) {
115
g_value_set_string (value, priv->text);
118
/* We don't have any other property... */
119
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
125
almanah_add_definition_dialog_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
127
AlmanahAddDefinitionDialog *self = ALMANAH_ADD_DEFINITION_DIALOG (object);
129
switch (property_id) {
131
almanah_add_definition_dialog_set_text (self, g_value_get_string (value));
134
/* We don't have any other property... */
135
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
140
AlmanahAddDefinitionDialog *
141
almanah_add_definition_dialog_new (void)
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",
154
builder = gtk_builder_new ();
156
if (gtk_builder_add_objects_from_file (builder, interface_filename, (gchar**) object_names, &error) == FALSE) {
158
GtkWidget *dialog = gtk_message_dialog_new (NULL,
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);
167
g_error_free (error);
168
g_object_unref (builder);
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);
177
if (add_definition_dialog == NULL) {
178
g_object_unref (builder);
182
priv = add_definition_dialog->priv;
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"));
189
almanah_definition_populate_model (priv->type_store, 1, 0, 2);
190
gtk_combo_box_set_active (priv->type_combo_box, 0);
192
g_object_unref (builder);
194
return add_definition_dialog;
198
destroy_extra_widgets (AlmanahAddDefinitionDialog *self)
200
gtk_container_foreach (GTK_CONTAINER (self->priv->vbox), (GtkCallback) gtk_widget_destroy, NULL);
204
response_cb (GtkDialog *dialog, gint response_id, AlmanahAddDefinitionDialog *self)
206
AlmanahAddDefinitionDialogPrivate *priv = self->priv;
208
/* Save the entered data */
209
g_assert (priv->definition != NULL);
210
almanah_definition_close_dialog (priv->definition, priv->vbox);
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);
218
add_type_combo_box_changed_cb (GtkComboBox *combo_box, AlmanahAddDefinitionDialog *self)
221
AlmanahDefinitionType type_id;
222
AlmanahAddDefinitionDialogPrivate *priv = self->priv;
224
destroy_extra_widgets (self);
226
if (gtk_combo_box_get_active_iter (combo_box, &iter) == FALSE)
228
gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter, 1, &type_id, -1);
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);
235
priv->definition = almanah_definition_new (type_id);
238
almanah_definition_build_dialog (priv->definition, priv->vbox);
239
if (priv->text != NULL)
240
almanah_definition_parse_text (priv->definition, priv->text);
244
almanah_add_definition_dialog_get_text (AlmanahAddDefinitionDialog *self)
246
return self->priv->text;
250
almanah_add_definition_dialog_set_text (AlmanahAddDefinitionDialog *self, const gchar *text)
252
AlmanahAddDefinitionDialogPrivate *priv = self->priv;
253
AlmanahDefinition *definition;
255
g_free (self->priv->text);
256
self->priv->text = g_strdup (text);
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;
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);
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);
275
g_object_notify (G_OBJECT (self), "text");
279
almanah_add_definition_dialog_get_definition (AlmanahAddDefinitionDialog *self)
281
g_assert (self->priv->text != NULL);
282
return self->priv->definition;