toys/msg2pdf: incomplete port to new webkitgtk3

Compiles, doesn't work yet.
This commit is contained in:
Dirk-Jan C. Binnema 2020-03-11 10:28:43 +02:00
parent 89ab9f7351
commit 08d2e78d61
5 changed files with 110 additions and 67 deletions

View File

@ -275,6 +275,7 @@ guile/tests/Makefile
guile/scripts/Makefile guile/scripts/Makefile
toys/Makefile toys/Makefile
toys/mug/Makefile toys/mug/Makefile
#toys/msg2pdf/Makefile
man/Makefile man/Makefile
m4/Makefile m4/Makefile
contrib/Makefile contrib/Makefile

View File

@ -1,4 +1,4 @@
## Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ## Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
## ##
## This program is free software; you can redistribute it and/or modify ## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by ## it under the terms of the GNU General Public License as published by
@ -20,7 +20,5 @@ SUBDIRS=
# for mug2 and msg2pdf, we need webkit # for mug2 and msg2pdf, we need webkit
if BUILD_GUI if BUILD_GUI
SUBDIRS += mug SUBDIRS += mug # msg2pdf
endif endif
# msg2pdf --> FIXME

View File

@ -19,14 +19,12 @@ include $(top_srcdir)/gtest.mk
# enforce compiling this dir first before decending into tests/ # enforce compiling this dir first before decending into tests/
SUBDIRS= . SUBDIRS= .
AM_CPPFLAGS=-I${top_srcdir}/lib $(GTK_CFLAGS) $(WEBKIT_CFLAGS)
AM_CPPFLAGS=-I${top_srcdir}/lib $(GTK_CFLAGS) $(WEBKIT_CFLAGS) -DICONDIR='"$(icondir)"'
# don't use -Werror, as it might break on other compilers # don't use -Werror, as it might break on other compilers
# use -Wno-unused-parameters, because some callbacks may not # use -Wno-unused-parameters, because some callbacks may not
# really need all the params they get # really need all the params they get
AM_CFLAGS=-Wall -Wextra -Wno-unused-parameter -Wdeclaration-after-statement AM_CFLAGS=$(ASAN_CFLAGS) -Wall -Wextra -Wno-unused-parameter -Wdeclaration-after-statement
AM_CXXFLAGS=-Wall -Wextra -Wno-unused-parameter
noinst_PROGRAMS= \ noinst_PROGRAMS= \
msg2pdf msg2pdf
@ -49,6 +47,7 @@ DISTCLEANFILES= \
msg2pdf_LDADD= \ msg2pdf_LDADD= \
$(ASAN_LDFLAGS) \
${top_builddir}/lib/libmu.la \ ${top_builddir}/lib/libmu.la \
$(GTK_LIBS) \ $(GTK_LIBS) \
${WEBKIT_LIBS} ${WEBKIT_LIBS}

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2012-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2012-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the ** under the terms of the GNU General Public License as published by the
@ -18,47 +18,97 @@
*/ */
#include <mu-msg.h> #include <mu-msg.h>
#include <mu-date.h> #include <utils/mu-date.h>
#include <mu-msg-part.h> #include <mu-msg-part.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <webkit/webkitwebview.h> #include <webkit2/webkit2.h>
#include <webkit/webkitwebresource.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
static gboolean typedef enum { UNKNOWN, OK, FAILED } Result;
print_to_pdf (WebKitWebFrame *frame, GError **err)
{
GtkPrintOperation *op;
GtkPrintOperationResult res;
char *path;
gboolean rv;
path = g_strdup_printf ("%s%c%x.pdf",mu_util_cache_dir(), static void
on_failed (WebKitPrintOperation *print_operation,
GError *error,
Result *result)
{
if (error)
g_warning ("%s", error->message);
*result = FAILED;
}
static void
on_finished (WebKitPrintOperation *print_operation,
Result *result)
{
if (*result == UNKNOWN)
*result = OK;
}
static gboolean
print_to_pdf (GtkWidget *webview, GError **err)
{
GtkWidget *win;
WebKitPrintOperation *op;
GtkPrintSettings *settings;
char *path, *uri;
Result res;
time_t started;
const int max_time = 3; /* max 3 seconds to download stuff */
path = g_strdup_printf ("%s%c%x.pdf", mu_util_cache_dir(),
G_DIR_SEPARATOR, (unsigned)random()); G_DIR_SEPARATOR, (unsigned)random());
if (!mu_util_create_dir_maybe (mu_util_cache_dir(),0700,FALSE)) { if (!mu_util_create_dir_maybe (mu_util_cache_dir(), 0700, FALSE)) {
g_warning ("Couldn't create tempdir"); g_warning ("Couldn't create tempdir");
g_free (path);
return FALSE;
}
uri = g_filename_to_uri (path, NULL, NULL);
g_print ("%s\n", path);
g_free(path);
if (!uri) {
g_warning ("failed to create uri");
return FALSE; return FALSE;
} }
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(win), webview);
gtk_widget_show_all(win);
op = gtk_print_operation_new ();
gtk_print_operation_set_export_filename
(GTK_PRINT_OPERATION(op), path);
res = webkit_web_frame_print_full (frame, op, op = webkit_print_operation_new (WEBKIT_WEB_VIEW(webview));
GTK_PRINT_OPERATION_ACTION_EXPORT, settings = gtk_print_settings_new();
err); gtk_print_settings_set(settings,
GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
gtk_print_settings_set(settings,
GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, "PDF");
g_free(uri);
webkit_print_operation_set_print_settings(op, settings);
webkit_print_operation_run_dialog(op, NULL);
res = UNKNOWN;
g_signal_connect(op, "failed", G_CALLBACK(on_failed), &res);
g_signal_connect(op, "finished", G_CALLBACK(on_finished), &res);
webkit_print_operation_print(op);
started = time (NULL);
do {
gtk_main_iteration_do (FALSE);
} while (res == UNKNOWN /*&& (time(NULL) - started) <= max_time*/);
g_object_unref (op); g_object_unref (op);
rv = (res != GTK_PRINT_OPERATION_RESULT_ERROR);
if (rv)
g_print ("%s\n", path);
g_free (path);
return rv;
return res == OK;
} }
@ -100,13 +150,14 @@ errexit:
} }
static void static void
on_resource_request_starting (WebKitWebView *self, WebKitWebFrame *frame, on_resource_load_started (WebKitWebView *self,
WebKitWebResource *resource, WebKitWebResource *resource,
WebKitNetworkRequest *request, WebKitURIRequest *request,
WebKitNetworkResponse *response, MuMsg *msg) MuMsg *msg)
{ {
const char* uri; const char* uri;
uri = webkit_network_request_get_uri (request);
uri = webkit_uri_request_get_uri (request);
if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) { if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) {
gchar *filepath; gchar *filepath;
@ -114,7 +165,8 @@ on_resource_request_starting (WebKitWebView *self, WebKitWebFrame *frame,
if (filepath) { if (filepath) {
gchar *fileuri; gchar *fileuri;
fileuri = g_strdup_printf ("file://%s", filepath); fileuri = g_strdup_printf ("file://%s", filepath);
webkit_network_request_set_uri (request, fileuri); webkit_uri_request_set_uri (request, fileuri);
g_debug("printing %s", fileuri);
g_free (fileuri); g_free (fileuri);
g_free (filepath); g_free (filepath);
} }
@ -127,46 +179,37 @@ 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; WebKitSettings *settings;
WebKitWebSettings *settings;
WebKitLoadStatus status;
time_t started; time_t started;
gboolean loading;
const int max_time = 3; /* max 3 seconds to download stuff */ const int max_time = 3; /* max 3 seconds to download stuff */
settings = webkit_web_settings_new (); settings = webkit_settings_new ();
g_object_set (G_OBJECT(settings), g_object_set (G_OBJECT(settings),
"enable-scripts", FALSE, "enable-javascript", FALSE,
"auto-load-images", TRUE, "auto-load-images", TRUE,
"enable-plugins", FALSE, NULL); "enable-plugins", FALSE,
NULL);
view = webkit_web_view_new (); view = webkit_web_view_new ();
/* to support cid: */ /* to support cid: */
g_signal_connect (G_OBJECT(view), "resource-request-starting", g_signal_connect (G_OBJECT(view), "resource-load-started",
G_CALLBACK (on_resource_request_starting), msg); G_CALLBACK (on_resource_load_started), msg);
webkit_web_view_set_settings (WEBKIT_WEB_VIEW(view), settings); webkit_web_view_set_settings (WEBKIT_WEB_VIEW(view), settings);
webkit_web_view_load_string (WEBKIT_WEB_VIEW(view), webkit_web_view_load_html (WEBKIT_WEB_VIEW(view), str, NULL);
str, "text/html", "utf-8", "");
g_object_unref (settings); g_object_unref (settings);
frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW(view));
if (!frame) {
g_set_error (err, 0, MU_ERROR, "cannot get web frame");
return FALSE;
}
started = time (NULL); started = time (NULL);
do { do {
status = webkit_web_view_get_load_status (WEBKIT_WEB_VIEW(view)); loading = webkit_web_view_is_loading (WEBKIT_WEB_VIEW(view));
gtk_main_iteration_do (FALSE); gtk_main_iteration_do (FALSE);
} while (status != WEBKIT_LOAD_FINISHED && } while (loading && (time(NULL) - started) <= max_time);
(time(NULL) - started) <= max_time);
return print_to_pdf (frame, err); return print_to_pdf (view, err);
} }
static void static void
add_header (GString *gstr, const char* header, const char *val) add_header (GString *gstr, const char* header, const char *val)
{ {

View File

@ -30,6 +30,7 @@ AM_CPPFLAGS=-I${top_srcdir} -I${top_srcdir}/lib $(GTK_CFLAGS) $(WEBKIT_CFLAGS) \
# really need all the params they get # really need all the params they get
AM_CFLAGS= \ AM_CFLAGS= \
$(WARN_CFLAGS) \ $(WARN_CFLAGS) \
$(ASAN_CFLAGS) \
-Wno-redundant-decls \ -Wno-redundant-decls \
-Wno-deprecated-declarations \ -Wno-deprecated-declarations \
-Wno-switch-enum -Wno-switch-enum
@ -67,6 +68,7 @@ DISTCLEANFILES= \
$(BUILT_SOURCES) $(BUILT_SOURCES)
mug_LDADD= \ mug_LDADD= \
$(ASAN_LDFLAGS) \
${top_builddir}/lib/libmu.la \ ${top_builddir}/lib/libmu.la \
libmuwidgets.la \ libmuwidgets.la \
${GTK_LIBS} ${GTK_LIBS}