commit 27b02f989ad73169687b56bd7199339d2ec080fc
parent 4b056240d06a7e13cabd523c69b7bca12288f486
Author: Kyle Milz <kmilz@ucalgary.ca>
Date: Thu, 20 Sep 2012 20:03:18 -0600
downloads: use hash table to track downloads.
use eina_hash_* to make a hash table of all the current downloads and
their progress.
Diffstat:
M | main.c | | | 83 | ++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- |
M | main.h | | | 7 | ++++++- |
2 files changed, 61 insertions(+), 29 deletions(-)
diff --git a/main.c b/main.c
@@ -71,9 +71,10 @@ static void update_url(const char *, void *);
// static char *jsapi_ref_to_string(JSContextRef context, JSValueRef ref);
// static void jsapi_evaluate_script(const gchar *script, gchar **value, gchar **message, void *data);
static void set_widget_font_and_color(Evas_Object *widget, const char *font_str, const char *bg_color_str, const char *fg_color_str);
+void download_complete_cb(void*, const char*, int);
+int download_progress_cb(void*, const char*, long int, long int, long int, long int);
-
static void
ascii_bar(int total, int state, char *string)
{
@@ -94,8 +95,7 @@ update_state(void *data)
App_Data *ad = data;
Evas_Object *view = elm_web_webkit_view_get(ad->current_web);
Evas_Object *frame = ewk_view_frame_main_get(view);
- // int download_count = g_list_length(activeDownloads);
- int download_count = eina_list_count(ad->active_downloads);
+ int download_count = eina_hash_population(ad->downloads);
// GString *status = g_string_new("");
char status[512] = "";
@@ -112,11 +112,9 @@ update_state(void *data)
// if (ad->current_modkey) g_string_append_c(status, ad->current_modkey);
if (ad->current_modkey) strcat(status, &ad->current_modkey);
- if (ad->active_downloads) {
- // g_string_append_printf(status, " %d active %s", download_count,
- // (download_count == 1) ? "download" : "downloads");
- char *download_status = strdup_printf(" %d active %s", download_count,
- (download_count == 1) ? "download" : "downloads");
+ if (download_count) {
+ char *download_status = strdup_printf(" %d DL%s", download_count,
+ (download_count == 1) ? "" : "s");
strcat(status, download_status);
free(download_status);
}
@@ -125,31 +123,29 @@ update_state(void *data)
int progress = -1;
char progressbar[progressbartick + 1];
- /*
- if (activeDownloads) {
+ if (eina_hash_population(ad->downloads)) {
progress = 0;
- Eina_List *ptr;
- for (ptr = activeDownloads; ptr; ptr = eina_list_next(ptr)) {
- progress += 100 * webkit_download_get_progress(ptr->data);
- }
+ Eina_Iterator *it = eina_hash_iterator_tuple_new(ad->downloads);
+ void *hash_data;
+ struct dl *dload;
- progress /= download_count;
+ while (eina_iterator_next(it, &hash_data)) {
+ Eina_Hash_Tuple *t = hash_data;
+ dload = t->data;
- } else if (webkit_web_view_get_load_status(webview) != WEBKIT_LOAD_FINISHED
- && webkit_web_view_get_load_status(webview) != WEBKIT_LOAD_FAILED) {
+ if (dload->dltotal != 0)
+ progress += 100 * (int) (dload->dlnow / dload->dltotal);
+ }
- // progress = webkit_web_view_get_progress(webview) * 100;
- }
- */
+ progress /= eina_hash_population(ad->downloads);
- if (ewk_view_load_progress_get(view) != 0.0f)
+ } else if (ewk_view_load_progress_get(view) != 0.0f) {
progress = ewk_view_load_progress_get(view) * 100;
+ }
if (progress >= 0) {
ascii_bar(progressbartick, progress * progressbartick / 100, progressbar);
- // g_string_append_printf(status, " %c%s%c",
- // progressborderleft, progressbar, progressborderright);
char *load_progress = strdup_printf(" %c%s%c",
progressborderleft, progressbar, progressborderright);
strcat(status, load_progress);
@@ -566,6 +562,11 @@ inspector_inspect_web_view_cb(gpointer inspector, WebKitWebView* web_view) {
}
*/
+static void
+download_entry_free_cb(void *data)
+{
+ free(data);
+}
void
download_complete_cb(void *data, const char *file, int status)
@@ -577,13 +578,26 @@ download_complete_cb(void *data, const char *file, int status)
echo(&a, data);
free(a.s);
- ad->active_downloads = eina_list_remove(ad->active_downloads, file);
+ // ad->active_downloads = eina_list_remove(ad->active_downloads, file);
+ eina_hash_del(ad->downloads, file, NULL);
+
+ update_state(data);
}
int
download_progress_cb(void *data, const char *file, long int dltotal, long int dlnow, long int ultotal, long int ulnow)
{
- printf("download_progress() file %s done %f\n", file, (float)dlnow/dltotal);
+ App_Data *ad = data;
+
+ void *stat_old;
+ struct dl *stat = malloc(sizeof(struct dl));
+
+ stat->dltotal = dltotal;
+ stat->dlnow = dlnow;
+
+ stat_old = eina_hash_modify(ad->downloads, file, (const void *)stat);
+ free(stat_old);
+
update_state(data);
return 0;
@@ -592,6 +606,7 @@ download_progress_cb(void *data, const char *file, long int dltotal, long int dl
static void
webview_download_cb(void *data, Evas_Object *obj, void *event_info)
{
+ Eina_Bool ret;
Tab_Data *td = data;
App_Data *ad = td->app;
@@ -600,12 +615,23 @@ webview_download_cb(void *data, Evas_Object *obj, void *event_info)
char *full_path = strdup_printf("/home/kyle/%s", suggested_name);
- ecore_file_download(dl->url, full_path, download_complete_cb,
+ ret = ecore_file_download(dl->url, full_path, download_complete_cb,
download_progress_cb, ad, NULL);
- ad->active_downloads = eina_list_append(ad->active_downloads, strdup(suggested_name));
+ /* if file already exists, ret == NULL */
+ if (!ret) {
+ Arg a;
+ a.i = Info;
+ a.s = strdup_printf("%s already exists!", suggested_name);
+ echo(&a, ad);
+ free(a.s);
+ return;
+ }
+
+ // ad->active_downloads = eina_list_append(ad->active_downloads, strdup(full_path));
+ eina_hash_add(ad->downloads, full_path, calloc(1, sizeof(struct dl)));
- free(full_path);
+ // free(full_path);
free(suggested_name);
update_state(ad);
@@ -1518,6 +1544,7 @@ elm_main(int argc, char *argv[])
ad->count = 0;
ad->echo_active = EINA_TRUE;
+ ad->downloads = eina_hash_string_superfast_new(download_entry_free_cb);
toggle_proxy(use_proxy);
diff --git a/main.h b/main.h
@@ -49,7 +49,7 @@ typedef struct
Eina_Bool manual_focus;
Eina_List *buffer_list;
- Eina_List *active_downloads;
+ Eina_Hash *downloads;
Eina_List *commandhistory;
int commandpointer;
@@ -70,5 +70,10 @@ struct _Tab_Data
Evas_Object *web_inspector;
};
+struct dl {
+ long int dltotal;
+ long int dlnow;
+};
+
#endif