1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
4
* Copyright (C) Philip Withnall 2008-2009 <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_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);
37
/* GtkBuilder callbacks */
38
void add_type_combo_box_changed_cb (GtkComboBox *combo_box, AlmanahAddDefinitionDialog *self);
40
struct _AlmanahAddDefinitionDialogPrivate {
41
GtkComboBox *type_combo_box;
43
GtkListStore *type_store;
44
AlmanahDefinition *definition;
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))
56
almanah_add_definition_dialog_class_init (AlmanahAddDefinitionDialogClass *klass)
58
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
60
g_type_class_add_private (klass, sizeof (AlmanahAddDefinitionDialogPrivate));
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;
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.",
71
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
75
almanah_add_definition_dialog_init (AlmanahAddDefinitionDialog *self)
77
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_ADD_DEFINITION_DIALOG, AlmanahAddDefinitionDialogPrivate);
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));
86
almanah_add_definition_dialog_dispose (GObject *object)
88
AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
90
if (priv->definition != NULL)
91
g_object_unref (priv->definition);
92
priv->definition = NULL;
94
/* Chain up to the parent class */
95
G_OBJECT_CLASS (almanah_add_definition_dialog_parent_class)->dispose (object);
99
almanah_add_definition_dialog_finalize (GObject *object)
101
AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
105
/* Chain up to the parent class */
106
G_OBJECT_CLASS (almanah_add_definition_dialog_parent_class)->finalize (object);
110
almanah_add_definition_dialog_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
112
AlmanahAddDefinitionDialogPrivate *priv = ALMANAH_ADD_DEFINITION_DIALOG (object)->priv;
114
switch (property_id) {
116
g_value_set_string (value, priv->text);
119
/* We don't have any other property... */
120
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
126
almanah_add_definition_dialog_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
128
AlmanahAddDefinitionDialog *self = ALMANAH_ADD_DEFINITION_DIALOG (object);
130
switch (property_id) {
132
almanah_add_definition_dialog_set_text (self, g_value_get_string (value));
135
/* We don't have any other property... */
136
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
141
AlmanahAddDefinitionDialog *
142
almanah_add_definition_dialog_new (void)
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",
155
builder = gtk_builder_new ();
157
if (gtk_builder_add_objects_from_file (builder, interface_filename, (gchar**) object_names, &error) == FALSE) {
159
GtkWidget *dialog = gtk_message_dialog_new (NULL,
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);
168
g_error_free (error);
169
g_object_unref (builder);
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);
178
if (add_definition_dialog == NULL) {
179
g_object_unref (builder);
183
priv = add_definition_dialog->priv;
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"));
190
almanah_definition_populate_model (priv->type_store, 1, 0, 2);
191
gtk_combo_box_set_active (priv->type_combo_box, 0);
193
g_object_unref (builder);
195
return add_definition_dialog;
199
destroy_extra_widgets (AlmanahAddDefinitionDialog *self)
201
gtk_container_foreach (GTK_CONTAINER (self->priv->vbox), (GtkCallback) gtk_widget_destroy, NULL);
205
response_cb (GtkDialog *dialog, gint response_id, AlmanahAddDefinitionDialog *self)
207
AlmanahAddDefinitionDialogPrivate *priv = self->priv;
209
/* Save the entered data */
210
g_assert (priv->definition != NULL);
211
almanah_definition_close_dialog (priv->definition, priv->vbox);
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);
219
add_type_combo_box_changed_cb (GtkComboBox *combo_box, AlmanahAddDefinitionDialog *self)
222
AlmanahDefinitionType type_id;
223
AlmanahAddDefinitionDialogPrivate *priv = self->priv;
225
destroy_extra_widgets (self);
227
if (gtk_combo_box_get_active_iter (combo_box, &iter) == FALSE)
229
gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter, 1, &type_id, -1);
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);
236
priv->definition = almanah_definition_new (type_id);
239
almanah_definition_build_dialog (priv->definition, priv->vbox);
240
if (priv->text != NULL)
241
almanah_definition_parse_text (priv->definition, priv->text);
245
almanah_add_definition_dialog_get_text (AlmanahAddDefinitionDialog *self)
247
return self->priv->text;
251
almanah_add_definition_dialog_set_text (AlmanahAddDefinitionDialog *self, const gchar *text)
253
AlmanahAddDefinitionDialogPrivate *priv = self->priv;
254
AlmanahDefinition *definition;
256
g_free (self->priv->text);
257
self->priv->text = g_strdup (text);
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;
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);
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);
276
g_object_notify (G_OBJECT (self), "text");
280
almanah_add_definition_dialog_get_definition (AlmanahAddDefinitionDialog *self)
282
g_assert (self->priv->text != NULL);
283
return self->priv->definition;