List all the accounts created by one application

This snippet searches and lists all accounts created by given account provider application, basing on its package name. 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;

	//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))
	{
		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);
	}
	else
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when getting account data!");
	}
	free(user_name);
	free(display_name);

	return true;
}

// Main function which is looking for accounts created by given account provider package
static void query_accounts(const char *pkg_name)
{
	int number_of_accounts = 0;
	dlog_print(DLOG_INFO, LOG_TAG, "Searching for accounts provided by %s...", pkg_name);
	//Connecting to account db
	if (account_connect_readonly() == ACCOUNT_ERROR_NONE)
	{
		if (account_query_account_by_package_name(account_callback, pkg_name, NULL) != ACCOUNT_ERROR_NONE)
		{
				dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when searching for the accounts!");
		}
	}
	if (account_disconnect() != ACCOUNT_ERROR_NONE)
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Cannot disconnect from account DB!");
	}
}

Responses

0 Replies