Exemple #1
0
	/**
		\brief Save the content of the current page.
		\param app Pointer to the application object (or NULL).
		\param w Pointer to the current window object (or NULL).
		\param context Execution context.
		\param arg List of arguments.
		\return return_void on success.
		\return return_error if there is no window or the argument list is badly formed.
	**/
	int save(application *app, app_window *w, const exec_context context, std::vector<std::string> arg)
	{
		unsigned arg_size = arg.size();
		std::string cmd = haku::string::implode(arg,' ');
		
		if ( w == NULL && arg_size >= 2 )
		{
			WebKitNetworkRequest *r = webkit_network_request_new(arg[1].c_str());			
			
			w = app->create_window();
			w->download = webkit_download_new(r);
			w->osl = (context == exec_context_callback);
			
			if ( arg_size >= 3 )
				webkit_download_set_destination_uri(w->download, haku::string::trim(cmd.substr(arg[0].size()+2+arg[1].size(), -1)).c_str());
			
			app_window::initdownload(NULL, w->download, w);
			
			return return_void;
		}
		else if ( w != NULL )
		{
			WebKitNetworkRequest *r = webkit_network_request_new(w->geturi().c_str());			
			WebKitDownload *o = webkit_download_new(r);
			
			if ( arg_size >= 2 )
				webkit_download_set_destination_uri(o, haku::string::trim(cmd.substr(arg[0].size(), -1)).c_str());
			
			app_window::initdownload(NULL, o, w);
			
			return return_void;
		}
		
		return return_error;
	}
