2001-02-20 14:56:38 +01:00
|
|
|
/*****************************************************************************
|
2004-02-09 08:12:33 +01:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2001-02-20 14:56:38 +01:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
2000-11-03 15:47:07 +01:00
|
|
|
/* Copyright (c) 2000 David Odin (aka DindinX) for MandrakeSoft */
|
|
|
|
/* an attempt to use the curl library in concert with a gtk-threaded application */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <curl/types.h> /* new for v7 */
|
|
|
|
#include <curl/easy.h> /* new for v7 */
|
|
|
|
|
|
|
|
GtkWidget *Bar;
|
|
|
|
|
2004-02-09 08:12:33 +01:00
|
|
|
size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
|
|
|
{
|
|
|
|
return fwrite(ptr, size, nmemb, stream);
|
|
|
|
}
|
|
|
|
|
2000-11-03 15:47:07 +01:00
|
|
|
size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
|
|
|
{
|
|
|
|
return fread(ptr, size, nmemb, stream);
|
|
|
|
}
|
|
|
|
|
2008-04-03 22:28:32 +02:00
|
|
|
int my_progress_func(GtkWidget *bar,
|
2002-03-11 16:00:57 +01:00
|
|
|
double t, /* dltotal */
|
|
|
|
double d, /* dlnow */
|
|
|
|
double ultotal,
|
|
|
|
double ulnow)
|
2000-11-03 15:47:07 +01:00
|
|
|
{
|
|
|
|
/* printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/
|
|
|
|
gdk_threads_enter();
|
2008-04-03 22:28:32 +02:00
|
|
|
gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);
|
2000-11-03 15:47:07 +01:00
|
|
|
gdk_threads_leave();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-22 14:43:52 +01:00
|
|
|
void *my_thread(void *ptr)
|
2000-11-03 15:47:07 +01:00
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res;
|
|
|
|
FILE *outfile;
|
|
|
|
gchar *url = ptr;
|
2004-02-09 08:12:33 +01:00
|
|
|
|
2000-11-03 15:47:07 +01:00
|
|
|
curl = curl_easy_init();
|
|
|
|
if(curl)
|
|
|
|
{
|
2004-02-09 08:12:33 +01:00
|
|
|
outfile = fopen("test.curl", "w");
|
2000-11-03 15:47:07 +01:00
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
2004-02-09 08:12:33 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
|
2000-11-03 15:47:07 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
|
2008-05-22 23:20:07 +02:00
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
|
2000-11-03 15:47:07 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
|
2004-02-09 08:12:33 +01:00
|
|
|
|
2000-11-03 15:47:07 +01:00
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
|
|
fclose(outfile);
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
2004-02-09 08:12:33 +01:00
|
|
|
|
2000-11-03 15:47:07 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
GtkWidget *Window, *Frame, *Frame2;
|
|
|
|
GtkAdjustment *adj;
|
|
|
|
|
2008-04-03 22:28:32 +02:00
|
|
|
/* Must initialize libcurl before any threads are started */
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
2000-11-03 15:47:07 +01:00
|
|
|
/* Init thread */
|
|
|
|
g_thread_init(NULL);
|
2004-02-09 08:12:33 +01:00
|
|
|
|
2000-11-03 15:47:07 +01:00
|
|
|
gtk_init(&argc, &argv);
|
|
|
|
Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
|
|
Frame = gtk_frame_new(NULL);
|
|
|
|
gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
|
|
|
|
gtk_container_add(GTK_CONTAINER(Window), Frame);
|
|
|
|
Frame2 = gtk_frame_new(NULL);
|
|
|
|
gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
|
|
|
|
gtk_container_add(GTK_CONTAINER(Frame), Frame2);
|
|
|
|
gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
|
|
|
|
adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
|
|
|
|
Bar = gtk_progress_bar_new_with_adjustment(adj);
|
|
|
|
gtk_container_add(GTK_CONTAINER(Frame2), Bar);
|
|
|
|
gtk_widget_show_all(Window);
|
|
|
|
|
2004-11-22 14:43:52 +01:00
|
|
|
if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
|
2004-02-09 08:12:33 +01:00
|
|
|
g_warning("can't create the thread");
|
|
|
|
|
|
|
|
|
2000-11-03 15:47:07 +01:00
|
|
|
gdk_threads_enter();
|
|
|
|
gtk_main();
|
|
|
|
gdk_threads_leave();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|