[DevicePolicyManager] Deprecate DevicePolicyManager APIs (#5180)
[platform/core/csapi/tizenfx.git] / src / Tizen.Security.DevicePolicyManager / Tizen.Security.DevicePolicyManager / EmailPolicy.cs
1 /*
2  *  Copyright (c) 2018 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 using System;
18
19 namespace Tizen.Security.DevicePolicyManager
20 {
21     /// <summary>
22     /// The EmailPolicy provides methods to manage email policies.
23     /// </summary>
24     /// <since_tizen> 6 </since_tizen>
25     /// <remarks>The EmailPolicy is created by <seealso cref="DevicePolicyManager.GetPolicy{T}"/>. and the DevicePolicyManager instance must exists when using the EmailPolicy.</remarks>
26     [Obsolete("Deprecated since API level 11.")]
27     public class EmailPolicy : DevicePolicy, IDisposable
28     {
29         /// <summary>
30         /// The PopImap email policy name. This represents <see cref="EmailPolicy.IsPopImapAllowed"/>.
31         /// </summary>
32         /// <remarks>This is used in <see cref="PolicyChangedEventArgs.PolicyName"/>.</remarks>
33         /// <since_tizen> 6 </since_tizen>
34         [Obsolete("Deprecated since API level 11.")]
35         public static readonly string PopImapEmailPolicyName = "PopImapEmail";
36
37         private readonly string _popImapPolicyName = "popimap_email";
38         private int _popImapCallbackId;
39         private bool _disposed = false;
40
41         private Interop.DevicePolicyManager.PolicyChangedCallback _popImapPolicyChangedCallback;
42         private EventHandler<PolicyChangedEventArgs> _popImapPolicyChanged;
43  
44         internal EmailPolicy(DevicePolicyManager dpm) : base(dpm)
45         {
46         }
47
48         /// <summary>
49         /// A Destructor of EmailPolicy.
50         /// </summary>
51         [Obsolete("Deprecated since API level 11.")]
52         ~EmailPolicy()
53         {
54             this.Dispose(false);
55         }
56
57         /// <summary>
58         /// Gets whether the access to POP or IMAP email is allowed or not.
59         /// </summary>
60         /// <value>true if the POP or IMAP email is allowed, false otherwise. The default value is true.</value>
61         /// <since_tizen> 6 </since_tizen>
62         [Obsolete("Deprecated since API level 11.")]
63         public bool IsPopImapAllowed
64         {
65             get
66             {
67                 int state;
68                 int ret = Interop.DevicePolicyManager.RestrictionGetPopimapEmailState(_dpm.GetHandle(), out state);
69                 if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
70                 {
71                     Log.Error(Globals.LogTag, "Failed to get popimap email policy " + ret);
72                     return true;
73                 }
74
75                 return state == 1;
76             }
77         }
78
79         /// <summary>
80         /// Releases any unmanaged resources used by this object.
81         /// </summary>
82         [Obsolete("Deprecated since API level 11.")]
83         public void Dispose()
84         {
85             this.Dispose(true);
86             GC.SuppressFinalize(this);
87         }
88
89         /// <summary>
90         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
91         /// </summary>
92         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
93         [Obsolete("Deprecated since API level 11.")]
94         protected virtual void Dispose(bool disposing)
95         {
96             if (!_disposed)
97             {
98                 if (disposing)
99                 {
100                     // to be used if there are any other disposable objects
101                 }
102
103                 if (_popImapCallbackId != 0)
104                 {
105                     try
106                     {
107                         RemovePopImapPolicyChangedCallback();
108                     }
109                     catch (Exception e)
110                     {
111                         Log.Error(Globals.LogTag, e.ToString());
112                     }
113                 }
114
115                 _disposed = true;
116             }
117         }
118
119         /// <summary>
120         /// The PopImapPolicyChanged event is raised when the popimap-email policy is changed.
121         /// </summary>
122         /// <remarks>This event will be removed automatically when EmailPolicy is destroyed.</remarks>
123         /// <since_tizen> 6 </since_tizen>
124         [Obsolete("Deprecated since API level 11.")]
125         public event EventHandler<PolicyChangedEventArgs> PopImapPolicyChanged
126         {
127             add
128             {
129                 if (_popImapPolicyChanged == null)
130                 {
131                     AddPopImapPolicyChangedCallback();
132                 }
133
134                 _popImapPolicyChanged += value;
135             }
136
137             remove
138             {
139                 _popImapPolicyChanged -= value;
140                 if (_popImapPolicyChanged == null)
141                 {
142                     RemovePopImapPolicyChangedCallback();
143                 }
144             }
145         }
146
147         private void AddPopImapPolicyChangedCallback()
148         {
149             if (_popImapPolicyChangedCallback == null)
150             {
151                 _popImapPolicyChangedCallback = (string name, string state, IntPtr userData) =>
152                 {
153                     _popImapPolicyChanged?.Invoke(this, new PolicyChangedEventArgs(PopImapEmailPolicyName, state));
154                 };
155             }
156
157             int ret = Interop.DevicePolicyManager.AddPolicyChangedCallback(_dpm.GetHandle(), _popImapPolicyName, _popImapPolicyChangedCallback, IntPtr.Zero, out _popImapCallbackId);
158             if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
159             {
160                 Log.Error(Globals.LogTag, "Failed to add policy changed callback, name " + _popImapPolicyName + ", ret : " + ret);
161                 throw DevicePolicyManagerErrorFactory.CreateException(ret);
162             }
163         }
164
165         private void RemovePopImapPolicyChangedCallback()
166         {
167             int ret = Interop.DevicePolicyManager.RemovePolicyChangedCallback(_dpm.GetHandle(), _popImapCallbackId);
168             if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
169             {
170                 Log.Error(Globals.LogTag, "Failed to remove policy changed callback, name " + _popImapPolicyName + ", ret : " + ret);
171                 throw DevicePolicyManagerErrorFactory.CreateException(ret);
172             }
173
174             _popImapPolicyChangedCallback = null;
175             _popImapCallbackId = 0;
176         }
177     }
178 }