summaryrefslogtreecommitdiff
path: root/src/notification.c
blob: 3a5520261dda3af3e5450905cf73e00c68ca9200 (plain)
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
/*
 * notification.c
 *
 * Copyright (C) 2002 Sun Microsystems, Inc.
 * Copyright (C) 2006 Christian Persch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Authors: Glynn Foster <glynn.foster@sun.com>
 */

#include <config.h>

#include <unistd.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <time.h>
#include <string.h>
#ifdef HAVE_LIBNOTIFY
#include <libnotify/notify.h>

#include "zenity.h"
#include "util.h"

#define MAX_HINTS 16

static char *icon_file;
static GHashTable *notification_hints;

static void
on_notification_default_action (NotifyNotification *n,
                                const char         *action,
                                void               *user_data)
{
  ZenityData *zen_data;

  zen_data = (ZenityData *)user_data;
  notify_notification_close (n, NULL);

  zen_data->exit_code = zenity_util_return_exit_code (ZENITY_OK);

  gtk_main_quit ();
}

static GHashTable *
zenity_notification_parse_hints_array (gchar **hints)
{
  GHashTable *result;
  gchar** pair;
  int i;

  result = g_hash_table_new_full (g_str_hash,
                                  g_str_equal,
                                  g_free,
                                  g_free);

  for(i = 0; i < g_strv_length (hints); i++) {
    pair = g_strsplit (hints[i], ":", 2);
    g_hash_table_replace (result, g_strdup (pair[0]), g_strdup (pair[1]));
    g_strfreev (pair);
  }
  if (g_hash_table_size (result) == 0) {
    g_hash_table_unref (result);
    return NULL;
  } else {
    return result;
  }
}

static GHashTable *
zenity_notification_parse_hints (gchar *hints)
{
  GHashTable *result;
  gchar** hint_array;

  hint_array = g_strsplit (g_strcompress (hints), "\n", MAX_HINTS);
  result = zenity_notification_parse_hints_array (hint_array);
  g_strfreev (hint_array);
  return result;
}

static void
zenity_notification_set_hint(gpointer key, gpointer value, gpointer user_data)
{
  NotifyNotification *notification;
  gchar *hint_name;
  GVariant *hint_value;

  gchar *string_value;
  gboolean boolean_value;
  gint32 int_value;
  guchar byte_value;

  hint_name = (gchar *) key;
  string_value = (gchar *) value;
  notification = (NotifyNotification *) user_data;

  if ((g_ascii_strcasecmp ("action-icons", hint_name) == 0)
      || (g_ascii_strcasecmp ("resident", hint_name) == 0)
      || (g_ascii_strcasecmp ("suppress-sound", hint_name) == 0)
      || (g_ascii_strcasecmp ("transient", hint_name) == 0)) {
    /* boolean hints */
    if (g_ascii_strcasecmp ("true", string_value) == 0) {
      boolean_value = TRUE;
    } else if (g_ascii_strcasecmp ("false", string_value) == 0) {
      boolean_value = FALSE;
    } else {
      g_printerr (_("Invalid value for a boolean typed hint.\nSupported values are 'true' or 'false'.\n"));
      return;
    }
    hint_value = g_variant_new_boolean (boolean_value);
  } else if ((g_ascii_strcasecmp ("category", hint_name) == 0)
             || (g_ascii_strcasecmp ("desktop-entry", hint_name) == 0)
             || (g_ascii_strcasecmp ("image-path", hint_name) == 0)
             || (g_ascii_strcasecmp ("image_path", hint_name) == 0)
             || (g_ascii_strcasecmp ("sound-file", hint_name) == 0)
             || (g_ascii_strcasecmp ("sound-name", hint_name) == 0)) {
    /* string hints */
    hint_value = g_variant_new_string (string_value);
  } else if ((g_ascii_strcasecmp ("image-data", hint_name) == 0)
             || (g_ascii_strcasecmp ("image_data", hint_name) == 0)
             || (g_ascii_strcasecmp ("icon-data", hint_name) == 0)) {
    /* (iibiiay) */
    g_printerr (_("Unsupported hint. Skipping.\n"));
    return;
  } else if ((g_ascii_strcasecmp ("x", hint_name) == 0)
             || (g_ascii_strcasecmp ("y", hint_name) == 0)) {
    /* int hints */
    int_value = (gint32) g_ascii_strtoll (string_value, NULL, 0);
    hint_value = g_variant_new_int32 (int_value);
  } else if ((g_ascii_strcasecmp ("urgency", hint_name) == 0)) {
    /* byte hints */
    byte_value = (guchar) g_ascii_strtoll (string_value, NULL, 0);
    hint_value = g_variant_new_byte (byte_value);
  } else {
    /* unknown hints */
    g_printerr (_("Unknown hint name. Skipping.\n"));
    return;
  }

  notify_notification_set_hint (notification,
                                hint_name,
                                hint_value);
}

static void
zenity_notification_set_hints (NotifyNotification *notification, GHashTable *hints)
{
  if (hints == NULL) {
    return;
  }

  g_hash_table_foreach (hints, zenity_notification_set_hint, notification);
}

