curlgtk.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (c) 2000 David Odin (aka DindinX) for MandrakeSoft
  9. */
  10. /* <DESC>
  11. * use the libcurl in a gtk-threaded application
  12. * </DESC>
  13. */
  14. #include <stdio.h>
  15. #include <gtk/gtk.h>
  16. #include <curl/curl.h>
  17. GtkWidget *Bar;
  18. size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
  19. {
  20. return fwrite(ptr, size, nmemb, stream);
  21. }
  22. size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
  23. {
  24. return fread(ptr, size, nmemb, stream);
  25. }
  26. int my_progress_func(GtkWidget *bar,
  27. double t, /* dltotal */
  28. double d, /* dlnow */
  29. double ultotal,
  30. double ulnow)
  31. {
  32. /* printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/
  33. gdk_threads_enter();
  34. gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);
  35. gdk_threads_leave();
  36. return 0;
  37. }
  38. void *my_thread(void *ptr)
  39. {
  40. CURL *curl;
  41. CURLcode res;
  42. FILE *outfile;
  43. gchar *url = ptr;
  44. curl = curl_easy_init();
  45. if(curl) {
  46. const char *filename = "test.curl";
  47. outfile = fopen(filename, "wb");
  48. curl_easy_setopt(curl, CURLOPT_URL, url);
  49. curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
  50. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
  51. curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
  52. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  53. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
  54. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
  55. res = curl_easy_perform(curl);
  56. fclose(outfile);
  57. /* always cleanup */
  58. curl_easy_cleanup(curl);
  59. }
  60. return NULL;
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. GtkWidget *Window, *Frame, *Frame2;
  65. GtkAdjustment *adj;
  66. /* Must initialize libcurl before any threads are started */
  67. curl_global_init(CURL_GLOBAL_ALL);
  68. /* Init thread */
  69. g_thread_init(NULL);
  70. gtk_init(&argc, &argv);
  71. Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  72. Frame = gtk_frame_new(NULL);
  73. gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
  74. gtk_container_add(GTK_CONTAINER(Window), Frame);
  75. Frame2 = gtk_frame_new(NULL);
  76. gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
  77. gtk_container_add(GTK_CONTAINER(Frame), Frame2);
  78. gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
  79. adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
  80. Bar = gtk_progress_bar_new_with_adjustment(adj);
  81. gtk_container_add(GTK_CONTAINER(Frame2), Bar);
  82. gtk_widget_show_all(Window);
  83. if(!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
  84. g_warning("can't create the thread");
  85. gdk_threads_enter();
  86. gtk_main();
  87. gdk_threads_leave();
  88. return 0;
  89. }