Setting since_tizen 3/4 on Tizen.NET API
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Shortcut / Tizen.Applications.ShortcutEvent / ShortcutEventManager.cs
1 /*
2  * Copyright (c) 2017 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 namespace Tizen.Applications.Shortcut
18 {
19     using System;
20     using System.Collections.Generic;
21     using Tizen.Internals.Errors;
22
23     /// <summary>
24     /// The callback function that is invoked when add request occurred
25     /// </summary>
26     /// <param name="args">Object that contain shortcut info to add.</param>
27     /// <returns>The result of handling a shortcut add request</returns>
28     /// <since_tizen> 4 </since_tizen>
29     public delegate ShortcutError ShortcutAdded(ShortcutAddedInfo args);
30
31     /// <summary>
32     /// The callback function that is invoked when delete request occurred
33     /// </summary>
34     /// <param name="args">Object that contain shortcut info to delete.</param>
35     /// <returns>The result of handling a shortcut delete request</returns>
36     /// <since_tizen> 4 </since_tizen>
37     public delegate ShortcutError ShortcutDeleted(ShortcutDeletedInfo args);
38
39     /// <summary>
40     /// This class provides a way to register callback function for shortcut add, delete events.
41     /// </summary>
42     /// <since_tizen> 4 </since_tizen>
43     public static class ShortcutEventManager
44     {
45         private static Interop.Shortcut.AddCallback shortcutAddCallback;
46
47         private static Interop.Shortcut.DeleteCallback shortcutDeleteCallback;
48
49         private static IList<ShortcutTemplate> shortcutTemplates = new List<ShortcutTemplate>();
50
51         private static ShortcutAdded shortcutAdded = null;
52
53         private static ShortcutDeleted shortcutDeleted = null;
54
55         /// <summary>
56         /// Registers a callback function to listen requests from applications.
57         /// </summary>
58         /// <since_tizen> 4 </since_tizen>
59         /// <param name="addedEvent">The callback function pointer that is invoked when Add() is requested</param>
60         /// <feature>http://tizen.org/feature/shortcut</feature>
61         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
62         /// <remarks>
63         /// Previous registered delegate function should be unregister.
64         /// </remarks>
65         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
66         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
67         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
68         /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
69         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
70         public static void RegisterEventHandler(ShortcutAdded addedEvent)
71         {
72             if (shortcutAddCallback == null)
73             {
74                 shortcutAddCallback = new Interop.Shortcut.AddCallback(AddCallback);
75
76                 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutAddCallback(shortcutAddCallback, IntPtr.Zero);
77                 if (err != Interop.Shortcut.ErrorCode.None)
78                 {
79                     shortcutAddCallback = null;
80                     throw ShortcutErrorFactory.GetException(err, "unable to register callback");
81                 }
82
83                 shortcutAdded = addedEvent;
84             }
85             else
86             {
87                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
88             }
89         }
90
91         /// <summary>
92         /// Registers a callback function to listen requests from applications.
93         /// </summary>
94         /// <since_tizen> 4 </since_tizen>
95         /// <param name="deletedEvent">The callback function pointer that is invoked when Delete() is requested</param>
96         /// <feature>http://tizen.org/feature/shortcut</feature>
97         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
98         /// <remarks>
99         /// Previous registered delegate function should be unregister.
100         /// </remarks>
101         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
102         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
103         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
104         /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
105         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
106         public static void RegisterEventHandler(ShortcutDeleted deletedEvent)
107         {
108             if (shortcutDeleteCallback == null)
109             {
110                 shortcutDeleteCallback = new Interop.Shortcut.DeleteCallback(DeleteCallback);
111
112                 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutDeleteCallback(shortcutDeleteCallback, IntPtr.Zero);
113                 if (err != Interop.Shortcut.ErrorCode.None)
114                 {
115                     shortcutDeleteCallback = null;
116                     throw ShortcutErrorFactory.GetException(err, "unable to register callback");
117                 }
118
119                 shortcutDeleted = deletedEvent;
120             }
121             else
122             {
123                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
124             }
125         }
126
127         /// <summary>
128         /// Unregisters a callback for the shortcut request.
129         /// </summary>
130         /// <since_tizen> 4 </since_tizen>
131         /// <param name="addedEvent">The callback function pointer that used for RegisterCallback</param>
132         /// <feature>http://tizen.org/feature/shortcut</feature>
133         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
134         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
135         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
136         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
137         public static void UnregisterEventHandler(ShortcutAdded addedEvent)
138         {
139             if (shortcutAdded != null && shortcutAdded.Equals(addedEvent))
140             {
141                 shortcutAdded = null;
142
143                 if (shortcutAddCallback != null)
144                 {
145                     Interop.Shortcut.UnsetShortcutAddCallback();
146                     shortcutAddCallback = null;
147
148                     int err = ErrorFacts.GetLastResult();
149                     if (err != (int)Interop.Shortcut.ErrorCode.None)
150                     {
151                         throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode)err, "unable to unregister callback");
152                     }
153                 }
154             }
155             else
156             {
157                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
158             }
159         }
160
161         /// <summary>
162         /// Unregisters a callback for the shortcut request.
163         /// </summary>
164         /// <since_tizen> 4 </since_tizen>
165         /// <param name="deletedEvent">The callback function pointer that used for RegisterCallback</param>
166         /// <feature>http://tizen.org/feature/shortcut</feature>
167         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
168         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
169         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
170         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
171         public static void UnregisterEventHandler(ShortcutDeleted deletedEvent)
172         {
173             if (shortcutDeleted != null && shortcutDeleted.Equals(deletedEvent))
174             {
175                 shortcutDeleted = null;
176
177                 if (shortcutDeleteCallback != null)
178                 {
179                     Interop.Shortcut.UnsetShortcutDeleteCallback();
180                     shortcutDeleteCallback = null;
181
182                     int err = ErrorFacts.GetLastResult();
183                     if (err != (int)Interop.Shortcut.ErrorCode.None)
184                     {
185                         throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode) err, "unable to unregister callback");
186                     }
187                 }
188             }
189             else
190             {
191                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
192             }
193         }
194
195         /// <summary>
196         /// Gets the preset list of shortcut template from the installed package.
197         /// </summary>
198         /// <since_tizen> 4 </since_tizen>
199         /// <param name="appId">Application ID.</param>
200         /// <returns>The List of ShortcutTemplate.</returns>
201         /// <feature>http://tizen.org/feature/shortcut</feature>
202         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
203         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
204         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
205         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
206         /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
207         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
208         public static IEnumerable<ShortcutTemplate> GetTemplateList(string appId)
209         {
210             shortcutTemplates.Clear();
211
212             if (string.IsNullOrEmpty(appId))
213             {
214                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
215             }
216
217             Interop.Shortcut.ListCallback callback = (appName, iconPath, shortcutName, extrakey, extraData, user_data) =>
218             {
219                 ShortcutTemplate template = new ShortcutTemplate
220                 {
221                     AppId = appName,
222                     ShortcutName = shortcutName,
223                     IconPath = iconPath,
224                     ExtraKey = extrakey,
225                     ExtraData = extraData,
226                 };
227
228                 shortcutTemplates.Add(template);
229
230                 return 0;
231             };
232
233             Interop.Shortcut.ErrorCode err = Interop.Shortcut.GetList(appId, callback, IntPtr.Zero);
234             if (err < Interop.Shortcut.ErrorCode.None)
235             {
236                 throw ShortcutErrorFactory.GetException(err, "unable to get ShortcutTemplate Lists");
237             }
238
239             return shortcutTemplates;
240         }
241
242         private static int AddCallback(string appId, string shortcutName, int type, string contentInfo, string iconPath, int processId, double period, bool isAllowDuplicate, IntPtr data)
243         {
244             ShortcutError err;
245
246             if (type == (int)ShortcutType.LaunchByApp || type == (int)ShortcutType.LaunchByUri)
247             {
248                 HomeShortcutAddedInfo shortcutInfo = new HomeShortcutAddedInfo
249                 {
250                     ShortcutName = shortcutName,
251                     IconPath = iconPath,
252                     IsAllowDuplicate = isAllowDuplicate,
253                     AppId = appId,
254                 };
255
256                 if (contentInfo != null && contentInfo != String.Empty)
257                 {
258                     shortcutInfo.Uri = contentInfo;
259                 }
260
261                 if (shortcutAdded != null)
262                 {
263                     err = shortcutAdded(shortcutInfo);
264                 }
265                 else
266                 {
267                     err = ShortcutError.IoError;
268                 }
269             }
270             else
271             {
272                 WidgetShortcutAddedInfo shortcutInfo = new WidgetShortcutAddedInfo
273                 {
274                     ShortcutName = shortcutName,
275                     IconPath = iconPath,
276                     IsAllowDuplicate = isAllowDuplicate,
277                     WidgetId = appId,
278                     WidgetSize = (ShortcutWidgetSize)type,
279                     Period = period,
280                 };
281
282                 if (shortcutAdded != null)
283                 {
284                     err = shortcutAdded(shortcutInfo);
285                 }
286                 else
287                 {
288                     err = ShortcutError.IoError;
289                 }
290             }
291
292             return (int)err;
293         }
294
295         private static int DeleteCallback(string appId, string shortcutName, int processId, IntPtr data)
296         {
297             ShortcutError err = ShortcutError.None;
298
299             ShortcutDeletedInfo deletedInfo = new ShortcutDeletedInfo
300             {
301                 AppId = appId,
302                 ShortcutName = shortcutName,
303             };
304
305             if (shortcutDeleted != null)
306             {
307                 err = shortcutDeleted(deletedInfo);
308             }
309             else
310             {
311                 err = ShortcutError.IoError;
312             }
313
314             return (int)err;
315         }
316     }
317 }