Get all the accounts registered on a device and list their details

This code snippet retrieves all the accounts registered on a device and lists their details such as an account ID, user name, display name and icon path. PRIVILEGE NEEDED to be set in tizen-manifest.xml file: http://tizen.org/privilege/account.read
//	PRIVILEGE NEEDED to be set in tizen-manifest.xml file: 
//	http://tizen.org/privilege/account.read

#include <stdlib.h>
#include <account.h>
#include <dlog.h> //for logging purposes

//Callback to be executed on each found account
static bool account_callback(account_h account, void *user_data)
{
	dlog_print(DLOG_INFO, LOG_TAG, "Account found!");
	int account_id = 0;
	char *user_name = NULL;
	char *display_name = NULL;
	char *icon_path = NULL;

	//Getting account details
	if ((account_get_account_id(account, &account_id) == ACCOUNT_ERROR_NONE)
		&& (account_get_user_name(account, &user_name) == ACCOUNT_ERROR_NONE)
		&& (account_get_display_name(account, &display_name) == ACCOUNT_ERROR_NONE)
		&& (account_get_icon_path(account, &icon_path) == ACCOUNT_ERROR_NONE))
	{
		 dlog_print(DLOG_INFO, LOG_TAG, "Account ID: %d", account_id);
		 dlog_print(DLOG_INFO, LOG_TAG, "Account user name: %s", user_name);
		 dlog_print(DLOG_INFO, LOG_TAG, "Account display name: %s", display_name);
		 dlog_print(DLOG_INFO, LOG_TAG, "Account icon path: %s", icon_path);
	}
	else
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when getting account data!");
	}
	free(user_name);
	free(display_name);
	free(icon_path);
	return true;
}

// Main function displaying number of accounts, iterating through them
// and displaying account details
static void list_all_accounts_details()
{
	int number_of_accounts = 0;

	//Connecting to account db
	if (account_connect_readonly() == ACCOUNT_ERROR_NONE)
	{
		if (account_get_total_count_from_db(&number_of_accounts) == ACCOUNT_ERROR_NONE)
		{
			dlog_print(DLOG_INFO, LOG_TAG, "Number of accounts on the device: %d", number_of_accounts);

			//Iterating through the accounts list
			if (account_foreach_account_from_db(account_callback, NULL) != ACCOUNT_ERROR_NONE)
			{
				dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when iterating through the account list!");
			}
		}
		else
		{
			dlog_print(DLOG_ERROR, LOG_TAG, "Cannot get the number of accounts!");
		}
	}
	if (account_disconnect() != ACCOUNT_ERROR_NONE)
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Cannot disconnect from account DB!");
	}
}

Responses

0 Replies