Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c - Webkit GTK :: How to detect when a download has finished?

I was wondering if there is a way to know when a webkitdownload has finished without error. I am using the C API. I was hoping a signal would be emitted (similar to when a download starts), however this seems to be not the case.

Searching the webkit API index, i see no signal which could be relevent: http://webkitgtk.org/reference/index-all.html

Must I really poll each webkit download until it has finished?

Cheers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a signal that gets emitted when a download is complete. The signal is WebKitWebView "load-finished", you can also monitor the property changes of "load-status" still in the object WebKitWebView.

The following C code shows you how to:

  #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
", 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
", uri);
          break;
      case WEBKIT_LOAD_COMMITTED:
          printf("Load commited: %s
", uri);
          break;
      case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
          printf("Load first visually non empty layout: %s
", uri);
          break;
      case WEBKIT_LOAD_FINISHED:
          printf("Load finished: %s
", 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
");
        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;
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...