[3.0] Fixed not to display mark-up text for copied text from calculator
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / clipboard-impl-ecore-wl.cpp
1 /*
2  * Copyright (c) 2014 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
18 // CLASS HEADER
19 #include "clipboard-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <Ecore.h>
23 #include <Ecore_Wayland.h>
24 #include <dali/public-api/object/any.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/debug.h>
27 #include <unistd.h>
28
29 #ifdef DALI_ELDBUS_AVAILABLE
30 #include <Eldbus.h>
31 #endif // DALI_ELDBUS_AVAILABLE
32
33 // INTERNAL INCLUDES
34 #include <singleton-service-impl.h>
35
36 #define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
37 #ifndef CBHM_DBUS_INTERFACE
38 #define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
39 #endif /* CBHM_DBUS_INTERFACE */
40 #define CBHM_COUNT_ALL 0    // ATOM_INDEX_CBHM_COUNT_ALL
41
42 #define CLIPBOARD_BUFFER_SIZE 3000
43
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
45 // Clipboard
46 ///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 namespace Dali
49 {
50
51 namespace Internal
52 {
53
54 namespace Adaptor
55 {
56
57 struct Clipboard::Impl
58 {
59   Impl()
60   {
61     Eldbus_Object *eldbus_obj;
62     cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
63     eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
64     eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
65     eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
66     eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
67     mVisible = false;
68     mIsFirstTimeHidden = true;
69   }
70
71   ~Impl()
72   {
73     if (cbhm_conn)
74       eldbus_connection_unref(cbhm_conn);
75   }
76
77   Eldbus_Proxy* cbhm_proxy_get()
78   {
79     return eldbus_proxy;
80   }
81
82   Eldbus_Connection* cbhm_connection_get()
83   {
84     return cbhm_conn;
85   }
86
87   void SetItem( const std::string &itemData )
88   {
89     const char *data = itemData.c_str();
90     const char *types[10] = {0, };
91     int i = -1;
92
93     if (data == NULL)
94     {
95       return;
96     }
97     strncpy(mSendBuf, data, CLIPBOARD_BUFFER_SIZE);
98
99     // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
100     types[++i] = "CLIPBOARD_BEGIN";
101
102     types[++i] = "text/plain;charset=utf-8";
103
104     // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
105     types[++i] = "CLIPBOARD_END";
106     ecore_wl_dnd_selection_set(ecore_wl_input_get(), types);
107   }
108
109   void RequestItem()
110   {
111     const char *types[10] = {0, };
112     int i = -1;
113
114     types[++i] = "text/plain;charset=utf-8";
115     ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
116   }
117
118   char *ExcuteSend( void *event )
119   {
120     Ecore_Wl_Event_Data_Source_Send *ev = (Ecore_Wl_Event_Data_Source_Send *)event;
121     int len_buf = strlen(mSendBuf);
122     int len_remained = len_buf;
123     int len_written = 0, ret;
124     char *buf = mSendBuf;
125
126     while (len_written < len_buf)
127     {
128        ret = write(ev->fd, buf, len_remained);
129        if (ret == -1) break;
130        buf += ret;
131        len_written += ret;
132        len_remained -= ret;
133     }
134     close(ev->fd);
135     return (char *)mSendBuf;
136   }
137
138   char *ExcuteReceive( void *event )
139   {
140     Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
141
142     return (char *)ev->data;
143   }
144
145   int GetCount()
146   {
147     Eldbus_Message *reply, *req;
148     const char *errname = NULL, *errmsg = NULL;
149     int count = -1;
150
151     if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
152     {
153       DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
154       return -1;
155     }
156
157     eldbus_message_ref(req);
158     eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL) ;
159     reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
160     if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
161     {
162       DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
163       errname, errmsg);
164       eldbus_message_unref(req);
165       return -1;
166     }
167
168     if (!eldbus_message_arguments_get(reply, "i", &count))
169     {
170       DALI_LOG_ERROR("Cannot get arguments from eldbus");
171       eldbus_message_unref(req);
172       return -1;
173     }
174
175     eldbus_message_unref(req);
176     DALI_LOG_ERROR("cbhm item count(%d)", count);
177     return count;
178   }
179
180   void ShowClipboard()
181   {
182     eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
183     mIsFirstTimeHidden = true;
184     mVisible = true;
185   }
186
187   void HideClipboard( bool skipFirstHide )
188   {
189     if ( skipFirstHide && mIsFirstTimeHidden )
190     {
191       mIsFirstTimeHidden = false;
192       return;
193     }
194     eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
195     mIsFirstTimeHidden = false;
196     mVisible = false;
197   }
198
199   bool IsVisible() const
200   {
201     return mVisible;
202   }
203
204   static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
205   {
206     static_cast<Clipboard::Impl*>(data)->RequestItem();
207   }
208
209   Eldbus_Proxy *eldbus_proxy;
210   Eldbus_Connection *cbhm_conn;
211
212   char mSendBuf[CLIPBOARD_BUFFER_SIZE+1];
213   bool mVisible;
214   bool mIsFirstTimeHidden;
215 };
216
217 Clipboard::Clipboard(Impl* impl)
218 : mImpl(impl)
219 {
220 }
221
222 Clipboard::~Clipboard()
223 {
224 }
225
226 Dali::Clipboard Clipboard::Get()
227 {
228   Dali::Clipboard clipboard;
229
230   Dali::SingletonService service( SingletonService::Get() );
231   if ( service )
232   {
233     // Check whether the singleton is already created
234     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
235     if(handle)
236     {
237       // If so, downcast the handle
238       clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
239     }
240     else
241     {
242       Clipboard::Impl* impl( new Clipboard::Impl() );
243       clipboard = Dali::Clipboard( new Clipboard(impl) );
244       service.Register( typeid(Dali::Clipboard), clipboard );
245     }
246   }
247
248   return clipboard;
249 }
250
251 bool Clipboard::SetItem(const std::string &itemData )
252 {
253   mImpl->SetItem( itemData );
254   return true;
255 }
256
257 /*
258  * Request clipboard service to give an item
259  */
260 void Clipboard::RequestItem()
261 {
262   mImpl->RequestItem();
263 }
264
265 /*
266  * Get number of items in clipboard
267  */
268 unsigned int Clipboard::NumberOfItems()
269 {
270   return mImpl->GetCount();
271 }
272
273 void Clipboard::ShowClipboard()
274 {
275   mImpl->ShowClipboard();
276 }
277
278 void Clipboard::HideClipboard(bool skipFirstHide)
279 {
280   mImpl->HideClipboard(skipFirstHide);
281 }
282
283 bool Clipboard::IsVisible() const
284 {
285   return mImpl->IsVisible();
286 }
287
288 char* Clipboard::ExcuteBuffered( bool type, void *event )
289 {
290   return (type ?  mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
291 }
292
293 } // namespace Adaptor
294
295 } // namespace Internal
296
297 } // namespace Dali