curlgtk.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  *  Copyright (c) 2000 David Odin (aka DindinX) for MandrakeSoft
00009  */
00010 /* <DESC>
00011  * use the libcurl in a gtk-threaded application
00012  * </DESC>
00013  */
00014 
00015 #include <stdio.h>
00016 #include <gtk/gtk.h>
00017 
00018 #include <curl/curl.h>
00019 
00020 GtkWidget *Bar;
00021 
00022 size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
00023 {
00024   return fwrite(ptr, size, nmemb, stream);
00025 }
00026 
00027 size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
00028 {
00029   return fread(ptr, size, nmemb, stream);
00030 }
00031 
00032 int my_progress_func(GtkWidget *bar,
00033                      double t, /* dltotal */
00034                      double d, /* dlnow */
00035                      double ultotal,
00036                      double ulnow)
00037 {
00038 /*  printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/
00039   gdk_threads_enter();
00040   gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);
00041   gdk_threads_leave();
00042   return 0;
00043 }
00044 
00045 void *my_thread(void *ptr)
00046 {
00047   CURL *curl;
00048   CURLcode res;
00049   FILE *outfile;
00050   gchar *url = ptr;
00051 
00052   curl = curl_easy_init();
00053   if(curl) {
00054     const char *filename = "test.curl";
00055     outfile = fopen(filename, "wb");
00056 
00057     curl_easy_setopt(curl, CURLOPT_URL, url);
00058     curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
00059     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
00060     curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
00061     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
00062     curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
00063     curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
00064 
00065     res = curl_easy_perform(curl);
00066 
00067     fclose(outfile);
00068     /* always cleanup */
00069     curl_easy_cleanup(curl);
00070   }
00071 
00072   return NULL;
00073 }
00074 
00075 int main(int argc, char **argv)
00076 {
00077   GtkWidget *Window, *Frame, *Frame2;
00078   GtkAdjustment *adj;
00079 
00080   /* Must initialize libcurl before any threads are started */
00081   curl_global_init(CURL_GLOBAL_ALL);
00082 
00083   /* Init thread */
00084   g_thread_init(NULL);
00085 
00086   gtk_init(&argc, &argv);
00087   Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00088   Frame = gtk_frame_new(NULL);
00089   gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
00090   gtk_container_add(GTK_CONTAINER(Window), Frame);
00091   Frame2 = gtk_frame_new(NULL);
00092   gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
00093   gtk_container_add(GTK_CONTAINER(Frame), Frame2);
00094   gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
00095   adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
00096   Bar = gtk_progress_bar_new_with_adjustment(adj);
00097   gtk_container_add(GTK_CONTAINER(Frame2), Bar);
00098   gtk_widget_show_all(Window);
00099 
00100   if(!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
00101     g_warning("can't create the thread");
00102 
00103 
00104   gdk_threads_enter();
00105   gtk_main();
00106   gdk_threads_leave();
00107   return 0;
00108 }
00109 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02