Fix notification removebutton api
[platform/core/csapi/tizenfx.git] / Tizen.Applications / Tizen.Applications / EventNotification.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.Runtime.InteropServices;
11
12 namespace Tizen.Applications.Notifications
13 {
14     /// <summary>
15     /// Class for creating a simple event notification
16     /// </summary>
17     /// <example>
18     /// <code>
19     /// public class EventNotificationExample
20     /// {
21     ///     /// ...
22     ///     EventNotification noti = new EventNotification();
23     ///     noti.EventCount = 2;
24     ///     noti.Title = "Unread Messages";
25     ///     NotificationManager.Post(noti);
26     /// }
27     /// </code>
28     /// </example>
29     public class EventNotification : Notification
30     {
31         /// <summary>
32         /// Intializes instance of EventNotification class
33         /// </summary>
34         public EventNotification()
35         {
36             int ret;
37
38             _handle = Interop.Notification.Create(NotificationType.Event);
39             if (_handle.IsInvalid)
40             {
41                 ret = Tizen.Internals.Errors.ErrorFacts.GetLastResult();
42                 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to create Event Notification");
43             }
44
45             ret = Interop.Notification.SetLayout(_handle, NotiLayout.SingleEvent);
46             if(ret != (int)NotificationError.None)
47             {
48                 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set event layout");
49             }
50
51             NotificationType = NotificationType.Event;
52         }
53
54         internal EventNotification(Interop.Notification.SafeNotificationHandle handle)
55         {
56             _handle = handle;
57
58             NotificationType = NotificationType.Event;
59         }
60
61         /// <summary>
62         /// Sets and gets the event count to be displayed on a Notification.
63         /// </summary>
64         /// <value>
65         /// EventCount is a numeric value which is displayed on a notification.
66         /// For example, to show unread message count on a messenger notification event count can be set.
67         /// </value>
68         /// <example>
69         /// <code>
70         /// EventNotification noti = new EventNotification();
71         /// noti.EventCount = 2;
72         /// Log.Debug(LogTag, "Event Count: " + noti.EventCount);
73         /// </code>
74         /// </example>
75         public int EventCount
76         {
77             get
78             {
79                 IntPtr countPtr;
80                 string count;
81                 int cnt;
82                 int ret = Interop.Notification.GetText(_handle, NotiText.EventCount, out countPtr);
83                 if(ret != (int)NotificationError.None)
84                 {
85                     Log.Warn(_logTag, "unable to get event count");
86                     return 0;
87                 }
88
89                 if(countPtr == IntPtr.Zero)
90                     return 0;
91
92                 count = Marshal.PtrToStringAnsi(countPtr);
93                 if(Int32.TryParse(count, out cnt))
94                 {
95                     return cnt;
96                 }
97                 else
98                 {
99                     Log.Warn(_logTag, "unable to parse string");
100                     return 0;
101                 }
102             }
103             set
104             {
105                 int ret;
106                 if(value <= 0)
107                 {
108                     throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid value for event count");
109                 }
110
111                 ret = Interop.Notification.SetText(_handle, NotiText.EventCount, value.ToString(), null, -1);
112                 if(ret != (int)NotificationError.None)
113                 {
114                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set event count");
115                 }
116             }
117         }
118
119         /// <summary>
120         /// Gets and sets TimeStamp. Defaults to 0 ms UTC i.e 1 Jan 1970.
121         /// </summary>
122         /// <value>
123         /// Timestamp can be used to display a timestamp on a notification.
124         /// For example, in a message received notification, Timestamp can be used to display the time of message reception.
125         /// </value>
126         /// <example>
127         /// <code>
128         /// EventNotification noti = new EventNotification();
129         /// noti.Timestamp = DateTime.Now;
130         /// Log.Debug(LogTag, "Timestamp: " + noti.Timestamp);
131         /// </code>
132         /// </example>
133         public DateTime Timestamp
134         {
135             get
136             {
137                 int time;
138                 int ret = Interop.Notification.GetTime(_handle, out time);
139                 if(ret != (int)NotificationError.None)
140                 {
141                     Log.Warn(_logTag, "unable to get timestamp");
142                     return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(0).ToLocalTime();
143                 }
144
145                 return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(time).ToLocalTime();
146             }
147             set
148             {
149                 int time = (int)value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
150                 int ret = Interop.Notification.SetTime(_handle, time);
151                 if(ret != (int)NotificationError.None)
152                 {
153                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set timestamp");
154                 }
155             }
156         }
157
158         /// <summary>
159         /// Method to add a button to an event notification
160         /// </summary>
161         /// <param name="index">Index of the Button to be added to notification</param>
162         /// <param name="imagePath">Path of Button image</param>
163         /// <param name="text">Text of button</param>
164         /// <param name="appControl">App control to be invoke on button click</param>
165         /// <remarks>
166         /// AddButton method can be used to add 2 different types of buttons, one for positive response and one for negative.
167         /// A button comprises of Image path for the button, Text for the button and an App control which is to be invoked on button click.
168         /// </remarks>
169         /// <example>
170         /// <code>
171         /// Application app = Application.Current;
172         /// DirectoryInfo dir = app.DirectoryInfo;
173         /// string imagePath = dir.Resource+"notification.png";
174         /// string text = "Click";
175         /// AppControl clickApp = new AppControl();
176         /// clickApp.ApplicationId = "org.tizen.setting";        /// EventNotification noti = new EventNotification();
177         /// noti.AddButton(ButtonIndex.Positive, imagePath, text, clickApp);
178         /// </code>
179         /// </example>
180         public void AddButton(ButtonIndex index, string imagePath = "", string text = "", AppControl appControl = null)
181         {
182             int ret = Interop.Notification.SetImage(_handle, (NotiImage)((int)index + (int)NotiImage.PositiveButton), imagePath);
183             if (ret != (int)NotificationError.None)
184             {
185                 throw NotificationErrorFactory.GetException((NotificationError)ret, "set button image path failed");
186             }
187
188             ret = Interop.Notification.SetText(_handle, (NotiText)((int)NotiText.PositiveButton + (int)index), text, null, -1);
189             if (ret != (int)NotificationError.None)
190             {
191                 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set button text");
192             }
193
194             if(appControl != null)
195             {
196                 ret = Interop.Notification.SetEventHandler(_handle, (int)index, appControl.SafeAppControlHandle);
197                 if (ret != (int)NotificationError.None)
198                 {
199                     throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set button event handler");
200                 }
201             }
202
203             ret = Interop.Notification.AddButton(_handle, (int)index + 1);
204             if(ret != (int)NotificationError.None)
205             {
206                 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to add button");
207             }
208         }
209
210         /// <summary>
211         /// Method to remove a button from an event notification
212         /// </summary>
213         /// <param name="index">Index of button to be removed</param>
214         /// <remarks>
215         /// RemoveButton method can be used to remove a button which has been added to the notification.
216         /// RemoveButton should only be called after a button has been added.
217         /// </remarks>
218         /// <example>
219         /// <code>
220         /// EventNotification noti = new EventNotification();
221         /// noti.AddButton(ButtonIndex.Positive);
222         /// /// ...
223         /// noti.RemoveButton(ButtonIndex.Negative);
224         /// </code>
225         /// </example>
226         public void RemoveButton(ButtonIndex index)
227         {
228             int ret = Interop.Notification.RemoveButton(_handle, (int)index + 1);
229             if(ret != (int)NotificationError.None)
230             {
231                 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to remove button");
232             }
233
234             ret = Interop.Notification.SetImage(_handle, (NotiImage)((int)index + (int)NotiImage.PositiveButton), string.Empty);
235             if (ret != (int)NotificationError.None)
236             {
237                 throw NotificationErrorFactory.GetException((NotificationError)ret, "set button image path failed");
238             }
239
240             ret = Interop.Notification.SetText(_handle, (NotiText)((int)NotiText.PositiveButton + (int)index), string.Empty, null, -1);
241             if (ret != (int)NotificationError.None)
242             {
243                 throw NotificationErrorFactory.GetException((NotificationError)ret, "unable to set button text");
244             }
245         }
246     }
247 }