1

I'm currently running an html file in webkit gtk view. I set these settings:

    let new_settings = new WebKit.WebSettings ();
    new_settings.enable_universal_access_from_file_uris = true;
    this._web_view.set_settings(new_settings);

thinking they would let me download a file on my computer (which isn't exactly what I'm trying to do but I wanted to test it). This didn't work :/

The html responsible is below:

<a href="resume/resume1.doc"><img class="shadow" src="images/design/1.jpg" alt="img01"></a>

What I'm trying to do is to automatically open resume1.doc inside libre office when the user clicks the image. I'm not too sure how to do that with GTK/HTML

Thanks! :)

user2494251
  • 33
  • 2
  • 9

2 Answers2

1

Its not clear whether the page is being served from server or loaded locally.

I have not done this with local files, but for pages served by server you will monitor mime type decision and indicate to webkit that it needs to download the mime type for mime types it can not handle (or even for the mime types that it can handle, if you want to download a web page). Down the line, you will provide a file name and monitor the progress. Webkit will inform you once the download is complete. Signals that will allow you to do this are

  • mime-type-policy-decision-requested
  • download-requested
  • notify::status

For local files, I don't know if above approach will work. If it does not, since you are controlling the page, you can have link attributes that can tell you that file needs to be opened rather than navigating.

Once you have the file path from either approach, you can use xdg-open command or it's equivalent functionality to open the file in the application that can handle the files.

user871199
  • 1,420
  • 19
  • 28
0

You have the start of it right. You just need to handle for mime-type and decide how you would like to open Libre Office. Here's an example for local files (the uri is the path to a specific document on the server that you would like to open locally):

this._web_view.connect('mime-type-policy-decision-requested',
        (function (webview, frame, request, mimetype, decision) {
            if (mimetype === 'application/msword' ||
                mimetype === 'application/vnd.oasis.opendocument.spreadsheet') {
                // Spawn a libreoffice process with this uri. Necessary because
                // we want to open the files as templates - the `-n` option
                // requires the user to save-as.
                GLib.spawn_async(null, /* cwd */
                                 ['libreoffice', '-n', request.get_uri()],
                                 null, /* inherit environment */
                                 GLib.SpawnFlags.DO_NOT_REAP_CHILD | GLib.SpawnFlags.SEARCH_PATH,
                                 null /* setup function */ );
                decision.ignore();
                return true;

            } else if (mimetype === 'application/pdf') {
                // if PDF, use the build in viewer (usually evince)
                Gtk.show_uri(null, request.get_uri(), 0);
                decision.ignore();
                return true;
            }
            // default handler
            return false;
    }).bind(this));