clinuxwebkitgtk

C Web Browser Download File


WebkitGTK+ API Reference

What I'm trying to do is run my HTML5 app on Linux that way I and my users can still use my app without relying on an internet connection.

My problem is when I go to download say a zip file. The download doesn't execute because there isn't an adequate url for the file to save to (like the desktop). Therefore it doesn't download.

Thus my question relies onto how am I suppose to get an adequate url to download this file when it's executed dynamically via JSZip. (It works fine in Chrome, just not in my app). Terminal says...

source.c:35:3: warning: ‘return’ with a value, in function returning void [enabled by default] return TRUE; ^

Here's my code:

/*
  Save this file as main.c and compile it using this command
  (those are backticks, not single quotes):
    gcc -Wall -g -o source source.c `pkg-config --cflags --libs gtk+-2.0 webkit-1.0` -export-dynamic

  Then execute it using:
  ./source

  If you can't compile chances are you don't have gcc installed.
  Install gcc/c with the following terminal command. (This command is for Debian based Linux distros)
    sudo apt-get install libgtk2.0-dev libgtk2.0-doc libglib2.0-doc

  WebKit requires libraries to successfully aquire, configure, and compile. You can get libraries by issuing the following command in your terminal:
    sudo apt-get install subversion gtk-doc-tools autoconf automake libtool libgtk2.0-dev libpango1.0-dev libicu-dev libxslt-dev libsoup2.4-dev libsqlite3-dev gperf bison flex libjpeg62-dev libpng12-dev libxt-dev autotools-dev libgstreamer-plugins-base0.10-dev libenchant-dev libgail-dev

  WebkitGTK+ API Reference: http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitdownload.html

  Ubuntu Webkit information - https://help.ubuntu.com/community/WebKit
    sudo apt-get install libwebkitgtk-dev python-webkit-dev python-webkit

  Required dependencies for this build: (If you installed all the above this is not needed)
    sudo apt-get install libgtk2.0-dev libgtk2.0-doc libglib2.0-doc subversion gtk-doc-tools autoconf automake libtool libgtk2.0-dev libpango1.0-dev libicu-dev libxslt-dev libsoup2.4-dev libsqlite3-dev gperf bison flex libjpeg62-dev libpng12-dev libxt-dev autotools-dev libgstreamer-plugins-base0.10-dev libenchant-dev libgail-dev libwebkitgtk-dev
*/

#include <gtk/gtk.h>
#include <webkit/webkit.h>

static void destroy_cb(GtkWidget* widget, gpointer data) {
  gtk_main_quit();
}

static void download_requested_cb(WebKitWebView *web_view, WebKitDownload *download) {
  const gchar* dest = g_strdup_printf("%s", "file:///home/michael/Downloads/test.zip");
  //set the destination uri (eg, send the file to a downloads folder)
  webkit_download_set_destination_uri(download, dest);
  webkit_download_start();

  return TRUE;
}

int main(int argc, char* argv[]) {
  GtkWidget* window;
  WebKitWebView* web_view;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_name (window, "AppName");
  gtk_window_set_default_size(GTK_WINDOW(window), 600, 600);
  //gtk_window_set_icon_from_file(window, "app/logo.png", NULL);
  g_signal_connect(window, "destroy", G_CALLBACK(destroy_cb), NULL);

  web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());

  /* Register a callback that gets invoked each time a download is requested */
  g_object_connect(web_view, "download-requested", G_CALLBACK(download_requested_cb), NULL);

  char uri[PATH_MAX];
  char cwd[PATH_MAX];

  getcwd(cwd, sizeof(cwd));

  if (argc > 1)
      snprintf(uri, sizeof(uri), "%s", argv[1]);
  else
      snprintf(uri, sizeof(uri), "file://%s/app/index.html", cwd);

  webkit_web_view_open (web_view, uri);

  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(web_view));
  gtk_widget_grab_focus(GTK_WIDGET(web_view));
  gtk_widget_show_all(window);
  gtk_main();
  return 0;
}

Solution

  • 1) Your basic premise is sound:

    file:///home/michael/Downloads/test.zip // This syntax should allow you to "upload" a local file on your hard drve

    2) This is a compile warning (not an error). In theory, you could ignore it:

    source.c:35:3: warning: ‘return’ with a value, in function returning void [enabled by default] return TRUE; ^
    

    3) This is the problem:

    static void download_requested_cb(WebKitWebView *web_view, ... 
      ...    
      return TRUE; // Delete this line.  You can't return anything from a "void" function!
    }