[GTK] Add webkit_print_operation_print() to WebKit2 GTK+ API
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 17 Feb 2012 14:25:58 +0000 (14:25 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 17 Feb 2012 14:25:58 +0000 (14:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=76536

Reviewed by Gustavo Noronha Silva.

* UIProcess/API/gtk/WebKitPrintOperation.cpp:
(webkitPrintOperationPrintPagesForFrame): Helper function to call
WebPageProxy::drawPagesForPrinting using the given
GtkPrintSettings and GtkPageSetup.
(webkitPrintOperationRunDialogForFrame): Use
webkitPrintOperationPrintPagesForFrame().
(webkit_print_operation_print): Print directly using current
GtkPrintSettings and GtkPageSetup without showing the print
dialog.
* UIProcess/API/gtk/WebKitPrintOperation.h:
* UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Add new symbols.
* UIProcess/API/gtk/tests/GNUmakefile.am:
* UIProcess/API/gtk/tests/TestPrinting.cpp:
(testPrintOperationPrintLoadChanged):
(testPrintOperationPrintDone):
(testPrintOperationPrintPrinter):
(testPrintOperationPrint):
(beforeAll):
(afterAll):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@108068 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/gtk/WebKitPrintOperation.cpp
Source/WebKit2/UIProcess/API/gtk/WebKitPrintOperation.h
Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt
Source/WebKit2/UIProcess/API/gtk/tests/GNUmakefile.am
Source/WebKit2/UIProcess/API/gtk/tests/TestPrinting.cpp

index 5c36299..a69d188 100644 (file)
@@ -1,3 +1,30 @@
+2012-02-17  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK] Add webkit_print_operation_print() to WebKit2 GTK+ API
+        https://bugs.webkit.org/show_bug.cgi?id=76536
+
+        Reviewed by Gustavo Noronha Silva.
+
+        * UIProcess/API/gtk/WebKitPrintOperation.cpp:
+        (webkitPrintOperationPrintPagesForFrame): Helper function to call
+        WebPageProxy::drawPagesForPrinting using the given
+        GtkPrintSettings and GtkPageSetup.
+        (webkitPrintOperationRunDialogForFrame): Use
+        webkitPrintOperationPrintPagesForFrame().
+        (webkit_print_operation_print): Print directly using current
+        GtkPrintSettings and GtkPageSetup without showing the print
+        dialog.
+        * UIProcess/API/gtk/WebKitPrintOperation.h:
+        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Add new symbols.
+        * UIProcess/API/gtk/tests/GNUmakefile.am:
+        * UIProcess/API/gtk/tests/TestPrinting.cpp:
+        (testPrintOperationPrintLoadChanged):
+        (testPrintOperationPrintDone):
+        (testPrintOperationPrintPrinter):
+        (testPrintOperationPrint):
+        (beforeAll):
+        (afterAll):
+
 2012-02-17  No'am Rosenthal  <noam.rosenthal@nokia.com>
 
         [Qt][WK2] Allow partial updates
index 6e4bb32..dab16bf 100644 (file)
@@ -240,6 +240,13 @@ static void drawPagesForPrintingCompleted(WKErrorRef, void* context)
     g_signal_emit(printOperation.get(), signals[DONE], 0, NULL);
 }
 
