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