Ticket #147: notify-send-support_body_from_file.patch
-
tools/notify-send.c
old new 124 124 return TRUE; 125 125 } 126 126 127 static gchar * 128 get_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 127 183 int 128 184 main(int argc, char *argv[]) 129 185 { 130 186 static const gchar *summary = NULL; 131 static const gchar *body = "";132 187 static const gchar *type = NULL; 188 static gchar *body = ""; 189 static gchar *body_file = NULL; 190 static gboolean body_allocated = FALSE; 133 191 static gchar *icon_str = NULL; 134 192 static gchar *icons = NULL; 135 193 static gchar **n_text = NULL; … … 147 205 { "urgency", 'u', 0, G_OPTION_ARG_CALLBACK, g_option_arg_urgency_cb, 148 206 N_("Specifies the urgency level (low, normal, critical)."), 149 207 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") }, 150 211 { "expire-time", 't', 0,G_OPTION_ARG_INT, &expire_timeout, 151 212 N_("Specifies the timeout in milliseconds at which to expire the " 152 213 "notification."), N_("TIME") }, … … 199 260 exit(1); 200 261 } 201 262 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) 203 269 { 204 270 body = n_text[1]; 205 271 … … 209 275 exit(1); 210 276 } 211 277 } 278 else if (body_file != NULL) 279 { 280 body = get_file_contents(body_file); 281 body_allocated = TRUE; 282 } 212 283 213 284 if (icons != NULL) 214 285 { … … 273 344 274 345 g_object_unref(G_OBJECT(notify)); 275 346 347 if (body_allocated) 348 g_free(body); 349 276 350 notify_uninit(); 277 351 278 352 exit(hint_error);
