xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 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 | |
| 29 | #include <stdio.h> |
| 30 | #include <gtk/gtk.h> |
| 31 | |
| 32 | #include <curl/curl.h> |
| 33 | |
| 34 | GtkWidget *Bar; |
| 35 | |
| 36 | static size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 37 | { |
| 38 | return fwrite(ptr, size, nmemb, stream); |
| 39 | } |
| 40 | |
| 41 | static size_t my_read_func(char *ptr, size_t size, size_t nmemb, FILE *stream) |
| 42 | { |
| 43 | return fread(ptr, size, nmemb, stream); |
| 44 | } |
| 45 | |
| 46 | static int my_progress_func(GtkWidget *bar, |
| 47 | double t, /* dltotal */ |
| 48 | double d, /* dlnow */ |
| 49 | double ultotal, |
| 50 | double ulnow) |
| 51 | { |
| 52 | /* printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/ |
| 53 | gdk_threads_enter(); |
| 54 | gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t); |
| 55 | gdk_threads_leave(); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static void *my_thread(void *ptr) |
| 60 | { |
| 61 | CURL *curl; |
| 62 | |
| 63 | curl = curl_easy_init(); |
| 64 | if(curl) { |
| 65 | gchar *url = ptr; |
| 66 | const char *filename = "test.curl"; |
| 67 | FILE *outfile = fopen(filename, "wb"); |
| 68 | |
| 69 | curl_easy_setopt(curl, CURLOPT_URL, url); |
| 70 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); |
| 71 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func); |
| 72 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func); |
| 73 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); |
| 74 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func); |
| 75 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar); |
| 76 | |
| 77 | curl_easy_perform(curl); |
| 78 | |
| 79 | fclose(outfile); |
| 80 | /* always cleanup */ |
| 81 | curl_easy_cleanup(curl); |
| 82 | } |
| 83 | |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | int main(int argc, char **argv) |
| 88 | { |
| 89 | GtkWidget *Window, *Frame, *Frame2; |
| 90 | GtkAdjustment *adj; |
| 91 | |
| 92 | /* Must initialize libcurl before any threads are started */ |
| 93 | curl_global_init(CURL_GLOBAL_ALL); |
| 94 | |
| 95 | /* Init thread */ |
| 96 | g_thread_init(NULL); |
| 97 | |
| 98 | gtk_init(&argc, &argv); |
| 99 | Window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 100 | Frame = gtk_frame_new(NULL); |
| 101 | gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT); |
| 102 | gtk_container_add(GTK_CONTAINER(Window), Frame); |
| 103 | Frame2 = gtk_frame_new(NULL); |
| 104 | gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN); |
| 105 | gtk_container_add(GTK_CONTAINER(Frame), Frame2); |
| 106 | gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5); |
| 107 | adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0); |
| 108 | Bar = gtk_progress_bar_new_with_adjustment(adj); |
| 109 | gtk_container_add(GTK_CONTAINER(Frame2), Bar); |
| 110 | gtk_widget_show_all(Window); |
| 111 | |
| 112 | if(!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0) |
| 113 | g_warning("cannot create the thread"); |
| 114 | |
| 115 | gdk_threads_enter(); |
| 116 | gtk_main(); |
| 117 | gdk_threads_leave(); |
| 118 | return 0; |
| 119 | } |