/** * Returns the status of the given download. * * The status will be one of the following: * - \c finished * - \c created * - \c started * - \c cancelled * - \c error * * Returns nothing if an error occurs. * * \param L The Lua VM state. * \param download The \ref download_t of the download. * * \luastack * \lparam A \c download object. * \lreturn The status of the download as a string or \c nil. */ static gint 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: luaH_download_unref(L, download); 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: luaH_download_unref(L, download); lua_pushstring(L, "cancelled"); break; case WEBKIT_DOWNLOAD_STATUS_ERROR: luaH_download_unref(L, download); lua_pushstring(L, "error"); break; default: luaH_warn(L, "unknown download status"); return 0; } return 1; }
/** * Returns the inferred MIME type of the given download. * * \param L The Lua VM state. * \param download The \ref download_t of the download. * * \luastack * \lparam A \c download object. * \lreturn The inferred MIME type of the download as a string. */ static gint luaH_download_get_mime_type(lua_State *L, download_t *download) { GError *error = NULL; const gchar *destination = webkit_download_get_destination_uri( download->webkit_download); GFile *file = g_file_new_for_uri(destination); GFileInfo *info = g_file_query_info(file, "standard::*", 0, NULL, &error); if (error) { if (download->error) g_free(download->error); download->error = g_strdup(error->message); luaH_warn(L, "%s", download->error); return 0; } const gchar *content_type = g_file_info_get_content_type(info); const gchar *mime_type = g_content_type_get_mime_type(content_type); g_object_unref(file); if (mime_type) { lua_pushstring(L, mime_type); return 1; } return 0; }
static int luaH_timer_stop(lua_State *L) { ltimer_t *timer = luaH_checktimer(L, 1); if (timer->id == TIMER_STOPPED) luaH_warn(L, "timer already stopped"); else luaH_timer_destroy(L, timer); return 0; }
/** * Aborts the download. * * Will produce a warning if the download is not running. * Unreferences the download to allow its garbage collection. * * \param L The Lua VM state. * * \luastack * \lparam A \c download object to abort. */ static gint luaH_download_cancel(lua_State *L) { download_t *download = luaH_checkdownload(L, 1); if (download_is_started(download)) { webkit_download_cancel(download->webkit_download); luaH_download_unref(L, download); } else luaH_warn(L, "download not started"); return 0; }
static int luaH_timer_stop(lua_State *L) { ltimer_t *timer = luaH_checkudata(L, 1, &timer_class); if (timer->timer_id == TIMER_STOPPED) { luaH_warn(L, "timer already stopped. Cannot stop a timer twice"); } else { luaH_timer_destroy(L, timer); } return 0; }
static int luaH_timer_start(lua_State *L) { ltimer_t *timer = luaH_checktimer(L, 1); if (!timer->interval) luaL_error(L, "interval not set"); if (timer->id == TIMER_STOPPED) { /* ensure timer isn't collected while running */ timer->ref = luaH_object_ref(L, 1); timer->id = g_timeout_add(timer->interval, timer_handle_timeout, timer); } else luaH_warn(L, "timer already started"); return 0; }
static int luaH_timer_start(lua_State *L) { ltimer_t *timer = luaH_checkudata(L, 1, &timer_class); // get interval lua_getfield(L, -1, TIMER_INTERVAL_FIELD); int millis = luaL_checkinteger(L, -1); timer->ref = luaH_object_ref(L, 1); // ensure that timers don't get collected while running if (timer->timer_id == TIMER_STOPPED) { timer->timer_id = g_timeout_add(millis, timer_handle_timeout, timer); } else { luaH_warn(L, "timer already started. Cannot start a timer twice"); } return 0; }
/** * Checks prerequesites for downloading. * - clears the last error message of the download. * - checks that a destination has been set. * * \param L The Lua VM state. * \param download The \ref download_t of the download. * * \returns \c TRUE if the download is ready to begin. */ static gboolean download_check_prerequesites(lua_State *L, download_t *download) { /* clear last download error message */ if (download->error) { g_free(download->error); download->error = NULL; } /* get download destination */ const gchar *destination = webkit_download_get_destination_uri( download->webkit_download); if (!destination) { download->error = g_strdup("Download destination not set"); luaH_warn(L, "%s", download->error); return FALSE; } /* ready to go */ return TRUE; }
/** * Starts the download. * * Will produce a warning if the download is already running. * References the download to prevent its garbage collection. * Will raise a Lua error if the start failed. * * \param L The Lua VM state. * * \luastack * \lparam A \c download object to start. */ static gint luaH_download_start(lua_State *L) { download_t *download = luaH_checkdownload(L, 1); if (!download_is_started(download)) { /* prevent lua garbage collection while downloading */ lua_pushvalue(L, 1); download->ref = luaH_object_ref(L, -1); /* check if we can download to destination */ if (download_check_prerequesites(L, download)) webkit_download_start(download->webkit_download); /* check for webkit/glib errors from download start */ if (download->error) { lua_pushstring(L, download->error); lua_error(L); } } else luaH_warn(L, "download already stared"); return 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; }