Exemple #2
0
gboolean
initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
	const char *filename;
	char *uri, *html;

	stop(c, NULL);
	c->download = o;
	filename = webkit_download_get_suggested_filename(o);
	if(!strcmp("", filename))
		filename = "index.html";
	uri = g_strconcat("file://", dldir, "/", filename, NULL);
	webkit_download_set_destination_uri(c->download, uri);
	c->progress = 0;
	g_free(uri);
	html = g_strdup_printf("Download <b>%s</b>...", filename);
	webkit_web_view_load_html_string(c->view, html,
			webkit_download_get_uri(c->download));
	g_signal_connect(c->download, "notify::progress", G_CALLBACK(updatedownload), c);
	g_signal_connect(c->download, "notify::status", G_CALLBACK(updatedownload), c);
	webkit_download_start(c->download);
	
	c->title = copystr(&c->title, filename);
	update(c);
	g_free(html);
	return TRUE;
}
Exemple #3
0
gboolean cb_wv_download_requested(WebKitWebView *view, WebKitDownload *download, Browser *b)
{
	const char *suggested_filename = webkit_download_get_suggested_filename(download);
	char *download_path = NULL;
	char *filename;

	/* build download dir if necessary */
	if (download_dir[0] == '~') {
		download_path = g_build_filename(g_get_home_dir(), download_dir + 1, NULL);
	} else {
		download_path = g_strdup(download_dir);
	}

	g_mkdir_with_parents(download_path, 0771);

	/* download file */
	filename = g_build_filename("file://", download_path, suggested_filename, NULL);

	webkit_download_set_destination_uri(download, filename);
	g_signal_connect(G_OBJECT(download), "notify::status", G_CALLBACK(cb_download_notify_status), b);

	g_free(download_path);
	g_free(filename);

	return TRUE;
}
Exemple #4
0
static void
test_webkit_download_create(void)
{
    WebKitNetworkRequest* request;
    WebKitDownload* download;
    const gchar* uri = "http://example.com";
    gchar* tmpDir;

    request = webkit_network_request_new(uri);
    download = webkit_download_new(request);
    g_object_unref(request);
    g_assert_cmpstr(webkit_download_get_uri(download), ==, uri);
    g_assert(webkit_download_get_network_request(download) == request);
    g_assert(g_strrstr(uri, webkit_download_get_suggested_filename(download)));
    g_assert(webkit_download_get_status(download) == WEBKIT_DOWNLOAD_STATUS_CREATED);
    g_assert(!webkit_download_get_total_size(download));
    g_assert(!webkit_download_get_current_size(download));
    g_assert(!webkit_download_get_progress(download));
    g_assert(!webkit_download_get_elapsed_time(download));
    tmpDir = g_filename_to_uri(g_get_tmp_dir(), NULL, NULL);
    webkit_download_set_destination_uri(download, tmpDir);
    g_assert_cmpstr(tmpDir, ==, webkit_download_get_destination_uri(download));;
    g_free(tmpDir);
    g_object_unref(download);
}
Exemple #5
0
/* download_on_idle */
static gboolean _download_on_idle(gpointer data)
{
    Download * download = data;
    DownloadPrefs * prefs = &download->prefs;
#ifdef WITH_WEBKIT
    char * p = NULL;
    char * cwd = NULL;
    size_t len;
    WebKitNetworkRequest * request;

    download->timeout = 0;
    if(prefs->output[0] != '/' && (cwd = getcwd(NULL, 0)) == NULL)
    {
        _download_error(download, prefs->output, 0);
        download_cancel(download);
        return FALSE;
    }
    len = ((cwd != NULL) ? strlen(cwd) : 0) + strlen(prefs->output) + 7;
    if((p = malloc(len)) == NULL)
    {
        _download_error(download, prefs->output, 0);
        download_cancel(download);
        free(cwd);
        return FALSE;
    }
    snprintf(p, len, "%s%s%s%s", "file:", (cwd != NULL) ? cwd : "",
             (cwd != NULL) ? "/" : "", prefs->output);
    request = webkit_network_request_new(download->url);
    download->conn = webkit_download_new(request);
    webkit_download_set_destination_uri(download->conn, p);
    free(p);
    free(cwd);
    webkit_download_start(download->conn);
#else
    download->timeout = 0;
    if((download->fp = fopen(prefs->output, "w")) == NULL)
    {
        _download_error(download, prefs->output, 0);
        download_cancel(download);
        return FALSE;
    }
    download->conn = gnet_conn_http_new();
    if(gnet_conn_http_set_method(download->conn, GNET_CONN_HTTP_METHOD_GET,
                                 NULL, 0) != TRUE)
        return _download_error(download, _("Unknown error"), FALSE);
    gnet_conn_http_set_uri(download->conn, download->url);
    if(prefs->user_agent != NULL)
        gnet_conn_http_set_user_agent(download->conn,
                                      prefs->user_agent);
    gnet_conn_http_run_async(download->conn, _download_on_http, download);
#endif
    download->timeout = g_timeout_add(250, _download_on_timeout, download);
    return FALSE;
}
Exemple #6
0
JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1webkit_1download_1set_1destination_1uri)
	(JNIEnv *env, jclass that, jintLong arg0, jbyteArray arg1)
{
	jbyte *lparg1=NULL;
	WebKitGTK_NATIVE_ENTER(env, that, _1webkit_1download_1set_1destination_1uri_FUNC);
	if (arg1) if ((lparg1 = (*env)->GetByteArrayElements(env, arg1, NULL)) == NULL) goto fail;
	webkit_download_set_destination_uri((WebKitDownload *)arg0, (const gchar *)lparg1);
fail:
	if (arg1 && lparg1) (*env)->ReleaseByteArrayElements(env, arg1, lparg1, 0);
	WebKitGTK_NATIVE_EXIT(env, that, _1webkit_1download_1set_1destination_1uri_FUNC);
}
Exemple #7
0
gboolean download_req(WebKitWebView* webView, WebKitDownload *download, gboolean *handled)
{
    const gchar *uri = webkit_download_get_uri(download);
    char *p = strrchr(uri, '/');
    if (!p)
	return FALSE;

    gchar *dest = g_strdup_printf("file:///%s/%s", video_dir, p+1);
    webkit_download_set_destination_uri(download, dest);
    g_free(dest);

    cur_download = download;
    return TRUE;
}
Exemple #8
0
Fichier : surf.c Projet : sr/surf
gboolean
download(WebKitWebView *view, WebKitDownload *o, gpointer d) {
    /* TODO */
    const gchar *home;
    gchar *uri, *filename;

    home = g_get_home_dir();
    filename = g_build_filename(home, "Desktop",
                                webkit_download_get_suggested_filename(o), NULL);
    uri = g_strconcat("file://", filename, NULL);
    webkit_download_set_destination_uri(o, uri);
    g_free(filename);
    g_free(uri);
    webkit_download_start(o);
    return TRUE;
}
Exemple #9
0
static gboolean
download_requested_cb(WebKitWebView* web_view,
                      WebKitDownload* download,
                      gboolean* beenThere)
{
    *beenThere = TRUE;
    if (temporaryFilename) {
        gchar *uri = g_filename_to_uri(temporaryFilename, NULL, NULL);
        if (uri)
            webkit_download_set_destination_uri(download, uri);
        g_free(uri);
    }

    g_signal_connect(download, "notify::status",
                     G_CALLBACK(notify_status_cb), NULL);

    return TRUE;
}
Exemple #10
0
/**
 * Sets the destination of a download.
 *
 * Converts the given destination to a \c file:// URI.
 *
 * \param L The Lua VM state.
 * \param download The \ref download_t of the download.
 *
 * \luastack
 * \lparam A \c download object.
 * \lvalue A string containing the new destination for the download.
 */
static gint
luaH_download_set_destination(lua_State *L, download_t *download)
{
    if (download_is_started(download)) {
        luaH_warn(L, "cannot change destination while download is running");
        return 0;
    }

    const gchar *destination = luaL_checkstring(L, -1);
    gchar *uri = g_filename_to_uri(destination, NULL, NULL);
    if (uri) {
        download->destination = g_strdup(destination);
        webkit_download_set_destination_uri(download->webkit_download, uri);
        g_free(uri);
        luaH_object_emit_signal(L, -3, "property::destination", 0, 0);

    /* g_filename_to_uri failed on destination path */
    } else {
        lua_pushfstring(L, "invalid destination: '%s'", destination);
        lua_error(L);
    }
    return 0;
}
Exemple #11
0
/**
 * ephy_download_set_destination_uri:
 * @download: an #EphyDownload
 * @destination: URI where to save @download
 *
 * Sets the destination URI of @download. It must be a proper URI, with a
 * scheme like file:/// or similar.
 **/
