aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-plugin-util.c
blob: 885d017e15ea52c8544ff1789d99a2757caa07dd (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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * 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-2010 Novell, Inc. (www.novell.com)
 */

#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <libsoup/soup.h>
#include <libedataserver/e-source.h>
#include <string.h>

#include "e-plugin-util.h"

/* name of a property on a widget with corresponding property name for an ESource */
#define EPU_SP_NAME "e-source-property-name"

#define EPU_CHECK_TRUE "epu-check-true-value"
#define EPU_CHECK_FALSE "epu-check-false-value"

static gboolean
epu_is_uri_proto (const gchar *uri, const gchar *protocol)
{
    gboolean res;

    g_return_val_if_fail (uri != NULL, FALSE);
    g_return_val_if_fail (protocol != NULL, FALSE);

    res = uri && g_ascii_strncasecmp (uri, protocol, strlen (protocol)) == 0;

    if (res)
        res = strchr (protocol, ':') != NULL || uri[strlen (protocol)] == ':';

    return res;
}

/**
 * e_plugin_util_is_source_proto:
 * @source: #ESource object
 * @protocol: protocol to check on, like "http", "https", ...
 *
 * Returns whether given source's uri is of the given protocol.
 **/
gboolean
e_plugin_util_is_source_proto (ESource *source, const gchar *protocol)
{
    gchar *uri;
    gboolean res;

    g_return_val_if_fail (source != NULL, FALSE);
    g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
    g_return_val_if_fail (protocol != NULL, FALSE);

    uri = e_source_get_uri (source);
    res = epu_is_uri_proto (uri, protocol);
    g_free (uri);

    return res;
}

/**
 * e_plugin_util_is_group_proto:
 * @group: #ESourceGroup object
 * @protocol: protocol to check on, like "http", "https", ...
 *
 * Returns whether given groups' base uri is of the given protocol.
 **/
gboolean
e_plugin_util_is_group_proto (ESourceGroup *group, const gchar *protocol)
{
    g_return_val_if_fail (group != NULL, FALSE);
    g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
    g_return_val_if_fail (protocol != NULL, FALSE);

    return epu_is_uri_proto (e_source_group_peek_base_uri (group), protocol);
}

/**
 * e_plugin_util_replace_at_sign:
 * @str: string to work with
 *
 * Replaces all '@' with '%40' in @str.
 * Returns newly allocated string.
 **/
gchar *
e_plugin_util_replace_at_sign (const gchar *str)
{
    gchar *res, *at;

    if (!str)
        return NULL;

    res = g_strdup (str);
    while (at = strchr (res, '@'), at) {
        gchar *tmp = g_malloc0 (sizeof (gchar) * (1 + strlen (res) + 2));

        strncpy (tmp, res, at - res);
        strcat (tmp, "%40");
        strcat (tmp, at + 1);

        g_free (res);
        res = tmp;
    }

    return res;
}

/**
 * e_plugin_util_uri_no_proto:
 * @uri: #SoupURI object
 *
 * Returns uri encoded as string, without protocol part.
 * Returned pointer should be freed with g_free.
 **/
gchar *
e_plugin_util_uri_no_proto (SoupURI *uri)
{
    gchar *full_uri, *uri_noproto;
    const gchar *tmp;

    g_return_val_if_fail (uri != NULL, NULL);

    full_uri = soup_uri_to_string (uri, FALSE);
    g_return_val_if_fail (full_uri != NULL, NULL);

    tmp = strstr (full_uri, "://");
    if (tmp && tmp < strchr (full_uri, '/')) {
        uri_noproto = g_strdup (tmp + 3);
    } else {
        uri_noproto = full_uri;
        full_uri = NULL;
    }

    g_free (full_uri);

    return uri_noproto;
}

static void
epu_update_source_property (ESource *source, GObject *object, const gchar *value)
{
    const gchar *property_name;

    g_return_if_fail (source != NULL);
    g_return_if_fail (E_IS_SOURCE (source));
    g_return_if_fail (object != NULL);
    g_return_if_fail (G_IS_OBJECT (object));

    property_name = g_object_get_data (object, EPU_SP_NAME);
    g_return_if_fail (property_name != NULL);

    e_source_set_property (source, property_name, value);
}

static void
epu_entry_changed_cb (GObject *entry, ESource *source)
{
    g_return_if_fail (entry != NULL);
    g_return_if_fail (GTK_IS_ENTRY (entry));

    epu_update_source_property (source, entry, gtk_entry_get_text (GTK_ENTRY (entry)));
}

static void
epu_check_toggled_cb (GObject *button, ESource *source)
{
    const gchar *true_value, *false_value;

    g_return_if_fail (button != NULL);
    g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));

    true_value = g_object_get_data (button, EPU_CHECK_TRUE);
    false_value = g_object_get_data (button, EPU_CHECK_FALSE);

    epu_update_source_property (source, button, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)) ? true_value : false_value);
}

