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