Refactor AppFW
[platform/core/csapi/tizenfx.git] / Tizen.Applications / Tizen.Applications / AppControl.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
8
9
10 using System;
11 using System.Collections.Generic;
12 using System.Runtime.InteropServices;
13 using Tizen.Internals.Errors;
14
15 namespace Tizen.Applications
16 {
17     /// <summary>
18     /// The AppControl to launch other application or an actor or a service.
19     /// </summary>
20     public class AppControl
21     {
22         private const string LogTag = "Tizen.Applications";
23
24         internal Interop.AppControl.SafeAppControlHandle _handle;
25
26         private string _operation = null;
27         private string _mime = null;
28         private string _uri = null;
29         private string _category = null;
30         private string _applicationId = null;
31
32         private ExtraDataCollection _extraData = null;
33
34         /// <summary>
35         /// 
36         /// </summary>
37         public event EventHandler<AppControlReplyReceivedEventArgs> AppControlReplyReceived;
38
39         /// <summary>
40         ///
41         /// </summary>
42         public AppControl()
43         {
44             ErrorCode err = Interop.AppControl.Create(out _handle);
45             if (err != ErrorCode.None)
46             {
47                 throw new InvalidOperationException("Failed to create the appcontrol handle. Err = " + err);
48             }
49         }
50
51         #region Public Properties
52
53         /// <summary>
54         /// The operation to be performed.
55         /// </summary>
56         public string Operation
57         {
58             get
59             {
60                 if (String.IsNullOrEmpty(_operation))
61                 {
62                     ErrorCode err = Interop.AppControl.GetOperation(_handle, out _operation);
63                     if (err != ErrorCode.None)
64                     {
65                         Log.Warn(LogTag, "Failed to get the operation from the appcontrol. Err = " + err);
66                     }
67                 }
68                 return _operation;
69             }
70             set
71             {
72                 ErrorCode err = Interop.AppControl.SetOperation(_handle, value);
73                 if (err == ErrorCode.None)
74                 {
75                     _operation = value;
76                 }
77                 else
78                 {
79                     Log.Warn(LogTag, "Failed to set the operation to the appcontrol. Err = " + err);
80                 }
81             }
82         }
83
84         /// <summary>
85         /// The explicit MIME type of the data.
86         /// </summary>
87         public string Mime
88         {
89             get
90             {
91                 if (String.IsNullOrEmpty(_mime))
92                 {
93                     ErrorCode err = Interop.AppControl.GetMime(_handle, out _mime);
94                     if (err != ErrorCode.None)
95                     {
96                         Log.Warn(LogTag, "Failed to get the mime from the appcontrol. Err = " + err);
97                     }
98                 }
99                 return _mime;
100             }
101             set
102             {
103                 ErrorCode err = Interop.AppControl.SetMime(_handle, value);
104                 if (err == ErrorCode.None)
105                 {
106                     _mime = value;
107                 }
108                 else
109                 {
110                     Log.Warn(LogTag, "Failed to set the mime to the appcontrol. Err = " + err);
111                 }
112             }
113         }
114
115         /// <summary>
116         /// The URI of the data.
117         /// </summary>
118         public string Uri
119         {
120             get
121             {
122                 if (String.IsNullOrEmpty(_uri))
123                 {
124                     ErrorCode err = Interop.AppControl.GetUri(_handle, out _uri);
125                     if (err != ErrorCode.None)
126                     {
127                         Log.Warn(LogTag, "Failed to get the uri from the appcontrol. Err = " + err);
128                     }
129                 }
130                 return _uri;
131             }
132             set
133             {
134                 ErrorCode err = Interop.AppControl.SetUri(_handle, value);
135                 if (err == ErrorCode.None)
136                 {
137                     _uri = value;
138                 }
139                 else
140                 {
141                     Log.Warn(LogTag, "Failed to set the uri to the appcontrol. Err = " + err);
142                 }
143             }
144         }
145
146         /// <summary>
147         /// 
148         /// </summary>
149         public string Category
150         {
151             get
152             {
153                 if (String.IsNullOrEmpty(_category))
154                 {
155                     ErrorCode err = Interop.AppControl.GetCategory(_handle, out _category);
156                     if (err != ErrorCode.None)
157                     {
158                         Log.Warn(LogTag, "Failed to get the category from the appcontrol. Err = " + err);
159                     }
160                 }
161                 return _category;
162             }
163             set
164             {
165                 ErrorCode err = Interop.AppControl.SetCategory(_handle, value);
166                 if (err == ErrorCode.None)
167                 {
168                     _category = value;
169                 }
170                 else
171                 {
172                     Log.Warn(LogTag, "Failed to set the category to the appcontrol. Err = " + err);
173                 }
174             }
175         }
176
177         /// <summary>
178         /// 
179         /// </summary>
180         public string ApplicationId
181         {
182             get
183             {
184                 if (String.IsNullOrEmpty(_applicationId))
185                 {
186                     ErrorCode err = Interop.AppControl.GetAppId(_handle, out _applicationId);
187                     if (err != ErrorCode.None)
188                     {
189                         Log.Warn(LogTag, "Failed to get the appId from the appcontrol. Err = " + err);
190                     }
191                 }
192                 return _applicationId;
193             }
194             set
195             {
196                 ErrorCode err = Interop.AppControl.SetAppId(_handle, value);
197                 if (err == ErrorCode.None)
198                 {
199                     _applicationId = value;
200                 }
201                 else
202                 {
203                     Log.Warn(LogTag, "Failed to set the appId to the appcontrol. Err = " + err);
204                 }
205             }
206         }
207
208         /// <summary>
209         /// 
210         /// </summary>
211         public AppControlLaunchMode LaunchMode
212         {
213             get
214             {
215                 int value = 0;
216                 ErrorCode err = Interop.AppControl.GetLaunchMode(_handle, out value);
217                 if (err != ErrorCode.None)
218                 {
219                     Log.Warn(LogTag, "Failed to get the launchMode from the appcontrol. Err = " + err);
220                 }
221                 return (AppControlLaunchMode)value;
222             }
223             set
224             {
225                 ErrorCode err = Interop.AppControl.SetLaunchMode(_handle, (int)value);
226                 if (err != ErrorCode.None)
227                 {
228                     Log.Warn(LogTag, "Failed to set the launchMode to the appcontrol. Err = " + err);
229                 }
230             }
231         }
232
233         public ExtraDataCollection ExtraData
234         {
235             get
236             {
237                 if (_extraData == null)
238                 {
239                     _extraData = new ExtraDataCollection();
240                 }
241                 return _extraData;
242             }
243         }
244
245         #endregion // Public Properties
246
247         /// <summary>
248         /// 
249         /// </summary>
250         /// <returns></returns>
251         public static IEnumerable<string> GetMatchedApplicationIds(AppControl control)
252         {
253             List<string> ids = new List<string>();
254             Interop.AppControl.AppMatchedCallback callback = new Interop.AppControl.AppMatchedCallback(
255                 (handle, applicationId, userData) =>
256                 {
257                     List<string> idsList = Marshal.GetObjectForIUnknown(userData) as List<string>;
258                     if (idsList != null)
259                     {
260                         idsList.Add(applicationId);
261                         return true;
262                     }
263                     else
264                     {
265                         return false;
266                     }
267                 });
268
269             IntPtr pointerToApplicationIds = Marshal.GetIUnknownForObject(ids);
270             if (pointerToApplicationIds != null)
271             {
272                 ErrorCode err = Interop.AppControl.ForeachAppMatched(control._handle, callback, pointerToApplicationIds);
273                 if (err != ErrorCode.None)
274                 {
275                     throw new InvalidOperationException("Failed to get matched appids. err = " + err);
276                 }
277                 return ids;
278             }
279
280             return ids;
281         }
282
283         /// <summary>
284         /// 
285         /// </summary>
286         /// <param name="request"></param>
287         public static void SendLaunchRequest(AppControl request)
288         {
289             throw new NotImplementedException();
290         }
291
292         /// <summary>
293         /// 
294         /// </summary>
295         /// <param name="request"></param>
296         public static void SendLaunchRequestForReply(AppControl request)
297         {
298             throw new NotImplementedException();
299         }
300
301         /// <summary>
302         /// 
303         /// </summary>
304         public class ExtraDataCollection
305         {
306             /// <summary>
307             /// 
308             /// </summary>
309             /// <param name="key"></param>
310             /// <param name="value"></param>
311             public void Add(string key, string value)
312             {
313                 throw new NotImplementedException();
314             }
315
316             /// <summary>
317             /// 
318             /// </summary>
319             /// <param name="key"></param>
320             /// <param name="value"></param>
321             public void Add(string key, IEnumerable<string> value)
322             {
323                 throw new NotImplementedException();
324             }
325
326             /// <summary>
327             /// 
328             /// </summary>
329             /// <typeparam name="T"></typeparam>
330             /// <param name="key"></param>
331             /// <returns></returns>
332             public T Get<T>(string key)
333             {
334                 throw new NotImplementedException();
335             }
336
337             /// <summary>
338             /// 
339             /// </summary>
340             /// <param name="key"></param>
341             /// <returns></returns>
342             public object Get(string key)
343             {
344                 throw new NotImplementedException();
345             }
346
347             /// <summary>
348             /// 
349             /// </summary>
350             /// <param name="key"></param>
351             public void Remove(string key)
352             {
353                 throw new NotImplementedException();
354             }
355         }
356
357     }
358 }