/**
 * e_plugin_util_add_entry:
 * @parent: two-columns #GtkTable or #GtkContainer, where to add new entry
 * @label: label for the entry; can be NULL for no label
 * @source: #ESource object to which tight the entry change; can be NULL for no property binding
 * @source_property: source's property name to use for a value; can be NULL for no property binding
 *
 * Adds a #GtkEntry to the table at the last row or to the container, with a given label.
 * The entry will be always at the second column of the table.
 * Value of an entry will be prefilled with a property value of the given
 * source, and the source will be updated on any change of the entry automatically.
 * Entry is shown by default.
 *
 * Returns pointer to newly added #GtkEntry.
 **/
GtkWidget *
e_plugin_util_add_entry (GtkWidget *parent, const gchar *label, ESource *source, const gchar *source_property)
{
    GtkWidget *entry, *lbl = NULL;
    const gchar *value;
    gint row = -1;

    g_return_val_if_fail (parent != NULL, NULL);
    g_return_val_if_fail (GTK_IS_TABLE (parent) || GTK_IS_CONTAINER (parent), NULL);

    if (source || source_property) {
        g_return_val_if_fail (source != NULL, NULL);
        g_return_val_if_fail (E_IS_SOURCE (source), NULL);
        g_return_val_if_fail (source_property != NULL, NULL);
        g_return_val_if_fail (*source_property != 0, NULL);
    }

    if (GTK_IS_TABLE (parent))
        row = GTK_TABLE (parent)->nrows;

    if (label) {
        lbl = gtk_label_new_with_mnemonic (label);
        gtk_widget_show (lbl);
        gtk_misc_set_alignment (GTK_MISC (lbl), 0.0, 0.5);
        if (row != -1)
            gtk_table_attach (GTK_TABLE (parent), lbl, 0, 1, row, row + 1, GTK_FILL, 0, 0, 0);
        else
            gtk_container_add (GTK_CONTAINER (parent), lbl);
    }

    if (source)
        value = e_source_get_property (source, source_property);
    else
        value = NULL;

    entry = gtk_entry_new ();
    gtk_widget_show (entry);
    gtk_entry_set_text (GTK_ENTRY (entry), value ? value : "");
    if (row != -1)
        gtk_table_attach (GTK_TABLE (parent), entry, 1, 2, row, row + 1, GTK_EXPAND | GTK_FILL, 0, 0, 0);
    else
        gtk_container_add (GTK_CONTAINER (parent), entry);

    if (lbl)
        gtk_label_set_mnemonic_widget (GTK_LABEL (lbl), entry);

    if (source) {
        g_object_set_data_full (G_OBJECT (entry), EPU_SP_NAME, g_strdup (source_property), g_free);
        g_signal_connect (G_OBJECT (entry), "changed", G_CALLBACK (epu_entry_changed_cb), source);
    }

    return entry;
}

