aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-charset-combo-box.c
blob: 5f542a6d48ad65cedc250708864d85f5a72178dc (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * e-charset-combo-box.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/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "e-charset-combo-box.h"

#include <glib/gi18n.h>

#include "e-util/e-charset.h"
#include "e-util/e-util.h"

#define E_CHARSET_COMBO_BOX_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_CHARSET_COMBO_BOX, ECharsetComboBoxPrivate))

#define DEFAULT_CHARSET "UTF-8"
#define OTHER_VALUE G_MAXINT

struct _ECharsetComboBoxPrivate {
    GtkActionGroup *action_group;
    GtkRadioAction *other_action;
    GHashTable *charset_index;

    /* Used when the user clicks Cancel in the character set
     * dialog. Reverts to the previous combo box setting. */
    gint previous_index;

    /* When setting the character set programmatically, this
     * prevents the custom character set dialog from running. */
    guint block_dialog : 1;
};

enum {
    PROP_0,
    PROP_CHARSET
};

G_DEFINE_TYPE (
    ECharsetComboBox,
    e_charset_combo_box,
    E_TYPE_ACTION_COMBO_BOX)

static void
charset_combo_box_entry_changed_cb (GtkEntry *entry,
                                    GtkDialog *dialog)
{
    const gchar *text;
    gboolean sensitive;

    text = gtk_entry_get_text (entry);
    sensitive = (text != NULL && *text != '\0');
    gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, sensitive);
}

static void
charset_combo_box_run_dialog (ECharsetComboBox *combo_box)
{
    GtkDialog *dialog;
    GtkEntry *entry;
    GtkWidget *container;
    GtkWidget *widget;
    GObject *object;
    gpointer parent;
    const gchar *charset;

    /* FIXME Using a dialog for this is lame.  Selecting "Other..."
     *       should unlock an entry directly in the Preferences tab.
     *       Unfortunately space in Preferences is at a premium right
     *       now, but we should revisit this when the space issue is
     *       finally resolved. */

    parent = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
    parent = gtk_widget_is_toplevel (parent) ? parent : NULL;

    object = G_OBJECT (combo_box->priv->other_action);
    charset = g_object_get_data (object, "charset");

    widget = gtk_dialog_new_with_buttons (
        _("Character Encoding"), parent,
        GTK_DIALOG_DESTROY_WITH_PARENT,
        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);

    /* Load the broken border width defaults so we can override them. */
    gtk_widget_ensure_style (widget);

    dialog = GTK_DIALOG (widget);

#if !GTK_CHECK_VERSION(2,90,7)
    g_object_set (dialog, "has-separator", FALSE, NULL);
#endif
    gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);

    gtk_container_set_border_width (GTK_CONTAINER (dialog), 12);

    widget = gtk_dialog_get_action_area (dialog);
    gtk_container_set_border_width (GTK_CONTAINER (widget), 0);

    widget = gtk_dialog_get_content_area (dialog);
    gtk_box_set_spacing (GTK_BOX (widget), 12);
    gtk_container_set_border_width (GTK_CONTAINER (widget), 0);

    container = widget;

    widget = gtk_label_new (_("Enter the character set to use"));
    gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
    gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    widget = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
    gtk_alignment_set_padding (GTK_ALIGNMENT (widget), 0, 0, 12, 0);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    container = widget;

    widget = gtk_entry_new ();
    entry = GTK_ENTRY (widget);
    gtk_entry_set_activates_default (entry, TRUE);
    gtk_container_add (GTK_CONTAINER (container), widget);
    gtk_widget_show (widget);

    g_signal_connect (
        entry, "changed",
        G_CALLBACK (charset_combo_box_entry_changed_cb), dialog);

    /* Set the default text -after- connecting the signal handler.
     * This will initialize the "OK" button to the proper state. */
    gtk_entry_set_text (entry, charset);

    if (gtk_dialog_run (dialog) != GTK_RESPONSE_OK) {
        gint active;

        /* Revert to the previously selected character set. */
        combo_box->priv->block_dialog = TRUE;
        active = combo_box->priv->previous_index;
        gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), active);
        combo_box->priv->block_dialog = FALSE;

        goto exit;
    }

    charset = gtk_entry_get_text (entry);
    g_return_if_fail (charset != NULL && charset != '\0');

    g_object_set_data_full (
        object, "charset", g_strdup (charset),
        (GDestroyNotify) g_free);

exit:
    gtk_widget_destroy (GTK_WIDGET (dialog));
}

static void
charset_combo_box_notify_charset_cb (ECharsetComboBox *combo_box)
{
    GtkToggleAction *action;

    action = GTK_TOGGLE_ACTION (combo_box->priv->other_action);
    if (!gtk_toggle_action_get_active (action))
        return;

    if (combo_box->priv->block_dialog)
        return;

    /* "Other" action was selected by user. */
    charset_combo_box_run_dialog (combo_box);
}

