static Eina_Bool _url_compl_cb(void *data, int type EINA_UNUSED, void *event_info) { url_test *info = data; Ecore_Con_Event_Url_Complete *ev = event_info; printf("Total downloaded bytes = %d\n", ecore_con_url_received_bytes_get(ev->url_con)); if (info->_tmpfd) { _free_url_test(info); ecore_con_url_free(ev->url_con); } else { fail_unless(ecore_con_url_url_set(ev->url_con, "ftp://ftp.kernel.org/pub/linux/kernel/README")); ecore_con_url_verbose_set (ev->url_con, EINA_FALSE); info->_tmpfd = eina_file_mkstemp("ecore_con_test_XXXXXX.html", &(info->_test_file)); if (info->_tmpfd < 0) { free(info); ecore_con_url_free(ev->url_con); fail(); } ecore_con_url_fd_set(ev->url_con, info->_tmpfd); if (!ecore_con_url_get(ev->url_con)) { _free_url_test(info); ecore_con_url_free(ev->url_con); fail(); } } return EINA_FALSE; }
bool FileDownloader::Start() { Utils::logger("downloader") << Priority::DEBUG << "FileDownloader: Start download (" << url << ")" << log4cpp::eol; if (url_con) { Utils::logger("downloader") << Priority::WARN << "A download is already in progress..." << log4cpp::eol; return false; } url_con = ecore_con_url_new(url.c_str()); if (!url_con) { string err = "Failed to create Ecore_Con_Url"; IPC::Instance().SendEvent("downloader::" + Utils::to_string(this), "failed", IPCData(new string(err), new DeletorT<string *>()), true); cb_signal.emit("failed", &err); cb_signal_user.emit("failed", &err, user_data); Utils::logger("downloader") << Priority::ERROR << "Download failed: " << err << log4cpp::eol; return false; } if (dest.empty()) { //Create a temporary file for download int cpt = 0; //Get a temporary filename do { tmpFile = "/tmp/calaos" + Utils::to_string(getpid()) + "_download_tmp_"; tmpFile += Utils::to_string(cpt); cpt++; } while (ecore_file_exists(tmpFile.c_str())); dl_file = fopen(tmpFile.c_str(), "wb"); } else { dl_file = fopen(dest.c_str(), "wb"); } if (!dl_file) { string err = "Failed to open file"; IPC::Instance().SendEvent("downloader::" + Utils::to_string(this), "failed", IPCData(new string(err), new DeletorT<string *>()), true); cb_signal.emit("failed", &err); cb_signal_user.emit("failed", &err, user_data); Utils::logger("downloader") << Priority::ERROR << "Download failed: " << err << log4cpp::eol; ecore_con_url_free(url_con); url_con = NULL; return false; } ecore_con_url_fd_set(url_con, fileno(dl_file)); ecore_con_url_data_set(url_con, this); ecore_con_url_ssl_verify_peer_set(url_con, false); bool ret = false; if (postData.empty()) { ret = ecore_con_url_get(url_con); } else { ret = ecore_con_url_post(url_con, postData.c_str(), postData.length(), postContentType.c_str()); } if (!ret) { string err = "Failed to call GET/POST"; IPC::Instance().SendEvent("downloader::" + Utils::to_string(this), "failed", IPCData(new string(err), new DeletorT<string *>()), true); cb_signal.emit("failed", &err); cb_signal_user.emit("failed", &err, user_data); Utils::logger("downloader") << Priority::ERROR << "Download failed: " << err << log4cpp::eol; ecore_con_url_free(url_con); url_con = NULL; fclose(dl_file); if (dest.empty()) ecore_file_unlink(tmpFile.c_str()); return false; } return 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_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; }