ENH: typo
[cmake.git] / Utilities / cmcurl / Testing / curlgtk.c
blob611ac60ccc3ac9918d153a2717155957285b505a
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: curlgtk.c,v 1.1 2005-06-24 13:02:16 andy 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 #include <pthread.h>
22 GtkWidget *Bar;
24 size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
26 return fread(ptr, size, nmemb, stream);
29 int my_progress_func(GtkWidget *Bar, int t, int d)
31 /* printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/
32 gdk_threads_enter();
33 gtk_progress_set_value(GTK_PROGRESS(Bar), d*100.0/t);
34 gdk_threads_leave();
35 return 0;
38 void *curl_thread(void *ptr)
40 CURL *curl;
41 CURLcode res;
42 FILE *outfile;
43 gchar *url = ptr;
45 curl = curl_easy_init();
46 if(curl)
48 outfile = fopen("/tmp/test.curl", "w");
50 curl_easy_setopt(curl, CURLOPT_URL, url);
51 curl_easy_setopt(curl, CURLOPT_FILE, outfile);
52 curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
53 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
54 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
56 res = curl_easy_perform(curl);
58 fclose(outfile);
59 /* always cleanup */
60 curl_easy_cleanup(curl);
62 return NULL;
65 int main(int argc, char **argv)
67 GtkWidget *Window, *Frame, *Frame2;
68 GtkAdjustment *adj;
69 pthread_t curl_tid;
71 /* Init thread */
72 g_thread_init(NULL);
74 gtk_init(&argc, &argv);
75 Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
76 Frame = gtk_frame_new(NULL);
77 gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
78 gtk_container_add(GTK_CONTAINER(Window), Frame);
79 Frame2 = gtk_frame_new(NULL);
80 gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
81 gtk_container_add(GTK_CONTAINER(Frame), Frame2);
82 gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
83 adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
84 Bar = gtk_progress_bar_new_with_adjustment(adj);
85 gtk_container_add(GTK_CONTAINER(Frame2), Bar);
86 gtk_widget_show_all(Window);
88 pthread_create(&curl_tid, NULL, curl_thread, argv[1]);
90 gdk_threads_enter();
91 gtk_main();
92 gdk_threads_leave();
93 return 0;