File: information.c

package info (click to toggle)
balsa 2.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,576 kB
  • sloc: ansic: 99,871; xml: 4,934; makefile: 769; sh: 185; awk: 60; python: 34
file content (193 lines) | stat: -rw-r--r-- 5,381 bytes parent folder | download | duplicates (3)
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
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
 *
 * Copyright (C) 1997-2021 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, see <https://www.gnu.org/licenses/>.
 */

#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif                          /* HAVE_CONFIG_H */
#include "information.h"
#include "libbalsa-conf.h"
#include <string.h>
#include <glib/gi18n.h>

static GApplication *notify_app = NULL;
static const gchar *notify_title = NULL;
static const gchar *id_base = NULL;
static GHashTable *hide_ids = NULL;
static gint serial = 0;


static void libbalsa_information_varg(LibBalsaInformationType  type,
									  const gchar             *hide_id,
									  const gchar             *fmt,
									  va_list                  ap);
static void add_hide_id(GSimpleAction *simple,
						GVariant      *parameter,
						gpointer       user_data);


void
libbalsa_information_init(GApplication *application, const gchar *title, const gchar *notification_id)
{
	GActionEntry actions[] = {
		{ "hide-notify", add_hide_id, "s", NULL, NULL },
	};
	gint hide_cnt;
	gchar **hide_list;

	g_return_if_fail(G_IS_APPLICATION(application) && (title != NULL) && (notification_id != NULL));

	g_action_map_add_action_entries(G_ACTION_MAP(application), actions, 1, NULL);
	notify_app = application;
	notify_title = title;
	id_base = notification_id;
	hide_ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
	libbalsa_conf_push_group("Notifications");
	libbalsa_conf_get_vector_with_default("hide", &hide_cnt, &hide_list, NULL);
	if (hide_list != NULL) {
		gint n;

		for (n = 0; hide_list[n] != NULL; n++) {
			g_hash_table_add(hide_ids, hide_list[n]);
		}
		g_free(hide_list);	/* note: strings consumed by the hash table */
	}
	libbalsa_conf_pop_group();
}

void
libbalsa_information_save_cfg(void)
{
	if (g_hash_table_size(hide_ids) > 0U) {
		gchar **ids;
		guint count;

		libbalsa_conf_push_group("Notifications");
		ids = (gchar **) g_hash_table_get_keys_as_array(hide_ids, &count);
		libbalsa_conf_set_vector("hide", (int) count, (const gchar **) ids);
		g_free(ids);
		libbalsa_conf_pop_group();
	}
}

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

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

void
libbalsa_information_may_hide(LibBalsaInformationType type,
							  const gchar *hide_id, const gchar *fmt, ...)
{
	va_list va_args;

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

void
libbalsa_information_shutdown(void)
{
	gint last_serial;

	if (hide_ids != NULL) {
		g_hash_table_destroy(hide_ids);
		hide_ids = NULL;
	}

	/* withdraw the last notification sent... */
	last_serial = g_atomic_int_get(&serial);
	if (last_serial > 0) {
		gchar *id;

		id = g_strdup_printf("%s%d", id_base, last_serial - 1);
		g_application_withdraw_notification(notify_app, id);
		g_free(id);
	}
	notify_app = NULL;		/* ...so calling libbalsa_information() later will fail */
}

static void
libbalsa_information_varg(LibBalsaInformationType type,
						  const gchar *hide_id,
						  const gchar *fmt, va_list ap)
{
	GNotification *notification;
	gchar *msg;
	gchar *id;
	const gchar *icon_str;

	g_return_if_fail(G_IS_APPLICATION(notify_app) && (notify_title != NULL) && (id_base != NULL) && (fmt != NULL));

	msg = g_strdup_vprintf(fmt, ap);
	if ((hide_id != NULL) && g_hash_table_contains(hide_ids, hide_id)) {
		g_debug("hide notification with id '%s': %s", hide_id, msg);
		g_free(msg);
		return;
	}

    switch (type) {
    case LIBBALSA_INFORMATION_MESSAGE:
        icon_str = "dialog-information";
        break;
    case LIBBALSA_INFORMATION_WARNING:
        icon_str = "dialog-warning";
        break;
    case LIBBALSA_INFORMATION_ERROR:
        icon_str = "dialog-error";
        break;
    default:
        icon_str = NULL;
        break;
    }

	notification = g_notification_new(notify_title);
	if (icon_str != NULL) {
		GIcon *icon;

		icon = g_themed_icon_new(icon_str);
		g_notification_set_icon(notification, icon);
		g_object_unref(icon);
	}

	g_notification_set_body(notification, msg);
	if (hide_id != NULL) {
		g_notification_add_button_with_target(notification,
			_("do not show again"), "app.hide-notify", "s", hide_id);
	}
	g_free(msg);

	id = g_strdup_printf("%s%d", id_base, g_atomic_int_add(&serial, 1));
	g_application_send_notification(notify_app, id, notification);
	g_free(id);
	g_object_unref(notification);
}

static void
add_hide_id(GSimpleAction *simple, GVariant *parameter, gpointer G_GNUC_UNUSED user_data)
{
	g_hash_table_add(hide_ids, g_variant_dup_string(parameter, NULL));
}