[Refactoring] Removed trailing whitespaces.
[framework/uifw/elementary.git] / src / lib / elm_cnp_helper.c
index 68262e1..8942909 100644 (file)
@@ -12,7 +12,6 @@
 
 //#define DEBUGON 1
 
-
 #ifdef DEBUGON
 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
 #else
@@ -143,7 +142,6 @@ static char *mark_up(const char *start, int inlen, int *lenp);
 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
 static void entry_deleted(void *images, Evas *e, Evas_Object *entry, void *unused);
 
-
 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
@@ -158,6 +156,7 @@ 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);
 
@@ -189,10 +188,10 @@ static Cnp_Atom atoms[CNP_N_ATOMS] = {
      },
      [CNP_ATOM_XELM] =  {
           "application/x-elementary-markup",
-          ELM_SEL_FORMAT_MARKUP,
+          ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
           edje_converter,
           NULL,
-          NULL,
+          notify_handler_edje,
           0
      },
      [CNP_ATOM_text_uri] = {
@@ -406,7 +405,6 @@ static Ecore_Event_Handler *handler_status = NULL;
 /* Stringshared, so I can just compare pointers later */
 static const char *text_uri;
 
-
 /* For convert EFL to HTML */
 
 #define TAGPOS_START    0x00000001
@@ -514,7 +512,11 @@ static PTagNode
 _new_tag_node(char *tag, char *tag_str, char* str, char *pos_in_ori_str)
 {
    PTagNode newNode = calloc(1, sizeof(TagNode));
+   if (tag)
+     eina_str_tolower(&tag);
    newNode->tag = tag;
+   if (tag_str)
+     eina_str_tolower(&tag_str);
    newNode->tag_str = tag_str;
    newNode->str = str;
    newNode->pos_in_ori_str = pos_in_ori_str;
@@ -823,10 +825,10 @@ _set_EFL_item_data(PItemTagData data, const char *tag_str)
              strcat(modify, path);
              data->href = modify;
              cnp_debug("image href ---%s---\n", data->href);
+             free(value);
           }
         else
           freeAndAssign(data->href, value);
-        free(value);
      }
 
    value = _get_tag_value(tag_str, "absize");
@@ -905,10 +907,10 @@ _set_HTML_img_data(PItemTagData data, const char *tag_str)
              strcat(modify, path);
              data->href = modify;
              cnp_debug("image src ---%s---\n", data->href);
+             free(value);
           }
         else
           freeAndAssign(data->href, value);
-        free(value);
      }
 
    value = _get_tag_value(tag_str, "width");
@@ -1112,13 +1114,13 @@ _convert_to_edje(Eina_List* nodes)
                               {
                                  PItemTagData data = trail->tagData;
                                  char *width = IMAGE_DEFAULT_WIDTH, *height = IMAGE_DEFAULT_HEIGHT;
-                                 if (data->href)
-                                   eina_strbuf_append_printf(html, " href=%s", data->href);
                                  if (data->width)
                                    width = data->width;
                                  if (data->height)
                                    height = data->height;
-                                 eina_strbuf_append_printf(html, " absize=%sx%s></item>", width, height);
+                                 eina_strbuf_append_printf(html, " absize=%sx%s", width, height);
+                                 if (data->href)
+                                   eina_strbuf_append_printf(html, " href=%s></item>", data->href);
                                  break;
                               }
                          }
@@ -1148,9 +1150,11 @@ _convert_to_edje(Eina_List* nodes)
                          }
                        switch(trail->tagPosType)
                          {
+                            /* not support in efl
                           case TAGPOS_ALONE:
                              eina_strbuf_append(html, " />");
                              break;
+                             */
                           default:
                              eina_strbuf_append(html, ">");
                              break;
@@ -1168,12 +1172,18 @@ _convert_to_edje(Eina_List* nodes)
    return ret;
 
 }
