diff options
-rw-r--r-- | AUTHORS | 3 | ||||
-rw-r--r-- | CMakeLists.txt | 107 | ||||
-rw-r--r-- | LICENSE | 206 | ||||
-rw-r--r-- | include/location-appman-crypt.h | 42 | ||||
-rw-r--r-- | include/location-appman-log.h | 43 | ||||
-rw-r--r-- | include/location-appman.h | 104 | ||||
-rw-r--r-- | liblocation-appman.manifest | 5 | ||||
-rw-r--r-- | location-appman.pc.in | 15 | ||||
-rw-r--r-- | location-appman.sql | 14 | ||||
-rw-r--r-- | packaging/liblocation-appman.spec | 70 | ||||
-rw-r--r-- | src/location-appman-crypt.c | 188 | ||||
-rw-r--r-- | src/location-appman.c | 993 | ||||
-rw-r--r-- | test/CMakeLists.txt | 19 | ||||
-rw-r--r-- | test/location_appman_test.c | 536 |
14 files changed, 2345 insertions, 0 deletions
@@ -0,0 +1,3 @@ +Youngae Kang <youngae.kang@samsung.com> +Minjune Kim <sena06.kim@samsung.com> +Genie Kim <daejins.kim@samsung.com> diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..30edd2a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,107 @@ + +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(fw_name "location-appman") + +PROJECT(${fw_name}) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(DBDIR "/opt/dbspace") + +SET(INC_DIR include) +INCLUDE_DIRECTORIES(${INC_DIR}) + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_name} REQUIRED dlog sqlite3 glib-2.0 libxml-2.0 capi-appfw-app-manager) +FOREACH(flag ${${fw_name}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -Wall -Werror") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") + +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-DTARGET") +ENDIF("${ARCH}" STREQUAL "arm") + +ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") +ADD_DEFINITIONS("-DTIZEN_DEBUG") +ADD_DEFINITIONS("-DLOCATION_APPMAN_DBDIR=\"${DBDIR}\"") + +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=/usr/lib") + +aux_source_directory(src SOURCES) +ADD_LIBRARY(${fw_name} SHARED ${SOURCES}) + +SET_TARGET_PROPERTIES(${fw_name} + PROPERTIES + SOVERSION 0.1.0 + CLEAN_DIRECT_OUTPUT 1 +) + +TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS} "-lgcrypt") + +INSTALL(TARGETS ${fw_name} DESTINATION lib) +INSTALL( + DIRECTORY ${INC_DIR}/ DESTINATION include/location-appman + FILES_MATCHING + PATTERN "${INC_DIR}/location-appman.h" + PATTERN "${INC_DIR}/location-appman-crypt.h" + ) + +EXEC_PROGRAM(sqlite3 ARGS "${CMAKE_BINARY_DIR}/.location-appman.db < ${CMAKE_SOURCE_DIR}/location-appman.sql" RETURN_VALUE RET) +IF (${RET}) + MESSAGE(SEND_ERROR "sqlite3 failed") +ENDIF(${RET}) + +INSTALL(FILES ${CMAKE_BINARY_DIR}/.location-appman.db DESTINATION ${DBDIR}) +INSTALL(FILES ${CMAKE_BINARY_DIR}/.location-appman.db-journal DESTINATION ${DBDIR}) + +SET(PC_NAME ${fw_name}) +SET(PC_REQUIRED ${pc_dependents}) +SET(PC_LDFLAGS -l$"location-appman") +SET(PC_CFLAGS -I\${includedir}/location) + +CONFIGURE_FILE( + ${fw_name}.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc + @ONLY +) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION lib/pkgconfig) + +#create db +#INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/location-appman.sql DESTINATION ${ETCDIR}) + +ADD_SUBDIRECTORY(test) + +IF(UNIX) + +ADD_CUSTOM_TARGET (distclean @echo cleaning for source distribution) +ADD_CUSTOM_COMMAND( + DEPENDS clean + COMMENT "distribution clean" + COMMAND find + ARGS . + -not -name config.cmake -and \( + -name tester.c -or + -name Testing -or + -name CMakeFiles -or + -name cmake.depends -or + -name cmake.check_depends -or + -name CMakeCache.txt -or + -name cmake.check_cache -or + -name *.cmake -or + -name Makefile -or + -name core -or + -name core.* -or + -name gmon.out -or + -name install_manifest.txt -or + -name *.pc -or + -name *~ \) + | grep -v TC | xargs rm -rf + TARGET distclean + VERBATIM +) + +ENDIF(UNIX) + @@ -0,0 +1,206 @@ +Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+
+
diff --git a/include/location-appman-crypt.h b/include/location-appman-crypt.h new file mode 100644 index 0000000..b4215e8 --- /dev/null +++ b/include/location-appman-crypt.h @@ -0,0 +1,42 @@ +/* + * liblocation-appman + * + * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com> + * Genie Kim <daejins.kim@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LOCATION_APPMAN_CRYPTION_H_ +#define LOCATION_APPMAN_CRYPTION_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + LOCATION_APPMAN_CRYPT_ERROR_NONE = 0, + LOCATION_APPMAN_CRYPT_ERROR_GCRYPT = -200, + LOCATION_APPMAN_CRYPT_ERROR_INTERNAL= LOCATION_APPMAN_CRYPT_ERROR_GCRYPT | 0x01, + LOCATION_APPMAN_CRYPT_ERROR_UNKNOWN = LOCATION_APPMAN_CRYPT_ERROR_GCRYPT | 0x02, +}Location_appamn_error_last_postion; + +int location_appman_encrypt(const char *key, const char *input, char *output); +int location_appman_decrypt(const char *key, const char *input, char *output); +#ifdef __cplusplus +} +#endif + +#endif /* LOCATION_APPMAN_CRYPTION_H_ */ diff --git a/include/location-appman-log.h b/include/location-appman-log.h new file mode 100644 index 0000000..c05e420 --- /dev/null +++ b/include/location-appman-log.h @@ -0,0 +1,43 @@ +/* + * liblocation-appman + * + * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com> + * Genie Kim <daejins.kim@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LOCATION_APPMAN_LOG_H__ +#define LOCATION_APPMAN_LOG_H__ + +/** + * @file location-appman-log.h + * @brief This file contains macro functions for logging. + */ + +#include <dlog.h> +#include <libgen.h> + +#define TAG_LOCATION_APPMAN "location-appman" + +#define LOCATION_APPMAN_LOG(fmt,args...) SLOG(LOG_DEBUG, TAG_LOCATION_APPMAN, "[%-15s:%-4d]"fmt"\n", basename(__FILE__), __LINE__, ##args) +#define LOCATION_APPMAN_FUNC LOCATION_APPMAN_LOGD("FUNC: %s", __FUNCTION__) + +#define LOCATION_APPMAN_NULL_ARG_CHECK(arg) \ + if(NULL != arg) {} else \ + {LOCATION_APPMAN_LOG("%s(0x%08x)\n", "LOCATION_APPMAN_ERROR_INVALID_PARAMETER",LOCATION_APPMAN_ERROR_INVALID_PARAMETER);} + +#endif + diff --git a/include/location-appman.h b/include/location-appman.h new file mode 100644 index 0000000..032bd3f --- /dev/null +++ b/include/location-appman.h @@ -0,0 +1,104 @@ +/* + * liblocation-appman + * + * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com> + * Genie Kim <daejins.kim@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LOCATION_APPMAN_H_ +#define LOCATION_APPMAN_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @addtogroup liblocation-appman + */ + +typedef struct{ + char package[64]; + char appname[64]; + char appicon[256]; + int appid; + int installed_date; + int recently_used; + int enable; +} location_appman_s; + +typedef enum { + LOCATION_APPMAN_COLUMN_PACKAGE = 0, + LOCATION_APPMAN_COLUMN_APPICON, + LOCATION_APPMAN_COLUMN_APPID, + LOCATION_APPMAN_COLUMN_INSTALLED_DATE, + LOCATION_APPMAN_COLUMN_RECENTLY_USED, + LOCATION_APPMAN_COLUMN_ENABLE +}location_appman_column_e; + +typedef enum { + LOCATION_APPMAN_ERROR_NONE = 0, + LOCATION_APPMAN_ERROR_SQLITE_FAIL = -100, + LOCATION_APPMAN_ERROR_INVALID_PARAMETER = LOCATION_APPMAN_ERROR_SQLITE_FAIL | 0x01, + LOCATION_APPMAN_ERROR_INTERNAL = LOCATION_APPMAN_ERROR_SQLITE_FAIL | 0x02, + LOCATION_APPMAN_ERROR_UNKNOWN = LOCATION_APPMAN_ERROR_SQLITE_FAIL | 0x03, + LOCATION_APPMAN_ERROR_INVALID_XML = LOCATION_APPMAN_ERROR_SQLITE_FAIL | 0x04, +}Location_appman_error_e; + +typedef enum { + LOCATION_APPMAN_ENABLE_OFF = 0, + LOCATION_APPMAN_ENABLE_ON, + LOCATION_APPMAN_PACKAGE_NOTFOUND +}Location_appman_onoff_e; + +/** + * @} + */ + +int location_appman_check_developer_mode(void); + +int location_appman_get_total_count(int *count); +int location_appman_insert(location_appman_s *appman); +int location_appman_delete(const char *package); +int location_appman_reset(void); +int location_appman_update(location_appman_s *appman); + +// GET +int location_appman_get_app_list(location_appman_column_e sort_by, int *list_count, location_appman_s **appman); +int location_appman_get_name(const char *package, char **appname); +int location_appman_get_icon(const char *package, char **appicon); +int location_appman_get_appid(const char *package, int *appid); +int location_appman_get_installed_date(const char *package, unsigned int *installed_date); +int location_appman_get_recently_used(const char *package, unsigned int *recently_used); +int location_appman_is_enabled(const char *package, int *enable); + +// SET +int location_appman_set_icon(const char *package, char *appicon); +int location_appman_set_appid(const char *package, int appid); +int location_appman_set_installed_date(const char *pakage, unsigned int installed_date); +int location_appman_set_recently_used(const char *package, unsigned int recently_used); +int location_appman_set_on(const char *package, int onoff); + +// For manifest +int location_appamn_parse_manifest(const char *xml, location_appman_s **appman); +int location_appman_get_package_by_pid(pid_t pid, location_appman_s **appman); +int location_appman_find_package(const char *package, int *findpackage); +int location_appman_register_package(location_appman_s *appman); + +#ifdef __cplusplus +} +#endif +#endif /* LOCATION_APPMAN_H_ */ diff --git a/liblocation-appman.manifest b/liblocation-appman.manifest new file mode 100644 index 0000000..97e8c31 --- /dev/null +++ b/liblocation-appman.manifest @@ -0,0 +1,5 @@ +<manifest> + <request> + <domain name="_"/> + </request> +</manifest> diff --git a/location-appman.pc.in b/location-appman.pc.in new file mode 100644 index 0000000..3c4555a --- /dev/null +++ b/location-appman.pc.in @@ -0,0 +1,15 @@ + +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=/usr +libdir=/usr/lib +includedir=/usr/include/location-appman + +Name: @PC_NAME@ +Description: @PACKAGE_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} -llocation-appman +Cflags: -I${includedir} + diff --git a/location-appman.sql b/location-appman.sql new file mode 100644 index 0000000..6b499d6 --- /dev/null +++ b/location-appman.sql @@ -0,0 +1,14 @@ +PRAGMA journal_mode = PERSIST; + +DROP TABLE IF EXISTS "LocationApp"; +CREATE TABLE LocationApp( + package VARCHAR(64) PRIMARY KEY, + icon VARCHAR(256), + app_id INTEGER, + installed_date INTEGER, + recently_used INTEGER, + enable BOOL + ); + +INSERT INTO "LocationApp" (package, app_id, installed_date, recently_used, enable) VALUES ("org.tizen.setting", 0, 0, 0, 1); +INSERT INTO "LocationApp" (package, app_id, installed_date, recently_used, enable) VALUES ("kmcele1k0n.Internet", 0, 0, 0, 1); diff --git a/packaging/liblocation-appman.spec b/packaging/liblocation-appman.spec new file mode 100644 index 0000000..033e28b --- /dev/null +++ b/packaging/liblocation-appman.spec @@ -0,0 +1,70 @@ +%define _optdir /opt +Name: liblocation-appman +Summary: Location DB controler +Version: 0.1.6 +Release: 1 +Group: System/Libraries +License: TBD +Source0: %{name}-%{version}.tar.gz + +Requires(post): sqlite +BuildRequires: cmake +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(libxml-2.0) +BuildRequires: pkgconfig(capi-appfw-app-manager) +BuildRequires: libgcrypt-devel + +%description +Description: Location DB controller library + + +%package devel +Summary: Location DB controller (Development files) +Group: System/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +Description : Location DB controller Development Package + + +%prep +%setup -q -n %{name}-%{version} + + +%build +cmake . -DCMAKE_INSTALL_PREFIX=/usr + +# Call make instruction with smp support +make %{?jobs:-j%jobs} + +%post +/sbin/ldconfig + +chown 0:5000 /opt/dbspace/.location-appman.db* +chmod 664 /opt/dbspace/.location-appman.db* +if [ -f /usr/lib/rpm-plugins/msm.so ] +then +chsmack -a 'location_fw::db' /opt/dbspace/.location-appman.db* +fi + +%postun -p /sbin/ldconfig + +%install +rm -rf %{buildroot} +%make_install + + +%clean +rm -rf %{buildroot} + +%files +%manifest liblocation-appman.manifest +%defattr(-,root,root,-) +%{_libdir}/liblocation-appman.so* +%{_optdir}/dbspace/.location-appman.db* + +%files devel +%{_includedir}/location-appman/*.h +%{_libdir}/pkgconfig/*.pc diff --git a/src/location-appman-crypt.c b/src/location-appman-crypt.c new file mode 100644 index 0000000..5541f73 --- /dev/null +++ b/src/location-appman-crypt.c @@ -0,0 +1,188 @@ +/* + * liblocation-appman + * + * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com> + * Genie Kim <daejins.kim@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <glib.h> +#include <gcrypt.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> +#include "location-appman.h" +#include "location-appman-log.h" +#include "location-appman-crypt.h" + +#define CIPHER_ALGORITHM GCRY_CIPHER_BLOWFISH +#define CIPHER_MODE GCRY_CIPHER_MODE_ECB +#define ONE_CIPHER_ALGORITHM_MIN_KEY_LENGTH 5 +#define ONE_CIPHER_ALGORITHM_MAX_KEY_LENGTH 32 + +static const char *key_string = "LocationAppman"; + +static void *string2hex(const char *string, size_t *buff_len) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(string); + LOCATION_APPMAN_NULL_ARG_CHECK(buff_len); + + *buff_len = g_utf8_strlen(string, -1)*2+1; + unsigned char *buffer = g_malloc0_n (*buff_len, sizeof(unsigned char*)); + unsigned int idx = 0; + for (idx = 0; idx < *buff_len; idx+=2) { + char temp[3]; + if (!g_ascii_isalnum(string[idx/2])) { + break; + } + g_snprintf (temp, 3, "%x", (unsigned int)string[idx/2]); + buffer[idx] = temp[0]; + buffer[idx+1] = temp[1]; + } + buffer[*buff_len-1] = '\0'; + return buffer; +} + +static gboolean +location_appman_encrypt_decrypt(gboolean is_encrypt, const void *key_buf, size_t key_buflen, void *inbuf, void *outbuf) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(inbuf); + LOCATION_APPMAN_NULL_ARG_CHECK(outbuf); + + gpg_error_t err; + gcry_cipher_hd_t hd; + int keylen, len; + void *outblock = NULL; + void *inblock = NULL; + + len = strlen((char *)inbuf); + + err = gcry_cipher_open(&hd, CIPHER_ALGORITHM, CIPHER_MODE, 0); + if(err) { + LOCATION_APPMAN_LOG("fail to gcry_cipher_open: %s\n", gpg_strerror (err)); + return FALSE; + } + + keylen = gcry_cipher_get_algo_keylen(CIPHER_ALGORITHM); + if(!keylen) { + LOCATION_APPMAN_LOG("fail to gcry_cipher_get_algo_keylen, %d\n", keylen); + return FALSE; + } + + if(keylen < ONE_CIPHER_ALGORITHM_MIN_KEY_LENGTH || keylen > ONE_CIPHER_ALGORITHM_MAX_KEY_LENGTH) + { + LOCATION_APPMAN_LOG("keylength problem (%d)", keylen); + return FALSE; + } + + err = gcry_cipher_setkey(hd, key_buf, key_buflen); + if(err) { + //delete + LOCATION_APPMAN_LOG("fail to gcry_cipher_setkey: %s\n", gpg_strerror (err)); + gcry_cipher_close(hd); + return FALSE; + } + + inblock = gcry_xmalloc (keylen); + outblock = gcry_xmalloc (keylen); + int idx =0 ; + while(idx < len) { + size_t tlen = ((len - idx)<keylen)? len-idx : keylen; + memset(inblock, 0, keylen); + memset(outblock, 0, keylen); + if(!memcpy(inblock, inbuf+idx, tlen)) { + LOCATION_APPMAN_LOG("location_appman_encrypt_decrypt : inblock MEMCPY ERROR/n"); + break; + } + + if(is_encrypt) err = gcry_cipher_encrypt(hd, outblock, keylen, inblock, keylen); + else err = gcry_cipher_decrypt(hd, outblock, keylen, inblock, keylen); + + if(err) { + LOCATION_APPMAN_LOG("fail to gcry_cipher_encrypt/gcry_cipher_decrypt : %s\n", gpg_strerror (err)); + break; + } + if(!memcpy(outbuf+idx, outblock, keylen)) { + LOCATION_APPMAN_LOG("gcry_cipher_decrypt : outbuf MEMCPY ERROR/n"); + } + break; + } + idx += keylen; + gcry_free(inblock); + gcry_free(outblock); + return TRUE; +} + +int location_appman_encrypt(const char *key, const char *input, char *output) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(input); + void *keybuf = NULL; + char tmp_outbuf[ONE_CIPHER_ALGORITHM_MAX_KEY_LENGTH] = {0, }; + size_t key_buflen; + + if(key == NULL) + keybuf = string2hex(key_string, &key_buflen); + else + keybuf = string2hex(key, &key_buflen); + + if(NULL != keybuf) { + if(!location_appman_encrypt_decrypt(TRUE, keybuf, key_buflen, (void *)input, (void *)tmp_outbuf)) { + LOCATION_APPMAN_LOG("location_appman_encrypt : location_private_encrypt_decrypt FAIL!!!\n"); + + return LOCATION_APPMAN_CRYPT_ERROR_GCRYPT; + }else { + strcpy(output, tmp_outbuf); + } + }else { + LOCATION_APPMAN_LOG("location_appman_encrypt : keybuf is NULL\n"); + return LOCATION_APPMAN_CRYPT_ERROR_INTERNAL; + } + + if(keybuf) gcry_free (keybuf); + + return LOCATION_APPMAN_CRYPT_ERROR_NONE; +} + + +int location_appman_decrypt(const char *key, const char *input, char *output) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(input); + size_t key_buflen; + void *keybuf = NULL; + char tmp_outbuf[ONE_CIPHER_ALGORITHM_MAX_KEY_LENGTH] = {0, }; + + if(key == NULL) + keybuf = string2hex(key_string, &key_buflen); + else + keybuf = string2hex(key, &key_buflen); + + if(NULL != keybuf) { + if(!location_appman_encrypt_decrypt(FALSE, keybuf, key_buflen, (void *)input, (void *)tmp_outbuf)) { + LOCATION_APPMAN_LOG("location_appman_decrypt : location_private_encrypt_decrypt FAIL!!\n");; + return LOCATION_APPMAN_CRYPT_ERROR_GCRYPT; + }else { + strcpy(output, tmp_outbuf); + } + }else { + LOCATION_APPMAN_LOG("location_appman_decrypt : keybuf is NULL\n"); + return LOCATION_APPMAN_CRYPT_ERROR_INTERNAL; + } + + if (keybuf) gcry_free (keybuf); + + return LOCATION_APPMAN_CRYPT_ERROR_NONE; +} diff --git a/src/location-appman.c b/src/location-appman.c new file mode 100644 index 0000000..a4b816a --- /dev/null +++ b/src/location-appman.c @@ -0,0 +1,993 @@ +/* + * liblocation-appman + * + * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com> + * Genie Kim <daejins.kim@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string.h> +#include <stdlib.h> +#include <stdarg.h> +#include <glib.h> +#include <sqlite3.h> +#include <libxml/parser.h> +#include <app_manager.h> +#include "location-appman.h" +#include "location-appman-log.h" + +#define LOCATION_APPMAN_QUERY_MAX_LEN 1024 + +#define LOCATION_APPMAN_PATH_FILE ".location-appman.db" +#define LOCATION_APPMAN_PATH_DBSPACE LOCATION_APPMAN_DBDIR +#define LOCATION_APPMAN_PATH_FULL LOCATION_APPMAN_PATH_DBSPACE"/"LOCATION_APPMAN_PATH_FILE + +#define LOCATION_APPMAN_PKG_NAME "PKG_NAME" + +sqlite3 *db_handle = NULL; + +static char *__create_table = "create table if not exists LocationApp(package VARCHAR(64) PRIMARY KEY,\ + icon VARCHAR(256), app_id INTEGER, installed_date INTEGER, recently_used INTEGER, enable BOOL);"; + +static char *__default_item[] = { + "insert into LocationApp (package, app_id, installed_date, recently_used, enable) values (\"org.tizen.setting\", 0, 0, 0, 1);", + "insert into LocationApp (package, app_id, installed_date, recently_used, enable) values (\"kmcele1k0n.Internet\", 0, 0, 0, 1);", +}; + +char *location_column_name[6] = { "package", "icon", "app_id", "installed_date", "recently_used", "enable" }; + +int location_appman_check_developer_mode(void) +{ + return FALSE; +} + +void location_appman_close(void) +{ + sqlite3_close(db_handle); + db_handle = NULL; +} + +int location_appman_open(void) +{ + int rc = SQLITE_OK; + int idx = 0; + + if (NULL != db_handle) { + LOCATION_APPMAN_LOG("location_appman_open & close\n"); + location_appman_close(); + } + + rc = sqlite3_open_v2(LOCATION_APPMAN_PATH_FULL, &db_handle, SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE, NULL); + if (SQLITE_OK != rc) { + // DB Recovery + if (SQLITE_CANTOPEN == rc) { + rc = sqlite3_open_v2(LOCATION_APPMAN_PATH_FULL, &db_handle, SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); + if (SQLITE_OK == rc) { + rc = sqlite3_exec (db_handle, __create_table, NULL, NULL, NULL); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Failed to create table : Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_close (db_handle); + db_handle = NULL; + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + for (idx = 0; idx < sizeof(__default_item) / sizeof(char*); idx++) { + rc = sqlite3_exec (db_handle, __default_item[idx], NULL, NULL, NULL); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Failed to insert default item : Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_close (db_handle); + db_handle = NULL; + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + } + return LOCATION_APPMAN_ERROR_NONE; + } + } + + LOCATION_APPMAN_LOG("Failed to open : Error[%s]\n", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_query(char *dest, const char *src) +{ + if (NULL == dest || NULL == src) { + LOCATION_APPMAN_LOG("location_appman_query : NULL\n"); + return FALSE; + } + + g_strlcat(dest, src, strlen(src) + 1); + return TRUE; +} + +int location_appman_drop(void) +{ + int rc = SQLITE_OK; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + rc = location_appman_query(query, "DROP TABLE IF EXISTS LocationApp"); + if (FALSE == rc) { + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_exec(db_handle, query, NULL, NULL, NULL); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to connect to table. Error[%s]\n", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_insert(location_appman_s * appman) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(appman); + sqlite3_stmt *state; + int rc = SQLITE_OK; + int sql_param_index = 1; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + const char *tail; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + rc = location_appman_query(query, + "INSERT INTO LocationApp(package, icon, app_id, installed_date, recently_used, enable)" + "VALUES (?, ?, ?, ?, ?, ?)"); + if (FALSE == rc) { + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, &tail); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]\n", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + sqlite3_bind_text(state, sql_param_index, appman->package, strlen(appman->package), SQLITE_STATIC); + sqlite3_bind_text(state, ++sql_param_index, appman->appicon, strlen(appman->appicon), SQLITE_STATIC); + sqlite3_bind_int(state, ++sql_param_index, appman->appid); + sqlite3_bind_int(state, ++sql_param_index, appman->installed_date); + sqlite3_bind_int(state, ++sql_param_index, appman->recently_used); + sqlite3_bind_int(state, ++sql_param_index, appman->enable); + + sqlite3_step(state); + sqlite3_reset(state); + + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_delete(const char *package) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + sqlite3_stmt *state = NULL; + int rc = SQLITE_OK; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + rc = location_appman_query(query, "DELETE from LocationApp where package = ?"); + if (FALSE == rc) { + LOCATION_APPMAN_LOG("location_app_delete. Error[%d]\n", rc); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, NULL); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to connect to table. Error[%s]\n", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // bind the values + rc = sqlite3_bind_text(state, 1, package, strlen(package), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // step + rc = sqlite3_step(state); + if (SQLITE_DONE != rc) { + LOCATION_APPMAN_LOG("Fail to step. Error[%d]", rc); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_reset(void) +{ + int rc = SQLITE_OK; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + if (location_appman_drop() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + rc = location_appman_query(query, "CREATE TABLE LocationApp(\n " + "package VARCHAR(64) PRIMARY KEY,\n" + "name VARChAR(32),\n" + "icon VARCHAR(256),\n" + "app_id INTEGER,\n" "installed_date INTEGER,\n" "recently_used INTEGER,\n" "enable BOOL);"); + + if (FALSE == rc) { + LOCATION_APPMAN_LOG("location_appman_reset. Error[%d]\n", rc); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_exec(db_handle, query, NULL, NULL, NULL); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to connect to table. Error[%s]\n", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_update(location_appman_s * appman) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(appman); + sqlite3_stmt *state = NULL; + int rc = SQLITE_OK; + int sql_param_index = 1; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + rc = location_appman_query(query, + "UPDATE LocationApp SET icon = ?, app_id = ?, installed_date = ?, recently_used = ?, enable = ? where package = ? "); + if (FALSE == rc) { + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, NULL); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to connect to table. Error[%s]\n", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // bind the values + // Icon + rc = sqlite3_bind_text(state, sql_param_index++, appman->appicon, strlen(appman->appicon), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // App ID + rc = sqlite3_bind_int(state, sql_param_index++, appman->appid); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // Installed Date + rc = sqlite3_bind_int(state, sql_param_index++, appman->installed_date); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // Recently used + rc = sqlite3_bind_int(state, sql_param_index++, appman->recently_used); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // Enable + rc = sqlite3_bind_int(state, sql_param_index++, appman->enable); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // Package + rc = sqlite3_bind_text(state, sql_param_index++, appman->package, strlen(appman->package), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + // step + rc = sqlite3_step(state); + if (SQLITE_DONE != rc) { + LOCATION_APPMAN_LOG("Fail to step. Error[%d]", rc); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_total_count(int *count) +{ + sqlite3_stmt *state; + int rc = SQLITE_OK; + int num_data; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + const char *tail; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + rc = location_appman_query(query, "SELECT count(*) FROM LocationApp"); + if (FALSE == rc) { + location_appman_close(); + *count = -1; + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, &tail); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + *count = -1; + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_step(state); + if (SQLITE_ROW == rc) { + num_data = sqlite3_column_int(state, 0); + *count = num_data; + } + + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_app_list(location_appman_column_e sort_by, int *list_count, location_appman_s ** apps) +{ + sqlite3_stmt *state; + int rc = SQLITE_OK; + int column_index = 0; + int i = 0; + int count = 0; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + const char *tail; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + if (LOCATION_APPMAN_COLUMN_PACKAGE > sort_by || LOCATION_APPMAN_COLUMN_ENABLE < sort_by) { + sort_by = LOCATION_APPMAN_COLUMN_INSTALLED_DATE; + g_snprintf(query, LOCATION_APPMAN_QUERY_MAX_LEN, "SELECT * FROM LocationApp ORDER BY %s", + location_column_name[sort_by]); + } else { + g_snprintf(query, LOCATION_APPMAN_QUERY_MAX_LEN, "SELECT * FROM LocationApp ORDER BY %s", + location_column_name[sort_by]); + } + + if (LOCATION_APPMAN_COLUMN_RECENTLY_USED == sort_by || LOCATION_APPMAN_COLUMN_ENABLE == sort_by) { + + rc = location_appman_query(query, " DESC"); + if (FALSE == rc) { + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + } + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, &tail); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + location_appman_get_total_count(&count); + LOCATION_APPMAN_LOG("count [%d]", count); + *apps = (location_appman_s *) g_malloc0(sizeof(location_appman_s) * count); + + for (i = 0; i < count; i++) { + char *name; + rc = sqlite3_step(state); + g_strlcpy((*apps + i)->package, (char *)sqlite3_column_text(state, column_index++), 64); + g_strlcpy((*apps + i)->appicon, (char *)sqlite3_column_text(state, column_index++), 256); + + (*apps + i)->appid = sqlite3_column_int(state, column_index++); + (*apps + i)->installed_date = sqlite3_column_int(state, column_index++); + (*apps + i)->recently_used = sqlite3_column_int(state, column_index++); + (*apps + i)->enable = (gboolean) sqlite3_column_int(state, column_index++); + column_index = 0; + + location_appman_get_name((*apps + i)->package, &name); + g_strlcpy((*apps + i)->appname, name, 64); + + if (SQLITE_ROW != rc) { + break; + } + } + + *list_count = i; + + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +// GET +int location_appman_get_column_text(const char *package, location_appman_column_e type, char **data) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + sqlite3_stmt *state; + int rc = LOCATION_APPMAN_ERROR_NONE; + const char *tail; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + g_snprintf(query, LOCATION_APPMAN_QUERY_MAX_LEN, "SELECT %s FROM LocationApp WHERE package = ?", + location_column_name[type]); + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, &tail); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + location_appman_close(); + data = NULL; + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_bind_text(state, 1, package, strlen(package), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_step(state); + + if (SQLITE_ROW == rc) { + *data = g_strdup((char *)sqlite3_column_text(state, 0)); + } + + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_column_int(const char *package, location_appman_column_e type, unsigned int *data) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + sqlite3_stmt *state; + int rc = LOCATION_APPMAN_ERROR_NONE; + int num_data = 0; + const char *tail; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + g_snprintf(query, LOCATION_APPMAN_QUERY_MAX_LEN, "SELECT %s FROM LocationApp WHERE package = ?", + location_column_name[type]); + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, &tail); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + location_appman_close(); + data = NULL; + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_bind_text(state, 1, package, strlen(package), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_step(state); + + if (SQLITE_ROW == rc) { + num_data = sqlite3_column_int(state, 0); + *data = num_data; + } + + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_name(const char *package, char **appname) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + app_info_h app_info; + char *name = NULL; + int ret = 0; + + ret = app_manager_get_app_info(package, &app_info); + if (ret != APP_MANAGER_ERROR_NONE) { + if (package) { + LOCATION_APPMAN_LOG("Can't get application information: [%s]", package); + name = strdup (package); + } + else { + LOCATION_APPMAN_LOG("Can't get application information: [%s]", package); + return LOCATION_APPMAN_ERROR_INTERNAL; + } + } + else { + + app_info_get_name(app_info, &name); + if (name == NULL || g_strcmp0 (name, "(NULL)") == 0) { + LOCATION_APPMAN_LOG("name of application[%s] is NULL", package); + name = strdup (package); + } + } + + *appname = (char *) g_malloc0(sizeof(char)*64); + g_strlcpy(*appname, name, 64); + g_free(name); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_icon(const char *package, char **appicon) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_get_column_text(package, LOCATION_APPMAN_COLUMN_APPICON, appicon); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_appid(const char *package, int *appid) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_get_column_int(package, LOCATION_APPMAN_COLUMN_APPID, (unsigned int *)appid); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_installed_date(const char *package, unsigned int *installed_date) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_get_column_int(package, LOCATION_APPMAN_COLUMN_INSTALLED_DATE, installed_date); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_recently_used(const char *package, unsigned int *recently_used) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_get_column_int(package, LOCATION_APPMAN_COLUMN_RECENTLY_USED, recently_used); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_is_enabled(const char *package, int *enable) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = SQLITE_OK; + + rc = location_appman_get_column_int(package, LOCATION_APPMAN_COLUMN_ENABLE, (unsigned int *)enable); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + return LOCATION_APPMAN_ERROR_NONE; +} + +// SET +int location_appman_set_column_text(const char *package, location_appman_column_e type, char *data) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + sqlite3_stmt *state = NULL; + int rc = SQLITE_OK; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + g_snprintf(query, LOCATION_APPMAN_QUERY_MAX_LEN, "UPDATE LocationApp SET %s = ? WHERE package = ?", + location_column_name[type]); + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, NULL); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to connect to table. Error[%s]\n", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_bind_text(state, 1, data, strlen(data), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_bind_text(state, 2, package, strlen(package), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_step(state); + if (SQLITE_DONE != rc) { + LOCATION_APPMAN_LOG("Fail to step. rc[%d]", rc); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +// SET +int location_appman_set_column_int(const char *package, location_appman_column_e type, int data) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + sqlite3_stmt *state = NULL; + int rc = SQLITE_OK; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + g_snprintf(query, LOCATION_APPMAN_QUERY_MAX_LEN, "UPDATE LocationApp SET %s = ? WHERE package = ?", + location_column_name[type]); + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, NULL); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to connect to table. Error[%s]\n", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_bind_int(state, 1, data); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind int to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_bind_text(state, 2, package, strlen(package), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_step(state); + if (SQLITE_DONE != rc) { + LOCATION_APPMAN_LOG("Fail to step. rc[%d]", rc); + sqlite3_finalize(state); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_set_icon(const char *package, char *appicon) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + LOCATION_APPMAN_NULL_ARG_CHECK(appicon); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_set_column_text(package, LOCATION_APPMAN_COLUMN_APPICON, appicon); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_set_appid(const char *package, int appid) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_set_column_int(package, LOCATION_APPMAN_COLUMN_APPID, appid); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("Fail to bind int to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_set_installed_date(const char *package, unsigned int installed_date) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_set_column_int(package, LOCATION_APPMAN_COLUMN_INSTALLED_DATE, installed_date); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("Fail to bind int to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_set_recently_used(const char *package, unsigned int recently_used) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_set_column_int(package, LOCATION_APPMAN_COLUMN_RECENTLY_USED, recently_used); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("Fail to bind int to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_set_on(const char *package, int onoff) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + int rc = LOCATION_APPMAN_ERROR_NONE; + + rc = location_appman_set_column_int(package, LOCATION_APPMAN_COLUMN_ENABLE, onoff); + if (LOCATION_APPMAN_ERROR_NONE != rc) { + LOCATION_APPMAN_LOG("Fail to bind int to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appamn_parse_manifest(const char *xml, location_appman_s ** appman) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(xml); + + xmlDocPtr doc; + xmlNodePtr cur; + xmlChar *key; + location_appman_s *tmp; + + char *element[4] = { "Location", "Package", "Appid", "Enable" }; + + doc = xmlParseDoc(BAD_CAST(xml)); + + if (NULL == doc) { + LOCATION_APPMAN_LOG("Document not parsed successfully.\n"); + return LOCATION_APPMAN_ERROR_INVALID_XML; + } + + cur = xmlDocGetRootElement(doc); + if (NULL == cur) { + LOCATION_APPMAN_LOG("empty document\n"); + xmlFreeDoc(doc); + return LOCATION_APPMAN_ERROR_INVALID_XML; + } + + if (xmlStrcmp(cur->name, (const xmlChar *)element[0]) != 0) { + LOCATION_APPMAN_LOG("document of the wrong type, root node != LocationApp\n"); + xmlFreeDoc(doc); + return LOCATION_APPMAN_ERROR_INVALID_XML; + } + + tmp = (location_appman_s *) g_malloc0(sizeof(location_appman_s)); + cur = cur->xmlChildrenNode; + while (NULL != cur) { + if (xmlStrcmp(cur->name, (const xmlChar *)element[1]) == 0) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + + g_strlcpy(tmp->package, (char *)key, 64); + LOCATION_APPMAN_LOG("tmp->package = [%s]", tmp->package); + xmlFree(key); + } else if (xmlStrcmp(cur->name, (const xmlChar *)element[2]) == 0) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + + tmp->appid = atoi((char *)key); + LOCATION_APPMAN_LOG("tmp->appid = [%d]", tmp->appid); + xmlFree(key); + } else if (xmlStrcmp(cur->name, (const xmlChar *)element[3]) == 0) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + + tmp->enable = atoi((char *)key); + LOCATION_APPMAN_LOG("tmp->enable = [%d]", tmp->enable); + xmlFree(key); + } + cur = cur->next; + } + + *appman = tmp; + + xmlFreeDoc(doc); + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_get_package_by_pid(pid_t pid, location_appman_s ** appman) +{ + if (pid <= 0) { + LOCATION_APPMAN_LOG("Invalid PID : [%ld]", pid); + return LOCATION_APPMAN_ERROR_INVALID_PARAMETER; + } + + location_appman_s *tmp = NULL; + char *package = NULL; + int ret = 0; + + ret = app_manager_get_package(pid, &package); + if (ret != APP_MANAGER_ERROR_NONE) { + const char *pkg_name = g_getenv(LOCATION_APPMAN_PKG_NAME); + if (!pkg_name) { + LOCATION_APPMAN_LOG("Can't find package name : [%ld]", pid); + return LOCATION_APPMAN_ERROR_INVALID_PARAMETER; + } + package = g_strdup(pkg_name); + } + + tmp = (location_appman_s *) g_malloc0(sizeof(location_appman_s)); + g_strlcpy(tmp->package, package, 64); + *appman = tmp; + + g_free(package); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_find_package(const char *package, int *findpackage) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(package); + sqlite3_stmt *state; + int rc = LOCATION_APPMAN_ERROR_NONE; + int enable = 0; + const char *tail; + char query[LOCATION_APPMAN_QUERY_MAX_LEN] = { 0, }; + + if (location_appman_open() != LOCATION_APPMAN_ERROR_NONE) + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + + g_snprintf(query, LOCATION_APPMAN_QUERY_MAX_LEN, "SELECT enable FROM LocationApp WHERE package = ?"); + + rc = sqlite3_prepare_v2(db_handle, query, strlen(query), &state, &tail); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("sqlite3_prepare_v2 failed : Error[%s]", sqlite3_errmsg(db_handle)); + location_appman_close(); + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_bind_text(state, 1, package, strlen(package), SQLITE_STATIC); + if (SQLITE_OK != rc) { + LOCATION_APPMAN_LOG("Fail to bind string to query. Error[%s]\n", sqlite3_errmsg(db_handle)); + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_SQLITE_FAIL; + } + + rc = sqlite3_step(state); + if (SQLITE_ROW == rc) { + enable = sqlite3_column_int(state, 0); + if (enable == 0) { + *findpackage = LOCATION_APPMAN_ENABLE_OFF; + } else { + *findpackage = LOCATION_APPMAN_ENABLE_ON; + } + } else if (SQLITE_DONE) { + *findpackage = LOCATION_APPMAN_PACKAGE_NOTFOUND; + } + + sqlite3_finalize(state); + location_appman_close(); + + return LOCATION_APPMAN_ERROR_NONE; +} + +int location_appman_register_package(location_appman_s * appman) +{ + LOCATION_APPMAN_NULL_ARG_CHECK(appman); + LOCATION_APPMAN_NULL_ARG_CHECK(appman->package); + + app_info_h app_info; + time_t timestamp; + char *name = NULL; + int ret = 0; + + ret = app_manager_get_app_info(appman->package, &app_info); + if (ret == APP_MANAGER_ERROR_NONE) { + app_info_get_name(app_info, &name); + if (name != NULL) { + g_strlcpy(appman->appname, name, 64); + g_free(name); + } + app_info_get_icon(app_info, &name); + if (name != NULL) { + g_strlcpy(appman->appicon, name, 256); + g_free(name); + } + } else { + LOCATION_APPMAN_LOG("Can't get application information: [%s]", appman->package); + return LOCATION_APPMAN_ERROR_INTERNAL; + } + + // wait for a new api. + //if(0 == appman->appid) + // appman->appid = 0; + + time(×tamp); + appman->installed_date = timestamp; + appman->recently_used = timestamp; + + location_appman_insert(appman); + + app_info_destroy(app_info); + + return LOCATION_APPMAN_ERROR_NONE; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..36afd64 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,19 @@ + +SET(fw_test "${fw_name}-test") + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_test} REQUIRED glib-2.0) +FOREACH(flag ${${fw_test}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") + +aux_source_directory(. sources) +FOREACH(src ${sources}) + GET_FILENAME_COMPONENT(src_name ${src} NAME_WE) + MESSAGE("${src_name}") + ADD_EXECUTABLE(${src_name} ${src}) + TARGET_LINK_LIBRARIES(${src_name} ${fw_name} ${${fw_test}_LDFLAGS}) +ENDFOREACH() + diff --git a/test/location_appman_test.c b/test/location_appman_test.c new file mode 100644 index 0000000..8de5583 --- /dev/null +++ b/test/location_appman_test.c @@ -0,0 +1,536 @@ +/* + * liblocation-appman + * + * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com> + * Genie Kim <daejins.kim@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <glib.h> +#include "location-appman.h" +#include "location-appman-crypt.h" + +static GMainLoop *g_mainloop = NULL; + +int location_appman_insert_test(void) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + int i; + + printf("location_appman_insert_test.\n"); + + location_appman_s test_data[5] = {{"test1_package", "test1_name", "test1_icon", 1, 1325882464, 1325893464, TRUE}, + {"test2_package", "test2_name", "test2_icon", 2, 1325893464, 1325900001, TRUE}, + {"test3_package", "test3_name", "test3_icon", 3, 1325898464, 1325900264, FALSE}, + {"test4_package", "test4_name", "test4_icon", 4, 1325866464, 1325901464, FALSE}, + {"test5_package", "test5_name", "test5_icon", 5, 1325888464, 1325900114, TRUE}}; + + + for(i = 0; i < 5; i++) { + rc = location_appman_insert(&test_data[i]); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_app_insert. Error[%d], i = %d\n", rc, i ); + return rc; + } + } + return rc; +} + +int location_appman_update_test(void) +{ + location_appman_s test_update; + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf("location_appman_update_test.\n"); + + strcpy(test_update.package, "test2_package"); + strcpy(test_update.appname, "modi2_name"); + strcpy(test_update.appicon, "modi2_icon"); + test_update.appid = 4; + test_update.installed_date = 1325903464; + test_update.recently_used = 1325903464; + test_update.enable = FALSE; + + rc = location_appman_update(&test_update); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_update. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_delete_test() +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf("location_appman_delete_test.\n"); + + rc = location_appman_delete((const char*)"test2_package"); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_delete. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_reset_test() +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf("location_appman_reset_test.\n"); + + rc = location_appman_reset(); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_delete. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_get_apps_test_by_installed_date() +{ + location_appman_s *appmantest = NULL; + int rc = LOCATION_APPMAN_ERROR_NONE, i; + int list_count = 0; + + rc = location_appman_get_app_list(LOCATION_APPMAN_COLUMN_INSTALLED_DATE, &list_count, &appmantest); + + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "location_appman_get_apps_test. Error[%d]\n", rc); + return rc; + } + + for(i = 0; i < list_count; i++) { + printf( "%s, %s, %s, %d, installed_date = %d, %d, %d\n", appmantest[i].package, + appmantest[i].appname, + appmantest[i].appicon, + appmantest[i].appid, + appmantest[i].installed_date, + appmantest[i].recently_used, + appmantest[i].enable); + } + + for(i = 0; i < list_count; i++) { + g_slice_free(location_appman_s, (appmantest + i)); + } + + return rc; +} + +int location_appman_get_apps_test_by_recently_used() +{ + location_appman_s *appmantest = NULL; + int rc = LOCATION_APPMAN_ERROR_NONE, i; + int list_count = 0; + + rc = location_appman_get_app_list(LOCATION_APPMAN_COLUMN_RECENTLY_USED, &list_count, &appmantest); + + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "location_appman_get_apps_test. Error[%d]\n", rc); + return rc; + } + + for(i = 0; i < list_count; i++) { + printf( "%s, %s, %s, %d, %d, recently_used = %d, %d\n", appmantest[i].package, + appmantest[i].appname, + appmantest[i].appicon, + appmantest[i].appid, + appmantest[i].installed_date, + appmantest[i].recently_used, + appmantest[i].enable); + } + + for(i = 0; i < list_count; i++) { + g_slice_free(location_appman_s, (appmantest + i)); + } + + return rc; +} + +int location_appman_get_apps_test_by_default() +{ + location_appman_s *appmantest = NULL; + int rc = LOCATION_APPMAN_ERROR_NONE, i; + int list_count = 0; + + rc = location_appman_get_app_list(-1, &list_count, &appmantest); + + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "location_appman_get_apps_test. Error[%d]\n", rc); + return rc; + } + + for(i = 0; i < list_count; i++) { + printf( "%s, %s, %s, %d, installed_date = %d, %d, %d\n", appmantest[i].package, + appmantest[i].appname, + appmantest[i].appicon, + appmantest[i].appid, + appmantest[i].installed_date, + appmantest[i].recently_used, + appmantest[i].enable); + } + + for(i = 0; i < list_count; i++) { + g_slice_free(location_appman_s, (appmantest + i)); + } + + return rc; +} + +/********** GET **********/ +int location_appman_get_apps_test() +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + int total = 0; + + printf( "location_appman_get_apps_test\n"); + + rc = location_appman_get_total_count(&total); + printf( "return value = %d\n", rc); + + printf( "============== SORTING INSTALLED_DATE ==============\n"); + rc = location_appman_get_apps_test_by_installed_date(); + printf( "location_appman_get_apps_test_by_installed_date : return value = %d\n", rc); + + printf( "============== SORTING RECENTLY_USED ==============\n"); + rc = location_appman_get_apps_test_by_recently_used(); + printf( "location_appman_get_apps_test_by_recently_used : return value = %d\n", rc); + + printf( "============== SORTING DEFAULT ==============\n"); + rc = location_appman_get_apps_test_by_default(); + printf( "location_appman_get_apps_test_by_default : return value = %d\n", rc); + + return rc; +} + +int location_appman_get_name_test(const char *package, char **name) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_get_name_test\n"); + rc = location_appman_get_name(package, name); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_get_name_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_get_icon_test(const char *package, char **icon) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_get_icon_test\n"); + rc = location_appman_get_icon(package, icon); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_get_icon_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_get_appid_test(const char *package, int *appid) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_get_appid_test\n"); + rc = location_appman_get_appid(package, appid); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_get_appid_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_get_installed_date_test(const char *package, unsigned int *installed_date) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_get_installed_date_test\n"); + rc = location_appman_get_installed_date(package, installed_date); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_get_installed_date_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_get_recently_used_test(const char *package, unsigned int *recently_used) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_get_recently_used_test\n"); + rc = location_appman_get_recently_used(package, recently_used); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_get_recently_used_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_is_enabled_test(const char *package, int *enable) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_get_enable_test\n"); + rc = location_appman_is_enabled(package, enable); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_get_enable_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +/********** SET **********/ +int location_appman_set_icon_test(const char *package, char *icon) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_set_icon_test\n"); + rc = location_appman_set_icon(package, icon); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_set_icon_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_set_appid_test(const char *package, int appid) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_set_appid_test\n"); + rc = location_appman_set_appid(package, appid); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_set_appid_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_set_installed_date_test(const char *package, unsigned int installed_date) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_set_installed_date_test\n"); + rc = location_appman_set_installed_date(package, installed_date); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_set_installed_date_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_set_recently_used_test(const char *package, unsigned int recently_used) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_set_recently_used_test\n"); + rc = location_appman_set_recently_used(package, recently_used); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_set_recently_used_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_set_on_test(const char *package, int onoff) +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + + printf( "location_appman_set_enable_test\n"); + rc = location_appman_set_on(package, onoff); + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_set_enable_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_set_get_encrypt_test() +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + //int nbuf = 0; + unsigned int n2buf = 0; + unsigned int nRet = 0; + char *buf = {0, }; + char tmpstr[32] = "efff222"; + char output[32] = {0, }; + char realvalue[32] = {0, }; + char pack[64] = "test3_package"; + char key[32] = "LocationAppman"; + + + // name + printf( "location_appman_set_get_encrypt_test\n"); + rc = location_appman_get_name(pack, &buf); + printf( "location_appman_set_get_encrypt_test : rc = %d, name = %s\n", rc, buf); + + rc = location_appman_encrypt(NULL, (const char *)buf, output); + printf( "location_appman_set_get_encrypt_test : rc = %d, name's output = %s\n", rc, output); + rc = location_appman_decrypt(NULL, (const char *)output, realvalue); + printf( "location_appman_set_get_encrypt_test : rc = %d, name's realvalue = %s\n", rc, realvalue); + g_free(buf); + + //recently_used + rc = location_appman_get_recently_used_test(pack, &n2buf); + printf( "location_appman_set_get_encrypt_test recently_used = %d\n", n2buf); + + sprintf(buf, "%d", n2buf); + strcpy(tmpstr, buf); + rc = location_appman_encrypt(key, (const char*)tmpstr, output); + printf( "location_appman_set_get_encrypt_test recently_used = %s\n", output); + + rc = location_appman_decrypt(key, (const char*)output, realvalue); + nRet = (unsigned int)atof(realvalue); + printf( "location_appman_set_get_encrypt_test : rc = %d, recently's nRet = %d\n", rc, nRet); + + //location_appman_decrypt(pack, output); + + if(LOCATION_APPMAN_ERROR_NONE != rc) { + printf( "Fail to location_appman_set_encrypt_test. Error[%d]\n", rc); + return rc; + } + return rc; +} + +int location_appman_get_set_test() +{ + int rc = LOCATION_APPMAN_ERROR_NONE; + int nbuf = 0; + unsigned int n2buf = 0; + char *buf = {0, }; + char *pack = "test3_package"; + + printf( "\n\nDB SET/GET ================ location_app_get_set_test\n"); + + rc = location_appman_set_icon(pack, (char *)"set_icon"); + rc = location_appman_set_appid(pack, 12); + rc = location_appman_set_installed_date(pack, 12000); + rc = location_appman_set_recently_used(pack, 21000); + rc = location_appman_set_on(pack, TRUE); + + rc = location_appman_get_name(pack, &buf); + printf( "get name = %s\n", buf); + g_free(buf); + + rc = location_appman_get_icon(pack, &buf); + printf( "get icon = %s\n", buf); + g_free(buf); + + rc = location_appman_get_appid(pack, &nbuf); + printf( "get appid = %d\n", nbuf); + + rc = location_appman_get_installed_date(pack, &n2buf); + printf( "get installed_date = %d\n", n2buf); + + rc = location_appman_get_recently_used(pack, &n2buf); + printf( "get recently_used = %d\n", n2buf); + + rc = location_appman_is_enabled(pack, &nbuf); + printf( "get enable = %d\n", nbuf); + + return rc; +} + +int location_appman_parse_test() +{ + location_appman_s *appmantest = NULL; + int rc = LOCATION_APPMAN_ERROR_NONE; + int findpackage = 0; + int n; + + const char *xmlbuffer = "<Location>\n<Package>package1</Package>\n<Appid>1</Appid>\n<Enable>1</Enable>\n</Location>"; + + rc = location_appamn_parse_manifest(xmlbuffer, &appmantest); + + if (rc == LOCATION_APPMAN_ERROR_NONE) { + location_appman_register_package(appmantest); + } + + printf( "%s, %s, %s, %d, installed_date = %d, %d, %d\n", appmantest->package, + appmantest->appname, + appmantest->appicon, + appmantest->appid, + appmantest->installed_date, + appmantest->recently_used, + appmantest->enable); + + //location_appman_s *appman_by_pid = NULL; + //pid_t t = 0; + //t = getpid(); + //printf("pid : %d\n", t); + //n = location_appman_get_package_by_pid(t, &appman_by_pid); + + n = location_appman_find_package("pppp", &findpackage); + printf("location_appman_find_package : n = %d, findpackage = %d\n", n, findpackage); + + g_slice_free(location_appman_s, appmantest); + + return rc; +} + +void location_appman_test() +{ + int n; + + n = location_appman_reset_test(); + printf("location_appman_reset_test : %d\n", n); + + n = location_appman_insert_test(); + printf("location_appman_insert_test : %d\n", n); + + n = location_appman_update_test(); + printf("location_appman_test_update_table_all : %d\n", n); + + n = location_appman_delete_test(); + printf("location_appman_delete_test : %d\n", n); + + n = location_appman_get_apps_test(); + printf("location_appman_test_update_table_all : %d\n", n); + + n = location_appman_get_set_test(); + printf("location_appman_get_set_test : %d\n", n); + + n = location_appman_parse_test(); + printf("location_appman_xml_parser_test : %d\n", n); + + /* + n = location_appman_set_get_encrypt_test(); + printf("location_appman_get_set_test : %d\n", n); + */ +} + +static gboolean exit_program(gpointer data) +{ + g_main_loop_quit(g_mainloop); + printf("Quit g_main_loop\n"); + return FALSE; +} + +int main(int argc, char **argv) +{ + g_setenv("PKG_NAME", "com.samsung.location-appman-test", 1); + g_mainloop = g_main_loop_new(NULL, 0); + location_appman_test(); + g_timeout_add_seconds(30, exit_program, NULL); + g_main_loop_run(g_mainloop); + return 0; +} + |