1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
4
* Copyright (C) Philip Withnall 2011 <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 "uri-entry-dialog.h"
26
#include "interface.h"
28
static void almanah_uri_entry_dialog_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
29
static void almanah_uri_entry_dialog_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
31
/* GtkBuilder callbacks */
32
G_MODULE_EXPORT void ued_uri_entry_notify_text_cb (GObject *gobject, GParamSpec *pspec, AlmanahUriEntryDialog *self);
34
struct _AlmanahUriEntryDialogPrivate {
44
G_DEFINE_TYPE (AlmanahUriEntryDialog, almanah_uri_entry_dialog, GTK_TYPE_DIALOG)
45
#define ALMANAH_URI_ENTRY_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ALMANAH_TYPE_URI_ENTRY_DIALOG, AlmanahUriEntryDialogPrivate))
48
almanah_uri_entry_dialog_class_init (AlmanahUriEntryDialogClass *klass)
50
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
52
g_type_class_add_private (klass, sizeof (AlmanahUriEntryDialogPrivate));
54
gobject_class->set_property = almanah_uri_entry_dialog_set_property;
55
gobject_class->get_property = almanah_uri_entry_dialog_get_property;
57
g_object_class_install_property (gobject_class, PROP_URI,
58
g_param_spec_string ("uri",
59
"URI", "The current URI entered in the dialog.",
61
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
65
almanah_uri_entry_dialog_init (AlmanahUriEntryDialog *self)
67
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_URI_ENTRY_DIALOG, AlmanahUriEntryDialogPrivate);
69
g_signal_connect (self, "response", (GCallback) gtk_widget_hide, self);
70
gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
71
gtk_window_set_title (GTK_WINDOW (self), _("Enter URI"));
75
almanah_uri_entry_dialog_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
77
AlmanahUriEntryDialogPrivate *priv = ALMANAH_URI_ENTRY_DIALOG (object)->priv;
79
switch (property_id) {
81
g_value_set_string (value, priv->uri);
84
/* We don't have any other property... */
85
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91
almanah_uri_entry_dialog_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
93
AlmanahUriEntryDialog *self = ALMANAH_URI_ENTRY_DIALOG (object);
95
switch (property_id) {
97
almanah_uri_entry_dialog_set_uri (self, g_value_get_string (value));
100
/* We don't have any other property... */
101
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
106
AlmanahUriEntryDialog *
107
almanah_uri_entry_dialog_new (void)
110
AlmanahUriEntryDialog *uri_entry_dialog;
111
AlmanahUriEntryDialogPrivate *priv;
112
GError *error = NULL;
113
const gchar *interface_filename = almanah_get_interface_filename ();
114
const gchar *object_names[] = {
115
"almanah_uri_entry_dialog",
116
"almanah_ui_manager", /* HACK: work around bgo#672789 */
120
builder = gtk_builder_new ();
122
if (gtk_builder_add_objects_from_file (builder, interface_filename, (gchar**) object_names, &error) == FALSE) {
124
GtkWidget *dialog = gtk_message_dialog_new (NULL,
128
_("UI file \"%s\" could not be loaded"), interface_filename);
129
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
130
gtk_dialog_run (GTK_DIALOG (dialog));
131
gtk_widget_destroy (dialog);
133
g_error_free (error);
134
g_object_unref (builder);
139
gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
140
uri_entry_dialog = ALMANAH_URI_ENTRY_DIALOG (gtk_builder_get_object (builder, "almanah_uri_entry_dialog"));
141
gtk_builder_connect_signals (builder, uri_entry_dialog);
143
if (uri_entry_dialog == NULL) {
144
g_object_unref (builder);
148
priv = uri_entry_dialog->priv;
151
priv->ok_button = GTK_WIDGET (gtk_builder_get_object (builder, "almanah_ued_ok_button"));
152
priv->uri_entry = GTK_ENTRY (gtk_builder_get_object (builder, "almanah_ued_uri_entry"));
154
g_object_unref (builder);
156
return uri_entry_dialog;
160
almanah_uri_entry_dialog_run (AlmanahUriEntryDialog *self)
162
/* Reset the URI entry and consequently the OK button */
163
gtk_entry_set_text (self->priv->uri_entry, "");
164
g_free (self->priv->uri);
165
self->priv->uri = NULL;
167
return (gtk_dialog_run (GTK_DIALOG (self)) == GTK_RESPONSE_OK) ? TRUE : FALSE;
171
is_uri_valid (const gchar *uri)
175
/* We assume that g_uri_parse_scheme() will fail if the URI is invalid. */
176
tmp = g_uri_parse_scheme (uri);
179
return (tmp != NULL) ? TRUE : FALSE;
183
ued_uri_entry_notify_text_cb (GObject *gobject, GParamSpec *param_spec, AlmanahUriEntryDialog *self)
185
AlmanahUriEntryDialogPrivate *priv = self->priv;
187
/* Enable/Disable the OK button based on whether the current URI is valid. */
188
if (is_uri_valid (gtk_entry_get_text (priv->uri_entry)) == TRUE) {
189
/* The URI was parsed successfully; update priv->uri and enable the OK button */
190
priv->uri = g_strdup (gtk_entry_get_text (priv->uri_entry));
191
gtk_widget_set_sensitive (priv->ok_button, TRUE);
193
/* Failure due to the URI entered being invalid; disable the OK button */
194
gtk_widget_set_sensitive (priv->ok_button, FALSE);
199
almanah_uri_entry_dialog_get_uri (AlmanahUriEntryDialog *self)
201
g_return_val_if_fail (ALMANAH_IS_URI_ENTRY_DIALOG (self), NULL);
202
return self->priv->uri;
206
almanah_uri_entry_dialog_set_uri (AlmanahUriEntryDialog *self, const gchar *uri)
208
g_return_if_fail (ALMANAH_IS_URI_ENTRY_DIALOG (self));
209
g_return_if_fail (uri == NULL || is_uri_valid (uri) == TRUE);
211
g_free (self->priv->uri);
212
self->priv->uri = g_strdup (uri);
214
g_object_notify (G_OBJECT (self), "uri");