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