+static void webkitPrintOperationPrintPagesForFrame(WebKitPrintOperation* printOperation, WebFrameProxy* webFrame, GtkPrintSettings* printSettings, GtkPageSetup* pageSetup)
+{
+    PrintInfo printInfo(printSettings, pageSetup);
+    WebPageProxy* page = webkitWebViewBaseGetPage(WEBKIT_WEB_VIEW_BASE(printOperation->priv->webView));
+    page->drawPagesForPrinting(webFrame, printInfo, VoidCallback::create(g_object_ref(printOperation), &drawPagesForPrintingCompleted));
+}
+
 void webkitPrintOperationRunDialogForFrame(WebKitPrintOperation* printOperation, GtkWindow* parent, WebFrameProxy* webFrame)
 {
     WebKitPrintOperationPrivate* priv = printOperation->priv;
@@ -260,9 +267,7 @@ void webkitPrintOperationRunDialogForFrame(WebKitPrintOperation* printOperation,
         return;
     }
 
-    PrintInfo printInfo(priv->printSettings.get(), priv->pageSetup.get());
-    WebPageProxy* page = webkitWebViewBaseGetPage(WEBKIT_WEB_VIEW_BASE(priv->webView));
-    page->drawPagesForPrinting(webFrame, printInfo, VoidCallback::create(g_object_ref(printOperation), &drawPagesForPrintingCompleted));
+    webkitPrintOperationPrintPagesForFrame(printOperation, webFrame, priv->printSettings.get(), priv->pageSetup.get());
 }
 
 /**
@@ -377,3 +382,24 @@ void webkit_print_operation_run_dialog(WebKitPrintOperation* printOperation, Gtk
     webkitPrintOperationRunDialogForFrame(printOperation, parent, page->mainFrame());
 }
 
+/**
+ * webkit_print_operation_print:
+ * @print_operation: a #WebKitPrintOperation
+ *
+ * Start a print operation using current print settings and page setup
+ * without showing the print dialog. If either print settings or page setup
+ * are not set with webkit_print_operation_set_print_settings() and
+ * webkit_print_operation_set_page_setup(), the default options will be used
+ * and the print job will be sent to the default printer.
+ */
+void webkit_print_operation_print(WebKitPrintOperation* printOperation)
+{
+    g_return_if_fail(WEBKIT_IS_PRINT_OPERATION(printOperation));
+
+    WebKitPrintOperationPrivate* priv = printOperation->priv;
+    GRefPtr<GtkPrintSettings> printSettings = priv->printSettings ? priv->printSettings : adoptGRef(gtk_print_settings_new());
+    GRefPtr<GtkPageSetup> pageSetup = priv->pageSetup ? priv->pageSetup : adoptGRef(gtk_page_setup_new());
+
+    WebPageProxy* page = webkitWebViewBaseGetPage(WEBKIT_WEB_VIEW_BASE(printOperation->priv->webView));
+    webkitPrintOperationPrintPagesForFrame(printOperation, page->mainFrame(), printSettings.get(), pageSetup.get());
+}
index c17a6e5..f956549 100644 (file)
@@ -74,6 +74,9 @@ WEBKIT_API void
 webkit_print_operation_run_dialog         (WebKitPrintOperation *print_operation,
                                            GtkWindow            *parent);
 
+WEBKIT_API void
+webkit_print_operation_print              (WebKitPrintOperation *print_operation);
+
 G_END_DECLS
 
 #endif
index 042645d..4f6bd2a 100644 (file)
@@ -426,6 +426,7 @@ webkit_print_operation_set_print_settings
 webkit_print_operation_get_page_setup
 webkit_print_operation_set_page_setup
 webkit_print_operation_run_dialog
+webkit_print_operation_print
 
 <SUBSECTION Standard>
 WebKitPrintOperationClass
index 5d19d83..d84d7a3 100644 (file)
@@ -122,6 +122,6 @@ Programs_WebKit2APITests_TestWebViewEditor_LDFLAGS = $(webkit2_tests_ldflags)
 
 Programs_WebKit2APITests_TestPrinting_SOURCES = \
         Source/WebKit2/UIProcess/API/gtk/tests/TestPrinting.cpp
-Programs_WebKit2APITests_TestPrinting_CPPFLAGS = $(webkit2_tests_cppflags)
-Programs_WebKit2APITests_TestPrinting_LDADD = $(webkit2_tests_ldadd)
+Programs_WebKit2APITests_TestPrinting_CPPFLAGS = $(webkit2_tests_cppflags) $(GTK_UNIX_PRINTING_CFLAGS)
+Programs_WebKit2APITests_TestPrinting_LDADD = $(webkit2_tests_ldadd) $(GTK_UNIX_PRINTING_LIBS)
 Programs_WebKit2APITests_TestPrinting_LDFLAGS = $(webkit2_tests_ldflags)
index b8bbc35..f0d5532 100644 (file)
 
 #include "config.h"
 #include "WebViewTest.h"
+#include <glib/gstdio.h>
 #include <wtf/gobject/GRefPtr.h>
 
+#ifdef HAVE_GTK_UNIX_PRINTING
+#include <gtk/gtkunixprint.h>
+#endif
+
+static char* kTempDirectory;
+
 static void testPrintOperationPrintSettings(WebViewTest* test, gconstpointer)
 {
     GRefPtr<WebKitPrintOperation> printOperation = adoptGRef(webkit_print_operation_new(test->m_webView));
@@ -64,12 +71,82 @@ static void testWebViewPrintRequested(WebViewTest* test, gconstpointer)
     g_main_loop_run(test->m_mainLoop);
 }
 
