Bugfix: a converting problem using elm_selection_get with ELM_SEL_FORMAT_HTML
[framework/uifw/elementary.git] / src / lib / elm_cnp_helper.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4 #include <Elementary.h>
5 #include "elm_priv.h"
6
7 #include <sys/mman.h>
8
9 #ifdef HAVE_ELEMENTARY_X
10
11 #define ARRAYINIT(foo)  [foo] =
12
13 //#define DEBUGON 1
14
15 #ifdef DEBUGON
16 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
17 #else
18 # define cnp_debug(x...)
19 #endif
20
21 #define PROVIDER_SET "__elm_cnp_provider_set"
22
23 typedef struct _Paste_Image   Paste_Image;
24 typedef struct _Cnp_Selection Cnp_Selection;
25 typedef struct _Escape        Escape;
26 typedef struct _Tmp_Info      Tmp_Info;
27 typedef struct _Cnp_Atom      Cnp_Atom;
28 typedef struct _Saved_Type    Saved_Type;
29 typedef struct _Dropable      Dropable;
30
31 typedef Eina_Bool (*Converter_Fn_Cb)     (char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
32 typedef int       (*Response_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
33 typedef int       (*Notify_Handler_Cb)   (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
34
35 enum
36 {
37    CNP_ATOM_TARGETS = 0,
38    CNP_ATOM_text_uri,
39    CNP_ATOM_text_urilist,
40    CNP_ATOM_text_x_vcard,
41    CNP_ATOM_image_png,
42    CNP_ATOM_image_jpeg,
43    CNP_ATOM_image_bmp,
44    CNP_ATOM_image_gif,
45    CNP_ATOM_image_tiff,
46    CNP_ATOM_image_svg,
47    CNP_ATOM_image_xpm,
48    CNP_ATOM_image_tga,
49    CNP_ATOM_image_ppm,
50    CNP_ATOM_XELM,
51    CNP_ATOM_text_html_utf8,
52    CNP_ATOM_text_html,
53    CNP_ATOM_UTF8STRING,
54    CNP_ATOM_STRING,
55    CNP_ATOM_TEXT,
56    CNP_ATOM_text_plain_utf8,
57    CNP_ATOM_text_plain,
58
59    CNP_N_ATOMS,
60 };
61
62 struct _Paste_Image
63 {
64    Evas_Object *entry;
65    const char  *tag;
66    const char  *file;
67    Evas_Object *img;
68 };
69
70 struct _Cnp_Selection
71 {
72    const char      *debug;
73    Evas_Object     *widget;
74    char            *selbuf;
75    Evas_Object     *requestwidget;
76    void            *udata;
77    Elm_Sel_Format   requestformat;
78    Elm_Drop_Cb      datacb;
79    Eina_Bool      (*set)     (Ecore_X_Window, const void *data, int size);
80    Eina_Bool      (*clear)   (void);
81    void           (*request) (Ecore_X_Window, const char *target);
82
83    Elm_Sel_Format    format;
84    Ecore_X_Selection ecore_sel;
85
86    Eina_Bool         active : 1;
87 };
88
89 struct _Escape
90 {
91    const char *escape;
92    const char  value;
93 };
94
95 struct _Tmp_Info
96 {
97    char *filename;
98    void *map;
99    int   fd;
100    int   len;
101 };
102
103 struct _Cnp_Atom
104 {
105    const char          *name;
106    Elm_Sel_Format       formats;
107    /* Called by ecore to do conversion */
108    Converter_Fn_Cb      converter;
109    Response_Handler_Cb  response;
110    Notify_Handler_Cb    notify;
111    /* Atom */
112    Ecore_X_Atom         atom;
113 };
114
115 struct _Saved_Type
116 {
117    const char  **types;
118    Paste_Image  *pi;
119    int           ntypes;
120    int           x, y;
121    Eina_Bool     textreq: 1;
122 };
123
124 struct _Dropable
125 {
126    Evas_Object     *obj;
127    /* FIXME: Cache window */
128    Elm_Sel_Format   types;
129    Elm_Drop_Cb      dropcb;
130    void            *cbdata;
131 };
132
133 static Tmp_Info *elm_cnp_tempfile_create(int size);
134 static int tmpinfo_free(Tmp_Info *tmp);
135
136 static Eina_Bool _elm_cnp_init(void);
137 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
138 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
139 static char *remove_tags(const char *p, int *len);
140 static char *mark_up(const char *start, int inlen, int *lenp);
141
142 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
143 static void entry_deleted(void *images, Evas *e, Evas_Object *entry, void *unused);
144
145 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
146 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
147 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
148 static Eina_Bool edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
149 static Eina_Bool uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
150 static Eina_Bool image_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
151 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
152
153 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
154
155 static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
156 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
157 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
158 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
159 static int notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
160 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
161 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
162
163 static Paste_Image *pasteimage_alloc(const char *file, int pathlen);
164 static Eina_Bool pasteimage_append(Paste_Image *pi, Evas_Object *entry);
165 static void pasteimage_free(Paste_Image *pi);
166 static void entry_insert_filter(Evas_Object* entry, char* str);
167
168 /* Optimisation: Turn this into a 256 byte table:
169  *      then can lookup in one index, not N checks */
170 static const Escape escapes[] = {
171   { "<br>",   '\n' },
172   { "<ps>",   '\n' },
173   { "<\t>",   '\t' },
174   { "gt;",    '>'  },
175   { "lt;",    '<'  },
176   { "amp;",   '&'  },
177   { "quot;",  '\'' },
178   { "dquot;", '"'  }
179 };
180 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
181
182 static Cnp_Atom atoms[CNP_N_ATOMS] = {
183      [CNP_ATOM_TARGETS] = {
184           "TARGETS",
185           (Elm_Sel_Format) -1, // everything
186           targets_converter,
187           response_handler_targets,
188           notify_handler_targets,
189           0
190      },
191      [CNP_ATOM_XELM] =  {
192           "application/x-elementary-markup",
193           ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
194           edje_converter,
195           NULL,
196           notify_handler_edje,
197           0
198      },
199      [CNP_ATOM_text_uri] = {
200           "text/uri",
201           ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
202           uri_converter,
203           NULL,
204           notify_handler_uri,
205           0
206      },
207      [CNP_ATOM_text_urilist] = {
208           "text/uri-list",
209           ELM_SEL_FORMAT_IMAGE,
210           uri_converter,
211           NULL,
212           notify_handler_uri,
213           0
214      },
215      [CNP_ATOM_text_x_vcard] = {
216           "text/x-vcard",
217           ELM_SEL_FORMAT_VCARD,
218           vcard_send, NULL,
219           vcard_receive, 0
220      },
221      [CNP_ATOM_image_png] = {
222           "image/png",
223           ELM_SEL_FORMAT_IMAGE,
224           image_converter,
225           NULL,
226           notify_handler_image,
227           0
228      },
229      [CNP_ATOM_image_jpeg] = {
230           "image/jpeg",
231           ELM_SEL_FORMAT_IMAGE,
232           image_converter,
233           NULL,
234           notify_handler_image,/* Raw image data is the same */
235           0
236      },
237      [CNP_ATOM_image_bmp] = {
238           "image/x-ms-bmp",
239           ELM_SEL_FORMAT_IMAGE,
240           image_converter,
241           NULL,
242           notify_handler_image,/* Raw image data is the same */
243           0
244      },
245      [CNP_ATOM_image_gif] = {
246           "image/gif",
247           ELM_SEL_FORMAT_IMAGE,
248           image_converter,
249           NULL,
250           notify_handler_image,/* Raw image data is the same */
251           0
252      },
253      [CNP_ATOM_image_tiff] = {
254           "image/tiff",
255           ELM_SEL_FORMAT_IMAGE,
256           image_converter,
257           NULL,
258           notify_handler_image,/* Raw image data is the same */
259           0
260      },
261      [CNP_ATOM_image_svg] = {
262           "image/svg+xml",
263           ELM_SEL_FORMAT_IMAGE,
264           image_converter,
265           NULL,
266           notify_handler_image,/* Raw image data is the same */
267           0
268      },
269      [CNP_ATOM_image_xpm] = {
270           "image/x-xpixmap",
271           ELM_SEL_FORMAT_IMAGE,
272           image_converter,
273           NULL,
274           notify_handler_image,/* Raw image data is the same */
275           0
276      },
277      [CNP_ATOM_image_tga] = {
278           "image/x-tga",
279           ELM_SEL_FORMAT_IMAGE,
280           image_converter,
281           NULL,
282           notify_handler_image,/* Raw image data is the same */
283           0
284      },
285      [CNP_ATOM_image_ppm] = {
286           "image/x-portable-pixmap",
287           ELM_SEL_FORMAT_IMAGE,
288           image_converter,
289           NULL,
290           notify_handler_image,/* Raw image data is the same */
291           0
292      },
293      [CNP_ATOM_text_html_utf8] = {
294           "text/html;charset=utf-8",
295           ELM_SEL_FORMAT_HTML,
296           html_converter,
297           NULL,
298           notify_handler_html,
299           0
300      },
301      [CNP_ATOM_text_html] = {
302           "text/html",
303           ELM_SEL_FORMAT_HTML,
304           html_converter,
305           NULL,
306           notify_handler_html, /* No encoding: Webkit only */
307           0
308      },
309      [CNP_ATOM_UTF8STRING] = {
310           "UTF8_STRING",
311           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
312           text_converter,
313           NULL,
314           notify_handler_text,
315           0
316      },
317      [CNP_ATOM_STRING] = {
318           "STRING",
319           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
320           text_converter,
321           NULL,
322           notify_handler_text,
323           0
324      },
325      [CNP_ATOM_TEXT] = {
326           "TEXT",
327           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
328           text_converter,
329           NULL,
330           NULL,
331           0
332      },
333      [CNP_ATOM_text_plain_utf8] = {
334           "text/plain;charset=utf-8",
335           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
336           text_converter,
337           NULL,
338           NULL,
339           0
340      },
341      [CNP_ATOM_text_plain] = {
342           "text/plain",
343           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
344           text_converter,
345           NULL,
346           NULL,
347           0
348      },
349 };
350
351 static Cnp_Selection selections[ELM_SEL_MAX] = {
352      ARRAYINIT(ELM_SEL_PRIMARY) {
353           .debug = "Primary",
354           .ecore_sel = ECORE_X_SELECTION_PRIMARY,
355           .set = ecore_x_selection_primary_set,
356           .clear = ecore_x_selection_primary_clear,
357           .request = ecore_x_selection_primary_request,
358      },
359      ARRAYINIT(ELM_SEL_SECONDARY) {
360           .debug = "Secondary",
361           .ecore_sel = ECORE_X_SELECTION_SECONDARY,
362           .set = ecore_x_selection_secondary_set,
363           .clear = ecore_x_selection_secondary_clear,
364           .request = ecore_x_selection_secondary_request,
365      },
366      ARRAYINIT(ELM_SEL_CLIPBOARD) {
367           .debug = "Clipboard",
368           .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
369           .set = ecore_x_selection_clipboard_set,
370           .clear = ecore_x_selection_clipboard_clear,
371           .request = ecore_x_selection_clipboard_request,
372      },
373      ARRAYINIT(ELM_SEL_XDND) {
374           .debug = "XDnD",
375           .ecore_sel = ECORE_X_SELECTION_XDND,
376           .request = ecore_x_selection_xdnd_request,
377      },
378 };
379
380 /* Data for DND in progress */
381 static Saved_Type savedtypes =  { NULL, NULL, 0, 0, 0, EINA_FALSE };
382
383 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
384 static void *dragdonedata = NULL;
385
386 static int _elm_cnp_init_count = 0;
387 /* FIXME: who left this out of XAtoms.h */
388 static Ecore_X_Atom clipboard_atom;
389
390 static Eina_List *pastedimages = NULL;
391
392 /**
393  * Drag & Drop functions
394  */
395
396 /* FIXME: Way too many globals */
397 static Eina_List *drops = NULL;
398 static Evas_Object *dragwin = NULL;
399 static int _dragx = 0, _dragy = 0;
400 static Ecore_Event_Handler *handler_pos = NULL;
401 static Ecore_Event_Handler *handler_drop = NULL;
402 static Ecore_Event_Handler *handler_enter = NULL;
403 static Ecore_Event_Handler *handler_status = NULL;
404
405 #endif
406
407 /* Stringshared, so I can just compare pointers later */
408 static const char *text_uri;
409
410 /* For convert EFL to HTML */
411
412 #define TAGPOS_START    0x00000001
413 #define TAGPOS_END      0x00000002
414 #define TAGPOS_ALONE    0x00000003
415
416 /* TEXTBLOCK tag using stack but close tag word has no mean maybe bug...
417  * TEXTBLOCK <b>bold<font>font</b>bold</font>
418  * HTML <b>bold<font>font bold</b>font</font> */
419
420 typedef struct _TagTable {
421      char *src;
422      char *dst;
423      char tagType;
424 }TagTable;
425
426 TagTable _EFLtoHTMLConvertTable[] = {
427        {"font", "font", 0},
428        {"underline", "del", 0},
429        {"strikethrough", "ins", 0},
430        {"br", "br", 1},
431        {"ps", "br", 1},
432        {"b", "b", 1},
433        {"item", "img", 1}
434 };
435
436 TagTable _HTMLtoEFLConvertTable[] = {
437        {"font", "", 0},
438        {"del", "underline", 0},
439        {"u", "underline", 0},
440        {"ins", "strikethrough", 0},
441        {"s", "strikethrough", 0},
442        {"br", "br", 1},
443        {"b", "b", 1},
444        {"strong", "b", 1},
445        {"img", "item", 1}
446 };
447
448
449 typedef struct _TagNode TagNode, *PTagNode;
450 struct _TagNode {
451      char *tag;  //EINA_STRINGSHARE if NULL just str
452      char *tag_str;
453      char *str;
454      char *pos_in_ori_str;
455      PTagNode matchTag;
456      void *tagData;
457      unsigned char tagPosType;
458 };
459
460 typedef struct _FontTagData FontTagData, *PFontTagData;
461 struct _FontTagData {
462      char *name;
463      char *color;
464      char *size;
465      char *bg_color;
466 };
467
468
469 typedef struct _ItemTagData ItemTagData, *PItemTagData;
470 struct _ItemTagData {
471      char *href;
472      char *width;
473      char *height;
474 };
475
476 #define SAFEFREE(ptr) \
477    do\
478 {\
479    if (ptr)\
480    free(ptr);\
481    ptr = NULL;\
482 } while(0);\
483
484 #define freeAndAssign(dst, value) \
485    do\
486 {\
487    if (value)\
488      {\
489         SAFEFREE(dst);\
490         dst = value;\
491      }\
492 } while(0);
493
494
495 static PTagNode _new_tag_node(char *tag, char *tag_str, char* str, char *pos_in_ori_str);
496 static PTagNode _get_start_node(char *str);
497 static PTagNode _get_next_node(PTagNode prev);
498 static void _delete_node(PTagNode node);
499 static void _link_match_tags(Eina_List *nodes);
500 static char *_get_tag_value(const char *tag_str, const char *tag_name);
501 static char *_convert_to_html(Eina_List* nodes);
502 static void _set_EFL_tag_data(Eina_List* nodes);
503 static char *_convert_to_edje(Eina_List* nodes);
504 static void _set_HTML_tag_data(Eina_List* nodes);
505 static PFontTagData _set_EFL_font_data(PFontTagData data, const char *tag_str);
506 static PItemTagData _set_EFL_item_data(PItemTagData data, const char *tag_str);
507 static PFontTagData _set_HTML_font_data(PFontTagData data, const char *tag_str);
508 static PItemTagData _set_HTML_img_data(PItemTagData data, const char *tag_str);
509
510 #ifdef DEBUGON
511 static void _dumpNode(Eina_List* nodes);
512 #endif
513
514 static PTagNode
515 _new_tag_node(char *tag, char *tag_str, char* str, char *pos_in_ori_str)
516 {
517    PTagNode newNode = calloc(1, sizeof(TagNode));
518    if (tag)
519      eina_str_tolower(&tag);
520    newNode->tag = tag;
521    if (tag_str)
522      eina_str_tolower(&tag_str);
523    newNode->tag_str = tag_str;
524    newNode->str = str;
525    newNode->pos_in_ori_str = pos_in_ori_str;
526    return newNode;
527 }
528
529 static PTagNode
530 _get_start_node(char *str)
531 {
532    char *startStr = NULL;
533    if (!str || str[0] == '\0')
534      return NULL;
535
536    if (str[0] != '<')
537      {
538         char *tagStart = strchr(str, '<');
539         if (!tagStart)
540           startStr = strdup(str);
541         else
542           {
543              int strLength = tagStart - str;
544              startStr = malloc(sizeof(char) * (strLength + 1));
545              strncpy(startStr, str, strLength);
546              startStr[strLength] = '\0';
547           }
548      }
549
550    return _new_tag_node(NULL, NULL, startStr, str);
551 }
552
553 static PTagNode
554 _get_next_node(PTagNode prev)
555 {
556    PTagNode retTag = NULL;
557    char *tagStart;
558    char *tagEnd;
559    char *tagNameEnd = NULL;
560    char *nextTagStart;
561
562    if (prev->tag == NULL)
563      tagStart = strchr(prev->pos_in_ori_str, '<');
564    else
565      tagStart = strchr(prev->pos_in_ori_str + 1, '<');
566
567    if (!tagStart)
568      return retTag;
569
570    tagEnd = strchr(tagStart, '>');
571    nextTagStart = strchr(tagStart + 1, '<');
572
573    if (!tagEnd || (nextTagStart && (nextTagStart < tagEnd)))
574         return _get_start_node(tagStart + 1);
575
576    int spCnt = 5;
577    char *spArray[spCnt];
578    spArray[0] = strchr(tagStart, '=');
579    spArray[1] = strchr(tagStart, '_');
580    spArray[2] = strchr(tagStart, ' ');
581    spArray[3] = strchr(tagStart, '\t');
582    spArray[4] = strchr(tagStart, '\n');
583    tagNameEnd = tagEnd;
584
585    int i;
586    for (i = 0; i < spCnt; i++)
587      {
588         if (spArray[i] && spArray[i] < tagNameEnd)
589           tagNameEnd = spArray[i];
590      }
591
592    int tagLength = tagNameEnd - tagStart - 1;
593    char *tagName = NULL;
594    if (!strncmp(&tagStart[1], "color", tagLength))
595      tagName = strndup("font", 4);
596    else if (!strncmp(&tagStart[1], "/color", tagLength))
597      tagName = strndup("/font", 5);
598    else if (!strncmp(&tagStart[1], "/item", tagLength))
599      tagName = strdup("");
600    else
601      tagName = strndup(&tagStart[1], tagLength);
602
603    int tagStrLength = 0;
604    char *tagStr = NULL;
605    if (tagName)
606      {
607         tagStrLength = tagEnd - tagStart + 1;
608         tagStr = strndup(tagStart, tagStrLength);
609      }
610
611    unsigned int strLength = nextTagStart ? (unsigned int)(nextTagStart - tagEnd - 1) : strlen(&tagEnd[1]);
612    char *str = strndup(&tagEnd[1], strLength);
613
614    retTag = _new_tag_node(tagName, tagStr, str, tagStart);
615    return retTag;
616 }
617
618
619 static void
620 _delete_node(PTagNode node)
621 {
622    if (node)
623      {
624         SAFEFREE(node->tag_str);
625         SAFEFREE(node->str);
626
627         if (node->tagData)
628           {
629              if (node->tag)
630                {
631                   if (!strcmp("font", node->tag))
632                     {
633                        PFontTagData data = node->tagData;
634                        SAFEFREE(data->name);
635                        SAFEFREE(data->color);
636                        SAFEFREE(data->size);
637                        SAFEFREE(data->bg_color);
638                     }
639                   if (!strcmp("item", node->tag))
640                     {
641                        PItemTagData data = node->tagData;
642                        SAFEFREE(data->href);
643                        SAFEFREE(data->width);
644                        SAFEFREE(data->height);
645                     }
646
647                }
648              SAFEFREE(node->tagData);
649           }
650         SAFEFREE(node->tag);
651         SAFEFREE(node);
652      }
653 }
654
655 static void
656 _link_match_tags(Eina_List *nodes)
657 {
658    Eina_List *stack = NULL;
659
660    PTagNode trail, popData;
661    Eina_List *l, *r;
662
663    EINA_LIST_FOREACH(nodes, l, trail)
664      {
665         if (!trail->tag || trail->tag[0] == '\0')
666           continue;
667         if (!strcmp("br", trail->tag))
668           {
669              trail->tagPosType = TAGPOS_ALONE;
670              continue;
671           }
672         else if (!strcmp("item", trail->tag) || !strcmp("img", trail->tag))
673           {
674              trail->tagPosType = TAGPOS_ALONE;
675              continue;
676           }
677
678         if (trail->tag[0] != '/') // PUSH
679           {
680              stack = eina_list_append(stack, trail);
681 /*             eina_array_push(stack, trail);
682              cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
683              cnp_debug("stack: %d, tag %s\n", eina_list_count(stack), trail->tag);
684           }
685         else // POP
686           {
687              if (!eina_list_count(stack))
688                {
689                   cnp_debug("tag not matched %s\n", trail->tag);
690                   continue;
691                }
692
693              EINA_LIST_REVERSE_FOREACH(stack, r, popData)
694                {
695                   if (popData->tag && !strcmp(popData->tag, &trail->tag[1]))
696                     {
697                        popData->tagPosType = TAGPOS_START;
698                        trail->tagPosType = TAGPOS_END;
699                        popData->matchTag = trail;
700                        trail->matchTag = popData;
701                        stack = eina_list_remove_list(stack, r);
702                        break;
703                     }
704                }
705 /*             popData = eina_array_pop(stack);
706
707              popData->tagPosType = TAGPOS_START;
708              trail->tagPosType = TAGPOS_END;
709              popData->matchTag = trail;
710              trail->matchTag = popData;
711              cnp_debug("pop stack: %d, tag %s\n", eina_array_count_get(stack), trail->tag);
712              */
713           }
714      }
715
716 /*   if (eina_array_count_get(stack))
717      cnp_debug("stack state: %d, tag %s\n", eina_array_count_get(stack), trail->tag);*/
718
719    /* Make Dummy close tag */
720 /*   while ((popData = eina_array_pop(stack)))  */
721
722    EINA_LIST_REVERSE_FOREACH(stack, r, popData)
723      {
724         PTagNode newData;
725         int tagLength = strlen(popData->tag);
726         char *tagName = malloc(sizeof(char) * (tagLength + 2));
727
728         tagName[0] = '/';
729         tagName[1] = '\0';
730         strcat(tagName, popData->tag);
731
732         newData = _new_tag_node(tagName, NULL, NULL, NULL);
733         popData->tagPosType = TAGPOS_START;
734         newData->tagPosType = TAGPOS_END;
735         popData->matchTag = newData;
736         newData->matchTag = popData;
737         nodes = eina_list_append(nodes, newData);
738 /*        cnp_debug("stack: %d, tag %s\n", eina_array_count_get(stack), popData->tag);*/
739      }
740 /*   cnp_debug("stack_top: %d\n", eina_array_count_get(stack));
741    eina_array_free(stack);*/
742    eina_list_free(stack);
743 }
744
745 static char *
746 _get_tag_value(const char *tag_str, const char *tag_name)
747 {
748    if (!tag_name || !tag_str)
749      return NULL;
750
751    char *tag;
752    if ((tag = strstr(tag_str, tag_name)))
753      {
754         if (tag[strlen(tag_name)] == '_')
755           return NULL;
756         char *value = strchr(tag, '=');
757         if (value)
758           {
759              do
760                {
761                   value++;
762                } while (!isalnum(*value) && *value != '#');
763
764              int spCnt = 6;
765              char *spArray[spCnt];
766              spArray[0] = strchr(value, ' ');
767              spArray[1] = strchr(value, '>');
768              spArray[2] = strchr(value, '\"');
769              spArray[3] = strchr(value, '\'');
770              spArray[4] = strchr(value, '\t');
771              spArray[5] = strchr(value, '\n');
772              char *valueEnd = strchr(value, '\0');
773
774              int i;
775              int start = 0;
776              if ((!strncmp(tag_str, "<item", 5) && !strcmp(tag_name, "href")) // EFL img tag
777                || (!strncmp(tag_str, "<img", 4) && !strcmp(tag_name, "src"))) // HTML img tag
778                start = 1;
779
780              for (i = start; i < spCnt; i++)
781                {
782                   if (spArray[i] && spArray[i] < valueEnd)
783                     valueEnd = spArray[i];
784                }
785
786              int valueLength = valueEnd - value;
787              return strndup(value, valueLength);
788           }
789      }
790    return NULL;
791 }
792
793 static PFontTagData
794 _set_EFL_font_data(PFontTagData data, const char *tag_str)
795 {
796    char *value;
797
798    if (!data)
799      data = calloc(1, sizeof(FontTagData));
800    value = _get_tag_value(tag_str, "font_size");
801    freeAndAssign(data->size, value);
802    value = _get_tag_value(tag_str, "color");
803    freeAndAssign(data->color, value);
804    value = _get_tag_value(tag_str, "bgcolor");
805    freeAndAssign(data->bg_color, value);
806    value = _get_tag_value(tag_str, "font");
807    freeAndAssign(data->name, value);
808
809    return data;
810 }
811
812 static PItemTagData
813 _set_EFL_item_data(PItemTagData data, const char *tag_str)
814 {
815    char *value;
816
817    if (!data)
818      data = calloc(1, sizeof(ItemTagData));
819    value = _get_tag_value(tag_str, "href");
820    if (value)
821      {
822         char *path = strstr(value, "file://");
823         if (path)
824           {
825              char *modify = malloc(sizeof(char) * (strlen(value) + 1));
826              strncpy(modify, "file://", 8);
827              path += 7;
828              while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
829                {
830                   path++;
831                }
832              strcat(modify, path);
833              data->href = modify;
834              cnp_debug("image href ---%s---\n", data->href);
835              free(value);
836           }
837         else
838           freeAndAssign(data->href, value);
839      }
840
841    value = _get_tag_value(tag_str, "absize");
842    if (value)
843      {
844         char *xpos = strchr(value, 'x');
845         if (xpos)
846           {
847              int absizeLen = strlen(value);
848              freeAndAssign(data->width, strndup(value, xpos - value));
849              freeAndAssign(data->height, strndup(xpos + 1, absizeLen - (xpos - value) - 1));
850              cnp_debug("image width: -%s-, height: -%s-\n", data->width, data->height);
851           }
852         free(value);
853      }
854    return data;
855 }
856
857 static void
858 _set_EFL_tag_data(Eina_List* nodes)
859 {
860    PTagNode trail;
861    Eina_List *l;
862
863    EINA_LIST_FOREACH(nodes, l, trail)
864      {
865         if (!trail->tag)
866           continue;
867         if (!strcmp("font", trail->tag))
868              trail->tagData = _set_EFL_font_data(trail->tagData, trail->tag_str);
869         else if (!strcmp("item", trail->tag))
870              trail->tagData = _set_EFL_item_data(trail->tagData, trail->tag_str);
871      }
872 }
873
874 static PFontTagData
875 _set_HTML_font_data(PFontTagData data, const char *tag_str)
876 {
877    char *value;
878
879    if (!data)
880      data = calloc(1, sizeof(FontTagData));
881    value = _get_tag_value(tag_str, "size");
882    freeAndAssign(data->size, value);
883    value = _get_tag_value(tag_str, "color");
884    freeAndAssign(data->color, value);
885    value = _get_tag_value(tag_str, "bgcolor");
886    freeAndAssign(data->bg_color, value);
887    value = _get_tag_value(tag_str, "face");
888    freeAndAssign(data->name, value);
889
890    return data;
891 }
892
893 static PItemTagData
894 _set_HTML_img_data(PItemTagData data, const char *tag_str)
895 {
896    char *value;
897
898    if (!data)
899      data = calloc(1, sizeof(ItemTagData));
900    value = _get_tag_value(tag_str, "src");
901    if (value)
902      {
903         char *path = strstr(value, "file://");
904         if (path)
905           {
906              char *modify = malloc(sizeof(char) * (strlen(value) + 1));
907              strncpy(modify, "file://", 8);
908              path += 7;
909              while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
910                {
911                   path++;
912                }
913              strcat(modify, path);
914              data->href = modify;
915              cnp_debug("image src ---%s---\n", data->href);
916              free(value);
917           }
918         else
919           freeAndAssign(data->href, value);
920      }
921
922    value = _get_tag_value(tag_str, "width");
923    freeAndAssign(data->width, value);
924    value = _get_tag_value(tag_str, "height");
925    freeAndAssign(data->height, value);
926    return data;
927 }
928
929 static void
930 _set_HTML_tag_data(Eina_List* nodes)
931 {
932    PTagNode trail;
933    Eina_List *l;
934
935    EINA_LIST_FOREACH(nodes, l, trail)
936      {
937         if (!trail->tag)
938           continue;
939         if (!strcmp("font", trail->tag))
940              trail->tagData = _set_HTML_font_data(trail->tagData, trail->tag_str);
941         else if (!strcmp("img", trail->tag))
942              trail->tagData = _set_HTML_img_data(trail->tagData, trail->tag_str);
943      }
944 }
945
946 #ifdef DEBUGON
947 static void
948 _dumpNode(Eina_List* nodes)
949 {
950    PTagNode trail;
951    Eina_List *l;
952
953    EINA_LIST_FOREACH(nodes, l, trail)
954      {
955         cnp_debug("tag: %s, tag_str: %s, str: %s, tagPosType: %d\n",
956                trail->tag, trail->tag_str, trail->str, trail->tagPosType);
957         cnp_debug("matchTag: %x ", (unsigned int)trail->matchTag);
958         if (trail->matchTag)
959           cnp_debug("matchTag->tag_str: %s", trail->matchTag->tag_str);
960         if (trail->tagData)
961           {
962              if (!strcmp(trail->tag, "font"))
963                {
964                   PFontTagData data = trail->tagData;
965                   cnp_debug(" tagData->name: %s, tagData->color: %s, tagData->size: %s, tagData->bg_color: %s",
966                          data->name, data->color, data->size, data->bg_color);
967                }
968              else if (!strcmp(trail->tag, "item") || !strcmp(trail->tag, "img"))
969                {
970                   PItemTagData data = trail->tagData;
971                   cnp_debug(" tagData->href: %s, tagData->width: %s, tagData->height: %s",
972                          data->href, data->width, data->height);
973                }
974              else
975                cnp_debug("\nERROR!!!! not need tagData");
976           }
977         cnp_debug("\n");
978      }
979 }
980 #endif
981
982 static char *
983 _convert_to_html(Eina_List* nodes)
984 {
985    PTagNode trail;
986    Eina_List *l;
987
988    Eina_Strbuf *html = eina_strbuf_new();
989
990    int tableCnt = sizeof(_EFLtoHTMLConvertTable) / sizeof(TagTable);
991
992    EINA_LIST_FOREACH(nodes, l, trail)
993      {
994         if (trail->tag)
995           {
996              char *tagName = trail->tagPosType == TAGPOS_END ?
997                 trail->matchTag->tag : trail->tag;
998              int j;
999              for(j = 0; j < tableCnt; j++)
1000                {
1001                   if (!strcmp(_EFLtoHTMLConvertTable[j].src, tagName))
1002                     {
1003                        switch(trail->tagPosType)
1004                          {
1005                           case TAGPOS_END:
1006                              eina_strbuf_append(html, "</");
1007                              break;
1008                           default:
1009                              eina_strbuf_append(html, "<");
1010                              break;
1011                          }
1012
1013                        eina_strbuf_append(html, _EFLtoHTMLConvertTable[j].dst);
1014                        if (trail->tagPosType != TAGPOS_END)
1015                          {
1016                             if (!strcmp(_EFLtoHTMLConvertTable[j].src, "font"))
1017                               {
1018                                  PFontTagData data = trail->tagData;
1019                                  if (data->name)
1020                                    {
1021                                    }
1022                                  if (data->color)
1023                                    eina_strbuf_append_printf(html, " color=\"%s\"", data->color);
1024                                  if (data->size)
1025                                    eina_strbuf_append_printf(html, " size=\"%s\"", data->size);
1026                                  if (data->bg_color)
1027                                    {
1028                                    }
1029                               }
1030                             else if (!strcmp(_EFLtoHTMLConvertTable[j].src, "item"))
1031                               {
1032                                  PItemTagData data = trail->tagData;
1033                                  if (data->href)
1034                                    eina_strbuf_append_printf(html, " src=\"%s\"", data->href);
1035                                  if (data->width)
1036                                    eina_strbuf_append_printf(html, " width=\"%s\"", data->width);
1037                                  if (data->height)
1038                                    eina_strbuf_append_printf(html, " height=\"%s\"", data->height);
1039                               }
1040                          }
1041                        switch(trail->tagPosType)
1042                          {
1043                             /* closed tag does not need in HTML
1044                           case TAGPOS_ALONE:
1045                              eina_strbuf_append(html, " />");
1046                              break;*/
1047                           default:
1048                              eina_strbuf_append(html, ">");
1049                              break;
1050                          }
1051                        break;
1052                     }
1053                }
1054           }
1055         if (trail->str)
1056           eina_strbuf_append(html, trail->str);
1057      }
1058
1059    char *ret = eina_strbuf_string_steal(html);
1060    eina_strbuf_free(html);
1061    return ret;
1062 }
1063
1064 #define IMAGE_DEFAULT_WIDTH "240"
1065 #define IMAGE_DEFAULT_HEIGHT "180"
1066
1067
1068 static char *
1069 _convert_to_edje(Eina_List* nodes)
1070 {
1071    PTagNode trail;
1072    Eina_List *l;
1073
1074    Eina_Strbuf *html = eina_strbuf_new();
1075
1076    int tableCnt = sizeof(_HTMLtoEFLConvertTable) / sizeof(TagTable);
1077
1078    EINA_LIST_FOREACH(nodes, l, trail)
1079      {
1080         if (trail->tag)
1081           {
1082              char *tagName = trail->tagPosType == TAGPOS_END ?
1083                 trail->matchTag->tag : trail->tag;
1084              int j;
1085              for(j = 0; j < tableCnt; j++)
1086                {
1087                   if (!strcmp(_HTMLtoEFLConvertTable[j].src, tagName))
1088                     {
1089                        if (_HTMLtoEFLConvertTable[j].dst[0] != '\0')
1090                          {
1091                             switch(trail->tagPosType)
1092                               {
1093                                case TAGPOS_END:
1094                                   eina_strbuf_append(html, "</");
1095                                   break;
1096                                default:
1097                                   eina_strbuf_append(html, "<");
1098                                   break;
1099                               }
1100
1101                             eina_strbuf_append(html, _HTMLtoEFLConvertTable[j].dst);
1102                          }
1103                        if (trail->tagPosType != TAGPOS_END)
1104                          {
1105                             if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1106                               {
1107                                  PFontTagData data = trail->tagData;
1108                                  if (data->name)
1109                                    {
1110                                    }
1111                                  if (data->color)
1112                                    eina_strbuf_append_printf(html, "<color=%s>", data->color);
1113                                  if (data->size)
1114                                    eina_strbuf_append_printf(html, "<font_size=%s>", data->size);
1115                                  if (data->bg_color)
1116                                    {
1117                                    }
1118                                  break;
1119                               }
1120                             else if (!strcmp(_HTMLtoEFLConvertTable[j].src, "img"))
1121                               {
1122                                  PItemTagData data = trail->tagData;
1123                                  char *width = IMAGE_DEFAULT_WIDTH, *height = IMAGE_DEFAULT_HEIGHT;
1124                                  if (data->width)
1125                                    width = data->width;
1126                                  if (data->height)
1127                                    height = data->height;
1128                                  eina_strbuf_append_printf(html, " absize=%sx%s", width, height);
1129                                  if (data->href)
1130                                    eina_strbuf_append_printf(html, " href=%s></item>", data->href);
1131                                  break;
1132                               }
1133                          }
1134                        else
1135                          {
1136                             if (_HTMLtoEFLConvertTable[j].dst[0] == '\0')
1137                               {
1138                                  if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1139                                    {
1140                                       if (trail->matchTag->tagData)
1141                                         {
1142                                            PFontTagData data = trail->matchTag->tagData;
1143                                            if (data->name)
1144                                              {
1145                                              }
1146                                            if (data->color)
1147                                              eina_strbuf_append_printf(html, "</color>");
1148                                            if (data->size)
1149                                              eina_strbuf_append_printf(html, "</font>");
1150                                            if (data->bg_color)
1151                                              {
1152                                              }
1153                                            break;
1154                                         }
1155                                    }
1156                               }
1157                          }
1158                        switch(trail->tagPosType)
1159                          {
1160                             /* not support in efl
1161                           case TAGPOS_ALONE:
1162                              eina_strbuf_append(html, " />");
1163                              break;
1164                              */
1165                           default:
1166                              eina_strbuf_append(html, ">");
1167                              break;
1168                          }
1169                        break;
1170                     }
1171                }/* for(j = 0; j < tableCnt; j++) end */
1172           }
1173         if (trail->str)
1174           eina_strbuf_append(html, trail->str);
1175      }
1176
1177    char *ret = eina_strbuf_string_steal(html);
1178    eina_strbuf_free(html);
1179    return ret;
1180
1181 }
1182
1183 Eina_Bool
1184 elm_selection_selection_has_owner(void)
1185 {
1186 #ifdef HAVE_ELEMENTARY_X
1187    return !!ecore_x_selection_owner_get(clipboard_atom);
1188 #else
1189    return EINA_FALSE;
1190 #endif
1191 }
1192
1193 Eina_Bool
1194 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
1195 {
1196 #ifdef HAVE_ELEMENTARY_X
1197    Evas_Object *top = elm_widget_top_get(widget);
1198    Ecore_X_Window xwin;
1199    Cnp_Selection *sel;
1200
1201    if (top) xwin = elm_win_xwindow_get(top);
1202    else xwin = elm_win_xwindow_get(widget);
1203    if (!xwin) return EINA_FALSE;
1204    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1205    if (!_elm_cnp_init_count) _elm_cnp_init();
1206    if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
1207      return elm_selection_clear(selection, widget);
1208
1209    sel = selections + selection;
1210
1211    sel->active = 1;
1212    sel->widget = widget;
1213
1214    sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
1215    sel->format = format;
1216    sel->selbuf = selbuf ? strdup(selbuf) : NULL;
1217
1218    return EINA_TRUE;
1219 #else
1220    return EINA_FALSE;
1221 #endif
1222 }
1223
1224 Eina_Bool
1225 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
1226 {
1227 #ifdef HAVE_ELEMENTARY_X
1228    Cnp_Selection *sel;
1229
1230    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1231    if (!_elm_cnp_init_count) _elm_cnp_init();
1232
1233    sel = selections + selection;
1234
1235    /* No longer this selection: Consider it gone! */
1236    if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
1237
1238    sel->active = 0;
1239    sel->widget = NULL;
1240    sel->clear();
1241
1242    return EINA_TRUE;
1243 #else
1244    return EINA_FALSE;
1245 #endif
1246 }
1247
1248 Eina_Bool
1249 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
1250                   Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
1251 {
1252 #ifdef HAVE_ELEMENTARY_X
1253    Evas_Object *top;
1254    Cnp_Selection *sel;
1255
1256    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1257    if (!_elm_cnp_init_count) _elm_cnp_init();
1258
1259    sel = selections + selection;
1260    top = elm_widget_top_get(widget);
1261    if (!top) return EINA_FALSE;
1262
1263    sel->requestformat = format;
1264    sel->requestwidget = widget;
1265    sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
1266    sel->datacb = datacb;
1267    sel->udata = udata;
1268
1269    return EINA_TRUE;
1270 #else
1271    return EINA_FALSE;
1272 #endif
1273 }
1274
1275 #ifdef HAVE_ELEMENTARY_X
1276
1277 static Eina_Bool
1278 _elm_cnp_init(void)
1279 {
1280    int i;
1281
1282    if (_elm_cnp_init_count++) return EINA_TRUE;
1283    for (i = 0; i < CNP_N_ATOMS; i++)
1284      {
1285         atoms[i].atom = ecore_x_atom_get(atoms[i].name);
1286         ecore_x_selection_converter_atom_add(atoms[i].atom,
1287                                              atoms[i].converter);
1288      }
1289    clipboard_atom = ecore_x_atom_get("CLIPBOARD");
1290
1291    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
1292    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
1293
1294    text_uri = eina_stringshare_add("text/uri-list");
1295    return EINA_TRUE;
1296 }
1297
1298 static Eina_Bool
1299 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
1300 {
1301    Ecore_X_Event_Selection_Clear *ev = event;
1302    Cnp_Selection *sel;
1303    int i;
1304
1305    for (i = 0; i < ELM_SEL_MAX; i++)
1306      {
1307         if (selections[i].ecore_sel == ev->selection) break;
1308      }
1309    cnp_debug("selection %d clear\n", i);
1310    /* Not me... Don't care */
1311    if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
1312
1313    sel = selections + i;
1314    sel->active = 0;
1315    sel->widget = NULL;
1316    sel->selbuf = NULL;
1317
1318    return ECORE_CALLBACK_PASS_ON;
1319 }
1320
1321
1322 /*
1323  * Response to a selection notify:
1324  *      - So we have asked for the selection list.
1325  *      - If it's the targets list, parse it, and fire of what we want,
1326  *      else it's the data we want.
1327  */
1328 static Eina_Bool
1329 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
1330 {
1331    Ecore_X_Event_Selection_Notify *ev = event;
1332    Cnp_Selection *sel;
1333    int i;
1334
1335    cnp_debug("selection notify callback: %d\n",ev->selection);
1336    switch (ev->selection)
1337      {
1338       case ECORE_X_SELECTION_CLIPBOARD:
1339          sel = selections + ELM_SEL_CLIPBOARD;
1340          break;
1341       case ECORE_X_SELECTION_PRIMARY:
1342          sel = selections + ELM_SEL_PRIMARY;
1343          break;
1344       case ECORE_X_SELECTION_SECONDARY:
1345          sel = selections + ELM_SEL_SECONDARY;
1346          break;
1347       case ECORE_X_SELECTION_XDND:
1348          sel = selections + ELM_SEL_XDND;
1349          break;
1350       default:
1351          return ECORE_CALLBACK_PASS_ON;
1352      }
1353    cnp_debug("Target is %s\n", ev->target);
1354
1355    for (i = 0; i < CNP_N_ATOMS; i++)
1356      {
1357         if (!strcmp(ev->target, atoms[i].name))
1358           {
1359              if (atoms[i].notify)
1360                {
1361                   cnp_debug("Found something: %s\n", atoms[i].name);
1362                   atoms[i].notify(sel, ev);
1363                }
1364              else
1365                {
1366                   cnp_debug("Ignored: No handler!\n");
1367                }
1368           }
1369      }
1370
1371    return ECORE_CALLBACK_PASS_ON;
1372 }
1373
1374
1375
1376 static Eina_Bool
1377 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
1378 {
1379    int i,count;
1380    Ecore_X_Atom *aret;
1381    Cnp_Selection *sel;
1382
1383    if (!data_ret) return EINA_FALSE;
1384    if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
1385      return EINA_FALSE;
1386
1387    sel = selections + *((int *)data);
1388
1389    for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
1390      {
1391         if (sel->format & atoms[i].formats) count++;
1392      }
1393
1394    aret = malloc(sizeof(Ecore_X_Atom) * count);
1395    for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
1396      {
1397         if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
1398      }
1399
1400    *data_ret = aret;
1401    if (typesize) *typesize = 32 /* urk */;
1402    if (ttype) *ttype = ECORE_X_ATOM_ATOM;
1403    if (size_ret) *size_ret = count;
1404
1405    return EINA_TRUE;
1406 }
1407
1408 static Eina_Bool
1409 image_converter(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__, void **data_ret __UNUSED__, int *size_ret __UNUSED__, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1410 {
1411    cnp_debug("Image converter called\n");
1412    return EINA_TRUE;
1413 }
1414
1415 static Eina_Bool
1416 vcard_send(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1417 {
1418    Cnp_Selection *sel;
1419
1420    cnp_debug("Vcard send called\n");
1421
1422    sel = selections + *((int *)data);
1423
1424    if (data_ret) *data_ret = strdup(sel->selbuf);
1425    if (size_ret) *size_ret = strlen(sel->selbuf);
1426
1427    return EINA_TRUE;
1428 }
1429
1430 static Eina_Bool
1431 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
1432 {
1433    Ecore_X_Selection_Data *data;
1434    char *p;
1435
1436    data = notify->data;
1437    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1438    if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
1439    else p = (char *)data->data;
1440
1441    if (!p) return EINA_TRUE;
1442    cnp_debug("Got %s\n", p);
1443    if (strncmp(p, "file://", 7))
1444      {
1445         if (*p != '/') return EINA_FALSE;
1446      }
1447
1448    return EINA_TRUE;
1449 }
1450
1451 /*
1452  * Callback to handle a targets response on a selection request:
1453  * So pick the format we'd like; and then request it.
1454  */
1455 static int
1456 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1457 {
1458    Ecore_X_Selection_Data_Targets *targets;
1459    Ecore_X_Atom *atomlist;
1460    Evas_Object *top;
1461    int i, j;
1462
1463    targets = notify->data;
1464    atomlist = (Ecore_X_Atom *)(targets->data.data);
1465
1466    for (j = 1; j < CNP_N_ATOMS; j++)
1467      {
1468         cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
1469         if (!(atoms[j].formats & sel->requestformat)) continue;
1470         for (i = 0; i < targets->data.length; i++)
1471           {
1472              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
1473                {
1474 #if 0
1475                   if ((j == CNP_ATOM_text_uri) ||
1476                       (j == CNP_ATOM_text_urilist))
1477                     {
1478                       if(!is_uri_type_data(sel, notify)) continue;
1479                     }
1480 #endif
1481                   /*
1482                    * temporary patch for elm_selection_get with ELM_SEL_FORMAT_HTML
1483                    * it will be removed after cbhm refactoring
1484                    */
1485                   if ((j == CNP_ATOM_XELM)
1486                       && (!(sel->requestformat & ELM_SEL_FORMAT_MARKUP)))
1487                     {
1488                        cnp_debug("Atom %s matched, but selection request is not ELM_SEL_FORMAT_MARKUP\n",
1489                                  atoms[j].name);
1490                        continue;
1491                     }
1492
1493                   cnp_debug("Atom %s matches\n",atoms[j].name);
1494                   goto done;
1495                }
1496           }
1497      }
1498
1499    cnp_debug("Couldn't find anything that matches\n");
1500    return ECORE_CALLBACK_PASS_ON;
1501
1502 done:
1503    top = elm_widget_top_get(sel->requestwidget);
1504    if (!top) top = sel->requestwidget;
1505    cnp_debug("Sending request for %s\n", atoms[j].name);
1506    sel->request(elm_win_xwindow_get(top), atoms[j].name);
1507
1508    return ECORE_CALLBACK_PASS_ON;
1509 }
1510
1511 static int
1512 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1513 {
1514    Ecore_X_Selection_Data_Targets *targets;
1515    Ecore_X_Atom *atomlist;
1516    Evas_Object *top;
1517    int i,j;
1518
1519    targets = notify->data;
1520    atomlist = (Ecore_X_Atom *)(targets->data.data);
1521
1522    /* Start from 1: Skip targets */
1523    for (j = 1 ; j < CNP_N_ATOMS ; j ++)
1524      {
1525         if (!(atoms[j].formats & sel->requestformat)) continue;
1526         for (i = 0 ; i < targets->data.length ; i ++)
1527           {
1528              if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
1529                {
1530                   /* Found a match: Use it */
1531                   goto found;
1532                }
1533           }
1534      }
1535 found:
1536    if (j == CNP_N_ATOMS)
1537      {
1538         cnp_debug("No matching type found\n");
1539         return 0;
1540      }
1541
1542    top = elm_widget_top_get(sel->requestwidget);
1543    if (!top) return 0;
1544
1545    sel->request(elm_win_xwindow_get(top), atoms[j].name);
1546    return 0;
1547 }
1548
1549 static void
1550 entry_insert_filter(Evas_Object* entry, char* str)
1551 {
1552    if (!entry || !str)
1553      return;
1554
1555    char *insertStr = str;
1556    // if entry has text only set then remove item tags
1557    if (elm_entry_cnp_mode_get(entry) != ELM_CNP_MODE_MARKUP)
1558      {
1559         while (EINA_TRUE)
1560           {
1561              char *startTag = NULL;
1562              char *endTag = NULL;
1563
1564              startTag = strstr(insertStr, "<item");
1565              if (!startTag)
1566                startTag = strstr(insertStr, "</item");
1567              if (startTag)
1568                endTag = strstr(startTag, ">");
1569              else
1570                break;
1571              if (!endTag || startTag > endTag)
1572                {
1573                   cnp_debug("Broken tag: %s\n", str);
1574                   break;
1575                }
1576
1577              size_t sindex = startTag - insertStr;
1578              size_t eindex = endTag - insertStr + 1;
1579
1580              Eina_Strbuf *buf = eina_strbuf_new();
1581              if (buf)
1582                {
1583                   eina_strbuf_append(buf, insertStr);
1584                   eina_strbuf_remove(buf, sindex, eindex);
1585                   insertStr = eina_strbuf_string_steal(buf);
1586                   eina_strbuf_free(buf);
1587                }
1588           }
1589      }
1590    cnp_debug("remove item tag: %s\n", insertStr);
1591
1592    // if entry has single line set then remove <br> & <ps> tags
1593    if (elm_entry_single_line_get(entry))
1594      {
1595         Eina_Strbuf *buf = eina_strbuf_new();
1596         if (buf)
1597           {
1598              eina_strbuf_append(buf, insertStr);
1599              eina_strbuf_replace_all(buf, "<br>", "");
1600              eina_strbuf_replace_all(buf, "<ps>", "");
1601              insertStr = eina_strbuf_string_steal(buf);
1602              eina_strbuf_free(buf);
1603           }
1604      }
1605    cnp_debug("remove break tag: %s\n", insertStr);
1606
1607    elm_entry_entry_insert(entry, insertStr);
1608
1609    if (insertStr != str)
1610      free(insertStr);
1611 }
1612
1613 static int
1614 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1615 {
1616    Ecore_X_Selection_Data *data;
1617    char *str;
1618
1619    data = notify->data;
1620
1621    if (sel->datacb)
1622      {
1623         Elm_Selection_Data ddata;
1624
1625         str = malloc(data->length);
1626         memcpy(str, data->data, data->length);
1627         ddata.x = ddata.y = 0;
1628         ddata.format = ELM_SEL_FORMAT_TEXT;
1629         ddata.data = str;
1630         ddata.len = data->length;
1631         sel->datacb(sel->udata, sel->widget, &ddata);
1632         free(str);
1633         return 0;
1634      }
1635
1636    cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
1637    str = mark_up((char *)data->data, data->length, NULL);
1638    cnp_debug("String is %s (from %s)\n", str, data->data);
1639    entry_insert_filter(sel->requestwidget, str);
1640    //elm_entry_entry_insert(sel->requestwidget, str);
1641    free(str);
1642    return 0;
1643 }
1644
1645
1646 /**
1647  * So someone is pasting an image into my entry or widget...
1648  */
1649 static int
1650 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1651 {
1652    Ecore_X_Selection_Data *data;
1653    Ecore_X_Selection_Data_Files *files;
1654    Paste_Image *pi;
1655    char *p, *stripstr;
1656
1657    data = notify->data;
1658    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1659    if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1660      {
1661         cnp_debug("got a files list\n");
1662         files = notify->data;
1663         if (files->num_files > 1)
1664           {
1665              /* Don't handle many items */
1666              cnp_debug("more then one file: Bailing\n");
1667              return 0;
1668           }
1669         stripstr = p = strdup(files->files[0]);
1670      }
1671    else
1672      {
1673         stripstr = p = strndup((char *)data->data, data->length);
1674      }
1675
1676    if (!p)
1677      {
1678         cnp_debug("Couldn't find a file\n");
1679         return 0;
1680      }
1681    cnp_debug("Got %s\n",p);
1682    if (sel->datacb)
1683      {
1684         Elm_Selection_Data ddata;
1685
1686         ddata.x = ddata.y = 0;
1687         ddata.format = ELM_SEL_FORMAT_MARKUP;
1688         ddata.data = p;
1689         ddata.len = data->length;
1690         sel->datacb(sel->udata, sel->widget, &ddata);
1691         free(p);
1692         return 0;
1693      }
1694    if (strncmp(p, "file://", 7))
1695      {
1696         /* Try and continue if it looks sane */
1697         if (*p != '/')
1698           {
1699              free(p);
1700              return 0;
1701           }
1702      }
1703    else
1704      {
1705         p += strlen("file://");
1706      }
1707
1708    if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1709    pi = pasteimage_alloc(p, strlen(p));
1710    if (savedtypes.textreq)
1711      {
1712         savedtypes.textreq = 0;
1713         savedtypes.pi = pi;
1714      }
1715    else
1716      {
1717         pasteimage_append(pi, sel->requestwidget);
1718         savedtypes.pi = NULL;
1719      }
1720    free(stripstr);
1721    return 0;
1722 }
1723
1724 /**
1725  * Just receieved an vcard, either through cut and paste, or dnd.
1726  */
1727 static int
1728 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1729 {
1730    Dropable *dropable;
1731    Eina_List *l;
1732    Ecore_X_Selection_Data *data;
1733
1734    data = notify->data;
1735    cnp_debug("vcard receive\n");
1736
1737    if (sel == (selections + ELM_SEL_XDND))
1738      {
1739         Elm_Selection_Data ddata;
1740
1741         cnp_debug("drag & drop\n");
1742         /* FIXME: this needs to be generic: Used for all receives */
1743         EINA_LIST_FOREACH(drops, l, dropable)
1744           {
1745              if (dropable->obj == sel->requestwidget) break;
1746           }
1747         if (!dropable)
1748           {
1749              cnp_debug("Unable to find drop object");
1750              ecore_x_dnd_send_finished();
1751              return 0;
1752           }
1753         dropable = eina_list_data_get(l);
1754         ddata.x = savedtypes.x;
1755         ddata.y = savedtypes.y;
1756         ddata.format = ELM_SEL_FORMAT_VCARD;
1757         ddata.data = data->data;
1758         ddata.len = data->length;
1759         dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1760         ecore_x_dnd_send_finished();
1761      }
1762    else if (sel->datacb)
1763      {
1764         Elm_Selection_Data ddata;
1765         ddata.x = ddata.y = 0;
1766         ddata.format = ELM_SEL_FORMAT_IMAGE;
1767         ddata.data = data->data;
1768         ddata.len = data->length;
1769         sel->datacb(sel->udata, sel->widget, &ddata);
1770      }
1771    else
1772      {
1773         cnp_debug("Paste request\n");
1774      }
1775
1776    return 0;
1777
1778 }
1779
1780
1781 static int
1782 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1783 {
1784    Ecore_X_Selection_Data *data;
1785    Tmp_Info *tmp;
1786    Paste_Image *pi;
1787
1788    cnp_debug("got a png (or a jpeg)!\n");
1789    data = notify->data;
1790
1791    cnp_debug("Size if %d\n", data->length);
1792
1793    if (sel->datacb)
1794      {
1795         Elm_Selection_Data ddata;
1796
1797         ddata.x = ddata.y = 0;
1798         ddata.format = ELM_SEL_FORMAT_IMAGE;
1799         ddata.data = data->data;
1800         ddata.len = data->length;
1801         sel->datacb(sel->udata, sel->widget, &ddata);
1802         return 0;
1803      }
1804
1805    /* generate tmp name */
1806    tmp = elm_cnp_tempfile_create(data->length);
1807    memcpy(tmp->map, data->data, data->length);
1808    munmap(tmp->map,data->length);
1809
1810    /* FIXME: Add to paste image data to clean up */
1811    pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1812    pasteimage_append(pi, sel->requestwidget);
1813
1814    tmpinfo_free(tmp);
1815    return 0;
1816 }
1817
1818 static int
1819 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1820 {
1821    Ecore_X_Selection_Data *data;
1822
1823    data = notify->data;
1824
1825    char *stripstr = NULL;
1826    stripstr = malloc(sizeof(char) * (data->length + 1));
1827    strncpy(stripstr, (char *)data->data, data->length);
1828    stripstr[data->length] = '\0';
1829
1830    if (sel->datacb)
1831      {
1832         Elm_Selection_Data ddata;
1833         ddata.x = ddata.y = 0;
1834         ddata.format = ELM_SEL_FORMAT_MARKUP;
1835         ddata.data = stripstr;
1836         ddata.len = data->length;
1837         sel->datacb(sel->udata, sel->widget, &ddata);
1838      }
1839    else
1840      entry_insert_filter(sel->requestwidget, stripstr);
1841      //elm_entry_entry_insert(sel->requestwidget, stripstr);
1842
1843    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1844    free(stripstr);
1845    return 0;
1846 }
1847
1848 /**
1849  *    Warning: Generic text/html can';t handle it sanely.
1850  *    Firefox sends ucs2 (i think).
1851  *       chrome sends utf8... blerg
1852  */
1853 static int
1854 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1855 {
1856    Ecore_X_Selection_Data *data;
1857
1858    cnp_debug("Got some HTML: Checking encoding is useful\n");
1859    data = notify->data;
1860
1861    char *stripstr = NULL;
1862    stripstr = malloc(sizeof(char) * (data->length + 1));
1863    strncpy(stripstr, (char *)data->data, data->length);
1864    stripstr[data->length] = '\0';
1865
1866    if (sel->datacb)
1867      {
1868         Elm_Selection_Data ddata;
1869         ddata.x = ddata.y = 0;
1870         ddata.format = ELM_SEL_FORMAT_HTML;
1871         ddata.data = stripstr;
1872         ddata.len = data->length;
1873         sel->datacb(sel->udata, sel->widget, &ddata);
1874      }
1875    else
1876      entry_insert_filter(sel->requestwidget, stripstr);
1877      //elm_entry_entry_insert(sel->requestwidget, stripstr);
1878
1879    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1880    free(stripstr);
1881    return 0;
1882 }
1883
1884
1885 static Eina_Bool
1886 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1887 {
1888    Cnp_Selection *sel;
1889
1890    cnp_debug("text converter\n");
1891    sel = selections + *((int *)data);
1892    if (!sel->active) return EINA_TRUE;
1893
1894    if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1895        (sel->format & ELM_SEL_FORMAT_HTML))
1896      {
1897         *data_ret = remove_tags(sel->selbuf, size_ret);
1898      }
1899    else if (sel->format & ELM_SEL_FORMAT_TEXT)
1900      {
1901         *data_ret = strdup(sel->selbuf);
1902         *size_ret = strlen(sel->selbuf);
1903      }
1904    else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1905      {
1906         cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1907         cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1908         evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1909         if (!*data_ret) *data_ret = strdup("No file");
1910         else *data_ret = strdup(*data_ret);
1911         *size_ret = strlen(*data_ret);
1912      }
1913    return EINA_TRUE;
1914 }
1915
1916 static Eina_Bool
1917 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1918 {
1919    Cnp_Selection *sel;
1920    sel = selections + *((int *)data);
1921 /*   if (data_ret) *data_ret = strdup(sel->selbuf);
1922    if (size_ret) *size_ret = strlen(sel->selbuf);*/
1923    char *edje = NULL;
1924
1925    if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
1926      {
1927         if (sel->selbuf && sel->selbuf[0] != '\0')
1928           edje = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
1929      }
1930    else if (data_ret && ((sel->format & ELM_SEL_FORMAT_HTML)))
1931      {
1932         Eina_List *nodeList = NULL;
1933         Eina_List *trail;
1934         PTagNode nodeData;
1935
1936         nodeData = _get_start_node(sel->selbuf);
1937
1938         while (nodeData)
1939           {
1940              nodeList = eina_list_append(nodeList, nodeData);
1941              nodeData = _get_next_node(nodeData);
1942           }
1943
1944         _link_match_tags(nodeList);
1945
1946         _set_HTML_tag_data(nodeList);
1947
1948 #ifdef DEBUGON
1949         _dumpNode(nodeList);
1950 #endif
1951         edje = _convert_to_edje(nodeList);
1952
1953         cnp_debug("convert edje: %s\n", edje);
1954
1955         EINA_LIST_FOREACH(nodeList, trail, nodeData)
1956            _delete_node(nodeData);
1957         eina_list_free(nodeList);
1958
1959      }
1960    if (data_ret)
1961      {
1962         if (edje)
1963           *data_ret = edje;
1964         else
1965            if (sel->selbuf)
1966              *data_ret = strdup(sel->selbuf);
1967            else
1968              *data_ret = strdup("");
1969      }
1970    if (size_ret)
1971      {
1972         if (edje)
1973           *size_ret = strlen(edje);
1974         else
1975           if (sel->selbuf)
1976             *size_ret = strlen(sel->selbuf);
1977           else
1978             *size_ret = 0;
1979      }
1980
1981    return EINA_TRUE;
1982 }
1983
1984 static Eina_Bool
1985 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1986 {
1987    Cnp_Selection *sel;
1988
1989    sel = selections + *(int *)data;
1990
1991    char *html = NULL;
1992    char *convert_target = sel->selbuf;
1993    Eina_Bool convert_edje = EINA_FALSE;
1994
1995    if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
1996      {
1997         if (sel->selbuf && sel->selbuf[0] != '\0')
1998           {
1999              convert_target = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
2000              convert_edje = EINA_TRUE;
2001           }
2002      }
2003
2004    if (data_ret && ((sel->format & ELM_SEL_FORMAT_MARKUP) || convert_edje))
2005      {
2006         Eina_List *nodeList = NULL;
2007         Eina_List *trail;
2008         PTagNode nodeData;
2009
2010         nodeData = _get_start_node(convert_target);
2011
2012         while (nodeData)
2013           {
2014              nodeList = eina_list_append(nodeList, nodeData);
2015              nodeData = _get_next_node(nodeData);
2016           }
2017
2018         _link_match_tags(nodeList);
2019
2020         _set_EFL_tag_data(nodeList);
2021
2022 #ifdef DEBUGON
2023         _dumpNode(nodeList);
2024 #endif
2025         html = _convert_to_html(nodeList);
2026
2027         cnp_debug("convert html: %s\n", html);
2028
2029         EINA_LIST_FOREACH(nodeList, trail, nodeData)
2030            _delete_node(nodeData);
2031         eina_list_free(nodeList);
2032      }
2033    if (data_ret)
2034      {
2035         if (html)
2036           *data_ret = html;
2037         else
2038           if (sel->selbuf)
2039             *data_ret = strdup(sel->selbuf);
2040           else
2041             *data_ret = strdup("");
2042      }
2043
2044    if (size_ret)
2045      {
2046         if (html)
2047           *size_ret = strlen(html);
2048         else
2049           if (sel->selbuf)
2050             *size_ret = strlen(sel->selbuf);
2051           else
2052             *size_ret = 0;
2053      }
2054    if (convert_target != sel->selbuf)
2055      free(convert_target);
2056
2057    return EINA_TRUE;
2058 }
2059
2060 static Eina_Bool
2061 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
2062 {
2063    Cnp_Selection *sel;
2064    sel = selections + *((int *)data);
2065    cnp_debug("Uri converter\n");
2066    if (data_ret) *data_ret = strdup(sel->selbuf);
2067    if (size_ret) *size_ret = strlen(sel->selbuf);
2068    return EINA_TRUE;
2069 }
2070
2071 /*
2072  * Image paste provide
2073  */
2074
2075 /* FIXME: Should add provider for each pasted item: Use data to store it
2076  * much easier */
2077 static Evas_Object *
2078 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
2079 {
2080    Paste_Image *pi;
2081    Eina_List *l;
2082
2083    cnp_debug("image provider for %s called\n", item);
2084    EINA_LIST_FOREACH(pastedimages, l, pi)
2085      {
2086         cnp_debug("is it %s?\n",pi->tag);
2087         if (!strcmp(pi->tag, item))
2088           {
2089              /* Found it */
2090              Evas_Object *o;
2091              o = evas_object_image_filled_add(evas_object_evas_get(entry));
2092              /* FIXME: Handle eets */
2093              cnp_debug("file is %s (object is %p)\n", pi->file, o);
2094              evas_object_image_file_set(o, pi->file, NULL);
2095              evas_object_show(o);
2096              return o;
2097           }
2098      }
2099    return NULL;
2100 }
2101
2102
2103 static Paste_Image *
2104 pasteimage_alloc(const char *file, int pathlen)
2105 {
2106    Paste_Image *pi;
2107    int len;
2108    char *buf, *filebuf;
2109    int prefixlen = strlen("file://");
2110
2111    pi = calloc(1, sizeof(Paste_Image));
2112    if (!pi) return NULL;
2113
2114    len = snprintf(NULL, 0, "pasteimage-%p", pi);
2115    len++;
2116    buf = malloc(len);
2117    if (!buf)
2118      {
2119         free(pi);
2120         return NULL;
2121      }
2122    snprintf(buf, len, "pasteimage-%p", pi);
2123    pi->tag = buf;
2124
2125    if (file)
2126      {
2127         if (strstr(file,"file://")) file += prefixlen;
2128         filebuf = alloca(pathlen + 1);
2129         strncpy(filebuf, file, pathlen);
2130         filebuf[pathlen] = 0;
2131         pi->file = strdup(filebuf);
2132      }
2133
2134    return pi;
2135 }
2136
2137 static void
2138 pasteimage_free(Paste_Image *pi)
2139 {
2140    if (!pi) return;
2141    if (pi->file) free((void*)pi->file);
2142    if (pi->tag) free((void*)pi->tag);
2143    free(pi);
2144 }
2145
2146 static Eina_Bool
2147 pasteimage_provider_set(Evas_Object *entry)
2148 {
2149    void *v;
2150    const char *type;
2151
2152    if (!entry) return EINA_FALSE;
2153    type = elm_widget_type_get(entry);
2154    cnp_debug("type is %s\n", type);
2155    if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
2156
2157    v = evas_object_data_get(entry, PROVIDER_SET);
2158    if (!v)
2159      {
2160         evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
2161         elm_entry_item_provider_append(entry, image_provider, NULL);
2162         evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
2163                                        entry_deleted, NULL);
2164      }
2165    return EINA_TRUE;
2166 }
2167
2168
2169 static Eina_Bool
2170 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
2171 {
2172    char *entrytag;
2173    int len;
2174    static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
2175
2176    if (!pi) return EINA_FALSE;
2177    if (!entry) return EINA_FALSE;
2178    if (elm_entry_cnp_mode_get(entry) != ELM_CNP_MODE_MARKUP) return EINA_FALSE;
2179
2180    pasteimage_provider_set(entry);
2181
2182    len = strlen(tagstring)+strlen(pi->file);
2183
2184    pastedimages = eina_list_append(pastedimages, pi);
2185    entrytag = alloca(len + 1);
2186    snprintf(entrytag, len + 1, tagstring, pi->file);
2187    elm_entry_entry_insert(entry, entrytag);
2188
2189    return EINA_TRUE;
2190 }
2191
2192 static void
2193 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
2194 {
2195    Paste_Image *pi;
2196    Eina_List *l,*next;
2197
2198    EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
2199      {
2200         if (pi->entry == entry)
2201           pastedimages = eina_list_remove_list(pastedimages, l);
2202      }
2203 }
2204
2205
2206 static char *
2207 remove_tags(const char *p, int *len)
2208 {
2209    char *q,*ret;
2210    int i;
2211    if (!p) return NULL;
2212
2213    q = malloc(strlen(p) + 1);
2214    if (!q) return NULL;
2215    ret = q;
2216
2217    while (*p)
2218      {
2219         if ((*p != '<') && (*p != '&')) *q++ = *p++;
2220         else if (*p == '<')
2221           {
2222              if ((p[1] == 'b') && (p[2] == 'r') &&
2223                  ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
2224                *q++ = '\n';
2225              while ((*p) && (*p != '>')) p++;
2226              p++;
2227           }
2228         else if (*p == '&')
2229           {
2230              p++;
2231              for (i = 0 ; i < N_ESCAPES ; i++)
2232                {
2233                   if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
2234                     {
2235                        p += strlen(escapes[i].escape);
2236                        *q = escapes[i].value;
2237                        q++;
2238                        break;
2239                     }
2240                }
2241              if (i == N_ESCAPES) *q ++= '&';
2242           }
2243      }
2244    *q = 0;
2245    if (len) *len = q - ret;
2246    return ret;
2247 }
2248
2249 /* Mark up */
2250 static char *
2251 mark_up(const char *start, int inlen, int *lenp)
2252 {
2253    int l, i;
2254    const char *p;
2255    char *q, *ret;
2256    const char *endp = NULL;
2257
2258    if (!start) return NULL;
2259    if (inlen >= 0) endp = start + inlen;
2260    /* First pass: Count characters */
2261    for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
2262      {
2263         for (i = 0 ; i < N_ESCAPES ; i ++)
2264           {
2265              if (*p == escapes[i].value)
2266                {
2267                   if (!iscntrl(escapes[i].value)) l++;
2268                   l += strlen(escapes[i].escape);
2269                   break;
2270                }
2271           }
2272         if (i == N_ESCAPES) l++;
2273      }
2274
2275    q = ret = malloc(l + 1);
2276
2277    /* Second pass: Change characters */
2278    for (p = start; ((!endp) || (p < endp)) && (*p); )
2279      {
2280         for (i = 0; i < N_ESCAPES; i++)
2281           {
2282              if (*p == escapes[i].value)
2283                {
2284                   if (!iscntrl(escapes[i].value)) *q++ = '&';
2285                   strcpy(q, escapes[i].escape);
2286                   q += strlen(escapes[i].escape);
2287                   p ++;
2288                   break;
2289                }
2290           }
2291         if (i == N_ESCAPES) *q++ = *p++;
2292      }
2293    *q = 0;
2294
2295    if (lenp) *lenp = l;
2296    return ret;
2297 }
2298
2299
2300 static Eina_Bool
2301 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2302 {
2303    Ecore_X_Event_Xdnd_Enter *enter = ev;
2304    int i;
2305
2306    /* Skip it */
2307    if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
2308
2309    cnp_debug("Types\n");
2310    savedtypes.ntypes = enter->num_types;
2311    if (savedtypes.types) free(savedtypes.types);
2312    savedtypes.types = malloc(sizeof(char *) * enter->num_types);
2313    if (!savedtypes.types) return EINA_FALSE;
2314
2315    for (i = 0; i < enter->num_types; i++)
2316      {
2317         savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
2318         cnp_debug("Type is %s %p %p\n", enter->types[i],
2319                   savedtypes.types[i],text_uri);
2320         if (savedtypes.types[i] == text_uri)
2321           {
2322              /* Request it, so we know what it is */
2323              cnp_debug("Sending uri request\n");
2324              savedtypes.textreq = 1;
2325              if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2326              savedtypes.pi = NULL; /* FIXME: Free? */
2327              ecore_x_selection_xdnd_request(enter->win, text_uri);
2328           }
2329      }
2330
2331    /* FIXME: Find an object and make it current */
2332    return EINA_TRUE;
2333 }
2334
2335 static Eina_Bool
2336 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2337 {
2338    struct _Ecore_X_Event_Xdnd_Drop *drop;
2339    Dropable *dropable;
2340    Eina_List *l;
2341    Ecore_Evas *ee;
2342    Ecore_X_Window xwin;
2343    Elm_Selection_Data ddata;
2344    int x, y, w, h;
2345    int i, j;
2346
2347    drop = ev;
2348
2349    // check we still have something to drop
2350    if (!drops) return EINA_TRUE;
2351
2352    /* Find any widget in our window; then work out geometry rel to our window */
2353    for (l = drops; l; l = l->next)
2354      {
2355         dropable = l->data;
2356         xwin = (Ecore_X_Window)ecore_evas_window_get
2357            (ecore_evas_ecore_evas_get(evas_object_evas_get
2358                                       (dropable->obj)));
2359         if (xwin == drop->win) break;
2360      }
2361    /* didn't find a window */
2362    if (!l) return EINA_TRUE;
2363
2364    /* Calculate real (widget relative) position */
2365    // - window position
2366    // - widget position
2367    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
2368    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2369    savedtypes.x = drop->position.x - x;
2370    savedtypes.y = drop->position.y - y;
2371
2372    cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
2373
2374    for (; l; l = l->next)
2375      {
2376         dropable = l->data;
2377         evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
2378         if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
2379             (savedtypes.x < x + w) && (savedtypes.y < y + h))
2380           break; /* found! */
2381      }
2382
2383    if (!l) return EINA_TRUE; /* didn't find one */
2384
2385    evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
2386    savedtypes.x -= x;
2387    savedtypes.y -= y;
2388
2389    /* Find our type from the previous list */
2390    for (i = 0; i < CNP_N_ATOMS; i++)
2391      {
2392         for (j = 0; j < savedtypes.ntypes; j++)
2393           {
2394              if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
2395           }
2396      }
2397
2398    cnp_debug("Didn't find a target\n");
2399    return EINA_TRUE;
2400
2401 found:
2402    cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
2403    cnp_debug("0x%x\n",xwin);
2404
2405    if (i == CNP_ATOM_text_urilist)
2406      {
2407         cnp_debug("We found a URI... (%scached) %s\n",
2408                   savedtypes.pi ? "" : "not ",
2409                   savedtypes.pi->file);
2410         if (savedtypes.pi)
2411           {
2412              char *entrytag;
2413              static const char *tagstring = "<item absize=240x180 href="
2414                 "file://%s></item>";
2415              ddata.x = savedtypes.x;
2416              ddata.y = savedtypes.y;
2417
2418              /* If it's markup that also supports images */
2419              if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
2420                  (dropable->types & ELM_SEL_FORMAT_IMAGE))
2421                {
2422                   int len;
2423                   ddata.format = ELM_SEL_FORMAT_MARKUP;
2424                   pasteimage_provider_set(dropable->obj);
2425
2426                   pastedimages = eina_list_append(pastedimages, savedtypes.pi);
2427                   len = strlen(tagstring) + strlen(savedtypes.pi->file);
2428                   entrytag = alloca(len + 1);
2429                   snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
2430                   ddata.data = entrytag;
2431                   cnp_debug("Insert %s\n", (char *)ddata.data);
2432                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2433                   ecore_x_dnd_send_finished();
2434
2435                   if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2436                   savedtypes.pi = NULL;
2437                   return EINA_TRUE;
2438                }
2439              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
2440                {
2441                   cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
2442                   ddata.format = ELM_SEL_FORMAT_IMAGE;
2443                   ddata.data = (char *)savedtypes.pi->file;
2444                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2445                   ecore_x_dnd_send_finished();
2446
2447                   if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2448                   savedtypes.pi = NULL;
2449
2450                   return EINA_TRUE;
2451                }
2452              else
2453                {
2454                   cnp_debug("Item doesn't support images... passing\n");
2455                   pasteimage_free(savedtypes.pi);
2456                   return EINA_TRUE;
2457                }
2458           }
2459         else if (savedtypes.textreq)
2460           {
2461              /* Already asked: Pretend we asked now, and paste immediately when
2462               * it comes in */
2463              savedtypes.textreq = 0;
2464              ecore_x_dnd_send_finished();
2465              return EINA_TRUE;
2466           }
2467      }
2468
2469    cnp_debug("doing a request then\n");
2470    selections[ELM_SEL_XDND].requestwidget = dropable->obj;
2471    selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
2472    selections[ELM_SEL_XDND].active = EINA_TRUE;
2473
2474    ecore_x_selection_xdnd_request(xwin, atoms[i].name);
2475
2476    return EINA_TRUE;
2477 }
2478 static Eina_Bool
2479 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2480 {
2481    struct _Ecore_X_Event_Xdnd_Position *pos;
2482    Ecore_X_Rectangle rect;
2483
2484    pos = ev;
2485
2486    /* Need to send a status back */
2487    /* FIXME: Should check I can drop here */
2488    /* FIXME: Should highlight widget */
2489    rect.x = pos->position.x - 5;
2490    rect.y = pos->position.y - 5;
2491    rect.width = 10;
2492    rect.height = 10;
2493    ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
2494
2495    return EINA_TRUE;
2496 }
2497
2498 /**
2499  * When dragging this is callback response from the destination.
2500  * The important thing we care about: Can we drop; thus update cursor
2501  * appropriately.
2502  */
2503 static Eina_Bool
2504 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2505 {
2506    struct _Ecore_X_Event_Xdnd_Status *status = ev;
2507
2508    if (!status) return EINA_TRUE;
2509
2510    /* Only thing we care about: will accept */
2511    if (status->will_accept)
2512      {
2513         cnp_debug("Will accept\n");
2514      }
2515    else
2516      { /* Won't accept */
2517         cnp_debug("Won't accept accept\n");
2518      }
2519    return EINA_TRUE;
2520 }
2521
2522 /**
2523  * Add a widget as drop target.
2524  */
2525 Eina_Bool
2526 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2527 {
2528    Dropable *drop;
2529    Ecore_X_Window xwin;
2530    Eina_List *item;
2531    int first;
2532
2533    if (!obj) return EINA_FALSE;
2534    if (!_elm_cnp_init_count) _elm_cnp_init();
2535
2536    /* Is this the first? */
2537    first = (!drops) ? 1 : 0;
2538
2539    EINA_LIST_FOREACH(drops, item, drop)
2540      {
2541         if (drop->obj == obj)
2542           {
2543              /* Update: Not a new one */
2544              drop->dropcb = dropcb;
2545              drop->cbdata = cbdata;
2546              drop->types = format;
2547              return EINA_TRUE;
2548           }
2549      }
2550
2551    /* Create new drop */
2552    drop = calloc(1, sizeof(Dropable));
2553    if (!drop) return EINA_FALSE;
2554    /* FIXME: Check for eina's deranged error method */
2555    drops = eina_list_append(drops, drop);
2556
2557    if (!drops/* || or other error */)
2558      {
2559         free(drop);
2560         return EINA_FALSE;
2561      }
2562    drop->dropcb = dropcb;
2563    drop->cbdata = cbdata;
2564    drop->types = format;
2565    drop->obj = obj;
2566
2567    evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
2568                                   /* I love C and varargs */
2569                                   (Evas_Object_Event_Cb)elm_drop_target_del,
2570                                   obj);
2571    /* FIXME: Handle resizes */
2572
2573    /* If not the first: We're done */
2574    if (!first) return EINA_TRUE;
2575
2576    xwin = (Ecore_X_Window)ecore_evas_window_get
2577       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2578
2579    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
2580
2581    cnp_debug("Adding drop target calls\n");
2582    handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
2583                                            _dnd_enter, NULL);
2584    handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
2585                                          _dnd_position, NULL);
2586    handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
2587                                           _dnd_drop, NULL);
2588
2589    return EINA_TRUE;
2590 }
2591
2592 Eina_Bool
2593 elm_drop_target_del(Evas_Object *obj)
2594 {
2595    Dropable *drop,*del;
2596    Eina_List *item;
2597    Ecore_X_Window xwin;
2598
2599    del = NULL;
2600    EINA_LIST_FOREACH(drops, item, drop)
2601      {
2602         if (drop->obj == obj)
2603           {
2604              drops = eina_list_remove_list(drops, item);
2605              del = drop;
2606              break;
2607           }
2608      }
2609    if (!del) return EINA_FALSE;
2610
2611    evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
2612                                   (Evas_Object_Event_Cb)elm_drop_target_del);
2613    free(drop);
2614    /* If still drops there: All fine.. continue */
2615    if (drops) return EINA_TRUE;
2616
2617    cnp_debug("Disabling DND\n");
2618    xwin = (Ecore_X_Window)ecore_evas_window_get
2619       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2620    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
2621
2622    ecore_event_handler_del(handler_pos);
2623    ecore_event_handler_del(handler_drop);
2624    ecore_event_handler_del(handler_enter);
2625
2626    if (savedtypes.pi)
2627      {
2628         pasteimage_free(savedtypes.pi);
2629         savedtypes.pi = NULL;
2630      }
2631
2632    return EINA_TRUE;
2633 }
2634
2635
2636 static void
2637 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
2638 {
2639    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
2640    ecore_x_dnd_drop();
2641    if (dragdonecb)
2642      {
2643         dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
2644         dragdonecb = NULL;
2645      }
2646    if (dragwin)
2647      {
2648         evas_object_del(dragwin);
2649         dragwin = NULL;
2650      }
2651 }
2652
2653 static void
2654 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
2655 {
2656    evas_object_move(dragwin,
2657                     pos->position.x - _dragx,
2658                     pos->position.y - _dragy);
2659 }
2660
2661
2662 Eina_Bool
2663 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
2664 {
2665    Ecore_X_Window xwin;
2666    Cnp_Selection *sel;
2667    Elm_Sel_Type xdnd = ELM_SEL_XDND;
2668    Ecore_Evas *ee;
2669    int x, y, x2, y2, x3, y3;
2670    Evas_Object *icon;
2671    int w, h;
2672
2673    if (!_elm_cnp_init_count) _elm_cnp_init();
2674
2675    xwin = elm_win_xwindow_get(obj);
2676
2677    cnp_debug("starting drag...\n");
2678
2679    ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
2680    sel = selections + ELM_SEL_XDND;
2681    sel->active = 1;
2682    sel->widget = obj;
2683    sel->format = format;
2684    sel->selbuf = data ? strdup(data) : NULL;
2685    dragdonecb = dragdone;
2686    dragdonedata = donecbdata;
2687
2688    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
2689    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
2690    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
2691                                   _drag_mouse_up, NULL);
2692
2693    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
2694                                             _dnd_status, NULL);
2695
2696    dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
2697    elm_win_override_set(dragwin, 1);
2698
2699    /* FIXME: Images only */
2700    icon = elm_icon_add(dragwin);
2701    elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
2702    elm_win_resize_object_add(dragwin,icon);
2703    evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2704    evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
2705
2706    /* Position subwindow appropriately */
2707    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
2708    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2709    evas_object_geometry_get(obj, &x2, &y2, &w, &h);
2710    x += x2;
2711    y += y2;
2712    evas_object_move(dragwin, x, y);
2713    evas_object_resize(icon, w, h);
2714    evas_object_resize(dragwin, w, h);
2715
2716    evas_object_show(icon);
2717    evas_object_show(dragwin);
2718
2719    evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
2720    _dragx = x3 - x2;
2721    _dragy = y3 - y2;
2722
2723    return EINA_TRUE;
2724 }
2725
2726 static Tmp_Info *
2727 elm_cnp_tempfile_create(int size)
2728 {
2729    Tmp_Info *info;
2730    const char *tmppath;
2731    int len;
2732
2733    info = malloc(sizeof(Tmp_Info));
2734    if (!info) return NULL;
2735
2736    tmppath = getenv("TMP");
2737    if (!tmppath) tmppath = P_tmpdir;
2738    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
2739    if (len < 0)
2740      {
2741         free(info);
2742         return NULL;
2743      }
2744    len++;
2745    info->filename = malloc(len);
2746    if (!info->filename)
2747      {
2748         free(info);
2749         return NULL;
2750      }
2751    snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
2752
2753    info->fd = mkstemp(info->filename);
2754
2755 # ifdef __linux__
2756      {
2757         char *tmp;
2758         /* And before someone says anything see POSIX 1003.1-2008 page 400 */
2759         long pid;
2760
2761         pid = (long)getpid();
2762         /* Use pid instead of /proc/self: That way if can be passed around */
2763         len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
2764         len++;
2765         tmp = malloc(len);
2766         if (tmp)
2767           {
2768              snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
2769              unlink(info->filename);
2770              free(info->filename);
2771              info->filename = tmp;
2772           }
2773      }
2774 # endif
2775
2776    cnp_debug("filename is %s\n", info->filename);
2777    if (size < 1)
2778      {
2779         /* Set map to NULL and return */
2780         info->map = NULL;
2781         info->len = 0;
2782         return info;
2783      }
2784
2785    /* Map it in */
2786    if (ftruncate(info->fd, size))
2787      {
2788         perror("ftruncate");
2789         info->map = NULL;
2790         info->len = 0;
2791         return info;
2792      }
2793
2794    eina_mmap_safety_enabled_set(EINA_TRUE);
2795
2796    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
2797    if (info->map == MAP_FAILED)
2798      {
2799         perror("mmap");
2800         info->map = NULL;
2801         info->len = 0;
2802      }
2803
2804    return info;
2805 }
2806
2807
2808 static int
2809 tmpinfo_free(Tmp_Info *info)
2810 {
2811    if (!info) return 0;
2812    free(info->filename);
2813    free(info);
2814    return 0;
2815 }
2816
2817 #else
2818 /* Stubs for windows */
2819 Eina_Bool
2820 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
2821 {
2822    return EINA_FALSE;
2823 }
2824
2825 Eina_Bool
2826 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2827 {
2828    return EINA_FALSE;
2829 }
2830
2831 Eina_Bool
2832 elm_drop_target_del(Evas_Object *o)
2833 {
2834    return EINA_TRUE;
2835 }
2836 #endif
2837
2838 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/