static void
charset_combo_box_set_property (GObject *object,
                                guint property_id,
                                const GValue *value,
                                GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_CHARSET:
            e_charset_combo_box_set_charset (
                E_CHARSET_COMBO_BOX (object),
                g_value_get_string (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
charset_combo_box_get_property (GObject *object,
                                guint property_id,
                                GValue *value,
                                GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_CHARSET:
            g_value_set_string (
                value, e_charset_combo_box_get_charset (
                E_CHARSET_COMBO_BOX (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
charset_combo_box_dispose (GObject *object)
{
    ECharsetComboBoxPrivate *priv;

    priv = E_CHARSET_COMBO_BOX_GET_PRIVATE (object);

    if (priv->action_group != NULL) {
        g_object_unref (priv->action_group);
        priv->action_group = NULL;
    }

    if (priv->other_action != NULL) {
        g_object_unref (priv->other_action);
        priv->other_action = NULL;
    }

    g_hash_table_remove_all (priv->charset_index);

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_charset_combo_box_parent_class)->dispose (object);
}

static void
charset_combo_box_finalize (GObject *object)
{
    ECharsetComboBoxPrivate *priv;

    priv = E_CHARSET_COMBO_BOX_GET_PRIVATE (object);

    g_hash_table_destroy (priv->charset_index);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_charset_combo_box_parent_class)->finalize (object);
}

static void
charset_combo_box_changed (GtkComboBox *combo_box)
{
    ECharsetComboBoxPrivate *priv;

    priv = E_CHARSET_COMBO_BOX_GET_PRIVATE (combo_box);

    /* Chain up to parent's changed() method. */
    GTK_COMBO_BOX_CLASS (e_charset_combo_box_parent_class)->
        changed (combo_box);

    /* Notify -before- updating previous index. */
    g_object_notify (G_OBJECT (combo_box), "charset");
    priv->previous_index = gtk_combo_box_get_active (combo_box);
}

static void
e_charset_combo_box_class_init (ECharsetComboBoxClass *class)
{
    GObjectClass *object_class;
    GtkComboBoxClass *combo_box_class;

    g_type_class_add_private (class, sizeof (ECharsetComboBoxPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = charset_combo_box_set_property;
    object_class->get_property = charset_combo_box_get_property;
    object_class->dispose = charset_combo_box_dispose;
    object_class->finalize = charset_combo_box_finalize;

    combo_box_class = GTK_COMBO_BOX_CLASS (class);
    combo_box_class->changed = charset_combo_box_changed;

    g_object_class_install_property (
        object_class,
        PROP_CHARSET,
        g_param_spec_string (
            "charset",
            "Charset",
            "The selected character set",
            "UTF-8",
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT));
}

static void
e_charset_combo_box_init (ECharsetComboBox *combo_box)
{
    GtkActionGroup *action_group;
    GtkRadioAction *radio_action;
    GHashTable *charset_index;
    GSList *group, *iter;

    action_group = gtk_action_group_new ("charset-combo-box-internal");

    charset_index = g_hash_table_new_full (
        g_str_hash, g_str_equal,
        (GDestroyNotify) g_free,
        (GDestroyNotify) g_object_unref);

    combo_box->priv = E_CHARSET_COMBO_BOX_GET_PRIVATE (combo_box);
    combo_box->priv->action_group = action_group;
    combo_box->priv->charset_index = charset_index;

    group = e_charset_add_radio_actions (
        action_group, "charset-", NULL, NULL, NULL);

    /* Populate the character set index. */
    for (iter = group; iter != NULL; iter = iter->next) {
        GObject *object = iter->data;
        const gchar *charset;

        charset = g_object_get_data (object, "charset");
        g_return_if_fail (charset != NULL);

        g_hash_table_insert (
            charset_index, g_strdup (charset),
            g_object_ref (object));
    }

    /* Note the "other" action is not included in the index. */

    radio_action = gtk_radio_action_new (
        "charset-other", _("Other..."), NULL, NULL, OTHER_VALUE);

    g_object_set_data (G_OBJECT (radio_action), "charset", (gpointer) "");

    gtk_radio_action_set_group (radio_action, group);
    group = gtk_radio_action_get_group (radio_action);

    e_action_combo_box_set_action (
        E_ACTION_COMBO_BOX (combo_box), radio_action);

    e_action_combo_box_add_separator_after (
        E_ACTION_COMBO_BOX (combo_box), g_slist_length (group));

    g_signal_connect (
        combo_box, "notify::charset",
        G_CALLBACK (charset_combo_box_notify_charset_cb), NULL);

    combo_box->priv->other_action = radio_action;
}

GtkWidget *
e_charset_combo_box_new (void)
{
    return g_object_new (E_TYPE_CHARSET_COMBO_BOX, NULL);
}

const gchar *
e_charset_combo_box_get_charset (ECharsetComboBox *combo_box)
{
    GtkRadioAction *radio_action;

    g_return_val_if_fail (E_IS_CHARSET_COMBO_BOX (combo_box), NULL);

    radio_action = combo_box->priv->other_action;
    radio_action = e_radio_action_get_current_action (radio_action);

    return g_object_get_data (G_OBJECT (radio_action), "charset");
}

void
e_charset_combo_box_set_charset (ECharsetComboBox *combo_box,
                                 const gchar *charset)
{
    GHashTable *charset_index;
    GtkRadioAction *radio_action;

    g_return_if_fail (E_IS_CHARSET_COMBO_BOX (combo_box));

    if (charset == NULL || *charset == '\0')
        charset = "UTF-8";

    charset_index = combo_box->priv->charset_index;
    radio_action = g_hash_table_lookup (charset_index, charset);

    if (radio_action == NULL) {
        radio_action = combo_box->priv->other_action;
        g_object_set_data_full (
            G_OBJECT (radio_action),
            "charset", g_strdup (charset),
            (GDestroyNotify) g_free);
    }

    combo_box->priv->block_dialog = TRUE;
    gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action), TRUE);
    combo_box->priv->block_dialog = FALSE;
}