Terminate sticker-receiver after sending result
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / sticker_info.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "sticker_info.h"
17 #include <sticker_provider.h>
18 #include <app_common.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <iostream>
22
23 #include "log.h"
24
25 using namespace std;
26
27 static sticker_provider_h sticker_provider = NULL;
28 static bool need_to_retrive = false;
29
30 sticker_data_h
31 set_sticker_data(sticker_data_uri_type_e type, const char* uri, const char* keyword,
32 int len, const char* group, const char* thumbnail, const char* description)
33 {
34     sticker_data_h sticker_data;
35     int ret;
36
37     /* Creates a Sticker data handle */
38     ret = sticker_data_create(&sticker_data);
39     if (ret != STICKER_ERROR_NONE) {
40         /* Error handling */
41         LOGE("Failed to create sticker data");
42     }
43
44     /* Sets the URI and URI type of the sticker */
45     ret = sticker_data_set_uri(sticker_data, type, uri);
46     if (ret != STICKER_ERROR_NONE) {
47         /* Error handling */
48         LOGE("Failed to set uri");
49     }
50
51     //for (int i = 0; i < len; i++)
52     {
53         /* Adds a keyword of the sticker to the list */
54         ret = sticker_data_add_keyword(sticker_data, keyword);
55         if (ret != STICKER_ERROR_NONE) {
56             /* Error handling */
57             LOGE("Failed to add keyword");
58         }
59     }
60
61     /* Sets the group name of the sticker */
62     ret = sticker_data_set_group_name(sticker_data, group);
63     if (ret != STICKER_ERROR_NONE) {
64         /* Error handling */
65         LOGE("Failed to set group name");
66     }
67
68     /* Sets the thumbnail local path of the sticker */
69     if (thumbnail) {
70         ret = sticker_data_set_thumbnail(sticker_data, thumbnail);
71         if (ret != STICKER_ERROR_NONE) {
72             /* Error handling */
73             LOGE("Failed to set thumbnail");
74         }
75     }
76
77     /* Sets the description of the sticker */
78     ret = sticker_data_set_description(sticker_data, description);
79     if (ret != STICKER_ERROR_NONE) {
80         /* Error handling */
81         LOGE("Failed to set description");
82     }
83
84     return sticker_data;
85 }
86
87 void
88 insert_sticker_data(const char *filepath, const char *keyword, const char *group, const char *desc)
89 {
90     sticker_data_h data_handle;
91     int ret;
92
93     data_handle = set_sticker_data(STICKER_DATA_URI_LOCAL_PATH, filepath, keyword, 1, group, NULL, desc);
94
95     ret = sticker_provider_insert_data(sticker_provider, data_handle);
96     if (ret != STICKER_ERROR_NONE) {
97         LOGE("Failed to insert data. error code : %x. message : %s", ret, get_error_message(ret));
98     }
99     else {
100         LOGI("Succeeded to insert data");
101     }
102
103     /* Destroys a sticker data handle */
104     ret = sticker_data_destroy(data_handle);
105     if (ret != STICKER_ERROR_NONE) {
106         /* Error handling */
107         LOGE("Failed to destroy sticker data");
108     }
109 }
110
111 int create_sticker_provider_handle(void)
112 {
113     int ret;
114     ret = sticker_provider_create(&sticker_provider);
115     if (ret != STICKER_ERROR_NONE) {
116         /* Error handling */
117         LOGE("Failed to create sticker provider");
118     }
119
120     return ret;
121 }
122
123 void destroy_sticker_provider_handle(void)
124 {
125     sticker_provider_destroy(sticker_provider);
126     sticker_provider = NULL;
127 }
128
129 static void _sticker_foreach_cb(sticker_data_h data_handle, void *user_data)
130 {
131     int ret;
132     char *del_file = (char *)user_data;
133     sticker_data_uri_type_e type;
134     char *uri = NULL;
135
136     if (!need_to_retrive)
137         return;
138
139     ret = sticker_data_get_uri(data_handle, &type, &uri);
140     if (ret != STICKER_ERROR_NONE)
141         LOGE("Failed to get sticker uri");
142
143     int result = (string(uri)).find(del_file);
144     if (result >= 0) {
145         LOGI("Delete sticker (%s)", uri);
146         need_to_retrive = false;
147
148         ret = sticker_provider_delete_data(sticker_provider, data_handle);
149         if (ret != STICKER_ERROR_NONE)
150             LOGE("Failed to delete sticker");
151     }
152 }
153
154 void delete_sticker_data(const char *fileName)
155 {
156     int ret;
157     int offset = 0;
158     int count = 0;
159     int result = 0;
160     need_to_retrive = true;
161
162     while (result == count && need_to_retrive) {
163         count = 20;
164         ret = sticker_provider_data_foreach_all(sticker_provider, offset, count, &result, _sticker_foreach_cb, (void *)fileName);
165         offset += result;
166         if (ret != STICKER_ERROR_NONE) {
167             LOGE("Failed to retrieve sticker");
168             return;
169         }
170     }
171 }