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

后端 未结 3 1385
情书的邮戳
情书的邮戳 2020-12-19 22:24

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 d

3条回答
  •  隐瞒了意图╮
    2020-12-19 22:32

    You can use the 'download-requested' WebView signal to get a WebKitDownload. Then use a 'notify' WebKitDownload signal the same way you do with your WebView 's load status change.

    I ran into this in Python, so I'll start with that in case I mistranslated below.

    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:
            ...
    

    Or, modifying @potyl's answer for a C version... it seems pretty straightforward.

    #include 
    #include 
    
    
    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;
    }
    

    Hope the ugly, untested C translation helps :)

提交回复
热议问题