+
 Eina_Bool
 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
 {
 #ifdef HAVE_ELEMENTARY_X
+   Evas_Object *top = elm_widget_top_get(widget);
+   Ecore_X_Window xwin;
    Cnp_Selection *sel;
 
+   if (top) xwin = elm_win_xwindow_get(top);
+   else xwin = elm_win_xwindow_get(widget);
+   if (!xwin) return EINA_FALSE;
    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
    if (!_elm_cnp_init_count) _elm_cnp_init();
    if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
@@ -1184,7 +1194,7 @@ elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format fo
    sel->active = 1;
    sel->widget = widget;
 
-   sel->set(elm_win_xwindow_get(widget),&selection,sizeof(Elm_Sel_Type));
+   sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
    sel->format = format;
    sel->selbuf = selbuf ? strdup(selbuf) : NULL;
 
@@ -1397,6 +1407,30 @@ vcard_send(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__,
 
    return EINA_TRUE;
 }
+
+static Eina_Bool
+is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
+{
+   Ecore_X_Selection_Data *data;
+   char *p;
+
+   data = notify->data;
+   cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
+   if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
+   else p = (char *)data->data;
+
+   if (!p) return EINA_TRUE;
+   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;
+     }
+
+   return EINA_TRUE;
+}
+
 /*
  * Callback to handle a targets response on a selection request:
  * So pick the format we'd like; and then request it.
@@ -1406,6 +1440,7 @@ notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notif
 {
    Ecore_X_Selection_Data_Targets *targets;
    Ecore_X_Atom *atomlist;
+   Evas_Object *top;
    int i, j;
 
    targets = notify->data;
@@ -1419,6 +1454,11 @@ notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notif
           {
              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
                {
+                  if ((j == CNP_ATOM_text_uri) ||
+                      (j == CNP_ATOM_text_urilist))
+                    {
+                      if(!is_uri_type_data(sel, notify)) continue;
+                    }
                   cnp_debug("Atom %s matches\n",atoms[j].name);
                   goto done;
                }
@@ -1429,8 +1469,10 @@ notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notif
    return ECORE_CALLBACK_PASS_ON;
 
 done:
-   cnp_debug("Sending request for %s\n",atoms[j].name);
-   sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
+   top = elm_widget_top_get(sel->requestwidget);
+   if (!top) top = sel->requestwidget;
+   cnp_debug("Sending request for %s\n", atoms[j].name);
+   sel->request(elm_win_xwindow_get(top), atoms[j].name);
 
    return ECORE_CALLBACK_PASS_ON;
 }
@@ -1485,7 +1527,7 @@ notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
    if (sel->datacb)
      {
         Elm_Selection_Data ddata;
-        
+
         str = mark_up((char *)data->data, data->length, NULL);
         ddata.x = ddata.y = 0;
         ddata.format = ELM_SEL_FORMAT_TEXT;
@@ -1495,7 +1537,7 @@ notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
         free(str);
         return 0;
      }
-   
+
    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);
@@ -1544,7 +1586,7 @@ notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
    if (sel->datacb)
      {
         Elm_Selection_Data ddata;
-        
+
         ddata.x = ddata.y = 0;
         ddata.format = ELM_SEL_FORMAT_MARKUP;
         ddata.data = p;
@@ -1671,6 +1713,34 @@ 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
+     elm_entry_entry_insert(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.
@@ -1685,23 +1755,24 @@ 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
+     elm_entry_entry_insert(sel->requestwidget, stripstr);
 
-   char *stripstr = NULL;
-   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_insert(sel->requestwidget, stripstr);
    free(stripstr);
    return 0;
 }
@@ -1742,7 +1813,6 @@ static Eina_Bool
 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
 {
    Cnp_Selection *sel;
-
    sel = selections + *((int *)data);
 /*   if (data_ret) *data_ret = strdup(sel->selbuf);
    if (size_ret) *size_ret = strlen(sel->selbuf);*/
@@ -2058,6 +2128,7 @@ mark_up(const char *start, int inlen, int *lenp)
           {
              if (*p == escapes[i].value)
                {
+                  if (!iscntrl(escapes[i].value)) l++;
                   l += strlen(escapes[i].escape);
                   break;
                }
@@ -2068,12 +2139,13 @@ mark_up(const char *start, int inlen, int *lenp)
    q = ret = malloc(l + 1);
 
    /* Second pass: Change characters */
-   for (p = start; *p; )
+   for (p = start; ((!endp) || (p < endp)) && (*p); )
      {
         for (i = 0; i < N_ESCAPES; i++)
           {
              if (*p == escapes[i].value)
                {
+                  if (!iscntrl(escapes[i].value)) *q++ = '&';
                   strcpy(q, escapes[i].escape);
                   q += strlen(escapes[i].escape);
                   p ++;
@@ -2114,6 +2186,7 @@ _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
              /* Request it, so we know what it is */
              cnp_debug("Sending uri request\n");
              savedtypes.textreq = 1;
+             if (savedtypes.pi) pasteimage_free(savedtypes.pi);
              savedtypes.pi = NULL; /* FIXME: Free? */
              ecore_x_selection_xdnd_request(enter->win, text_uri);
           }
@@ -2222,6 +2295,9 @@ found:
                   cnp_debug("Insert %s\n", (char *)ddata.data);
                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
                   ecore_x_dnd_send_finished();
+
+                  if (savedtypes.pi) pasteimage_free(savedtypes.pi);
+                  savedtypes.pi = NULL;
                   return EINA_TRUE;
                }
              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
@@ -2232,7 +2308,7 @@ found:
                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
                   ecore_x_dnd_send_finished();
 
-                  pasteimage_free(savedtypes.pi);
+                  if (savedtypes.pi) pasteimage_free(savedtypes.pi);
                   savedtypes.pi = NULL;
 
                   return EINA_TRUE;
@@ -2580,6 +2656,8 @@ elm_cnp_tempfile_create(int size)
         return info;
      }
 
+   //eina_mmap_safety_enabled_set(EINA_TRUE); // NOTE: Temporary comment
+
    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
    if (info->map == MAP_FAILED)
      {