Webkit GTK:如何检测下载何时完成?

4
我想知道在使用C API时,是否有一种方法可以知道webkitdownload何时完成且没有错误。我希望会发出一个信号(类似于下载开始时),但是似乎并不是这种情况。在搜索webkit API索引时,我看不到任何可能相关的信号: http://webkitgtk.org/reference/index-all.html 我真的必须轮询每个webkit下载直到它完成吗? 干杯
3个回答

7

当下载完成时,会发出一个信号。该信号是 WebKitWebView 的 "load-finished",您还可以监视 WebKitWebView 对象中 "load-status" 的属性更改。

以下 C 代码向您展示了如何:

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

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

  static void load_finished_cb(WebKitWebView *web_view, WebKitWebFrame *web_frame, gpointer data) {
      printf("Finished downloading %s\n", webkit_web_view_get_uri(web_view));
  }

  static void load_status_cb(GObject* object, GParamSpec* pspec, gpointer data) {
      WebKitWebView *web_view;
      WebKitLoadStatus status;
      const gchar *uri;

      web_view = WEBKIT_WEB_VIEW(object);
      status = webkit_web_view_get_load_status(web_view);
      uri = webkit_web_view_get_uri(web_view);

      switch (status) {
      case WEBKIT_LOAD_PROVISIONAL:
          printf("Load provisional: %s\n", uri);
          break;
      case WEBKIT_LOAD_COMMITTED:
          printf("Load commited: %s\n", uri);
          break;
      case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
          printf("Load first visually non empty layout: %s\n", uri);
          break;
      case WEBKIT_LOAD_FINISHED:
          printf("Load finished: %s\n", uri);
          break;
      default:
          g_assert_not_reached();
      }
  }

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

    gtk_init(&argc, &argv);

    if (argc == 1) {
        printf("Usage: URI\n");
        return 1;
    }
    uri = argv[1];

    if(!g_thread_supported())
      g_thread_init(NULL);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
    g_signal_connect(window, "destroy", G_CALLBACK(destroy_cb), NULL);

    web_view = web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
    webkit_web_view_set_transparent(web_view, TRUE);

    /* Register a callback that gets invoked each time that a page is finished downloading */
    g_signal_connect(web_view, "load-finished", G_CALLBACK(load_finished_cb), NULL);

    /* Register a callback that gets invoked each time that the load status changes */
    g_object_connect(web_view, "signal::notify::load-status", G_CALLBACK(load_status_cb), NULL);

    webkit_web_view_load_uri(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;
  }

"load-finished已经过时,因此load-status可能是更好的选择。" - Edd Barrett
我唯一剩下的问题是,该信号是用于页面加载和下载的,对吗?那么我该如何区分它们? - Edd Barrett
在我的例子中,你可以看到你能够获取正在下载的URI。我想通过这个URI,你可以把它们区分开来。 - Emmanuel Rodriguez

3
您可以使用“download-requested”WebView信号获取WebKitDownload 。然后使用“notify”WebKitDownload信号,就像处理WebView的加载状态更改一样。
我在Python中遇到了这个问题,因此我将从Python开始,以防下面有误翻译。
def on_download_requested(webview, download):
    #set the destination uri (downloads folder, etc)
    ...
    #connect the status change handler
    download.connect('notify::status', on_download_status_change)
    return True

def on_download_status_change(download, status):
    #get the status from download, since the provided status is a little weird
    #(i think this is specific to the binding)
    if download.get_status().value_name == 'WEBKIT_DOWNLOAD_STATUS_ERROR':
        #error handling
        ...
    elif download.get_status().value_name == 'WEBKIT_DOWNLOAD_STATUS_FINISHED':
        #the download is finished!
        ...
    else:
        ...

或者,修改@potyl的答案以适用于C版本...这似乎非常简单。
#include <gtk/gtk.h>
#include <webkit/webkit.h>


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

static void download_status_cb(GObject* object, GParamSpec* pspec, gpointer data){ //WebKitDownload *download, GObject* object, GParamSpec* pspec, gpointer data) {
    WebKitDownload *download;
    WebKitDownloadStatus status;
    concst gchar *uri;

    download = WEBKIT_DOWNLOAD(object);
    status = webkit_download_get_status(download);
    uri = webkit_download_get_uri(download):

    switch (status) {
      case WEBKIT_DOWNLOAD_STATUS_ERROR:
          printf("download error: %s\n", uri);
          break;
      case WEBKIT_DOWNLOAD_STATUS_CREATED:
          printf("download created: %s\n", uri);
          break;
      case WEBKIT_DOWNLOAD_STATUS_STARTED:
          printf("download started: %s\n", uri);
          break;
      case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
          printf("download cancelled: %s\n", uri);
          break;
      case WEBKIT_DOWNLOAD_STATUS_FINISHED:
          printf("download finished!: %s\n", uri);
          break;
      default:
          g_assert_not_reached();
    }
}

static void download_requested_cb(WebKitWebView *web_view, WebKitDownload *download) {
  const char *filename;
  //note that if the suggested filename has spaces, it'll cause trouble below
  filename = webkit_download_get_suggested_filename();
  //set the destination uri (eg, send the file to a downloads folder)
  webkit_download_set_destination_uri(download,"file:///some/place/for/your/Downloads/test.mp3");
  //connect the status callback
  g_object_connect(download, "signal::notify::status", G_CALLBACK(download_status_cb), NULL);
  return true; //if we return false, the download will be cancelled
}

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

  gtk_init(&argc, &argv);

  if (argc == 1) {
      printf("Usage: URI\n");
      return 1;
  }
  uri = argv[1];

  if(!g_thread_supported())
    g_thread_init(NULL);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
  g_signal_connect(window, "destroy", G_CALLBACK(destroy_cb), NULL);

  web_view = web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
  webkit_web_view_set_transparent(web_view, TRUE);

  /* Register a callback that gets invoked each time that a page is finished downloading */
  g_signal_connect(web_view, "load-finished", G_CALLBACK(load_finished_cb), NULL);

  /* 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);

  webkit_web_view_load_uri(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;
}

希望这个丑陋、未经测试的C语言翻译有所帮助 :)

我会尝试一下这个,谢谢。 - Edd Barrett
@vext01 你有没有试过这个? - Matt Luongo

0

看起来你必须手动轮询WebKitDownload

WebKitDownloadStatus s = webkit_download_get_status(downloader);
if(s == WEBKIT_DOWNLOAD_STATUS_FINISHED) {
    /* The download has successfully finished. */
}
else if(s == WEBKIT_DOWNLOAD_STATUS_STARTED) {
    /* Still going, 100*webkit_download_get_progress(downloader) percent complete. */
}

这正是我担心不得不做的事情。这意味着使用alarm()来定期轮询下载... 这不是我正在寻找的解决方案。 - Edd Barrett
你不能挂钩你的主事件循环,而不是完全手动轮询吗?或者有一个单独的“下载管理器”线程?目的可能是要有一个独立的下载管理器,通过轮询来管理一组WebKitDownload实例,以避免在GUI中使用单个下载状态窗口时出现锁定问题。 - mu is too short

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接