[3.0] Fixed not to count copied image in a clipboard
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / clipboard-impl-ecore-wl.cpp
index 6c1ff67..6f99055 100644 (file)
 #include <dali/public-api/object/any.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/integration-api/debug.h>
+#include <unistd.h>
+
+#ifdef DALI_ELDBUS_AVAILABLE
+#include <Eldbus.h>
+#endif // DALI_ELDBUS_AVAILABLE
 
 // INTERNAL INCLUDES
 #include <singleton-service-impl.h>
 
-namespace //unnamed namespace
-{
-const char* const CBHM_WINDOW = "CBHM_XWIN";
-const char* const CBHM_MSG = "CBHM_MSG";
-const char* const CBHM_ITEM = "CBHM_ITEM";
-const char* const CBHM_cCOUNT = "CBHM_cCOUNT";
-const char* const CBHM_ERROR = "CBHM_ERROR";
-const char* const SET_ITEM = "SET_ITEM";
-const char* const SHOW = "show0";
-const char* const HIDE = "cbhm_hide";
-}
+#define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
+#ifndef CBHM_DBUS_INTERFACE
+#define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
+#endif /* CBHM_DBUS_INTERFACE */
+#define CBHM_COUNT_ALL 0    // ATOM_INDEX_CBHM_COUNT_ALL
+
+#define CLIPBOARD_BUFFER_SIZE 3000
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Clipboard
@@ -53,17 +54,174 @@ namespace Internal
 namespace Adaptor
 {
 
-namespace
-{
-BaseHandle Create()
+struct Clipboard::Impl
 {
-  BaseHandle handle( Clipboard::Get() );
+  Impl()
+  {
+    Eldbus_Object *eldbus_obj;
+    cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
+    eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
+    eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
+    eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
+    eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
+    mVisible = false;
+    mIsFirstTimeHidden = true;
+  }
 
-  return handle;
-}
-TypeRegistration CLIPBOARD_TYPE( typeid(Dali::Clipboard), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
+  ~Impl()
+  {
+    if (cbhm_conn)
+      eldbus_connection_unref(cbhm_conn);
+  }
+
+  Eldbus_Proxy* cbhm_proxy_get()
+  {
+    return eldbus_proxy;
+  }
+
+  Eldbus_Connection* cbhm_connection_get()
+  {
+    return cbhm_conn;
+  }
+
+  void SetItem( const std::string &itemData )
+  {
+    const char *data = itemData.c_str();
+    const char *types[10] = {0, };
+    int i = -1;
+
+    if (data == NULL)
+    {
+      return;
+    }
+    strncpy(mSendBuf, data, CLIPBOARD_BUFFER_SIZE);
+
+    // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
+    types[++i] = "CLIPBOARD_BEGIN";
+
+    types[++i] = "application/x-elementary-markup";
+    types[++i] = "text/plain";
+    types[++i] = "text/plain;charset=utf-8";
+
+    // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
+    types[++i] = "CLIPBOARD_END";
+    ecore_wl_dnd_selection_set(ecore_wl_input_get(), types);
+  }
+
+  void RequestItem()
+  {
+    const char *types[10] = {0, };
+    int i = -1;
+
+    types[++i] = "application/x-elementary-markup";
+    types[++i] = "text/plain";
+    types[++i] = "text/plain;charset=utf-8";
+    ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
+  }
+
+  char *ExcuteSend( void *event )
+  {
+    Ecore_Wl_Event_Data_Source_Send *ev = (Ecore_Wl_Event_Data_Source_Send *)event;
+    int len_buf = strlen(mSendBuf);
+    int len_remained = len_buf;
+    int len_written = 0, ret;
+    char *buf = mSendBuf;
+
+    while (len_written < len_buf)
+    {
+       ret = write(ev->fd, buf, len_remained);
+       if (ret == -1) break;
+       buf += ret;
+       len_written += ret;
+       len_remained -= ret;
+    }
+    close(ev->fd);
+    return (char *)mSendBuf;
+  }
+
+  char *ExcuteReceive( void *event )
+  {
+    Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
+
+    return (char *)ev->data;
+  }
+
+  int GetCount()
+  {
+    Eldbus_Message *reply, *req;
+    const char *errname = NULL, *errmsg = NULL;
+    int count = -1;
+
+    if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
+    {
+      DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
+      return -1;
+    }
+
+    eldbus_message_ref(req);
+    eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL) ;
+    reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
+    if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
+    {
+      DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
+      errname, errmsg);
+      eldbus_message_unref(req);
+      return -1;
+    }
+
+    if (!eldbus_message_arguments_get(reply, "i", &count))
+    {
+      DALI_LOG_ERROR("Cannot get arguments from eldbus");
+      eldbus_message_unref(req);
+      return -1;
+    }
+
+    eldbus_message_unref(req);
+    DALI_LOG_ERROR("cbhm item count(%d)", count);
+    return count;
+  }
+
+  void ShowClipboard()
+  {
+    eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
+    mIsFirstTimeHidden = true;
+    mVisible = true;
+  }
+
+  void HideClipboard( bool skipFirstHide )
+  {
+    if ( skipFirstHide && mIsFirstTimeHidden )
+    {
+      mIsFirstTimeHidden = false;
+      return;
+    }
+    eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
+    mIsFirstTimeHidden = false;
+    mVisible = false;
+  }
+
+  bool IsVisible() const
+  {
+    return mVisible;
+  }
 
-} // unnamed namespace
+  static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
+  {
+    static_cast<Clipboard::Impl*>(data)->RequestItem();
+  }
+
+  Eldbus_Proxy *eldbus_proxy;
+  Eldbus_Connection *cbhm_conn;
+
+  char mSendBuf[CLIPBOARD_BUFFER_SIZE+1];
+  bool mVisible;
+  bool mIsFirstTimeHidden;
+};
+
+Clipboard::Clipboard(Impl* impl)
+: mImpl(impl)
+{
+}
 
 Clipboard::~Clipboard()
 {
@@ -83,30 +241,29 @@ Dali::Clipboard Clipboard::Get()
       // If so, downcast the handle
       clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
     }
