Add exception handling
[platform/core/csapi/tizenfx.git] / Tizen.Applications / Tizen.Applications.Managers / InstalledApplicationFilter.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 using System;
10 using System.Collections.Generic;
11
12 namespace Tizen.Applications.Managers
13 {
14     /// <summary>
15     /// InstalledApplicationFilter class. This class is a parameter of InstallerApplicationAppsAsync method.
16     /// </summary>
17     public class InstalledApplicationFilter : IDisposable
18     {
19         /// <summary>
20         /// Keys class. This class is a possible key to use in the InstalledApplicationFilter.
21         /// </summary>
22         public static class Keys
23         {
24             /// <summary>
25             ///
26             /// </summary>
27             public const string Id = "PACKAGE_INFO_PROP_APP_ID";
28             /// <summary>
29             ///
30             /// </summary>
31             public const string Type = "PACKAGE_INFO_PROP_APP_TYPE";
32             /// <summary>
33             ///
34             /// </summary>
35             public const string Category = "PACKAGE_INFO_PROP_APP_CATEGORY";
36             /// <summary>
37             ///
38             /// </summary>
39             public const string NoDisplay = "PACKAGE_INFO_PROP_APP_NODISPLAY";
40             /// <summary>
41             ///
42             /// </summary>
43             public const string TaskManage = "PACKAGE_INFO_PROP_APP_TASKMANAGE";
44         }
45
46         private IntPtr _handle;
47         private bool disposed = false;
48
49         private const string LogTag = "Tizen.Applications.Managers";
50         private int ret = 0;
51
52         public InstalledApplicationFilter(IDictionary<string, string> filter)
53         {
54             ret = Interop.ApplicationManager.AppInfoFilterCreate(out _handle);
55             if (ret != 0)
56             {
57                 ApplicationManagerErrorFactory.ExceptionChecker(ret, _handle, "InstalledApplicationFilter creation failed.");
58             }
59             foreach (var item in filter)
60             {
61                 if ((item.Key == Keys.Id) || (item.Key == Keys.Type) || (item.Key == Keys.Category))
62                 {
63                     ret = Interop.ApplicationManager.AppInfoFilterAddString(_handle, item.Key, item.Value);
64                     if (ret != 0)
65                     {
66                         ApplicationManagerErrorFactory.ExceptionChecker(ret, _handle, "InstalledApplicationFilter item add failed.");
67                     }
68                 }
69                 else if ((item.Key == Keys.NoDisplay) || (item.Key == Keys.TaskManage))
70                 {
71                     ret = Interop.ApplicationManager.AppInfoFilterAddBool(_handle, item.Key, Convert.ToBoolean(item.Value));
72                     if (ret != 0)
73                     {
74                         ApplicationManagerErrorFactory.ExceptionChecker(ret, _handle, "InstalledApplicationFilter item add failed.");
75                     }
76                 }
77                 else
78                 {
79                     Log.Warn(LogTag, "InstalledApplicationFilter is NOT supported " + item.Key + " key.");
80                 }
81             }
82         }
83
84         internal IntPtr Handle
85         {
86             get
87             {
88                 return _handle;
89             }
90         }
91
92         ~InstalledApplicationFilter()
93         {
94             Dispose(false);
95         }
96
97         public void Dispose()
98         {
99             Dispose(true);
100             GC.SuppressFinalize(this);
101         }
102
103         private void Dispose(bool disposing)
104         {
105             if (disposed)
106                 return;
107             if (disposing)
108             {
109                 // to be used if there are any other disposable objects
110             }
111             if (_handle != IntPtr.Zero)
112             {
113                 Interop.ApplicationManager.AppInfoFilterDestroy(_handle);
114             }
115             disposed = true;
116         }
117     }
118
119 }