--- /dev/null
+youngsub ko <ys4610.ko at samsung dot com>
+seung taek jung <seungtaek.chung at samsung dot com>
--- /dev/null
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(badge 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(DBDIR "/opt/dbspace")
+SET(DBFILE ".${PROJECT_NAME}.db")
+SET(DBPATH "${DBDIR}/${DBFILE}")
+SET(VERSION_MAJOR 0)
+SET(VERSION "${VERSION_MAJOR}.0.1")
+
+SET(CMAKE_SKIP_BUILD_RPATH true)
+
+SET(INSTALL_HEADERS
+ badge.h
+ badge_error.h
+)
+
+SET(SRCS
+ src/badge.c
+ src/badge_internal.c
+)
+
+SET(SUBMODULES
+ badge
+)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(pkgs REQUIRED
+ aul
+ dlog
+ dbus-1
+ dbus-glib-1
+)
+
+FOREACH(flag ${pkgs_CFLAGS})
+ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -g -Wall -O2")
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+
+ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
+ADD_DEFINITIONS("-DBADGE_USE_DLOG")
+ADD_DEFINITIONS("-DDBDIR=\"${DBDIR}\"")
+ADD_DEFINITIONS("-DDBFILE=\"${DBFILE}\"")
+ADD_DEFINITIONS("-DBADGE_DB_PATH=\"${DBPATH}\"")
+
+FOREACH(lib_file ${SUBMODULES})
+ ADD_LIBRARY(${lib_file} SHARED ${SRCS})
+ TARGET_LINK_LIBRARIES(${lib_file} ${pkgs_LDFLAGS})
+ SET_TARGET_PROPERTIES(${lib_file} PROPERTIES SOVERSION ${VERSION_MAJOR})
+ SET_TARGET_PROPERTIES(${lib_file} PROPERTIES VERSION ${VERSION})
+ INSTALL(TARGETS ${lib_file} DESTINATION lib COMPONENT RuntimeLibraries)
+ENDFOREACH(lib_file)
+
+FOREACH(pcfile ${SUBMODULES})
+ CONFIGURE_FILE(${pcfile}.pc.in ${pcfile}.pc @ONLY)
+ SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${pcfile}.pc")
+ INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${pcfile}.pc DESTINATION lib/pkgconfig)
+ENDFOREACH(pcfile)
+
+FOREACH(hfile ${INSTALL_HEADERS})
+ INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/${hfile} DESTINATION include/${PROJECT_NAME})
+ENDFOREACH(hfile)
+
--- /dev/null
+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.
+
--- /dev/null
+Copyright (c) Samsung Electronics Co., Ltd. All rights reserved.
+Except as noted, this software is licensed under Apache License, Version 2.
+Please, see the LICENSE file for Apache License terms and conditions.
--- /dev/null
+<manifest>
+ <request>
+ <domain name="_"/>
+ </request>
+</manifest>
--- /dev/null
+prefix=@PREFIX@
+exec_prefix=@EXEC_PREFIX@
+libdir=@LIBDIR@
+includedir=@INCLUDEDIR@
+
+Name: badge
+Description: Badge supporting library
+Version: @VERSION@
+Requires:
+Libs: -L${libdir} -lbadge
+Cflags: -I${includedir}
--- /dev/null
+/*
+ * libbadge
+ *
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
+ * Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@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 __BADGE_DEF_H__
+#define __BADGE_DEF_H__
+
+#include <stdbool.h>
+#include <badge_error.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * @defgroup BADGE_LIBRARY Badge Library
+ * @brief This badge library to store badge information of applications.
+ */
+
+/**
+ * @ingroup BADGE_LIBRARY
+ * @defgroup BADGE_TYPE badge type
+ * @brief Badge type
+ */
+
+/**
+ * @addtogroup BADGE_TYPE
+ * @{
+ */
+
+/**
+ * @breief Enumeration for Badge action
+ */
+enum _badge_action {
+ BADGE_ACTION_CREATE = 0,
+ BADGE_ACTION_REMOVE,
+ BADGE_ACTION_UPDATE,
+ BADGE_ACTION_CHANGED_DISPLAY,
+};
+
+
+/**
+ * @brief Called to retrieve the badge existed.
+ * @param[in] pkgname The name of package
+ * @param[in] count The count of badge
+ * @param[in] user_data The user data passed from the foreach function
+ * @pre badge_foreach_existed() will invoke this callback.
+ * @see badge_foreach_existed()
+ */
+typedef void (*badge_cb)(const char *pkgname, unsigned int count, void *data);
+
+
+/**
+ * @brief Called when badge information is changed.
+ * @param[in] action The type of changing
+ * @param[in] pkgname The name of package
+ * @param[in] count The count of badge
+ * @param[in] user_data The user data passed from the callback register function
+ * @pre badge_register_changed_cb() will invoke this callback.
+ * @see badge_unregister_changed_cb()
+ */
+typedef void (*badge_change_cb)(unsigned int action, const char *pkgname,
+ unsigned int count, void *data);
+
+
+/**
+ * @ingroup BADGE_LIBRARY
+ * @defgroup BADGE badge core API
+ * @brief badge core API
+ */
+
+/**
+ * @addtogroup BADGE
+ * @{
+ */
+
+
+/**
+ * @brief This function creates badge for designated package.
+ * @details Creates new badge to display.
+ * @param[in] pkgname The name of designated package
+ * @param[in] writable_pkg The name of package which is authorized to change badge
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @par Sample code:
+ * @code
+#include <badge.h>
+...
+{
+ badge_error_e err = BADGE_ERROR_NONE;
+
+ err = badge_create("org.tizen.sms", "org.tizen.sms2");
+ if(err != BADGE_ERROR_NONE) {
+ return;
+ }
+
+}
+ * @endcode
+ */
+badge_error_e badge_create(const char *pkgname, const char *writable_pkg);
+
+
+/**
+ * @brief This function removes badge for designated package.
+ * @param[in] pkgname The name of designated package
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @par Sample code:
+ * @code
+#include <badge.h>
+...
+{
+ badge_error_e err = BADGE_ERROR_NONE;
+
+ err = badge_remove("org.tizen.sms");
+ if(err != BADGE_ERROR_NONE) {
+ return;
+ }
+
+}
+ * @endcode
+ */
+badge_error_e badge_remove(const char *pkgname);
+
+/**
+ * @brief This function sets badge count for designated package.
+ * @param[in] pkgname The name of designated package
+ * @param[in] count The count of badge
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @see badge_create()
+ * @par Sample code:
+ * @code
+#include <badge.h>
+...
+{
+ badge_error_e err = BADGE_ERROR_NONE;
+
+ err = badge_set_count("org.tizen.sms", 1);
+ if(err != BADGE_ERROR_NONE) {
+ return;
+ }
+
+}
+ * @endcode
+ */
+badge_error_e badge_set_count(const char *pkgname, unsigned int count);
+
+/**
+ * @brief This function gets badge count for designated package.
+ * @param[in] pkgname The name of designated package
+ * @param[out] count The count of badge
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @see badge_create()
+ * @see badge_set_count()
+ * @par Sample code:
+ * @code
+#include <badge.h>
+...
+{
+ badge_error_e err = BADGE_ERROR_NONE;
+
+ err = badge_get_count("org.tizen.sms", 1);
+ if(err != BADGE_ERROR_NONE) {
+ return;
+ }
+
+}
+ * @endcode
+ */
+badge_error_e badge_get_count(const char *pkgname, unsigned int *count);
+
+/**
+ * @brief This function sets displaying option for designated package.
+ * @param[in] pkgname The name of designated package
+ * @param[in] is_display The displaying option, 1 = display 0 = not display
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @see badge_create()
+ * @par Sample code:
+ * @code
+#include <badge.h>
+...
+{
+ badge_error_e err = BADGE_ERROR_NONE;
+
+ err = badge_set_display("org.tizen.sms", 1);
+ if(err != BADGE_ERROR_NONE) {
+ return;
+ }
+
+}
+ * @endcode
+ */
+badge_error_e badge_set_display(const char *pkgname, unsigned int is_display);
+
+/**
+ * @brief This function gets displaying option for designated package.
+ * @param[in] pkgname The name of designated package
+ * @param[out] is_display The displaying option, 1 = display 0 = not display
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @see badge_create()
+ * @see badge_set_count()
+ * @par Sample code:
+ * @code
+#include <badge.h>
+...
+{
+ int is_display = 0;
+ badge_error_e err = BADGE_ERROR_NONE;
+
+ err = badge_get_display("org.tizen.sms", &is_display);
+ if(err != BADGE_ERROR_NONE) {
+ return;
+ }
+
+}
+ * @endcode
+ */
+badge_error_e badge_get_display(const char *pkgname, unsigned int *is_display);
+
+/**
+ * @brief This function tests badge for designated package is existed or not.
+ * @param[in] pkgname The name of designated package
+ * @param[out] existing The bool value of badge existing status
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @see badge_create()
+ * @see badge_remove()
+ * @par Sample code:
+ * @code
+#include <badge.h>
+...
+{
+ badge_error_e err = BADGE_ERROR_NONE;
+ bool exist;
+
+ err = badge_is_existing("org.tizen.sms", &exist);
+ if(err != BADGE_ERROR_NONE) {
+ return;
+ }
+
+}
+ * @endcode
+ */
+badge_error_e badge_is_existing(const char *pkgname, bool *existing);
+
+/**
+ * @brief This function retrieves all badges which are existed.
+ * @param[in] callback The callback function
+ * @param[in] data The user data to be passed to the callback function
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @see badge_get_count()
+ * @see badge_is_existing()
+ */
+badge_error_e badge_foreach_existed(badge_cb callback, void *data);
+
+/**
+ * @brief This function registers callback function to receive badge changed event.
+ * @param[in] callback The callback function
+ * @param[in] data The user data to be passed to the callback function
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @see badge_create()
+ * @see badge_remove()
+ * @see badge_set_count()
+ */
+badge_error_e badge_register_changed_cb(badge_change_cb callback, void *data);
+
+/**
+ * @brief This function unregisters callback function to receive badge changed event.
+ * @param[in] callback The callback function
+ * @return #BADGE_ERROR_NONE if success, other value if failure
+ * @see #badge_error_e
+ * @see badge_register_changed_cb()
+ */
+badge_error_e badge_unregister_changed_cb(badge_change_cb callback);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __BADGE_DEF_H__ */
+
+
--- /dev/null
+/*
+ * libbadge
+ *
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
+ * Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@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 __BADGE_ERROR_DEF_H__
+#define __BADGE_ERROR_DEF_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @ingroup BADGE_LIBRARY
+ * @defgroup BADGE_TYPE badge type
+ * @brief Badge type
+ */
+
+/**
+ * @addtogroup BADGE_TYPE
+ * @{
+ */
+
+/**
+ * @breief Enumeration for Badge error
+ */
+typedef enum _badge_error_e {
+ BADGE_ERROR_NONE = 0, /**< Success */
+ BADGE_ERROR_INVALID_DATA = -1, /**< Invalid parameter */
+ BADGE_ERROR_NO_MEMORY = -2, /**< No memory */
+ BADGE_ERROR_FROM_DB = -3, /**< Error from DB */
+ BADGE_ERROR_ALREADY_EXIST = -4, /**< Already exist */
+ BADGE_ERROR_FROM_DBUS = -5, /**< Error from DBus */
+ BADGE_ERROR_NOT_EXIST = -6, /**< Not exist */
+ BADGE_ERROR_PERMISSION_DENIED = -7, /**< Permission denied */
+} badge_error_e;
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* __BADGE_ERROR_DEF_H__ */
+
--- /dev/null
+/*
+ * libbadge
+ *
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
+ * Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@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 __BADGE_INTERNAL_DEF_H__
+#define __BADGE_INTERNAL_DEF_H__
+
+#include <stdbool.h>
+#include <stdarg.h>
+
+#include "badge_error.h"
+#include "badge.h"
+
+#ifndef EXPORT_API
+#define EXPORT_API __attribute__ ((visibility("default")))
+#endif
+
+typedef struct _badge_h badge_h;
+
+char *_badge_get_pkgname_by_pid(void);
+
+badge_error_e _badge_is_existing(const char *pkgname, bool *existing);
+
+badge_error_e _badge_foreach_existed(badge_cb callback, void *data);
+
+badge_error_e _badge_insert(badge_h *badge);
+
+badge_error_e _badge_remove(const char *caller, const char *pkgname);
+
+badge_error_e _badget_set_count(const char *caller, const char *pkgname,
+ unsigned int count);
+
+badge_error_e _badget_get_count(const char *pkgname, unsigned int *count);
+
+badge_error_e _badget_set_display(const char *pkgname,
+ unsigned int is_display);
+
+badge_error_e _badget_get_display(const char *pkgname, unsigned int *is_display);
+
+badge_error_e _badge_register_changed_cb(badge_change_cb callback, void *data);
+
+badge_error_e _badge_unregister_changed_cb(badge_change_cb callback);
+
+badge_error_e _badge_free(badge_h *badge);
+
+badge_h *_badge_new(const char *pkgname, const char *writable_pkgs,
+ badge_error_e *err);
+
+char *_badge_pkgs_new(badge_error_e *err, const char *pkg1, ...);
+
+char *_badge_pkgs_new_valist(badge_error_e *err,
+ const char *pkg1, va_list args);
+
+#endif /* __BADGE_INTERNAL_DEF_H__ */
+
--- /dev/null
+/*
+ * libbadge
+ *
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
+ * Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@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 __BADGE_LOG_DEF_H__
+#define __BADGE_LOG_DEF_H__
+
+#ifdef BADGE_USE_DLOG
+
+#include <dlog.h>
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "libbadge"
+
+#define DBG(fmt , args...) \
+ do { \
+ LOGD("[%s : %d] "fmt"\n", __func__, __LINE__, ##args); \
+ } while (0)
+
+#define INFO(fmt , args...) \
+ do { \
+ LOGI("[%s : %d] "fmt"\n", __func__, __LINE__, ##args); \
+ } while (0)
+
+#define WARN(fmt , args...) \
+ do { \
+ LOGI("[%s : %d] "fmt"\n", __func__, __LINE__, ##args); \
+ } while (0)
+
+#define ERR(fmt , args...) \
+ do { \
+ LOGI("[%s : %d] "fmt"\n", __func__, __LINE__, ##args); \
+ } while (0)
+
+#else /* BADGE_USE_DLOG */
+
+#include <stdio.h>
+
+#define DBG(fmt , args...) \
+ do { \
+ printf("[D][%s : %d] "fmt"\n", __func__, __LINE__, ##args); \
+ } while (0)
+
+#define INFO(fmt , args...) \
+ do { \
+ printf("[I][%s : %d] "fmt"\n", __func__, __LINE__, ##args); \
+ } while (0)
+
+#define WARN(fmt , args...) \
+ do { \
+ printf("[W][%s : %d] "fmt"\n", __func__, __LINE__, ##args); \
+ } while (0)
+
+#define ERR(fmt , args...) \
+ do { \
+ printf("[E][%s : %d] "fmt"\n", __func__, __LINE__, ##args); \
+ } while (0)
+
+#endif /* BADGE_USE_DLOG */
+
+#endif /* __BADGE_LOG_DEF_H__ */
--- /dev/null
+%define DBDIR "/opt/dbspace"
+Name: badge
+Summary: badge library
+Version: 0.0.2
+Release: 2
+Group: TBD
+License: Apache-2.0
+Source0: %{name}-%{version}.tar.gz
+BuildRequires: pkgconfig(aul)
+BuildRequires: pkgconfig(dbus-1)
+BuildRequires: pkgconfig(dbus-glib-1)
+BuildRequires: pkgconfig(dlog)
+BuildRequires: cmake
+Requires(post): /sbin/ldconfig
+requires(postun): /sbin/ldconfig
+%description
+Badge library.
+
+%prep
+%setup -q
+
+%package devel
+Summary: Badge library (devel)
+Group: Development/Libraries
+Requires: %{name} = %{version}-%{release}
+
+%description devel
+Badge library (devel).
+
+%build
+export LDFLAGS+="-Wl,--rpath=%{_prefix}/lib -Wl,--as-needed"
+LDFLAGS="$LDFLAGS" cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix}
+make %{?jobs:-j%jobs}
+
+%install
+%make_install
+
+mkdir -p %{buildroot}/usr/share/license
+cp -f LICENSE.APLv2.0 %{buildroot}/usr/share/license/%{name}
+
+
+%post
+/sbin/ldconfig
+
+if [ ! -d %{DBDIR} ]
+then
+ mkdir -p %{DBDIR}
+fi
+
+if [ ! -f %{DBDIR}/.%{name}.db ]
+then
+ sqlite3 %{DBDIR}/.%{name}.db 'PRAGMA journal_mode = PERSIST;
+ create table if not exists badge_data (
+ pkgname TEXT NOT NULL,
+ writable_pkgs TEXT,
+ badge INTEGER default 0,
+ rowid INTEGER PRIMARY KEY AUTOINCREMENT,
+ UNIQUE (pkgname)
+ );
+ create table if not exists badge_option (
+ pkgname TEXT NOT NULL,
+ display INTEGER default 1,
+ UNIQUE (pkgname)
+ );
+ '
+fi
+
+chown :5000 %{DBDIR}/.%{name}.db
+chown :5000 %{DBDIR}/.%{name}.db-journal
+chmod 660 %{DBDIR}/.%{name}.db
+chmod 660 %{DBDIR}/.%{name}.db-journal
+
+%postun -p /sbin/ldconfig
+
+%files
+%manifest badge.manifest
+%defattr(-,root,root,-)
+%{_libdir}/lib%{name}.so*
+/usr/share/license/%{name}
+
+%files devel
+%defattr(-,root,root,-)
+%{_includedir}/%{name}/*.h
+%{_libdir}/pkgconfig/%{name}.pc
+
--- /dev/null
+/*
+ * libbadge
+ *
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
+ * Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@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 <stdlib.h>
+#include <stdarg.h>
+
+#include "badge.h"
+#include "badge_log.h"
+#include "badge_error.h"
+#include "badge_internal.h"
+
+EXPORT_API
+badge_error_e badge_create(const char *pkgname, const char *writable_pkg)
+{
+ badge_error_e err = BADGE_ERROR_NONE;
+ badge_h *badge = NULL;
+ char *pkgs = NULL;
+
+ if (!pkgname) {
+ WARN("package name is NULL");
+ return BADGE_ERROR_INVALID_DATA;
+ }
+
+ pkgs = _badge_pkgs_new(&err, writable_pkg, NULL);
+ if (!pkgs) {
+ ERR("fail to _badge_pkgs_new : %d", err);
+ return err;
+ }
+
+ INFO("pkgs : %s", pkgs);
+
+ badge = _badge_new(pkgname, pkgs, &err);
+ if (!badge) {
+ ERR("fail to _badge_new : %d", err);
+ free(pkgs);
+ return err;
+ }
+ free(pkgs);
+
+ err = _badge_insert(badge);
+ if (err != BADGE_ERROR_NONE) {
+ ERR("fail to _badge_insert : %d", err);
+ _badge_free(badge);
+ return err;
+ }
+
+ _badge_free(badge);
+
+ return BADGE_ERROR_NONE;
+}
+
+EXPORT_API
+badge_error_e badge_remove(const char *pkgname)
+{
+ char *caller = NULL;
+ badge_error_e result = BADGE_ERROR_NONE;
+
+ caller = _badge_get_pkgname_by_pid();
+ if (!caller) {
+ ERR("fail to get caller pkgname");
+ return BADGE_ERROR_PERMISSION_DENIED;
+
+ }
+
+ result = _badge_remove(caller, pkgname);
+ free(caller);
+
+ return result;
+}
+
+EXPORT_API
+badge_error_e badge_is_existing(const char *pkgname, bool *existing)
+{
+ return _badge_is_existing(pkgname, existing);
+}
+
+
+EXPORT_API
+badge_error_e badge_foreach_existed(badge_cb callback, void *data)
+{
+ return _badge_foreach_existed(callback, data);
+}
+
+EXPORT_API
+badge_error_e badge_set_count(const char *pkgname, unsigned int count)
+{
+ char *caller = NULL;
+ badge_error_e result = BADGE_ERROR_NONE;
+
+ caller = _badge_get_pkgname_by_pid();
+ if (!caller) {
+ ERR("fail to get caller pkgname");
+ return BADGE_ERROR_PERMISSION_DENIED;
+ }
+
+ result = _badget_set_count(caller, pkgname, count);
+
+ free(caller);
+
+ return result;
+}
+
+EXPORT_API
+badge_error_e badge_get_count(const char *pkgname, unsigned int *count)
+{
+ return _badget_get_count(pkgname, count);
+}
+
+EXPORT_API
+badge_error_e badge_set_display(const char *pkgname, unsigned int is_display)
+{
+ badge_error_e result = BADGE_ERROR_NONE;
+
+ result = _badget_set_display(pkgname, is_display);
+
+ return result;
+}
+
+EXPORT_API
+badge_error_e badge_get_display(const char *pkgname, unsigned int *is_display)
+{
+ return _badget_get_display(pkgname, is_display);
+}
+
+EXPORT_API
+badge_error_e badge_register_changed_cb(badge_change_cb callback, void *data)
+{
+ return _badge_register_changed_cb(callback, data);
+}
+
+EXPORT_API
+badge_error_e badge_unregister_changed_cb(badge_change_cb callback)
+{
+ return _badge_unregister_changed_cb(callback);
+}
+
--- /dev/null
+/*
+ * libbadge
+ *
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
+ * Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@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 <fcntl.h>
+#include <unistd.h>
+#include <glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <aul.h>
+#include <sqlite3.h>
+#include <db-util.h>
+
+#include "badge_log.h"
+#include "badge_error.h"
+#include "badge_internal.h"
+
+#define BADGE_PKGNAME_LEN 512
+#define BADGE_TABLE_NAME "badge_data"
+#define BADGE_OPTION_TABLE_NAME "badge_option"
+
+#define BADGE_CHANGED_NOTI "badge_changed"
+#define BADGE_DBUS_BUS_NAME "org.tizen.libbadge"
+#define BADGE_DBUS_PATH "/org/tizen/libbadge"
+#define BADGE_DBUS_INTERFACE "org.tizen.libbadge.signal"
+
+struct _badge_h {
+ char *pkgname;
+ char *writable_pkgs;
+};
+
+struct _badge_cb_data {
+ badge_change_cb callback;
+ void *data;
+};
+
+static GList *g_badge_cb_list;
+static DBusConnection *g_badge_cb_handle;
+
+static inline long _get_max_len(void)
+{
+ long max = 0;
+ long path_max_len = 4096;
+
+#ifdef _PC_PATH_MAX
+ max = (pathconf("/", _PC_PATH_MAX) < 1 ? path_max_len
+ : pathconf("/", _PC_PATH_MAX));
+#else /* _PC_PATH_MAX */
+ max = path_max_len;
+#endif /* _PC_PATH_MAX */
+ return max;
+}
+
+char *_badge_get_pkgname_by_pid(void)
+{
+ char *pkgname = NULL;
+ int pid = 0;
+ int ret = AUL_R_OK;
+ int fd = 0;
+ long max = 0;
+
+ pid = getpid();
+ max = _get_max_len();
+ pkgname = malloc(max);
+ if (!pkgname) {
+ ERR("fail to alloc memory");
+ return NULL;
+ }
+ memset(pkgname, 0x00, max);
+
+ ret = aul_app_get_pkgname_bypid(pid, pkgname, max);
+ if (ret != AUL_R_OK) {
+ fd = open("/proc/self/cmdline", O_RDONLY);
+ if (fd < 0) {
+ free(pkgname);
+ return NULL;
+ }
+
+ ret = read(fd, pkgname, max - 1);
+ if (ret <= 0) {
+ close(fd);
+ free(pkgname);
+ return NULL;
+ }
+
+ close(fd);
+ }
+
+ if (pkgname[0] == '\0') {
+ free(pkgname);
+ return NULL;
+ } else
+ return pkgname;
+}
+
+
+static void _badge_changed(unsigned int action, const char *pkgname,
+ unsigned int count)
+{
+ DBusConnection *connection = NULL;
+ DBusMessage *message = NULL;
+ DBusError err;
+ dbus_bool_t ret;
+
+ if (!pkgname) {
+ ERR("pkgname is NULL");
+ return;
+ }
+
+ dbus_error_init(&err);
+ connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
+ if (!connection) {
+ ERR("Fail to dbus_bus_get : %s", err.message);
+ dbus_error_free(&err);
+ return;
+ }
+
+ message = dbus_message_new_signal(BADGE_DBUS_PATH,
+ BADGE_DBUS_INTERFACE,
+ BADGE_CHANGED_NOTI);
+
+ if (!message) {
+ ERR("fail to create dbus message");
+ goto release_n_return;
+ }
+
+ dbus_message_append_args(message,
+ DBUS_TYPE_UINT32, &action,
+ DBUS_TYPE_STRING, &pkgname,
+ DBUS_TYPE_UINT32, &count,
+ DBUS_TYPE_INVALID);
+
+ ret = dbus_connection_send(connection, message, NULL);
+ if (!ret) {
+ ERR("fail to send dbus message : [%u][%s][%u]",
+ action, pkgname, count);
+ goto release_n_return;
+ }
+
+ dbus_connection_flush(connection);
+
+ DBG("success to emit signal [%u][%s][%u]",
+ action, pkgname, count);
+
+release_n_return:
+ dbus_error_free(&err);
+
+ if (message)
+ dbus_message_unref(message);
+
+ if (connection)
+ dbus_connection_unref(connection);
+}
+
+static badge_error_e _badge_check_data_inserted(const char *pkgname,
+ sqlite3 *db)
+{
+ sqlite3_stmt *stmt = NULL;
+ int count = 0;
+ int result = BADGE_ERROR_NONE;
+ char *sqlbuf = NULL;
+ int sqlret;
+
+ if (!pkgname)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (!db)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
+ "pkgname = %Q",
+ BADGE_TABLE_NAME, pkgname);
+
+ if (!sqlbuf) {
+ ERR("fail to alloc sql query");
+ return BADGE_ERROR_NO_MEMORY;
+ }
+
+ sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
+ if (sqlret != SQLITE_OK) {
+ ERR("DB err [%s]", sqlite3_errmsg(db));
+ ERR("query[%s]", sqlbuf);
+ result = BADGE_ERROR_FROM_DB;
+ goto free_and_return;
+ }
+
+ sqlret = sqlite3_step(stmt);
+ if (sqlret == SQLITE_ROW)
+ count = sqlite3_column_int(stmt, 0);
+ else
+ count = 0;
+
+ DBG("[%s], count[%d]", sqlbuf, count);
+
+ if (count > 0)
+ result = BADGE_ERROR_ALREADY_EXIST;
+ else
+ result = BADGE_ERROR_NOT_EXIST;
+
+free_and_return:
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ if (stmt)
+ sqlite3_finalize(stmt);
+
+ return result;
+}
+
+static badge_error_e _badge_check_option_inserted(const char *pkgname,
+ sqlite3 *db)
+{
+ sqlite3_stmt *stmt = NULL;
+ int count = 0;
+ int result = BADGE_ERROR_NONE;
+ char *sqlbuf = NULL;
+ int sqlret;
+
+ if (!pkgname)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (!db)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
+ "pkgname = %Q",
+ BADGE_OPTION_TABLE_NAME, pkgname);
+
+ if (!sqlbuf) {
+ ERR("fail to alloc sql query");
+ return BADGE_ERROR_NO_MEMORY;
+ }
+
+ sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
+ if (sqlret != SQLITE_OK) {
+ ERR("DB err [%s]", sqlite3_errmsg(db));
+ ERR("query[%s]", sqlbuf);
+ result = BADGE_ERROR_FROM_DB;
+ goto free_and_return;
+ }
+
+ sqlret = sqlite3_step(stmt);
+ if (sqlret == SQLITE_ROW)
+ count = sqlite3_column_int(stmt, 0);
+ else
+ count = 0;
+
+ DBG("[%s], count[%d]", sqlbuf, count);
+
+ if (count > 0)
+ result = BADGE_ERROR_ALREADY_EXIST;
+ else
+ result = BADGE_ERROR_NOT_EXIST;
+
+free_and_return:
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ if (stmt)
+ sqlite3_finalize(stmt);
+
+ return result;
+}
+
+static badge_error_e _badge_check_writable(const char *caller,
+ const char *pkgname, sqlite3 *db)
+{
+ sqlite3_stmt *stmt = NULL;
+ int count = 0;
+ badge_error_e result = BADGE_ERROR_NONE;
+ char *sqlbuf = NULL;
+ int sqlret;
+
+ if (!db)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (!caller)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (!pkgname)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (g_strcmp0(caller, pkgname) == 0)
+ return BADGE_ERROR_NONE;
+
+ sqlbuf = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE " \
+ "pkgname = %Q AND writable_pkgs LIKE '%%%q%%'",
+ BADGE_TABLE_NAME,
+ pkgname, caller);
+ if (!sqlbuf) {
+ ERR("fail to alloc sql query");
+ return BADGE_ERROR_NO_MEMORY;
+ }
+
+ sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
+ if (sqlret != SQLITE_OK) {
+ ERR("DB err [%s]", sqlite3_errmsg(db));
+ ERR("query[%s]", sqlbuf);
+ result = BADGE_ERROR_FROM_DB;
+ goto free_and_return;
+ }
+
+ sqlret = sqlite3_step(stmt);
+ if (sqlret == SQLITE_ROW)
+ count = sqlite3_column_int(stmt, 0);
+ else
+ count = 0;
+
+ DBG("[%s], count[%d]", sqlbuf, count);
+
+ if (count <= 0)
+ result = BADGE_ERROR_PERMISSION_DENIED;
+
+free_and_return:
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ if (stmt)
+ sqlite3_finalize(stmt);
+
+ return result;
+}
+
+
+badge_error_e _badge_is_existing(const char *pkgname, bool *existing)
+{
+ sqlite3 *db = NULL;
+ int sqlret;
+ badge_error_e ret = BADGE_ERROR_NONE;
+ badge_error_e result = BADGE_ERROR_NONE;
+
+ if (!pkgname || !existing) {
+ ERR("pkgname : %s, existing : %p", pkgname, existing);
+ return BADGE_ERROR_INVALID_DATA;
+ }
+
+ sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+ if (sqlret != SQLITE_OK || !db) {
+ ERR("fail to db_util_open - [%d]", sqlret);
+ return BADGE_ERROR_FROM_DB;
+ }
+
+ result = _badge_check_data_inserted(pkgname, db);
+ if (result == BADGE_ERROR_ALREADY_EXIST)
+ *existing = TRUE;
+ else if (result == BADGE_ERROR_NOT_EXIST)
+ *existing = FALSE;
+ else
+ ret = result;
+
+ sqlret = db_util_close(db);
+ if (sqlret != SQLITE_OK)
+ WARN("fail to db_util_close - [%d]", sqlret);
+
+ return BADGE_ERROR_NONE;
+}
+
+badge_error_e _badge_foreach_existed(badge_cb callback, void *data)
+{
+ sqlite3 *db = NULL;
+ badge_error_e result = BADGE_ERROR_NONE;
+ char *sqlbuf = NULL;
+ sqlite3_stmt *stmt = NULL;
+ int sqlret;
+
+ if (!callback)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+ if (sqlret != SQLITE_OK || !db) {
+ ERR("fail to db_util_open - [%d]", sqlret);
+ return BADGE_ERROR_FROM_DB;
+ }
+
+ sqlbuf = sqlite3_mprintf("SELECT pkgname, badge FROM %s",
+ BADGE_TABLE_NAME);
+ if (!sqlbuf) {
+ ERR("fail to alloc sql query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto free_and_return;
+ }
+
+ sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to sqlite3_prepare_v2 - [%s]", sqlite3_errmsg(db));
+ ERR("query[%s]", sqlbuf);
+ result = BADGE_ERROR_FROM_DB;
+ goto free_and_return;
+ }
+
+ sqlret = sqlite3_step(stmt);
+ if (sqlret == SQLITE_DONE) {
+ INFO("badge db has no data");
+ result = BADGE_ERROR_NOT_EXIST;
+ goto free_and_return;
+ } else if (sqlret == SQLITE_ROW) {
+ const char *pkg = NULL;
+ unsigned int badge_count = 0;
+
+ pkg = (const char *)sqlite3_column_text(stmt, 0);
+ badge_count = (unsigned int)sqlite3_column_int(stmt, 1);
+
+ if (pkg)
+ callback(pkg, badge_count, data);
+ else
+ ERR("db has invaild data");
+ } else {
+ ERR("fail to sqlite3_step : %d", sqlret);
+ result = BADGE_ERROR_FROM_DB;
+ goto free_and_return;
+ }
+
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ const char *pkg = NULL;
+ unsigned int badge_count = 0;
+
+ pkg = (const char *)sqlite3_column_text(stmt, 0);
+ badge_count = (unsigned int)sqlite3_column_int(stmt, 1);
+
+
+ if (pkg)
+ callback(pkg, badge_count, data);
+ else
+ ERR("db has invaild data");
+ }
+
+free_and_return:
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ if (stmt)
+ sqlite3_finalize(stmt);
+
+ sqlret = db_util_close(db);
+ if (sqlret != SQLITE_OK)
+ WARN("fail to db_util_close - [%d]", sqlret);
+
+ return result;
+}
+
+badge_error_e _badge_insert(badge_h *badge)
+{
+ sqlite3 *db = NULL;
+ int sqlret;
+ badge_error_e ret = BADGE_ERROR_NONE;
+ badge_error_e result = BADGE_ERROR_NONE;
+ char *sqlbuf = NULL;
+ char *err_msg = NULL;
+
+ if (!badge || !badge->pkgname || !badge->writable_pkgs)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+ if (sqlret != SQLITE_OK || !db) {
+ ERR("fail to db_util_open - [%s][%d]", BADGE_DB_PATH, sqlret);
+ return BADGE_ERROR_FROM_DB;
+ }
+
+ /* Check pkgname & id */
+ ret = _badge_check_data_inserted(badge->pkgname, db);
+ if (ret != BADGE_ERROR_NOT_EXIST) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ sqlbuf = sqlite3_mprintf("INSERT INTO %s " \
+ "(pkgname, " \
+ "writable_pkgs) " \
+ "VALUES "
+ "(%Q, %Q);",
+ BADGE_TABLE_NAME,
+ badge->pkgname, badge->writable_pkgs);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_exec(db, sqlbuf, NULL, NULL, &err_msg);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to insert badge[%s], err[%d - %s]",
+ badge->pkgname, sqlret, err_msg);
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+
+ _badge_changed(BADGE_ACTION_CREATE, badge->pkgname, 0);
+
+ /* inserting badge options */
+ ret = _badge_check_option_inserted(badge->pkgname, db);
+ if (ret != BADGE_ERROR_NOT_EXIST) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ sqlbuf = sqlite3_mprintf("INSERT INTO %s " \
+ "(pkgname) " \
+ "VALUES "
+ "(%Q);",
+ BADGE_OPTION_TABLE_NAME,
+ badge->pkgname);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_exec(db, sqlbuf, NULL, NULL, &err_msg);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to insert badge option[%s], err[%d - %s]",
+ badge->pkgname, sqlret, err_msg);
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+
+return_close_db:
+ if (err_msg)
+ free(err_msg);
+
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ sqlret = db_util_close(db);
+ if (sqlret != SQLITE_OK)
+ WARN("fail to db_util_close - [%d]", sqlret);
+
+ return result;
+}
+
+badge_error_e _badge_remove(const char *caller, const char *pkgname)
+{
+ badge_error_e ret = BADGE_ERROR_NONE;
+ badge_error_e result = BADGE_ERROR_NONE;
+ sqlite3 *db = NULL;
+ int sqlret;
+ char *sqlbuf = NULL;
+ char *err_msg = NULL;
+
+ if (!caller)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (!pkgname)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+ if (sqlret != SQLITE_OK || !db) {
+ ERR("fail to db_util_open - [%d]", sqlret);
+ return BADGE_ERROR_FROM_DB;
+ }
+
+ ret = _badge_check_data_inserted(pkgname, db);
+ if (ret != BADGE_ERROR_ALREADY_EXIST) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ ret = _badge_check_writable(caller, pkgname, db);
+ if (ret != BADGE_ERROR_NONE) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ sqlbuf = sqlite3_mprintf("DELETE FROM %s WHERE pkgname = %Q",
+ BADGE_TABLE_NAME, pkgname);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_exec(db, sqlbuf, NULL, NULL, &err_msg);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to remove badge[%s], err[%d - %s]",
+ pkgname, sqlret, err_msg);
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+
+ _badge_changed(BADGE_ACTION_REMOVE, pkgname, 0);
+
+ /* treating option table */
+ ret = _badge_check_option_inserted(pkgname, db);
+ if (ret != BADGE_ERROR_ALREADY_EXIST) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ sqlbuf = sqlite3_mprintf("DELETE FROM %s WHERE pkgname = %Q",
+ BADGE_OPTION_TABLE_NAME, pkgname);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_exec(db, sqlbuf, NULL, NULL, &err_msg);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to remove badge option[%s], err[%d - %s]",
+ pkgname, sqlret, err_msg);
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+
+return_close_db:
+ if (err_msg)
+ free(err_msg);
+
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ sqlret = db_util_close(db);
+ if (sqlret != SQLITE_OK)
+ WARN("fail to db_util_close - [%d]", sqlret);
+
+ return result;
+}
+
+badge_error_e _badget_set_count(const char *caller, const char *pkgname,
+ unsigned int count)
+{
+ badge_error_e ret = BADGE_ERROR_NONE;
+ badge_error_e result = BADGE_ERROR_NONE;
+ sqlite3 *db = NULL;
+ char *sqlbuf = NULL;
+ int sqlret;
+ char *err_msg = NULL;
+
+ if (!caller)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (!pkgname)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+ if (sqlret != SQLITE_OK || !db) {
+ ERR("fail to db_util_open - [%d]", sqlret);
+ return BADGE_ERROR_FROM_DB;
+ }
+
+ ret = _badge_check_data_inserted(pkgname, db);
+ if (ret != BADGE_ERROR_ALREADY_EXIST) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ ret = _badge_check_writable(caller, pkgname, db);
+ if (ret != BADGE_ERROR_NONE) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ sqlbuf = sqlite3_mprintf("UPDATE %s SET badge = %d " \
+ "WHERE pkgname = %Q",
+ BADGE_TABLE_NAME, count, pkgname);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_exec(db, sqlbuf, NULL, NULL, &err_msg);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to set badge[%s] count[%d], err[%d - %s]",
+ pkgname, count, sqlret, err_msg);
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+
+ _badge_changed(BADGE_ACTION_UPDATE, pkgname, count);
+
+return_close_db:
+ if (err_msg)
+ free(err_msg);
+
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ sqlret = db_util_close(db);
+ if (sqlret != SQLITE_OK)
+ WARN("fail to db_util_close - [%d]", sqlret);
+
+ return result;
+}
+
+badge_error_e _badget_get_count(const char *pkgname, unsigned int *count)
+{
+ badge_error_e ret = BADGE_ERROR_NONE;
+ badge_error_e result = BADGE_ERROR_NONE;
+ sqlite3 *db = NULL;
+ char *sqlbuf = NULL;
+ sqlite3_stmt *stmt = NULL;
+ int sqlret;
+
+ if (!pkgname)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (!count)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+ if (sqlret != SQLITE_OK || !db) {
+ ERR("fail to db_util_open - [%d]", sqlret);
+ return BADGE_ERROR_FROM_DB;
+ }
+
+ ret = _badge_check_data_inserted(pkgname, db);
+ if (ret != BADGE_ERROR_ALREADY_EXIST) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ sqlbuf = sqlite3_mprintf("SELECT badge FROM %s " \
+ "WHERE pkgname = %Q",
+ BADGE_TABLE_NAME, pkgname);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to prepare %s - [%s]",
+ sqlbuf, sqlite3_errmsg(db));
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_step(stmt);
+ if (sqlret == SQLITE_ROW)
+ *count = (unsigned int)sqlite3_column_int(stmt, 0);
+ else
+ *count = (unsigned int)0;
+
+return_close_db:
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ if (stmt)
+ sqlite3_finalize(stmt);
+
+ sqlret = db_util_close(db);
+ if (sqlret != SQLITE_OK)
+ WARN("fail to db_util_close - [%d]", sqlret);
+
+ return result;
+}
+
+badge_error_e _badget_set_display(const char *pkgname,
+ unsigned int is_display)
+{
+ badge_error_e ret = BADGE_ERROR_NONE;
+ badge_error_e result = BADGE_ERROR_NONE;
+ sqlite3 *db = NULL;
+ char *sqlbuf = NULL;
+ int sqlret;
+ char *err_msg = NULL;
+
+ if (!pkgname)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (is_display != 0 && is_display != 1)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+ if (sqlret != SQLITE_OK || !db) {
+ ERR("fail to db_util_open - [%d]", sqlret);
+ return BADGE_ERROR_FROM_DB;
+ }
+
+ ret = _badge_check_option_inserted(pkgname, db);
+ if (ret == BADGE_ERROR_ALREADY_EXIST) {
+ sqlbuf = sqlite3_mprintf("UPDATE %s SET display = %d " \
+ "WHERE pkgname = %Q",
+ BADGE_OPTION_TABLE_NAME, is_display, pkgname);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_exec(db, sqlbuf, NULL, NULL, &err_msg);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to set badge[%s] option[%d], err[%d - %s]",
+ pkgname, is_display, sqlret, err_msg);
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+ } else if (ret == BADGE_ERROR_NOT_EXIST) {
+ sqlbuf = sqlite3_mprintf("INSERT INTO %s " \
+ "(pkgname, " \
+ "display) " \
+ "VALUES "
+ "(%Q, %d);",
+ BADGE_OPTION_TABLE_NAME,
+ pkgname, is_display);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_exec(db, sqlbuf, NULL, NULL, &err_msg);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to set badge[%s] option[%d], err[%d - %s]",
+ pkgname, is_display, sqlret, err_msg);
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+ } else {
+ result = ret;
+ goto return_close_db;
+ }
+
+ _badge_changed(BADGE_ACTION_CHANGED_DISPLAY, pkgname, is_display);
+
+return_close_db:
+ if (err_msg)
+ free(err_msg);
+
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ sqlret = db_util_close(db);
+ if (sqlret != SQLITE_OK)
+ WARN("fail to db_util_close - [%d]", sqlret);
+
+ return result;
+}
+
+badge_error_e _badget_get_display(const char *pkgname, unsigned int *is_display)
+{
+ badge_error_e ret = BADGE_ERROR_NONE;
+ badge_error_e result = BADGE_ERROR_NONE;
+ sqlite3 *db = NULL;
+ char *sqlbuf = NULL;
+ sqlite3_stmt *stmt = NULL;
+ int sqlret;
+
+ if (!pkgname)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (!is_display)
+ return BADGE_ERROR_INVALID_DATA;
+
+ sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+ if (sqlret != SQLITE_OK || !db) {
+ ERR("fail to db_util_open - [%d]", sqlret);
+ return BADGE_ERROR_FROM_DB;
+ }
+
+ ret = _badge_check_option_inserted(pkgname, db);
+ if (ret != BADGE_ERROR_ALREADY_EXIST) {
+ result = ret;
+ goto return_close_db;
+ }
+
+ sqlbuf = sqlite3_mprintf("SELECT display FROM %s " \
+ "WHERE pkgname = %Q",
+ BADGE_OPTION_TABLE_NAME, pkgname);
+ if (!sqlbuf) {
+ ERR("fail to alloc query");
+ result = BADGE_ERROR_NO_MEMORY;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
+ if (sqlret != SQLITE_OK) {
+ ERR("fail to prepare %s - [%s]",
+ sqlbuf, sqlite3_errmsg(db));
+ result = BADGE_ERROR_FROM_DB;
+ goto return_close_db;
+ }
+
+ sqlret = sqlite3_step(stmt);
+ if (sqlret == SQLITE_ROW)
+ *is_display = (unsigned int)sqlite3_column_int(stmt, 0);
+ else
+ *is_display = (unsigned int)1;
+
+return_close_db:
+ if (sqlbuf)
+ sqlite3_free(sqlbuf);
+
+ if (stmt)
+ sqlite3_finalize(stmt);
+
+ sqlret = db_util_close(db);
+ if (sqlret != SQLITE_OK)
+ WARN("fail to db_util_close - [%d]", sqlret);
+
+ return result;
+}
+
+static void _badge_changed_cb_call(unsigned int action, const char *pkgname,
+ unsigned int count)
+{
+ GList *list = g_badge_cb_list;
+
+ while (list) {
+ struct _badge_cb_data *bd = g_list_nth_data(list, 0);
+ if (!bd)
+ continue;
+
+ if (bd->callback)
+ bd->callback(action, pkgname, count, bd->data);
+
+ list = g_list_next(list);
+ }
+}
+
+static DBusHandlerResult _badge_signal_filter(DBusConnection *conn,
+ DBusMessage *msg, void *user_data)
+{
+ const char *interface;
+ DBusError error;
+ dbus_bool_t ret;
+ unsigned int action = 0;
+ const char *pkgname = NULL;
+ unsigned int count = 0;
+
+ dbus_error_init(&error);
+
+ interface = dbus_message_get_interface(msg);
+ DBG("path : %s", dbus_message_get_path(msg));
+ DBG("interface : %s", interface);
+
+ if (g_strcmp0(BADGE_DBUS_INTERFACE, interface))
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ ret = dbus_message_is_signal(msg, interface, BADGE_CHANGED_NOTI);
+ if (!ret) {
+ DBG("this msg is not signal");
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+
+ ret = dbus_message_get_args(msg, &error,
+ DBUS_TYPE_UINT32, &action,
+ DBUS_TYPE_STRING, &pkgname,
+ DBUS_TYPE_UINT32, &count,
+ DBUS_TYPE_INVALID);
+ if (!ret) {
+ ERR("fail to get args : %s", error.message);
+ dbus_error_free(&error);
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+
+ _badge_changed_cb_call(action, pkgname, count);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static inline void __bus_rule_get(char *buf, int buf_len)
+{
+ if (!buf)
+ return;
+ if (buf_len <= 1)
+ return;
+
+ snprintf(buf, buf_len,
+ "path='%s',type='signal',interface='%s',member='%s'",
+ BADGE_DBUS_PATH,
+ BADGE_DBUS_INTERFACE,
+ BADGE_CHANGED_NOTI);
+}
+
+static void _badge_changed_monitor_init()
+{
+ DBusError err;
+ DBusConnection *conn = NULL;
+ char rule[1024] = {'\0', };
+
+ if (g_badge_cb_handle)
+ return;
+
+ dbus_error_init(&err);
+ conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
+ if (!conn) {
+ ERR("fail to get bus : %s", err.message);
+ dbus_error_free(&err);
+ return;
+ }
+ dbus_connection_setup_with_g_main(conn, NULL);
+
+ __bus_rule_get(rule, sizeof(rule));
+
+ dbus_bus_add_match(conn, rule, &err);
+ if (dbus_error_is_set(&err)) {
+ ERR("fail to dbus_bus_remove_match : %s",
+ err.message);
+ dbus_error_free(&err);
+ dbus_connection_close(conn);
+ return;
+ }
+
+ if (dbus_connection_add_filter(conn, _badge_signal_filter,
+ NULL, NULL) == FALSE) {
+ ERR("fail to dbus_connection_add_filter : %s",
+ err.message);
+ dbus_error_free(&err);
+ dbus_connection_close(conn);
+ return;
+ }
+
+ dbus_connection_set_exit_on_disconnect(conn, FALSE);
+
+ g_badge_cb_handle = conn;
+
+ return;
+}
+
+static void _badge_chanaged_monitor_fini()
+{
+ DBusConnection *conn = g_badge_cb_handle;
+ char rule[1024] = {'\0', };
+ DBusError err;
+
+ if (!conn)
+ return;
+
+ dbus_error_init(&err);
+
+ dbus_connection_remove_filter(conn, _badge_signal_filter, NULL);
+
+ __bus_rule_get(rule, sizeof(rule));
+
+ dbus_bus_remove_match(conn, rule, &err);
+ if (dbus_error_is_set(&err)) {
+ ERR("fail to dbus_bus_remove_match : %s",
+ err.message);
+ dbus_error_free(&err);
+ }
+
+ dbus_connection_close(conn);
+
+ g_badge_cb_handle = NULL;
+}
+
+static gint _badge_data_compare(gconstpointer a, gconstpointer b)
+{
+ const struct _badge_cb_data *bd = NULL;
+
+ if (!a)
+ return -1;
+ bd = a;
+
+ if (bd->callback == b)
+ return 0;
+
+ return 1;
+}
+
+badge_error_e _badge_register_changed_cb(badge_change_cb callback, void *data)
+{
+ struct _badge_cb_data *bd = NULL;
+ GList *found = NULL;
+
+ if (!g_badge_cb_handle)
+ _badge_changed_monitor_init();
+
+ found = g_list_find_custom(g_badge_cb_list, (gconstpointer)callback,
+ _badge_data_compare);
+
+ if (found) {
+ bd = g_list_nth_data(found, 0);
+ bd->data = data;
+ } else {
+
+ bd = malloc(sizeof(struct _badge_cb_data));
+ if (!bd)
+ return BADGE_ERROR_NO_MEMORY;
+
+
+ bd->callback = callback;
+ bd->data = data;
+
+ g_badge_cb_list = g_list_append(g_badge_cb_list, bd);
+ }
+ return BADGE_ERROR_NONE;
+}
+
+badge_error_e _badge_unregister_changed_cb(badge_change_cb callback)
+{
+ GList *found = NULL;
+
+ found = g_list_find_custom(g_badge_cb_list, (gconstpointer)callback,
+ _badge_data_compare);
+
+ if (found) {
+ struct _badge_cb_data *bd = g_list_nth_data(found, 0);
+ g_badge_cb_list = g_list_delete_link(g_badge_cb_list, found);
+ free(bd);
+ }
+
+ if (!g_badge_cb_list)
+ _badge_chanaged_monitor_fini();
+
+ return BADGE_ERROR_NONE;
+}
+
+badge_error_e _badge_free(badge_h *badge)
+{
+ if (!badge)
+ return BADGE_ERROR_INVALID_DATA;
+
+ if (badge->pkgname)
+ free(badge->pkgname);
+
+ if (badge->writable_pkgs)
+ free(badge->writable_pkgs);
+
+ free(badge);
+
+ return BADGE_ERROR_NONE;
+}
+
+badge_h *_badge_new(const char *pkgname, const char *writable_pkgs,
+ badge_error_e *err)
+{
+ badge_h *badge = NULL;
+
+ if (!pkgname) {
+ ERR("PKGNAME is NULL");
+ if (err)
+ *err = BADGE_ERROR_INVALID_DATA;
+ return NULL;
+ }
+
+ if (!writable_pkgs) {
+ ERR("writable_pkgs is NULL");
+ if (err)
+ *err = BADGE_ERROR_INVALID_DATA;
+ return NULL;
+ }
+
+
+ badge = (badge_h *)malloc(sizeof(struct _badge_h));
+ if (!badge) {
+ ERR("fail to alloc handle");
+ if (err)
+ *err = BADGE_ERROR_NO_MEMORY;
+ return NULL;
+ }
+
+ badge->pkgname = strdup(pkgname);
+ badge->writable_pkgs = strdup(writable_pkgs);
+ if (err)
+ *err = BADGE_ERROR_NONE;
+
+ return badge;
+}
+
+char *_badge_pkgs_new(badge_error_e *err, const char *pkg1, ...)
+{
+ char *caller_pkgname = NULL;
+ char *s = NULL;
+ char *result = NULL;
+ char *ptr = NULL;
+ gsize length;
+ va_list args;
+
+
+ if (err)
+ *err = BADGE_ERROR_NONE;
+
+ caller_pkgname = _badge_get_pkgname_by_pid();
+ if (!caller_pkgname) {
+ ERR("fail to get caller pkgname");
+ if (err)
+ *err = BADGE_ERROR_PERMISSION_DENIED;
+ return NULL;
+ }
+
+ if (!pkg1) {
+ WARN("pkg1 is NULL");
+ return caller_pkgname;
+ }
+
+ length = strlen(pkg1);
+ va_start(args, pkg1);
+ s = va_arg(args, char *);
+ while (s) {
+ length += strlen(s) ;
+ s = va_arg(args, char *);
+ }
+ va_end(args);
+
+ if (length <= 0) {
+ WARN("length is %d", length);
+ return caller_pkgname;
+ }
+
+ result = g_new0(char, length + 1); /* 1 for null terminate */
+ if (!result) {
+ ERR("fail to alloc memory");
+ if (err)
+ *err = BADGE_ERROR_NO_MEMORY;
+ free(caller_pkgname);
+ return NULL;
+ }
+
+ ptr = result;
+ ptr = g_stpcpy(ptr, pkg1);
+ va_start(args, pkg1);
+ s = va_arg(args, char *);
+ while (s) {
+ ptr = g_stpcpy(ptr, s);
+ s = va_arg(args, char *);
+ }
+ va_end(args);
+
+ if (g_strstr_len(result, -1, caller_pkgname) == NULL) {
+ char *new_pkgs = NULL;
+ new_pkgs = g_strdup_printf("%s%s", caller_pkgname, result);
+ if (!new_pkgs) {
+ ERR("fail to alloc memory");
+ if (err)
+ *err = BADGE_ERROR_NO_MEMORY;
+
+ free(result);
+ free(caller_pkgname);
+ return NULL;
+ }
+ free(result);
+ result = new_pkgs;
+ }
+
+ free(caller_pkgname);
+
+ return result;
+}
+
+char *_badge_pkgs_new_valist(badge_error_e *err, const char *pkg1, va_list args)
+{
+ char *caller_pkgname = NULL;
+ char *s = NULL;
+ char *result = NULL;
+ char *ptr = NULL;
+ gsize length;
+ va_list args2;
+
+ if (err)
+ *err = BADGE_ERROR_NONE;
+
+ caller_pkgname = _badge_get_pkgname_by_pid();
+ if (!caller_pkgname) {
+ ERR("fail to get caller pkgname");
+ if (err)
+ *err = BADGE_ERROR_PERMISSION_DENIED;
+ return NULL;
+ }
+
+ if (!pkg1) {
+ WARN("pkg1 is NULL");
+ return caller_pkgname;
+ }
+
+ G_VA_COPY(args2, args);
+
+ length = strlen(pkg1);
+ s = va_arg(args, char *);
+ while (s) {
+ length += strlen(s) ;
+ s = va_arg(args, char *);
+ }
+
+ if (length <= 0) {
+ WARN("length is %d", length);
+ va_end(args2);
+ return caller_pkgname;
+ }
+
+ result = g_new0(char, length + 1); /* 1 for null terminate */
+ if (!result) {
+ ERR("fail to alloc memory");
+ if (err)
+ *err = BADGE_ERROR_NO_MEMORY;
+ free(caller_pkgname);
+ va_end(args2);
+ return NULL;
+ }
+
+ ptr = result;
+ ptr = g_stpcpy(ptr, pkg1);
+ s = va_arg(args2, char *);
+ while (s) {
+ ptr = g_stpcpy(ptr, s);
+ s = va_arg(args2, char *);
+ }
+ va_end(args2);
+
+ if (g_strstr_len(result, -1, caller_pkgname) == NULL) {
+ char *new_pkgs = NULL;
+ new_pkgs = g_strdup_printf("%s%s", caller_pkgname, result);
+ if (!new_pkgs) {
+ ERR("fail to alloc memory");
+ if (err)
+ *err = BADGE_ERROR_NO_MEMORY;
+
+ free(result);
+ free(caller_pkgname);
+ return NULL;
+ }
+ free(result);
+ result = new_pkgs;
+ }
+
+ free(caller_pkgname);
+
+ return result;
+}
+