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