curlgtk.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (c) 2000 - 2022 David Odin (aka DindinX) for MandrakeSoft
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * use the libcurl in a gtk-threaded application
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <gtk/gtk.h>
  30. #include <curl/curl.h>
  31. GtkWidget *Bar;
  32. static size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
  33. {
  34. return fwrite(ptr, size, nmemb, stream);
  35. }
  36. static size_t my_read_func(char *ptr, size_t size, size_t nmemb, FILE *stream)
  37. {
  38. return fread(ptr, size, nmemb, stream);
  39. }
  40. static int my_progress_func(GtkWidget *bar,
  41. double t, /* dltotal */
  42. double d, /* dlnow */
  43. double ultotal,
  44. double ulnow)
  45. {
  46. /* printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/
  47. gdk_threads_enter();
  48. gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);
  49. gdk_threads_leave();
  50. return 0;
  51. }
  52. static void *my_thread(void *ptr)
  53. {
  54. CURL *curl;
  55. curl = curl_easy_init();
  56. if(curl) {
  57. gchar *url = ptr;
  58. const char *filename = "test.curl";
  59. FILE *outfile = fopen(filename, "wb");
  60. curl_easy_setopt(curl, CURLOPT_URL, url);
  61. curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
  62. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
  63. curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
  64. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  65. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
  66. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
  67. curl_easy_perform(curl);
  68. fclose(outfile);
  69. /* always cleanup */
  70. curl_easy_cleanup(curl);
  71. }
  72. return NULL;
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. GtkWidget *Window, *Frame, *Frame2;
  77. GtkAdjustment *adj;
  78. /* Must initialize libcurl before any threads are started */
  79. curl_global_init(CURL_GLOBAL_ALL);
  80. /* Init thread */
  81. g_thread_init(NULL);
  82. gtk_init(&argc, &argv);
  83. Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  84. Frame = gtk_frame_new(NULL);
  85. gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
  86. gtk_container_add(GTK_CONTAINER(Window), Frame);
  87. Frame2 = gtk_frame_new(NULL);
  88. gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
  89. gtk_container_add(GTK_CONTAINER(Frame), Frame2);
  90. gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
  91. adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
  92. Bar = gtk_progress_bar_new_with_adjustment(adj);
  93. gtk_container_add(GTK_CONTAINER(Frame2), Bar);
  94. gtk_widget_show_all(Window);
  95. if(!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
  96. g_warning("cannot create the thread");
  97. gdk_threads_enter();
  98. gtk_main();
  99. gdk_threads_leave();
  100. return 0;
  101. }