aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-alert-bar.c
blob: 8fa7eff6ba394bdb089ce277289e96e2f6497b29 (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
/*
 * e-alert-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/>
 *
 */

#include "e-alert-bar.h"

#include <config.h>
#include <glib/gi18n-lib.h>

/* GTK_ICON_SIZE_DIALOG is a tad too big. */
#define ICON_SIZE   GTK_ICON_SIZE_DND

struct _EAlertBarPrivate {
    GQueue alerts;
    GtkWidget *image;       /* not referenced */
    GtkWidget *primary_label;   /* not referenced */
    GtkWidget *secondary_label; /* not referenced */
};

G_DEFINE_TYPE (
    EAlertBar,
    e_alert_bar,
    GTK_TYPE_INFO_BAR)

static void
alert_bar_show_alert (EAlertBar *alert_bar)
{
    GtkImage *image;
    GtkLabel *label;
    GtkInfoBar *info_bar;
    GtkWidget *action_area;
    EAlert *alert;
    GList *actions;
    GList *children;
    GtkMessageType message_type;
    const gchar *stock_id;
    const gchar *text;
    gint response_id;

    info_bar = GTK_INFO_BAR (alert_bar);
    action_area = gtk_info_bar_get_action_area (info_bar);

    alert = g_queue_peek_head (&alert_bar->priv->alerts);
    g_return_if_fail (E_IS_ALERT (alert));

    /* Remove all buttons from the previous alert. */
    children = gtk_container_get_children (GTK_CONTAINER (action_area));
    while (children != NULL) {
        GtkWidget *child = GTK_WIDGET (children->data);
        gtk_container_remove (GTK_CONTAINER (action_area), child);
        children = g_list_delete_link (children, children);
    }

    /* Add new buttons. */
    actions = e_alert_peek_actions (alert);
    while (actions != NULL) {
        GtkWidget *button;

        /* These actions are already wired to trigger an
         * EAlert::response signal when activated, which
         * will in turn call gtk_info_bar_response(), so
         * we can add buttons directly to the action
         * area without knowning their response IDs. */

        button = gtk_button_new ();

        gtk_activatable_set_related_action (
            GTK_ACTIVATABLE (button),
            GTK_ACTION (actions->data));

        gtk_box_pack_end (
            GTK_BOX (action_area),
            button, FALSE, FALSE, 0);

        actions = g_list_next (actions);
    }

    response_id = e_alert_get_default_response (alert);
    gtk_info_bar_set_default_response (info_bar, response_id);

    message_type = e_alert_get_message_type (alert);
    gtk_info_bar_set_message_type (info_bar, message_type);

    text = e_alert_get_primary_text (alert);
    label = GTK_LABEL (alert_bar->priv->primary_label);
    gtk_label_set_text (label, text);

    text = e_alert_get_secondary_text (alert);
    label = GTK_LABEL (alert_bar->priv->secondary_label);
    gtk_label_set_text (label, text);

    stock_id = e_alert_get_stock_id (alert);
    image = GTK_IMAGE (alert_bar->priv->image);
    gtk_image_set_from_stock (image, stock_id, ICON_SIZE);

    gtk_widget_show (GTK_WIDGET (alert_bar));
}

static void
alert_bar_response_cb (EAlert *alert,
                       gint response_id,
                       EAlertBar *alert_bar)
{
    GQueue *queue;
    EAlert *head;
    GList *link;
    gboolean was_head;

    queue = &alert_bar->priv->alerts;
    head = g_queue_peek_head (queue);
    was_head = (alert == head);

    g_signal_handlers_disconnect_by_func (
        alert, alert_bar_response_cb, alert_bar);

    /* XXX Would be easier if g_queue_remove() returned a boolean. */
    link = g_queue_find (queue, alert);
    if (link != NULL) {
        g_queue_delete_link (queue, link);
        g_object_unref (alert);
    }

    if (g_queue_is_empty (queue))
        gtk_widget_hide (GTK_WIDGET (alert_bar));
    else if (was_head) {
        GtkInfoBar *info_bar = GTK_INFO_BAR (alert_bar);
        gtk_info_bar_response (info_bar, response_id);
        alert_bar_show_alert (alert_bar);
    }
}

static void
alert_bar_dispose (GObject *object)
{
    EAlertBarPrivate *priv;

    priv = E_ALERT_BAR (object)->priv;

    while (!g_queue_is_empty (&priv->alerts)) {
        EAlert *alert = g_queue_pop_head (&priv->alerts);
        g_signal_handlers_disconnect_by_func (
            alert, alert_bar_response_cb, object);
        g_object_unref (alert);
    }

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

static void
e_alert_bar_class_init (EAlertBarClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EAlertBarPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = alert_bar_dispose;
}

static void
e_alert_bar_init (EAlertBar *alert_bar)
{
    GtkWidget *container;
    GtkWidget *widget;
    PangoAttribute *attr;
    PangoAttrList *attr_list;

    alert_bar->priv = G_TYPE_INSTANCE_GET_PRIVATE (alert_bar, E_TYPE_ALERT_BAR, EAlertBarPrivate);

    g_queue_init (&alert_bar->priv->alerts);

    container = gtk_info_bar_get_content_area (GTK_INFO_BAR (alert_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_misc_set_alignment (GTK_MISC (widget), 0.5, 0.0);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    alert_bar->priv->image = widget;
    gtk_widget_show (widget);

    widget = gtk_vbox_new (FALSE, 12);
    gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
    gtk_widget_show (widget);

    container = widget;

    attr_list = pango_attr_list_new ();
    attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
    pango_attr_list_insert (attr_list, attr);

    widget = gtk_label_new (NULL);
    gtk_label_set_attributes (GTK_LABEL (widget), attr_list);
    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);
    alert_bar->priv->primary_label = widget;
    gtk_widget_show (widget);

    pango_attr_list_unref (attr_list);

    attr_list = pango_attr_list_new ();
    attr = pango_attr_scale_new  (PANGO_SCALE_SMALL);
    pango_attr_list_insert (attr_list, attr);

    widget = gtk_label_new (NULL);
    gtk_label_set_attributes (GTK_LABEL (widget), attr_list);
    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);
    alert_bar->priv->secondary_label = widget;
    gtk_widget_show (widget);

    pango_attr_list_unref (attr_list);
}

GtkWidget *
e_alert_bar_new (void)
{
    return g_object_new (E_TYPE_ALERT_BAR, NULL);
}

typedef struct {
    gboolean found;
    EAlert *looking_for;
} DuplicateData;

static void
alert_bar_find_duplicate_cb (EAlert *alert,
                             DuplicateData *dd)
{
    g_return_if_fail (dd->looking_for != NULL);

    dd->found |= (
        e_alert_get_message_type (alert) ==
        e_alert_get_message_type (dd->looking_for) &&
        g_strcmp0 (e_alert_get_primary_text (alert),
        e_alert_get_primary_text (dd->looking_for)) == 0 &&
        g_strcmp0 (e_alert_get_secondary_text (alert),
        e_alert_get_secondary_text (dd->looking_for)) == 0);
}

void
e_alert_bar_add_alert (EAlertBar *alert_bar,
                       EAlert *alert)
{
    DuplicateData dd;

    g_return_if_fail (E_IS_ALERT_BAR (alert_bar));
    g_return_if_fail (E_IS_ALERT (alert));

    dd.found = FALSE;
    dd.looking_for = alert;

    g_queue_foreach (
        &alert_bar->priv->alerts,
        (GFunc) alert_bar_find_duplicate_cb, &dd);

    if (dd.found)
        return;

    g_signal_connect (
        alert, "response",
        G_CALLBACK (alert_bar_response_cb), alert_bar);

    g_queue_push_head (&alert_bar->priv->alerts, g_object_ref (alert));

    alert_bar_show_alert (alert_bar);
}