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