[Sticker][TCSACR-481] Add Sticker APIs (#3948)
[platform/core/csapi/tizenfx.git] / src / Tizen.Uix.Sticker / Tizen.Uix.Sticker / StickerData.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 static Interop.StickerData;
21
22 namespace Tizen.Uix.Sticker
23 {
24     /// <summary>
25     /// Enumeration for the URI type.
26     /// </summary>
27     /// <since_tizen> 10 </since_tizen>
28     public enum UriType
29     {
30         /// <summary>
31         /// Local path URI
32         /// </summary>
33         LocalPath = 1,
34         /// <summary>
35         /// Web resource URI
36         /// </summary>
37         WebResource = 2
38     };
39
40     /// <summary>
41     /// Enumeration for the display type.
42     /// </summary>
43     /// <since_tizen> 10 </since_tizen>
44     public enum DisplayType
45     {
46         /// <summary>
47         /// Emoji type
48         /// </summary>
49         Emoji = 1,
50         /// <summary>
51         /// Wallpaper type
52         /// </summary>
53         Wallpaper = 2
54     };
55
56     /// <summary>
57     /// The StickerData provides the methods to get/set the sticker data.
58     /// </summary>
59     /// <since_tizen> 10 </since_tizen>
60     public class StickerData : IDisposable
61     {
62         internal SafeStickerDataHandle _handle;
63         private bool _disposed = false;
64
65         /// <summary>
66         /// The public constructor.
67         /// </summary>
68         /// <since_tizen> 10 </since_tizen>
69         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
70         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
71         /// <exception cref="InvalidOperationException">
72         /// This can occur due to the following reasons:
73         /// 1) This exception can be due to out of memory.
74         /// 2) This exception can be due to operation failed.
75         /// </exception>
76         public StickerData()
77         {
78             SafeStickerDataHandle handle;
79             ErrorCode error = StickerDataCreate(out handle);
80             if (error != ErrorCode.None)
81             {
82                 Log.Error(LogTag, "StickerData Failed with error " + error);
83                 throw ExceptionFactory.CreateException(error);
84             }
85             _handle = handle;
86         }
87
88         internal SafeStickerDataHandle SafeStickerDataHandle
89         {
90             get
91             {
92                 return _handle;
93             }
94         }
95
96         internal StickerData(SafeStickerDataHandle handle)
97         {
98             _handle = handle;
99         }
100
101         internal StickerData(IntPtr handle)
102         {
103             IntPtr cloneHandle;
104             ErrorCode error = StickerDataClone(handle, out cloneHandle);
105             if (error != ErrorCode.None)
106             {
107                 Log.Error(LogTag, "StickerData Failed with error " + error);
108                 throw ExceptionFactory.CreateException(error);
109             }
110
111             _handle = new SafeStickerDataHandle(cloneHandle);
112         }
113
114         /// <summary>
115         /// The destructor of the StickerData class.
116         /// </summary>
117         ~StickerData()
118         {
119             Dispose(false);
120         }
121
122         /// <summary>
123         /// Release any unmanaged resources used by this object.
124         /// </summary>
125         /// <since_tizen> 10 </since_tizen>
126         public void Dispose()
127         {
128             Dispose(true);
129             GC.SuppressFinalize(this);
130         }
131
132         /// <summary>
133         /// Release any unmanaged resources used by this object.
134         /// </summary>
135         /// <param name="disposing">
136         /// If true, disposes any disposable objects. If false, does not dispose disposable objects.
137         /// </param>
138         /// <since_tizen> 10 </since_tizen>
139         protected virtual void Dispose(bool disposing)
140         {
141             if (_disposed)
142                 return;
143
144             if (disposing)
145             {
146                 _handle?.Dispose();
147                 _handle = null;
148             }
149
150             _disposed = true;
151         }
152
153         /// <summary>
154         /// Gets the name of the sticker provider application from sticker data.
155         /// </summary>
156         /// <since_tizen> 10 </since_tizen>
157         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
158         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
159         /// <exception cref="InvalidOperationException">This exception can be due to no data available.</exception>
160         public string AppId
161         {
162             get
163             {
164                 string appId;
165                 ErrorCode error = StickerDataGetAppId(_handle, out appId);
166                 if (error != ErrorCode.None)
167                 {
168                     Log.Error(LogTag, "GetAppId Failed with error " + error);
169                     throw ExceptionFactory.CreateException(error);
170                 }
171
172                 return appId;
173             }
174         }
175
176         /// <summary>
177         /// Gets the URI from sticker data.
178         /// </summary>
179         /// <since_tizen> 10 </since_tizen>
180         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
181         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
182         /// <exception cref="InvalidOperationException">This exception can be due to no data available.</exception>
183         public string Uri
184         {
185             get
186             {
187                 string uri;
188                 UriType uriType;
189                 ErrorCode error = StickerDataGetUri(_handle, out uriType, out uri);
190                 if (error != ErrorCode.None)
191                 {
192                     Log.Error(LogTag, "GetUri Failed with error " + error);
193                     throw ExceptionFactory.CreateException(error);
194                 }
195
196                 return uri;
197             }
198         }
199
200         /// <summary>
201         /// Gets the URI type from sticker data.
202         /// </summary>
203         /// <since_tizen> 10 </since_tizen>
204         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
205         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
206         /// <exception cref="InvalidOperationException">This exception can be due to no data available.</exception>
207         public UriType UriType
208         {
209             get
210             {
211                 string uri;
212                 UriType uriType;
213                 ErrorCode error = StickerDataGetUri(_handle, out uriType, out uri);
214                 if (error != ErrorCode.None)
215                 {
216                     Log.Error(LogTag, "GetUriType Failed with error " + error);
217                     throw ExceptionFactory.CreateException(error);
218                 }
219
220                 return uriType;
221             }
222         }
223
224         /// <summary>
225         /// Sets the URI of the sticker data.
226         /// </summary>
227         /// <remarks><paramref name="uri"/> must be a path inside the app package directory like 'res/smile.png' when the type of URI is local path.</remarks>
228         /// <since_tizen> 10 </since_tizen>
229         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
230         /// <param name="uriType">The URI type to be saved.</param>
231         /// <param name="uri">The URI to be saved.</param>
232         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
233         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
234         public void SetUri(UriType uriType, string uri)
235         {
236             ErrorCode error = StickerDataSetUri(_handle, uriType, uri);
237             if (error != ErrorCode.None)
238             {
239                 Log.Error(LogTag, "SetUri Failed with error " + error);
240                 throw ExceptionFactory.CreateException(error);
241             }
242         }
243
244         /// <summary>
245         /// Adds a keyword of the sticker to the list.
246         /// </summary>
247         /// <since_tizen> 10 </since_tizen>
248         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
249         /// <param name="keyword">The keyword to be saved.</param>
250         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
251         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
252         public void AddKeyword(string keyword)
253         {
254             ErrorCode error = StickerDataAddKeyword(_handle, keyword);
255             if (error != ErrorCode.None)
256             {
257                 Log.Error(LogTag, "AddKeyword Failed with error " + error);
258                 throw ExceptionFactory.CreateException(error);
259             }
260         }
261
262         /// <summary>
263         /// Removes a keyword of the sticker from the list.
264         /// </summary>
265         /// <since_tizen> 10 </since_tizen>
266         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
267         /// <param name="keyword">The keyword to be removed.</param>
268         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
269         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
270         public void RemoveKeyword(string keyword)
271         {
272             ErrorCode error = StickerDataRemoveKeyword(_handle, keyword);
273             if (error != ErrorCode.None)
274             {
275                 Log.Error(LogTag, "RemoveKeyword Failed with error " + error);
276                 throw ExceptionFactory.CreateException(error);
277             }
278         }
279
280         /// <summary>
281         /// Retrieves all keywords of the sticker data.
282         /// </summary>
283         /// <since_tizen> 10 </since_tizen>
284         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
285         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
286         /// <exception cref="InvalidOperationException">This exception can be due to no data available.</exception>
287         public IEnumerable<string> GetAllKeywords()
288         {
289             var keywords = new List<string>();
290             StickerDataKeywordForeachCallback _keywordDelegate = (string keyword, IntPtr userData) =>
291             {
292                 keywords.Add(keyword);
293             };
294
295             ErrorCode error = StickerDataForeachKeyword(_handle, _keywordDelegate, IntPtr.Zero);
296             if (error != ErrorCode.None)
297             {
298                 Log.Error(LogTag, "GetAllKeywords Failed with error " + error);
299                 throw ExceptionFactory.CreateException(error);
300             }
301
302             return keywords.AsReadOnly();
303         }
304
305         /// <summary>
306         /// Gets/Sets the group name of the sticker data.
307         /// </summary>
308         /// <since_tizen> 10 </since_tizen>
309         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
310         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
311         /// <exception cref="InvalidOperationException">This exception can be due to no data available.</exception>
312         public string GroupName
313         {
314             get
315             {
316                 string groupName;
317                 ErrorCode error = StickerDataGetGroupName(_handle, out groupName);
318                 if (error != ErrorCode.None)
319                 {
320                     Log.Error(LogTag, "GetGroupName Failed with error " + error);
321                     throw ExceptionFactory.CreateException(error);
322                 }
323
324                 return groupName;
325             }
326             set
327             {
328                 ErrorCode error = StickerDataSetGroupName(_handle, value);
329                 if (error != ErrorCode.None)
330                 {
331                     Log.Error(LogTag, "SetGroupName Failed with error " + error);
332                     throw ExceptionFactory.CreateException(error);
333                 }
334             }
335         }
336
337         /// <summary>
338         /// Gets/Sets the thumbnail local path of the sticker data.
339         /// </summary>
340         /// <since_tizen> 10 </since_tizen>
341         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
342         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
343         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
344         public string Thumbnail
345         {
346             get
347             {
348                 string thumbnail;
349                 ErrorCode error = StickerDataGetThumbnail(_handle, out thumbnail);
350                 if (error != ErrorCode.None)
351                 {
352                     Log.Error(LogTag, "GetThumbnail Failed with error " + error);
353                     throw ExceptionFactory.CreateException(error);
354                 }
355
356                 return thumbnail;
357             }
358             set
359             {
360                 ErrorCode error = StickerDataSetThumbnail(_handle, value);
361                 if (error != ErrorCode.None)
362                 {
363                     Log.Error(LogTag, "SetThumbnail Failed with error " + error);
364                     throw ExceptionFactory.CreateException(error);
365                 }
366             }
367         }
368
369         /// <summary>
370         /// Gets/Sets the description of the sticker data.
371         /// </summary>
372         /// <since_tizen> 10 </since_tizen>
373         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
374         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
375         public string Description
376         {
377             get
378             {
379                 string description;
380                 ErrorCode error = StickerDataGetDescription(_handle, out description);
381                 if (error != ErrorCode.None)
382                 {
383                     Log.Error(LogTag, "GetDescription Failed with error " + error);
384                     throw ExceptionFactory.CreateException(error);
385                 }
386
387                 return description;
388             }
389             set
390             {
391                 ErrorCode error = StickerDataSetDescription(_handle, value);
392                 if (error != ErrorCode.None)
393                 {
394                     Log.Error(LogTag, "SetDescription Failed with error " + error);
395                     throw ExceptionFactory.CreateException(error);
396                 }
397             }
398         }
399
400         /// <summary>
401         /// Gets the last update date from sticker data.
402         /// </summary>
403         /// <since_tizen> 10 </since_tizen>
404         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
405         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
406         /// <exception cref="InvalidOperationException">This exception can be due to no data available.</exception>
407         public DateTime LastUpdated
408         {
409             get
410             {
411                 string date;
412                 ErrorCode error = StickerDataGetDate(_handle, out date);
413                 if (error != ErrorCode.None)
414                 {
415                     Log.Error(LogTag, "GetDate Failed with error " + error);
416                     throw ExceptionFactory.CreateException(error);
417                 }
418
419                 return Convert.ToDateTime(date);
420             }
421         }
422
423         /// <summary>
424         /// Gets/Sets the display type of the sticker data.
425         /// </summary>
426         /// <since_tizen> 10 </since_tizen>
427         /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
428         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
429         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
430         public DisplayType DisplayType
431         {
432             get
433             {
434                 DisplayType displayType;
435                 ErrorCode error = StickerDataGetDisplayType(_handle, out displayType);
436                 if (error != ErrorCode.None)
437                 {
438                     Log.Error(LogTag, "GetDisplayType Failed with error " + error);
439                     throw ExceptionFactory.CreateException(error);
440                 }
441
442                 return displayType;
443             }
444             set
445             {
446                 ErrorCode error = StickerDataSetDisplayType(_handle, value);
447                 if (error != ErrorCode.None)
448                 {
449                     Log.Error(LogTag, "SetDisplayType Failed with error " + error);
450                     throw ExceptionFactory.CreateException(error);
451                 }
452             }
453         }
454     }
455 }