File: information.c

package info (click to toggle)
balsa 2.4.12-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 21,456 kB
  • ctags: 8,303
  • sloc: ansic: 93,724; xml: 13,662; sh: 11,146; makefile: 615; awk: 60
file content (206 lines) | stat: -rw-r--r-- 6,633 bytes parent folder | download | duplicates (2)
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
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
 *
 * Copyright (C) 1997-2007 Stuart Parmenter and others,
 *                         See the file AUTHORS for a list.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version.
 *  
 * 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., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */

#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif                          /* HAVE_CONFIG_H */
#include "information.h"

#ifdef HAVE_NOTIFY
#include <libnotify/notify.h>
#include <gtk/gtk.h>
#endif
#include <string.h>

struct information_data {
    GtkWindow *parent;
    LibBalsaInformationType message_type;
    gchar *msg;
};

static gboolean libbalsa_information_idle_handler(struct information_data*);

LibBalsaInformationFunc libbalsa_real_information_func;

#ifdef HAVE_NOTIFY
static void lbi_notification_closed_cb(NotifyNotification * note,
                                       gpointer data);

static void
lbi_notification_parent_weak_notify(gpointer data, GObject * parent)
{
    NotifyNotification *note = NOTIFY_NOTIFICATION(data);
    g_signal_handlers_disconnect_by_func(note, lbi_notification_closed_cb,
                                         parent);
    notify_notification_close(note, NULL);
    g_object_unref(note);
}

static void
lbi_notification_closed_cb(NotifyNotification * note, gpointer data)
{
    GObject *parent = G_OBJECT(data);
    g_object_weak_unref(parent, lbi_notification_parent_weak_notify, note);
    g_object_unref(note);
}
#endif

/*
 * We are adding an idle handler - we do not need to hold the gdk lock
 * for this.
 *
 * We can't just grab the GDK lock and call the real_error function
 * since this runs a dialog, which has a nested g_main loop - glib
 * doesn't like haveing main loops active in two threads at one
 * time. When the idle handler gets run it is from the main thread.
 *
 */
void
libbalsa_information_varg(GtkWindow *parent, LibBalsaInformationType type,
                          const char *fmt, va_list ap)
{
    struct information_data *data;

    g_return_if_fail(fmt != NULL);
    g_assert(libbalsa_real_information_func != NULL);

#ifdef HAVE_NOTIFY
    if (notify_is_initted()) {
        gchar *msg, *p, *q;
        GString *escaped;
        NotifyNotification *note;
        char *icon_str;

        switch (type) {
        case LIBBALSA_INFORMATION_MESSAGE:
            icon_str = GTK_STOCK_DIALOG_INFO;
            break;
        case LIBBALSA_INFORMATION_WARNING:
            icon_str = GTK_STOCK_DIALOG_WARNING;
            break;
        case LIBBALSA_INFORMATION_ERROR:
            icon_str = GTK_STOCK_DIALOG_ERROR;
            break;
        default:
            icon_str = NULL;
            break;
        }
        msg = g_strdup_vprintf(fmt, ap);
        /* libnotify/DBUS uses HTML markup, so we must replace '<' and
         * '&' with the corresponding entity in the message string. */
        escaped = g_string_new(NULL);
        for (p = msg; (q = strpbrk(p, "<>&\"")) != NULL; p = ++q) {
            g_string_append_len(escaped, p, q - p);
            switch (*q) {
                case '<': g_string_append(escaped, "&lt;");   break;
                case '>': g_string_append(escaped, "&gt;");   break;
                case '&': g_string_append(escaped, "&amp;");  break;
                case '"': g_string_append(escaped, "&quot;"); break;
                default: break;
            }
        }
        g_string_append(escaped, p);
        g_free(msg);

#if HAVE_NOTIFY >= 7
        note = notify_notification_new("Balsa", escaped->str, icon_str);
#else
        /* prior to 0.7.0 */
        note = notify_notification_new("Balsa", escaped->str, icon_str, NULL);
#endif

        g_string_free(escaped, TRUE);

        notify_notification_set_timeout(note, 7000);    /* 7 seconds */
        notify_notification_show(note, NULL);
        if (parent) {
            /* Close with parent if earlier. */
            g_object_weak_ref(G_OBJECT(parent),
                              lbi_notification_parent_weak_notify, note);
            g_signal_connect(note, "closed",
                             G_CALLBACK(lbi_notification_closed_cb),
                             parent);
        } else
            g_object_unref(note);
        return;
    }
    /* Fall through to the ordinary notification scheme */
#endif
    data = g_new(struct information_data, 1);
    data->parent = parent;
    data->message_type = type;

    /* We format the string here. It must be free()d in the idle
     * handler We parse the args here because by the time the idle
     * function runs we will no longer be in this stack frame. 
     */
    data->msg = g_strdup_vprintf(fmt, ap);
    if (parent)
        g_object_add_weak_pointer(G_OBJECT(parent),
                                  (gpointer) & data->parent);
    g_idle_add((GSourceFunc) libbalsa_information_idle_handler, data);
}

void
libbalsa_information(LibBalsaInformationType type,
                     const char *fmt, ...)
{
    va_list va_args;

#ifndef DEBUG
    if (type == LIBBALSA_INFORMATION_DEBUG) return;
#endif
    va_start(va_args, fmt);
    libbalsa_information_varg(NULL, type, fmt, va_args);
    va_end(va_args);
}

void
libbalsa_information_parented(GtkWindow *parent, LibBalsaInformationType type,
                              const char *fmt, ...)
{
    va_list va_args;

    va_start(va_args, fmt);
    libbalsa_information_varg(parent, type, fmt, va_args);
    va_end(va_args);
}

/*
 * This is an idle handler, so we need to grab the GDK lock 
 */
static gboolean
libbalsa_information_idle_handler(struct information_data *data)
{
    gdk_threads_enter();
    libbalsa_real_information_func(data->parent,
                                   data->message_type,
                                   data->msg);
    gdk_threads_leave();

    if(data->parent)
        g_object_remove_weak_pointer(G_OBJECT(data->parent), 
                                     (gpointer) &data->parent);
    g_free(data->msg);
    g_free(data);
    return FALSE;
}