~ubuntu-branches/ubuntu/wily/almanah/wily-proposed

« back to all changes in this revision

Viewing changes to src/uri-entry-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 2011 <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 "uri-entry-dialog.h"
 
26
#include "interface.h"
 
27
 
 
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);
 
30
 
 
31
/* GtkBuilder callbacks */
 
32
G_MODULE_EXPORT void ued_uri_entry_notify_text_cb (GObject *gobject, GParamSpec *pspec, AlmanahUriEntryDialog *self);
 
33
 
 
34
struct _AlmanahUriEntryDialogPrivate {
 
35
        gchar *uri;
 
36
        GtkWidget *ok_button;
 
37
        GtkEntry *uri_entry;
 
38
};
 
39
 
 
40
enum {
 
41
        PROP_URI = 1
 
42
};
 
43
 
 
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))
 
46
 
 
47
static void
 
48
almanah_uri_entry_dialog_class_init (AlmanahUriEntryDialogClass *klass)
 
49
{
 
50
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
51
 
 
52
        g_type_class_add_private (klass, sizeof (AlmanahUriEntryDialogPrivate));
 
53
 
 
54
        gobject_class->set_property = almanah_uri_entry_dialog_set_property;
 
55
        gobject_class->get_property = almanah_uri_entry_dialog_get_property;
 
56
 
 
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.",
 
60
                                                              NULL,
 
61
                                                              G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
62
}
 
63
 
 
64
static void
 
65
almanah_uri_entry_dialog_init (AlmanahUriEntryDialog *self)
 
66
{
 
67
        self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_URI_ENTRY_DIALOG, AlmanahUriEntryDialogPrivate);
 
68
 
 
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"));
 
72
}
 
73
 
 
74
static void
 
75
almanah_uri_entry_dialog_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
 
76
{
 
77
        AlmanahUriEntryDialogPrivate *priv = ALMANAH_URI_ENTRY_DIALOG (object)->priv;
 
78
 
 
79
        switch (property_id) {
 
80
                case PROP_URI:
 
81
                        g_value_set_string (value, priv->uri);
 
82
                        break;
 
83
                default:
 
84
                        /* We don't have any other property... */
 
85
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
86
                        break;
 
87
        }
 
88
}
 
89
 
 
90
static void
 
91
almanah_uri_entry_dialog_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
 
92
{
 
93
        AlmanahUriEntryDialog *self = ALMANAH_URI_ENTRY_DIALOG (object);
 
94
 
 
95
        switch (property_id) {
 
96
                case PROP_URI:
 
97
                        almanah_uri_entry_dialog_set_uri (self, g_value_get_string (value));
 
98
                        break;
 
99
                default:
 
100
                        /* We don't have any other property... */
 
101
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
102
                        break;
 
103
        }
 
104
}
 
105
 
 
106
AlmanahUriEntryDialog *
 
107
almanah_uri_entry_dialog_new (void)
 
108
{
 
109
        GtkBuilder *builder;
 
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 */
 
117
                NULL
 
118
        };
 
119
 
 
120
        builder = gtk_builder_new ();
 
121
 
 
122
        if (gtk_builder_add_objects_from_file (builder, interface_filename, (gchar**) object_names, &error) == FALSE) {
 
123
                /* Show an error */
 
124
                GtkWidget *dialog = gtk_message_dialog_new (NULL,
 
125
                                GTK_DIALOG_MODAL,
 
126
                                GTK_MESSAGE_ERROR,
 
127
                                GTK_BUTTONS_OK,
 
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);
 
132
 
 
133
                g_error_free (error);
 
134
                g_object_unref (builder);
 
135
 
 
136
                return NULL;
 
137
        }
 
138
 
 
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);
 
142
 
 
143
        if (uri_entry_dialog == NULL) {
 
144
                g_object_unref (builder);
 
145
                return NULL;
 
146
        }
 
147
 
 
148
        priv = uri_entry_dialog->priv;
 
149
 
 
150
        /* Grab widgets */
 
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"));
 
153
 
 
154
        g_object_unref (builder);
 
155
 
 
156
        return uri_entry_dialog;
 
157
}
 
158
 
 
159
gboolean
 
160
almanah_uri_entry_dialog_run (AlmanahUriEntryDialog *self)
 
161
{
 
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;
 
166
 
 
167
        return (gtk_dialog_run (GTK_DIALOG (self)) == GTK_RESPONSE_OK) ? TRUE : FALSE;
 
168
}
 
169
 
 
170
static gboolean
 
171
is_uri_valid (const gchar *uri)
 
172
{
 
173
        gchar *tmp;
 
174
 
 
175
        /* We assume that g_uri_parse_scheme() will fail if the URI is invalid. */
 
176
        tmp = g_uri_parse_scheme (uri);
 
177
        g_free (tmp);
 
178
 
 
179
        return (tmp != NULL) ? TRUE : FALSE;
 
180
}
 
181
 
 
182
void
 
183
ued_uri_entry_notify_text_cb (GObject *gobject, GParamSpec *param_spec, AlmanahUriEntryDialog *self)
 
184
{
 
185
        AlmanahUriEntryDialogPrivate *priv = self->priv;
 
186
 
 
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);
 
192
        } else {
 
193
                /* Failure due to the URI entered being invalid; disable the OK button */
 
194
                gtk_widget_set_sensitive (priv->ok_button, FALSE);
 
195
        }
 
196
}
 
197
 
 
198
const gchar *
 
199
almanah_uri_entry_dialog_get_uri (AlmanahUriEntryDialog *self)
 
200
{
 
201
        g_return_val_if_fail (ALMANAH_IS_URI_ENTRY_DIALOG (self), NULL);
 
202
        return self->priv->uri;
 
203
}
 
204
 
 
205
void
 
206
almanah_uri_entry_dialog_set_uri (AlmanahUriEntryDialog *self, const gchar *uri)
 
207
{
 
208
        g_return_if_fail (ALMANAH_IS_URI_ENTRY_DIALOG (self));
 
209
        g_return_if_fail (uri == NULL || is_uri_valid (uri) == TRUE);
 
210
 
 
211
        g_free (self->priv->uri);
 
212
        self->priv->uri = g_strdup (uri);
 
213
 
 
214
        g_object_notify (G_OBJECT (self), "uri");
 
215
}