Skip to content

Commit

Permalink
first try at implementing WebKitDownloads
Browse files Browse the repository at this point in the history
created objects/ subdir to hold non-widget classes
wrapped WebKitDownload class
  • Loading branch information
karottenreibe committed Aug 14, 2010
1 parent d92ee6b commit e9b4642
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ GPERF = common/tokenize.gperf
GSRC = common/tokenize.c
GHEAD = common/tokenize.h

SRCS = $(filter-out ${GSRC},$(wildcard *.c) $(wildcard common/*.c) $(wildcard widgets/*.c)) ${GSRC}
HEADS = $(filter-out ${GHEAD},$(wildcard *.h) $(wildcard common/*.h) $(wildcard widgets/*.h)) ${GHEAD}
SRCS = $(filter-out ${GSRC},$(wildcard *.c) $(wildcard common/*.c) $(wildcard objects/*.c) $(wildcard widgets/*.c)) ${GSRC}
HEADS = $(filter-out ${GHEAD},$(wildcard *.h) $(wildcard common/*.h) $(wildcard objects/*.h) $(wildcard widgets/*.h)) ${GHEAD}
OBJS = $(foreach obj,$(SRCS:.c=.o),$(obj))

all: options newline luakit
Expand Down
7 changes: 7 additions & 0 deletions common/tokenize.gperf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ bg
clear_search
count
current
current_size
destination_uri
destroy
elapsed_time
entry
eval_js
eventbox
Expand Down Expand Up @@ -34,6 +37,7 @@ loading
notebook
pack_end
pack_start
progress
remove
search
selectable
Expand All @@ -52,10 +56,13 @@ show_frame
show_scrollbars
show_tabs
spacing
status
suggested_filename
switch
text
textbutton
title
total_size
type
uri
vbox
Expand Down
4 changes: 4 additions & 0 deletions luah.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "common/lualib.h"
#include "luakit.h"
#include "widget.h"
#include "objects/download.h"
#include "luah.h"

void
Expand Down Expand Up @@ -526,6 +527,9 @@ luaH_init(xdgHandle *xdg)
/* Export widget */
widget_class_setup(L);

/* Export download */
download_class_setup(L);

/* add Lua search paths */
lua_getglobal(L, "package");
if(LUA_TTABLE != lua_type(L, 1)) {
Expand Down
228 changes: 228 additions & 0 deletions objects/download.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* download.c - Wrapper for the WebKitDownload class
*
* Copyright © 2009 Julien Danjou <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/

#include <webkit/webkitdownload.h>
#include <webkit/webkitnetworkrequest.h>

#include "globalconf.h"
#include "luah.h"
#include "objects/download.h"
#include "common/luaobject.h"

typedef struct
{
LUA_OBJECT_HEADER
WebKitDownload* webkit_download;
} download_t;

static lua_class_t download_class;
LUA_OBJECT_FUNCS(download_class, download_t, download)

/* Wraps and pushes the given download onto the Lua stack.
* \param L The Lua VM state.
* \param download The WebKitDownload to push onto the stack.
*/
void
luaH_pushdownload(lua_State *L, WebKitDownload* download)
{
luaH_class_new(L, &download_class);
download_t *lua_download = luaH_checkudata(L, -1, &download_class);
lua_download->webkit_download = download;
}

static int
luaH_download_new(lua_State *L)
{
const char *uri = luaL_checkstring(L, 1);
luaH_class_new(L, &download_class);
download_t *download = luaH_checkudata(L, -1, &download_class);
WebKitNetworkRequest *request = webkit_network_request_new(uri);
download->webkit_download = webkit_download_new(request);
return 1;
}

static int
luaH_download_set_destination_uri(lua_State *L, download_t *download)
{
const char *destination_uri = luaL_checkstring(L, -1);
webkit_download_set_destination_uri(download->webkit_download, destination_uri);
luaH_object_emit_signal(L, -3, "property::destination_uri", 0, 0);
return 0;
}

static int
luaH_download_get_destination_uri(lua_State *L, download_t *download)
{
const char *destination_uri = webkit_download_get_destination_uri(download->webkit_download);
lua_pushstring(L, destination_uri);
return 1;
}

static int
luaH_download_get_progress(lua_State *L, download_t *download)
{
double progress = webkit_download_get_progress(download->webkit_download);
lua_pushnumber(L, progress);
return 1;
}

static int
luaH_download_get_status(lua_State *L, download_t *download)
{
WebKitDownloadStatus status = webkit_download_get_status(download->webkit_download);
switch (status) {
case WEBKIT_DOWNLOAD_STATUS_FINISHED:
lua_pushstring(L, "finished");
break;
case WEBKIT_DOWNLOAD_STATUS_CREATED:
lua_pushstring(L, "created");
break;
case WEBKIT_DOWNLOAD_STATUS_STARTED:
lua_pushstring(L, "started");
break;
case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
lua_pushstring(L, "cancelled");
break;
case WEBKIT_DOWNLOAD_STATUS_ERROR:
lua_pushstring(L, "error");
break;
default:
luaH_warn(L, "unknown download status");
return 0;
}
return 1;
}

static int
luaH_download_get_total_size(lua_State *L, download_t *download)
{
double total_size = webkit_download_get_total_size(download->webkit_download);
lua_pushnumber(L, total_size);
return 1;
}

static int
luaH_download_get_current_size(lua_State *L, download_t *download)
{
double current_size = webkit_download_get_current_size(download->webkit_download);
lua_pushnumber(L, current_size);
return 1;
}

static int
luaH_download_get_elapsed_time(lua_State *L, download_t *download)
{
double elapsed_time = webkit_download_get_elapsed_time(download->webkit_download);
lua_pushnumber(L, elapsed_time);
return 1;
}

static int
luaH_download_get_suggested_filename(lua_State *L, download_t *download)
{
const char *suggested_filename = webkit_download_get_suggested_filename(download->webkit_download);
lua_pushstring(L, suggested_filename);
return 1;
}

static int
luaH_download_get_uri(lua_State *L, download_t *download)
{
const char *uri = webkit_download_get_uri(download->webkit_download);
lua_pushstring(L, uri);
return 1;
}

static int
luaH_download_start(lua_State *L)
{
download_t *download = luaH_checkudata(L, 1, &download_class);
luaH_object_ref(L, 1); // TODO why? necessary?
webkit_download_start(download->webkit_download);
return 0;
}
static int
luaH_download_cancel(lua_State *L)
{
download_t *download = luaH_checkudata(L, 1, &download_class);
luaH_object_ref(L, 1); // TODO why? necessary?
webkit_download_cancel(download->webkit_download);
return 0;
}

void
download_class_setup(lua_State *L)
{
static const struct luaL_reg download_methods[] =
{
LUA_CLASS_METHODS(download)
{ "__call", luaH_download_new },
{ NULL, NULL }
};

static const struct luaL_reg download_meta[] =
{
LUA_OBJECT_META(download)
LUA_CLASS_META
{ "start", luaH_download_start },
{ "cancel", luaH_download_cancel },
{ NULL, NULL },
};

luaH_class_setup(L, &download_class, "download",
(lua_class_allocator_t) download_new,
luaH_class_index_miss_property, luaH_class_newindex_miss_property,
download_methods, download_meta);
luaH_class_add_property(&download_class, L_TK_DESTINATION_URI,
(lua_class_propfunc_t) luaH_download_set_destination_uri,
(lua_class_propfunc_t) luaH_download_get_destination_uri,
(lua_class_propfunc_t) luaH_download_set_destination_uri);
luaH_class_add_property(&download_class, L_TK_PROGRESS,
NULL,
(lua_class_propfunc_t) luaH_download_get_progress,
NULL);
luaH_class_add_property(&download_class, L_TK_STATUS,
NULL,
(lua_class_propfunc_t) luaH_download_get_status,
NULL);
luaH_class_add_property(&download_class, L_TK_TOTAL_SIZE,
NULL,
(lua_class_propfunc_t) luaH_download_get_total_size,
NULL);
luaH_class_add_property(&download_class, L_TK_CURRENT_SIZE,
NULL,
(lua_class_propfunc_t) luaH_download_get_current_size,
NULL);
luaH_class_add_property(&download_class, L_TK_ELAPSED_TIME,
NULL,
(lua_class_propfunc_t) luaH_download_get_elapsed_time,
NULL);
luaH_class_add_property(&download_class, L_TK_SUGGESTED_FILENAME,
NULL,
(lua_class_propfunc_t) luaH_download_get_suggested_filename,
NULL);
luaH_class_add_property(&download_class, L_TK_URI,
NULL,
(lua_class_propfunc_t) luaH_download_get_uri,
NULL);
}

// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
33 changes: 33 additions & 0 deletions objects/download.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* download.h - WebKitDownload wrapper header
*
* Copyright © 2009 Julien Danjou <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/

#ifndef AWESOME_OBJECTS_DOWNLOAD_H
#define AWESOME_OBJECTS_DOWNLOAD_H

#include <lua.h>
#include <webkit/webkitdownload.h>

void download_class_setup(lua_State *);
void luaH_pushdownload(lua_State *L, WebKitDownload* download);

#endif

// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
14 changes: 14 additions & 0 deletions widgets/webview.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "luah.h"
#include "widgets/common.h"
#include "objects/download.h"
#include <JavaScriptCore/JavaScript.h>
#include <webkit/webkit.h>
#include <libsoup/soup.h>
Expand Down Expand Up @@ -209,6 +210,18 @@ progress_cb(WebKitWebView *v, gint p, widget_t *w)
lua_pop(L, 1);
}

static void
download_requested_cb(WebKitWebView *v, GObject* download, widget_t *w)
{
(void) v;

lua_State *L = globalconf.L;
luaH_object_push(L, w->ref);
luaH_pushdownload(L, WEBKIT_DOWNLOAD(download));
luaH_object_emit_signal(L, -1, "download-requested", 1, 0);
lua_pop(L, 1);
}

static void
title_changed_cb(WebKitWebView *v, WebKitWebFrame *f, const gchar *title, widget_t *w)
{
Expand Down Expand Up @@ -795,6 +808,7 @@ widget_webview(widget_t *w)
"signal::navigation-policy-decision-requested", (GCallback)navigation_decision_cb, w,
"signal::parent-set", (GCallback)parent_set_cb, w,
"signal::title-changed", (GCallback)title_changed_cb, w,
"signal::download-requested", (GCallback)download_requested_cb, w,
NULL);

/* setup */
Expand Down

0 comments on commit e9b4642

Please sign in to comment.