Fix Coverity issues
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / clipboard-impl-ecore-wl.cpp
index a55508a..68a10e5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "clipboard-impl.h"
 
 // EXTERNAL INCLUDES
+// Ecore is littered with C style cast
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wold-style-cast"
 #include <Ecore.h>
 #include <Ecore_Wayland.h>
 #include <dali/public-api/object/any.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/integration-api/debug.h>
-#include <bundle.h>
+#include <unistd.h>
+
+#ifdef DALI_ELDBUS_AVAILABLE
+#include <Eldbus.h>
+#endif // DALI_ELDBUS_AVAILABLE
 
 // INTERNAL INCLUDES
 #include <singleton-service-impl.h>
 
-#define CLIPBOARD_STR  "CLIPBOARD_STR"
+#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
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Clipboard
@@ -48,37 +59,165 @@ struct Clipboard::Impl
 {
   Impl()
   {
-    mBundle = bundle_create();
+    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;
+  }
+
+  ~Impl()
+  {
+    if (cbhm_conn)
+      eldbus_connection_unref(cbhm_conn);
+  }
+
+  Eldbus_Proxy* cbhm_proxy_get()
+  {
+    return eldbus_proxy;
   }
 
-  void SetItem(const char *data)
+  Eldbus_Connection* cbhm_connection_get()
   {
-    char *temp = NULL;
+    return cbhm_conn;
+  }
+
+  void SetItem( const std::string &itemData )
+  {
+    const char *types[10] = {0, };
+    int i = -1;
 
-    if (bundle_get_str(mBundle, CLIPBOARD_STR, &temp) == BUNDLE_ERROR_NONE)
+    if (itemData.length() == 0)
     {
-      bundle_del(mBundle, CLIPBOARD_STR);
+      return;
     }
-    bundle_add_str(mBundle, CLIPBOARD_STR, data);
+    mSendBuffer = itemData;
+
+    // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
+    types[++i] = "CLIPBOARD_BEGIN";
+
+    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);
   }
 
-  char *GetItem()
+  void RequestItem()
   {
-    char *data = NULL;
+    const char *types[10] = {0, };
+    int i = -1;
 
-    if ( bundle_get_count(mBundle) )
+    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 = mSendBuffer.length();
+    int len_remained = len_buf;
+    int len_written = 0, ret;
+    const char *buf = mSendBuffer.c_str();
+
+    while (len_written < len_buf)
     {
-      bundle_get_str(mBundle, CLIPBOARD_STR, &data);
+       ret = write(ev->fd, buf, len_remained);
+       if (ret == -1) break;
+       buf += ret;
+       len_written += ret;
+       len_remained -= ret;
     }
-    return data;
+    close(ev->fd);
+    return NULL;
+  }
+
+  char *ExcuteReceive( void *event )
+  {
+    Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
+
+    return (char *)ev->data;
   }
 
   int GetCount()
   {
-    return bundle_get_count(mBundle);
+    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);
+      if( reply )
+      {
+        eldbus_message_unref(reply);
+      }
+      return -1;
+    }
+
+    if (!eldbus_message_arguments_get(reply, "i", &count))
+    {
+      DALI_LOG_ERROR("Cannot get arguments from eldbus");
+      eldbus_message_unref(req);
+      eldbus_message_unref(reply);
+      return -1;
+    }
+
+    eldbus_message_unref(req);
+    eldbus_message_unref(reply);
+    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;
   }
 
-  bundle *mBundle;
+  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;
+
+  std::string mSendBuffer;
+  bool mVisible;
+  bool mIsFirstTimeHidden;
 };
 
 Clipboard::Clipboard(Impl* impl)
@@ -88,6 +227,7 @@ Clipboard::Clipboard(Impl* impl)
 
 Clipboard::~Clipboard()
 {
+  delete mImpl;
 }
 
 Dali::Clipboard Clipboard::Get()
@@ -117,17 +257,16 @@ Dali::Clipboard Clipboard::Get()
 
 bool Clipboard::SetItem(const std::string &itemData )
 {
-  mImpl->SetItem( const_cast<char*>( itemData.c_str()) );
+  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()
 {
-  std::string clipboardString(mImpl->GetItem());
-  return clipboardString;
+  mImpl->RequestItem();
 }
 
 /*
@@ -138,22 +277,30 @@ unsigned int Clipboard::NumberOfItems()
   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
 
 } // namespace Internal
 
 } // namespace Dali
+
+#pragma GCC diagnostic pop