Elm entry: Fixed 'Paste' to only show if there clipboard isn't empty.
[framework/uifw/elementary.git] / src / lib / elm_cnp_helper.c
index 518adf9..96b3b07 100644 (file)
@@ -163,18 +163,19 @@ static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *not
 static Paste_Image *pasteimage_alloc(const char *file, int pathlen);
 static Eina_Bool pasteimage_append(Paste_Image *pi, Evas_Object *entry);
 static void pasteimage_free(Paste_Image *pi);
+static void entry_insert_filter(Evas_Object* entry, char* str);
 
 /* Optimisation: Turn this into a 256 byte table:
  *     then can lookup in one index, not N checks */
 static const Escape escapes[] = {
-       { "<br>",   '\n' },
-       { "<ps>",   '\n' },
-       { "<\t>",   '\t' },
-       { "gt;",    '>'  },
-       { "lt;",    '<'  },
-       { "amp;",   '&'  },
-       { "quot;",  '\'' },
-       { "dquot;", '"'  }
+  { "<br>",   '\n' },
+  { "<ps>",   '\n' },
+  { "<\t>",   '\t' },
+  { "gt;",    '>'  },
+  { "lt;",    '<'  },
+  { "amp;",   '&'  },
+  { "quot;",  '\'' },
+  { "dquot;", '"'  }
 };
 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
 
@@ -771,7 +772,12 @@ _get_tag_value(const char *tag_str, const char *tag_name)
              char *valueEnd = strchr(value, '\0');
 
              int i;
