2
0

curlgtk.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (c) 2000 - 2019 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. curl = curl_easy_init();
  42. if(curl) {
  43. gchar *url = ptr;
  44. const char *filename = "test.curl";
  45. FILE *outfile = fopen(filename, "wb");
  46. curl_easy_setopt(curl, CURLOPT_URL, url);
  47. curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
  48. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
  49. curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
  50. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  51. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
  52. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
  53. curl_easy_perform(curl);
  54. fclose(outfile);
  55. /* always cleanup */
  56. curl_easy_cleanup(curl);
  57. }
  58. return NULL;
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. GtkWidget *Window, *Frame, *Frame2;
  63. GtkAdjustment *adj;
  64. /* Must initialize libcurl before any threads are started */
  65. curl_global_init(CURL_GLOBAL_ALL);
  66. /* Init thread */
  67. g_thread_init(NULL);
  68. gtk_init(&argc, &argv);
  69. Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  70. Frame = gtk_frame_new(NULL);
  71. gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
  72. gtk_container_add(GTK_CONTAINER(Window), Frame);
  73. Frame2 = gtk_frame_new(NULL);
  74. gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
  75. gtk_container_add(GTK_CONTAINER(Frame), Frame2);
  76. gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
  77. adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
  78. Bar = gtk_progress_bar_new_with_adjustment(adj);
  79. gtk_container_add(GTK_CONTAINER(Frame2), Bar);
  80. gtk_widget_show_all(Window);
  81. if(!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
  82. g_warning("can't create the thread");
  83. gdk_threads_enter();
  84. gtk_main();
  85. gdk_threads_leave();
  86. return 0;
  87. }