\hypertarget{ecore_con_url_download_example_8c-example}{}\section{ecore\+\_\+con\+\_\+url\+\_\+download\+\_\+example.\+c}
Shows how to download a file using an Ecore\+\_\+\+Con\+\_\+\+Url object. See the complete example description at \hyperlink{ecore_con_url_download_example_c}{Ecore\+\_\+\+Con\+\_\+\+Url -\/ downloading a file}


\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{// gcc -o ecore\_con\_url\_download\_example ecore\_con\_url\_download\_example.c `pkg-config --libs --cflags ecore
       ecore-con`}

\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <sys/types.h>}
\textcolor{preprocessor}{#include <sys/stat.h>}
\textcolor{preprocessor}{#include <fcntl.h>}
\textcolor{preprocessor}{#include <unistd.h>}

\textcolor{preprocessor}{#include <Ecore.h>}
\textcolor{preprocessor}{#include <Ecore\_Con.h>}

\textcolor{preprocessor}{#ifndef O\_BINARY}
\textcolor{preprocessor}{# define O\_BINARY 0}
\textcolor{preprocessor}{#endif}

\textcolor{keyword}{struct }\_request
\{
   \textcolor{keywordtype}{long} size;
\};

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_url\_progress\_cb(\textcolor{keywordtype}{void} *data \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keywordtype}{int} type \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keywordtype}{void} *event\_info)
\{
   \hyperlink{struct__Ecore__Con__Event__Url__Progress}{Ecore\_Con\_Event\_Url\_Progress} *url\_progress = event\_info;
   \textcolor{keywordtype}{float} percent;

   \textcolor{keywordflow}{if} (url\_progress->\hyperlink{struct__Ecore__Con__Event__Url__Progress_a7443bc2bc747c37290d53d41e0d378b5}{down}.\hyperlink{struct__Ecore__Con__Event__Url__Progress_a4ac5badcaedbbbdab002e162c11ed26b}{total} > 0)
     \{
        \textcolor{keyword}{struct }\_request *req = \hyperlink{group__Ecore__Con__Url__Group_gacad8548217172607bad2d2741935ffaa}{ecore\_con\_url\_data\_get}(url\_progress->
      \hyperlink{struct__Ecore__Con__Event__Url__Progress_a262d26bbc8e5483dc64e819a157a0b62}{url\_con});
        req->size = url\_progress->\hyperlink{struct__Ecore__Con__Event__Url__Progress_a7443bc2bc747c37290d53d41e0d378b5}{down}.\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now};

        percent = (url\_progress->\hyperlink{struct__Ecore__Con__Event__Url__Progress_a7443bc2bc747c37290d53d41e0d378b5}{down}.\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now} / url\_progress->\hyperlink{struct__Ecore__Con__Event__Url__Progress_a7443bc2bc747c37290d53d41e0d378b5}{down}.\hyperlink{struct__Ecore__Con__Event__Url__Progress_a4ac5badcaedbbbdab002e162c11ed26b}{total}) * 100;
        printf(\textcolor{stringliteral}{"Total of download complete: %0.1f (%0.0f)%%\(\backslash\)n"},
               percent, url\_progress->\hyperlink{struct__Ecore__Con__Event__Url__Progress_a7443bc2bc747c37290d53d41e0d378b5}{down}.\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now});
     \}

   printf(\textcolor{stringliteral}{"status: %d\(\backslash\)n"}, \hyperlink{group__Ecore__Con__Url__Group_gaf2e8dc10b2fcad5bbf012c0936424251}{ecore\_con\_url\_status\_code\_get}(url\_progress->
      \hyperlink{struct__Ecore__Con__Event__Url__Progress_a262d26bbc8e5483dc64e819a157a0b62}{url\_con}));

   \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
\}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_url\_complete\_cb(\textcolor{keywordtype}{void} *data \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keywordtype}{int} type \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keywordtype}{void} *event\_info)
\{
   \hyperlink{struct__Ecore__Con__Event__Url__Complete}{Ecore\_Con\_Event\_Url\_Complete} *url\_complete = event\_info;

   \textcolor{keyword}{struct }\_request *req = \hyperlink{group__Ecore__Con__Url__Group_gacad8548217172607bad2d2741935ffaa}{ecore\_con\_url\_data\_get}(url\_complete->
      \hyperlink{struct__Ecore__Con__Event__Url__Complete_a58335ec0eac21f9c0828695922807039}{url\_con});
   \textcolor{keywordtype}{int} nbytes = \hyperlink{group__Ecore__Con__Url__Group_ga9587eb74811d11b69b281a715b6fd1a1}{ecore\_con\_url\_received\_bytes\_get}(url\_complete->
      \hyperlink{struct__Ecore__Con__Event__Url__Complete_a58335ec0eac21f9c0828695922807039}{url\_con});

   printf(\textcolor{stringliteral}{"\(\backslash\)n"});
   printf(\textcolor{stringliteral}{"download completed with status code: %d\(\backslash\)n"}, url\_complete->\hyperlink{struct__Ecore__Con__Event__Url__Complete_ab842caa1615a911c3578e0dfeae5f773}{status});
   printf(\textcolor{stringliteral}{"Total size of downloaded file: %ld bytes\(\backslash\)n"}, req->size);
   printf(\textcolor{stringliteral}{"Total size of downloaded file: %d bytes "}
          \textcolor{stringliteral}{"(from received\_bytes\_get)\(\backslash\)n"}, nbytes);
   \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}();

   \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *argv[])
\{
   Ecore\_Con\_Url *ec\_url = NULL;
   \textcolor{keyword}{struct }\_request *req;
   \textcolor{keywordtype}{int} fd;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *filename = \textcolor{stringliteral}{"downloadedfile.dat"};

   \textcolor{keywordflow}{if} (argc < 2)
     \{
        printf(\textcolor{stringliteral}{"need one parameter: <url>\(\backslash\)n"});
        \textcolor{keywordflow}{return} -1;
     \}

   fd = open(filename, O\_CREAT | O\_BINARY | O\_WRONLY | O\_TRUNC, 0644);

   \textcolor{keywordflow}{if} (fd == -1)
     \{
        printf(\textcolor{stringliteral}{"error: could not open file for writing: \(\backslash\)"%s\(\backslash\)"\(\backslash\)n"},
               filename);
        \textcolor{keywordflow}{return} -1;
     \}

   \hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}();
   \hyperlink{group__Ecore__Con__Events__Group_gacf5ecf42c516bb9c8b6759721e9559bd}{ecore\_con\_init}();
   \hyperlink{group__Ecore__Con__Url__Group_gac3fbeaf9cde529362e4567fc37e45c9f}{ecore\_con\_url\_init}();

   ec\_url = \hyperlink{group__Ecore__Con__Url__Group_gae40b7c1a94413166c61524b707118f19}{ecore\_con\_url\_new}(argv[1]);
   \textcolor{keywordflow}{if} (!ec\_url)
     \{
        printf(\textcolor{stringliteral}{"error when creating ecore con url object.\(\backslash\)n"});
        \textcolor{keywordflow}{goto} end;
     \}

   req = malloc(\textcolor{keyword}{sizeof}(*req));
   req->size = 0;
   \hyperlink{group__Ecore__Con__Url__Group_ga194f122b9e4e9f2aa9b5c2f07e9d7f3e}{ecore\_con\_url\_data\_set}(ec\_url, req);

   \hyperlink{group__Ecore__Con__Url__Group_ga7b58b82b9b49060642ad3fe64348dc8d}{ecore\_con\_url\_fd\_set}(ec\_url, fd);

   \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\hyperlink{group__Ecore__Con__Events__Group_ga2a169e3fec20576f464b844df18143f8}{ECORE\_CON\_EVENT\_URL\_PROGRESS},
       \_url\_progress\_cb, NULL);
   \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\hyperlink{group__Ecore__Con__Events__Group_gae90b9b857e1e0adc0c7f24e7ccecb44b}{ECORE\_CON\_EVENT\_URL\_COMPLETE},
       \_url\_complete\_cb, NULL);

   \textcolor{keywordflow}{if} (!\hyperlink{group__Ecore__Con__Url__Group_ga46f75358256eec26bd99eef96b25ffa8}{ecore\_con\_url\_get}(ec\_url))
     \{
        printf(\textcolor{stringliteral}{"could not realize request.\(\backslash\)n"});
        \textcolor{keywordflow}{goto} free\_ec\_url;
     \}

   \hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}();

free\_ec\_url:
   free(req);
   \hyperlink{group__Ecore__Con__Url__Group_ga711510886aaece16f5dee6903c603fb2}{ecore\_con\_url\_free}(ec\_url);
end:

   close(fd);
   \hyperlink{group__Ecore__Con__Url__Group_ga0da252aa2c919722894dd41cc6549435}{ecore\_con\_url\_shutdown}();
   \hyperlink{group__Ecore__Con__Events__Group_gaccfd8dd2a1187b71339b5cc39dc83938}{ecore\_con\_shutdown}();
   \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();

   \textcolor{keywordflow}{return} 0;
\}

\end{DoxyCodeInclude}
 