From 6caaf47bc76a7b7d3287395d4a30add71cf9cd7f Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Mon, 10 Apr 2017 21:44:04 +0900 Subject: [PATCH] Modified to use app history internal api Change-Id: I9bede57c4192ec01c5d49fa4d07ce80ed5d9479f Signed-off-by: Somin Kim --- CMakeLists.txt | 3 +- packaging/capi-context.spec | 1 + src/context_history.cpp | 150 ++++++++++++++++++------------------ 3 files changed, 77 insertions(+), 77 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4f1573..db9fe5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,12 +10,13 @@ FILE(GLOB_RECURSE SRCS src/*.cpp) MESSAGE("Sources: ${SRCS}") # Dependencies -SET(DEPS "gio-2.0 context-common-legacy aul bundle capi-appfw-app-control pkgmgr-info") +SET(DEPS "gio-2.0 context-common-legacy aul bundle capi-appfw-app-control pkgmgr-info context-app-history-client") # Common Options INCLUDE(FindPkgConfig) INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/include + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/context-service/private ) ADD_DEFINITIONS(-O2 -Wall -fPIC -fdata-sections -ffunction-sections -fvisibility=hidden) SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -Wl,--as-needed -Wl,--gc-sections -Wl,--print-gc-sections") diff --git a/packaging/capi-context.spec b/packaging/capi-context.spec index e3982b1..2e8102b 100644 --- a/packaging/capi-context.spec +++ b/packaging/capi-context.spec @@ -13,6 +13,7 @@ BuildRequires: pkgconfig(aul) BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(capi-appfw-app-control) BuildRequires: pkgconfig(pkgmgr-info) +BuildRequires: pkgconfig(context-app-history-client) # Backward compatibility Provides: context diff --git a/src/context_history.cpp b/src/context_history.cpp index d9864b8..c340c3b 100644 --- a/src/context_history.cpp +++ b/src/context_history.cpp @@ -15,16 +15,21 @@ */ #include -#include +#include #include -#include -#include +#include +#include +#include +#include #include #define TYPE_NUMERIC 0 #define TYPE_STRING 1 #define FILTER_KEY_LIMIT 10 +#define ASSERT_ALLOC(X) IF_FAIL_RETURN_TAG(X, E_NO_MEM, _E, "Memory allocation failed") +#define ASSERT_NOT_NULL(X) IF_FAIL_RETURN_TAG(X, E_PARAM, _E, "Parameter null") + // handles typedef struct _context_history_handle_s { /* At this point, this handle has no purpose. @@ -38,15 +43,16 @@ typedef struct _context_history_filter_handle_s { } _cx_history_filter_handle; typedef struct _context_history_list_handle_s { - ctx::Json jlist; - int current; + ctx_history_cursor_h cursor; + _context_history_list_handle_s() { - current = 0; + cursor = NULL; } } _cx_history_list_handle; typedef struct _context_history_record_handle_s { - ctx::Json jrecord; + std::vector keys; + std::shared_ptr tuple; } _cx_history_record_handle; static std::string convert_filter_to_string(context_history_filter_e filter_type); @@ -55,9 +61,10 @@ static bool check_record_key_data_type(int type, std::string key); static bool check_filter_data_int(context_history_filter_e filter_type, int val); static bool check_filter_data_string(context_history_filter_e filter_type, const char* val); static bool check_invalid_filter(context_history_data_e data_type, context_history_filter_h filter); +static int __getIndexOf(context_history_record_h record, const std::string& key); // life-cycle -SO_EXPORT int context_history_create(context_history_h* handle) +EXPORT_API int context_history_create(context_history_h* handle) { ASSERT_NOT_NULL(handle); @@ -67,7 +74,7 @@ SO_EXPORT int context_history_create(context_history_h* handle) return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_destroy(context_history_h handle) +EXPORT_API int context_history_destroy(context_history_h handle) { ASSERT_NOT_NULL(handle); delete handle; @@ -75,7 +82,7 @@ SO_EXPORT int context_history_destroy(context_history_h handle) } // Read filter manipulation -SO_EXPORT int context_history_filter_create(context_history_filter_h* filter) +EXPORT_API int context_history_filter_create(context_history_filter_h* filter) { ASSERT_NOT_NULL(filter); @@ -85,7 +92,7 @@ SO_EXPORT int context_history_filter_create(context_history_filter_h* filter) return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_filter_destroy(context_history_filter_h filter) +EXPORT_API int context_history_filter_destroy(context_history_filter_h filter) { ASSERT_NOT_NULL(filter); delete filter; @@ -93,7 +100,7 @@ SO_EXPORT int context_history_filter_destroy(context_history_filter_h filter) return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_filter_set_int(context_history_filter_h filter, context_history_filter_e filter_type, int val) +EXPORT_API int context_history_filter_set_int(context_history_filter_h filter, context_history_filter_e filter_type, int val) { ASSERT_NOT_NULL(filter); @@ -110,7 +117,7 @@ SO_EXPORT int context_history_filter_set_int(context_history_filter_h filter, co return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_filter_set_string(context_history_filter_h filter, context_history_filter_e filter_type, const char* val) +EXPORT_API int context_history_filter_set_string(context_history_filter_h filter, context_history_filter_e filter_type, const char* val) { ASSERT_NOT_NULL(filter); ASSERT_NOT_NULL(val); @@ -128,7 +135,7 @@ SO_EXPORT int context_history_filter_set_string(context_history_filter_h filter, return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_get_list(context_history_h handle, context_history_data_e data_type, context_history_filter_h filter, context_history_list_h* list) +EXPORT_API int context_history_get_list(context_history_h handle, context_history_data_e data_type, context_history_filter_h filter, context_history_list_h* list) { ASSERT_NOT_NULL(handle); ASSERT_NOT_NULL(list); @@ -145,137 +152,116 @@ SO_EXPORT int context_history_get_list(context_history_h handle, context_history if (filter) IF_FAIL_RETURN_TAG(check_invalid_filter(data_type, filter), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid filter key"); - int req_id; - ctx::Json tmp_list; - ctx::DBusClient dbusClient; + *list = new(std::nothrow) _cx_history_list_handle(); + ASSERT_ALLOC(*list); - int err = dbusClient.readSync(data_type_str, filter ? filter->jfilter : NULL, &req_id, &tmp_list); + int err = _ctx_history_query(data_type_str.c_str(), filter ? filter->jfilter.str().c_str() : "", &((*list)->cursor)); if (err == TIZEN_ERROR_NOT_SUPPORTED) err = CONTEXT_HISTORY_ERROR_NO_DATA; - IF_FAIL_RETURN_TAG(err == ERR_NONE, err, _E, "Getting list failed"); - - _J("Read response", tmp_list); - - IF_FAIL_RETURN_TAG(tmp_list.getSize(NULL, KEY_QUERY_RESULT) > 0, CONTEXT_HISTORY_ERROR_NO_DATA, _D, "No data"); - - *list = new(std::nothrow) _cx_history_list_handle(); - ASSERT_ALLOC(*list); - (*list)->jlist = tmp_list; - (*list)->current = 0; + IF_FAIL_RETURN_TAG(err == E_NONE, err, _E, "Query failed"); return CONTEXT_HISTORY_ERROR_NONE; } // Data object manipulation -SO_EXPORT int context_history_list_get_count(context_history_list_h list, int* count) +EXPORT_API int context_history_list_get_count(context_history_list_h list, int* count) { - ASSERT_NOT_NULL(list); - ASSERT_NOT_NULL(count); - *count = 0; + ASSERT_NOT_NULL(list && count); - int result = list->jlist.getSize(NULL, KEY_QUERY_RESULT); - IF_FAIL_RETURN(result > 0, CONTEXT_HISTORY_ERROR_OPERATION_FAILED); + unsigned int value; - *count = result; + int err = ctx_history_cursor_get_count(list->cursor, &value); + IF_FAIL_RETURN_TAG(err == E_NONE, err, _E, "Get count failed"); - return CONTEXT_HISTORY_ERROR_NONE; + *count = value; + return E_NONE; } -SO_EXPORT int context_history_list_get_current(context_history_list_h list, context_history_record_h* record) +EXPORT_API int context_history_list_get_current(context_history_list_h list, context_history_record_h* record) { ASSERT_NOT_NULL(list); ASSERT_NOT_NULL(record); *record = NULL; - ctx::Json tmp_record; - int error = list->jlist.getAt(NULL, KEY_QUERY_RESULT, list->current, &tmp_record); - IF_FAIL_RETURN_TAG(error, CONTEXT_HISTORY_ERROR_OPERATION_FAILED, _E, "Record load failed"); + unsigned int position = 0; + int err = ctx_history_cursor_get_position(list->cursor, &position); + IF_FAIL_RETURN_TAG(err == E_NONE, err, _E, "Get position failed"); *record = new(std::nothrow) _cx_history_record_handle(); ASSERT_ALLOC(*record); - (*record)->jrecord = tmp_record; + + (*record)->keys = list->cursor->keys; + (*record)->tuple = ctx::Tuple::duplicate(list->cursor->tuples[position]); return CONTEXT_HISTORY_ERROR_NONE; } - -SO_EXPORT int context_history_list_move_first(context_history_list_h list) +EXPORT_API int context_history_list_move_first(context_history_list_h list) { ASSERT_NOT_NULL(list); - - list->current = 0; - - return CONTEXT_HISTORY_ERROR_NONE; + return ctx_history_cursor_first(list->cursor); } -SO_EXPORT int context_history_list_move_next(context_history_list_h list) +EXPORT_API int context_history_list_move_next(context_history_list_h list) { ASSERT_NOT_NULL(list); - - IF_FAIL_RETURN_TAG(list->current+1 < list->jlist.getSize(NULL, KEY_QUERY_RESULT), CONTEXT_HISTORY_ERROR_NO_DATA, _D, "End of list"); - - list->current++; - - return CONTEXT_HISTORY_ERROR_NONE; + return ctx_history_cursor_next(list->cursor); } -SO_EXPORT int context_history_list_destroy(context_history_list_h list) +EXPORT_API int context_history_list_destroy(context_history_list_h list) { ASSERT_NOT_NULL(list); - + ctx_history_cursor_destroy(list->cursor); delete list; return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_record_get_int(context_history_record_h record, const char* key, int* val) +EXPORT_API int context_history_record_get_int(context_history_record_h record, const char* key, int* val) { ASSERT_NOT_NULL(record && val && key); // Check key and data type IF_FAIL_RETURN_TAG(check_record_key_data_type(TYPE_NUMERIC, key), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Data type mismatched"); - std::list key_list; - record->jrecord.getKeys(&key_list); - IF_FAIL_RETURN_TAG(key_list.size() > 0, CONTEXT_HISTORY_ERROR_NO_DATA, _E, "No data"); + int index = __getIndexOf(record, key); + IF_FAIL_RETURN_TAG(index >= 0, CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid record key"); - // Check invalid record key - IF_FAIL_RETURN_TAG(record->jrecord.get(NULL, key, val), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid record key"); + int64_t value; + IF_FAIL_RETURN_TAG(record->tuple->getAt(index, &value), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid data"); + + *val = value; return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_record_get_double(context_history_record_h record, const char* key, double* val) +EXPORT_API int context_history_record_get_double(context_history_record_h record, const char* key, double* val) { ASSERT_NOT_NULL(record && val && key); // Check key and data type IF_FAIL_RETURN_TAG(check_record_key_data_type(TYPE_NUMERIC, key), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Data type mismatched"); - std::list key_list; - record->jrecord.getKeys(&key_list); - IF_FAIL_RETURN_TAG(key_list.size() > 0, CONTEXT_HISTORY_ERROR_NO_DATA, _E, "No data"); + int index = __getIndexOf(record, key); + IF_FAIL_RETURN_TAG(index >= 0, CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid record key"); - // Check invalid record key - IF_FAIL_RETURN_TAG(record->jrecord.get(NULL, key, val), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid record key"); + IF_FAIL_RETURN_TAG(record->tuple->getAt(index, val), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid data"); return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_record_get_string(context_history_record_h record, const char* key, char** val) +EXPORT_API int context_history_record_get_string(context_history_record_h record, const char* key, char** val) { ASSERT_NOT_NULL(record && val && key); // Check key and data type IF_FAIL_RETURN_TAG(check_record_key_data_type(TYPE_STRING, key), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Data type mismatched"); - std::list key_list; - record->jrecord.getKeys(&key_list); - IF_FAIL_RETURN_TAG(key_list.size() > 0, CONTEXT_HISTORY_ERROR_NO_DATA, _E, "No data"); + int index = __getIndexOf(record, key); + IF_FAIL_RETURN_TAG(index >= 0, CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid record key"); - // Check Invalid record key std::string str; - IF_FAIL_RETURN_TAG(record->jrecord.get(NULL, key, &str), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Value load failed"); + IF_FAIL_RETURN_TAG(record->tuple->getAt(index, &str), CONTEXT_HISTORY_ERROR_INVALID_PARAMETER, _E, "Invalid data"); *val = g_strdup(str.c_str()); ASSERT_ALLOC(*val); @@ -283,7 +269,7 @@ SO_EXPORT int context_history_record_get_string(context_history_record_h record, return CONTEXT_HISTORY_ERROR_NONE; } -SO_EXPORT int context_history_record_destroy(context_history_record_h record) +EXPORT_API int context_history_record_destroy(context_history_record_h record) { ASSERT_NOT_NULL(record); delete record; @@ -403,7 +389,7 @@ bool check_filter_data_int(context_history_filter_e filter_type, int val) case CONTEXT_HISTORY_FILTER_RESULT_SIZE: case CONTEXT_HISTORY_FILTER_START_TIME: case CONTEXT_HISTORY_FILTER_END_TIME: - if (val > 0 ) + if (val > 0) return true; break; case CONTEXT_HISTORY_FILTER_DAY_OF_WEEK: @@ -534,3 +520,15 @@ bool check_invalid_filter(context_history_data_e data_type, context_history_filt return true; } + +int __getIndexOf(context_history_record_h record, const std::string& key) +{ + unsigned int i = 0; + + for (i = 0; i < record->keys.size(); ++i) { + if (record->keys[i] == key) { + return i; + } + } + return -1; +} -- 2.34.1