2001-02-20 08:56:38 -05:00
|
|
|
/*****************************************************************************
|
2004-02-09 02:12:33 -05:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2001-02-20 08:56:38 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2019-11-02 18:41:43 -04:00
|
|
|
* Copyright (c) 2000 - 2019 David Odin (aka DindinX) for MandrakeSoft
|
2015-06-18 05:38:54 -04:00
|
|
|
*/
|
|
|
|
/* <DESC>
|
|
|
|
* use the libcurl in a gtk-threaded application
|
|
|
|
* </DESC>
|
2001-02-20 08:56:38 -05:00
|
|
|
*/
|
2000-11-03 09:47:07 -05:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
GtkWidget *Bar;
|
|
|
|
|
2004-02-09 02:12:33 -05: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 09:47:07 -05: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 16:28:32 -04:00
|
|
|
int my_progress_func(GtkWidget *bar,
|
2002-03-11 10:00:57 -05:00
|
|
|
double t, /* dltotal */
|
|
|
|
double d, /* dlnow */
|
|
|
|
double ultotal,
|
|
|
|
double ulnow)
|
2000-11-03 09:47:07 -05:00
|
|
|
{
|
|
|
|
/* printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/
|
|
|
|
gdk_threads_enter();
|
2008-04-03 16:28:32 -04:00
|
|
|
gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);
|
2000-11-03 09:47:07 -05:00
|
|
|
gdk_threads_leave();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-22 08:43:52 -05:00
|
|
|
void *my_thread(void *ptr)
|
2000-11-03 09:47:07 -05:00
|
|
|
{
|
|
|
|
CURL *curl;
|
2004-02-09 02:12:33 -05:00
|
|
|
|
2000-11-03 09:47:07 -05:00
|
|
|
curl = curl_easy_init();
|
2016-02-11 03:42:38 -05:00
|
|
|
if(curl) {
|
2019-05-21 04:44:16 -04:00
|
|
|
gchar *url = ptr;
|
2016-02-11 03:42:38 -05:00
|
|
|
const char *filename = "test.curl";
|
2019-05-21 04:44:16 -04:00
|
|
|
FILE *outfile = fopen(filename, "wb");
|
2000-11-03 09:47:07 -05:00
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
2004-02-09 02:12:33 -05:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
|
2000-11-03 09:47:07 -05:00
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
|
2008-05-22 17:20:07 -04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
|
2000-11-03 09:47:07 -05:00
|
|
|
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
|
2004-02-09 02:12:33 -05:00
|
|
|
|
2019-05-20 05:50:23 -04:00
|
|
|
curl_easy_perform(curl);
|
2000-11-03 09:47:07 -05:00
|
|
|
|
|
|
|
fclose(outfile);
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
2004-02-09 02:12:33 -05:00
|
|
|
|
2000-11-03 09:47:07 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
GtkWidget *Window, *Frame, *Frame2;
|
|
|
|
GtkAdjustment *adj;
|
|
|
|
|
2008-04-03 16:28:32 -04:00
|
|
|
/* Must initialize libcurl before any threads are started */
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
2000-11-03 09:47:07 -05:00
|
|
|
/* Init thread */
|
|
|
|
g_thread_init(NULL);
|
2004-02-09 02:12:33 -05:00
|
|
|
|
2000-11-03 09:47:07 -05: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);
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
|
2004-02-09 02:12:33 -05:00
|
|
|
g_warning("can't create the thread");
|
|
|
|
|
|
|
|
|
2000-11-03 09:47:07 -05:00
|
|
|
gdk_threads_enter();
|
|
|
|
gtk_main();
|
|
|
|
gdk_threads_leave();
|
|
|
|
return 0;
|
|
|
|
}
|