\chapter{Ecore\+\_\+\+Con\+\_\+\+Url -\/ downloading a file}
\hypertarget{ecore_con_url_download_example_c}{}\label{ecore_con_url_download_example_c}\index{Ecore\_Con\_Url -\/ downloading a file@{Ecore\_Con\_Url -\/ downloading a file}}
This is a simple example that shows how to download a file using Ecore\+\_\+\+Con\+\_\+\+Url. The full source code for this example can be found at \doxylink{ecore_con_url_download_example_8c-example}{ecore\+\_\+con\+\_\+url\+\_\+download\+\_\+example.\+c}.

First we are setting some callbacks for events that will be sent when data arrives in our connection (the data is the content of the file being downloaded), and when the download is completed. The {\ttfamily \+\_\+url\+\_\+progress\+\_\+cb} and {\ttfamily \+\_\+url\+\_\+complete\+\_\+cb} are these callbacks\+:

 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{struct\ }\_request}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{long}\ size;}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_url\_progress\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress}{Ecore\_Con\_Event\_Url\_Progress}}\ *url\_progress\ =\ event\_info;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{float}\ percent;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a4ac5badcaedbbbdab002e162c11ed26b}{total}}\ >\ 0)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{struct\ }\_request\ *req\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gacba8bd1b8da593f7365d9e42bad9b279}{ecore\_con\_url\_data\_get}}(url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a262d26bbc8e5483dc64e819a157a0b62}{url\_con}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ req-\/>size\ =\ url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ percent\ =\ (url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now}}\ /\ url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a4ac5badcaedbbbdab002e162c11ed26b}{total}})\ *\ 100;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Total\ of\ download\ complete:\ \%0.1f\ (\%0.0f)\%\%\(\backslash\)n"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ percent,\ url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now}});}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}status:\ \%d\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gab42d35f0a18ae6d02e3e023b96e455ab}{ecore\_con\_url\_status\_code\_get}}(url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a262d26bbc8e5483dc64e819a157a0b62}{url\_con}}));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_url\_complete\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete}{Ecore\_Con\_Event\_Url\_Complete}}\ *url\_complete\ =\ event\_info;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }\_request\ *req\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gacba8bd1b8da593f7365d9e42bad9b279}{ecore\_con\_url\_data\_get}}(url\_complete-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete_a58335ec0eac21f9c0828695922807039}{url\_con}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ nbytes\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga564d9e14d2bb0edbe1d8d9d7eec60a1c}{ecore\_con\_url\_received\_bytes\_get}}(url\_complete-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete_a58335ec0eac21f9c0828695922807039}{url\_con}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}download\ completed\ with\ status\ code:\ \%d\(\backslash\)n"{}},\ url\_complete-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete_ab842caa1615a911c3578e0dfeae5f773}{status}});}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Total\ size\ of\ downloaded\ file:\ \%ld\ bytes\(\backslash\)n"{}},\ req-\/>size);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Total\ size\ of\ downloaded\ file:\ \%d\ bytes\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}(from\ received\_bytes\_get)\(\backslash\)n"{}},\ nbytes);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 Notice that we also declared a struct that will hold how many bytes were downloaded through this object. It will be set in the {\ttfamily main} function using \doxylink{group__Ecore__Con__Url__Group_gab5587450c5ac21e9128fb46e8dc80c33}{ecore\+\_\+con\+\_\+url\+\_\+data\+\_\+set()}.

In the next step, on the {\ttfamily main} function, we open a file where we are going to save the content being downloaded\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *argv[])}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ Ecore\_Con\_Url\ *ec\_url\ =\ NULL;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }\_request\ *req;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ fd;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *filename\ =\ \textcolor{stringliteral}{"{}downloadedfile.dat"{}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (argc\ <\ 2)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}need\ one\ parameter:\ <url>\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ fd\ =\ open(filename,\ O\_CREAT\ |\ O\_BINARY\ |\ O\_WRONLY\ |\ O\_TRUNC,\ 0644);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (fd\ ==\ -\/1)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}error:\ could\ not\ open\ file\ for\ writing:\ \(\backslash\)"{}\%s\(\backslash\)"{}\(\backslash\)n"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ filename);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 With the file successfully open, let\textquotesingle{}s create our Ecore\+\_\+\+Con\+\_\+\+Url object. For this, we initialize the libraries and create the object\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Lib__Group_ga8a20f47372627a03e86a8705a6f8a82d}{ecore\_con\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga96388997453269f58bde7873811af6d5}{ecore\_con\_url\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ ec\_url\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga71cce8e67deb5ea13d5f62d67d51ad9b}{ecore\_con\_url\_new}}(argv[1]);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!ec\_url)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}error\ when\ creating\ ecore\ con\ url\ object.\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ end;}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 Then we allocate and set the data struct to the connection object, and set a file descriptor from our previously open file to it. We also add the event handlers (callbacks) to the events that will be emitted on data being received and download complete\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ req\ =\ malloc(\textcolor{keyword}{sizeof}(*req));}
\DoxyCodeLine{\ \ \ req-\/>size\ =\ 0;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gab5587450c5ac21e9128fb46e8dc80c33}{ecore\_con\_url\_data\_set}}(ec\_url,\ req);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga006356bcaff26d482e407c7d9d0d691e}{ecore\_con\_url\_fd\_set}}(ec\_url,\ fd);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_ga80f2cc4f6c436a5291150f8791d1a72f}{ECORE\_CON\_EVENT\_URL\_PROGRESS}},\ \_url\_progress\_cb,\ NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Con__Events__Group_gaa814f51fd5f97a92cdc76b624651d85a}{ECORE\_CON\_EVENT\_URL\_COMPLETE}},\ \_url\_complete\_cb,\ NULL);}

\end{DoxyCodeInclude}
 Finally we start our request, and run the main loop\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Ecore__Con__Url__Group_gaeb79adc8f07e54ad7a87a8c55de5ec72}{ecore\_con\_url\_get}}(ec\_url))}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}could\ not\ realize\ request.\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ free\_ec\_url;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{}
\DoxyCodeLine{free\_ec\_url:}
\DoxyCodeLine{\ \ \ free(req);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga856c091a11980598adb0666cec9b5add}{ecore\_con\_url\_free}}(ec\_url);}
\DoxyCodeLine{end:}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ close(fd);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gaabcdae26e4a474dc5b9c43f60e4d00d4}{ecore\_con\_url\_shutdown}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Lib__Group_ga0e5e01c67f7135e16db0e8f17d087d99}{ecore\_con\_shutdown}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The rest of this code was just freeing resources, with some labels to be used for error handling. 