[Sticker][TCSACR-481] Add Sticker APIs (#3948)
[platform/core/csapi/tizenfx.git] / src / Tizen.Uix.Sticker / Tizen.Uix.Sticker / StickerProvider.cs
1 /*
2 * Copyright (c) 2022 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
18 using System;
19 using System.Collections.Generic;
20 using System.Threading.Tasks;
21 using static Interop.StickerData;
22 using static Interop.StickerProvider;
23
24 namespace Tizen.Uix.Sticker
25 {
26     /// <summary>
27     /// The StickerProvider provides the methods to create/update/delete the sticker data.
28     /// </summary>
29     /// <since_tizen> 10 </since_tizen>
30     public static class StickerProvider
31     {
32         private static IntPtr _handle = IntPtr.Zero;
33         private static StickerProviderInsertFinishedCallback _insertDelegate;
34
35         /// <summary>
36         /// Gets a flag indicating whether the StickerProvider is initialized
37         /// </summary>
38         public static bool Initialized { get; private set; }
39
40         /// <summary>
41         /// Initialize sticker provider.
42         /// </summary>
43         /// <since_tizen> 10 </since_tizen>
44         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
45         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
46         /// <exception cref="InvalidOperationException">
47         /// This can occur due to the following reasons:
48         /// 1) This exception can be due to out of memory.
49         /// 2) This exception can be due to operation failed.
50         /// </exception>
51         public static void Initialize()
52         {
53             if (!Initialized)
54             {
55                 IntPtr handle;
56                 ErrorCode error = StickerProviderCreate(out handle);
57                 if (error != ErrorCode.None)
58                 {
59                     Log.Error(LogTag, "Create Failed with error " + error);
60                     throw ExceptionFactory.CreateException(error);
61                 }
62                 _handle = handle;
63                 Initialized = true;
64             }
65             else
66             {
67                 Log.Debug(LogTag, "Already initialized");
68             }
69         }
70
71         /// <summary>
72         /// Deinitialize the sticker provider.
73         /// </summary>
74         /// <since_tizen> 10 </since_tizen>
75         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
76         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
77         /// <exception cref="InvalidOperationException">This exception can be due to operation failed.</exception>
78         /// <pre>
79         /// StickerProvider must be initialized.
80         /// </pre>
81         public static void Deinitialize()
82         {
83             ErrorCode error = StickerProviderDestroy(_handle);
84             if (error != ErrorCode.None)
85             {
86                 Log.Error(LogTag, "Destroy Failed with error " + error);
87                 if (error == ErrorCode.InvalidParameter)
88                     throw ExceptionFactory.CreateException(ErrorCode.OperationFailed);
89                 else
90                     throw ExceptionFactory.CreateException(error);
91             }
92         }
93
94         /// <summary>
95         /// Inserts a sticker data to the sticker database.
96         /// </summary>
97         /// <since_tizen> 10 </since_tizen>
98         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
99         /// <param name="data">The sticker data to be saved.</param>
100         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
101         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
102         /// <exception cref="InvalidOperationException">
103         /// This can occur due to the following reasons:
104         /// 1) This exception can be due to operation failed.
105         /// 2) This exception can be due to file exists.
106         /// 3) This exception can be due to no such file.
107         /// </exception>
108         public static void InsertData(StickerData data)
109         {
110             ErrorCode error = StickerProviderInsertData(_handle, data.SafeStickerDataHandle);
111             if (error != ErrorCode.None)
112             {
113                 Log.Error(LogTag, "InsertData Failed with error " + error);
114                 throw ExceptionFactory.CreateException(error);
115             }
116         }
117
118         /// <summary>
119         /// Inserts a sticker data using JSON file.
120         /// </summary>
121         /// <since_tizen> 10 </since_tizen>
122         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
123         /// <remarks>
124         /// All data except thumbnail, description, and display_type must be set in the JSON file to insert the sticker data.
125         /// <paramref name="jsonPath"/> must have a non-null value and must be an existing file.
126         /// If the URI type is lacal path, the sticker file is copied to a sticker directory.
127         /// It is recommended to delete your sticker files after inserting a sticker data.
128         /// </remarks>
129         /// <param name="jsonPath">The path of JSON file containing sticker data to be saved</param>
130         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
131         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
132         /// <exception cref="InvalidOperationException">This exception can be due to operation failed.</exception>
133         public static Task InsertData(string jsonPath)
134         {
135             var task = new TaskCompletionSource<bool>();
136             _insertDelegate = (ErrorCode errorCode, IntPtr userData) =>
137             {
138                 if (errorCode != ErrorCode.None)
139                 {
140                     Log.Error(LogTag, "Exception occurred while inserting");
141                     throw ExceptionFactory.CreateException(errorCode);
142                 }
143                 else
144                 {
145                     task.SetResult(true);
146                 }
147             };
148             ErrorCode error = StickerProviderInsertDataByJsonFile(_handle, jsonPath, _insertDelegate, IntPtr.Zero);
149             if (error != ErrorCode.None)
150             {
151                 Log.Error(LogTag, "InsertData Failed with error " + error);
152                 task.SetException(ExceptionFactory.CreateException(error));
153             }
154             return task.Task;
155         }
156
157         /// <summary>
158         /// Updates a sticker data in the sticker database.
159         /// </summary>
160         /// <since_tizen> 10 </since_tizen>
161         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
162         /// <param name="data">The sticker data to be updated.</param>
163         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
164         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
165         /// <exception cref="InvalidOperationException">
166         /// This can occur due to the following reasons:
167         /// 1) This exception can be due to operation failed.
168         /// 2) This exception can be due to file exists.
169         /// 3) This exception can be due to no such file.
170         /// </exception>
171         public static void UpdateData(StickerData data)
172         {
173             ErrorCode error = StickerProviderUpdateData(_handle, data.SafeStickerDataHandle);
174             if (error != ErrorCode.None)
175             {
176                 Log.Error(LogTag, "UpdateData Failed with error " + error);
177                 throw ExceptionFactory.CreateException(error);
178             }
179         }
180
181         /// <summary>
182         /// Deletes a sticker data in the sticker database.
183         /// </summary>
184         /// <since_tizen> 10 </since_tizen>
185         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
186         /// <param name="data">The sticker data to be deleted.</param>
187         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
188         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
189         /// <exception cref="InvalidOperationException">This exception can be due to operation failed.</exception>
190         public static void DeleteData(StickerData data)
191         {
192             ErrorCode error = StickerProviderDeleteData(_handle, data.SafeStickerDataHandle);
193             if (error != ErrorCode.None)
194             {
195                 Log.Error(LogTag, "DeleteData Failed with error " + error);
196                 throw ExceptionFactory.CreateException(error);
197             }
198         }
199
200         /// <summary>
201         /// Deletes a sticker data in the sticker database.
202         /// </summary>
203         /// <since_tizen> 10 </since_tizen>
204         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
205         /// <param name="uri">The URI of the sticker data to be deleted.</param>
206         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
207         /// <exception cref="InvalidOperationException">
208         /// This can occur due to the following reasons:
209         /// 1) This exception can be due to operation failed.
210         /// 2) This exception can be due to no such file.
211         /// </exception>
212         public static void DeleteData(string uri)
213         {
214             ErrorCode error = StickerProviderDeleteDataByUri(_handle, uri);
215             if (error != ErrorCode.None)
216             {
217                 Log.Error(LogTag, "DeleteData Failed with error " + error);
218                 throw ExceptionFactory.CreateException(error);
219             }
220         }
221
222         /// <summary>
223         /// Gets the count of stickers stored by the provider application.
224         /// </summary>
225         /// <since_tizen> 10 </since_tizen>
226         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
227         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
228         /// <exception cref="InvalidOperationException">This exception can be due to operation failed.</exception>
229         public static int StickerCount
230         {
231             get
232             {
233                 int _count = 0;
234                 ErrorCode error = StickerProviderGetStickerCount(_handle, out _count);
235                 if (error != ErrorCode.None)
236                 {
237                     Log.Error(LogTag, "StickerCount Failed with error " + error);
238                     throw ExceptionFactory.CreateException(error);
239                 }
240
241                 return _count;
242             }
243         }
244
245         /// <summary>
246         /// Retrieves all sticker data in the sticker database.
247         /// </summary>
248         /// <since_tizen> 10 </since_tizen>
249         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
250         /// <param name="offset">The start position (Starting from zero).</param>
251         /// <param name="count">The number of stickers to be retrieved with respect to the offset.</param>
252         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
253         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
254         /// <exception cref="InvalidOperationException">
255         /// This can occur due to the following reasons:
256         /// 1) This exception can be due to out of memory.
257         /// 2) This exception can be due to operation failed.
258         /// </exception>
259         public static IEnumerable<StickerData> GetAllStickers(int offset, int count)
260         {
261             var stickers = new List<StickerData>();
262             StickerProviderDataForeachCallback _dataForeachDelegate = (IntPtr stickerData, IntPtr userData) =>
263             {
264                 StickerData data = new StickerData(stickerData);
265                 stickers.Add(data);
266             };
267             ErrorCode error = StickerProviderDataForeachAll(_handle, offset, count, out var result, _dataForeachDelegate, IntPtr.Zero);
268             if (error != ErrorCode.None)
269             {
270                 Log.Error(LogTag, "GetAllStickers Failed with error " + error);
271                 throw ExceptionFactory.CreateException(error);
272             }
273
274             return stickers;
275         }
276
277         /// <summary>
278         /// Sets the image of the sticker group.
279         /// URI must be a relative path like '/res/smile.png' when URI type is local path.
280         /// </summary>
281         /// <since_tizen> 10 </since_tizen>
282         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
283         /// <param name="groupImage">The group image to be set.</param>
284         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
285         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
286         /// <exception cref="InvalidOperationException">
287         /// This can occur due to the following reasons:
288         /// 1) This exception can be due to operation failed.
289         /// 2) This exception can be due to no such file.
290         /// </exception>
291         public static void SetGroupImage(GroupImage groupImage)
292         {
293             ErrorCode error = StickerProviderSetGroupImage(_handle, groupImage.GroupName, groupImage.UriType, groupImage.Uri);
294             if (error != ErrorCode.None)
295             {
296                 Log.Error(LogTag, "SetGroupImage Failed with error " + error);
297                 throw ExceptionFactory.CreateException(error);
298             }
299         }
300     }
301 }