Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Messaging / Tizen.Messaging.Messages / MmsMessage.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
20 namespace Tizen.Messaging.Messages
21 {
22     /// <summary>
23     /// A class to represent multimedia messages.
24     /// </summary>
25     public class MmsMessage : Message
26     {
27         private IList<MessagesAttachment> _attachment = new List<MessagesAttachment>();
28
29         /// <summary>
30         /// Creates a multimedia message.
31         /// </summary>
32         public MmsMessage() : base(MessageType.Mms)
33         {
34         }
35
36         internal MmsMessage(IntPtr messageHandle) : base(messageHandle)
37         {
38             GetAllAttachments();
39         }
40
41         /// <summary>
42         /// The subject of the multimedia message
43         /// </summary>
44         public string Subject
45         {
46             get
47             {
48                 string subject = null;
49                 int ret = Interop.Messages.GetSubject(_messageHandle, out subject);
50                 if (ret != (int)MessagesError.None)
51                 {
52                     Log.Error(Globals.LogTag, "Failed to get subject, Error - " + (MessagesError)ret);
53                 }
54
55                 return subject;
56             }
57             set
58             {
59                 int ret = Interop.Messages.SetSubject(_messageHandle, value);
60                 if (ret != (int)MessagesError.None)
61                 {
62                     Log.Error(Globals.LogTag, "Failed to set subject, Error - " + (MessagesError)ret);
63                     MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
64                 }
65             }
66         }
67
68         /// <summary>
69         /// Collection of normal message recipients
70         /// </summary>
71         public ICollection<MessagesAddress> To
72         {
73             get
74             {
75                 return _to;
76             }
77         }
78
79         /// <summary>
80         /// Collection of CC(carbon copy) message recipients
81         /// </summary>
82         public ICollection<MessagesAddress> Cc
83         {
84             get
85             {
86                 return _cc;
87             }
88         }
89
90         /// <summary>
91         /// Collection of BCC(blind carbon copy) message recipients
92         /// </summary>
93         public ICollection<MessagesAddress> Bcc
94         {
95             get
96             {
97                 return _bcc;
98             }
99         }
100
101         /// <summary>
102         /// The list of attachment files
103         /// </summary>
104         public IList<MessagesAttachment> Attachments
105         {
106             get
107             {
108                 return _attachment;
109             }
110         }
111
112         internal void SetAttachments()
113         {
114             foreach (var it in _attachment)
115             {
116                 AddAttachment(it);
117             }
118         }
119
120         private void AddAttachment(MessagesAttachment attach)
121         {
122             int ret = Interop.Messages.AddAttachment(_messageHandle, (int)attach.Type, attach.FilePath);
123             if (ret != (int)MessagesError.None)
124             {
125                 Log.Error(Globals.LogTag, "Failed to add attachment, Error - " + (MessagesError)ret);
126                 MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
127             }
128         }
129
130         private void GetAllAttachments()
131         {
132             int count;
133
134             int ret = Interop.Messages.GetAttachmentCount(_messageHandle, out count);
135             if (ret != (int)MessagesError.None)
136             {
137                 Log.Error(Globals.LogTag, "Failed to get attachment count, Error - " + (MessagesError)ret);
138                 MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
139             }
140
141             string path;
142             int type;
143             var attachmentList = new List<MessagesAttachment>();
144
145             for (int i = 0; i < count; i++)
146             {
147                 ret = Interop.Messages.GetAttachment(_messageHandle, i, out type, out path);
148                 if (ret != (int)MessagesError.None)
149                 {
150                     Log.Error(Globals.LogTag, "Failed to get attachment, Error - " + (MessagesError)ret);
151                     MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
152                 }
153
154                 var attachmentItem = new MessagesAttachment((MediaType)type, path);
155                 attachmentList.Add(attachmentItem);
156             }
157
158             _attachment = attachmentList;
159         }
160
161     }
162 }