-             for (i = 0; i < spCnt; i++)
+             int start = 0;
+             if ((!strncmp(tag_str, "<item", 5) && !strcmp(tag_name, "href")) // EFL img tag
+               || (!strncmp(tag_str, "<img", 4) && !strcmp(tag_name, "src"))) // HTML img tag
+               start = 1;
+
+             for (i = start; i < spCnt; i++)
                {
                   if (spArray[i] && spArray[i] < valueEnd)
                     valueEnd = spArray[i];
@@ -817,8 +823,7 @@ _set_EFL_item_data(PItemTagData data, const char *tag_str)
         if (path)
           {
              char *modify = malloc(sizeof(char) * (strlen(value) + 1));
-             strncpy(modify, "file://", 7);
-             modify[7] = '\0';
+             strncpy(modify, "file://", 8);
              path += 7;
              while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
                {
@@ -899,8 +904,7 @@ _set_HTML_img_data(PItemTagData data, const char *tag_str)
         if (path)
           {
              char *modify = malloc(sizeof(char) * (strlen(value) + 1));
-             strncpy(modify, "file://", 7);
-             modify[7] = '\0';
+             strncpy(modify, "file://", 8);
              path += 7;
              while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
                {
@@ -1036,9 +1040,10 @@ _convert_to_html(Eina_List* nodes)
                          }
                        switch(trail->tagPosType)
                          {
+                            /* closed tag does not need in HTML
                           case TAGPOS_ALONE:
                              eina_strbuf_append(html, " />");
-                             break;
+                             break;*/
                           default:
                              eina_strbuf_append(html, ">");
                              break;
@@ -1176,6 +1181,16 @@ _convert_to_edje(Eina_List* nodes)
 }
 
 Eina_Bool
+elm_selection_selection_has_owner(void)
+{
+#ifdef HAVE_ELEMENTARY_X
+   return !!ecore_x_selection_owner_get(clipboard_atom);
+#else
+   return EINA_FALSE;
+#endif
+}
+
+Eina_Bool
 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
 {
 #ifdef HAVE_ELEMENTARY_X
@@ -1366,6 +1381,8 @@ targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void
    Cnp_Selection *sel;
 
    if (!data_ret) return EINA_FALSE;
+   if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
+     return EINA_FALSE;
 
    sel = selections + *((int *)data);
 
@@ -1425,8 +1442,6 @@ is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *
    cnp_debug("Got %s\n", p);
    if (strncmp(p, "file://", 7))
      {
-        /* elm_selection_set send target notify->data just "x" */
-        if (*p == 'x') return EINA_TRUE;
         if (*p != '/') return EINA_FALSE;
      }
 
@@ -1456,11 +1471,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;
                     }
+#endif
                   cnp_debug("Atom %s matches\n",atoms[j].name);
                   goto done;
                }
@@ -1517,6 +1534,69 @@ found:
    return 0;
 }
 
+static void
+entry_insert_filter(Evas_Object* entry, char* str)
+{
+   if (!entry || !str)
+     return;
+
+   char *insertStr = str;
+   // if entry has text only set then remove item tags
+   if (elm_entry_cnp_textonly_get(entry))
+     {
+        while (EINA_TRUE)
+          {
+             char *startTag = NULL;
+             char *endTag = NULL;
+
+             startTag = strstr(insertStr, "<item");
+             if (!startTag)
+               startTag = strstr(insertStr, "</item");
+             if (startTag)
+               endTag = strstr(startTag, ">");
+             else
+               break;
+             if (!endTag || startTag > endTag)
+               {
+                  cnp_debug("Broken tag: %s\n", str);
+                  break;
+               }
+
+             size_t sindex = startTag - insertStr;
+             size_t eindex = endTag - insertStr + 1;
+
+             Eina_Strbuf *buf = eina_strbuf_new();
+             if (buf)
+               {
+                  eina_strbuf_append(buf, insertStr);
+                  eina_strbuf_remove(buf, sindex, eindex);
+                  insertStr = eina_strbuf_string_steal(buf);
+                  eina_strbuf_free(buf);
+               }
+          }
+     }
+   cnp_debug("remove item tag: %s\n", insertStr);
+
+   // 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_insert(entry, insertStr);
+
+   if (insertStr != str)
+     free(insertStr);
+}
 
 static int
 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
@@ -1530,7 +1610,7 @@ notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
      {
         Elm_Selection_Data ddata;
 
-        str = mark_up((char *)data->data, data->length, NULL);
+        str = strdup(data->data);
         ddata.x = ddata.y = 0;
         ddata.format = ELM_SEL_FORMAT_TEXT;
         ddata.data = str;
@@ -1543,7 +1623,8 @@ notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
    cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
    str = mark_up((char *)data->data, data->length, NULL);
    cnp_debug("String is %s (from %s)\n", str, data->data);
-   elm_entry_entry_insert(sel->requestwidget, str);
+   entry_insert_filter(sel->requestwidget, str);
+   //elm_entry_entry_insert(sel->requestwidget, str);
    free(str);
    return 0;
 }
@@ -1558,7 +1639,7 @@ notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
    Ecore_X_Selection_Data *data;
    Ecore_X_Selection_Data_Files *files;
    Paste_Image *pi;
-   char *p;
+   char *p, *stripstr;
 
    data = notify->data;
    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
@@ -1572,11 +1653,11 @@ notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
              cnp_debug("more then one file: Bailing\n");
              return 0;
           }
-        p = files->files[0];
+        stripstr = p = strdup(files->files[0]);
      }
    else
      {
-        p = (char *)data->data;
+        stripstr = p = strndup((char *)data->data, data->length);
      }
 
    if (!p)
@@ -1594,12 +1675,17 @@ notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
         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 */
-        if (*p != '/') return 0;
+        if (*p != '/')
+          {
+             free(p);
+             return 0;
+          }
      }
    else
      {
@@ -1618,6 +1704,7 @@ notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
         pasteimage_append(pi, sel->requestwidget);
         savedtypes.pi = NULL;
      }
+   free(stripstr);
    return 0;
 }
 
@@ -1737,7 +1824,8 @@ notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
         sel->datacb(sel->udata, sel->widget, &ddata);
      }
    else
-     elm_entry_entry_insert(sel->requestwidget, stripstr);
+     entry_insert_filter(sel->requestwidget, stripstr);
+     //elm_entry_entry_insert(sel->requestwidget, stripstr);
 
    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
    free(stripstr);
@@ -1772,7 +1860,8 @@ notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
         sel->datacb(sel->udata, sel->widget, &ddata);
      }
    else
