00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <gtk/gtk.h>
00038 #include <glib.h>
00039 #include <unistd.h>
00040 #include <pthread.h>
00041
00042 #include <curl/curl.h>
00043
00044 #define NUMT 4
00045
00046 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
00047 int j = 0;
00048 gint num_urls = 9;
00049 const char * const urls[]= {
00050 "90022",
00051 "90023",
00052 "90024",
00053 "90025",
00054 "90026",
00055 "90027",
00056 "90028",
00057 "90029",
00058 "90030"
00059 };
00060
00061 size_t write_file(void *ptr, size_t size, size_t nmemb, FILE *stream)
00062 {
00063
00064 return fwrite(ptr, size, nmemb, stream);
00065 }
00066
00067
00068 void *pull_one_url(void *NaN)
00069 {
00070 CURL *curl;
00071 CURLcode res;
00072 gchar *http;
00073 FILE *outfile;
00074
00075
00076 pthread_mutex_lock(&lock);
00077 while(j < num_urls) {
00078 printf("j = %d\n", j);
00079
00080 http =
00081 g_strdup_printf("xoap.weather.com/weather/local/%s?cc=*&dayf=5&unit=i\n",
00082 urls[j]);
00083
00084 printf("http %s", http);
00085
00086 curl = curl_easy_init();
00087 if(curl) {
00088
00089 outfile = fopen(urls[j], "wb");
00090
00091
00092 curl_easy_setopt(curl, CURLOPT_URL, http);
00093
00094
00095 curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
00096 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file);
00097
00098 j++;
00099 pthread_mutex_unlock(&lock);
00100
00101 res = curl_easy_perform(curl);
00102
00103 fclose(outfile);
00104 printf("fclose\n");
00105
00106 curl_easy_cleanup(curl);
00107 }
00108 g_free(http);
00109
00110
00111 sleep(1);
00112
00113 }
00114 return NULL;
00115 }
00116
00117
00118 gboolean pulse_bar(gpointer data)
00119 {
00120 gdk_threads_enter();
00121 gtk_progress_bar_pulse(GTK_PROGRESS_BAR (data));
00122 gdk_threads_leave();
00123
00124
00125
00126
00127 return TRUE;
00128 }
00129
00130 void *create_thread(void *progress_bar)
00131 {
00132 pthread_t tid[NUMT];
00133 int i;
00134 int error;
00135
00136
00137 for(i=0; i < NUMT && i < num_urls ; i++) {
00138 error = pthread_create(&tid[i],
00139 NULL,
00140 pull_one_url,
00141 NULL);
00142 if(0 != error)
00143 fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
00144 else
00145 fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
00146 }
00147
00148
00149 for(i=0; i < NUMT && i < num_urls; i++) {
00150 error = pthread_join(tid[i], NULL);
00151 fprintf(stderr, "Thread %d terminated\n", i);
00152 }
00153
00154
00155
00156 g_source_remove(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(progress_bar),
00157 "pulse_id")));
00158
00159
00160 gtk_widget_destroy(progress_bar);
00161
00162
00163
00164
00165
00166 return NULL;
00167
00168 }
00169
00170 static gboolean cb_delete(GtkWidget *window, gpointer data)
00171 {
00172 gtk_main_quit();
00173 return FALSE;
00174 }
00175
00176 int main(int argc, char **argv)
00177 {
00178 GtkWidget *top_window, *outside_frame, *inside_frame, *progress_bar;
00179
00180
00181 curl_global_init(CURL_GLOBAL_ALL);
00182
00183
00184 g_thread_init(NULL);
00185 gdk_threads_init();
00186 gdk_threads_enter();
00187
00188 gtk_init(&argc, &argv);
00189
00190
00191 top_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00192
00193
00194 outside_frame = gtk_frame_new(NULL);
00195 gtk_frame_set_shadow_type(GTK_FRAME(outside_frame), GTK_SHADOW_OUT);
00196 gtk_container_add(GTK_CONTAINER(top_window), outside_frame);
00197
00198
00199 inside_frame = gtk_frame_new(NULL);
00200 gtk_frame_set_shadow_type(GTK_FRAME(inside_frame), GTK_SHADOW_IN);
00201 gtk_container_set_border_width(GTK_CONTAINER(inside_frame), 5);
00202 gtk_container_add(GTK_CONTAINER(outside_frame), inside_frame);
00203
00204
00205 progress_bar = gtk_progress_bar_new();
00206 gtk_progress_bar_pulse(GTK_PROGRESS_BAR (progress_bar));
00207
00208 gint pulse_ref = g_timeout_add(300, pulse_bar, progress_bar);
00209 g_object_set_data(G_OBJECT(progress_bar), "pulse_id",
00210 GINT_TO_POINTER(pulse_ref));
00211 gtk_container_add(GTK_CONTAINER(inside_frame), progress_bar);
00212
00213 gtk_widget_show_all(top_window);
00214 printf("gtk_widget_show_all\n");
00215
00216 g_signal_connect(G_OBJECT (top_window), "delete-event",
00217 G_CALLBACK(cb_delete), NULL);
00218
00219 if(!g_thread_create(&create_thread, progress_bar, FALSE, NULL) != 0)
00220 g_warning("can't create the thread");
00221
00222 gtk_main();
00223 gdk_threads_leave();
00224 printf("gdk_threads_leave\n");
00225
00226 return 0;
00227 }