1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 https://curl.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 /* <DESC>
23 * A multi threaded application that uses a progress bar to show
24 * status. It uses Gtk+ to make a smooth pulse.
25 * </DESC>
26 */
27 /*
28 * Written by Jud Bishop after studying the other examples provided with
29 * libcurl.
30 *
31 * To compile (on a single line):
32 * gcc -ggdb `pkg-config --cflags --libs gtk+-2.0` -lcurl -lssl -lcrypto
33 * -lgthread-2.0 -dl smooth-gtk-thread.c -o smooth-gtk-thread
34 */
35
36 #include <stdio.h>
37 #include <gtk/gtk.h>
38 #include <glib.h>
39 #include <unistd.h>
40 #include <pthread.h>
41
42 #include <curl/curl.h>
43
44 #define NUMT 4
45
46 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
47 int j = 0;
48 gint num_urls = 9; /* Just make sure this is less than urls[]*/
49 const char * const urls[]= {
50 "90022",
51 "90023",
52 "90024",
53 "90025",
54 "90026",
55 "90027",
56 "90028",
57 "90029",
58 "90030"
59 };
60
write_file(void * ptr,size_t size,size_t nmemb,FILE * stream)61 size_t write_file(void *ptr, size_t size, size_t nmemb, FILE *stream)
62 {
63 return fwrite(ptr, size, nmemb, stream);
64 }
65
run_one(gchar * http,int j)66 static void run_one(gchar *http, int j)
67 {
68 FILE *outfile = fopen(urls[j], "wb");
69 CURL *curl;
70
71 curl = curl_easy_init();
72 if(curl) {
73 printf("j = %d\n", j);
74
75 /* Set the URL and transfer type */
76 curl_easy_setopt(curl, CURLOPT_URL, http);
77
78 /* Write to the file */
79 curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
80 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file);
81 curl_easy_perform(curl);
82
83 fclose(outfile);
84 curl_easy_cleanup(curl);
85 }
86 }
87
pull_one_url(void * NaN)88 void *pull_one_url(void *NaN)
89 {
90 /* protect the reading and increasing of 'j' with a mutex */
91 pthread_mutex_lock(&lock);
92 while(j < num_urls) {
93 int i = j;
94 j++;
95 pthread_mutex_unlock(&lock);
96 http = g_strdup_printf("https://example.com/%s", urls[i]);
97 if(http) {
98 run_one(http, i);
99 g_free(http);
100 }
101 pthread_mutex_lock(&lock);
102 }
103 pthread_mutex_unlock(&lock);
104 return NULL;
105 }
106
107
pulse_bar(gpointer data)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
create_thread(void * progress_bar)120 void *create_thread(void *progress_bar)
121 {
122 pthread_t tid[NUMT];
123 int i;
124
125 /* Make sure I do not create more threads than urls. */
126 for(i = 0; i < NUMT && i < num_urls ; i++) {
127 int 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 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
cb_delete(GtkWidget * window,gpointer data)159 static gboolean cb_delete(GtkWidget *window, gpointer data)
160 {
161 gtk_main_quit();
162 return FALSE;
163 }
164
main(int argc,char ** argv)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("cannot create the thread");
210
211 gtk_main();
212 gdk_threads_leave();
213 printf("gdk_threads_leave\n");
214
215 return 0;
216 }
217