Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / curlgtk.c
blob2fdc081175c9c177bd508d98de2ead498e5d4b57
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: curlgtk.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
9 */
10 /* Copyright (c) 2000 David Odin (aka DindinX) for MandrakeSoft */
11 /* an attempt to use the curl library in concert with a gtk-threaded application */
13 #include <stdio.h>
14 #include <gtk/gtk.h>
16 #include <curl/curl.h>
17 #include <curl/types.h> /* new for v7 */
18 #include <curl/easy.h> /* new for v7 */
20 GtkWidget *Bar;
22 size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
24 return fwrite(ptr, size, nmemb, stream);
27 size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
29 return fread(ptr, size, nmemb, stream);
32 int my_progress_func(GtkWidget *bar,
33 double t, /* dltotal */
34 double d, /* dlnow */
35 double ultotal,
36 double ulnow)
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;
45 void *my_thread(void *ptr)
47 CURL *curl;
48 CURLcode res;
49 FILE *outfile;
50 gchar *url = ptr;
52 curl = curl_easy_init();
53 if(curl)
55 outfile = fopen("test.curl", "w");
57 curl_easy_setopt(curl, CURLOPT_URL, url);
58 curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
59 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
60 curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
61 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
62 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
63 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
65 res = curl_easy_perform(curl);
67 fclose(outfile);
68 /* always cleanup */
69 curl_easy_cleanup(curl);
72 return NULL;
75 int main(int argc, char **argv)
77 GtkWidget *Window, *Frame, *Frame2;
78 GtkAdjustment *adj;
80 /* Must initialize libcurl before any threads are started */
81 curl_global_init(CURL_GLOBAL_ALL);
83 /* Init thread */
84 g_thread_init(NULL);
86 gtk_init(&argc, &argv);
87 Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
88 Frame = gtk_frame_new(NULL);
89 gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
90 gtk_container_add(GTK_CONTAINER(Window), Frame);
91 Frame2 = gtk_frame_new(NULL);
92 gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
93 gtk_container_add(GTK_CONTAINER(Frame), Frame2);
94 gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
95 adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
96 Bar = gtk_progress_bar_new_with_adjustment(adj);
97 gtk_container_add(GTK_CONTAINER(Frame2), Bar);
98 gtk_widget_show_all(Window);
100 if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
101 g_warning("can't create the thread");
104 gdk_threads_enter();
105 gtk_main();
106 gdk_threads_leave();
107 return 0;