static gboolean
zenity_notification_handle_stdin (GIOChannel *channel,
				  GIOCondition condition,
				  gpointer user_data)
{
  ZenityData *zen_data;

  zen_data = (ZenityData *)user_data;

  if ((condition & G_IO_IN) != 0) {
    GString *string;
    GError *error = NULL;

    string = g_string_new (NULL);
    while (channel->is_readable == FALSE)
      ;
    do {
      gint status;
      gchar *command, *value, *colon;

      do {
	status = g_io_channel_read_line_string (channel, string, NULL, &error);
	while (gdk_events_pending ())
	  gtk_main_iteration ();

      } while (status == G_IO_STATUS_AGAIN);

      if (status  != G_IO_STATUS_NORMAL) {
	if (error) {
	  g_warning ("zenity_notification_handle_stdin () : %s",
		     error->message);
	  g_error_free (error);
	  error = NULL;
	}
	continue;
      }

      zenity_util_strip_newline (string->str);
      colon = strchr(string->str, ':');
      if (colon == NULL) {
	g_printerr (_("Could not parse command from stdin\n"));
	continue;
      }
      /* split off the command and value */
      command = g_strstrip (g_strndup (string->str, colon - string->str));

      value = colon + 1;
      while (*value && g_ascii_isspace (*value)) value++;

      if (!g_ascii_strcasecmp (command, "icon")) {
        g_free (icon_file);
        icon_file = g_strdup (value);
      } else if (!g_ascii_strcasecmp (command, "hints")) {
        if (notification_hints != NULL) {
          g_hash_table_unref (notification_hints);
        }
        notification_hints = zenity_notification_parse_hints (value);
      } else if (!g_ascii_strcasecmp (command, "message")) {
        /* display a notification bubble */
        if (!g_utf8_validate (value, -1, NULL)) {
          g_warning ("Invalid UTF-8 in input!");
        } else {
          NotifyNotification *notif;
          gchar **message;
          error = NULL;

          /* message[1] (the summary) will be NULL in case there's
           * no \n in the string. In which case only the title is
           * defined */
          message = g_strsplit (g_strcompress (value), "\n", 2);

          if (*message == NULL) {
            g_printerr (_("Could not parse message from stdin\n"));
            continue;
          }

          notif = notify_notification_new (message[0] /* title */,
                                           message[1] /* summary */,
                                           icon_file);

          g_strfreev (message);

          zenity_notification_set_hints (notif, notification_hints);

          notify_notification_show (notif, &error);
          if (error) {
            g_warning ("Error showing notification: %s", error->message);
            g_error_free (error);
            error = NULL;
          }

	  g_object_unref (notif);
        }
      } else if (!g_ascii_strcasecmp (command, "tooltip")) {
        if (!g_utf8_validate (value, -1, NULL)) {
          g_warning ("Invalid UTF-8 in input!");
        } else {
          NotifyNotification *notif;

          notif = notify_notification_new (value,
                                           NULL,
                                           icon_file);

          zenity_notification_set_hints (notif, notification_hints);

          notify_notification_show (notif, &error);
          if (error) {
            g_warning ("Error showing notification: %s", error->message);
            g_error_free (error);
            error = NULL;
          }
        }
      } else if (!g_ascii_strcasecmp (command, "visible")) {

      } else {
	g_warning ("Unknown command '%s'", command);
      }
      g_free (command);

    } while (g_io_channel_get_buffer_condition (channel) == G_IO_IN);
    g_string_free (string, TRUE);
  }

  if ((condition & G_IO_HUP) != 0) {
    g_io_channel_shutdown (channel, TRUE, NULL);
    zen_data->exit_code = zenity_util_return_exit_code (ZENITY_OK);
    gtk_main_quit ();
    return FALSE;
  }

  return TRUE;
}

static void
zenity_notification_listen_on_stdin (ZenityData *data)
{
  GIOChannel *channel;

  channel = g_io_channel_unix_new (0);
  g_io_channel_set_encoding (channel, NULL, NULL);
  g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
  g_io_add_watch (channel, G_IO_IN | G_IO_HUP,
		  zenity_notification_handle_stdin, data);
}

void
zenity_notification (ZenityData *data, ZenityNotificationData *notification_data)
{
  GError *error;
  NotifyNotification *notification;
  GHashTable *notification_hints;

  /* create the notification widget */
  if (!notify_is_initted ()) {
    notify_init (_("Zenity notification"));
  }

  if (notification_data->listen) {
    zenity_notification_listen_on_stdin (data);
  } else {
    if (notification_data->notification_text == NULL) {
      exit (1);
    }

    notification = notify_notification_new (notification_data->notification_text, NULL, data->window_icon);
    if (notification == NULL) {
      exit (1);
    }

    /* if we aren't listening for changes, then close on default action */
    notify_notification_add_action (notification,
                                    "default",
                                    "Do Default Action",
                                    (NotifyActionCallback) on_notification_default_action,
                                    data,
                                    NULL);

    /* set the notification hints for the displayed notification */
    notification_hints = zenity_notification_parse_hints_array (notification_data->notification_hints);
    zenity_notification_set_hints(notification, notification_hints);
    g_hash_table_unref (notification_hints);

    /* Show icon and wait */
    error = NULL;
    if (!notify_notification_show (notification, &error)) {
      if (error != NULL) {
        g_warning ("Error showing notification: %s", error->message);
        g_error_free (error);
      }
      exit (1);
    }

  }

  if (data->timeout_delay > 0) {
    g_timeout_add_seconds (data->timeout_delay, (GSourceFunc) zenity_util_timeout_handle, NULL);
  }

  gtk_main ();
}

#endif