/**
 * e_plugin_util_add_check:
 * @parent: either two-columns #GtkTable or #GtkContainer where to add new check box; or NULL to just create it
 * @label: label for the check; cannot be NULL
 * @source: #ESource object to which tight the check change; can be NULL for no property binding
 * @source_property: source's property name to use for a value; can be NULL for no property binding
 * @true_value: what value use for a checked state in a source
 * @false_value: what value use for an unchecked state in a source
 *
 * Adds a #GtkCheckButton to the parent (if provided) at the last row, with a given label.
 * The check will be always at the second column of the table.
 * Value of a check will be prefilled with a property value of the given
 * source, and the source will be updated on any change of the check automatically.
 * Check is shown by default.
 *
 * Returns pointer to newly added #GtkCheckButton.
 **/
GtkWidget *
e_plugin_util_add_check (GtkWidget *parent, const gchar *label, ESource *source, const gchar *source_property, const gchar *true_value, const gchar *false_value)
{
    GtkWidget *check;
    const gchar *value;

    g_return_val_if_fail (parent == NULL || GTK_IS_TABLE (parent) || GTK_IS_CONTAINER (parent), NULL);
    g_return_val_if_fail (label != NULL, NULL);

    if (source || source_property) {
        g_return_val_if_fail (source != NULL, NULL);
        g_return_val_if_fail (E_IS_SOURCE (source), NULL);
        g_return_val_if_fail (source_property != NULL, NULL);
        g_return_val_if_fail (*source_property != 0, NULL);
    }

    if (source)
        value = e_source_get_property (source, source_property);
    else
        value = NULL;

    check = gtk_check_button_new_with_mnemonic (label);
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check),
        value && (value == true_value ||
              (true_value && g_str_equal (value, true_value)) ||
              (!source && g_str_equal (value, "1"))));
    gtk_widget_show (check);

    if (parent && GTK_IS_TABLE (parent)) {
        gint row = GTK_TABLE (parent)->nrows;

        gtk_table_attach (GTK_TABLE (parent), check, 1, 2, row , row + 1, GTK_FILL, 0, 0, 0);
    } else if (parent) {
        gtk_container_add (GTK_CONTAINER (parent), check);
    }

    if (source) {
        g_object_set_data_full (G_OBJECT (check), EPU_SP_NAME, g_strdup (source_property), g_free);
        g_object_set_data_full (G_OBJECT (check), EPU_CHECK_TRUE, g_strdup (true_value), g_free);
        g_object_set_data_full (G_OBJECT (check), EPU_CHECK_FALSE, g_strdup (false_value), g_free);
        g_signal_connect (G_OBJECT (check), "toggled", G_CALLBACK (epu_check_toggled_cb), source);
    }

    return check;
}

static void
epu_update_refresh_value (GtkWidget *spin, GtkWidget *combobox, ESource *source)
{
    gchar *value;
    gint setting = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spin));
    switch (gtk_combo_box_get_active (GTK_COMBO_BOX (combobox))) {
    case 0:
        /* minutes */
        break;
    case 1:
        /* hours */
        setting *= 60;
        break;
    case 2:
        /* days */
        setting *= 1440;
        break;
    case 3:
        /* weeks */
        setting *= 10080;
        break;
    default:
        g_warning ("%s: Time unit out of range", G_STRFUNC);
        break;
    }

    value = g_strdup_printf ("%d", setting);
    epu_update_source_property (source, G_OBJECT (spin), value);
    g_free (value);
}

static void
epu_refresh_spin_changed_cb (GtkWidget *spin, ESource *source)
{
    g_return_if_fail (spin != NULL);
    g_return_if_fail (GTK_IS_SPIN_BUTTON (spin));

    epu_update_refresh_value (spin, g_object_get_data (G_OBJECT (spin), "refresh-combo"), source);
}

static void
epu_refresh_combo_changed_cb (GtkWidget *combobox, ESource *source)
{
    g_return_if_fail (combobox != NULL);
    g_return_if_fail (GTK_IS_COMBO_BOX (combobox));

    epu_update_refresh_value (g_object_get_data (G_OBJECT (combobox), "refresh-spin"), combobox, source);
}

