'Save page of WebKitWebView into file

I have this almost solved. I've found this function:

void
webkit_web_view_save_to_file (WebKitWebView *web_view,
                              GFile *file,
                              WebKitSaveMode save_mode,
                              GCancellable *cancellable,
                              GAsyncReadyCallback callback,
                              gpointer user_data);

in the WebKitGTK official documentation

In my project I use it like this:

webkit_web_view_save_to_file( WEBKIT_WEB_VIEW(webview), g_file_new_for_path("/absolute/path/to/file"), WEBKIT_SAVE_MODE_MHTML, NULL, NULL, NULL);

However, it does absolutely nothing (There's no file where it should be - maybe I'm missing something?). I've even put it inside a while(true) loop (With a sleep(10) after the webkit_web_view_save_to_file()).

How can I make it work?



Solution 1:[1]

It seems that the process is asynchronous, so I had to write another function that got the result.

The function:

static void webkit_result (GObject      *source_object,
                GAsyncResult *res,
                gpointer      user_data)
{
    gboolean success = FALSE;

    success = webkit_web_view_save_to_file_finish(WEBKIT_WEB_VIEW(source_object), res, NULL);

    if (success)
        printf("Hurray!\n");
    else
        printf("Uh oh!\n");

    /* ... */

}

And then the corrected line of code:

  webkit_web_view_save_to_file(WEBKIT_WEB_VIEW(webview),  g_file_new_for_path("/absolute/path/to/file"), WEBKIT_SAVE_MODE_MHTML, NULL, webkit_result, NULL);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1