void
ephy_download_set_destination_uri (EphyDownload *download,
                                   const char *destination)
{
  EphyDownloadPrivate *priv;
  char *scheme;

  g_return_if_fail (EPHY_IS_DOWNLOAD (download));
  g_return_if_fail (destination != NULL);

  priv = download->priv;

  scheme = g_uri_parse_scheme (destination);
  g_return_if_fail (scheme != NULL);
  g_free (scheme);

  priv->destination = g_strdup (destination);

#ifndef HAVE_WEBKIT2
  webkit_download_set_destination_uri (priv->download, priv->destination);
#endif
  g_object_notify (G_OBJECT (download), "destination");
}
Exemple #12
0
gboolean
download_cb(WebKitWebView *web_view, WebKitDownload *download, gpointer user_data) {
    (void) web_view; (void) user_data;

    /* get the URI being downloaded */
    const gchar *uri = webkit_download_get_uri(download);

    /* get the destination path, if specified.
     * this is only intended to be set when this function is trigger by an
     * explicit download using uzbl's 'download' action. */
    const gchar *destination = user_data;

    if (uzbl.state.verbose)
        printf("Download requested -> %s\n", uri);

    if (!uzbl.behave.download_handler) {
        webkit_download_cancel(download);
        return FALSE; /* reject downloads when there's no download handler */
    }

    /* get a reasonable suggestion for a filename */
    const gchar *suggested_filename;
#ifdef USE_WEBKIT2
    WebKitURIResponse *response;
    g_object_get(download, "network-response", &response, NULL);
#if WEBKIT_CHECK_VERSION (1, 9, 90)
    g_object_get(response, "suggested-filename", &suggested_filename, NULL);
#else
    suggested_filename = webkit_uri_response_get_suggested_filename(respose);
#endif
#elif WEBKIT_CHECK_VERSION (1, 9, 6)
    WebKitNetworkResponse *response;
    g_object_get(download, "network-response", &response, NULL);
    g_object_get(response, "suggested-filename", &suggested_filename, NULL);
#else
    g_object_get(download, "suggested-filename", &suggested_filename, NULL);
#endif

    /* get the mimetype of the download */
    const gchar *content_type = NULL;
    WebKitNetworkResponse *r  = webkit_download_get_network_response(download);
    /* downloads can be initiated from the context menu, in that case there is
       no network response yet and trying to get one would crash. */
    if(WEBKIT_IS_NETWORK_RESPONSE(r)) {
        SoupMessage        *m = webkit_network_response_get_message(r);
        SoupMessageHeaders *h = NULL;
        g_object_get(m, "response-headers", &h, NULL);
        if(h) /* some versions of libsoup don't have "response-headers" here */
            content_type = soup_message_headers_get_one(h, "Content-Type");
    }

    if(!content_type)
        content_type = "application/octet-stream";

    /* get the filesize of the download, as given by the server.
       (this may be inaccurate, there's nothing we can do about that.)  */
    unsigned int total_size = webkit_download_get_total_size(download);

    GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
    const CommandInfo *c = parse_command_parts(uzbl.behave.download_handler, a);
    if(!c) {
        webkit_download_cancel(download);
        g_array_free(a, TRUE);
        return FALSE;
    }

    g_array_append_val(a, uri);
    g_array_append_val(a, suggested_filename);
    g_array_append_val(a, content_type);
    gchar *total_size_s = g_strdup_printf("%d", total_size);
    g_array_append_val(a, total_size_s);

    if(destination)
        g_array_append_val(a, destination);

    GString *result = g_string_new ("");
    run_parsed_command(c, a, result);

    g_free(total_size_s);
    g_array_free(a, TRUE);

    /* no response, cancel the download */
    if(result->len == 0) {
        webkit_download_cancel(download);
        return FALSE;
    }

    /* we got a response, it's the path we should download the file to */
    gchar *destination_path = result->str;
    g_string_free(result, FALSE);

    /* presumably people don't need newlines in their filenames. */
    char *p = strchr(destination_path, '\n');
    if ( p != NULL ) *p = '\0';

    /* set up progress callbacks */
    g_signal_connect(download, "notify::status",   G_CALLBACK(download_status_cb),   NULL);
    g_signal_connect(download, "notify::progress", G_CALLBACK(download_progress_cb), NULL);

    /* convert relative path to absolute path */
    if(destination_path[0] != '/') {
        gchar *rel_path = destination_path;
        gchar *cwd = g_get_current_dir();
        destination_path = g_strconcat(cwd, "/", destination_path, NULL);
        g_free(cwd);
        g_free(rel_path);
    }

    send_event(DOWNLOAD_STARTED, NULL, TYPE_STR, destination_path, NULL);

    /* convert absolute path to file:// URI */
    gchar *destination_uri = g_strconcat("file://", destination_path, NULL);
    g_free(destination_path);

    webkit_download_set_destination_uri(download, destination_uri);
    g_free(destination_uri);

    return TRUE;
}