From f02143fb1d1dc4b1d809aedeac06287ed19e05b1 Mon Sep 17 00:00:00 2001 From: Sung-jae Park Date: Thu, 20 Dec 2012 17:15:02 +0900 Subject: [PATCH] Initialize the project. Change-Id: I2c7c9fa03496479511eff82e694327adedeec7ab --- CMakeLists.txt | 62 ++ LICENSE | 83 ++ include/debug.h | 27 + include/dlist.h | 43 + include/livebox-service.h | 207 +++++ include/livebox-service_PG.h | 16 + include/util.h | 29 + liblivebox-service.manifest | 5 + livebox-service.pc.in | 11 + packaging/liblivebox-service.spec | 54 ++ src/dlist.c | 180 ++++ src/livebox-service.c | 1667 +++++++++++++++++++++++++++++++++++++ src/util.c | 299 +++++++ 13 files changed, 2683 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 include/debug.h create mode 100644 include/dlist.h create mode 100644 include/livebox-service.h create mode 100644 include/livebox-service_PG.h create mode 100644 include/util.h create mode 100644 liblivebox-service.manifest create mode 100644 livebox-service.pc.in create mode 100644 packaging/liblivebox-service.spec create mode 100644 src/dlist.c create mode 100644 src/livebox-service.c create mode 100644 src/util.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e2042d7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,62 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(livebox-service C) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "\${prefix}") +SET(PROJECT_NAME "${PROJECT_NAME}") +SET(LIBDIR "\${exec_prefix}/lib") +SET(INCLUDEDIR "\${prefix}/include/${PROJECT_NAME}") +SET(VERSION_MAJOR 0) +SET(VERSION "${VERSION_MAJOR}.0.1") + +set(CMAKE_SKIP_BUILD_RPATH true) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) + +INCLUDE(FindPkgConfig) +pkg_check_modules(pkgs REQUIRED + dlog + glib-2.0 + gio-2.0 + com-core + sqlite3 + db-util + pkgmgr + x11 + vconf +) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror -Winline -g") + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"") +ADD_DEFINITIONS("-DLOG_TAG=\"${PROJECT_NAME}\"") +ADD_DEFINITIONS("-DNDEBUG") +ADD_DEFINITIONS("-DMASTER_PKGNAME=\"org.tizen.data-provider-master\"") +ADD_DEFINITIONS("-DINFO_SOCKET=\"/opt/usr/share/live_magazine/.live.socket\"") +ADD_DEFINITIONS("-DCLIENT_SOCKET=\"/opt/usr/share/live_magazine/.client.socket\"") +ADD_DEFINITIONS("-DSLAVE_SOCKET=\"/opt/usr/share/live_magazine/.slave.socket\"") +ADD_DEFINITIONS("-DSERVICE_SOCKET=\"/opt/usr/share/live_magazine/.service.socket\"") + +ADD_LIBRARY(${PROJECT_NAME} SHARED + src/dlist.c + src/livebox-service.c + src/util.c +) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${VERSION_MAJOR}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${VERSION}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS}) + +CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${PROJECT_NAME}.pc @ONLY) +SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_NAME}.pc") + +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION lib) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION lib/pkgconfig) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/livebox-service.h DESTINATION include/${PROJECT_NAME}) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/livebox-service_PG.h DESTINATION /usr/share/doc/${PROJECT_NAME}) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/LICENSE DESTINATION /usr/share/license RENAME "lib${PROJECT_NAME}") diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..27daa90 --- /dev/null +++ b/LICENSE @@ -0,0 +1,83 @@ +Flora License + +Version 1.0, May, 2012 + +http://www.tizenopensource.org/license + +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. + +"Tizen Certified Platform" shall mean a software platform that complies with the standards set forth in the Compatibility Definition Document and passes the Compatibility Test Suite as defined from time to time by the Tizen Technical Steering Group and certified by the Tizen Association or its designated agent. + +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 solely as incorporated into a Tizen Certified Platform, 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 solely as incorporated into a Tizen Certified Platform 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 pursuant to the copyright license above, in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of this License; and +You must cause any modified files to carry prominent notices stating that You changed the files; and +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 +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 Flora License to your work + +To apply the Flora 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 Flora License, Version 1.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.tizenopensource.org/license + + + + 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/debug.h b/include/debug.h new file mode 100644 index 0000000..2d2748e --- /dev/null +++ b/include/debug.h @@ -0,0 +1,27 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.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.tizenopensource.org/license + * + * 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. + */ + +#if !defined(FLOG) +#define DbgPrint(format, arg...) LOGD("[%s/%s:%d] " format, util_basename(__FILE__), __func__, __LINE__, ##arg) +#define ErrPrint(format, arg...) LOGE("[%s/%s:%d] " format, util_basename(__FILE__), __func__, __LINE__, ##arg) +#else +extern FILE *__file_log_fp; +#define DbgPrint(format, arg...) do { fprintf(__file_log_fp, "[LOG] [%s/%s:%d] " format, util_basename(__FILE__), __func__, __LINE__, ##arg); fflush(__file_log_fp); } while (0) + +#define ErrPrint(format, arg...) do { fprintf(__file_log_fp, "[ERR] [%s/%s:%d] " format, util_basename(__FILE__), __func__, __LINE__, ##arg); fflush(__file_log_fp); } while (0) +#endif + +/* End of a file */ diff --git a/include/dlist.h b/include/dlist.h new file mode 100644 index 0000000..b4a5719 --- /dev/null +++ b/include/dlist.h @@ -0,0 +1,43 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.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.tizenopensource.org/license + * + * 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. + */ + +#define dlist_remove_data(list, data) do { \ + struct dlist *l; \ + l = dlist_find_data(list, data); \ + list = dlist_remove(list, l); \ +} while (0) + +#define dlist_foreach(list, l, data) \ + for ((l) = (list); (l) && ((data) = dlist_data(l)); (l) = dlist_next(l)) + +#define dlist_foreach_safe(list, l, n, data) \ + for ((l) = (list), (n) = dlist_next(l); \ + (l) && ((data) = dlist_data(l)); \ + (l) = (n), (n) = dlist_next(l)) + +struct dlist; + +extern struct dlist *dlist_append(struct dlist *list, void *data); +extern struct dlist *dlist_prepend(struct dlist *list, void *data); +extern struct dlist *dlist_remove(struct dlist *list, struct dlist *l); +extern struct dlist *dlist_find_data(struct dlist *list, void *data); +extern void *dlist_data(struct dlist *l); +extern struct dlist *dlist_next(struct dlist *l); +extern struct dlist *dlist_prev(struct dlist *l); +extern int dlist_count(struct dlist *l); +extern struct dlist *dlist_nth(struct dlist *l, int nth); + +/* End of a file */ diff --git a/include/livebox-service.h b/include/livebox-service.h new file mode 100644 index 0000000..f9f8383 --- /dev/null +++ b/include/livebox-service.h @@ -0,0 +1,207 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.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.tizenopensource.org/license + * + * 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 __LIVEBOX_SERVICE_H +#define __LIVEBOX_SERVICE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define NR_OF_SIZE_LIST 7 + +enum livebox_size_type { + LB_SIZE_TYPE_1x1 = 0x01, + LB_SIZE_TYPE_2x1 = 0x02, + LB_SIZE_TYPE_2x2 = 0x04, + LB_SIZE_TYPE_4x1 = 0x08, + LB_SIZE_TYPE_4x2 = 0x10, + LB_SIZE_TYPE_4x3 = 0x20, + LB_SIZE_TYPE_4x4 = 0x40, + LB_SIZE_TYPE_UNKNOWN = 0xFF, +}; + +/*! + * \param[in] type + * \param[out] width + * \param[out] height + */ +extern int livebox_service_get_size(int type, int *width, int *height); + +/*! + * \param[in] width + * \param[in] height + * \return Type of a livebox + */ +extern int livebox_service_size_type(int width, int height); + +/*! + */ +extern int livebox_service_trigger_update(const char *pkgname, const char *cluster, const char *category); + +/*! + * \brief Synchronous package list getter + * callback (appid, lbid, is_prime) + * appid == Application Package ID (pkgname) + * lbid = Livebox Package ID + * is_prime = 1 if the livebox is default one for associated application package + * \param[in] cb Callback function + * \return Count of livebox packages or errno (-EIO, -EINVAL) + */ +extern int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *lbid, int is_prime, void *data), void *data); + +/*! + * \brief Get the name(appid or pkgname) of a primary livebox. + * \param[in] appid + * \return pkgname String which is allocated on the heap + */ +extern char *livebox_service_pkgname(const char *appid); + + +/*! + * \brief Get the name of a livebox (provider name == livebox appid), you have to release the return value after use it + * \param[in] Livebox AppID + * \return Livebox AppId (Same with input) + */ +extern char *livebox_service_provider_name(const char *lbid); + +/*! + * \brief Get the setup app ID + * \param[in] Livebox AppId + * \return Setup AppId if exists or NULL + */ +extern char *livebox_service_setup_appid(const char *lbid); + +/*! + *\brief Get the application pkgname(appid) of given livebox package name, + * \param[in] pkgname + * \return appid String which is allocated on the heap + */ +extern char *livebox_service_appid(const char *pkgname); + +/*! + * \brief Internationalized name of given livebox package. + * USER must has to do "free" after using the returned string. + * \param[in] pkgid App ID of a livebox. (It must has to be a livebox package ID. not the UI-APP and the PACKAGE. + * \param[in] lang locale(en-us, ko-kr, ...), if it is NULL, function will use the system locale automatically + * \return name if it fails to get name, returns NULL, or allocated heap address. + */ +extern char *livebox_service_i18n_name(const char *pkgid, const char *lang); + +/*! + * \brief + * \param[in] pkgid livebox ID. NOT the UI-APP ID and PACKAGE ID + * \param[in] size_type + * \return path preview image path + */ +extern char *livebox_service_preview(const char *pkgid, int size_type); + +/*! + * \brief Get the default content string of the given livebox + * \param[in] pkgid Livebox ID. Not the UI-APP ID and PACKAGE ID + * \return content content string + */ +extern char *livebox_service_content(const char *pkgid); + +/*! + * \brief Internationalized icon path of given livebox package. + * USER must has to do "free" after using the returned string. + * \param[in] pkgid App ID of a livebox. (It must has to be a livebox package ID. not the UI-APP and the PACKAGE. + * \param[in] lang locale(en-us, ko-kr, ...), if it is NULL, function will use the system locale automatically + * \return name if it fails to get path of an icon, returns NULL, or allocated heap address. + */ +extern char *livebox_service_i18n_icon(const char *pkgid, const char *lang); + +/*! + * \brief Get the path of the plug-in module + * \param[in] appid Package name of a livebox + * \return path String which is allocated on the heap + */ +extern char *livebox_service_libexec(const char *appid); + +/*! + * \brief Get the "nodisplay" value + * \param[in] pkgid Livebox package id. + * \return true/false + */ +extern int livebox_service_nodisplay(const char *pkgid); + +/*! + * \brief + * \param[in] + * \return + */ +extern char *livebox_service_lb_script_path(const char *pkgid); + +/*! + * \brief + * \param[in] + * \return + */ +extern char *livebox_service_lb_script_group(const char *pkgid); + +/*! + * \brief + * \param[in] + * \return + */ +extern char *livebox_service_pd_script_path(const char *pkgid); + +/*! + * \brief + * \param[in] + * \return + */ +extern char *livebox_service_pd_script_group(const char *pkgid); + +/*! + * \brief + * \return + */ +extern int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h); + +/*! + * \brief + * \return + */ +extern int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types); + +/*! + */ +extern int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data); + +/*! + */ +extern int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data); + +/*! + * \brief + * \return + */ +extern int livebox_service_init(void); + +/*! + * \brief + * \return + */ +extern int livebox_service_fini(void); + +#ifdef __cplusplus +} +#endif + +#endif +/* End of a file */ diff --git a/include/livebox-service_PG.h b/include/livebox-service_PG.h new file mode 100644 index 0000000..62077ae --- /dev/null +++ b/include/livebox-service_PG.h @@ -0,0 +1,16 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.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.tizenopensource.org/license + * + * 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/util.h b/include/util.h new file mode 100644 index 0000000..3dd8a74 --- /dev/null +++ b/include/util.h @@ -0,0 +1,29 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.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.tizenopensource.org/license + * + * 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. + */ + +extern double util_timestamp(void); +extern const char *util_basename(const char *name); +extern unsigned long util_free_space(const char *path); +extern char *util_replace_string(const char *src, const char *pattern, const char *replace); +extern const char *util_uri_to_path(const char *uri); +extern int util_validate_livebox_package(const char *pkgname); +extern char *util_conf_get_libexec(const char *pkgname); + +#define SCHEMA_FILE "file://" +#define SCHEMA_PIXMAP "pixmap://" +#define SCHEMA_SHM "shm://" + +/* End of a file */ diff --git a/liblivebox-service.manifest b/liblivebox-service.manifest new file mode 100644 index 0000000..a76fdba --- /dev/null +++ b/liblivebox-service.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/livebox-service.pc.in b/livebox-service.pc.in new file mode 100644 index 0000000..c0abee8 --- /dev/null +++ b/livebox-service.pc.in @@ -0,0 +1,11 @@ +prefix=@PREFIX@ +exec_prefix=@EXEC_PREFIX@ +libdir=@LIBDIR@ +includedir=@INCLUDEDIR@ + +Name: livebox-service +Description: livebox service library +Version: @VERSION@ +Libs: -L${libdir} -llivebox-service +Cflags: -I${includedir} +cppflags: -I${includedir} diff --git a/packaging/liblivebox-service.spec b/packaging/liblivebox-service.spec new file mode 100644 index 0000000..c508200 --- /dev/null +++ b/packaging/liblivebox-service.spec @@ -0,0 +1,54 @@ +Name: liblivebox-service +Summary: Library for the development of a livebox service +Version: 0.2.8 +Release: 1 +Group: main/app +License: Flora License +Source0: %{name}-%{version}.tar.gz +BuildRequires: cmake, gettext-tools +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(com-core) +BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(db-util) +BuildRequires: pkgconfig(pkgmgr) +BuildRequires: pkgconfig(x11) +BuildRequires: pkgconfig(vconf) + +%description +Livebox service library + +%package devel +Summary: Files for livebox service. +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +Livebox service library (dev) + +%prep +%setup -q + +%build +cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install +mkdir -p %{buildroot}/usr/share/license + +%post + +%files -n liblivebox-service +%manifest liblivebox-service.manifest +%defattr(-,root,root,-) +/usr/lib/*.so* +/usr/share/license/* + +%files devel +%defattr(-,root,root,-) +/usr/include/livebox-service/livebox-service.h +/usr/share/doc/livebox-service/livebox-service_PG.h +/usr/lib/pkgconfig/*.pc diff --git a/src/dlist.c b/src/dlist.c new file mode 100644 index 0000000..56ff060 --- /dev/null +++ b/src/dlist.c @@ -0,0 +1,180 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.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.tizenopensource.org/license + * + * 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 +#include +#include + +#include "dlist.h" + +/*! + * \brief + * This dlist is called Modified Doubly Linked List. + * + * Noramlly, The dobule linked list contains address of previous and next element. + * This dlist also contains them, but the tail element only contains prev address. + * + * The head element's prev pointer indicates the last element. + * But the last element's next pointer indicates NIL. + * + * So we can find the last element while crawling this DList + * But we have to remember the address of the head element. + */ + +struct dlist { + struct dlist *next; + struct dlist *prev; + void *data; +}; + +struct dlist *dlist_append(struct dlist *list, void *data) +{ + struct dlist *item; + + item = malloc(sizeof(*item)); + if (!item) + return NULL; + + item->next = NULL; + item->data = data; + + if (!list) { + item->prev = item; + + list = item; + } else { + item->prev = list->prev; + item->prev->next = item; + list->prev = item; + } + + assert(!list->prev->next && "item NEXT"); + + return list; +} + +struct dlist *dlist_prepend(struct dlist *list, void *data) +{ + struct dlist *item; + + item = malloc(sizeof(*item)); + if (!item) + return NULL; + + item->data = data; + + if (!list) { + item->prev = item; + item->next = NULL; + } else { + if (list->prev->next) + list->prev->next = item; + + item->prev = list->prev; + item->next = list; + + list->prev = item; + + } + + return item; +} + +struct dlist *dlist_remove(struct dlist *list, struct dlist *l) +{ + if (!list || !l) + return NULL; + + if (l == list) + list = l->next; + else + l->prev->next = l->next; + + if (l->next) + l->next->prev = l->prev; + /*! + * \note + * If the removed entry 'l' has no next element, it is the last element. + * In this case, check the existence of the list first, + * and if the list is not empty, update the 'prev' of the list (which is a head element of the list) + * + * If we didn't care about this, the head element(list) can indicates the invalid element. + */ + else if (list) + list->prev = l->prev; + + free(l); + return list; +} + +struct dlist *dlist_find_data(struct dlist *list, void *data) +{ + struct dlist *l; + void *_data; + + dlist_foreach(list, l, _data) { + if (data == _data) + return l; + } + + return NULL; +} + +void *dlist_data(struct dlist *l) +{ + return l ? l->data : NULL; +} + +struct dlist *dlist_next(struct dlist *l) +{ + return l ? l->next : NULL; +} + +struct dlist *dlist_prev(struct dlist *l) +{ + return l ? l->prev : NULL; +} + +int dlist_count(struct dlist *l) +{ + register int i; + struct dlist *n; + void *data; + + i = 0; + dlist_foreach(l, n, data) { + i++; + } + + return i; +} + +struct dlist *dlist_nth(struct dlist *l, int nth) +{ + register int i; + struct dlist *n; + + i = 0; + for (n = l; n; n = n->next) { + if (i == nth) + return n; + i++; + } + + return NULL; +} + +/* End of a file */ diff --git a/src/livebox-service.c b/src/livebox-service.c new file mode 100644 index 0000000..292834f --- /dev/null +++ b/src/livebox-service.c @@ -0,0 +1,1667 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.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.tizenopensource.org/license + * + * 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 +#include +#include /* malloc */ +#include /* strdup, strerror */ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "dlist.h" +#include "util.h" +#include "debug.h" +#include "livebox-service.h" + +#define EAPI __attribute__((visibility("default"))) +#define DEFAULT_TIMEOUT 2.0 +#define MAX_COLUMN 80 + +static struct supported_size_list { + int w; + int h; +} SIZE_LIST[NR_OF_SIZE_LIST] = { + { 172, 172 }, /*!< 1x1 */ + { 348, 172 }, /*!< 2x1 */ + { 348, 348 }, /*!< 2x2 */ + { 700, 172 }, /*!< 4x1 */ + { 700, 348 }, /*!< 4x2 */ + { 700, 520 }, /*!< 4x3 */ + { 700, 700 }, /*!< 4x4 */ +}; + +static struct info { + sqlite3 *handle; + const char *dbfile; + const char *conf_file; + int init_count; + int res_resolved; +} s_info = { + .handle = NULL, + .dbfile = "/opt/dbspace/.livebox.db", + .conf_file = "/usr/share/data-provider-master/resolution.ini", + .init_count = 0, + .res_resolved = 0, +}; + +static inline int update_info(int width_type, int height_type, int width, int height) +{ + int idx; + + if (width_type == 1 && height_type == 1) { + DbgPrint("1x1 Updated to %dx%d\n", width, height); + idx = 0; + } else if (width_type == 2 && height_type == 1) { + DbgPrint("2x1 Updated to %dx%d\n", width, height); + idx = 1; + } else if (width_type == 2 && height_type == 2) { + DbgPrint("2x2 Updated to %dx%d\n", width, height); + idx = 2; + } else if (width_type == 4 && height_type == 1) { + DbgPrint("4x1 Updated to %dx%d\n", width, height); + idx = 3; + } else if (width_type == 4 && height_type == 2) { + DbgPrint("4x2 Updated to %dx%d\n", width, height); + idx = 4; + } else if (width_type == 4 && height_type == 3) { + DbgPrint("4x3 Updated to %dx%d\n", width, height); + idx = 5; + } else if (width_type == 4 && height_type == 4) { + DbgPrint("4x4 Updated to %dx%d\n", width, height); + idx = 6; + } else { + ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height); + return 0; + } + + SIZE_LIST[idx].w = width; + SIZE_LIST[idx].h = height; + return 1; +} + +static inline int update_from_file(void) +{ + FILE *fp; + int updated; + int width_type; + int height_type; + int width; + int height; + char buffer[MAX_COLUMN]; + int ch; + int idx; + enum status { + START = 0x0, + TYPE = 0x01, + SIZE = 0x02, + COMMENT = 0x03, + ERROR = 0x04, + EOL = 0x05, + TYPE_END = 0x06, + SIZE_START = 0x07, + } status; + + fp = fopen(s_info.conf_file, "r"); + if (!fp) { + ErrPrint("Open failed: %s\n", strerror(errno)); + return -EIO; + } + + updated = 0; + status = START; + idx = 0; + do { + ch = fgetc(fp); + + if (idx == MAX_COLUMN) { + ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN); + status = ERROR; + } + + switch (status) { + case START: + if (isspace(ch) || ch == EOF) + continue; + + if (ch == '#') { + status = COMMENT; + } else { + status = TYPE; + idx = 0; + ungetc(ch, fp); + } + break; + case TYPE: + if (isblank(ch)) { + buffer[idx] = '\0'; + status = TYPE_END; + if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) { + ErrPrint("Invalid syntax: [%s]\n", buffer); + status = ERROR; + } + break; + } else if (ch == '=') { + buffer[idx] = '\0'; + status = SIZE_START; + if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) { + ErrPrint("Invalid syntax: [%s]\n", buffer); + status = ERROR; + } + break; + } else if (ch == EOF) { + ErrPrint("Invalid Syntax\n"); + status = ERROR; + continue; + } + buffer[idx++] = ch; + break; + case TYPE_END: + if (ch == '=') + status = SIZE_START; + break; + case SIZE_START: + if (isspace(ch) || ch == EOF) + continue; + + status = SIZE; + idx = 0; + ungetc(ch, fp); + break; + case SIZE: + if (isspace(ch) || ch == EOF) { + buffer[idx] = '\0'; + status = EOL; + + if (sscanf(buffer, "%dx%d", &width, &height) != 2) { + ErrPrint("Invalid syntax: [%s]\n", buffer); + status = ERROR; + } else if (ch == EOF) { + updated += update_info(width_type, height_type, width, height); + } + break; + } + buffer[idx++] = ch; + break; + case EOL: + updated += update_info(width_type, height_type, width, height); + status = START; + ungetc(ch, fp); + break; + case ERROR: + if (ch == '\n' || ch == '\r' || ch == '\f') + status = START; + break; + case COMMENT: + if (ch == '\n' || ch == '\r' || ch == '\f') + status = START; + break; + default: + ErrPrint("Unknown status. couldn't be reach to here\n"); + break; + } + } while (!feof(fp)); + fclose(fp); + + return NR_OF_SIZE_LIST - updated; +} + +static int update_resolution(void) +{ + Display *disp; + Window root; + Window dummy; + int x, y; + unsigned int width; + unsigned int height; + unsigned int border; + unsigned int depth; + register int i; + + if (s_info.res_resolved) + return 0; + + disp = XOpenDisplay(NULL); + if (!disp) { + ErrPrint("Failed to open a display\n"); + return -EFAULT; + } + + root = XDefaultRootWindow(disp); + if (!XGetGeometry(disp, root, &dummy, &x, &y, &width, &height, &border, &depth)) { + XCloseDisplay(disp); + return -EFAULT; + } + + if (update_from_file() == 0) + DbgPrint("Resolution info is all updated by file\n"); + + DbgPrint("Screen resolution: %dx%d\n", width, height); + for (i = 0; i < NR_OF_SIZE_LIST; i++) { + SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f); + SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)height / 1280.0f); + DbgPrint("(Ratio)Size is updated [%d] %dx%d\n", i, SIZE_LIST[i].w, SIZE_LIST[i].h); + } + + XCloseDisplay(disp); + s_info.res_resolved = 1; + return 0; +} + +static inline sqlite3 *open_db(void) +{ + sqlite3 *handle; + + if (!s_info.handle) { + int ret; + + ret = db_util_open(s_info.dbfile, &handle, DB_UTIL_REGISTER_HOOK_METHOD); + if (ret != SQLITE_OK) { + ErrPrint("Failed to open a DB\n"); + return NULL; + } + } else { + handle = s_info.handle; + } + + return handle; +} + +static inline void close_db(sqlite3 *handle) +{ + if (!s_info.handle) + db_util_close(handle); +} + +static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height) +{ + int idx; + + switch (type) { + case LB_SIZE_TYPE_1x1: /*!< 172x172 */ + idx = 0; + break; + case LB_SIZE_TYPE_2x1: /*!< 348x172 */ + idx = 1; + break; + case LB_SIZE_TYPE_2x2: /*!< 348x348 */ + idx = 2; + break; + case LB_SIZE_TYPE_4x1: /*!< 700x172 */ + idx = 3; + break; + case LB_SIZE_TYPE_4x2: /*!< 700x348 */ + idx = 4; + break; + case LB_SIZE_TYPE_4x3: /*!< 700x520 */ + idx = 5; + break; + case LB_SIZE_TYPE_4x4: /*!< 700x700 */ + idx = 6; + break; + default: + return -EINVAL; + } + + if (update_resolution() < 0) + ErrPrint("Failed to update resolution\n"); + + *width = SIZE_LIST[idx].w; + *height = SIZE_LIST[idx].h; + return 0; +} + +EAPI int livebox_service_trigger_update(const char *pkgname, const char *cluster, const char *category) +{ + struct packet *packet; + struct packet *result; + int ret; + + if (!pkgname) { + ErrPrint("Invalid argument\n"); + return -EINVAL; + } + + if (access("/tmp/.live.paused", R_OK) == 0) { + DbgPrint("Provider is paused\n"); + return -ECANCELED; + } + + if (!cluster) + cluster = "user,created"; + + if (!category) + category = "default"; + + packet = packet_create("service_update", "sss", pkgname, cluster, category); + if (!packet) { + ErrPrint("Failed to create a packet for service_update\n"); + return -EFAULT; + } + + result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT); + packet_unref(packet); + + if (result) { + if (packet_get(result, "i", &ret) != 1) { + ErrPrint("Failed to parse a result packet\n"); + ret = -EINVAL; + } + + packet_unref(result); + } else { + ErrPrint("Failed to get result packet\n"); + ret = -EFAULT; + } + + return ret; +} + +EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data) +{ + int ret; + sqlite3_stmt *stmt; + char *appid; + char *pkgid; + int is_prime; + sqlite3 *handle; + + if (!cb) + return -EINVAL; + + handle = open_db(); + if (!handle) + return -EIO; + + ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + ret = -EIO; + goto out; + } + + ret = 0; + while (sqlite3_step(stmt) == SQLITE_ROW) { + appid = (char *)sqlite3_column_text(stmt, 0); + if (!appid || !strlen(appid)) { + ErrPrint("APPID is not valid\n"); + continue; + } + + pkgid = (char *)sqlite3_column_text(stmt, 1); + if (!pkgid || !strlen(pkgid)) { + ErrPrint("pkgid is not valid\n"); + continue; + } + + is_prime = sqlite3_column_int(stmt, 2); + + ret++; + + if (cb(appid, pkgid, is_prime, data) < 0) { + DbgPrint("Callback stopped package crawling\n"); + break; + } + } + + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + +out: + close_db(handle); + return ret; +} + +EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int size; + int ret; + + if (!types || !cnt || !pkgid) + return -EINVAL; + + handle = open_db(); + if (!handle) + return -EIO; + + ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + ret = -EIO; + goto out; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + ret = -EIO; + goto out; + } + + if (*cnt > NR_OF_SIZE_LIST) + *cnt = NR_OF_SIZE_LIST; + + ret = 0; + while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) { + size = sqlite3_column_int(stmt, 0); + types[ret] = size; + ret++; + } + + *cnt = ret; + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + ret = 0; +out: + close_db(handle); + return ret; +} + +static inline char *cur_locale(void) +{ + char *language; + language = vconf_get_str(VCONFKEY_LANGSET); + if (language) { + char *ptr; + + ptr = language; + while (*ptr) { + if (*ptr == '.') { + *ptr = '\0'; + break; + } + + if (*ptr == '_') + *ptr = '-'; + + ptr++; + } + } else { + language = strdup("en-us"); + if (!language) + ErrPrint("Heap: %s\n", strerror(errno)); + } + + return language; +} + +static inline char *get_default_name(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + char *name = NULL; + int ret; + + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + return NULL; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + const char *tmp; + + tmp = (const char *)sqlite3_column_text(stmt, 0); + if (tmp && strlen(tmp)) { + name = strdup(tmp); + if (!name) + ErrPrint("Heap: %s\n", strerror(errno)); + } + } + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + return name; +} + +static inline char *get_default_icon(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + char *icon = NULL; + int ret; + + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + return NULL; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + const char *tmp; + + tmp = (const char *)sqlite3_column_text(stmt, 0); + if (tmp && strlen(tmp)) { + icon = strdup(tmp); + if (!icon) + ErrPrint("Heap: %s\n", strerror(errno)); + } + } + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + return icon; +} + +EAPI char *livebox_service_content(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + char *content = NULL; + int ret; + + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + return NULL; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + const char *tmp; + + tmp = (const char *)sqlite3_column_text(stmt, 0); + if (tmp && strlen(tmp)) { + content = strdup(tmp); + if (!content) + ErrPrint("Heap: %s\n", strerror(errno)); + } + } + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + return content; +} + +EAPI char *livebox_service_setup_appid(const char *lbid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int ret; + char *appid; + + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + return NULL; + } + + appid = NULL; + ret = sqlite3_bind_text(stmt, 1, lbid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + const char *tmp; + + tmp = (const char *)sqlite3_column_text(stmt, 0); + if (!tmp || !strlen(tmp)) + goto out; + + appid = strdup(tmp); + if (!appid) + ErrPrint("Error: %s\n", strerror(errno)); + } + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + return appid; +} + +EAPI int livebox_service_nodisplay(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int ret; + + handle = open_db(); + if (!handle) + return 0; + + ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + return 0; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + ret = 0; + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) + ret = !!sqlite3_column_int(stmt, 0); + else + ret = 0; + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + return ret; +} + +EAPI char *livebox_service_preview(const char *pkgid, int size_type) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int ret; + char *preview = NULL; + + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + return NULL; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_int(stmt, 2, size_type); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + const char *tmp; + tmp = (const char *)sqlite3_column_text(stmt, 0); + if (tmp && strlen(tmp)) { + preview = strdup(tmp); + if (!preview) + ErrPrint("Heap: %s\n", strerror(errno)); + } + } + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + return preview; +} + +EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + char *language; + char *icon = NULL; + int ret; + + if (lang) { + language = strdup(lang); + if (!language) { + ErrPrint("Heap: %s\n", strerror(errno)); + return NULL; + } + } else { + language = cur_locale(); + if (!language) + return NULL; + } + + handle = open_db(); + if (!handle) { + free(language); + return NULL; + } + + ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + free(language); + return NULL; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 2, language, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + const char *tmp; + tmp = (const char *)sqlite3_column_text(stmt, 0); + if (!tmp || !strlen(tmp)) { + icon = get_default_icon(pkgid); + } else { + icon = strdup(tmp); + if (!icon) + ErrPrint("Heap: %s\n", strerror(errno)); + } + } else { + icon = get_default_icon(pkgid); + } + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + free(language); + return icon; +} + +EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + char *language; + char *name = NULL; + int ret; + + if (lang) { + language = strdup(lang); + if (!language) { + ErrPrint("Error: %s\n", strerror(errno)); + return NULL; + } + } else { + language = cur_locale(); + if (!language) + return NULL; + } + + handle = open_db(); + if (!handle) { + free(language); + return NULL; + } + + ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + free(language); + return NULL; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 2, language, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + const char *tmp; + tmp = (const char *)sqlite3_column_text(stmt, 0); + if (!tmp || !strlen(tmp)) { + name = get_default_name(pkgid); + } else { + name = strdup(tmp); + if (!name) + ErrPrint("Heap: %s\n", strerror(errno)); + } + } else { + name = get_default_name(pkgid); + } + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + free(language); + return name; +} + +EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int size; + int ret; + + if (!w || !h || !cnt || !pkgid) + return -EINVAL; + + handle = open_db(); + if (!handle) + return -EIO; + + ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + ret = -EIO; + goto out; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + ret = -EIO; + goto out; + } + + if (*cnt > NR_OF_SIZE_LIST) + *cnt = NR_OF_SIZE_LIST; + + ret = 0; + while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) { + size = sqlite3_column_int(stmt, 0); + ret += (convert_size_from_type(size, w + ret, h + ret) == 0); + } + + *cnt = ret; + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + ret = 0; +out: + close_db(handle); + return ret; +} + +EAPI char *livebox_service_libexec(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int ret; + char *libexec; + char *appid; + char *path; + + if (!pkgid) + return NULL; + + libexec = NULL; + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_finalize(stmt); + goto out; + } + + if (sqlite3_step(stmt) != SQLITE_ROW) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + + libexec = util_conf_get_libexec(pkgid); + DbgPrint("Fallback to conf checker: %s\n", libexec); + goto out; + } + + appid = (char *)sqlite3_column_text(stmt, 0); + if (!appid || !strlen(appid)) { + ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + path = (char *)sqlite3_column_text(stmt, 1); + if (!path || !strlen(path)) { + ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + libexec = strdup(path); + if (!libexec) { + ErrPrint("Heap: %s\n", strerror(errno)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + DbgPrint("libexec: %s\n", libexec); + + sqlite3_reset(stmt); + sqlite3_finalize(stmt); +out: + close_db(handle); + return libexec; +} + +static inline char *get_lb_pkgname_by_appid(const char *appid) +{ + sqlite3_stmt *stmt; + char *pkgid; + char *tmp; + sqlite3 *handle; + int ret; + + if (!appid) + return NULL; + + pkgid = NULL; + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + close_db(handle); + return NULL; + } + + ret = sqlite3_bind_text(stmt, 1, appid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 2, appid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + if (sqlite3_step(stmt) != SQLITE_ROW) { + ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid); + goto out; + } + + tmp = (char *)sqlite3_column_text(stmt, 0); + if (tmp && strlen(tmp)) { + pkgid = strdup(tmp); + if (!pkgid) + ErrPrint("Heap: %s\n", strerror(errno)); + } + +out: + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + close_db(handle); + return pkgid; +} + +EAPI char *livebox_service_pkgname(const char *appid) +{ + char *lb_pkgname; + pkgmgr_appinfo_h handle; + int ret; + char *new_appid; + + if (!appid) + return NULL; + + lb_pkgname = get_lb_pkgname_by_appid(appid); + if (lb_pkgname) + return lb_pkgname; + + /*! + * \note + * Try to get the package id using given appid + */ + ret = pkgmgr_appinfo_get_appinfo(appid, &handle); + if (ret != PKGMGR_R_OK) { + ErrPrint("Failed to get appinfo\n"); + return NULL; + } + + ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid); + if (ret != PKGMGR_R_OK) { + pkgmgr_appinfo_destroy_appinfo(handle); + ErrPrint("Failed to get pkgname for (%s)\n", appid); + return NULL; + } + + lb_pkgname = get_lb_pkgname_by_appid(new_appid); + pkgmgr_appinfo_destroy_appinfo(handle); + + if (!lb_pkgname && util_validate_livebox_package(appid) == 0) + return strdup(appid); + + return lb_pkgname; +} + +EAPI char *livebox_service_provider_name(const char *lbid) +{ + char *ret; + + if (!lbid) + return NULL; + + ret = strdup(lbid); + if (!ret) { + ErrPrint("Error: %s\n", strerror(errno)); + return NULL; + } + + return ret; +} + +EAPI char *livebox_service_appid(const char *pkgname) +{ + sqlite3_stmt *stmt; + char *appid; + char *tmp; + sqlite3 *handle; + int is_prime; + int ret; + + if (!pkgname) + return NULL; + + appid = NULL; + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 1, pkgname, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_bind_text(stmt, 2, pkgname, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + pkgmgr_appinfo_h pkg_handle; + char *new_appid; + + ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + + ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle); + if (ret != PKGMGR_R_OK) { + ErrPrint("Failed to get appinfo: %s\n", pkgname); + goto out; + } + + ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid); + if (ret != PKGMGR_R_OK) { + ErrPrint("Failed to get pkgname for (%s)\n", appid); + pkgmgr_appinfo_destroy_appinfo(pkg_handle); + goto out; + } + + appid = strdup(new_appid); + if (!appid) + ErrPrint("Heap: %s\n", strerror(errno)); + + pkgmgr_appinfo_destroy_appinfo(pkg_handle); + goto out; + } + + tmp = (char *)sqlite3_column_text(stmt, 0); + if (!tmp || !strlen(tmp)) { + ErrPrint("APPID is NIL\n"); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + appid = strdup(tmp); + if (!appid) { + ErrPrint("Heap: %s\n", strerror(errno)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + is_prime = sqlite3_column_int(stmt, 1); + + sqlite3_reset(stmt); + sqlite3_finalize(stmt); +out: + close_db(handle); + return appid; +} + +EAPI char *livebox_service_lb_script_path(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int ret; + char *path; + char *appid; + char *lb_src; + + if (!pkgid) + return NULL; + + path = NULL; + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + appid = (char *)sqlite3_column_text(stmt, 0); + if (!appid || !strlen(appid)) { + ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + lb_src = (char *)sqlite3_column_text(stmt, 1); + if (!lb_src || !strlen(lb_src)) { + ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + path = strdup(lb_src); + if (!path) { + ErrPrint("Heap: %s\n", strerror(errno)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + DbgPrint("LB Src: %s\n", path); + + sqlite3_reset(stmt); + sqlite3_finalize(stmt); +out: + close_db(handle); + return path; +} + +EAPI char *livebox_service_lb_script_group(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int ret; + char *group; + char *tmp; + + if (!pkgid) + return NULL; + + group = NULL; + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + tmp = (char *)sqlite3_column_text(stmt, 0); + if (tmp && strlen(tmp)) { + group = strdup(tmp); + if (!group) + ErrPrint("Heap: %s\n", strerror(errno)); + } + + sqlite3_reset(stmt); + sqlite3_finalize(stmt); +out: + close_db(handle); + return group; +} + +EAPI char *livebox_service_pd_script_path(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int ret; + char *path; + char *pd_src; + const char *appid; + + if (!pkgid) + return NULL; + + path = NULL; + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + appid = (char *)sqlite3_column_text(stmt, 0); + if (!appid || !strlen(appid)) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + pd_src = (char *)sqlite3_column_text(stmt, 1); + if (!pd_src || !strlen(pd_src)) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + path = strdup(pd_src); + if (!path) { + ErrPrint("Heap: %s\n", strerror(errno)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + DbgPrint("PD Src: %s\n", path); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); +out: + close_db(handle); + return path; +} + +EAPI char *livebox_service_pd_script_group(const char *pkgid) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + int ret; + char *group; + char *tmp; + + if (!pkgid) + return NULL; + + group = NULL; + handle = open_db(); + if (!handle) + return NULL; + + ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + goto out; + } + + ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_finalize(stmt); + goto out; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); + goto out; + } + + tmp = (char *)sqlite3_column_text(stmt, 0); + if (tmp && strlen(tmp)) { + group = strdup(tmp); + if (!group) + ErrPrint("Heap: %s\n", strerror(errno)); + } + sqlite3_reset(stmt); + sqlite3_finalize(stmt); +out: + close_db(handle); + return group; +} + +EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + const char *cluster; + int cnt; + int ret; + + if (!cb) + return -EINVAL; + + handle = open_db(); + if (!handle) + return -EIO; + + cnt = 0; + ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + cnt = -EIO; + goto out; + } + + while (sqlite3_step(stmt) == SQLITE_ROW) { + cluster = (const char *)sqlite3_column_text(stmt, 0); + if (!cluster || !strlen(cluster)) + continue; + + if (cb(cluster, data) < 0) + break; + + cnt++; + } + + sqlite3_reset(stmt); + sqlite3_finalize(stmt); +out: + close_db(handle); + return cnt; +} + +EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data) +{ + sqlite3_stmt *stmt; + sqlite3 *handle; + const char *category; + int cnt; + int ret; + + if (!cluster || !cb) + return -EINVAL; + + handle = open_db(); + if (!handle) + return -EIO; + + ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL); + if (ret != SQLITE_OK) { + ErrPrint("Error: %s\n", sqlite3_errmsg(handle)); + cnt = -EIO; + goto out; + } + + cnt = 0; + while (sqlite3_step(stmt) == SQLITE_ROW) { + category = (const char *)sqlite3_column_text(stmt, 0); + if (!category || !strlen(category)) + continue; + + if (cb(cluster, category, data) < 0) + break; + + cnt++; + } + + sqlite3_reset(stmt); + sqlite3_finalize(stmt); +out: + close_db(handle); + return cnt; +} + +EAPI int livebox_service_init(void) +{ + if (s_info.handle) { + DbgPrint("Already initialized\n"); + s_info.init_count++; + return 0; + } + + s_info.handle = open_db(); + if (s_info.handle) { + s_info.init_count++; + return 0; + } + + return -EIO; +} + +EAPI int livebox_service_fini(void) +{ + if (!s_info.handle || s_info.init_count <= 0) { + ErrPrint("Service is not initialized\n"); + return -EIO; + } + + s_info.init_count--; + if (s_info.init_count > 0) { + DbgPrint("Init count %d\n", s_info.init_count); + return 0; + } + + db_util_close(s_info.handle); + s_info.handle = NULL; + return 0; +} + +EAPI int livebox_service_get_size(int type, int *width, int *height) +{ + int _width; + int _height; + + if (!width) + width = &_width; + + if (!height) + height = &_height; + + return convert_size_from_type(type, width, height); +} + +EAPI int livebox_service_size_type(int width, int height) +{ + int idx; + + if (update_resolution() < 0) + ErrPrint("Failed to update the size list\n"); + + for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) { + if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height) + break; + } + + switch (idx) { + case 0: + return LB_SIZE_TYPE_1x1; + case 1: + return LB_SIZE_TYPE_2x1; + case 2: + return LB_SIZE_TYPE_2x2; + case 3: + return LB_SIZE_TYPE_4x1; + case 4: + return LB_SIZE_TYPE_4x2; + case 5: + return LB_SIZE_TYPE_4x3; + case 6: + return LB_SIZE_TYPE_4x4; + default: + break; + } + + return LB_SIZE_TYPE_UNKNOWN; +} + +/* End of a file */ diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..566dab9 --- /dev/null +++ b/src/util.c @@ -0,0 +1,299 @@ +/* + * Copyright 2012 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.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.tizenopensource.org/license + * + * 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 +#include +#include +#include +#include +#include +#include + +#include + +#include "util.h" +#include "debug.h" + +int errno; + +static inline char *check_native_livebox(const char *pkgname) +{ + int len; + char *path; + + len = strlen(pkgname) * 2; + len += strlen("/opt/usr/live/%s/libexec/liblive-%s.so"); + + path = malloc(len + 1); + if (!path) { + ErrPrint("Heap: %s\n", strerror(errno)); + return NULL; + } + + snprintf(path, len, "/opt/usr/live/%s/libexec/liblive-%s.so", pkgname, pkgname); + if (access(path, F_OK | R_OK) != 0) { + ErrPrint("%s is not a valid package\n", pkgname); + free(path); + return NULL; + } + + return path; +} + +static inline char *check_web_livebox(const char *pkgname) +{ + int len; + char *path; + + len = strlen(pkgname) * 2; + len += strlen("/opt/usr/apps/%s/res/wgt/livebox/index.html"); + + path = malloc(len + 1); + if (!path) { + ErrPrint("Heap: %s\n", strerror(errno)); + return NULL; + } + + snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname); + if (access(path, F_OK | R_OK) != 0) { + ErrPrint("%s is not a valid package\n", pkgname); + free(path); + return NULL; + } + + return path; +} + +int util_validate_livebox_package(const char *pkgname) +{ + char *path; + + if (!pkgname) { + ErrPrint("Invalid argument\n"); + return -EINVAL; + } + + path = check_native_livebox(pkgname); + if (path) { + free(path); + return 0; + } + + path = check_web_livebox(pkgname); + if (path) { + free(path); + return 0; + } + + return -EINVAL; +} + +double util_timestamp(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + + return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f; +} + +const char *util_basename(const char *name) +{ + int length; + length = name ? strlen(name) : 0; + if (!length) + return "."; + + while (--length > 0 && name[length] != '/'); + + return length <= 0 ? name : (name + length + (name[length] == '/')); +} + +unsigned long util_free_space(const char *path) +{ + struct statvfs st; + unsigned long space; + + if (statvfs(path, &st) < 0) { + ErrPrint("statvfs: %s\n", strerror(errno)); + return 0lu; + } + + space = st.f_bsize * st.f_bfree; + /*! + * \note + * Must have to check the overflow + */ + return space; +} + +char *util_replace_string(const char *src, const char *pattern, const char *replace) +{ + int s_idx; + int p_idx; + int n_idx; + int t_idx; + int r_idx; + int idx; + char *result; + int len; + int rlen; + int matched; + + if (!src || !pattern || !replace || !src[0] || !pattern[0]) { + ErrPrint("Invalid argument: %s %s %s\n", src, pattern, replace); + return NULL; + } + + rlen = strlen(replace); + len = strlen(src); + result = malloc(len); + if (!result) { + ErrPrint("Heap:%s\n", strerror(errno)); + return NULL; + } + + r_idx = 0; + idx = 0; + matched = 0; + for (s_idx = 0; src[s_idx]; s_idx++) { + if (idx == len) { + char *tmp; + + len += (rlen > len ? rlen : len); + tmp = realloc(result, len); + if (!tmp) { + ErrPrint("Heap: %s\n", strerror(errno)); + free(result); + return NULL; + } + result = tmp; + } + + if (src[s_idx] == pattern[0]) { + n_idx = -1; + t_idx = s_idx; + r_idx = idx; + + if (r_idx == len) { + char *tmp; + len += (rlen > len ? rlen : len); + tmp = realloc(result, len); + if (!tmp) { + ErrPrint("Heap: %s\n", strerror(errno)); + free(result); + return NULL; + } + result = tmp; + } + result[r_idx++] = src[t_idx++]; + p_idx = 1; + while (pattern[p_idx]) { + if (src[t_idx] == pattern[p_idx]) { + if (n_idx < 0) { + if (src[t_idx] == pattern[0]) { + n_idx = t_idx; + } else { + if (r_idx == len) { + char *tmp; + len += (rlen > len ? rlen : len); + tmp = realloc(result, len); + if (!tmp) { + ErrPrint("Heap: %s\n", strerror(errno)); + free(result); + return NULL; + } + result = tmp; + } + result[r_idx++] = src[t_idx]; + } + } + + p_idx++; + t_idx++; + continue; + } + + if (n_idx < 0) + s_idx = t_idx; + else + s_idx = n_idx; + + break; + } + + if (pattern[p_idx] == '\0') { + if (idx + rlen >= len) { + char *tmp; + len += (rlen > len ? rlen : len); + tmp = realloc(result, len); + if (!tmp) { + ErrPrint("Heap: %s\n", strerror(errno)); + free(result); + return NULL; + } + result = tmp; + matched++; + } + strcpy(result + idx, replace); + idx += strlen(replace); + s_idx = t_idx - 1; + } else { + idx = r_idx; + s_idx = (n_idx < 0 ? t_idx : n_idx) - 1; + } + } else { + result[idx++] = src[s_idx]; + } + } + + result[idx] = '\0'; + + if (!matched) { + free(result); + result = NULL; + } + + return result; +} + +const char *util_uri_to_path(const char *uri) +{ + int len; + + len = strlen(SCHEMA_FILE); + if (strncasecmp(uri, SCHEMA_FILE, len)) + return NULL; + + return uri + len; +} + +char *util_conf_get_libexec(const char *pkgname) +{ + char *path; + + if (!pkgname) { + ErrPrint("Invalid argument\n"); + return NULL; + } + + path = check_native_livebox(pkgname); + if (!path) + path = check_web_livebox(pkgname); + + return path; +} +/* End of a file */ -- 2.7.4