Simple file download with notification
This set of functions will help you to download file from the internet to the default download folder. The download status will be shown using notification pop-up even when client process is terminated.
// PRIVILEGES NEEDED TO BE SET IN tizen-manifest.xml:
// http://tizen.org/privilege/download
// http://tizen.org/privilege/mediastorage
#include <download.h>
#include <dlog.h> //for logging purposes
//Callback to be called on the download state change.
void download_state_callback(int download_id, download_state_e state, void *user_data)
{
//when download is completed, failed or was canceled
if(state >= DOWNLOAD_STATE_COMPLETED)
{
dlog_print(DLOG_INFO, LOG_TAG, "Download completed!");
if (download_destroy(download_id) == DOWNLOAD_ERROR_NONE)
{
dlog_print(DLOG_INFO, LOG_TAG, "Successfully released the memory of a download request!");
}
}
}
// Main function for downloading the file with notification to default file name and location
bool download_file_with_notification(const char *url)
{
int download_id = 0;
if (download_create(&download_id) == DOWNLOAD_ERROR_NONE)
{
if (download_set_url(download_id, url) != DOWNLOAD_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when setting url!");
download_destroy(download_id);
return false;
}
if (download_set_state_changed_cb(download_id, download_state_callback, NULL) != DOWNLOAD_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when setting callback!");
download_destroy(download_id);
return false;
}
// When auto download is set to true, the download will be continued
// even when the client process is terminated.
if (download_set_auto_download(download_id, true) != DOWNLOAD_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when setting auto download!");
download_destroy(download_id);
return false;
}
// Setting the notification to be displayed on each stage of download.
if (download_set_notification_type(download_id, DOWNLOAD_NOTIFICATION_TYPE_ALL) != DOWNLOAD_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when enabling all types of download notification!");
download_destroy(download_id);
return false;
}
if (download_start(download_id) != DOWNLOAD_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when starting download!");
download_destroy(download_id);
return false;
}
else
{
dlog_print(DLOG_INFO, LOG_TAG, "Download started...");
}
}
else
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when creating download request!");
return false;
}
return true;
}