ecore_con_url_download_example.c

Shows how to download a file using an Ecore_Con_Url object.

Shows how to download a file using an Ecore_Con_Url object. See the complete example description at Ecore_Con_Url - downloading a file

//Compile with:
// gcc -o ecore_con_url_download_example ecore_con_url_download_example.c `pkg-config --libs --cflags ecore ecore-con`
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <Ecore.h>
#include <Ecore_Con.h>
#ifndef O_BINARY
# define O_BINARY 0
#endif
struct _request
{
long size;
};
static Eina_Bool
_url_progress_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event_info)
{
Ecore_Con_Event_Url_Progress *url_progress = event_info;
float percent;
if (url_progress->down.total > 0)
{
struct _request *req = ecore_con_url_data_get(url_progress->url_con);
req->size = url_progress->down.now;
percent = (url_progress->down.now / url_progress->down.total) * 100;
printf("Total of download complete: %0.1f (%0.0f)%%\n",
percent, url_progress->down.now);
}
printf("status: %d\n", ecore_con_url_status_code_get(url_progress->url_con));
return EINA_TRUE;
}
static Eina_Bool
_url_complete_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event_info)
{
Ecore_Con_Event_Url_Complete *url_complete = event_info;
struct _request *req = ecore_con_url_data_get(url_complete->url_con);
int nbytes = ecore_con_url_received_bytes_get(url_complete->url_con);
printf("\n");
printf("download completed with status code: %d\n", url_complete->status);
printf("Total size of downloaded file: %ld bytes\n", req->size);
printf("Total size of downloaded file: %d bytes "
"(from received_bytes_get)\n", nbytes);
ecore_main_loop_quit();
return EINA_TRUE;
}
int
main(int argc, const char *argv[])
{
Ecore_Con_Url *ec_url = NULL;
struct _request *req;
int fd;
const char *filename = "downloadedfile.dat";
if (argc < 2)
{
printf("need one parameter: <url>\n");
return -1;
}
fd = open(filename, O_CREAT | O_BINARY | O_WRONLY | O_TRUNC, 0644);
if (fd == -1)
{
printf("error: could not open file for writing: \"%s\"\n",
filename);
return -1;
}
ecore_init();
ecore_con_init();
ecore_con_url_init();
ec_url = ecore_con_url_new(argv[1]);
if (!ec_url)
{
printf("error when creating ecore con url object.\n");
goto end;
}
req = malloc(sizeof(*req));
req->size = 0;
ecore_con_url_data_set(ec_url, req);
ecore_con_url_fd_set(ec_url, fd);
ecore_event_handler_add(ECORE_CON_EVENT_URL_PROGRESS, _url_progress_cb, NULL);
ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_complete_cb, NULL);
if (!ecore_con_url_get(ec_url))
{
printf("could not realize request.\n");
goto free_ec_url;
}
ecore_main_loop_begin();
free_ec_url:
free(req);
ecore_con_url_free(ec_url);
end:
close(fd);
ecore_con_url_shutdown();
ecore_con_shutdown();
ecore_shutdown();
return 0;
}
ECORE_CON_EVENT_URL_PROGRESS
ECORE_CON_API int ECORE_CON_EVENT_URL_PROGRESS
A URL object has made progress in its transfer.
Definition ecore_con_url.c:31
ECORE_CON_EVENT_URL_COMPLETE
ECORE_CON_API int ECORE_CON_EVENT_URL_COMPLETE
A URL object has completed its transfer to and from the server and can be reused.
Definition ecore_con_url.c:30
ecore_con_shutdown
ECORE_CON_API int ecore_con_shutdown(void)
Shuts down the Ecore_Con library.
Definition ecore_con.c:133
ecore_con_init
ECORE_CON_API int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition ecore_con.c:68
ecore_con_url_fd_set
ECORE_CON_API void ecore_con_url_fd_set(Ecore_Con_Url *url_con, int fd)
Sets up a file for receiving response data.
Definition ecore_con_url.c:1138
ecore_con_url_received_bytes_get
ECORE_CON_API int ecore_con_url_received_bytes_get(Ecore_Con_Url *url_con)
Retrieves the number of bytes received.
Definition ecore_con_url.c:1279
ecore_con_url_new
ECORE_CON_API Ecore_Con_Url * ecore_con_url_new(const char *url)
Creates and initializes a new Ecore_Con_Url connection object.
Definition ecore_con_url.c:782
ecore_con_url_free
ECORE_CON_API void ecore_con_url_free(Ecore_Con_Url *url_con)
Destroys an Ecore_Con_Url connection object.
Definition ecore_con_url.c:819
ecore_con_url_init
ECORE_CON_API int ecore_con_url_init(void)
Initializes the Ecore_Con_Url library.
Definition ecore_con_url.c:45
Ecore_Con_Event_Url_Complete
struct _Ecore_Con_Event_Url_Complete Ecore_Con_Event_Url_Complete
Used as the data param for the corresponding event.
Definition Ecore_Con.h:428
ecore_con_url_shutdown
ECORE_CON_API int ecore_con_url_shutdown(void)
Shuts down the Ecore_Con_Url library.
Definition ecore_con_url.c:68
ecore_con_url_status_code_get
ECORE_CON_API int ecore_con_url_status_code_get(Ecore_Con_Url *url_con)
Gets the returned HTTP STATUS code.
Definition ecore_con_url.c:1286
ecore_con_url_data_set
ECORE_CON_API void ecore_con_url_data_set(Ecore_Con_Url *url_con, void *data)
Associates data with a connection object.
Definition ecore_con_url.c:837
Ecore_Con_Url
struct _Ecore_Con_Url Ecore_Con_Url
Used to provide legacy API/ABI compatibility with non-Eo applications.
Definition Ecore_Con.h:323
Ecore_Con_Event_Url_Progress
struct _Ecore_Con_Event_Url_Progress Ecore_Con_Event_Url_Progress
Used as the data param for the corresponding event.
Definition Ecore_Con.h:435
ecore_con_url_data_get
ECORE_CON_API void * ecore_con_url_data_get(Ecore_Con_Url *url_con)
Retrieves data associated with a Ecore_Con_Url connection object.
Definition ecore_con_url.c:830
ecore_con_url_get
ECORE_CON_API Eina_Bool ecore_con_url_get(Ecore_Con_Url *url_con)
Sends a get request.
Definition ecore_con_url.c:862
ecore_event_handler_add
Ecore_Event_Handler * ecore_event_handler_add(int type, Ecore_Event_Handler_Cb func, const void *data)
Adds an event handler.
Definition ecore_events.c:13
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition ecore.c:371
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition ecore.c:230
ecore_main_loop_quit
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition ecore_main.c:1321
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition ecore_main.c:1311
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition eina_types.h:539
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition eina_types.h:339
_Ecore_Con_Event_Url_Complete::url_con
Ecore_Con_Url * url_con
a pointer to the connection object
Definition Ecore_Con.h:587
_Ecore_Con_Event_Url_Complete::status
int status
HTTP status code of the operation (200, 404, 401, etc.).
Definition Ecore_Con.h:588
_Ecore_Con_Event_Url_Progress::url_con
Ecore_Con_Url * url_con
a pointer to the connection object
Definition Ecore_Con.h:598
_Ecore_Con_Event_Url_Progress::now
double now
current size of the downloading data (in bytes)
Definition Ecore_Con.h:602
_Ecore_Con_Event_Url_Progress::total
double total
total size of the downloading data (in bytes)
Definition Ecore_Con.h:601
_Ecore_Con_Event_Url_Progress::down
struct _Ecore_Con_Event_Url_Progress::@110130324222164306215031112163043076056075104121 down
download info