diff options
author | hj kim <backto.kim@samsung.com> | 2018-05-14 14:57:52 +0900 |
---|---|---|
committer | hj kim <backto.kim@samsung.com> | 2018-05-14 14:59:31 +0900 |
commit | 3a33b11264ec0295351591c474bb32e119b7ba5e (patch) | |
tree | 0d9b9ed383ee49b674d3f6b414a2e49d8ed96cbf | |
parent | e476f25d87ecd930abf8c87951b2f45b89717ce2 (diff) | |
download | media-controller-3a33b11264ec0295351591c474bb32e119b7ba5e.tar.gz media-controller-3a33b11264ec0295351591c474bb32e119b7ba5e.tar.bz2 media-controller-3a33b11264ec0295351591c474bb32e119b7ba5e.zip |
Code refactoring. remove duplicated code when send command
Change-Id: I29cd01bfe5374cca124f8864932415a62092e1bf
-rwxr-xr-x | src/media_controller_client.c | 115 |
1 files changed, 37 insertions, 78 deletions
diff --git a/src/media_controller_client.c b/src/media_controller_client.c index 8a2f897..60ddf28 100755 --- a/src/media_controller_client.c +++ b/src/media_controller_client.c @@ -370,6 +370,37 @@ static int __mc_parse_server_name(const char *interface_name, char **server_name return MEDIA_CONTROLLER_ERROR_NONE; } +static int __send_command(mc_client_h client, const char *server_name, const char *command_id, const char *signal_name, const char *message) +{ + int ret = MEDIA_CONTROLLER_ERROR_NONE; + media_controller_client_s *mc_client = (media_controller_client_s *)client; + bool exist_server = FALSE; + char *interface_name = NULL; + + mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); + mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name"); + mc_retvm_if(!MC_STRING_VALID(command_id), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid command_id"); + mc_retvm_if(!MC_STRING_VALID(signal_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid signal_id"); + mc_retvm_if(!MC_STRING_VALID(message), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid message"); + + ret = mc_db_check_server_table_exist(mc_client->db_handle, server_name, &exist_server); + mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_db_check_server_table_exist [%d]", ret); + mc_retvm_if(exist_server == FALSE, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Error Invalid server_name"); + + ret = mc_util_set_command_available(mc_client->client_name, command_id, NULL); + mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_util_set_command_available [%d]", ret); + + interface_name = mc_util_get_interface_name(MC_SERVER, server_name); + + ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, signal_name, message, NULL); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) + mc_error("Error mc_ipc_send_message [%d]", ret); + + MC_SAFE_G_FREE(interface_name); + + return ret; +} + int mc_client_create(mc_client_h *client) { int ret = MEDIA_CONTROLLER_ERROR_NONE; @@ -1078,30 +1109,18 @@ int mc_client_send_playback_state_command(mc_client_h client, const char *server int ret = MEDIA_CONTROLLER_ERROR_NONE; char *message = NULL; media_controller_client_s *mc_client = (media_controller_client_s *)client; - bool exist_server = FALSE; mc_warning("DEPRECATION WARNING: mc_client_send_playback_state_command() is deprecated and will be removed from next release. Use mc_client_send_playback_action() instead."); mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name"); mc_retvm_if(((state < MC_PLAYBACK_STATE_PLAYING) || (state > MC_PLAYBACK_STATE_REWIND)), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "state is invalid"); - ret = mc_db_check_server_table_exist(mc_client->db_handle, server_name, &exist_server); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_db_check_server_table_exist [%d]", ret); - mc_retvm_if(exist_server == FALSE, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Error Invalid server_name"); - - ret = mc_util_set_command_available(mc_client->client_name, MC_COMMAND_PLAYBACKSTATE, NULL); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_util_set_command_available [%d]", ret); - message = g_strdup_printf("%s%s%d", mc_client->client_name, MC_STRING_DELIMITER, state); mc_retvm_if(message == NULL, MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "fail making message [%d]", ret); - char *interface_name = mc_util_get_interface_name(MC_SERVER, server_name); - ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_PLAYBACK_STATE_CMD, message, NULL); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) - mc_error("Error mc_ipc_send_message [%d]", ret); + ret = __send_command(client, server_name, MC_COMMAND_PLAYBACKSTATE, MC_DBUS_SIGNAL_NAME_PLAYBACK_STATE_CMD, message); MC_SAFE_G_FREE(message); - MC_SAFE_G_FREE(interface_name); return ret; } @@ -1111,29 +1130,17 @@ int mc_client_send_playback_action(mc_client_h client, const char *server_name, int ret = MEDIA_CONTROLLER_ERROR_NONE; char *message = NULL; media_controller_client_s *mc_client = (media_controller_client_s *)client; - bool exist_server = FALSE; mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name"); mc_retvm_if(((action < MC_PLAYBACK_ACTION_PLAY) || (action > MC_PLAYBACK_ACTION_TOGGLE_PLAY_PAUSE)), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid action"); - ret = mc_db_check_server_table_exist(mc_client->db_handle, server_name, &exist_server); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_db_check_server_table_exist [%d]", ret); - mc_retvm_if(exist_server == FALSE, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Error Invalid server_name"); - - ret = mc_util_set_command_available(mc_client->client_name, MC_COMMAND_PLAYBACKACTION, NULL); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_util_set_command_available [%d]", ret); - message = g_strdup_printf("%s%s%d", mc_client->client_name, MC_STRING_DELIMITER, action); mc_retvm_if(message == NULL, MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "fail making message [%d]", ret); - char *interface_name = mc_util_get_interface_name(MC_SERVER, server_name); - ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_PLAYBACK_ACTION, message, NULL); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) - mc_error("Error mc_ipc_send_message [%d]", ret); + ret = __send_command(client, server_name, MC_COMMAND_PLAYBACKACTION, MC_DBUS_SIGNAL_NAME_PLAYBACK_ACTION, message); MC_SAFE_G_FREE(message); - MC_SAFE_G_FREE(interface_name); return ret; } @@ -1143,28 +1150,16 @@ int mc_client_send_playback_position_command(mc_client_h client, const char *ser int ret = MEDIA_CONTROLLER_ERROR_NONE; char *message = NULL; media_controller_client_s *mc_client = (media_controller_client_s *)client; - bool exist_server = FALSE; mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name"); - ret = mc_db_check_server_table_exist(mc_client->db_handle, server_name, &exist_server); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_db_check_server_table_exist [%d]", ret); - mc_retvm_if(exist_server == FALSE, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Error Invalid server_name"); - - ret = mc_util_set_command_available(mc_client->client_name, MC_COMMAND_PLAYBACKPOSITION, NULL); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_util_set_command_available [%d]", ret); - message = g_strdup_printf("%s%s%llu", mc_client->client_name, MC_STRING_DELIMITER, position); mc_retvm_if(message == NULL, MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "fail making message [%d]", ret); - char *interface_name = mc_util_get_interface_name(MC_SERVER, server_name); - ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_PLAYBACK_POS_CMD, message, NULL); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) - mc_error("Error mc_ipc_send_message [%d]", ret); + ret = __send_command(client, server_name, MC_COMMAND_PLAYBACKPOSITION, MC_DBUS_SIGNAL_NAME_PLAYBACK_POS_CMD, message); MC_SAFE_G_FREE(message); - MC_SAFE_G_FREE(interface_name); return ret; } @@ -1174,29 +1169,17 @@ int mc_client_send_shuffle_mode_command(mc_client_h client, const char *server_n int ret = MEDIA_CONTROLLER_ERROR_NONE; char *message = NULL; media_controller_client_s *mc_client = (media_controller_client_s *)client; - bool exist_server = FALSE; mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name"); mc_retvm_if(((shuffle_mode < MC_SHUFFLE_MODE_ON) || (shuffle_mode > MC_SHUFFLE_MODE_OFF)), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "shuffle is invalid"); - ret = mc_db_check_server_table_exist(mc_client->db_handle, server_name, &exist_server); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_db_check_server_table_exist [%d]", ret); - mc_retvm_if(exist_server == FALSE, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Error Invalid server_name"); - - ret = mc_util_set_command_available(mc_client->client_name, MC_COMMAND_SHUFFLE, NULL); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_util_set_command_available [%d]", ret); - message = g_strdup_printf("%s%s%d", mc_client->client_name, MC_STRING_DELIMITER, shuffle_mode); mc_retvm_if(message == NULL, MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "fail making message [%d]", ret); - char *interface_name = mc_util_get_interface_name(MC_SERVER, server_name); - ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_SHUFFLE_CMD, message, NULL); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) - mc_error("Error mc_ipc_send_message [%d]", ret); + ret = __send_command(client, server_name, MC_COMMAND_SHUFFLE, MC_DBUS_SIGNAL_NAME_SHUFFLE_CMD, message); MC_SAFE_G_FREE(message); - MC_SAFE_G_FREE(interface_name); return ret; } @@ -1206,29 +1189,17 @@ int mc_client_send_repeat_mode_command(mc_client_h client, const char *server_na int ret = MEDIA_CONTROLLER_ERROR_NONE; char *message = NULL; media_controller_client_s *mc_client = (media_controller_client_s *)client; - bool exist_server = FALSE; mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name"); mc_retvm_if(((repeat_mode < MC_REPEAT_MODE_ON) || (repeat_mode > MC_REPEAT_MODE_ONE_MEDIA)), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "repeat is invalid [%d]", repeat_mode); - ret = mc_db_check_server_table_exist(mc_client->db_handle, server_name, &exist_server); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_db_check_server_table_exist [%d]", ret); - mc_retvm_if(exist_server == FALSE, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Error Invalid server_name"); - - ret = mc_util_set_command_available(mc_client->client_name, MC_COMMAND_REPEAT, NULL); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_util_set_command_available [%d]", ret); - message = g_strdup_printf("%s%s%d", mc_client->client_name, MC_STRING_DELIMITER, repeat_mode); mc_retvm_if(message == NULL, MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "fail making message [%d]", ret); - char *interface_name = mc_util_get_interface_name(MC_SERVER, server_name); - ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_REPEAT_CMD, message, NULL); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) - mc_error("Error mc_ipc_send_message [%d]", ret); + ret = __send_command(client, server_name, MC_COMMAND_REPEAT, MC_DBUS_SIGNAL_NAME_REPEAT_CMD, message); MC_SAFE_G_FREE(message); - MC_SAFE_G_FREE(interface_name); return ret; } @@ -1290,7 +1261,6 @@ int mc_client_send_playlist_command(mc_client_h client, const char *server_name, int ret = MEDIA_CONTROLLER_ERROR_NONE; char *message = NULL; media_controller_client_s *mc_client = (media_controller_client_s *)client; - bool exist_server = FALSE; mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name"); @@ -1298,23 +1268,12 @@ int mc_client_send_playlist_command(mc_client_h client, const char *server_name, mc_retvm_if(!MC_STRING_VALID(index), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid index"); mc_retvm_if(((action < MC_PLAYBACK_ACTION_PLAY) || (action > MC_PLAYBACK_ACTION_TOGGLE_PLAY_PAUSE)), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid action"); - ret = mc_db_check_server_table_exist(mc_client->db_handle, server_name, &exist_server); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_db_check_server_table_exist [%d]", ret); - mc_retvm_if(exist_server == FALSE, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Error Invalid server_name"); - - ret = mc_util_set_command_available(mc_client->client_name, MC_COMMAND_PLAY_PLAYLIST, NULL); - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "Error mc_util_set_command_available [%d]", ret); - message = g_strdup_printf("%s%s%s%s%s%s%d%s%llu", mc_client->client_name, MC_STRING_DELIMITER, playlist_name, MC_STRING_DELIMITER, index, MC_STRING_DELIMITER, action, MC_STRING_DELIMITER, position); mc_retvm_if(message == NULL, MEDIA_CONTROLLER_ERROR_INVALID_OPERATION, "fail making message [%d]", ret); - char *interface_name = mc_util_get_interface_name(MC_SERVER, server_name); - ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_PLAYBACK_PLAYLIST, message, NULL); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) - mc_error("Error mc_ipc_send_message [%d]", ret); + ret = __send_command(client, server_name, MC_COMMAND_PLAY_PLAYLIST, MC_DBUS_SIGNAL_NAME_PLAYBACK_PLAYLIST, message); MC_SAFE_G_FREE(message); - MC_SAFE_G_FREE(interface_name); return ret; } |