Ticket #147: notify-send-support_body_from_file.patch

  • tools/notify-send.c

    old new  
    124124    return TRUE; 
    125125} 
    126126 
     127static gchar * 
     128get_file_contents(const gchar *filename) 
     129{ 
     130    GError *error = NULL; 
     131    gchar *contents = NULL; 
     132    gsize length = 0; 
     133 
     134    if (!strcmp(filename, "-")) 
     135    { 
     136        GIOChannel *channel = NULL; 
     137        GIOStatus status = G_IO_STATUS_NORMAL; 
     138 
     139#if G_OS_WIN32 
     140        channel = g_io_channel_win32_new_fd(0); 
     141#else 
     142        channel = g_io_channel_unix_new(0); 
     143#endif 
     144        if (!channel) 
     145        { 
     146            fprintf(stderr, "%s\n", error->message); 
     147            g_error_free(error); 
     148            exit(1); 
     149        } 
     150 
     151        do { 
     152            status = g_io_channel_read_to_end(channel, 
     153                    &contents, &length, &error); 
     154        } while (status == G_IO_STATUS_AGAIN); 
     155        if (status != G_IO_STATUS_NORMAL) 
     156        { 
     157            fprintf(stderr, "%s\n", error->message); 
     158            g_error_free(error); 
     159            exit(1); 
     160        } 
     161 
     162        g_io_channel_unref(channel); 
     163    } 
     164    else if (!g_file_get_contents(filename, &contents, &length, &error)) 
     165    { 
     166        fprintf(stderr, "%s\n", error->message); 
     167        g_error_free(error); 
     168        exit(1); 
     169    } 
     170 
     171    /* Remove the end-of-file newline */ 
     172    /* Not using g_chomp() to allow users to end with newlines if they like */ 
     173    if (contents[length-2] == '\r' && contents[length-1] == '\n') 
     174        contents[length-2] = '\0'; 
     175    else if (contents[length-1] == '\n') 
     176        contents[length-1] = '\0'; 
     177    else if (contents[length-1] == '\r') 
     178        contents[length-1] = '\0'; 
     179 
     180    return contents; 
     181} 
     182 
    127183int 
    128184main(int argc, char *argv[]) 
    129185{ 
    130186    static const gchar *summary = NULL; 
    131     static const gchar *body = ""; 
    132187    static const gchar *type = NULL; 
     188    static gchar *body = ""; 
     189    static gchar *body_file = NULL; 
     190    static gboolean body_allocated = FALSE; 
    133191    static gchar *icon_str = NULL; 
    134192    static gchar *icons = NULL; 
    135193    static gchar **n_text = NULL; 
     
    147205        { "urgency", 'u', 0, G_OPTION_ARG_CALLBACK, g_option_arg_urgency_cb, 
    148206          N_("Specifies the urgency level (low, normal, critical)."), 
    149207          N_("LEVEL") }, 
     208        { "file", 'f', 0, G_OPTION_ARG_FILENAME, &body_file, 
     209          N_("Specifies a file whose contents will form the body of the " 
     210             "notification."), N_("FILENAME") }, 
    150211        { "expire-time", 't', 0,G_OPTION_ARG_INT, &expire_timeout, 
    151212          N_("Specifies the timeout in milliseconds at which to expire the " 
    152213             "notification."), N_("TIME") }, 
     
    199260        exit(1); 
    200261    } 
    201262 
    202     if (n_text[1] != NULL) 
     263    if (n_text[1] != NULL && body_file != NULL) 
     264    { 
     265        fprintf(stderr, "%s\n", N_("Only one notification body may be given.")); 
     266        exit(1); 
     267    } 
     268    else if (n_text[1] != NULL) 
    203269    { 
    204270        body = n_text[1]; 
    205271 
     
    209275            exit(1); 
    210276        } 
    211277    } 
     278    else if (body_file != NULL) 
     279    { 
     280        body = get_file_contents(body_file); 
     281        body_allocated = TRUE; 
     282    } 
    212283 
    213284    if (icons != NULL) 
    214285    { 
     
    273344 
    274345    g_object_unref(G_OBJECT(notify)); 
    275346 
     347    if (body_allocated) 
     348        g_free(body); 
     349 
    276350    notify_uninit(); 
    277351 
    278352    exit(hint_error);