Download file from the internet with status notification pop-up and media DB update

This set of functions will help you to download file from the internet with reflecting download status update using notification pop-up even when client process is terminated. Additionally, the media database is updated to make the newly downloaded file visible in media galleries on the device.
//		PRIVILEGES NEEDED TO BE SET IN tizen-manifest.xml:
//		http://tizen.org/privilege/download
//		http://tizen.org/privilege/mediastorage
//		http://tizen.org/privilege/externalstorage
//		http://tizen.org/privilege/content.write

#include <download.h>
#include <media_content.h>
#include <stdlib.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)
{
	if(state == DOWNLOAD_STATE_COMPLETED)
	{
		char *path = NULL;
		if (download_get_downloaded_file_path(download_id, &path) == DOWNLOAD_ERROR_NONE)
		{
			dlog_print(DLOG_INFO, LOG_TAG, "Download completed! File path: %s", path);
		}

		if (media_content_connect() == MEDIA_CONTENT_ERROR_NONE)
		{
			if (media_content_scan_file(path) == MEDIA_CONTENT_ERROR_NONE)
			{
				dlog_print(DLOG_INFO, LOG_TAG, "Media database updated with the newly downloaded file!");
			}
			media_content_disconnect();
		}
		if (download_destroy(download_id) == DOWNLOAD_ERROR_NONE)
		{
			dlog_print(DLOG_INFO, LOG_TAG, "Successfully released the memory of download request!");
		}
		free(path);
	}
	else if(state == DOWNLOAD_STATE_FAILED || state == DOWNLOAD_STATE_CANCELED)
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Download failed or canceled by user!");
		if (download_destroy(download_id) == DOWNLOAD_ERROR_NONE)
		{
			dlog_print(DLOG_INFO, LOG_TAG, "Successfully released the memory of download request!");
		}
	}
}

// Main function for downloading the file with notification
// and updating the media database when download finished successfully
bool download_file_with_notification_and_update_media_db(const char *url, const char *dest_folder, const char *dest_filename)
{
	int download_id = 0;
	if (download_create(&download_id) == DOWNLOAD_ERROR_NONE)
	{
		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;
		}

		if ((download_set_url(download_id, url) != DOWNLOAD_ERROR_NONE)
			|| (download_set_destination(download_id, dest_folder) != DOWNLOAD_ERROR_NONE)
			|| (download_set_file_name(download_id, dest_filename) != DOWNLOAD_ERROR_NONE))
		{
			dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when setting download parameters!");
			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;
}

Responses

0 Replies