+#ifdef HAVE_GTK_UNIX_PRINTING
+static void testPrintOperationPrintLoadChanged(WebKitWebView*, WebKitLoadEvent loadEvent, WebViewTest* test)
+{
+    if (loadEvent != WEBKIT_LOAD_FINISHED)
+        return;
+    g_main_loop_quit(test->m_mainLoop);
+}
+
+static void testPrintOperationPrintDone(WebKitPrintOperation* printOperation, WebViewTest* test)
+{
+    g_object_unref(printOperation);
+    g_main_loop_quit(test->m_mainLoop);
+}
+
+static gboolean testPrintOperationPrintPrinter(GtkPrinter* printer, gpointer userData)
+{
+    if (strcmp(gtk_printer_get_name(printer), "Print to File"))
+        return FALSE;
+
+    GtkPrinter** foundPrinter = static_cast<GtkPrinter**>(userData);
+    *foundPrinter = static_cast<GtkPrinter*>(g_object_ref(printer));
+    return TRUE;
+}
+
+static void testPrintOperationPrint(WebViewTest* test, gconstpointer)
+{
+    g_signal_connect(test->m_webView, "load-changed", G_CALLBACK(testPrintOperationPrintLoadChanged), test);
+    test->loadHtml("<html><body>WebKitGTK+ printing test</body></html>", 0);
+    g_main_loop_run(test->m_mainLoop);
+
+    GtkPrinter* printer = 0;
+    gtk_enumerate_printers(testPrintOperationPrintPrinter, &printer, 0, TRUE);
+    if (!printer) {
+        g_message("%s", "Cannot test WebKitPrintOperation/print: no suitable printer found");
+        return;
+    }
+
+    GOwnPtr<char> outputFilename(g_build_filename(kTempDirectory, "webkit-print.pdf", NULL));
+    GRefPtr<GFile> outputFile = adoptGRef(g_file_new_for_path(outputFilename.get()));
+    GOwnPtr<char> outputURI(g_file_get_uri(outputFile.get()));
+
+    GRefPtr<GtkPrintSettings> printSettings = adoptGRef(gtk_print_settings_new());
+    gtk_print_settings_set_printer(printSettings.get(), gtk_printer_get_name(printer));
+    gtk_print_settings_set(printSettings.get(), GTK_PRINT_SETTINGS_OUTPUT_URI, outputURI.get());
+    g_object_unref(printer);
+
+    GRefPtr<WebKitPrintOperation> printOperation = webkit_print_operation_new(test->m_webView);
+    test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(printOperation.get()));
+    g_signal_connect(printOperation.get(), "done", G_CALLBACK(testPrintOperationPrintDone), test);
+    webkit_print_operation_set_print_settings(printOperation.get(), printSettings.get());
+    webkit_print_operation_print(printOperation.get());
+    g_main_loop_run(test->m_mainLoop);
+
+    GRefPtr<GFileInfo> fileInfo = adoptGRef(g_file_query_info(outputFile.get(), G_FILE_ATTRIBUTE_STANDARD_SIZE","G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                                                              static_cast<GFileQueryInfoFlags>(0), 0, 0));
+    g_assert(fileInfo.get());
+    g_assert_cmpint(g_file_info_get_size(fileInfo.get()), >, 0);
+    g_assert_cmpstr(g_file_info_get_content_type(fileInfo.get()), ==, "application/pdf");
+
+    g_file_delete(outputFile.get(), 0, 0);
+}
+#endif // HAVE_GTK_UNIX_PRINTING
+
 void beforeAll()
 {
+    kTempDirectory = g_dir_make_tmp("WebKit2Tests-XXXXXX", 0);
+    g_assert(kTempDirectory);
+
     WebViewTest::add("WebKitPrintOperation", "printing-settings", testPrintOperationPrintSettings);
     WebViewTest::add("WebKitWebView", "print-requested", testWebViewPrintRequested);
+#ifdef HAVE_GTK_UNIX_PRINTING
+    WebViewTest::add("WebKitPrintOperation", "print", testPrintOperationPrint);
+#endif
 }
 
 void afterAll()
 {
+    g_rmdir(kTempDirectory);
 }