* msg2pdf.c: make a bit more robust

This commit is contained in:
djcb 2012-04-29 17:31:54 +03:00
parent 27cc5cea62
commit ee9dd9c06a
1 changed files with 40 additions and 44 deletions

View File

@ -28,17 +28,37 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
static GtkPrintOperation* static gboolean
get_print_operation (const char *path) print_to_pdf (WebKitWebFrame *frame, GError **err)
{ {
GtkPrintOperation *printop; GtkPrintOperation *op;
GtkPrintOperationResult res;
char *path;
gboolean rv;
printop = gtk_print_operation_new (); path = g_strdup_printf ("%s%c%x.pdf",mu_util_cache_dir(),
G_DIR_SEPARATOR, (unsigned)random());
if (!mu_util_create_dir_maybe (mu_util_cache_dir(),0700,FALSE)) {
g_warning ("Couldn't create tempdir");
return FALSE;
}
op = gtk_print_operation_new ();
gtk_print_operation_set_export_filename gtk_print_operation_set_export_filename
(GTK_PRINT_OPERATION(printop), path); (GTK_PRINT_OPERATION(op), path);
res = webkit_web_frame_print_full (frame, op,
GTK_PRINT_OPERATION_ACTION_EXPORT,
err);
g_object_unref (op);
rv = (res != GTK_PRINT_OPERATION_RESULT_ERROR);
if (rv)
g_print ("%s\n", path);
g_free (path);
return rv;
return printop;
} }
@ -103,18 +123,15 @@ on_resource_request_starting (WebKitWebView *self, WebKitWebFrame *frame,
/* return the path to the output file, or NULL in case of error */ /* return the path to the output file, or NULL in case of error */
static char* static gboolean
generate_pdf (MuMsg *msg, const char *str, GError **err) generate_pdf (MuMsg *msg, const char *str, GError **err)
{ {
GtkWidget *view; GtkWidget *view;
WebKitWebFrame *frame; WebKitWebFrame *frame;
WebKitWebSettings *settings; WebKitWebSettings *settings;
GtkPrintOperationResult printres;
GtkPrintOperation *printop;
char *path;
WebKitLoadStatus status; WebKitLoadStatus status;
time_t start; time_t started;
const int max_time = 2; /* max two seconds to download stuff */ const int max_time = 3; /* max two seconds to download stuff */
settings = webkit_web_settings_new (); settings = webkit_web_settings_new ();
g_object_set (G_OBJECT(settings), g_object_set (G_OBJECT(settings),
@ -141,33 +158,14 @@ generate_pdf (MuMsg *msg, const char *str, GError **err)
return FALSE; return FALSE;
} }
path = g_strdup_printf ("%s%c%x.pdf",mu_util_cache_dir(), started = time (NULL);
G_DIR_SEPARATOR, (unsigned)random());
if (!mu_util_create_dir_maybe (mu_util_cache_dir(),0700,FALSE)) {
g_warning ("Couldn't create tempdir");
return FALSE;
}
start = time (NULL);
do { do {
status = webkit_web_view_get_load_status (WEBKIT_WEB_VIEW(view)); status = webkit_web_view_get_load_status (WEBKIT_WEB_VIEW(view));
g_main_context_iteration (NULL, TRUE); gtk_main_iteration_do (FALSE);
} while (status != WEBKIT_LOAD_FINISHED || } while (status != WEBKIT_LOAD_FINISHED &&
time(NULL) - start > max_time); (time(NULL) - started) <= max_time);
printop = get_print_operation (path); return print_to_pdf (frame, err);
printres = webkit_web_frame_print_full
(frame, printop, GTK_PRINT_OPERATION_ACTION_EXPORT,
err);
g_object_unref (printop);
if (printres != GTK_PRINT_OPERATION_RESULT_ERROR)
return path;
else {
g_free (path);
return NULL;
}
} }
@ -185,12 +183,13 @@ add_header (GString *gstr, const char* header, const char *val)
} }
/* return the path to the output file, or NULL in case of error */ /* return the path to the output file, or NULL in case of error */
static char* static gboolean
convert_to_pdf (MuMsg *msg, GError **err) convert_to_pdf (MuMsg *msg, GError **err)
{ {
GString *gstr; GString *gstr;
const char *body; const char *body;
gchar *data, *path; gchar *data;
gboolean rv;
gstr = g_string_sized_new (4096); gstr = g_string_sized_new (4096);
@ -220,10 +219,10 @@ convert_to_pdf (MuMsg *msg, GError **err)
} }
data = g_string_free (gstr, FALSE); data = g_string_free (gstr, FALSE);
path = generate_pdf (msg, data, err); rv = generate_pdf (msg, data, err);
g_free (data); g_free (data);
return path; return rv;
} }
@ -232,7 +231,6 @@ main(int argc, char *argv[])
{ {
MuMsg *msg; MuMsg *msg;
GError *err; GError *err;
char *path;
if (argc != 2) { if (argc != 2) {
g_print ("msg2pdf: generate pdf files from e-mail messages\n" g_print ("msg2pdf: generate pdf files from e-mail messages\n"
@ -258,8 +256,7 @@ main(int argc, char *argv[])
return 1; return 1;
} }
path = convert_to_pdf (msg, &err); if (!convert_to_pdf (msg, &err)) {
if (!path) {
g_printerr ("failed to create pdf from %s: %s\n", g_printerr ("failed to create pdf from %s: %s\n",
argv[1], argv[1],
err && err->message ? err->message : err && err->message ? err->message :
@ -270,6 +267,5 @@ main(int argc, char *argv[])
} }
mu_msg_unref (msg); mu_msg_unref (msg);
g_print ("%s\n", path);
return 0; return 0;
} }