/**
 * e_plugin_util_add_refresh:
 * @parent: two-columns #GtkTable where to add new "refresh" setup widgets or NULL to just create an hbox
 * @label: label for the widgets; can be NULL, but for parent == NULL is ignored
 * @source: #ESource object to which tight the refresh change; cannot be NULL
 * @source_property: source's property name to use for a value; cannot be NULL
 *
 * Adds widgets to setup Refresh interval. The stored value is in minutes.
 * Returns pointer to an HBox, which contains two widgets, spin and a combo box.
 * Both can be accessed by g_object_get_data with a name "refresh-spin" and "refresh-combo".
 **/
GtkWidget *
e_plugin_util_add_refresh (GtkWidget *parent, const gchar *label, ESource *source, const gchar *source_property)
{
    GtkWidget *lbl = NULL, *hbox, *spin, *combo;
    const gchar *value;
    gint row = -1, value_num, item_num = 0;

    g_return_val_if_fail (parent == NULL || GTK_IS_TABLE (parent), NULL);
    g_return_val_if_fail (source != NULL, NULL);
    g_return_val_if_fail (E_IS_SOURCE (source), NULL);
    g_return_val_if_fail (source_property != NULL, NULL);
    g_return_val_if_fail (*source_property != 0, NULL);

    if (parent)
        row = GTK_TABLE (parent)->nrows;

    value = e_source_get_property (source, source_property);
    if (!value) {
        value = "30";
        e_source_set_property (source, source_property, value);
    }

    if (label && parent) {
        lbl = gtk_label_new_with_mnemonic (label);
        gtk_widget_show (lbl);
        gtk_misc_set_alignment (GTK_MISC (lbl), 0.0, 0.5);
        gtk_table_attach (GTK_TABLE (parent), lbl, 0, 1, row, row + 1, GTK_FILL, 0, 0, 0);
    }

    hbox = gtk_hbox_new (FALSE, 6);
    gtk_widget_show (hbox);

    spin = gtk_spin_button_new_with_range (0, 100, 1);
    gtk_widget_show (spin);
    gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, TRUE, 0);

    combo = gtk_combo_box_new_text ();
    gtk_widget_show (combo);
    gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("minutes"));
    gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("hours"));
    gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("days"));
    gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("weeks"));
    gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, TRUE, 0);

    value_num = value ? atoi (value) : 30;

    if (value_num && !(value_num % 10080)) {
        /* weeks */
        item_num = 3;
        value_num /= 10080;
    } else if (value_num && !(value_num % 1440)) {
        /* days */
        item_num = 2;
        value_num /= 1440;
    } else if (value_num && !(value_num % 60)) {
        /* hours */
        item_num = 1;
        value_num /= 60;
    }

    gtk_combo_box_set_active (GTK_COMBO_BOX (combo), item_num);
    gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), value_num);

    if (lbl)
        gtk_label_set_mnemonic_widget (GTK_LABEL (lbl), spin);

    g_object_set_data_full (G_OBJECT (spin), EPU_SP_NAME, g_strdup (source_property), g_free);

    g_object_set_data (G_OBJECT (combo), "refresh-spin", spin);
    g_object_set_data (G_OBJECT (spin), "refresh-combo", combo);
    g_object_set_data (G_OBJECT (hbox), "refresh-spin", spin);
    g_object_set_data (G_OBJECT (hbox), "refresh-combo", combo);
    g_signal_connect (G_OBJECT (combo), "changed", G_CALLBACK (epu_refresh_combo_changed_cb), source);
    g_signal_connect (G_OBJECT (spin), "value-changed", G_CALLBACK (epu_refresh_spin_changed_cb), source);

    if (parent)
        gtk_table_attach (GTK_TABLE (parent), hbox, 1, 2, row, row + 1, GTK_EXPAND | GTK_FILL, 0, 0, 0);

    return hbox;
}