示例#1
0
static gboolean
mail_printer_print_timeout_cb (gpointer user_data)
{
    GSimpleAsyncResult *simple;
    AsyncContext *async_context;
    GCancellable *cancellable;
    GtkPrintOperation *print_operation;
    GtkPrintOperationAction print_action;
    EMailPrinter *printer;
    WebKitWebFrame *web_frame;
    gulong create_custom_widget_handler_id;
    gulong custom_widget_apply_handler_id;
    gulong draw_page_handler_id;
    GError *error = NULL;

    simple = G_SIMPLE_ASYNC_RESULT (user_data);
    async_context = g_simple_async_result_get_op_res_gpointer (simple);

    cancellable = async_context->cancellable;
    print_action = async_context->print_action;

    /* Check for cancellation one last time before printing. */
    if (g_cancellable_set_error_if_cancelled (cancellable, &error))
        goto exit;

    /* This returns a new reference. */
    printer = (EMailPrinter *) g_async_result_get_source_object (
                  G_ASYNC_RESULT (simple));

    print_operation = e_print_operation_new ();

    gtk_print_operation_set_show_progress (print_operation, TRUE);
    gtk_print_operation_set_unit (print_operation, GTK_UNIT_PIXEL);

    if (async_context->print_action == GTK_PRINT_OPERATION_ACTION_EXPORT) {
        const gchar *export_filename;

        export_filename =
            e_mail_printer_get_export_filename (printer);
        gtk_print_operation_set_export_filename (
            print_operation, export_filename);
    }

    create_custom_widget_handler_id = g_signal_connect (
                                          print_operation, "create-custom-widget",
                                          G_CALLBACK (mail_printer_create_custom_widget_cb),
                                          async_context);

    custom_widget_apply_handler_id = g_signal_connect (
                                         print_operation, "custom-widget-apply",
                                         G_CALLBACK (mail_printer_custom_widget_apply_cb),
                                         async_context);

    draw_page_handler_id = g_signal_connect (
                               print_operation, "draw-page",
                               G_CALLBACK (mail_printer_draw_footer_cb),
                               async_context->cancellable);

    web_frame = webkit_web_view_get_main_frame (async_context->web_view);

    async_context->print_result = webkit_web_frame_print_full (
                                      web_frame, print_operation, print_action, &error);

    /* Sanity check. */
    switch (async_context->print_result) {
    case GTK_PRINT_OPERATION_RESULT_ERROR:
        if (error == NULL)
            g_warning (
                "WebKit print operation returned "
                "ERROR result without setting a "
                "GError");
        break;
    case GTK_PRINT_OPERATION_RESULT_APPLY:
        if (error != NULL)
            g_warning (
                "WebKit print operation returned "
                "APPLY result but also set a GError");
        break;
    case GTK_PRINT_OPERATION_RESULT_CANCEL:
        if (error != NULL)
            g_warning (
                "WebKit print operation returned "
                "CANCEL result but also set a GError");
        break;
    default:
        g_warn_if_reached ();
    }

    g_signal_handler_disconnect (
        print_operation, create_custom_widget_handler_id);

    g_signal_handler_disconnect (
        print_operation, custom_widget_apply_handler_id);

    g_signal_handler_disconnect (
        print_operation, draw_page_handler_id);

    g_object_unref (print_operation);

    g_object_unref (printer);

exit:
    if (error != NULL)
        g_simple_async_result_take_error (simple, error);

    g_simple_async_result_complete_in_idle (simple);

    return FALSE;
}
示例#2
0
static gboolean
mail_printer_print_timeout_cb (GTask *task)
{
	AsyncContext *async_context;
	gpointer source_object;
	const gchar *export_filename;
	GtkPrintSettings *print_settings = NULL;
	WebKitPrintOperation *print_operation = NULL;
	WebKitPrintOperationResponse response;
	/* FIXME WK2
	gulong draw_page_handler_id;
	gulong create_custom_widget_handler_id;
	gulong custom_widget_apply_handler_id;*/

	async_context = g_task_get_task_data (task);

	g_return_val_if_fail (async_context != NULL, G_SOURCE_REMOVE);

	source_object = g_task_get_source_object (task);

	g_return_val_if_fail (E_IS_MAIL_PRINTER (source_object), G_SOURCE_REMOVE);

	print_settings = gtk_print_settings_new ();
	export_filename = e_mail_printer_get_export_filename (E_MAIL_PRINTER (source_object));
	gtk_print_settings_set (
		print_settings,
		GTK_PRINT_SETTINGS_OUTPUT_BASENAME,
		export_filename);

	print_operation = webkit_print_operation_new (async_context->web_view);
	webkit_print_operation_set_print_settings (print_operation, print_settings);

	g_signal_connect_data (
		print_operation, "failed",
		G_CALLBACK (mail_printer_print_failed_cb),
		g_object_ref (task),
		(GClosureNotify) g_object_unref, 0);

	g_signal_connect_data (
		print_operation, "finished",
		G_CALLBACK (mail_printer_print_finished_cb),
		g_object_ref (task),
		(GClosureNotify) g_object_unref, 0);

	/* FIXME WK2
	create_custom_widget_handler_id = g_signal_connect (
		print_operation, "create-custom-widget",
		G_CALLBACK (mail_printer_create_custom_widget_cb),
		async_context);

	custom_widget_apply_handler_id = g_signal_connect (
		print_operation, "custom-widget-apply",
		G_CALLBACK (mail_printer_custom_widget_apply_cb),
		async_context); */

	/* FIXME WK2 - this will be hard to add back to WK2 API.. There is a CSS draft
	 * that can be used to add a page numbers, but it is not in WebKit yet.
	 * http://www.w3.org/TR/css3-page/
	draw_page_handler_id = g_signal_connect (
		print_operation, "draw-page",
		G_CALLBACK (mail_printer_draw_footer_cb),
		async_context->cancellable); */

	response = webkit_print_operation_run_dialog (print_operation, NULL);

	/* FIXME WK2
	g_signal_handler_disconnect (
		print_operation, create_custom_widget_handler_id);

	g_signal_handler_disconnect (
		print_operation, custom_widget_apply_handler_id);

	g_signal_handler_disconnect (
		print_operation, draw_page_handler_id); */

	g_clear_object (&print_operation);
	g_clear_object (&print_settings);

	if (response == WEBKIT_PRINT_OPERATION_RESPONSE_CANCEL) {
		async_context->print_result = GTK_PRINT_OPERATION_RESULT_CANCEL;
		g_task_return_boolean (task, TRUE);
	}

	return G_SOURCE_REMOVE;
}