+    else
+    {
+      Clipboard::Impl* impl( new Clipboard::Impl() );
+      clipboard = Dali::Clipboard( new Clipboard(impl) );
+      service.Register( typeid(Dali::Clipboard), clipboard );
+    }
   }
 
   return clipboard;
 }
+
 bool Clipboard::SetItem(const std::string &itemData )
 {
+  mImpl->SetItem( itemData );
   return true;
 }
 
 /*
- * Get string at given index of clipboard
+ * Request clipboard service to give an item
  */
-std::string Clipboard::GetItem( unsigned int index )  // change string to a Dali::Text object.
+void Clipboard::RequestItem()
 {
-  if ( index >= NumberOfItems() )
-  {
-    return "";
-  }
-
-  std::string emptyString( "" );
-  char sendBuf[20];
-
-  snprintf( sendBuf, 20,  "%s%d", CBHM_ITEM, index );
-  return emptyString;
+  mImpl->RequestItem();
 }
 
 /*
@@ -114,24 +271,28 @@ std::string Clipboard::GetItem( unsigned int index )  // change string to a Dali
  */
 unsigned int Clipboard::NumberOfItems()
 {
-  int count = -1;
-
-  return count;
+  return mImpl->GetCount();
 }
 
-/**
- * Show clipboard window
- * Function to send message to show the Clipboard (cbhm) as no direct API available
- * Reference elementary/src/modules/ctxpopup_copypasteUI/cbhm_helper.c
- */
 void Clipboard::ShowClipboard()
 {
+  mImpl->ShowClipboard();
 }
 
-void Clipboard::HideClipboard()
+void Clipboard::HideClipboard(bool skipFirstHide)
 {
+  mImpl->HideClipboard(skipFirstHide);
 }
 
+bool Clipboard::IsVisible() const
+{
+  return mImpl->IsVisible();
+}
+
+char* Clipboard::ExcuteBuffered( bool type, void *event )
+{
+  return (type ?  mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
+}
 
 } // namespace Adaptor