aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-activity-handler.c
blob: 305d2cc5f317fc21f60078b0f4b88d6e0d4e087a (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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-activity-handler.c
 *
 * Copyright (C) 2001, 2002, 2003 Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: Ettore Perazzoli <ettore@ximian.com>
 */

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

#include "e-activity-handler.h"

#include <gtk/gtksignal.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include <glib/gi18n.h>
#include <libgnomeui/gnome-popup-menu.h>

#include <misc/e-popup-menu.h>

#define ICON_SIZE 16


struct _ActivityInfo {
    char *component_id;
    GdkPixbuf *icon_pixbuf;
    guint id;
    char *information;
    gboolean cancellable;
    double progress;
    GtkWidget *menu;
    void (*cancel_func) (gpointer data);
    gpointer data;
    gpointer error;
    time_t  error_time;
};
typedef struct _ActivityInfo ActivityInfo;

struct _EActivityHandlerPrivate {
    guint next_activity_id;
    GList *activity_infos;
    GSList *task_bars;
    ELogger *logger;
    guint error_timer;
    guint error_flush_interval;

};

G_DEFINE_TYPE (EActivityHandler, e_activity_handler, G_TYPE_OBJECT)

/* Utility functions.  */

static void handle_error (ETaskWidget *task);

static unsigned int
get_new_activity_id (EActivityHandler *activity_handler)
{
    EActivityHandlerPrivate *priv;

    priv = activity_handler->priv;

    return priv->next_activity_id ++;
}

static GList *
lookup_activity (GList *list,
         guint activity_id,
         int *order_number_return)
{
    GList *p;
    int i;

    for (p = list, i = 0; p != NULL; p = p->next, i ++) {
        ActivityInfo *activity_info;

        activity_info = (ActivityInfo *) p->data;
        if (activity_info->id == activity_id) {
            *order_number_return = i;
            return p;
        }
    }

    *order_number_return = -1;
    return NULL;
}


/* ETaskWidget actions.  */

static int
task_widget_button_press_event_callback (GtkWidget *widget,
                     GdkEventButton *button_event,
                     void *data)
{
    ActivityInfo *activity_info;

    activity_info = (ActivityInfo *) data;

    if (button_event->button == 3)
        return activity_info->cancellable;

    if (button_event->button != 1)
        return FALSE;

    return TRUE;
}


/* Creating and destroying ActivityInfos.  */

static ActivityInfo *
activity_info_new (const char *component_id,
           guint id,
           GdkPixbuf *icon,
           const char *information,
           gboolean cancellable)
{
    ActivityInfo *info;

    info = g_new (ActivityInfo, 1);
    info->component_id   = g_strdup (component_id);
    info->id             = id;
    info->icon_pixbuf    = icon ? g_object_ref (icon): NULL;
    info->information    = g_strdup (information);
    info->cancellable    = cancellable;
    info->progress       = -1.0; /* (Unknown) */
    info->menu           = NULL;
    info->error          = NULL;
    info->cancel_func    = NULL;
    
    return info;
}

static void
activity_info_free (ActivityInfo *info)
{
    g_free (info->component_id);

    if (info->icon_pixbuf)
        g_object_unref (info->icon_pixbuf);
    g_free (info->information);

    if (info->menu != NULL)
        gtk_widget_destroy (info->menu);

    g_free (info);
}

static ETaskWidget *
task_widget_new_from_activity_info (ActivityInfo *activity_info)
{
    GtkWidget *widget;
    ETaskWidget *etw;

    widget = e_task_widget_new_with_cancel (activity_info->icon_pixbuf,
                    activity_info->component_id,
                    activity_info->information, activity_info->cancel_func, activity_info->data);
    etw = (ETaskWidget *) widget;
    etw->id = activity_info->id;
    gtk_widget_show (widget);

    g_signal_connect (widget, "button_press_event",
              G_CALLBACK (task_widget_button_press_event_callback),
              activity_info);

    return E_TASK_WIDGET (widget);
}


/* Task Bar handling.  */

static void
setup_task_bar (EActivityHandler *activity_handler,
        ETaskBar *task_bar)
{
    EActivityHandlerPrivate *priv;
    GList *p;

    priv = activity_handler->priv;

    for (p = g_list_last (priv->activity_infos); p != NULL; p = p->prev) {
        ActivityInfo *info = p->data;
        ETaskWidget *task_widget = task_widget_new_from_activity_info (info);
        task_widget->id = info->id;
        e_task_bar_prepend_task (task_bar, task_widget);
        if (info->error) {
            /* Prepare to handle existing errors*/
            GtkWidget *tool;

            tool = e_task_widget_update_image (task_widget, "stock_dialog-warning", g_object_get_data (info->error, "error"));
            g_object_set_data ((GObject *) task_widget, "tool", tool);
            g_object_set_data ((GObject *) task_widget, "error", info->error);
            g_object_set_data ((GObject *) task_widget, "activity-handler", activity_handler);
            g_object_set_data ((GObject *) task_widget, "activity", GINT_TO_POINTER(info->id));
            g_signal_connect_swapped (tool, "clicked", G_CALLBACK(handle_error), task_widget);          
        }
    }
}

static void
task_bar_destroy_notify (void *data,
             GObject *task_bar_instance)
{
    EActivityHandler *activity_handler;
    EActivityHandlerPrivate *priv;

    activity_handler = E_ACTIVITY_HANDLER (data);
    priv = activity_handler->priv;

    priv->task_bars = g_slist_remove (priv->task_bars, task_bar_instance);
}


/* GObject methods.  */

static void
impl_dispose (GObject *object)
{
    EActivityHandler *handler;
    EActivityHandlerPrivate *priv;
    GList *p;
    GSList *sp;

    handler = E_ACTIVITY_HANDLER (object);
    priv = handler->priv;

    for (p = priv->activity_infos; p != NULL; p = p->next) {
        ActivityInfo *info;

        info = (ActivityInfo *) p->data;
        activity_info_free (info);
    }

    g_list_free (priv->activity_infos);
    priv->activity_infos = NULL;

    for (sp = priv->task_bars; sp != NULL; sp = sp->next)
        g_object_weak_unref (G_OBJECT (sp->data), task_bar_destroy_notify, handler);
    priv->task_bars = NULL;

    (* G_OBJECT_CLASS (e_activity_handler_parent_class)->dispose) (object);
}

static void
impl_finalize (GObject *object)
{
    EActivityHandler *handler;
    EActivityHandlerPrivate *priv;

    handler = E_ACTIVITY_HANDLER (object);
    priv = handler->priv;

    g_free (priv);

    (* G_OBJECT_CLASS (e_activity_handler_parent_class)->finalize) (object);
}

static void
e_activity_handler_class_init (EActivityHandlerClass *activity_handler_class)
{
    GObjectClass *object_class = (GObjectClass *) activity_handler_class;

    object_class->dispose  = impl_dispose;
    object_class->finalize = impl_finalize;
}

static void
e_activity_handler_init (EActivityHandler *activity_handler)
{
    EActivityHandlerPrivate *priv;

    priv = g_new (EActivityHandlerPrivate, 1);
    priv->next_activity_id = 1;
    priv->activity_infos   = NULL;
    priv->task_bars        = NULL;
    priv->logger           = NULL;
    priv->error_timer      = 0;
    priv->error_flush_interval = 0;
    activity_handler->priv = priv;
}


EActivityHandler *
e_activity_handler_new (void)
{
    return g_object_new (e_activity_handler_get_type (), NULL);
}

void
e_activity_handler_set_error_flush_time (EActivityHandler *handler, int time)
{
    handler->priv->error_flush_interval = time;
}
void
e_activity_handler_set_logger (EActivityHandler *handler, ELogger *logger)
{
    handler->priv->logger = logger;
}

void
e_activity_handler_set_message (EActivityHandler *activity_handler,
                const char       *message)
{
    EActivityHandlerPrivate *priv;
    GSList *i;

    priv = activity_handler->priv;

    for (i = priv->task_bars; i; i = i->next)
        e_task_bar_set_message (E_TASK_BAR (i->data), message);
}

void
e_activity_handler_unset_message (EActivityHandler *activity_handler)
{
    EActivityHandlerPrivate *priv;
    GSList *i;

    priv = activity_handler->priv;

    for (i = priv->task_bars; i; i = i->next)
        e_task_bar_unset_message (E_TASK_BAR (i->data));
}

void
e_activity_handler_attach_task_bar (EActivityHandler *activity_handler,
                    ETaskBar *task_bar)
{
    EActivityHandlerPrivate *priv;

    g_return_if_fail (activity_handler != NULL);
    g_return_if_fail (E_IS_ACTIVITY_HANDLER (activity_handler));
    g_return_if_fail (task_bar != NULL);
    g_return_if_fail (E_IS_TASK_BAR (task_bar));

    priv = activity_handler->priv;

    g_object_weak_ref (G_OBJECT (task_bar), task_bar_destroy_notify, activity_handler);

    priv->task_bars = g_slist_prepend (priv->task_bars, task_bar);

    setup_task_bar (activity_handler, task_bar);
}

struct _cancel_wdata {
    EActivityHandler *handler;  
    ActivityInfo *info;
    guint id;
    void (*cancel)(gpointer);
    gpointer data;
};

static void 
cancel_wrapper (gpointer pdata)
{
    struct _cancel_wdata *data = (struct _cancel_wdata *) pdata;
    /* This can be invoked in two scenario. Either to cancel or to hide error */
    if (data->info->error) {
        /* Hide the error */
        EActivityHandler *handler = data->handler;
        int order, len;
        GSList *sp;
        GList *p = lookup_activity (handler->priv->activity_infos, data->id, &order);
        e_logger_log (handler->priv->logger, E_LOG_ERROR, g_object_get_data (data->info->error, "primary"), 
                            g_object_get_data (data->info->error, "secondary"));
        gtk_widget_destroy (data->info->error);
        printf("%p %p %d\n", data->info->error, data->info, data->info->id);
        data->info->error = NULL;
        for (sp = handler->priv->task_bars; sp != NULL; sp = sp->next) {
            ETaskBar *task_bar;

            task_bar = E_TASK_BAR (sp->data);
            e_task_bar_remove_task_from_id (task_bar, data->info->id);  
        }
        activity_info_free (data->info);
        len = g_list_length (handler->priv->activity_infos);
        handler->priv->activity_infos = g_list_remove_link (handler->priv->activity_infos, p);
        if (len == 1)
            handler->priv->activity_infos = NULL;
    } else {
        /* Cancel the operation */
        data->cancel (data->data);
    }
    /* No need to free the data. It will be freed as part of the task widget destroy */
}

/* CORBA methods.  */
guint  e_activity_handler_cancelable_operation_started  (EActivityHandler *activity_handler,
                              const char       *component_id,
                              GdkPixbuf        *icon_pixbuf,
                                  const char       *information,
                                  gboolean          cancellable,
                              void (*cancel_func)(gpointer),
                              gpointer user_data)
{
    EActivityHandlerPrivate *priv;
    ActivityInfo *activity_info;
    unsigned int activity_id;
    GSList *p;
    struct _cancel_wdata *data;
    gboolean bfree = FALSE;
    priv = activity_handler->priv;

    activity_id = get_new_activity_id (activity_handler);
    activity_info = activity_info_new (component_id, activity_id, icon_pixbuf, information, cancellable);

    data = g_new(struct _cancel_wdata, 1);
    data->handler = activity_handler;
    data->id = activity_id;
    data->info = activity_info;
    data->cancel = cancel_func;
    data->data = user_data;

    activity_info->cancel_func = cancel_wrapper;
    activity_info->data = data;
    for (p = priv->task_bars; p != NULL; p = p->next) {
        ETaskWidget *tw = task_widget_new_from_activity_info (activity_info);
        tw->id = activity_id;
        if (!bfree) {
            /* The data will be freed part of the widget destroy */
            g_object_set_data_full ((GObject *) tw, "free-data", data, g_free);
            bfree = TRUE;
        }
        e_task_bar_prepend_task (E_TASK_BAR (p->data), tw);
    }

    priv->activity_infos = g_list_prepend (priv->activity_infos, activity_info);

    return activity_id;

}

guint
e_activity_handler_operation_started (EActivityHandler *activity_handler,
                      const char *component_id,
                      GdkPixbuf *icon_pixbuf,
                      const char *information,
                      gboolean cancellable)
{
    EActivityHandlerPrivate *priv;
    ActivityInfo *activity_info;
    unsigned int activity_id;
    GSList *p;

    priv = activity_handler->priv;

    activity_id = get_new_activity_id (activity_handler);

    activity_info = activity_info_new (component_id, activity_id, icon_pixbuf, information, cancellable);

    for (p = priv->task_bars; p != NULL; p = p->next) {
        ETaskWidget *tw = task_widget_new_from_activity_info (activity_info);
        tw->id = activity_id;
        e_task_bar_prepend_task (E_TASK_BAR (p->data), tw);
    }

    priv->activity_infos = g_list_prepend (priv->activity_infos, activity_info);

    return activity_id;
}

static void
handle_error (ETaskWidget *task)
{
    GtkWidget *tool, *error;
    EActivityHandler *activity_handler;
    guint id;
    
    tool = g_object_get_data ((GObject *) task, "tool");
    error = g_object_get_data ((GObject *) task, "error");
    activity_handler = g_object_get_data ((GObject *) task, "activity-handler");
    id = GPOINTER_TO_UINT (g_object_get_data ((GObject *) task, "activity"));
    e_activity_handler_operation_finished (activity_handler, id);
    gtk_widget_show (error);
    e_logger_log (activity_handler->priv->logger, E_LOG_ERROR, g_object_get_data ((GObject *) error, "primary"), 
                    g_object_get_data ((GObject *) error, "secondary"));
}

static gboolean
error_cleanup (EActivityHandler *activity_handler)
{
    EActivityHandlerPrivate *priv = activity_handler->priv;
    GList *p, *node;
    GSList *sp;
    int i;
    time_t now = time (NULL);
    gboolean berror = FALSE;

    for (p = priv->activity_infos, i = 0; p != NULL; i++) {
        ActivityInfo *info;

        info = (ActivityInfo *) p->data;
        if (info->error)
            berror = TRUE;
        if (info->error && info->error_time && (now - info->error_time) > 5 ) {
            /* Error older than wanted time. So cleanup */
            e_logger_log (priv->logger, E_LOG_ERROR, g_object_get_data (info->error, "primary"), 
                            g_object_get_data (info->error, "secondary"));
            gtk_widget_destroy (info->error);
            node = p;
            p = p->next;

            for (sp = priv->task_bars; sp != NULL; sp = sp->next) {
                ETaskBar *task_bar;

                task_bar = E_TASK_BAR (sp->data);
                e_task_bar_remove_task_from_id (task_bar, info->id);
            }
            activity_info_free (info);
            priv->activity_infos = g_list_remove_link (priv->activity_infos, node);
        
        } else
            p = p->next;
    }
    if (!berror)
        priv->error_timer = 0;
    return berror;
}

guint
e_activity_handler_make_error (EActivityHandler *activity_handler,
                      const char *component_id,
                      const char *information,
                      GtkWidget  *error)
{
    EActivityHandlerPrivate *priv;
    ActivityInfo *activity_info;
    unsigned int activity_id;
    GSList *p;

    priv = activity_handler->priv;

    activity_id = get_new_activity_id (activity_handler);

    activity_info = activity_info_new (component_id, activity_id, NULL, information, TRUE);
    activity_info->error = error;
    activity_info->error_time = time (NULL);

    for (p = priv->task_bars; p != NULL; p = p->next) {
        ETaskBar *task_bar;
        ETaskWidget *task_widget;
        GtkWidget *tool;

        task_bar = E_TASK_BAR (p->data);
        task_widget = task_widget_new_from_activity_info (activity_info); 
        task_widget->id = activity_id;
        e_task_bar_prepend_task (E_TASK_BAR (p->data), task_widget);
        
        tool = e_task_widget_update_image (task_widget, "stock_dialog-warning", g_object_get_data ((GObject *) error, "primary"));
        g_object_set_data ((GObject *) task_widget, "tool", tool);
        g_object_set_data ((GObject *) task_widget, "error", error);
        g_object_set_data ((GObject *) task_widget, "activity-handler", activity_handler);
        g_object_set_data ((GObject *) task_widget, "activity", GINT_TO_POINTER(activity_id));
        g_signal_connect_swapped (tool, "clicked", G_CALLBACK(handle_error), task_widget);      
    }

    priv->activity_infos = g_list_prepend (priv->activity_infos, activity_info);

    if (!activity_handler->priv->error_timer)
        activity_handler->priv->error_timer = g_timeout_add (activity_handler->priv->error_flush_interval, (GSourceFunc)error_cleanup, activity_handler);

    return activity_id;
}

void
e_activity_handler_operation_set_error(EActivityHandler *activity_handler,
                      guint activity_id,
                      GtkWidget *error)
{
    EActivityHandlerPrivate *priv = activity_handler->priv;
    ActivityInfo *activity_info;
    GList *p;
    GSList *sp;
    int order_number;

    p = lookup_activity (priv->activity_infos, activity_id, &order_number);
    if (p == NULL) {
        g_warning ("EActivityHandler: unknown operation %d", activity_id);
        return;
    }

    activity_info = (ActivityInfo *) p->data;
    activity_info->error = error;
    activity_info->error_time = time (NULL);
    for (sp = priv->task_bars; sp != NULL; sp = sp->next) {
        ETaskBar *task_bar;
        ETaskWidget *task_widget;
        GtkWidget *tool;

        task_bar = E_TASK_BAR (sp->data);
        task_widget = e_task_bar_get_task_widget_from_id (task_bar, activity_info->id);
        if (!task_widget)
            continue;

        tool = e_task_widget_update_image (task_widget, "stock_dialog-warning", g_object_get_data ((GObject *) error, "primary"));
        g_object_set_data ((GObject *) task_widget, "tool", tool);
        g_object_set_data ((GObject *) task_widget, "error", error);
        g_object_set_data ((GObject *) task_widget, "activity-handler", activity_handler);
        g_object_set_data ((GObject *) task_widget, "activity", GINT_TO_POINTER(activity_id));
        g_signal_connect_swapped (tool, "clicked", G_CALLBACK(handle_error), task_widget);
    }

    if (!activity_handler->priv->error_timer)
        activity_handler->priv->error_timer = g_timeout_add (activity_handler->priv->error_flush_interval, (GSourceFunc) error_cleanup, activity_handler);
}

void
e_activity_handler_operation_progressing (EActivityHandler *activity_handler,
                      guint activity_id,
                      const char *information,
                      double progress)
{
    EActivityHandlerPrivate *priv = activity_handler->priv;
    ActivityInfo *activity_info;
    GList *p;
    GSList *sp;
    int order_number;

    p = lookup_activity (priv->activity_infos, activity_id, &order_number);
    if (p == NULL) {
        g_warning ("EActivityHandler: unknown operation %d", activity_id);
        return;
    }

    activity_info = (ActivityInfo *) p->data;

    g_free (activity_info->information);
    activity_info->information = g_strdup (information);

    activity_info->progress = progress;

    for (sp = priv->task_bars; sp != NULL; sp = sp->next) {
        ETaskBar *task_bar;
        ETaskWidget *task_widget;

        task_bar = E_TASK_BAR (sp->data);
        task_widget = e_task_bar_get_task_widget_from_id (task_bar, activity_info->id);
        if (!task_widget)
            continue;

        e_task_widget_update (task_widget, information, progress);
    }
}

void
e_activity_handler_operation_finished (EActivityHandler *activity_handler,
                       guint activity_id)
{
    EActivityHandlerPrivate *priv = activity_handler->priv;
    GList *p;
    GSList *sp;
    int order_number;

    p = lookup_activity (priv->activity_infos, activity_id, &order_number);
    if (p == NULL) {
        g_warning ("e_activity_handler_operation_finished: Unknown activity %d\n", activity_id);
        return;
    }

    activity_info_free ((ActivityInfo *) p->data);
    priv->activity_infos = g_list_remove_link (priv->activity_infos, p);

    for (sp = priv->task_bars; sp != NULL; sp = sp->next) {
        ETaskBar *task_bar;

        task_bar = E_TASK_BAR (sp->data);
        e_task_bar_remove_task_from_id (task_bar, activity_id);
    }
}