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

C Webkit GTK::如何检测下载何时完成?,c,webkit,C,Webkit,我想知道是否有一种方法可以知道WebKit下载何时完成而没有错误。我正在使用C API。我希望会发出一个信号(类似于下载开始时),但情况似乎并非如此 搜索webkit API索引时,我没有看到可能相关的信号: 我真的必须在每个webkit下载完成之前对其进行轮询吗 干杯看起来你必须用手: 下载完成后会发出一个信号。信号是WebKitWebView“load finished”,您还可以监视对象WebKitWebView中“load status”的属性更改 以下C代码向您展示了如何: #i

我想知道是否有一种方法可以知道WebKit下载何时完成而没有错误。我正在使用C API。我希望会发出一个信号(类似于下载开始时),但情况似乎并非如此

搜索webkit API索引时,我没有看到可能相关的信号:

我真的必须在每个webkit下载完成之前对其进行轮询吗

干杯

看起来你必须用手:


下载完成后会发出一个信号。信号是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;
  }
#包括
#包括
静态void destroy_cb(GtkWidget*小部件,gpointer数据){
gtk_main_quit();
}
静态无效载荷\u完成\u cb(WebKitWebView*web\u视图、WebKitWebFrame*web\u框架、gpointer数据){
printf(“已完成下载%s\n”,webkit_web_view_get_uri(web_view));
}
静态无效荷载\状态\ cb(GObject*对象、GParamSpec*pspec、gpointer数据){
WebKitWebView*网络视图;
WebKitLoadStatus;
常量gchar*uri;
web\u视图=WEBKIT\u web\u视图(对象);
状态=webkit\u web\u view\u get\u load\u状态(web\u view);
uri=webkit\u web\u view\u get\u uri(web\u view);
开关(状态){
案例WEBKIT\u加载\u临时:
printf(“加载临时文件:%s\n”,uri);
打破
案例WEBKIT\u加载\u提交:
printf(“已提交的加载:%s\n”,uri);
打破
案例WEBKIT\u加载\u第一\u视觉\u非空\u布局:
printf(“加载第一个非空布局:%s\n”,uri);
打破
案例WEBKIT_加载_完成:
printf(“加载完成:%s\n”,uri);
打破
违约:
g_assert_not_reach();
}
}
int main(int argc,char*argv[]){
常量char*uri;
GtkWidget*窗口;
WebKitWebView*网络视图;
gtk_init(&argc,&argv);
如果(argc==1){
printf(“用法:URI\n”);
返回1;
}
uri=argv[1];
如果(!g_thread_supported())
g_thread_init(NULL);
窗口=gtk_窗口_新建(gtk_窗口_顶层);
gtk_窗口设置默认大小(gtk_窗口(窗口),600,400);
g_信号连接(窗口,“销毁”,g_回调(销毁cb),空);
web_view=web_view=WEBKIT_web_view(WEBKIT_web_view_new());
webkit\u web\u view\u set\u transparent(web\u view,TRUE);
/*注册一个回调,该回调在每次页面下载完成时调用*/
g_信号连接(web_视图,“加载完成”,g_回调(加载完成,cb),空);
/*注册每次加载状态更改时调用的回调*/
g_对象_连接(web_视图,“信号::通知::加载状态”,g_回调(加载状态_cb),NULL);
webkit_web_view_load_uri(web_view,uri);
gtk_容器添加(gtk_容器(窗口)、gtk_小部件(web_视图));
gtk_小部件抓取焦点(gtk_小部件(web_视图));
gtk_小部件_全部显示(窗口);
gtk_main();
返回0;
}

您可以使用“下载请求”
WebView
信号获取
WebKitDownload
。然后使用“通知”
WebKitDownload
信号,方法与
WebView
的加载状态更改相同

我在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;
}
#包括
#包括
静态void destroy_cb(GtkWidget*小部件,gpointer数据){
gtk_main_quit();
}
静态无效下载\u状态\u cb(GObject*对象,GParamSpec*pspec,gpointer数据){//WebKitDownload*下载,GObject*对象,GParamSpec*pspec,gpointer数据){
WebKitDownload*下载;
WebKitDownloadStatus状态;
concst gchar*uri;
下载=WEBKIT_下载(对象);
状态=webkit\u下载\u获取\u状态(下载);
uri=webkit\u下载\u获取\u uri(下载):
开关(状态){
案例WEBKIT\下载\状态\错误:
printf(“下载错误:%s\n”,uri);
打破
案例WEBKIT_下载_状态_创建:
printf(“已创建下载:%s\n”,uri);
打破
案例WEBKIT\u下载\u状态\u已启动:
printf(“已开始下载:%s\n”,uri);
打破
案例WEBKIT\u下载\u状态\u取消:
printf(“已取消下载:%s\n”,uri);
打破
案例WEBKIT\u下载\u状态\u完成:
printf(“下载完成!:%s\n”,uri);
打破
违约:
g_assert_not_reach();
}
}
静态无效下载\u请求\u cb(WebKitWebView*web\u视图,WebKitDownload*下载){
常量字符*文件名;
//请注意,如果建议的文件名包含空格,则会导致下面的问题
filename=webkit_下载_获取_建议的_文件名();
//设置目标uri(例如,将文件发送到下载文件夹)
webkit\u下载\u设置\u目的地\u uri(下载,“file:///some/place/for/your/Downloads/test.mp3");
//连接状态回调
g_对象_连接(下载,“信号::通知::状态”,g_回调(下载_状态_cb),NULL);
return true;//如果返回false,下载将被取消
}
int main(int argc,char*argv[]){
常量char*uri;
GtkWidget*窗口;
WebKitWebView*网络视图;
gtk_init(&argc,&argv);
如果(argc==1){
printf(“用法:URI\n”);
返回1;
}
uri=argv[1];
如果(!g_thread_supported())
g_thread_init(NULL);
窗口=gtk_窗口_新建(gtk_窗口_顶层);
gtk_窗口设置默认大小(gtk_窗口(窗口),600,400);
g_信号连接(窗口,“销毁”,g_回调(销毁cb),空);
web_view=web_view=WEBKIT_web_view(WEBKIT_web_view_new());
webkit\u web\u view\u set\u transparent(web\u view,TRUE);
/*注册一个回调,该回调在每次页面下载完成时调用*/
g_信号连接(web_视图,“加载完成”,g_回调(加载完成,cb),空);
/*注册一个获取invo的回调
#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;
}