aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-url-entry.c
blob: 501f4e5760169c7bd123c98f4747995f0d023602 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * e-url-entry.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-url-entry.h"

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "e-misc-utils.h"

#define E_URL_ENTRY_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_URL_ENTRY, EUrlEntryPrivate))

#define ICON_POSITION GTK_ENTRY_ICON_SECONDARY

G_DEFINE_TYPE (
    EUrlEntry,
    e_url_entry,
    GTK_TYPE_ENTRY)

static gboolean
url_entry_text_to_sensitive (GBinding *binding,
                             const GValue *source_value,
                             GValue *target_value,
                             gpointer user_data)
{
    const gchar *text;
    gboolean sensitive = FALSE;

    text = g_value_get_string (source_value);

    if (text != NULL) {
        gchar *scheme;

        /* Skip leading whitespace. */
        while (g_ascii_isspace (*text))
            text++;

        scheme = g_uri_parse_scheme (text);
        sensitive = (scheme != NULL);
        g_free (scheme);
    }

    g_value_set_boolean (target_value, sensitive);

    return TRUE;
}

static void
url_entry_icon_release_cb (GtkEntry *entry,
                           GtkEntryIconPosition icon_position,
                           GdkEvent *event)
{
    gpointer toplevel;

    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
    toplevel = gtk_widget_is_toplevel (toplevel) ? toplevel : NULL;

    if (icon_position == ICON_POSITION) {
        const gchar *text;

        text = gtk_entry_get_text (entry);
        g_return_if_fail (text != NULL);

        /* Skip leading whitespace. */
        while (g_ascii_isspace (*text))
            text++;

        e_show_uri (toplevel, text);
    }
}

static void
e_url_entry_class_init (EUrlEntryClass *class)
{
}

static void
e_url_entry_init (EUrlEntry *url_entry)
{
    GtkEntry *entry;

    entry = GTK_ENTRY (url_entry);

    gtk_entry_set_icon_from_stock (
        entry, ICON_POSITION, GTK_STOCK_JUMP_TO);

    gtk_entry_set_icon_tooltip_text (
        entry, ICON_POSITION, _("Click here to open the URL"));

    gtk_entry_set_placeholder_text (entry, _("Enter a URL here"));

    /* XXX GtkEntryClass has no "icon_release" method pointer to
     *     override, so instead we have to connect to the signal. */
    g_signal_connect (
        url_entry, "icon-release",
        G_CALLBACK (url_entry_icon_release_cb), NULL);

    g_object_bind_property_full (
        url_entry, "text",
        url_entry, "secondary-icon-sensitive",
        G_BINDING_SYNC_CREATE,
        url_entry_text_to_sensitive,
        (GBindingTransformFunc) NULL,
        NULL, (GDestroyNotify) NULL);
}

GtkWidget *
e_url_entry_new (void)
{
    return g_object_new (E_TYPE_URL_ENTRY, NULL);
}