Add code to delete all AR emoji stickers
[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
17 #include "sticker_info.h"
18 #include <sticker_provider.h>
19 #include <app_common.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "log.h"
24
25 static sticker_provider_h sticker_provider = NULL;
26
27 sticker_data_h
28 set_sticker_data(sticker_data_uri_type_e type, const char* uri, const char* keyword,
29 int len, const char* group, const char* thumbnail, const char* description, sticker_data_display_type_e disp_type)
30 {
31     sticker_data_h sticker_data;
32     int ret;
33
34     /* Creates a Sticker data handle */
35     ret = sticker_data_create(&sticker_data);
36     if (ret != STICKER_ERROR_NONE) {
37         /* Error handling */
38         LOGE("Failed to create sticker data");
39     }
40
41     /* Sets the URI and URI type of the sticker */
42     ret = sticker_data_set_uri(sticker_data, type, uri);
43     if (ret != STICKER_ERROR_NONE) {
44         /* Error handling */
45         LOGE("Failed to set uri");
46     }
47
48     //for (int i = 0; i < len; i++)
49     {
50         /* Adds a keyword of the sticker to the list */
51         ret = sticker_data_add_keyword(sticker_data, keyword);
52         if (ret != STICKER_ERROR_NONE) {
53             /* Error handling */
54             LOGE("Failed to add keyword");
55         }
56     }
57
58     /* Sets the group name of the sticker */
59     ret = sticker_data_set_group_name(sticker_data, group);
60     if (ret != STICKER_ERROR_NONE) {
61         /* Error handling */
62         LOGE("Failed to set group name");
63     }
64
65     /* Sets the thumbnail local path of the sticker */
66     if (thumbnail) {
67         ret = sticker_data_set_thumbnail(sticker_data, thumbnail);
68         if (ret != STICKER_ERROR_NONE) {
69             /* Error handling */
70             LOGE("Failed to set thumbnail");
71         }
72     }
73
74     /* Sets the description of the sticker */
75     ret = sticker_data_set_description(sticker_data, description);
76     if (ret != STICKER_ERROR_NONE) {
77         /* Error handling */
78         LOGE("Failed to set description");
79     }
80
81     /* Sets the display type of the sticker.*/
82     ret = sticker_data_set_display_type(sticker_data, disp_type);
83     if (ret != STICKER_ERROR_NONE) {
84         /* Error handling */
85         LOGE("Failed to set display type");
86     }
87
88     return sticker_data;
89 }
90
91 void
92 insert_sticker_data(const char *filepath, const char *keyword, const char *group, const char *desc, const char *thumbnail, const char *disp_type)
93 {
94     sticker_data_h data_handle;
95     int ret;
96     sticker_data_display_type_e display_type;
97
98     if (strcmp(disp_type, "input") == 0)
99         display_type = STICKER_DATA_DISP_EMOJI;
100     else
101         display_type = STICKER_DATA_DISP_WALLPAPER;
102
103     data_handle = set_sticker_data(STICKER_DATA_URI_LOCAL_PATH, filepath, keyword, 1, group, thumbnail, desc, display_type);
104
105     ret = sticker_provider_insert_data(sticker_provider, data_handle);
106     if (ret != STICKER_ERROR_NONE) {
107         LOGE("Failed to insert data. error code : %x. message : %s", ret, get_error_message(ret));
108     }
109     else {
110         LOGI("Succeeded to insert data");
111     }
112
113     /* Destroys a sticker data handle */
114     ret = sticker_data_destroy(data_handle);
115     if (ret != STICKER_ERROR_NONE) {
116         /* Error handling */
117         LOGE("Failed to destroy sticker data");
118     }
119 }
120
121 int create_sticker_provider_handle(void)
122 {
123     int ret;
124     ret = sticker_provider_create(&sticker_provider);
125     if (ret != STICKER_ERROR_NONE) {
126         /* Error handling */
127         LOGE("Failed to create sticker provider");
128     }
129
130     return ret;
131 }
132
133 void destroy_sticker_provider_handle(void)
134 {
135     sticker_provider_destroy(sticker_provider);
136     sticker_provider = NULL;
137 }
138
139 void delete_sticker_data(const char *fileName)
140 {
141     int ret;
142     ret = sticker_provider_delete_data_by_uri(sticker_provider, fileName);
143     if (ret != STICKER_ERROR_NONE)
144         LOGE("Failed to delete sticker. ret : %d", ret);
145 }
146
147 static void _delete_all_stickers_cb(sticker_data_h data_handle, void *user_data)
148 {
149     int ret;
150     char *sticker_group = NULL;
151     char *del_group = (char *) user_data;
152
153     ret = sticker_data_get_group_name(data_handle, &sticker_group);
154     if (ret != STICKER_ERROR_NONE)
155         LOGE("Failed to get group name. ret : %d", ret);
156
157     if (strcmp(sticker_group, del_group) == 0) {
158         ret = sticker_provider_delete_data(sticker_provider, data_handle);
159         if (ret != STICKER_ERROR_NONE)
160             LOGE("Failed to delete sticker. ret : %d", ret);
161     }
162 }
163
164 void delete_all_stickers(const char *groupName)
165 {
166     int ret;
167     int total_cnt;
168     int result;
169
170     ret = sticker_provider_get_sticker_count(sticker_provider, &total_cnt);
171     if (ret != STICKER_ERROR_NONE)
172         LOGE("Failed to get sticker count. ret : %d", ret);
173
174     ret = sticker_provider_data_foreach_all(sticker_provider, 0, total_cnt, &result, _delete_all_stickers_cb, (void *)groupName);
175     if (ret != STICKER_ERROR_NONE)
176         LOGE("Failed to retrieve all sticker data. ret : %d", ret);
177 }