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