You need to sign in or sign up before continuing.
notification.c 11.7 KB
Newer Older
Victor Ananjesky's avatar
Victor Ananjesky committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * This file is part of YAD.
 *
 * YAD 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 3 of the License, or
 * (at your option) any later version.
 *
 * YAD 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 YAD. If not, see <http://www.gnu.org/licenses/>.
 *
17
 * Copyright (C) 2008-2023, Victor Ananjevsky <victor@sanana.kiev.ua>
Victor Ananjesky's avatar
Victor Ananjesky committed
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
 */

#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#include "yad.h"

typedef struct {
  gchar *name;
  gchar *action;
  gchar *icon;
} MenuData;

static GtkStatusIcon *status_icon;

static gchar *icon = NULL;
static gchar *action = NULL;

static GSList *menu_data = NULL;

static gint exit_code;

43 44
static void popup_menu_cb (GtkStatusIcon *, guint, guint, gpointer);

Victor Ananjesky's avatar
Victor Ananjesky committed
45
static void
46
free_menu_data (gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
47 48 49
{
  MenuData *m = (MenuData *) data;

50 51 52 53 54 55
  if (m) {
    g_free (m->name);
    g_free (m->action);
    g_free (m->icon);
    g_free (m);
  }
Victor Ananjesky's avatar
Victor Ananjesky committed
56 57 58 59 60 61 62 63 64 65
}

static void
parse_menu_str (gchar * str)
{
  gchar **menu_vals;
  gint i = 0;

  if (menu_data)
    {
66
      g_slist_free_full (menu_data, free_menu_data);
Victor Ananjesky's avatar
Victor Ananjesky committed
67 68 69 70 71 72 73 74 75 76 77 78
      menu_data = NULL;
    }

  menu_vals = g_strsplit (str, options.common_data.separator, -1);

  while (menu_vals[i] != NULL)
    {
      MenuData *mdata = g_new0 (MenuData, 1);
      gchar **s = g_strsplit (menu_vals[i], options.common_data.item_separator, 3);

      if (s[0])
        {
79 80 81 82 83 84 85 86
          YadStock sit;
          if (stock_lookup (s[0], &sit))
            {
              mdata->name = g_strdup (sit.label);
              mdata->icon = g_strdup (sit.icon);
            }
          else
              mdata->name = g_strdup (s[0]);
Victor Ananjesky's avatar
Victor Ananjesky committed
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
          if (s[1])
            {
              mdata->action = g_strdup (s[1]);
              if (s[2])
                mdata->icon = g_strdup (s[2]);
            }
        }
      menu_data = g_slist_append (menu_data, mdata);
      g_strfreev (s);
      i++;
    }

  g_strfreev (menu_vals);
}

static void
timeout_cb (gpointer data)
{
  exit_code = YAD_RESPONSE_TIMEOUT;
  gtk_main_quit ();
}

static void
set_icon (void)
{
  GdkPixbuf *pixbuf;
  GError *err = NULL;

  if (icon == NULL)
    {
117
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
118
      gtk_status_icon_set_from_icon_name (status_icon, "yad");
119
      G_GNUC_END_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
120 121 122 123 124
      return;
    }

  if (g_file_test (icon, G_FILE_TEST_EXISTS))
    {
125 126 127
      gint isize = (options.common_data.icon_size > 0) ? options.common_data.icon_size : 16;

      pixbuf = gdk_pixbuf_new_from_file_at_scale (icon, isize, isize, TRUE, &err);
Victor Ananjesky's avatar
Victor Ananjesky committed
128 129 130 131 132 133 134
      if (err)
        {
          g_printerr (_("Could not load notification icon '%s': %s\n"), icon, err->message);
          g_clear_error (&err);
        }
      if (pixbuf)
        {
135
          G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
136
          gtk_status_icon_set_from_pixbuf (status_icon, pixbuf);
137
          G_GNUC_END_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
138 139 140
          g_object_unref (pixbuf);
        }
      else
141 142 143 144 145
        {
          G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
          gtk_status_icon_set_from_icon_name (status_icon, "yad");
          G_GNUC_END_IGNORE_DEPRECATIONS;
        }
Victor Ananjesky's avatar
Victor Ananjesky committed
146 147
    }
  else
148 149 150 151 152
    {
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_status_icon_set_from_icon_name (status_icon, icon);
      G_GNUC_END_IGNORE_DEPRECATIONS;
    }
Victor Ananjesky's avatar
Victor Ananjesky committed
153 154 155 156 157 158 159 160 161 162 163
}

static gboolean
activate_cb (GtkWidget * widget, YadData * data)
{
  if ((action == NULL && !options.common_data.listen) || (action && g_ascii_strcasecmp (action, "quit") == 0))
    {
      exit_code = YAD_RESPONSE_OK;
      gtk_main_quit ();
    }
  else if (action)
164 165 166 167
    {
      if (g_ascii_strcasecmp (action, "menu") == 0)
        popup_menu_cb (GTK_STATUS_ICON (widget), 1, GDK_CURRENT_TIME, data);
      else
168
        run_command_async (action);
169
    }
Victor Ananjesky's avatar
Victor Ananjesky committed
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

  return TRUE;
}

static gboolean
middle_quit_cb (GtkStatusIcon * icon, GdkEventButton * ev, gpointer data)
{
  if (ev->button == 2)
    {
      if (options.data.escape_ok)
        exit_code = YAD_RESPONSE_OK;
      else
        exit_code = YAD_RESPONSE_ESC;
      gtk_main_quit ();
    }

  return FALSE;
}

static void
popup_menu_item_activate_cb (GtkWidget * w, gpointer data)
{
  gchar *cmd = (gchar *) data;

  if (cmd)
    {
196
      if (g_ascii_strcasecmp (g_strstrip (cmd), "quit") == 0)
Victor Ananjesky's avatar
Victor Ananjesky committed
197 198 199 200 201
        {
          exit_code = YAD_RESPONSE_OK;
          gtk_main_quit ();
        }
      else
202
        run_command_async (cmd);
Victor Ananjesky's avatar
Victor Ananjesky committed
203 204 205 206
    }
}

static void
207
popup_menu_cb (GtkStatusIcon *icon, guint button, guint activate_time, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
208 209 210 211 212 213 214 215 216
{
  GtkWidget *menu;
  GtkWidget *item;
  GSList *m;

  if (!menu_data)
    return;

  menu = gtk_menu_new ();
217 218
  gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);

Victor Ananjesky's avatar
Victor Ananjesky committed
219 220 221 222 223 224
  for (m = menu_data; m; m = m->next)
    {
      MenuData *d = (MenuData *) m->data;

      if (d->name)
        {
225 226 227 228 229
          GtkWidget *b, *i, *l;

          item = gtk_menu_item_new ();
          b = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);

Victor Ananjesky's avatar
Victor Ananjesky committed
230 231
          if (d->icon)
            {
232
              GdkPixbuf *pb = get_pixbuf (d->icon, YAD_SMALL_ICON, TRUE);
Victor Ananjesky's avatar
Victor Ananjesky committed
233 234
              if (pb)
                {
235 236
                  i = gtk_image_new_from_pixbuf (pb);
                  gtk_container_add (GTK_CONTAINER (b), i);
Victor Ananjesky's avatar
Victor Ananjesky committed
237 238 239
                  g_object_unref (pb);
                }
            }
240 241 242 243 244 245 246
          l = gtk_label_new_with_mnemonic (d->name);
          gtk_label_set_xalign (GTK_LABEL (l), 0.0);
          gtk_label_set_mnemonic_widget (GTK_LABEL (l), item);
          gtk_box_pack_end (GTK_BOX (b), l, TRUE, TRUE, 0);

          gtk_container_add (GTK_CONTAINER (item), b);

Victor Ananjesky's avatar
Victor Ananjesky committed
247 248 249 250 251 252
          g_signal_connect (GTK_MENU_ITEM (item), "activate",
                            G_CALLBACK (popup_menu_item_activate_cb), (gpointer) d->action);
        }
      else
        item = gtk_separator_menu_item_new ();

253
      gtk_widget_show_all (item);
Victor Ananjesky's avatar
Victor Ananjesky committed
254 255
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
    }
256

257
  gtk_menu_popup_at_pointer (GTK_MENU (menu), NULL);
Victor Ananjesky's avatar
Victor Ananjesky committed
258 259 260 261 262 263 264 265 266 267 268
}