-     elm_entry_entry_insert(sel->requestwidget, stripstr);
+     entry_insert_filter(sel->requestwidget, stripstr);
+     //elm_entry_entry_insert(sel->requestwidget, stripstr);
 
    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
    free(stripstr);
@@ -1820,7 +1909,12 @@ edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **
    if (size_ret) *size_ret = strlen(sel->selbuf);*/
    char *edje = NULL;
 
-   if (data_ret && (sel->format & ELM_SEL_FORMAT_HTML))
+   if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
+     {
+        if (sel->selbuf && sel->selbuf[0] != '\0')
+          edje = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
+     }
+   else if (data_ret && ((sel->format & ELM_SEL_FORMAT_HTML)))
      {
         Eina_List *nodeList = NULL;
         Eina_List *trail;
@@ -1855,14 +1949,20 @@ edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **
         if (edje)
           *data_ret = edje;
         else
-          *data_ret = strdup(sel->selbuf);
+           if (sel->selbuf)
+             *data_ret = strdup(sel->selbuf);
+           else
+             *data_ret = strdup("");
      }
    if (size_ret)
      {
         if (edje)
           *size_ret = strlen(edje);
         else
-          *size_ret = strlen(sel->selbuf);
+          if (sel->selbuf)
+            *size_ret = strlen(sel->selbuf);
+          else
+            *size_ret = 0;
      }
 
    return EINA_TRUE;
@@ -1876,13 +1976,25 @@ html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **
    sel = selections + *(int *)data;
 
    char *html = NULL;
-   if (data_ret && (sel->format & ELM_SEL_FORMAT_MARKUP))
+   char *convert_target = sel->selbuf;
+   Eina_Bool convert_edje = EINA_FALSE;
+
+   if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
+     {
+        if (sel->selbuf && sel->selbuf[0] != '\0')
+          {
+             convert_target = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
+             convert_edje = EINA_TRUE;
+          }
+     }
+
+   if (data_ret && ((sel->format & ELM_SEL_FORMAT_MARKUP) || convert_edje))
      {
         Eina_List *nodeList = NULL;
         Eina_List *trail;
         PTagNode nodeData;
 
-        nodeData = _get_start_node(sel->selbuf);
+        nodeData = _get_start_node(convert_target);
 
         while (nodeData)
           {
@@ -1910,7 +2022,10 @@ html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **
         if (html)
           *data_ret = html;
         else
-          *data_ret = strdup(sel->selbuf);
+          if (sel->selbuf)
+            *data_ret = strdup(sel->selbuf);
+          else
+            *data_ret = strdup("");
      }
 
    if (size_ret)
@@ -1918,8 +2033,13 @@ html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **
         if (html)
           *size_ret = strlen(html);
         else
-          *size_ret = strlen(sel->selbuf);
+          if (sel->selbuf)
+            *size_ret = strlen(sel->selbuf);
+          else
+            *size_ret = 0;
      }
+   if (convert_target != sel->selbuf)
+     free(convert_target);
 
    return EINA_TRUE;
 }
@@ -2042,6 +2162,7 @@ pasteimage_append(Paste_Image *pi, Evas_Object *entry)
 
    if (!pi) return EINA_FALSE;
    if (!entry) return EINA_FALSE;
+   if (elm_entry_cnp_textonly_get(entry)) return EINA_FALSE;
 
    pasteimage_provider_set(entry);
 
@@ -2601,7 +2722,6 @@ elm_cnp_tempfile_create(int size)
 
    tmppath = getenv("TMP");
    if (!tmppath) tmppath = P_tmpdir;
-   if (!tmppath) tmppath = "/tmp";
    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
    if (len < 0)
      {
@@ -2658,7 +2778,7 @@ elm_cnp_tempfile_create(int size)
         return info;
      }
 
-   //eina_mmap_safety_enabled_set(EINA_TRUE); // NOTE: Temporary comment
+   eina_mmap_safety_enabled_set(EINA_TRUE);
 
    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
    if (info->map == MAP_FAILED)