[Tizen.Messaging]Fix XML Doc warnings
[platform/core/csapi/tizenfx.git] / src / Tizen.Messaging / Tizen.Messaging.Messages / Message.cs
1 /*
2 * Copyright (c) 2016 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 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
20
21 namespace Tizen.Messaging.Messages
22 {
23     /// <summary>
24     /// This class represents all the messages.
25     /// </summary>
26     public abstract class Message : IDisposable
27     {
28         internal IntPtr _messageHandle = IntPtr.Zero;
29         private bool disposed = false;
30         private int _memoryPressureSize = IntPtr.Size * 11 + sizeof(int) * 5 + sizeof(bool) * 5 + sizeof(short) * 2 + sizeof(byte) * 1176;
31
32         private ICollection<MessagesAddress> _from = new Collection<MessagesAddress>();
33         internal ICollection<MessagesAddress> _to = new Collection<MessagesAddress>();
34         internal ICollection<MessagesAddress> _cc = new Collection<MessagesAddress>();
35         internal ICollection<MessagesAddress> _bcc = new Collection<MessagesAddress>();
36
37         internal Message(MessageType type)
38         {
39             int ret = Interop.Messages.CreateMessage((int)type, out _messageHandle);
40             if (ret != (int)MessagesError.None)
41             {
42                 Log.Error(Globals.LogTag, "Failed to create message handle, Error - " + (MessagesError)ret);
43                 MessagesErrorFactory.ThrowMessagesException(ret);
44             }
45
46             GC.AddMemoryPressure(_memoryPressureSize);
47         }
48
49         internal Message(IntPtr messageHandle)
50         {
51             _messageHandle = messageHandle;
52             GetAllAddresses();
53             GC.AddMemoryPressure(_memoryPressureSize);
54         }
55
56         internal void FillHandle()
57         {
58             SetAddresses();
59             (this as MmsMessage)?.SetAttachments();
60         }
61
62         /// <summary>
63         /// Destructor
64         /// </summary>
65         ~Message()
66         {
67             Dispose(false);
68         }
69
70         /// <summary>
71         /// Releases all resources used by the Message.
72         /// </summary>
73         public void Dispose()
74         {
75             Dispose(true);
76             GC.SuppressFinalize(this);
77             GC.RemoveMemoryPressure(_memoryPressureSize);
78         }
79
80         private void Dispose(bool disposing)
81         {
82             if (disposed)
83                 return;
84
85             if (disposing)
86             {
87                 // Free managed objects
88             }
89
90             // Free unmanaged objects
91             if (_messageHandle != IntPtr.Zero)
92             {
93                 Interop.Messages.DestroyMessage(_messageHandle);
94                 _messageHandle = IntPtr.Zero;
95             }
96             disposed = true;
97         }
98
99         internal IntPtr GetHandle()
100         {
101             return _messageHandle;
102         }
103
104         private void SetAddresses()
105         {
106             foreach (var it in _to)
107             {
108                 AddAddress(it);
109             }
110
111             foreach (var it in _cc)
112             {
113                 AddAddress(it);
114             }
115
116             foreach (var it in _bcc)
117             {
118                 AddAddress(it);
119             }
120         }
121
122         private void AddAddress(MessagesAddress address)
123         {
124             int ret = Interop.Messages.AddAddress(_messageHandle, address.Number, (int)address.Type);
125             if (ret != (int)MessagesError.None)
126             {
127                 Log.Error(Globals.LogTag, "Failed to add address, Error - " + (MessagesError)ret);
128                 MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
129             }
130         }
131
132         private void GetAllAddresses()
133         {
134             int count;
135
136             int ret = Interop.Messages.GetAddressCount(_messageHandle, out count);
137             if (ret != (int)MessagesError.None)
138             {
139                 Log.Error(Globals.LogTag, "Failed to get address count, Error - " + (MessagesError)ret);
140                 MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
141             }
142
143             string number;
144             int type;
145             var To = new Collection<MessagesAddress>();
146             var Cc = new Collection<MessagesAddress>();
147             var Bcc = new Collection<MessagesAddress>();
148             var From = new Collection<MessagesAddress>();
149
150             for (int i = 0; i < count; i++)
151             {
152                 ret = Interop.Messages.GetAddress(_messageHandle, i, out number, out type);
153                 if (ret != (int)MessagesError.None)
154                 {
155                     Log.Error(Globals.LogTag, "Failed to get address, Error - " + (MessagesError)ret);
156                     MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
157                 }
158
159                 var addressItem = new MessagesAddress((RecipientType)type, number);
160                 switch ((RecipientType)type)
161                 {
162                     case RecipientType.To:
163                         To.Add(addressItem);
164                         break;
165                     case RecipientType.Cc:
166                         Cc.Add(addressItem);
167                         break;
168                     case RecipientType.Bcc:
169                         Bcc.Add(addressItem);
170                         break;
171                     default:
172                         From.Add(addressItem);
173                         break;
174                 }
175             }
176
177             _to = To;
178             _cc = Cc;
179             _bcc = Bcc;
180             _from = From;
181         }
182
183         /// <summary>
184         /// The message ID.
185         /// </summary>
186         /// <remarks>
187         /// After creating the Message object, the default value of this property is 0. After sending, this value is changed.
188         /// </remarks>
189         public int Id
190         {
191             get
192             {
193                 int id = 0;
194                 int ret = Interop.Messages.GetMessageId(_messageHandle, out id);
195                 if (ret != (int)MessagesError.None)
196                 {
197                     Log.Error(Globals.LogTag, "Failed to get message id, Error - " + (MessagesError)ret);
198                 }
199
200                 return id;
201             }
202         }
203
204         /// <summary>
205         /// The destination port of the message.
206         /// </summary>
207         public int Port
208         {
209             get
210             {
211                 int port = 0;
212                 int ret = Interop.Messages.GetMessagePort(_messageHandle, out port);
213                 if (ret != (int)MessagesError.None)
214                 {
215                     Log.Error(Globals.LogTag, "Failed to get message port, Error - " + (MessagesError)ret);
216                 }
217
218                 return port;
219             }
220         }
221
222         /// <summary>
223         /// The message box type.
224         /// </summary>
225         public MessageBoxType BoxType
226         {
227             get
228             {
229                 int boxType = (int)MessageBoxType.All;
230                 int ret = Interop.Messages.GetMboxType(_messageHandle, out boxType);
231                 if (ret != (int)MessagesError.None)
232                 {
233                     Log.Error(Globals.LogTag, "Failed to get message box type, Error - " + (MessagesError)ret);
234                 }
235
236                 return (MessageBoxType)boxType;
237             }
238             set
239             {
240                 int ret = Interop.Messages.SetMboxType(_messageHandle, (int)value);
241                 if (ret != (int)MessagesError.None)
242                 {
243                     Log.Error(Globals.LogTag, "Failed to set message box type, Error - " + (MessagesError)ret);
244                     MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
245                 }
246             }
247         }
248
249         /// <summary>
250         /// The text of the message.
251         /// </summary>
252         public string Text
253         {
254             get
255             {
256                 string text = null;
257                 int ret = Interop.Messages.GetText(_messageHandle, out text);
258                 if (ret != (int)MessagesError.None)
259                 {
260                     Log.Error(Globals.LogTag, "Failed to get text, Error - " + (MessagesError)ret);
261                 }
262
263                 return text;
264             }
265             set
266             {
267                 int ret = Interop.Messages.SetText(_messageHandle, value);
268                 if (ret != (int)MessagesError.None)
269                 {
270                     Log.Error(Globals.LogTag, "Failed to set text, Error - " + (MessagesError)ret);
271                     MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
272                 }
273             }
274         }
275
276         /// <summary>
277         /// The time of the message.
278         /// </summary>
279         public DateTime Time
280         {
281             get
282             {
283                 int time = 0;
284                 int ret = Interop.Messages.GetTime(_messageHandle, out time);
285                 if (ret != (int)MessagesError.None)
286                 {
287                     Log.Error(Globals.LogTag, "Failed to get time, Error - " + (MessagesError)ret);
288                 }
289
290                 return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(time).ToLocalTime();
291             }
292             set
293             {
294                 int ret = Interop.Messages.SetTime(_messageHandle, (int)(value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds));
295                 if (ret != (int)MessagesError.None)
296                 {
297                     Log.Error(Globals.LogTag, "Failed to set time, Error - " + (MessagesError)ret);
298                     MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
299                 }
300             }
301         }
302
303         /// <summary>
304         /// The SIM slot index of the message.
305         /// </summary>
306         public SimSlotId SimId
307         {
308             get
309             {
310                 int simId = (int)SimSlotId.Unknown;
311                 int ret = Interop.Messages.GetSimId(_messageHandle, out simId);
312                 if (ret != (int)MessagesError.None)
313                 {
314                     Log.Error(Globals.LogTag, "Failed to get simId, Error - " + (MessagesError)ret);
315                 }
316
317                 return (SimSlotId)simId;
318             }
319             set
320             {
321                 int ret = Interop.Messages.SetSimId(_messageHandle, (int)value);
322                 if (ret != (int)MessagesError.None)
323                 {
324                     Log.Error(Globals.LogTag, "Failed to set simId, Error - " + (MessagesError)ret);
325                     MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
326                 }
327             }
328         }
329
330         /// <summary>
331         /// Indicates the sender of the message.
332         /// </summary>
333         public IReadOnlyCollection<MessagesAddress> From
334         {
335             get
336             {
337                 return _from as IReadOnlyCollection<MessagesAddress>;
338             }
339         }
340     }
341 }