aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-activity-bar.c
blob: 5407814ab6aaa604106071a12279eaee09a75b4c (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
/*
 * e-activity-bar.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-activity-bar.h"

#define FEEDBACK_PERIOD     1 /* seconds */
#define COMPLETED_ICON_NAME "emblem-default"

struct _EActivityBarPrivate {
    EActivity *activity;    /* weak reference */
    GtkWidget *image;   /* not referenced */
    GtkWidget *label;   /* not referenced */
    GtkWidget *cancel;  /* not referenced */
    GtkWidget *spinner; /* not referenced */

    /* If the user clicks the Cancel button, keep the cancelled
     * EActivity object alive for a short duration so the user
     * gets some visual feedback that cancellation worked. */
    guint timeout_id;
};

enum {
    PROP_0,
    PROP_ACTIVITY
};

G_DEFINE_TYPE (
    EActivityBar,
    e_activity_bar,
    GTK_TYPE_INFO_BAR)

static void
activity_bar_feedback (EActivityBar *bar)
{
    EActivity *activity;
    EActivityState state;

    activity = e_activity_bar_get_activity (bar);
    g_return_if_fail (E_IS_ACTIVITY (activity));

    state = e_activity_get_state (activity);
    if (state != E_ACTIVITY_CANCELLED && state != E_ACTIVITY_COMPLETED)
        return;

    if (bar->priv->timeout_id > 0)
        g_source_remove (bar->priv->timeout_id);

    /* Hold a reference on the EActivity for a short
     * period so the activity bar stays visible. */
    bar->priv->timeout_id = g_timeout_add_seconds_full (
        G_PRIORITY_LOW, FEEDBACK_PERIOD, (GSourceFunc) gtk_false,
        g_object_ref (activity), (GDestroyNotify) g_object_unref);
}

static void
activity_bar_update (EActivityBar *bar)
{
    EActivity *activity;
    EActivityState state;
    GCancellable *cancellable;
    const gchar *icon_name;
    gboolean sensitive;
    gboolean visible;
    gchar *description;

    activity = e_activity_bar_get_activity (bar);

    if (activity == NULL) {
        gtk_widget_hide (GTK_WIDGET (bar));
        return;
    }

    cancellable = e_activity_get_cancellable (activity);
    icon_name = e_activity_get_icon_name (activity);
    state = e_activity_get_state (activity);

    description = e_activity_describe (activity);
    gtk_label_set_text (GTK_LABEL (bar->priv->label), description);

    if (state == E_ACTIVITY_CANCELLED) {
        PangoAttribute *attr;
        PangoAttrList *attr_list;

        attr_list = pango_attr_list_new ();

        attr = pango_attr_strikethrough_new (TRUE);
        pango_attr_list_insert (attr_list, attr);

        gtk_label_set_attributes (
            GTK_LABEL (bar->priv->label), attr_list);

        pango_attr_list_unref (attr_list);
    } else
        gtk_label_set_attributes (
            GTK_LABEL (bar->priv->label), NULL);

    if (state == E_ACTIVITY_COMPLETED)
        icon_name = COMPLETED_ICON_NAME;

    if (state == E_ACTIVITY_CANCELLED) {
        gtk_image_set_from_stock (
            GTK_IMAGE (bar->priv->image),
            GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
        gtk_widget_show (bar->priv->image);
    } else if (icon_name != NULL) {
        gtk_image_set_from_icon_name (
            GTK_IMAGE (bar->priv->image),
            icon_name, GTK_ICON_SIZE_BUTTON);
        gtk_widget_show (bar->priv->image);
    } else {
        gtk_widget_hide (bar->priv->image);
    }

    visible = (cancellable != NULL);
    gtk_widget_set_visible (bar->priv->cancel, visible);

    sensitive = (state == E_ACTIVITY_RUNNING);
    gtk_widget_set_sensitive (bar->priv->cancel, sensitive);

    visible = (description != NULL && *description != '\0');
    gtk_widget_set_visible (GTK_WIDGET (bar), visible);

    g_free (description);
}

static void
activity_bar_cancel (EActivityBar *bar)
{
    EActivity *activity;
    GCancellable *cancellable;

    activity = e_activity_bar_get_activity (bar);
    g_return_if_fail (E_IS_ACTIVITY (activity));

    cancellable = e_activity_get_cancellable (activity);
    g_cancellable_cancel (cancellable);

    activity_bar_update (bar);
}

static void
activity_bar_weak_notify_cb (EActivityBar *bar,
                             GObject *where_the_object_was)
{
    g_return_if_fail (E_IS_ACTIVITY_BAR (bar));

    bar->priv->activity = NULL;
    e_activity_bar_set_activity (bar, NULL);
}

static void
activity_bar_set_property (GObject *object,
                           guint property_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_ACTIVITY:
            e_activity_bar_set_activity (
                E_ACTIVITY_BAR (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
activity_bar_get_property (GObject *object,
                           guint property_id,
                           GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_ACTIVITY:
            g_value_set_object (
                value, e_activity_bar_get_activity (
                E_ACTIVITY_BAR (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
activity_bar_dispose (GObject *object)
{
    EActivityBarPrivate *priv;

    priv = E_ACTIVITY_BAR (object)->priv;

    if (priv->timeout_id > 0) {
        g_source_remove (priv->timeout_id);
        priv->timeout_id = 0;
    }

    if (priv->activity != NULL) {
        g_signal_handlers_disconnect_matched (
            priv->activity, G_SIGNAL_MATCH_DATA,
            0, 0, NULL, NULL, object);
        g_object_weak_unref (
            G_OBJECT (priv->activity), (GWeakNotify)
            activity_bar_weak_notify_cb, object);
        priv->activity = NULL;
    }

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

static void
e_activity_bar_class_init (EActivityBarClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EActivityBarPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = activity_bar_set_property;
    object_class->get_property = activity_bar_get_property;
    object_class->dispose = activity_bar_dispose;

    g_object_class_install_property (
        object_class,
        PROP_ACTIVITY,
        g_param_spec_object (
            "activity",
            NULL,
            NULL,
            E_TYPE_ACTIVITY,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT));
}

static void
e_activity_bar_init (EActivityBar *bar)
{
    GtkWidget *container;
    GtkWidget *widget;

    bar->priv = G_TYPE_INSTANCE_GET_PRIVATE (
        bar, E_TYPE_ACTIVITY_BAR, EActivityBarPrivate);

    container = gtk_info_bar_get_content_area (GTK_INFO_BAR (bar));

    widget = gtk_hbox_new (FALSE, 12);
    gtk_container_add (GTK_CONTAINER (container), widget);
    gtk_widget_show (widget);

    container = widget;

    widget = gtk_image_new ();
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    bar->priv->image = widget;

    widget = gtk_spinner_new ();
    gtk_spinner_start (GTK_SPINNER (widget));
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    bar->priv->spinner = widget;

    /* The spinner is only visible when the image is not. */
    g_object_bind_property (
        bar->priv->image, "visible",
        bar->priv->spinner, "visible",
        G_BINDING_BIDIRECTIONAL |
        G_BINDING_SYNC_CREATE |
        G_BINDING_INVERT_BOOLEAN);

    widget = gtk_label_new (NULL);
    gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
    gtk_label_set_ellipsize (GTK_LABEL (widget), PANGO_ELLIPSIZE_END);
    gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
    bar->priv->label = widget;
    gtk_widget_show (widget);

    /* This is only shown if the EActivity has a GCancellable. */
    widget = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
    gtk_info_bar_add_action_widget (
        GTK_INFO_BAR (bar), widget, GTK_RESPONSE_CANCEL);
    bar->priv->cancel = widget;
    gtk_widget_hide (widget);

    g_signal_connect_swapped (
        widget, "clicked",
        G_CALLBACK (activity_bar_cancel), bar);
}

GtkWidget *
e_activity_bar_new (void)
{
    return g_object_new (E_TYPE_ACTIVITY_BAR, NULL);
}

EActivity *
e_activity_bar_get_activity (EActivityBar *bar)
{
    g_return_val_if_fail (E_IS_ACTIVITY_BAR (bar), NULL);

    return bar->priv->activity;
}

void
e_activity_bar_set_activity (EActivityBar *bar,
                             EActivity *activity)
{
    g_return_if_fail (E_IS_ACTIVITY_BAR (bar));

    if (activity != NULL)
        g_return_if_fail (E_IS_ACTIVITY (activity));

    if (bar->priv->timeout_id > 0) {
        g_source_remove (bar->priv->timeout_id);
        bar->priv->timeout_id = 0;
    }

    if (bar->priv->activity != NULL) {
        g_signal_handlers_disconnect_matched (
            bar->priv->activity, G_SIGNAL_MATCH_DATA,
            0, 0, NULL, NULL, bar);
        g_object_weak_unref (
            G_OBJECT (bar->priv->activity),
            (GWeakNotify) activity_bar_weak_notify_cb, bar);
    }

    bar->priv->activity = activity;

    if (activity != NULL) {
        g_object_weak_ref (
            G_OBJECT (activity), (GWeakNotify)
            activity_bar_weak_notify_cb, bar);

        g_signal_connect_swapped (
            activity, "notify::state",
            G_CALLBACK (activity_bar_feedback), bar);

        g_signal_connect_swapped (
            activity, "notify",
            G_CALLBACK (activity_bar_update), bar);
    }

    activity_bar_update (bar);

    g_object_notify (G_OBJECT (bar), "activity");
}