[shortcut] Add resultcb for AddToWidget func
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Shortcut / Tizen.Applications.Shortcut / ShortcutManager.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
21     /// <summary>
22     /// This class provides the some functions to add, delete shortcut.
23     /// </summary>
24     public static class ShortcutManager
25     {
26         private const string LogTag = "Tizen.Applications.Shortcut";
27
28         private static Interop.Shortcut.ResultCallback shortcutAddResult = null;
29
30         private static Interop.Shortcut.ResultCallback widgetAddResult = null;
31
32         private static Interop.Shortcut.ResultCallback shortcutDeleteResult = null;
33
34         /// <summary>
35         /// Adds a shortcut on home-screen.
36         /// </summary>
37         /// <since_tizen> 3 </since_tizen>
38         /// <param name="shortcut">Object that contain shortcut info.</param>
39         /// <feature>http://tizen.org/feature/shortcut</feature>
40         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
41         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
42         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
43         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
44         /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
45         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
46         public static void Add(HomeShortcutInfo shortcut)
47         {
48             Interop.Shortcut.ErrorCode err = Interop.Shortcut.ErrorCode.None;
49
50             try
51             {
52                 int type;
53
54                 if (string.IsNullOrEmpty(shortcut.Uri))
55                 {
56                     type = 0;
57                 }
58                 else
59                 {
60                     type = 1;
61                 }
62
63                 if (shortcutAddResult == null)
64                 {
65                     shortcutAddResult = new Interop.Shortcut.ResultCallback(ShortcutAddResultCallback);
66                 }
67
68                 err = Interop.Shortcut.AddToHome(shortcut.ShortcutName, type, shortcut.Uri, shortcut.IconPath, Convert.ToInt32(shortcut.IsAllowDuplicate), shortcutAddResult, IntPtr.Zero);
69                 if (err != Interop.Shortcut.ErrorCode.None)
70                 {
71                     throw ShortcutErrorFactory.GetException(err, "unable to add shortcut");
72                 }
73             }
74             catch (Exception e)
75             {
76                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.IoError, e.Message);
77             }
78         }
79
80         /// <summary>
81         /// Adds a shortcut on home-screen.
82         /// </summary>
83         /// <since_tizen> 3 </since_tizen>
84         /// <param name="shortcut">Object that contain shortcut info.</param>
85         /// <feature>http://tizen.org/feature/shortcut</feature>
86         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
87         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
88         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
89         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
90         /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
91         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
92         public static void Add(WidgetShortcutInfo shortcut)
93         {
94             Interop.Shortcut.ErrorCode err = Interop.Shortcut.ErrorCode.None;
95
96             if (shortcut.Period < 0.0)
97             {
98                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, "Invalid parameter");
99             }
100
101             try
102             {
103                 if (widgetAddResult == null)
104                 {
105                     widgetAddResult = new Interop.Shortcut.ResultCallback(WidgetAddResultCallback);
106                 }
107
108                 err = Interop.Shortcut.AddToWidget(shortcut.ShortcutName, shortcut.WidgetSize, shortcut.WidgetId, shortcut.IconPath, shortcut.Period, Convert.ToInt32(shortcut.IsAllowDuplicate), widgetAddResult, IntPtr.Zero);
109                 if (err != Interop.Shortcut.ErrorCode.None)
110                 {
111                     throw ShortcutErrorFactory.GetException(err, "unable to add widget");
112                 }
113             }
114             catch (Exception e)
115             {
116                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.IoError, e.Message);
117             }
118         }
119
120         /// <summary>
121         /// Removes a shortcut from home by ShortcutName.
122         /// </summary>
123         /// <since_tizen> 3 </since_tizen>
124         /// <param name="shortcutName">Shortcut name string.</param>
125         /// <feature>http://tizen.org/feature/shortcut</feature>
126         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
127         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
128         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
129         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
130         /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
131         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
132         public static void Delete(string shortcutName)
133         {
134             Interop.Shortcut.ErrorCode err = Interop.Shortcut.ErrorCode.None;
135
136             if (shortcutName == null)
137             {
138                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, "Invalid parameter");
139             }
140
141             try
142             {
143                 if (shortcutDeleteResult == null)
144                 {
145                     shortcutDeleteResult = new Interop.Shortcut.ResultCallback(DeleteResultCallback);
146                 }
147
148                 err = Interop.Shortcut.Delete(shortcutName, shortcutDeleteResult, IntPtr.Zero);
149                 if (err != Interop.Shortcut.ErrorCode.None)
150                 {
151                     throw ShortcutErrorFactory.GetException(err, "unable to delete shortcut");
152                 }
153             }
154             catch (Exception e)
155             {
156                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.IoError, e.Message);
157             }
158         }
159
160         /// <summary>
161         /// Removes a shortcut from home by ShortcutInfo.
162         /// </summary>
163         /// <since_tizen> 3 </since_tizen>
164         /// <param name="shortcut">Object that contain shortcut info.</param>
165         /// <feature>http://tizen.org/feature/shortcut</feature>
166         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
167         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
168         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
169         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
170         /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
171         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
172         public static void Delete(ShortcutInfo shortcut)
173         {
174             if (shortcut == null)
175             {
176                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, "Invalid parameter");
177             }
178
179             try
180             {
181                 Delete(shortcut.ShortcutName);
182             }
183             catch (Exception e)
184             {
185                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.IoError, e.Message);
186             }
187         }
188
189         private static int ShortcutAddResultCallback(int ret, IntPtr data)
190         {
191             if (ret != (int)Interop.Shortcut.ErrorCode.None)
192             {
193                 throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode)ret, "unable to add shortcut");
194             }
195
196             return 0;
197         }
198
199         private static int WidgetAddResultCallback(int ret, IntPtr data)
200         {
201             if (ret != (int)Interop.Shortcut.ErrorCode.None)
202             {
203                 throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode)ret, "unable to add widget");
204             }
205
206             return 0;
207         }
208
209         private static int DeleteResultCallback(int ret, IntPtr data)
210         {
211             if (ret != (int)Interop.Shortcut.ErrorCode.None)
212             {
213                 throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode)ret, "unable to delete shortcut");
214             }
215
216             return 0;
217         }
218     }
219 }