2d41aa248665a76c4f6b5bc148763a7d4398d142
[platform/upstream/curl.git] / docs / examples / smooth-gtk-thread.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  *
10  * This is a multi threaded application that uses a progress bar to show
11  * status.  It uses Gtk+ to make a smooth pulse.
12  *
13  * Written by Jud Bishop after studying the other examples provided with
14  * libcurl.
15  *
16  * To compile (on a single line):
17  * gcc -ggdb `pkg-config --cflags  --libs gtk+-2.0` -lcurl -lssl -lcrypto
18  *   -lgthread-2.0 -dl  smooth-gtk-thread.c -o smooth-gtk-thread
19  */
20
21 #include <stdio.h>
22 #include <gtk/gtk.h>
23 #include <glib.h>
24 #include <unistd.h>
25 #include <pthread.h>
26
27 #include <curl/curl.h>
28 #include <curl/types.h> /* new for v7 */
29 #include <curl/easy.h> /* new for v7 */
30
31 #define NUMT 4
32
33 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
34 int j = 0;
35 gint num_urls = 9; /* Just make sure this is less than urls[]*/
36 const char * const urls[]= {
37   "90022",
38   "90023",
39   "90024",
40   "90025",
41   "90026",
42   "90027",
43   "90028",
44   "90029",
45   "90030"
46 };
47
48 size_t write_file(void *ptr, size_t size, size_t nmemb, FILE *stream)
49 {
50   /* printf("write_file\n"); */
51   return fwrite(ptr, size, nmemb, stream);
52 }
53
54 /* http://xoap.weather.com/weather/local/46214?cc=*&dayf=5&unit=i */
55 void *pull_one_url(void *NaN)
56 {
57   CURL *curl;
58   CURLcode res;
59   gchar *http;
60   FILE *outfile;
61
62   /* Stop threads from entering unless j is incremented */
63   pthread_mutex_lock(&lock);
64   while ( j < num_urls )
65   {
66     printf("j = %d\n", j);
67
68     http =
69       g_strdup_printf("xoap.weather.com/weather/local/%s?cc=*&dayf=5&unit=i\n",
70                       urls[j]);
71
72     printf( "http %s", http );
73
74     curl = curl_easy_init();
75     if(curl)
76     {
77
78       outfile = fopen(urls[j], "w");
79       /* printf("fopen\n"); */
80
81       /* Set the URL and transfer type */
82       curl_easy_setopt(curl, CURLOPT_URL, http);
83
84       /* Write to the file */
85       curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
86       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file);
87
88       j++;  /* critical line */
89       pthread_mutex_unlock(&lock);
90
91       res = curl_easy_perform(curl);
92
93       fclose(outfile);
94       printf("fclose\n");
95
96       curl_easy_cleanup(curl);
97     }
98     g_free (http);
99
100     /* Adds more latency, testing the mutex.*/
101     sleep(1);
102
103   } /* end while */
104   return NULL;
105 }
106
107
108 gboolean pulse_bar(gpointer data)
109 {
110   gdk_threads_enter();
111   gtk_progress_bar_pulse (GTK_PROGRESS_BAR (data));
112   gdk_threads_leave();
113
114   /* Return true so the function will be called again;
115    * returning false removes this timeout function.
116    */
117   return TRUE;
118 }
119
120 void *create_thread(void *progress_bar)
121 {
122   pthread_t tid[NUMT];
123   int i;
124   int error;
125
126   /* Make sure I don't create more threads than urls. */
127   for(i=0; i < NUMT && i < num_urls ; i++) {
128     error = pthread_create(&tid[i],
129                            NULL, /* default attributes please */
130                            pull_one_url,
131                            NULL);
132     if(0 != error)
133       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
134     else
135       fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
136   }
137
138   /* Wait for all threads to terminate. */
139   for(i=0; i < NUMT && i < num_urls; i++) {
140     error = pthread_join(tid[i], NULL);
141     fprintf(stderr, "Thread %d terminated\n", i);
142   }
143
144   /* This stops the pulsing if you have it turned on in the progress bar
145      section */
146   g_source_remove(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(progress_bar),
147                                                     "pulse_id")));
148
149   /* This destroys the progress bar */
150   gtk_widget_destroy(progress_bar);
151
152   /* [Un]Comment this out to kill the program rather than pushing close. */
153   /* gtk_main_quit(); */
154
155
156   return NULL;
157
158 }
159
160 static gboolean cb_delete(GtkWidget *window, gpointer data)
161 {
162   gtk_main_quit();
163   return FALSE;
164 }
165
166 int main(int argc, char **argv)
167 {
168   GtkWidget *top_window, *outside_frame, *inside_frame, *progress_bar;
169
170   /* Must initialize libcurl before any threads are started */
171   curl_global_init(CURL_GLOBAL_ALL);
172
173   /* Init thread */
174   g_thread_init(NULL);
175   gdk_threads_init ();
176   gdk_threads_enter ();
177
178   gtk_init(&argc, &argv);
179
180   /* Base window */
181   top_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
182
183   /* Frame */
184   outside_frame = gtk_frame_new(NULL);
185   gtk_frame_set_shadow_type(GTK_FRAME(outside_frame), GTK_SHADOW_OUT);
186   gtk_container_add(GTK_CONTAINER(top_window), outside_frame);
187
188   /* Frame */
189   inside_frame = gtk_frame_new(NULL);
190   gtk_frame_set_shadow_type(GTK_FRAME(inside_frame), GTK_SHADOW_IN);
191   gtk_container_set_border_width(GTK_CONTAINER(inside_frame), 5);
192   gtk_container_add(GTK_CONTAINER(outside_frame), inside_frame);
193
194   /* Progress bar */
195   progress_bar = gtk_progress_bar_new();
196   gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progress_bar));
197   /* Make uniform pulsing */
198   gint pulse_ref = g_timeout_add (300, pulse_bar, progress_bar);
199   g_object_set_data(G_OBJECT(progress_bar), "pulse_id",
200                     GINT_TO_POINTER(pulse_ref));
201   gtk_container_add(GTK_CONTAINER(inside_frame), progress_bar);
202
203   gtk_widget_show_all(top_window);
204   printf("gtk_widget_show_all\n");
205
206   g_signal_connect(G_OBJECT (top_window), "delete-event",
207                    G_CALLBACK(cb_delete), NULL);
208
209   if (!g_thread_create(&create_thread, progress_bar, FALSE, NULL) != 0)
210     g_warning("can't create the thread");
211
212   gtk_main();
213   gdk_threads_leave();
214   printf("gdk_threads_leave\n");
215
216   return 0;
217 }
218