[NUI] Add DataSelected in Clipboard
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Clipboard / Clipboard.cs
1 /*
2  * Copyright(c) 2023 Samsung Electronics Co., Ltd.
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.ComponentModel;
20 using System.Diagnostics.CodeAnalysis;
21 using System.Linq;
22 using System.Runtime.InteropServices;
23
24 using Tizen.NUI.BaseComponents;
25
26 namespace Tizen.NUI
27 {
28     /// <summary>
29     /// Clipboard.
30     /// </summary>
31     [EditorBrowsable(EditorBrowsableState.Never)]
32     public partial class Clipboard : BaseHandle
33     {
34         /// <summary>
35         /// User callback for clipboard event.
36         /// </summary>
37         /// <remarks>
38         /// Receives requested data through <see cref="Tizen.NUI.ClipEvent"/>.
39         /// </remarks>
40         public delegate void ClipboardCallback(bool success, ClipEvent clipEvent);
41
42         internal bool hasClipboardDataReceived = false;
43         internal Dictionary<uint, ClipboardCallback> receivedCallbackDictionary = new Dictionary<uint, ClipboardCallback>();
44
45         private Clipboard() : this(Interop.Clipboard.Get(), true)
46         {
47             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
48         }
49
50         private Clipboard(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
51         {
52         }
53
54         /// <summary>
55         /// Gets the singleton instance of Clipboard.
56         /// </summary>
57         [EditorBrowsable(EditorBrowsableState.Never)]
58         public static Clipboard Instance { get; } = new Clipboard();
59
60         /// <summary>
61         /// Request set the given data to the clipboard.
62         /// </summary>
63         /// <param name="mimeType">The mime type of the data.</param>
64         /// <param name="data">The data to be set on the clipboard.</param>
65         /// <returns>True if the internal clipboard sending request is successful.</returns>
66         /// <example>
67         /// The following example demonstrates how to use the SetData.
68         /// <code>
69         /// string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
70         /// Clipboard.Instance.SetData(MIME_TYPE_PLAIN_TEXT, "Hello Clipboard");
71         /// </code>
72         /// </example>
73         [EditorBrowsable(EditorBrowsableState.Never)]
74         public bool SetData(string mimeType, string data)
75         {
76             bool setData = Interop.Clipboard.SetData(SwigCPtr, mimeType, data);
77             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
78             return setData;
79         }
80
81         /// <summary>
82         /// Request get data of the specified mime type from clipboard<br/>
83         /// and invokes the given callback with the received clipboard data.
84         /// </summary>
85         /// <param name="mimeType">The mime type of data to request.</param>
86         /// <param name="dataReceivedCallback">The callback method to handle the received clipboard data.</param>
87         /// <remarks>
88         /// GetData() method is introduced to fetch data of the specified mime type,<br/>
89         /// and it expects a callback function as a parameter.<br/>
90         /// The given callback is invoked with received clipboard data.<br/>
91         /// The callback is designed to be used only once for handling the data.
92         /// </remarks>
93         /// <example>
94         /// The following example demonstrates how to use the GetData and ClipboardCallback.
95         /// <code>
96         /// string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
97         /// Clipboard.Instance.GetData(MIME_TYPE_PLAIN_TEXT, OnClipboardDataReceived);
98         /// ...
99         /// public void OnClipboardDataReceived(bool success, ClipEvent clipEvent)
100         /// {
101         ///     if (!success) return;
102         ///     string mimeType = clipEvent.MimeType;
103         ///     string data = clipEvent.Data;
104         /// }
105         /// </code>
106         /// </example>
107         [EditorBrowsable(EditorBrowsableState.Never)]
108         public void GetData(string mimeType, ClipboardCallback dataReceivedCallback)
109         {
110             if(!hasClipboardDataReceived)
111             {
112                 ClipboardDataReceived += OnClipboardDataReceived;
113                 hasClipboardDataReceived = true;
114             }
115
116             uint id = Interop.Clipboard.GetData(SwigCPtr, mimeType);
117             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
118
119             if (id == 0)
120             {
121                 // Calls the failure callback even if the request fails.
122                 var clipEvent = new ClipEvent()
123                 {
124                     MimeType = string.Empty,
125                     Data = string.Empty,
126                 };
127                 dataReceivedCallback(false, clipEvent);
128             }
129             else
130             {
131                 receivedCallbackDictionary[id] = dataReceivedCallback;
132             }
133         }
134
135         private void OnClipboardDataReceived(object sender, ClipboardEventArgs e)
136         {
137             if (!receivedCallbackDictionary.Any()) return;
138
139             uint id = e.Id;
140             if (receivedCallbackDictionary.ContainsKey(id))
141             {
142                 ClipboardCallback callback = receivedCallbackDictionary[id];
143                 if (callback != null)
144                 {
145                     callback(e.Success, e.ClipEvent);
146                 }
147                 receivedCallbackDictionary.Remove(id);
148             }
149         }
150
151         /// <summary>
152         /// Dispose.
153         /// </summary>
154         protected override void Dispose(DisposeTypes type)
155         {
156             if (disposed)
157             {
158                 return;
159             }
160
161             if (hasClipboardDataReceived)
162             {
163                 ClipboardDataReceived -= OnClipboardDataReceived;
164                 receivedCallbackDictionary.Clear();
165             }
166
167             if (this.HasBody())
168             {
169                 if (clipboardDataReceivedCallback != null)
170                 {
171                     this.ClipboardDataReceivedSignal().Disconnect(clipboardDataReceivedCallback);
172                 }
173
174                 if (clipboardDataSelectedCallback != null)
175                 {
176                     this.ClipboardDataSelectedSignal().Disconnect(clipboardDataSelectedCallback);
177                 }
178             }
179
180             base.Dispose(type);
181         }
182     }
183 }