--- tools/notify-send.c.old 2007-10-15 20:19:20.000000000 -0400 +++ tools/notify-send.c 2007-10-15 21:44:19.000000000 -0400 @@ -124,12 +124,70 @@ return TRUE; } +static gchar * +get_file_contents(const gchar *filename) +{ + GError *error = NULL; + gchar *contents = NULL; + gsize length = 0; + + if (!strcmp(filename, "-")) + { + GIOChannel *channel = NULL; + GIOStatus status = G_IO_STATUS_NORMAL; + +#if G_OS_WIN32 + channel = g_io_channel_win32_new_fd(0); +#else + channel = g_io_channel_unix_new(0); +#endif + if (!channel) + { + fprintf(stderr, "%s\n", error->message); + g_error_free(error); + exit(1); + } + + do { + status = g_io_channel_read_to_end(channel, + &contents, &length, &error); + } while (status == G_IO_STATUS_AGAIN); + if (status != G_IO_STATUS_NORMAL) + { + fprintf(stderr, "%s\n", error->message); + g_error_free(error); + exit(1); + } + + g_io_channel_unref(channel); + } + else if (!g_file_get_contents(filename, &contents, &length, &error)) + { + fprintf(stderr, "%s\n", error->message); + g_error_free(error); + exit(1); + } + + /* Remove the end-of-file newline */ + /* Not using g_chomp() to allow users to end with newlines if they like */ + if (contents[length-2] == '\r' && contents[length-1] == '\n') + contents[length-2] = '\0'; + else if (contents[length-1] == '\n') + contents[length-1] = '\0'; + else if (contents[length-1] == '\r') + contents[length-1] = '\0'; + + return contents; +} + int main(int argc, char *argv[]) { static const gchar *summary = NULL; - static const gchar *body = ""; static const gchar *type = NULL; + static gchar *body = ""; + static gchar *body_file = NULL; + static gboolean body_allocated = FALSE; static gchar *icon_str = NULL; static gchar *icons = NULL; static gchar **n_text = NULL; @@ -147,6 +205,9 @@ { "urgency", 'u', 0, G_OPTION_ARG_CALLBACK, g_option_arg_urgency_cb, N_("Specifies the urgency level (low, normal, critical)."), N_("LEVEL") }, + { "file", 'f', 0, G_OPTION_ARG_FILENAME, &body_file, + N_("Specifies a file whose contents will form the body of the " + "notification."), N_("FILENAME") }, { "expire-time", 't', 0,G_OPTION_ARG_INT, &expire_timeout, N_("Specifies the timeout in milliseconds at which to expire the " "notification."), N_("TIME") }, @@ -199,7 +260,12 @@ exit(1); } - if (n_text[1] != NULL) + if (n_text[1] != NULL && body_file != NULL) + { + fprintf(stderr, "%s\n", N_("Only one notification body may be given.")); + exit(1); + } + else if (n_text[1] != NULL) { body = n_text[1]; @@ -209,6 +275,11 @@ exit(1); } } + else if (body_file != NULL) + { + body = get_file_contents(body_file); + body_allocated = TRUE; + } if (icons != NULL) { @@ -273,6 +344,9 @@ g_object_unref(G_OBJECT(notify)); + if (body_allocated) + g_free(body); + notify_uninit(); exit(hint_error);