static gboolean
handle_stdin (GIOChannel * channel, GIOCondition condition, gpointer data)
{
  if ((condition & G_IO_IN) != 0)
    {
      GString *string;
      GError *err = NULL;

      string = g_string_new (NULL);
269
      while (channel->is_readable == FALSE)
270
        usleep (100);
Victor Ananjesky's avatar
Victor Ananjesky committed
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

      do
        {
          gint status;
          gchar *command = NULL, *value = NULL, **args;

          do
            {
              status = g_io_channel_read_line_string (channel, string, NULL, &err);

              while (gdk_events_pending ())
                gtk_main_iteration ();
            }
          while (status == G_IO_STATUS_AGAIN);

          if (status != G_IO_STATUS_NORMAL)
            {
              if (err)
                {
                  g_printerr ("yad_notification_handle_stdin(): %s\n", err->message);
                  g_error_free (err);
                  err = NULL;
                }
              /* stop handling but not exit */
              g_io_channel_shutdown (channel, TRUE, NULL);
              return FALSE;
            }

          strip_new_line (string->str);
          if (!string->str[0])
            continue;

          args = g_strsplit (string->str, ":", 2);
          command = g_strdup (args[0]);
          if (args[1])
            value = g_strdup (args[1]);
          g_strfreev (args);
          if (value)
            g_strstrip (value);

          if (!g_ascii_strcasecmp (command, "icon") && value)
            {
              g_free (icon);
              icon = g_strdup (value);

316
              G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
317 318
              if (gtk_status_icon_get_visible (status_icon) && gtk_status_icon_is_embedded (status_icon))
                set_icon ();
319
              G_GNUC_END_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
320 321 322 323 324 325
            }
          else if (!g_ascii_strcasecmp (command, "tooltip"))
            {
              if (g_utf8_validate (value, -1, NULL))
                {
                  gchar *message = g_strcompress (value);
326
                  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
327 328 329 330
                  if (!options.data.no_markup)
                    gtk_status_icon_set_tooltip_markup (status_icon, message);
                  else
                    gtk_status_icon_set_tooltip_text (status_icon, message);
331
                  G_GNUC_END_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
332 333 334 335 336 337 338
                  g_free (message);
                }
              else
                g_printerr (_("Invalid UTF-8 in tooltip!\n"));
            }
          else if (!g_ascii_strcasecmp (command, "visible"))
            {
339
              G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
340
              if (!g_ascii_strcasecmp (value, "false"))
Victor Ananjevsky's avatar
Victor Ananjevsky committed
341
                gtk_status_icon_set_visible (status_icon, FALSE);
Victor Ananjesky's avatar
Victor Ananjesky committed
342
              else
Victor Ananjevsky's avatar
Victor Ananjevsky committed
343
                gtk_status_icon_set_visible (status_icon, TRUE);
344
              G_GNUC_END_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
345 346 347 348
            }
          else if (!g_ascii_strcasecmp (command, "action"))
            {
              g_free (action);
349 350
              action = NULL;
              if (value && *value)
Victor Ananjesky's avatar
Victor Ananjesky committed
351 352 353 354 355 356 357 358 359
                action = g_strdup (value);
            }
          else if (!g_ascii_strcasecmp (command, "quit"))
            {
              exit_code = YAD_RESPONSE_OK;
              gtk_main_quit ();
            }
          else if (!g_ascii_strcasecmp (command, "menu"))
            {
360
              if (value && *value)
Victor Ananjesky's avatar
Victor Ananjesky committed
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
                parse_menu_str (value);
            }
          else
            g_printerr (_("Unknown command '%s'\n"), command);

          g_free (command);
          g_free (value);
        }
      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);
      gtk_main_quit ();
      return FALSE;
    }

  return TRUE;
}

