bitch--
[framework/uifw/elementary.git] / src / lib / elm_cnp.c
index 41c8f52..2fa68a8 100644 (file)
@@ -142,10 +142,12 @@ static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_No
 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
+static int notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
 
 static Eina_Bool pasteimage_append(char *file, Evas_Object *entry);
+static void entry_insert_filter(Evas_Object* entry, char* str);
 
 static Cnp_Atom atoms[CNP_N_ATOMS] = {
      [CNP_ATOM_TARGETS] = {
@@ -169,12 +171,12 @@ static Cnp_Atom atoms[CNP_N_ATOMS] = {
           ELM_SEL_FORMAT_MARKUP,
           general_converter,
           NULL,
-          NULL,
+          notify_handler_edje,
           0
      },
      [CNP_ATOM_text_uri] = {
           "text/uri",
-          ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
+          ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
           general_converter,
           NULL,
           notify_handler_uri,
@@ -324,7 +326,7 @@ static Cnp_Atom atoms[CNP_N_ATOMS] = {
      },
 };
 
-static Cnp_Selection selections[ELM_SEL_TYPE_MAX] = {
+static Cnp_Selection selections[ELM_SEL_TYPE_CLIPBOARD + 1] = {
      ARRAYINIT(ELM_SEL_TYPE_PRIMARY) {
           .debug = "Primary",
           .ecore_sel = ECORE_X_SELECTION_PRIMARY,
@@ -339,6 +341,11 @@ static Cnp_Selection selections[ELM_SEL_TYPE_MAX] = {
           .clear = ecore_x_selection_secondary_clear,
           .request = ecore_x_selection_secondary_request,
      },
+     ARRAYINIT(ELM_SEL_TYPE_XDND) {
+          .debug = "XDnD",
+          .ecore_sel = ECORE_X_SELECTION_XDND,
+          .request = ecore_x_selection_xdnd_request,
+     },
      ARRAYINIT(ELM_SEL_TYPE_CLIPBOARD) {
           .debug = "Clipboard",
           .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
@@ -346,11 +353,6 @@ static Cnp_Selection selections[ELM_SEL_TYPE_MAX] = {
           .clear = ecore_x_selection_clipboard_clear,
           .request = ecore_x_selection_clipboard_request,
      },
-     ARRAYINIT(ELM_SEL_TYPE_XDND) {
-          .debug = "XDnD",
-          .ecore_sel = ECORE_X_SELECTION_XDND,
-          .request = ecore_x_selection_xdnd_request,
-     },
 };
 
 /* Data for DND in progress */
@@ -390,7 +392,7 @@ elm_selection_selection_has_owner(void)
 }
 
 EAPI Eina_Bool
-elm_cnp_selection_set(Elm_Sel_Type selection, Evas_Object *obj,
+elm_cnp_selection_set(Evas_Object *obj, Elm_Sel_Type selection,
                       Elm_Sel_Format format, const void *selbuf, size_t buflen)
 {
 #ifdef HAVE_ELEMENTARY_X
@@ -400,11 +402,11 @@ elm_cnp_selection_set(Elm_Sel_Type selection, Evas_Object *obj,
 
    if (top) xwin = elm_win_xwindow_get(top);
    else xwin = elm_win_xwindow_get(obj);
-   if ((!xwin) || (selection >= ELM_SEL_TYPE_MAX))
+   if ((!xwin) || (selection > ELM_SEL_TYPE_CLIPBOARD))
      return EINA_FALSE;
    if (!_elm_cnp_init_count) _elm_cnp_init();
    if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
-     return elm_cnp_selection_clear(selection, obj);
+     return elm_object_cnp_selection_clear(obj, selection);
 
    sel = selections + selection;
 
@@ -433,12 +435,12 @@ elm_cnp_selection_set(Elm_Sel_Type selection, Evas_Object *obj,
 }
 
 EAPI Eina_Bool
-elm_cnp_selection_clear(Elm_Sel_Type selection, Evas_Object *obj)
+elm_object_cnp_selection_clear(Evas_Object *obj, Elm_Sel_Type selection)
 {
 #ifdef HAVE_ELEMENTARY_X
    Cnp_Selection *sel;
 
-   if ((unsigned int)selection >= (unsigned int)ELM_SEL_TYPE_MAX)
+   if ((unsigned int)selection > (unsigned int)ELM_SEL_TYPE_CLIPBOARD)
      return EINA_FALSE;
    if (!_elm_cnp_init_count) _elm_cnp_init();
 
@@ -463,14 +465,14 @@ elm_cnp_selection_clear(Elm_Sel_Type selection, Evas_Object *obj)
 }
 
 EAPI Eina_Bool
-elm_cnp_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
-                      Evas_Object *obj, Elm_Drop_Cb datacb, void *udata)
+elm_cnp_selection_get(Evas_Object *obj, Elm_Sel_Type selection,
+                      Elm_Sel_Format format, Elm_Drop_Cb datacb, void *udata)
 {
 #ifdef HAVE_ELEMENTARY_X
    Evas_Object *top;
    Cnp_Selection *sel;
 
-   if (selection >= ELM_SEL_TYPE_MAX)
+   if (selection > ELM_SEL_TYPE_CLIPBOARD)
      return EINA_FALSE;
    if (!_elm_cnp_init_count) _elm_cnp_init();
 
@@ -519,13 +521,13 @@ selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
    Cnp_Selection *sel;
    int i;
 
-   for (i = 0; i < ELM_SEL_TYPE_MAX; i++)
+   for (i = 0; i <= ELM_SEL_TYPE_CLIPBOARD; i++)
      {
         if (selections[i].ecore_sel == ev->selection) break;
      }
    cnp_debug("selection %d clear\n", i);
    /* Not me... Don't care */
-   if (i == ELM_SEL_TYPE_MAX) return ECORE_CALLBACK_PASS_ON;
+   if (i > ELM_SEL_TYPE_CLIPBOARD) return ECORE_CALLBACK_PASS_ON;
 
    sel = selections + i;
    sel->active = EINA_FALSE;
@@ -556,9 +558,6 @@ selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
    cnp_debug("selection notify callback: %d\n",ev->selection);
    switch (ev->selection)
      {
-      case ECORE_X_SELECTION_CLIPBOARD:
-         sel = selections + ELM_SEL_TYPE_CLIPBOARD;
-         break;
       case ECORE_X_SELECTION_PRIMARY:
          sel = selections + ELM_SEL_TYPE_PRIMARY;
          break;
@@ -568,6 +567,9 @@ selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
       case ECORE_X_SELECTION_XDND:
          sel = selections + ELM_SEL_TYPE_XDND;
          break;
+      case ECORE_X_SELECTION_CLIPBOARD:
+         sel = selections + ELM_SEL_TYPE_CLIPBOARD;
+         break;
       default:
          return ECORE_CALLBACK_PASS_ON;
      }
@@ -600,7 +602,7 @@ _get_selection_type(void *data, int size)
         Cnp_Selection *sel = selections + *((int *)data);
         if (sel->active &&
             (sel->format >= ELM_SEL_FORMAT_TARGETS) &&
-            (sel->format < ELM_SEL_FORMAT_MAX))
+            (sel->format <= ELM_SEL_FORMAT_HTML))
           return sel->format;
      }
    return ELM_SEL_FORMAT_NONE;
@@ -616,6 +618,9 @@ targets_converter(char *target __UNUSED__, void *data, int size, void **data_ret
 
    if (!data_ret) return EINA_FALSE;
 
+   if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
+     return EINA_FALSE;
+
    if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
      {
         /* TODO : fallback into precise type */
@@ -660,6 +665,8 @@ vcard_send(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__,
 
    cnp_debug("Vcard send called\n");
 
+   if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
+     return EINA_FALSE;
    sel = selections + *((int *)data);
 
    if (data_ret) *data_ret = strdup(sel->selbuf);
@@ -668,6 +675,7 @@ vcard_send(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__,
    return EINA_TRUE;
 }
 
+#if 0
 static Eina_Bool
 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
 {
@@ -688,7 +696,7 @@ is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *
 
    return EINA_TRUE;
 }
-
+#endif
 /*
  * Callback to handle a targets response on a selection request:
  * So pick the format we'd like; and then request it.
@@ -712,11 +720,13 @@ notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notif
           {
              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
                {
+#if 0
                   if ((j == CNP_ATOM_text_uri) ||
                       (j == CNP_ATOM_text_urilist))
                     {
-                      if(!is_uri_type_data(sel, notify)) continue;
+                      if (!is_uri_type_data(sel, notify)) continue;
                     }
+#endif
                   cnp_debug("Atom %s matches\n",atoms[j].name);
                   goto done;
                }
@@ -769,14 +779,51 @@ found:
    return 0;
 }
 
+static void
+entry_insert_filter(Evas_Object* entry, char* str)
+{
+   if (!entry || !str)
+     return;
+
+   char *insertStr = str;
+
+   // if entry has single line set then remove <br/> & <ps/> tags
+   if (elm_entry_single_line_get(entry))
+     {
+        Eina_Strbuf *buf = eina_strbuf_new();
+        if (buf)
+          {
+             eina_strbuf_append(buf, insertStr);
+             eina_strbuf_replace_all(buf, "<br/>", "");
+             eina_strbuf_replace_all(buf, "<ps/>", "");
+             insertStr = eina_strbuf_string_steal(buf);
+             eina_strbuf_free(buf);
+          }
+     }
+   cnp_debug("remove break tag: %s\n", insertStr);
+
+   _elm_entry_entry_paste(entry, insertStr);
+
+   if (insertStr != str)
+     free(insertStr);
+}
 
 static int
 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
 {
    Ecore_X_Selection_Data *data;
    char *str;
+   char *mkupstr;
 
    data = notify->data;
+   str = malloc(sizeof(char) * (data->length + 1));
+   if (str)
+     {
+        strncpy(str, (char *)data->data, data->length);
+        str[data->length] = '\0';
+     }
+   else
+     str = data->data;
 
    if (sel->datacb)
      {
@@ -784,17 +831,28 @@ notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
 
         ddata.x = ddata.y = 0;
         ddata.format = ELM_SEL_FORMAT_TEXT;
-        ddata.data = data->data;
+        ddata.data = str;
         ddata.len = data->length;
         sel->datacb(sel->udata, sel->widget, &ddata);
-        return 0;
      }
+   else
+     {
+        cnp_debug("Notify handler text %d %d %p\n", data->format, data->length, data->data);
+        mkupstr = _elm_util_text_to_mkup((const char *) str);
+        if (mkupstr)
+          {
+             entry_insert_filter(sel->requestwidget, mkupstr);
+             free(mkupstr);
+          }
+        else
+          entry_insert_filter(sel->requestwidget, str);
 
-   cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
-   str = _elm_util_text_to_mkup((const char *) data->data);
-   cnp_debug("String is %s (from %s)\n", str, data->data);
-   _elm_entry_entry_paste(sel->requestwidget, str);
-   free(str);
+        cnp_debug("String is %s (from %s)\n", str, data->data);
+        //_elm_entry_entry_paste(sel->requestwidget, str);
+     }
+
+   if (str != data->data)
+     free(str);
    return 0;
 }
 
@@ -836,6 +894,18 @@ notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
         return 0;
      }
    cnp_debug("Got %s\n",p);
+   if (sel->datacb)
+     {
+        Elm_Selection_Data ddata;
+
+        ddata.x = ddata.y = 0;
+        ddata.format = ELM_SEL_FORMAT_IMAGE;
+        ddata.data = p;
+        ddata.len = data->length;
+        sel->datacb(sel->udata, sel->widget, &ddata);
+        free(p);
+        return 0;
+     }
    if (strncmp(p, "file://", 7))
      {
         /* Try and continue if it looks sane */
@@ -956,6 +1026,35 @@ notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
    return 0;
 }
 
+static int
+notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
+{
+   Ecore_X_Selection_Data *data;
+
+   data = notify->data;
+
+   char *stripstr = NULL;
+   stripstr = malloc(sizeof(char) * (data->length + 1));
+   strncpy(stripstr, (char *)data->data, data->length);
+   stripstr[data->length] = '\0';
+
+   if (sel->datacb)
+     {
+        Elm_Selection_Data ddata;
+        ddata.x = ddata.y = 0;
+        ddata.format = ELM_SEL_FORMAT_MARKUP;
+        ddata.data = stripstr;
+        ddata.len = data->length;
+        sel->datacb(sel->udata, sel->widget, &ddata);
+     }
+   else
+     entry_insert_filter(sel->requestwidget, stripstr);
+     //_elm_entry_entry_paste(sel->requestwidget, stripstr);
+
+   cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
+   free(stripstr);
+   return 0;
+}
 
 /**
  *    Warning: Generic text/html can';t handle it sanely.
@@ -970,22 +1069,25 @@ notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
    cnp_debug("Got some HTML: Checking encoding is useful\n");
    data = notify->data;
 
+   char *stripstr = NULL;
+   stripstr = malloc(sizeof(char) * (data->length + 1));
+   strncpy(stripstr, (char *)data->data, data->length);
+   stripstr[data->length] = '\0';
+
    if (sel->datacb)
      {
         Elm_Selection_Data ddata;
         ddata.x = ddata.y = 0;
         ddata.format = ELM_SEL_FORMAT_HTML;
-        ddata.data = data->data;
+        ddata.data = stripstr;
         ddata.len = data->length;
         sel->datacb(sel->udata, sel->widget, &ddata);
-        return 0;
      }
+   else
+     entry_insert_filter(sel->requestwidget, stripstr);
+   //_elm_entry_entry_paste(sel->requestwidget, stripstr);
 
-   char *stripstr = malloc(sizeof(char) * (data->length + 1));
-   strncpy(stripstr, (char *)data->data, data->length);
-   stripstr[data->length] = '\0';
    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
-   _elm_entry_entry_paste(sel->requestwidget, stripstr);
    free(stripstr);
    return 0;
 }
@@ -997,6 +1099,8 @@ text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, i
    Cnp_Selection *sel;
 
    cnp_debug("text converter\n");
+   if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
+     return EINA_FALSE;
    if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
      {
         if (data_ret)
@@ -1011,11 +1115,23 @@ text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, i
    sel = selections + *((int *)data);
    if (!sel->active) return EINA_TRUE;
 
-   if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
-       (sel->format & ELM_SEL_FORMAT_HTML))
+   if (sel->format & ELM_SEL_FORMAT_MARKUP)
+     *data_ret = _elm_util_mkup_to_text(sel->selbuf);
+   else if (sel->format & ELM_SEL_FORMAT_HTML)
      {
-        *data_ret = _elm_util_mkup_to_text(sel->selbuf);
-        if (size_ret) *size_ret = strlen(*data_ret);
+        char *text = NULL;
+        Eina_Strbuf *buf = eina_strbuf_new();
+        if (buf)
+          {
+             eina_strbuf_append(buf, sel->selbuf);
+             eina_strbuf_replace_all(buf, "&nbsp;", " ");
+             text = eina_strbuf_string_steal(buf);
+             eina_strbuf_free(buf);
+             *data_ret = _elm_util_mkup_to_text(text);
+             free(text);
+          }
+        else
+          *data_ret = _elm_util_mkup_to_text(sel->selbuf);
      }
    else if (sel->format & ELM_SEL_FORMAT_TEXT)
      {
@@ -1050,7 +1166,11 @@ general_converter(char *target __UNUSED__, void *data, int size, void **data_ret
      }
    else
      {
-        Cnp_Selection *sel = selections + *((int *)data);
+        Cnp_Selection *sel;
+        if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
+          return EINA_FALSE;
+
+        sel = selections + *((int *)data);
         if (data_ret) *data_ret = strdup(sel->selbuf);
         if (size_ret) *size_ret = strlen(sel->selbuf);
      }
@@ -1471,7 +1591,7 @@ elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (
    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
-                                  _drag_mouse_up, (uintptr_t *)xwin);
+                                  _drag_mouse_up, (void *)(long)xwin);
 
    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
                                             _dnd_status, NULL);