aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-taskbar.c
blob: 0910ea98d696c1a6d3ba728e24c06cadac4dea18 (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
/*
 * e-shell-taskbar.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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

/**
 * SECTION: e-shell-taskbar
 * @short_description: the bottom of the main window
 * @include: shell/e-shell-taskbar.h
 **/

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

#include "e-shell-taskbar.h"

#include <libebackend/libebackend.h>

#include <e-shell-view.h>

#define E_SHELL_TASKBAR_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_SHELL_TASKBAR, EShellTaskbarPrivate))

struct _EShellTaskbarPrivate {

    gpointer shell_view;  /* weak pointer */

    /* Keep a reference to the shell backend since
     * we connect to its "activity-added" signal. */
    EShellBackend *shell_backend;

    GtkWidget *label;
    GtkWidget *hbox;

    GHashTable *proxy_table;

    gint fixed_height;

    /* Basically the main() thread, aka UI thread, where the gtk calls
     * can be done. */
    GThread *main_thread;
};

enum {
    PROP_0,
    PROP_MESSAGE,
    PROP_SHELL_VIEW
};

G_DEFINE_TYPE_WITH_CODE (
    EShellTaskbar,
    e_shell_taskbar,
    GTK_TYPE_HBOX,
    G_IMPLEMENT_INTERFACE (
        E_TYPE_EXTENSIBLE, NULL))

typedef struct {
    EShellTaskbar *shell_taskbar;
    EActivity *activity;
    GtkWidget *proxy; /* not referenced */
} EShellTaskbarIdleData;

static void
shell_taskbar_free_idle_data (gpointer data)
{
    EShellTaskbarIdleData *idle_data = data;

    g_clear_object (&idle_data->shell_taskbar);
    g_clear_object (&idle_data->activity);
    g_free (idle_data);
}

static void
shell_taskbar_remove_proxy_container (EShellTaskbar *shell_taskbar,
                                      GtkWidget *proxy)
{
    GList *children;
    GtkContainer *container;

    container = GTK_CONTAINER (shell_taskbar->priv->hbox);
    gtk_container_remove (container, proxy);

    children = gtk_container_get_children (container);

    if (children == NULL)
        gtk_widget_hide (GTK_WIDGET (container));

    g_list_free (children);
}

static gboolean
shell_taskbar_remove_proxy_container_idle_cb (gpointer user_data)
{
    EShellTaskbarIdleData *idle_data = user_data;

    g_return_val_if_fail (idle_data != NULL, FALSE);
    g_return_val_if_fail (idle_data->shell_taskbar != NULL, FALSE);
    g_return_val_if_fail (idle_data->proxy != NULL, FALSE);

    shell_taskbar_remove_proxy_container (idle_data->shell_taskbar, idle_data->proxy);

    return FALSE;
}

static void
shell_taskbar_weak_notify_cb (EShellTaskbar *shell_taskbar,
                              GObject *where_the_activity_was)
{
    GtkWidget *proxy;
    GHashTable *proxy_table;

    proxy_table = shell_taskbar->priv->proxy_table;
    proxy = g_hash_table_lookup (proxy_table, where_the_activity_was);
    g_hash_table_remove (proxy_table, where_the_activity_was);
    g_return_if_fail (proxy != NULL);

    if (shell_taskbar->priv->main_thread == g_thread_self ()) {
        shell_taskbar_remove_proxy_container (shell_taskbar, proxy);
    } else {
        EShellTaskbarIdleData *idle_data;

        idle_data = g_new0 (EShellTaskbarIdleData, 1);
        idle_data->shell_taskbar = g_object_ref (shell_taskbar);
        idle_data->proxy = proxy;

        g_idle_add_full (
            G_PRIORITY_DEFAULT,
            shell_taskbar_remove_proxy_container_idle_cb,
            idle_data, shell_taskbar_free_idle_data);
    }
}

static void
shell_taskbar_activity_add (EShellTaskbar *shell_taskbar,
                            EActivity *activity)
{
    GtkBox *box;
    GtkWidget *proxy;
    EActivityState state;
    GHashTable *proxy_table;

    /* Sanity check the activity state. */
    state = e_activity_get_state (activity);
    g_return_if_fail (state == E_ACTIVITY_RUNNING);

    /* Make sure it hasn't already been added. */
    proxy_table = shell_taskbar->priv->proxy_table;
    proxy = g_hash_table_lookup (proxy_table, activity);
    g_return_if_fail (proxy == NULL);

    /* Proxy widgets manage their own visibility.
     * Don't call gtk_widget_show() on it here. */
    proxy = e_activity_proxy_new (activity);
    box = GTK_BOX (shell_taskbar->priv->hbox);
    gtk_box_pack_start (box, proxy, TRUE, TRUE, 0);
    gtk_box_reorder_child (box, proxy, 0);
    gtk_widget_show (GTK_WIDGET (box));

    /* The proxy widget also holds a weak reference to the activity,
     * so the activity should get finalized in the normal course of
     * operation.  When that happens we remove the corresponding
     * proxy widget from the taskbar. */

    g_object_weak_ref (
        G_OBJECT (activity), (GWeakNotify)
        shell_taskbar_weak_notify_cb, shell_taskbar);

    g_hash_table_insert (proxy_table, activity, proxy);
}

static gboolean
shell_taskbar_add_activity_idle_cb (gpointer user_data)
{
    EShellTaskbarIdleData *idle_data = user_data;

    g_return_val_if_fail (idle_data != NULL, FALSE);
    g_return_val_if_fail (idle_data->shell_taskbar != NULL, FALSE);
    g_return_val_if_fail (idle_data->activity != NULL, FALSE);

    shell_taskbar_activity_add (idle_data->shell_taskbar, idle_data->activity);

    return FALSE;
}

static void
shell_taskbar_activity_added_cb (EShellTaskbar *shell_taskbar,
                                 EActivity *activity)
{
    if (shell_taskbar->priv->main_thread == g_thread_self ()) {
        shell_taskbar_activity_add (shell_taskbar, activity);
    } else {
        EShellTaskbarIdleData *idle_data;

        idle_data = g_new0 (EShellTaskbarIdleData, 1);
        idle_data->shell_taskbar = g_object_ref (shell_taskbar);
        idle_data->activity = g_object_ref (activity);

        g_idle_add_full (
            G_PRIORITY_DEFAULT,
            shell_taskbar_add_activity_idle_cb,
            idle_data, shell_taskbar_free_idle_data);
    }
}

static gboolean
shell_taskbar_weak_unref (EActivity *activity,
                          EActivityProxy *proxy,
                          EShellTaskbar *shell_taskbar)
{
    g_object_weak_unref (
        G_OBJECT (activity), (GWeakNotify)
        shell_taskbar_weak_notify_cb, shell_taskbar);

    return TRUE;
}

static void
shell_taskbar_set_shell_view (EShellTaskbar *shell_taskbar,
                              EShellView *shell_view)
{
    g_return_if_fail (shell_taskbar->priv->shell_view == NULL);

    shell_taskbar->priv->shell_view = shell_view;

    g_object_add_weak_pointer (
        G_OBJECT (shell_view),
        &shell_taskbar->priv->shell_view);
}

static void
shell_taskbar_set_property (GObject *object,
                            guint property_id,
                            const GValue *value,
                            GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_MESSAGE:
            e_shell_taskbar_set_message (
                E_SHELL_TASKBAR (object),
                g_value_get_string (value));
            return;

        case PROP_SHELL_VIEW:
            shell_taskbar_set_shell_view (
                E_SHELL_TASKBAR (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
shell_taskbar_get_property (GObject *object,
                            guint property_id,
                            GValue *value,
                            GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_MESSAGE:
            g_value_set_string (
                value, e_shell_taskbar_get_message (
                E_SHELL_TASKBAR (object)));
            return;

        case PROP_SHELL_VIEW:
            g_value_set_object (
                value, e_shell_taskbar_get_shell_view (
                E_SHELL_TASKBAR (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
shell_taskbar_dispose (GObject *object)
{
    EShellTaskbarPrivate *priv;

    priv = E_SHELL_TASKBAR_GET_PRIVATE (object);

    g_hash_table_foreach_remove (
        priv->proxy_table, (GHRFunc)
        shell_taskbar_weak_unref, object);

    if (priv->shell_view != NULL) {
        g_object_remove_weak_pointer (
            G_OBJECT (priv->shell_view), &priv->shell_view);
        priv->shell_view = NULL;
    }

    if (priv->shell_backend != NULL) {
        g_signal_handlers_disconnect_matched (
            priv->shell_backend, G_SIGNAL_MATCH_DATA,
            0, 0, NULL, NULL, object);
    }

    g_clear_object (&priv->shell_backend);
    g_clear_object (&priv->label);
    g_clear_object (&priv->hbox);

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

static void
shell_taskbar_finalize (GObject *object)
{
    EShellTaskbarPrivate *priv;

    priv = E_SHELL_TASKBAR_GET_PRIVATE (object);

    g_hash_table_destroy (priv->proxy_table);

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

static void
shell_taskbar_constructed (GObject *object)
{
    EShellView *shell_view;
    EShellBackend *shell_backend;
    EShellTaskbar *shell_taskbar;

    shell_taskbar = E_SHELL_TASKBAR (object);
    shell_view = e_shell_taskbar_get_shell_view (shell_taskbar);
    shell_backend = e_shell_view_get_shell_backend (shell_view);

    /* Keep a reference to the shell backend so we can
     * disconnect the signal handler during dispose(). */
    shell_taskbar->priv->shell_backend = g_object_ref (shell_backend);

    g_signal_connect_swapped (
        shell_backend, "activity-added",
        G_CALLBACK (shell_taskbar_activity_added_cb), shell_taskbar);

    e_extensible_load_extensions (E_EXTENSIBLE (object));

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

static void
shell_taskbar_size_allocate (GtkWidget *widget,
                             GtkAllocation *allocation)
{
    EShellTaskbar *shell_taskbar;
    gint fixed_height;

    shell_taskbar = E_SHELL_TASKBAR (widget);

    /* Maximum height allocation sticks. */
    fixed_height = shell_taskbar->priv->fixed_height;
    fixed_height = MAX (fixed_height, allocation->height);
    shell_taskbar->priv->fixed_height = fixed_height;

    /* Chain up to parent's size_allocate() method. */
    GTK_WIDGET_CLASS (e_shell_taskbar_parent_class)->
        size_allocate (widget, allocation);
}

static void
shell_taskbar_get_preferred_height (GtkWidget *widget,
                                    gint *minimum_height,
                                    gint *natural_height)
{
    EShellTaskbar *shell_taskbar;

    shell_taskbar = E_SHELL_TASKBAR (widget);

    if (minimum_height != NULL)
        *minimum_height = shell_taskbar->priv->fixed_height;

    if (natural_height != NULL)
        *natural_height = shell_taskbar->priv->fixed_height;
}

static void
shell_taskbar_get_preferred_width (GtkWidget *widget,
                                   gint *minimum_width,
                                   gint *natural_width)
{
    /* to never get larger than allocated size (which changes window width) */

    if (minimum_width != NULL)
        *minimum_width = 1;

    if (natural_width != NULL)
        *natural_width = 1;
}

static void
e_shell_taskbar_class_init (EShellTaskbarClass *class)
{
    GObjectClass *object_class;
    GtkWidgetClass *widget_class;

    g_type_class_add_private (class, sizeof (EShellTaskbarPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = shell_taskbar_set_property;
    object_class->get_property = shell_taskbar_get_property;
    object_class->dispose = shell_taskbar_dispose;
    object_class->finalize = shell_taskbar_finalize;
    object_class->constructed = shell_taskbar_constructed;

    widget_class = GTK_WIDGET_CLASS (class);
    widget_class->size_allocate = shell_taskbar_size_allocate;
    widget_class->get_preferred_height = shell_taskbar_get_preferred_height;
    widget_class->get_preferred_width = shell_taskbar_get_preferred_width;

    /**
     * EShellTaskbar:message
     *
     * The message to display in the taskbar.
     **/
    g_object_class_install_property (
        object_class,
        PROP_MESSAGE,
        g_param_spec_string (
            "message",
            NULL,
            NULL,
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT |
            G_PARAM_STATIC_STRINGS));

    /**
     * EShellTaskbar:shell-view
     *
     * The #EShellView to which the taskbar widget belongs.
     **/
    g_object_class_install_property (
        object_class,
        PROP_SHELL_VIEW,
        g_param_spec_object (
            "shell-view",
            NULL,
            NULL,
            E_TYPE_SHELL_VIEW,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));
}

static void
e_shell_taskbar_init (EShellTaskbar *shell_taskbar)
{
    GtkWidget *widget;

    shell_taskbar->priv = E_SHELL_TASKBAR_GET_PRIVATE (shell_taskbar);
    shell_taskbar->priv->proxy_table = g_hash_table_new (NULL, NULL);
    shell_taskbar->priv->main_thread = g_thread_self ();

    gtk_box_set_spacing (GTK_BOX (shell_taskbar), 12);

    widget = gtk_label_new (NULL);
    gtk_label_set_ellipsize (GTK_LABEL (widget), PANGO_ELLIPSIZE_END);
    gtk_box_pack_start (GTK_BOX (shell_taskbar), widget, TRUE, TRUE, 0);
    gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
    shell_taskbar->priv->label = g_object_ref (widget);
    gtk_widget_hide (widget);

    widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
    gtk_box_pack_start (GTK_BOX (shell_taskbar), widget, TRUE, TRUE, 0);
    shell_taskbar->priv->hbox = g_object_ref (widget);
    gtk_widget_hide (widget);
}

/**
 * e_shell_taskbar_new:
 * @shell_view: an #EShellView
 *
 * Creates a new #EShellTaskbar instance belonging to @shell_view.
 *
 * Returns: a new #EShellTaskbar instance
 **/
GtkWidget *
e_shell_taskbar_new (EShellView *shell_view)
{
    g_return_val_if_fail (E_IS_SHELL_VIEW (shell_view), NULL);

    return g_object_new (
        E_TYPE_SHELL_TASKBAR, "shell-view", shell_view, NULL);
}

/**
 * e_shell_taskbar_get_shell_view:
 * @shell_taskbar: an #EShellTaskbar
 *
 * Returns the #EShellView that was passed to e_shell_taskbar_new().
 *
 * Returns: the #EShellView to which @shell_taskbar belongs
 **/
EShellView *
e_shell_taskbar_get_shell_view (EShellTaskbar *shell_taskbar)
{
    g_return_val_if_fail (E_IS_SHELL_TASKBAR (shell_taskbar), NULL);

    return shell_taskbar->priv->shell_view;
}

/**
 * e_shell_taskbar_get_message:
 * @shell_taskbar: an #EShellTaskbar
 *
 * Returns the message currently shown in the taskbar, or an empty string
 * if no message is shown.  Taskbar messages are used primarily for menu
 * tooltips.
 *
 * Returns: the current taskbar message
 **/
const gchar *
e_shell_taskbar_get_message (EShellTaskbar *shell_taskbar)
{
    GtkWidget *label;

    g_return_val_if_fail (E_IS_SHELL_TASKBAR (shell_taskbar), NULL);

    label = shell_taskbar->priv->label;

    return gtk_label_get_text (GTK_LABEL (label));
}

/**
 * e_shell_taskbar_set_message:
 * @shell_taskbar: an #EShellTaskbar
 * @message: the message to show
 *
 * Shows a message in the taskbar.  If @message is %NULL or an empty string,
 * the taskbar message is cleared.  Taskbar messages are used primarily for
 * menu tooltips.
 **/
void
e_shell_taskbar_set_message (EShellTaskbar *shell_taskbar,
                             const gchar *message)
{
    GtkWidget *label;

    g_return_if_fail (E_IS_SHELL_TASKBAR (shell_taskbar));

    label = shell_taskbar->priv->label;
    gtk_label_set_text (GTK_LABEL (label), message);

    if (message != NULL && *message != '\0')
        gtk_widget_show (label);
    else
        gtk_widget_hide (label);

    g_object_notify (G_OBJECT (shell_taskbar), "message");
}

/**
 * e_shell_taskbar_unset_message:
 * @shell_taskbar: an #EShellTaskbar
 *
 * This is equivalent to passing a %NULL message to
 * e_shell_taskbar_set_message().
 **/
void
e_shell_taskbar_unset_message (EShellTaskbar *shell_taskbar)
{
    g_return_if_fail (E_IS_SHELL_TASKBAR (shell_taskbar));

    e_shell_taskbar_set_message (shell_taskbar, NULL);
}

/**
 * e_shell_taskbar_get_activity_count:
 * @shell_taskbar: an #EShellTaskbar
 *
 * Returns the number of active #EActivity instances being tracked.
 *
 * Returns: the number of #EActivity instances
 **/
guint
e_shell_taskbar_get_activity_count (EShellTaskbar *shell_taskbar)
{
    g_return_val_if_fail (E_IS_SHELL_TASKBAR (shell_taskbar), 0);

    return g_hash_table_size (shell_taskbar->priv->proxy_table);
}