gint
yad_notification_run ()
{
  GIOChannel *channel = NULL;

388
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
389
  status_icon = gtk_status_icon_new ();
390
  G_GNUC_END_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
391

392
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
393 394 395 396 397 398 399 400 401
  if (options.data.dialog_text)
    {
      if (!options.data.no_markup)
        gtk_status_icon_set_tooltip_markup (status_icon, options.data.dialog_text);
      else
        gtk_status_icon_set_tooltip_text (status_icon, options.data.dialog_text);
    }
  else
    gtk_status_icon_set_tooltip_text (status_icon, _("Yad notification"));
402
  G_GNUC_END_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
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

  if (options.data.dialog_image)
    icon = g_strdup (options.data.dialog_image);
  if (options.common_data.command)
    action = g_strdup (options.common_data.command);

  set_icon ();

  g_signal_connect (status_icon, "activate", G_CALLBACK (activate_cb), NULL);
  g_signal_connect (status_icon, "popup_menu", G_CALLBACK (popup_menu_cb), NULL);

  if (options.notification_data.menu)
    parse_menu_str (options.notification_data.menu);

  /* quit on middle click (like press Esc) */
  if (options.notification_data.middle)
    g_signal_connect (status_icon, "button-press-event", G_CALLBACK (middle_quit_cb), NULL);

  if (options.common_data.listen)
    {
      channel = g_io_channel_unix_new (0);
      if (channel)
        {
          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, handle_stdin, NULL);
        }
    }

  /* Show icon and wait */
433
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
434
  gtk_status_icon_set_visible (status_icon, !options.notification_data.hidden);
435
  G_GNUC_END_IGNORE_DEPRECATIONS;
Victor Ananjesky's avatar
Victor Ananjesky committed
436 437 438 439 440 441 442 443

  if (options.data.timeout > 0)
    g_timeout_add_seconds (options.data.timeout, (GSourceFunc) timeout_cb, NULL);

  gtk_main ();

  return exit_code;
}