Add two new options to e-notify-send:
authorptomaine <ptomaine@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Tue, 6 Jan 2009 04:09:34 +0000 (04:09 +0000)
committerptomaine <ptomaine@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Tue, 6 Jan 2009 04:09:34 +0000 (04:09 +0000)
  -p    Print notification ID to STDOUT.
  -r    Replace the notification with the specified id.

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/e_dbus@38474 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/bin/notify-send.c

index fea04cb..058675b 100644 (file)
@@ -27,6 +27,8 @@ usage(void)
          "  -n, --name=NAME                   Specifies the application name to use (default is e-notify-send).\n"
          "  -u, --urgency=LEVEL               Specifies the urgency level (low, normal, critical).\n"
          "  -t, --expire-time=TIME            Specifies the timeout in milliseconds at which to expire the notification.\n"
+         "  -r, --replace=ID                  Specifies the ID of notification to replace.\n"
+         "  -p, --print-id                    Prints the ID of notification to STDOUT.\n"
          "  -i, --icon=ICON                   Specifies an icon filename or stock icon to display.\n"
          "  -c, --category=TYPE               Specifies the notification category.\n"
          "  -v, --version                     Version of the package.\n"
@@ -34,11 +36,45 @@ usage(void)
 }
 
 int
+read_int_arg(long long *result, const char *name, intmax_t min, intmax_t max)
+{
+  char *endptr;
+
+  errno = 0;  
+  *result = strtoll(optarg, &endptr, 10);
+  if ((errno != 0 && *result == 0) || endptr == optarg) 
+    {
+       fprintf(stderr, "Cannot parse integer value '%s' for %s\n", optarg, name);
+       return 0;
+    }
+  else if (*result > max || *result < min)
+    {
+       fprintf(stderr, "Integer value '%s' for %s out of range\n", optarg, name);
+       return 0;
+    }
+
+  return 1;
+}
+
+void 
+send_cb(void *user_data, void *method_return, DBusError *error)
+{
+   E_Notification_Return_Notify *r = method_return;
+
+   if(!r)
+     return;
+
+   printf("%u\n", r->notification_id );
+
+   ecore_main_loop_quit();
+}
+
+int
 main(int argc, char **argv)
 {
   int ch;
-  char *endptr;
-  int timeout;
+  long long value;
+  int print_id = 0;
   E_Notification *n;
 
   e_notification_init();
@@ -52,13 +88,15 @@ main(int argc, char **argv)
       { "name",        required_argument,      NULL,           'n' },
       { "urgency",     required_argument,      NULL,           'u' },
       { "expire-time", required_argument,      NULL,           't' },
+      { "replace",     required_argument,      NULL,           'r' },
+      { "print-id",    no_argument,            NULL,           'p' },
       { "icon",        required_argument,      NULL,           'i' },
       { "category",    required_argument,      NULL,           'c' },
       { "version",     no_argument,            NULL,           'v' },
       { NULL,          0,                      NULL,             0 }
   };
 
-  while ((ch = getopt_long(argc, argv, "?vn:u:t:i:c:", longopts, NULL)) != -1)
+  while ((ch = getopt_long(argc, argv, "p?vn:u:t:r:i:c:", longopts, NULL)) != -1)
     switch (ch) {
     case '?':
       usage();
@@ -82,20 +120,16 @@ main(int argc, char **argv)
         printf("Urgency level must be: low, normal or critical\n");
       break;
     case 't':
-      errno = 0;
-      timeout = strtol(optarg, &endptr, 10);
-      if ((errno != 0 && timeout == 0) || endptr == optarg) 
-        {
-          fprintf(stderr, "Cannot parse integer value '%s' for -t\n", optarg);
-          return EXIT_FAILURE;
-        }
-      else if (timeout > INT_MAX || timeout < INT_MIN)
-        {
-          fprintf(stderr, "Integer value '%s' for -t out of range\n", optarg);
-          return EXIT_FAILURE;
-        }
+      if (!read_int_arg(&value, "-t", INT_MIN, INT_MAX))
+        return EXIT_FAILURE;
       else 
-        e_notification_timeout_set(n, timeout);
+        e_notification_timeout_set(n, (int)value);
+      break;
+    case 'r':
+      if (!read_int_arg(&value, "-r", 0, UINT_MAX))
+        return EXIT_FAILURE;
+      else
+           e_notification_replaces_id_set(n, (unsigned int)value);
       break;
     case 'i':
       e_notification_app_icon_set(n, optarg);
@@ -103,6 +137,9 @@ main(int argc, char **argv)
     case 'c':
       e_notification_hint_category_set(n, optarg);
       break;
+    case 'p':
+      print_id = 1;
+      break;
     default:
       usage();
       return EXIT_FAILURE;
@@ -119,7 +156,15 @@ main(int argc, char **argv)
   e_notification_summary_set(n, argv[0]);
   if (argc > 1) e_notification_body_set(n, argv[1]);
 
-  e_notification_send(n, NULL, NULL);
+
+  if (print_id)
+    {
+       e_notification_send(n, send_cb, NULL);
+       ecore_main_loop_begin();
+    }
+  else
+    e_notification_send(n, NULL, NULL);
+
   e_notification_unref(n);
   e_notification_shutdown();