Merge branch 'master' of 165.213.180.234:/slp/pkgs/e/elementary
[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://", 7);
827              modify[7] = '\0';
828              path += 7;
829              while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
830                {
831                   path++;
832                }
833              strcat(modify, path);
834              data->href = modify;
835              cnp_debug("image href ---%s---\n", data->href);
836              free(value);
837           }
838         else
839           freeAndAssign(data->href, value);
840      }
841
842    value = _get_tag_value(tag_str, "absize");
843    if (value)
844      {
845         char *xpos = strchr(value, 'x');
846         if (xpos)
847           {
848              int absizeLen = strlen(value);
849              freeAndAssign(data->width, strndup(value, xpos - value));
850              freeAndAssign(data->height, strndup(xpos + 1, absizeLen - (xpos - value) - 1));
851              cnp_debug("image width: -%s-, height: -%s-\n", data->width, data->height);
852           }
853         free(value);
854      }
855    return data;
856 }
857
858 static void
859 _set_EFL_tag_data(Eina_List* nodes)
860 {
861    PTagNode trail;
862    Eina_List *l;
863
864    EINA_LIST_FOREACH(nodes, l, trail)
865      {
866         if (!trail->tag)
867           continue;
868         if (!strcmp("font", trail->tag))
869              trail->tagData = _set_EFL_font_data(trail->tagData, trail->tag_str);
870         else if (!strcmp("item", trail->tag))
871              trail->tagData = _set_EFL_item_data(trail->tagData, trail->tag_str);
872      }
873 }
874
875 static PFontTagData
876 _set_HTML_font_data(PFontTagData data, const char *tag_str)
877 {
878    char *value;
879
880    if (!data)
881      data = calloc(1, sizeof(FontTagData));
882    value = _get_tag_value(tag_str, "size");
883    freeAndAssign(data->size, value);
884    value = _get_tag_value(tag_str, "color");
885    freeAndAssign(data->color, value);
886    value = _get_tag_value(tag_str, "bgcolor");
887    freeAndAssign(data->bg_color, value);
888    value = _get_tag_value(tag_str, "face");
889    freeAndAssign(data->name, value);
890
891    return data;
892 }
893
894 static PItemTagData
895 _set_HTML_img_data(PItemTagData data, const char *tag_str)
896 {
897    char *value;
898
899    if (!data)
900      data = calloc(1, sizeof(ItemTagData));
901    value = _get_tag_value(tag_str, "src");
902    if (value)
903      {
904         char *path = strstr(value, "file://");
905         if (path)
906           {
907              char *modify = malloc(sizeof(char) * (strlen(value) + 1));
908              strncpy(modify, "file://", 7);
909              modify[7] = '\0';
910              path += 7;
911              while (path[1] && path[0] && path[1] == '/' && path[0] == '/')
912                {
913                   path++;
914                }
915              strcat(modify, path);
916              data->href = modify;
917              cnp_debug("image src ---%s---\n", data->href);
918              free(value);
919           }
920         else
921           freeAndAssign(data->href, value);
922      }
923
924    value = _get_tag_value(tag_str, "width");
925    freeAndAssign(data->width, value);
926    value = _get_tag_value(tag_str, "height");
927    freeAndAssign(data->height, value);
928    return data;
929 }
930
931 static void
932 _set_HTML_tag_data(Eina_List* nodes)
933 {
934    PTagNode trail;
935    Eina_List *l;
936
937    EINA_LIST_FOREACH(nodes, l, trail)
938      {
939         if (!trail->tag)
940           continue;
941         if (!strcmp("font", trail->tag))
942              trail->tagData = _set_HTML_font_data(trail->tagData, trail->tag_str);
943         else if (!strcmp("img", trail->tag))
944              trail->tagData = _set_HTML_img_data(trail->tagData, trail->tag_str);
945      }
946 }
947
948 #ifdef DEBUGON
949 static void
950 _dumpNode(Eina_List* nodes)
951 {
952    PTagNode trail;
953    Eina_List *l;
954
955    EINA_LIST_FOREACH(nodes, l, trail)
956      {
957         cnp_debug("tag: %s, tag_str: %s, str: %s, tagPosType: %d\n",
958                trail->tag, trail->tag_str, trail->str, trail->tagPosType);
959         cnp_debug("matchTag: %x ", (unsigned int)trail->matchTag);
960         if (trail->matchTag)
961           cnp_debug("matchTag->tag_str: %s", trail->matchTag->tag_str);
962         if (trail->tagData)
963           {
964              if (!strcmp(trail->tag, "font"))
965                {
966                   PFontTagData data = trail->tagData;
967                   cnp_debug(" tagData->name: %s, tagData->color: %s, tagData->size: %s, tagData->bg_color: %s",
968                          data->name, data->color, data->size, data->bg_color);
969                }
970              else if (!strcmp(trail->tag, "item") || !strcmp(trail->tag, "img"))
971                {
972                   PItemTagData data = trail->tagData;
973                   cnp_debug(" tagData->href: %s, tagData->width: %s, tagData->height: %s",
974                          data->href, data->width, data->height);
975                }
976              else
977                cnp_debug("\nERROR!!!! not need tagData");
978           }
979         cnp_debug("\n");
980      }
981 }
982 #endif
983
984 static char *
985 _convert_to_html(Eina_List* nodes)
986 {
987    PTagNode trail;
988    Eina_List *l;
989
990    Eina_Strbuf *html = eina_strbuf_new();
991
992    int tableCnt = sizeof(_EFLtoHTMLConvertTable) / sizeof(TagTable);
993
994    EINA_LIST_FOREACH(nodes, l, trail)
995      {
996         if (trail->tag)
997           {
998              char *tagName = trail->tagPosType == TAGPOS_END ?
999                 trail->matchTag->tag : trail->tag;
1000              int j;
1001              for(j = 0; j < tableCnt; j++)
1002                {
1003                   if (!strcmp(_EFLtoHTMLConvertTable[j].src, tagName))
1004                     {
1005                        switch(trail->tagPosType)
1006                          {
1007                           case TAGPOS_END:
1008                              eina_strbuf_append(html, "</");
1009                              break;
1010                           default:
1011                              eina_strbuf_append(html, "<");
1012                              break;
1013                          }
1014
1015                        eina_strbuf_append(html, _EFLtoHTMLConvertTable[j].dst);
1016                        if (trail->tagPosType != TAGPOS_END)
1017                          {
1018                             if (!strcmp(_EFLtoHTMLConvertTable[j].src, "font"))
1019                               {
1020                                  PFontTagData data = trail->tagData;
1021                                  if (data->name)
1022                                    {
1023                                    }
1024                                  if (data->color)
1025                                    eina_strbuf_append_printf(html, " color=\"%s\"", data->color);
1026                                  if (data->size)
1027                                    eina_strbuf_append_printf(html, " size=\"%s\"", data->size);
1028                                  if (data->bg_color)
1029                                    {
1030                                    }
1031                               }
1032                             else if (!strcmp(_EFLtoHTMLConvertTable[j].src, "item"))
1033                               {
1034                                  PItemTagData data = trail->tagData;
1035                                  if (data->href)
1036                                    eina_strbuf_append_printf(html, " src=\"%s\"", data->href);
1037                                  if (data->width)
1038                                    eina_strbuf_append_printf(html, " width=\"%s\"", data->width);
1039                                  if (data->height)
1040                                    eina_strbuf_append_printf(html, " height=\"%s\"", data->height);
1041                               }
1042                          }
1043                        switch(trail->tagPosType)
1044                          {
1045                             /* closed tag does not need in HTML
1046                           case TAGPOS_ALONE:
1047                              eina_strbuf_append(html, " />");
1048                              break;*/
1049                           default:
1050                              eina_strbuf_append(html, ">");
1051                              break;
1052                          }
1053                        break;
1054                     }
1055                }
1056           }
1057         if (trail->str)
1058           eina_strbuf_append(html, trail->str);
1059      }
1060
1061    char *ret = eina_strbuf_string_steal(html);
1062    eina_strbuf_free(html);
1063    return ret;
1064 }
1065
1066 #define IMAGE_DEFAULT_WIDTH "240"
1067 #define IMAGE_DEFAULT_HEIGHT "180"
1068
1069
1070 static char *
1071 _convert_to_edje(Eina_List* nodes)
1072 {
1073    PTagNode trail;
1074    Eina_List *l;
1075
1076    Eina_Strbuf *html = eina_strbuf_new();
1077
1078    int tableCnt = sizeof(_HTMLtoEFLConvertTable) / sizeof(TagTable);
1079
1080    EINA_LIST_FOREACH(nodes, l, trail)
1081      {
1082         if (trail->tag)
1083           {
1084              char *tagName = trail->tagPosType == TAGPOS_END ?
1085                 trail->matchTag->tag : trail->tag;
1086              int j;
1087              for(j = 0; j < tableCnt; j++)
1088                {
1089                   if (!strcmp(_HTMLtoEFLConvertTable[j].src, tagName))
1090                     {
1091                        if (_HTMLtoEFLConvertTable[j].dst[0] != '\0')
1092                          {
1093                             switch(trail->tagPosType)
1094                               {
1095                                case TAGPOS_END:
1096                                   eina_strbuf_append(html, "</");
1097                                   break;
1098                                default:
1099                                   eina_strbuf_append(html, "<");
1100                                   break;
1101                               }
1102
1103                             eina_strbuf_append(html, _HTMLtoEFLConvertTable[j].dst);
1104                          }
1105                        if (trail->tagPosType != TAGPOS_END)
1106                          {
1107                             if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1108                               {
1109                                  PFontTagData data = trail->tagData;
1110                                  if (data->name)
1111                                    {
1112                                    }
1113                                  if (data->color)
1114                                    eina_strbuf_append_printf(html, "<color=%s>", data->color);
1115                                  if (data->size)
1116                                    eina_strbuf_append_printf(html, "<font_size=%s>", data->size);
1117                                  if (data->bg_color)
1118                                    {
1119                                    }
1120                                  break;
1121                               }
1122                             else if (!strcmp(_HTMLtoEFLConvertTable[j].src, "img"))
1123                               {
1124                                  PItemTagData data = trail->tagData;
1125                                  char *width = IMAGE_DEFAULT_WIDTH, *height = IMAGE_DEFAULT_HEIGHT;
1126                                  if (data->width)
1127                                    width = data->width;
1128                                  if (data->height)
1129                                    height = data->height;
1130                                  eina_strbuf_append_printf(html, " absize=%sx%s", width, height);
1131                                  if (data->href)
1132                                    eina_strbuf_append_printf(html, " href=%s></item>", data->href);
1133                                  break;
1134                               }
1135                          }
1136                        else
1137                          {
1138                             if (_HTMLtoEFLConvertTable[j].dst[0] == '\0')
1139                               {
1140                                  if (!strcmp(_HTMLtoEFLConvertTable[j].src, "font"))
1141                                    {
1142                                       if (trail->matchTag->tagData)
1143                                         {
1144                                            PFontTagData data = trail->matchTag->tagData;
1145                                            if (data->name)
1146                                              {
1147                                              }
1148                                            if (data->color)
1149                                              eina_strbuf_append_printf(html, "</color>");
1150                                            if (data->size)
1151                                              eina_strbuf_append_printf(html, "</font>");
1152                                            if (data->bg_color)
1153                                              {
1154                                              }
1155                                            break;
1156                                         }
1157                                    }
1158                               }
1159                          }
1160                        switch(trail->tagPosType)
1161                          {
1162                             /* not support in efl
1163                           case TAGPOS_ALONE:
1164                              eina_strbuf_append(html, " />");
1165                              break;
1166                              */
1167                           default:
1168                              eina_strbuf_append(html, ">");
1169                              break;
1170                          }
1171                        break;
1172                     }
1173                }/* for(j = 0; j < tableCnt; j++) end */
1174           }
1175         if (trail->str)
1176           eina_strbuf_append(html, trail->str);
1177      }
1178
1179    char *ret = eina_strbuf_string_steal(html);
1180    eina_strbuf_free(html);
1181    return ret;
1182
1183 }
1184
1185 Eina_Bool
1186 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
1187 {
1188 #ifdef HAVE_ELEMENTARY_X
1189    Evas_Object *top = elm_widget_top_get(widget);
1190    Ecore_X_Window xwin;
1191    Cnp_Selection *sel;
1192
1193    if (top) xwin = elm_win_xwindow_get(top);
1194    else xwin = elm_win_xwindow_get(widget);
1195    if (!xwin) return EINA_FALSE;
1196    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1197    if (!_elm_cnp_init_count) _elm_cnp_init();
1198    if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
1199      return elm_selection_clear(selection, widget);
1200
1201    sel = selections + selection;
1202
1203    sel->active = 1;
1204    sel->widget = widget;
1205
1206    sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
1207    sel->format = format;
1208    sel->selbuf = selbuf ? strdup(selbuf) : NULL;
1209
1210    return EINA_TRUE;
1211 #else
1212    return EINA_FALSE;
1213 #endif
1214 }
1215
1216 Eina_Bool
1217 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
1218 {
1219 #ifdef HAVE_ELEMENTARY_X
1220    Cnp_Selection *sel;
1221
1222    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1223    if (!_elm_cnp_init_count) _elm_cnp_init();
1224
1225    sel = selections + selection;
1226
1227    /* No longer this selection: Consider it gone! */
1228    if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
1229
1230    sel->active = 0;
1231    sel->widget = NULL;
1232    sel->clear();
1233
1234    return EINA_TRUE;
1235 #else
1236    return EINA_FALSE;
1237 #endif
1238 }
1239
1240 Eina_Bool
1241 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
1242                   Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
1243 {
1244 #ifdef HAVE_ELEMENTARY_X
1245    Evas_Object *top;
1246    Cnp_Selection *sel;
1247
1248    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
1249    if (!_elm_cnp_init_count) _elm_cnp_init();
1250
1251    sel = selections + selection;
1252    top = elm_widget_top_get(widget);
1253    if (!top) return EINA_FALSE;
1254
1255    sel->requestformat = format;
1256    sel->requestwidget = widget;
1257    sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
1258    sel->datacb = datacb;
1259    sel->udata = udata;
1260
1261    return EINA_TRUE;
1262 #else
1263    return EINA_FALSE;
1264 #endif
1265 }
1266
1267 #ifdef HAVE_ELEMENTARY_X
1268
1269 static Eina_Bool
1270 _elm_cnp_init(void)
1271 {
1272    int i;
1273
1274    if (_elm_cnp_init_count++) return EINA_TRUE;
1275    for (i = 0; i < CNP_N_ATOMS; i++)
1276      {
1277         atoms[i].atom = ecore_x_atom_get(atoms[i].name);
1278         ecore_x_selection_converter_atom_add(atoms[i].atom,
1279                                              atoms[i].converter);
1280      }
1281    clipboard_atom = ecore_x_atom_get("CLIPBOARD");
1282
1283    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
1284    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
1285
1286    text_uri = eina_stringshare_add("text/uri-list");
1287    return EINA_TRUE;
1288 }
1289
1290 static Eina_Bool
1291 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
1292 {
1293    Ecore_X_Event_Selection_Clear *ev = event;
1294    Cnp_Selection *sel;
1295    int i;
1296
1297    for (i = 0; i < ELM_SEL_MAX; i++)
1298      {
1299         if (selections[i].ecore_sel == ev->selection) break;
1300      }
1301    cnp_debug("selection %d clear\n", i);
1302    /* Not me... Don't care */
1303    if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
1304
1305    sel = selections + i;
1306    sel->active = 0;
1307    sel->widget = NULL;
1308    sel->selbuf = NULL;
1309
1310    return ECORE_CALLBACK_PASS_ON;
1311 }
1312
1313
1314 /*
1315  * Response to a selection notify:
1316  *      - So we have asked for the selection list.
1317  *      - If it's the targets list, parse it, and fire of what we want,
1318  *      else it's the data we want.
1319  */
1320 static Eina_Bool
1321 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
1322 {
1323    Ecore_X_Event_Selection_Notify *ev = event;
1324    Cnp_Selection *sel;
1325    int i;
1326
1327    cnp_debug("selection notify callback: %d\n",ev->selection);
1328    switch (ev->selection)
1329      {
1330       case ECORE_X_SELECTION_CLIPBOARD:
1331          sel = selections + ELM_SEL_CLIPBOARD;
1332          break;
1333       case ECORE_X_SELECTION_PRIMARY:
1334          sel = selections + ELM_SEL_PRIMARY;
1335          break;
1336       case ECORE_X_SELECTION_SECONDARY:
1337          sel = selections + ELM_SEL_SECONDARY;
1338          break;
1339       case ECORE_X_SELECTION_XDND:
1340          sel = selections + ELM_SEL_XDND;
1341          break;
1342       default:
1343          return ECORE_CALLBACK_PASS_ON;
1344      }
1345    cnp_debug("Target is %s\n", ev->target);
1346
1347    for (i = 0; i < CNP_N_ATOMS; i++)
1348      {
1349         if (!strcmp(ev->target, atoms[i].name))
1350           {
1351              if (atoms[i].notify)
1352                {
1353                   cnp_debug("Found something: %s\n", atoms[i].name);
1354                   atoms[i].notify(sel, ev);
1355                }
1356              else
1357                {
1358                   cnp_debug("Ignored: No handler!\n");
1359                }
1360           }
1361      }
1362
1363    return ECORE_CALLBACK_PASS_ON;
1364 }
1365
1366
1367
1368 static Eina_Bool
1369 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
1370 {
1371    int i,count;
1372    Ecore_X_Atom *aret;
1373    Cnp_Selection *sel;
1374
1375    if (!data_ret) return EINA_FALSE;
1376    if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
1377      return EINA_FALSE;
1378
1379    sel = selections + *((int *)data);
1380
1381    for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
1382      {
1383         if (sel->format & atoms[i].formats) count++;
1384      }
1385
1386    aret = malloc(sizeof(Ecore_X_Atom) * count);
1387    for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
1388      {
1389         if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
1390      }
1391
1392    *data_ret = aret;
1393    if (typesize) *typesize = 32 /* urk */;
1394    if (ttype) *ttype = ECORE_X_ATOM_ATOM;
1395    if (size_ret) *size_ret = count;
1396
1397    return EINA_TRUE;
1398 }
1399
1400 static Eina_Bool
1401 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__)
1402 {
1403    cnp_debug("Image converter called\n");
1404    return EINA_TRUE;
1405 }
1406
1407 static Eina_Bool
1408 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__)
1409 {
1410    Cnp_Selection *sel;
1411
1412    cnp_debug("Vcard send called\n");
1413
1414    sel = selections + *((int *)data);
1415
1416    if (data_ret) *data_ret = strdup(sel->selbuf);
1417    if (size_ret) *size_ret = strlen(sel->selbuf);
1418
1419    return EINA_TRUE;
1420 }
1421
1422 static Eina_Bool
1423 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
1424 {
1425    Ecore_X_Selection_Data *data;
1426    char *p;
1427
1428    data = notify->data;
1429    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1430    if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
1431    else p = (char *)data->data;
1432
1433    if (!p) return EINA_TRUE;
1434    cnp_debug("Got %s\n", p);
1435    if (strncmp(p, "file://", 7))
1436      {
1437         if (*p != '/') return EINA_FALSE;
1438      }
1439
1440    return EINA_TRUE;
1441 }
1442
1443 /*
1444  * Callback to handle a targets response on a selection request:
1445  * So pick the format we'd like; and then request it.
1446  */
1447 static int
1448 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1449 {
1450    Ecore_X_Selection_Data_Targets *targets;
1451    Ecore_X_Atom *atomlist;
1452    Evas_Object *top;
1453    int i, j;
1454
1455    targets = notify->data;
1456    atomlist = (Ecore_X_Atom *)(targets->data.data);
1457
1458    for (j = 1; j < CNP_N_ATOMS; j++)
1459      {
1460         cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
1461         if (!(atoms[j].formats & sel->requestformat)) continue;
1462         for (i = 0; i < targets->data.length; i++)
1463           {
1464              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
1465                {
1466 /*                  if ((j == CNP_ATOM_text_uri) ||
1467                       (j == CNP_ATOM_text_urilist))
1468                     {
1469                       if(!is_uri_type_data(sel, notify)) continue;
1470                     }*/
1471                   cnp_debug("Atom %s matches\n",atoms[j].name);
1472                   goto done;
1473                }
1474           }
1475      }
1476
1477    cnp_debug("Couldn't find anything that matches\n");
1478    return ECORE_CALLBACK_PASS_ON;
1479
1480 done:
1481    top = elm_widget_top_get(sel->requestwidget);
1482    if (!top) top = sel->requestwidget;
1483    cnp_debug("Sending request for %s\n", atoms[j].name);
1484    sel->request(elm_win_xwindow_get(top), atoms[j].name);
1485
1486    return ECORE_CALLBACK_PASS_ON;
1487 }
1488
1489 static int
1490 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1491 {
1492    Ecore_X_Selection_Data_Targets *targets;
1493    Ecore_X_Atom *atomlist;
1494    Evas_Object *top;
1495    int i,j;
1496
1497    targets = notify->data;
1498    atomlist = (Ecore_X_Atom *)(targets->data.data);
1499
1500    /* Start from 1: Skip targets */
1501    for (j = 1 ; j < CNP_N_ATOMS ; j ++)
1502      {
1503         if (!(atoms[j].formats & sel->requestformat)) continue;
1504         for (i = 0 ; i < targets->data.length ; i ++)
1505           {
1506              if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
1507                {
1508                   /* Found a match: Use it */
1509                   goto found;
1510                }
1511           }
1512      }
1513 found:
1514    if (j == CNP_N_ATOMS)
1515      {
1516         cnp_debug("No matching type found\n");
1517         return 0;
1518      }
1519
1520    top = elm_widget_top_get(sel->requestwidget);
1521    if (!top) return 0;
1522
1523    sel->request(elm_win_xwindow_get(top), atoms[j].name);
1524    return 0;
1525 }
1526
1527 static void
1528 entry_insert_filter(Evas_Object* entry, char* str)
1529 {
1530    if (!entry || !str)
1531      return;
1532
1533    char *insertStr = str;
1534    // if entry has text only set then remove item tags
1535    if (elm_entry_cnp_textonly_get(entry))
1536      {
1537         while (EINA_TRUE)
1538           {
1539              char *startTag = NULL;
1540              char *endTag = NULL;
1541
1542              startTag = strstr(insertStr, "<item");
1543              if (!startTag)
1544                startTag = strstr(insertStr, "</item");
1545              if (startTag)
1546                endTag = strstr(startTag, ">");
1547              else
1548                break;
1549              if (!endTag || startTag > endTag)
1550                {
1551                   cnp_debug("Broken tag: %s\n", str);
1552                   break;
1553                }
1554
1555              size_t sindex = startTag - insertStr;
1556              size_t eindex = endTag - insertStr + 1;
1557
1558              Eina_Strbuf *buf = eina_strbuf_new();
1559              if (buf)
1560                {
1561                   eina_strbuf_append(buf, insertStr);
1562                   eina_strbuf_remove(buf, sindex, eindex);
1563                   insertStr = eina_strbuf_string_steal(buf);
1564                   eina_strbuf_free(buf);
1565                }
1566           }
1567      }
1568    cnp_debug("remove item tag: %s\n", insertStr);
1569
1570    // if entry has single line set then remove <br> & <ps> tags
1571    if (elm_entry_single_line_get(entry))
1572      {
1573         Eina_Strbuf *buf = eina_strbuf_new();
1574         if (buf)
1575           {
1576              eina_strbuf_append(buf, insertStr);
1577              eina_strbuf_replace_all(buf, "<br>", "");
1578              eina_strbuf_replace_all(buf, "<ps>", "");
1579              insertStr = eina_strbuf_string_steal(buf);
1580              eina_strbuf_free(buf);
1581           }
1582      }
1583    cnp_debug("remove break tag: %s\n", insertStr);
1584
1585    elm_entry_entry_insert(entry, insertStr);
1586
1587    if (insertStr != str)
1588      free(insertStr);
1589 }
1590
1591 static int
1592 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1593 {
1594    Ecore_X_Selection_Data *data;
1595    char *str;
1596
1597    data = notify->data;
1598
1599    if (sel->datacb)
1600      {
1601         Elm_Selection_Data ddata;
1602
1603         str = strdup(data->data);
1604         ddata.x = ddata.y = 0;
1605         ddata.format = ELM_SEL_FORMAT_TEXT;
1606         ddata.data = str;
1607         ddata.len = data->length;
1608         sel->datacb(sel->udata, sel->widget, &ddata);
1609         free(str);
1610         return 0;
1611      }
1612
1613    cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
1614    str = mark_up((char *)data->data, data->length, NULL);
1615    cnp_debug("String is %s (from %s)\n", str, data->data);
1616    entry_insert_filter(sel->requestwidget, str);
1617    //elm_entry_entry_insert(sel->requestwidget, str);
1618    free(str);
1619    return 0;
1620 }
1621
1622
1623 /**
1624  * So someone is pasting an image into my entry or widget...
1625  */
1626 static int
1627 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1628 {
1629    Ecore_X_Selection_Data *data;
1630    Ecore_X_Selection_Data_Files *files;
1631    Paste_Image *pi;
1632    char *p, *stripstr;
1633
1634    data = notify->data;
1635    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1636    if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1637      {
1638         cnp_debug("got a files list\n");
1639         files = notify->data;
1640         if (files->num_files > 1)
1641           {
1642              /* Don't handle many items */
1643              cnp_debug("more then one file: Bailing\n");
1644              return 0;
1645           }
1646         stripstr = p = strdup(files->files[0]);
1647      }
1648    else
1649      {
1650         stripstr = p = strndup((char *)data->data, data->length);
1651      }
1652
1653    if (!p)
1654      {
1655         cnp_debug("Couldn't find a file\n");
1656         return 0;
1657      }
1658    cnp_debug("Got %s\n",p);
1659    if (sel->datacb)
1660      {
1661         Elm_Selection_Data ddata;
1662
1663         ddata.x = ddata.y = 0;
1664         ddata.format = ELM_SEL_FORMAT_MARKUP;
1665         ddata.data = p;
1666         ddata.len = data->length;
1667         sel->datacb(sel->udata, sel->widget, &ddata);
1668         free(p);
1669         return 0;
1670      }
1671    if (strncmp(p, "file://", 7))
1672      {
1673         /* Try and continue if it looks sane */
1674         if (*p != '/')
1675           {
1676              free(p);
1677              return 0;
1678           }
1679      }
1680    else
1681      {
1682         p += strlen("file://");
1683      }
1684
1685    if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1686    pi = pasteimage_alloc(p, strlen(p));
1687    if (savedtypes.textreq)
1688      {
1689         savedtypes.textreq = 0;
1690         savedtypes.pi = pi;
1691      }
1692    else
1693      {
1694         pasteimage_append(pi, sel->requestwidget);
1695         savedtypes.pi = NULL;
1696      }
1697    free(stripstr);
1698    return 0;
1699 }
1700
1701 /**
1702  * Just receieved an vcard, either through cut and paste, or dnd.
1703  */
1704 static int
1705 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1706 {
1707    Dropable *dropable;
1708    Eina_List *l;
1709    Ecore_X_Selection_Data *data;
1710
1711    data = notify->data;
1712    cnp_debug("vcard receive\n");
1713
1714    if (sel == (selections + ELM_SEL_XDND))
1715      {
1716         Elm_Selection_Data ddata;
1717
1718         cnp_debug("drag & drop\n");
1719         /* FIXME: this needs to be generic: Used for all receives */
1720         EINA_LIST_FOREACH(drops, l, dropable)
1721           {
1722              if (dropable->obj == sel->requestwidget) break;
1723           }
1724         if (!dropable)
1725           {
1726              cnp_debug("Unable to find drop object");
1727              ecore_x_dnd_send_finished();
1728              return 0;
1729           }
1730         dropable = eina_list_data_get(l);
1731         ddata.x = savedtypes.x;
1732         ddata.y = savedtypes.y;
1733         ddata.format = ELM_SEL_FORMAT_VCARD;
1734         ddata.data = data->data;
1735         ddata.len = data->length;
1736         dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1737         ecore_x_dnd_send_finished();
1738      }
1739    else if (sel->datacb)
1740      {
1741         Elm_Selection_Data ddata;
1742         ddata.x = ddata.y = 0;
1743         ddata.format = ELM_SEL_FORMAT_IMAGE;
1744         ddata.data = data->data;
1745         ddata.len = data->length;
1746         sel->datacb(sel->udata, sel->widget, &ddata);
1747      }
1748    else
1749      {
1750         cnp_debug("Paste request\n");
1751      }
1752
1753    return 0;
1754
1755 }
1756
1757
1758 static int
1759 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1760 {
1761    Ecore_X_Selection_Data *data;
1762    Tmp_Info *tmp;
1763    Paste_Image *pi;
1764
1765    cnp_debug("got a png (or a jpeg)!\n");
1766    data = notify->data;
1767
1768    cnp_debug("Size if %d\n", data->length);
1769
1770    if (sel->datacb)
1771      {
1772         Elm_Selection_Data ddata;
1773
1774         ddata.x = ddata.y = 0;
1775         ddata.format = ELM_SEL_FORMAT_IMAGE;
1776         ddata.data = data->data;
1777         ddata.len = data->length;
1778         sel->datacb(sel->udata, sel->widget, &ddata);
1779         return 0;
1780      }
1781
1782    /* generate tmp name */
1783    tmp = elm_cnp_tempfile_create(data->length);
1784    memcpy(tmp->map, data->data, data->length);
1785    munmap(tmp->map,data->length);
1786
1787    /* FIXME: Add to paste image data to clean up */
1788    pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1789    pasteimage_append(pi, sel->requestwidget);
1790
1791    tmpinfo_free(tmp);
1792    return 0;
1793 }
1794
1795 static int
1796 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1797 {
1798    Ecore_X_Selection_Data *data;
1799
1800    data = notify->data;
1801
1802    char *stripstr = NULL;
1803    stripstr = malloc(sizeof(char) * (data->length + 1));
1804    strncpy(stripstr, (char *)data->data, data->length);
1805    stripstr[data->length] = '\0';
1806
1807    if (sel->datacb)
1808      {
1809         Elm_Selection_Data ddata;
1810         ddata.x = ddata.y = 0;
1811         ddata.format = ELM_SEL_FORMAT_MARKUP;
1812         ddata.data = stripstr;
1813         ddata.len = data->length;
1814         sel->datacb(sel->udata, sel->widget, &ddata);
1815      }
1816    else
1817      entry_insert_filter(sel->requestwidget, stripstr);
1818      //elm_entry_entry_insert(sel->requestwidget, stripstr);
1819
1820    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1821    free(stripstr);
1822    return 0;
1823 }
1824
1825 /**
1826  *    Warning: Generic text/html can';t handle it sanely.
1827  *    Firefox sends ucs2 (i think).
1828  *       chrome sends utf8... blerg
1829  */
1830 static int
1831 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1832 {
1833    Ecore_X_Selection_Data *data;
1834
1835    cnp_debug("Got some HTML: Checking encoding is useful\n");
1836    data = notify->data;
1837
1838    char *stripstr = NULL;
1839    stripstr = malloc(sizeof(char) * (data->length + 1));
1840    strncpy(stripstr, (char *)data->data, data->length);
1841    stripstr[data->length] = '\0';
1842
1843    if (sel->datacb)
1844      {
1845         Elm_Selection_Data ddata;
1846         ddata.x = ddata.y = 0;
1847         ddata.format = ELM_SEL_FORMAT_HTML;
1848         ddata.data = stripstr;
1849         ddata.len = data->length;
1850         sel->datacb(sel->udata, sel->widget, &ddata);
1851      }
1852    else
1853      entry_insert_filter(sel->requestwidget, stripstr);
1854      //elm_entry_entry_insert(sel->requestwidget, stripstr);
1855
1856    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1857    free(stripstr);
1858    return 0;
1859 }
1860
1861
1862 static Eina_Bool
1863 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1864 {
1865    Cnp_Selection *sel;
1866
1867    cnp_debug("text converter\n");
1868    sel = selections + *((int *)data);
1869    if (!sel->active) return EINA_TRUE;
1870
1871    if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1872        (sel->format & ELM_SEL_FORMAT_HTML))
1873      {
1874         *data_ret = remove_tags(sel->selbuf, size_ret);
1875      }
1876    else if (sel->format & ELM_SEL_FORMAT_TEXT)
1877      {
1878         *data_ret = strdup(sel->selbuf);
1879         *size_ret = strlen(sel->selbuf);
1880      }
1881    else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1882      {
1883         cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1884         cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1885         evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1886         if (!*data_ret) *data_ret = strdup("No file");
1887         else *data_ret = strdup(*data_ret);
1888         *size_ret = strlen(*data_ret);
1889      }
1890    return EINA_TRUE;
1891 }
1892
1893 static Eina_Bool
1894 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1895 {
1896    Cnp_Selection *sel;
1897    sel = selections + *((int *)data);
1898 /*   if (data_ret) *data_ret = strdup(sel->selbuf);
1899    if (size_ret) *size_ret = strlen(sel->selbuf);*/
1900    char *edje = NULL;
1901
1902    if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
1903      {
1904         if (sel->selbuf && sel->selbuf[0] != '\0')
1905           edje = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
1906      }
1907    else if (data_ret && ((sel->format & ELM_SEL_FORMAT_HTML)))
1908      {
1909         Eina_List *nodeList = NULL;
1910         Eina_List *trail;
1911         PTagNode nodeData;
1912
1913         nodeData = _get_start_node(sel->selbuf);
1914
1915         while (nodeData)
1916           {
1917              nodeList = eina_list_append(nodeList, nodeData);
1918              nodeData = _get_next_node(nodeData);
1919           }
1920
1921         _link_match_tags(nodeList);
1922
1923         _set_HTML_tag_data(nodeList);
1924
1925 #ifdef DEBUGON
1926         _dumpNode(nodeList);
1927 #endif
1928         edje = _convert_to_edje(nodeList);
1929
1930         cnp_debug("convert edje: %s\n", edje);
1931
1932         EINA_LIST_FOREACH(nodeList, trail, nodeData)
1933            _delete_node(nodeData);
1934         eina_list_free(nodeList);
1935
1936      }
1937    if (data_ret)
1938      {
1939         if (edje)
1940           *data_ret = edje;
1941         else
1942           *data_ret = strdup(sel->selbuf);
1943      }
1944    if (size_ret)
1945      {
1946         if (edje)
1947           *size_ret = strlen(edje);
1948         else
1949           *size_ret = strlen(sel->selbuf);
1950      }
1951
1952    return EINA_TRUE;
1953 }
1954
1955 static Eina_Bool
1956 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1957 {
1958    Cnp_Selection *sel;
1959
1960    sel = selections + *(int *)data;
1961
1962    char *html = NULL;
1963    char *convert_target = sel->selbuf;
1964    Eina_Bool convert_edje = EINA_FALSE;
1965
1966    if (data_ret && (sel->format == ELM_SEL_FORMAT_TEXT))
1967      {
1968         if (sel->selbuf && sel->selbuf[0] != '\0')
1969           {
1970              convert_target = mark_up(sel->selbuf, strlen(sel->selbuf), NULL);
1971              convert_edje = EINA_TRUE;
1972           }
1973      }
1974
1975    if (data_ret && ((sel->format & ELM_SEL_FORMAT_MARKUP) || convert_edje))
1976      {
1977         Eina_List *nodeList = NULL;
1978         Eina_List *trail;
1979         PTagNode nodeData;
1980
1981         nodeData = _get_start_node(convert_target);
1982
1983         while (nodeData)
1984           {
1985              nodeList = eina_list_append(nodeList, nodeData);
1986              nodeData = _get_next_node(nodeData);
1987           }
1988
1989         _link_match_tags(nodeList);
1990
1991         _set_EFL_tag_data(nodeList);
1992
1993 #ifdef DEBUGON
1994         _dumpNode(nodeList);
1995 #endif
1996         html = _convert_to_html(nodeList);
1997
1998         cnp_debug("convert html: %s\n", html);
1999
2000         EINA_LIST_FOREACH(nodeList, trail, nodeData)
2001            _delete_node(nodeData);
2002         eina_list_free(nodeList);
2003      }
2004    if (data_ret)
2005      {
2006         if (html)
2007           *data_ret = html;
2008         else
2009           *data_ret = strdup(sel->selbuf);
2010      }
2011
2012    if (size_ret)
2013      {
2014         if (html)
2015           *size_ret = strlen(html);
2016         else
2017           *size_ret = strlen(sel->selbuf);
2018      }
2019    if (convert_target != sel->selbuf)
2020      free(convert_target);
2021
2022    return EINA_TRUE;
2023 }
2024
2025 static Eina_Bool
2026 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
2027 {
2028    Cnp_Selection *sel;
2029    sel = selections + *((int *)data);
2030    cnp_debug("Uri converter\n");
2031    if (data_ret) *data_ret = strdup(sel->selbuf);
2032    if (size_ret) *size_ret = strlen(sel->selbuf);
2033    return EINA_TRUE;
2034 }
2035
2036 /*
2037  * Image paste provide
2038  */
2039
2040 /* FIXME: Should add provider for each pasted item: Use data to store it
2041  * much easier */
2042 static Evas_Object *
2043 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
2044 {
2045    Paste_Image *pi;
2046    Eina_List *l;
2047
2048    cnp_debug("image provider for %s called\n", item);
2049    EINA_LIST_FOREACH(pastedimages, l, pi)
2050      {
2051         cnp_debug("is it %s?\n",pi->tag);
2052         if (!strcmp(pi->tag, item))
2053           {
2054              /* Found it */
2055              Evas_Object *o;
2056              o = evas_object_image_filled_add(evas_object_evas_get(entry));
2057              /* FIXME: Handle eets */
2058              cnp_debug("file is %s (object is %p)\n", pi->file, o);
2059              evas_object_image_file_set(o, pi->file, NULL);
2060              evas_object_show(o);
2061              return o;
2062           }
2063      }
2064    return NULL;
2065 }
2066
2067
2068 static Paste_Image *
2069 pasteimage_alloc(const char *file, int pathlen)
2070 {
2071    Paste_Image *pi;
2072    int len;
2073    char *buf, *filebuf;
2074    int prefixlen = strlen("file://");
2075
2076    pi = calloc(1, sizeof(Paste_Image));
2077    if (!pi) return NULL;
2078
2079    len = snprintf(NULL, 0, "pasteimage-%p", pi);
2080    len++;
2081    buf = malloc(len);
2082    if (!buf)
2083      {
2084         free(pi);
2085         return NULL;
2086      }
2087    snprintf(buf, len, "pasteimage-%p", pi);
2088    pi->tag = buf;
2089
2090    if (file)
2091      {
2092         if (strstr(file,"file://")) file += prefixlen;
2093         filebuf = alloca(pathlen + 1);
2094         strncpy(filebuf, file, pathlen);
2095         filebuf[pathlen] = 0;
2096         pi->file = strdup(filebuf);
2097      }
2098
2099    return pi;
2100 }
2101
2102 static void
2103 pasteimage_free(Paste_Image *pi)
2104 {
2105    if (!pi) return;
2106    if (pi->file) free((void*)pi->file);
2107    if (pi->tag) free((void*)pi->tag);
2108    free(pi);
2109 }
2110
2111 static Eina_Bool
2112 pasteimage_provider_set(Evas_Object *entry)
2113 {
2114    void *v;
2115    const char *type;
2116
2117    if (!entry) return EINA_FALSE;
2118    type = elm_widget_type_get(entry);
2119    cnp_debug("type is %s\n", type);
2120    if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
2121
2122    v = evas_object_data_get(entry, PROVIDER_SET);
2123    if (!v)
2124      {
2125         evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
2126         elm_entry_item_provider_append(entry, image_provider, NULL);
2127         evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
2128                                        entry_deleted, NULL);
2129      }
2130    return EINA_TRUE;
2131 }
2132
2133
2134 static Eina_Bool
2135 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
2136 {
2137    char *entrytag;
2138    int len;
2139    static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
2140
2141    if (!pi) return EINA_FALSE;
2142    if (!entry) return EINA_FALSE;
2143    if (elm_entry_cnp_textonly_get(entry)) return EINA_FALSE;
2144
2145    pasteimage_provider_set(entry);
2146
2147    len = strlen(tagstring)+strlen(pi->file);
2148
2149    pastedimages = eina_list_append(pastedimages, pi);
2150    entrytag = alloca(len + 1);
2151    snprintf(entrytag, len + 1, tagstring, pi->file);
2152    elm_entry_entry_insert(entry, entrytag);
2153
2154    return EINA_TRUE;
2155 }
2156
2157 static void
2158 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
2159 {
2160    Paste_Image *pi;
2161    Eina_List *l,*next;
2162
2163    EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
2164      {
2165         if (pi->entry == entry)
2166           pastedimages = eina_list_remove_list(pastedimages, l);
2167      }
2168 }
2169
2170
2171 static char *
2172 remove_tags(const char *p, int *len)
2173 {
2174    char *q,*ret;
2175    int i;
2176    if (!p) return NULL;
2177
2178    q = malloc(strlen(p) + 1);
2179    if (!q) return NULL;
2180    ret = q;
2181
2182    while (*p)
2183      {
2184         if ((*p != '<') && (*p != '&')) *q++ = *p++;
2185         else if (*p == '<')
2186           {
2187              if ((p[1] == 'b') && (p[2] == 'r') &&
2188                  ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
2189                *q++ = '\n';
2190              while ((*p) && (*p != '>')) p++;
2191              p++;
2192           }
2193         else if (*p == '&')
2194           {
2195              p++;
2196              for (i = 0 ; i < N_ESCAPES ; i++)
2197                {
2198                   if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
2199                     {
2200                        p += strlen(escapes[i].escape);
2201                        *q = escapes[i].value;
2202                        q++;
2203                        break;
2204                     }
2205                }
2206              if (i == N_ESCAPES) *q ++= '&';
2207           }
2208      }
2209    *q = 0;
2210    if (len) *len = q - ret;
2211    return ret;
2212 }
2213
2214 /* Mark up */
2215 static char *
2216 mark_up(const char *start, int inlen, int *lenp)
2217 {
2218    int l, i;
2219    const char *p;
2220    char *q, *ret;
2221    const char *endp = NULL;
2222
2223    if (!start) return NULL;
2224    if (inlen >= 0) endp = start + inlen;
2225    /* First pass: Count characters */
2226    for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
2227      {
2228         for (i = 0 ; i < N_ESCAPES ; i ++)
2229           {
2230              if (*p == escapes[i].value)
2231                {
2232                   if (!iscntrl(escapes[i].value)) l++;
2233                   l += strlen(escapes[i].escape);
2234                   break;
2235                }
2236           }
2237         if (i == N_ESCAPES) l++;
2238      }
2239
2240    q = ret = malloc(l + 1);
2241
2242    /* Second pass: Change characters */
2243    for (p = start; ((!endp) || (p < endp)) && (*p); )
2244      {
2245         for (i = 0; i < N_ESCAPES; i++)
2246           {
2247              if (*p == escapes[i].value)
2248                {
2249                   if (!iscntrl(escapes[i].value)) *q++ = '&';
2250                   strcpy(q, escapes[i].escape);
2251                   q += strlen(escapes[i].escape);
2252                   p ++;
2253                   break;
2254                }
2255           }
2256         if (i == N_ESCAPES) *q++ = *p++;
2257      }
2258    *q = 0;
2259
2260    if (lenp) *lenp = l;
2261    return ret;
2262 }
2263
2264
2265 static Eina_Bool
2266 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2267 {
2268    Ecore_X_Event_Xdnd_Enter *enter = ev;
2269    int i;
2270
2271    /* Skip it */
2272    if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
2273
2274    cnp_debug("Types\n");
2275    savedtypes.ntypes = enter->num_types;
2276    if (savedtypes.types) free(savedtypes.types);
2277    savedtypes.types = malloc(sizeof(char *) * enter->num_types);
2278    if (!savedtypes.types) return EINA_FALSE;
2279
2280    for (i = 0; i < enter->num_types; i++)
2281      {
2282         savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
2283         cnp_debug("Type is %s %p %p\n", enter->types[i],
2284                   savedtypes.types[i],text_uri);
2285         if (savedtypes.types[i] == text_uri)
2286           {
2287              /* Request it, so we know what it is */
2288              cnp_debug("Sending uri request\n");
2289              savedtypes.textreq = 1;
2290              if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2291              savedtypes.pi = NULL; /* FIXME: Free? */
2292              ecore_x_selection_xdnd_request(enter->win, text_uri);
2293           }
2294      }
2295
2296    /* FIXME: Find an object and make it current */
2297    return EINA_TRUE;
2298 }
2299
2300 static Eina_Bool
2301 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2302 {
2303    struct _Ecore_X_Event_Xdnd_Drop *drop;
2304    Dropable *dropable;
2305    Eina_List *l;
2306    Ecore_Evas *ee;
2307    Ecore_X_Window xwin;
2308    Elm_Selection_Data ddata;
2309    int x, y, w, h;
2310    int i, j;
2311
2312    drop = ev;
2313
2314    // check we still have something to drop
2315    if (!drops) return EINA_TRUE;
2316
2317    /* Find any widget in our window; then work out geometry rel to our window */
2318    for (l = drops; l; l = l->next)
2319      {
2320         dropable = l->data;
2321         xwin = (Ecore_X_Window)ecore_evas_window_get
2322            (ecore_evas_ecore_evas_get(evas_object_evas_get
2323                                       (dropable->obj)));
2324         if (xwin == drop->win) break;
2325      }
2326    /* didn't find a window */
2327    if (!l) return EINA_TRUE;
2328
2329    /* Calculate real (widget relative) position */
2330    // - window position
2331    // - widget position
2332    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
2333    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2334    savedtypes.x = drop->position.x - x;
2335    savedtypes.y = drop->position.y - y;
2336
2337    cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
2338
2339    for (; l; l = l->next)
2340      {
2341         dropable = l->data;
2342         evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
2343         if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
2344             (savedtypes.x < x + w) && (savedtypes.y < y + h))
2345           break; /* found! */
2346      }
2347
2348    if (!l) return EINA_TRUE; /* didn't find one */
2349
2350    evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
2351    savedtypes.x -= x;
2352    savedtypes.y -= y;
2353
2354    /* Find our type from the previous list */
2355    for (i = 0; i < CNP_N_ATOMS; i++)
2356      {
2357         for (j = 0; j < savedtypes.ntypes; j++)
2358           {
2359              if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
2360           }
2361      }
2362
2363    cnp_debug("Didn't find a target\n");
2364    return EINA_TRUE;
2365
2366 found:
2367    cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
2368    cnp_debug("0x%x\n",xwin);
2369
2370    if (i == CNP_ATOM_text_urilist)
2371      {
2372         cnp_debug("We found a URI... (%scached) %s\n",
2373                   savedtypes.pi ? "" : "not ",
2374                   savedtypes.pi->file);
2375         if (savedtypes.pi)
2376           {
2377              char *entrytag;
2378              static const char *tagstring = "<item absize=240x180 href="
2379                 "file://%s></item>";
2380              ddata.x = savedtypes.x;
2381              ddata.y = savedtypes.y;
2382
2383              /* If it's markup that also supports images */
2384              if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
2385                  (dropable->types & ELM_SEL_FORMAT_IMAGE))
2386                {
2387                   int len;
2388                   ddata.format = ELM_SEL_FORMAT_MARKUP;
2389                   pasteimage_provider_set(dropable->obj);
2390
2391                   pastedimages = eina_list_append(pastedimages, savedtypes.pi);
2392                   len = strlen(tagstring) + strlen(savedtypes.pi->file);
2393                   entrytag = alloca(len + 1);
2394                   snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
2395                   ddata.data = entrytag;
2396                   cnp_debug("Insert %s\n", (char *)ddata.data);
2397                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2398                   ecore_x_dnd_send_finished();
2399
2400                   if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2401                   savedtypes.pi = NULL;
2402                   return EINA_TRUE;
2403                }
2404              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
2405                {
2406                   cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
2407                   ddata.format = ELM_SEL_FORMAT_IMAGE;
2408                   ddata.data = (char *)savedtypes.pi->file;
2409                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
2410                   ecore_x_dnd_send_finished();
2411
2412                   if (savedtypes.pi) pasteimage_free(savedtypes.pi);
2413                   savedtypes.pi = NULL;
2414
2415                   return EINA_TRUE;
2416                }
2417              else
2418                {
2419                   cnp_debug("Item doesn't support images... passing\n");
2420                   pasteimage_free(savedtypes.pi);
2421                   return EINA_TRUE;
2422                }
2423           }
2424         else if (savedtypes.textreq)
2425           {
2426              /* Already asked: Pretend we asked now, and paste immediately when
2427               * it comes in */
2428              savedtypes.textreq = 0;
2429              ecore_x_dnd_send_finished();
2430              return EINA_TRUE;
2431           }
2432      }
2433
2434    cnp_debug("doing a request then\n");
2435    selections[ELM_SEL_XDND].requestwidget = dropable->obj;
2436    selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
2437    selections[ELM_SEL_XDND].active = EINA_TRUE;
2438
2439    ecore_x_selection_xdnd_request(xwin, atoms[i].name);
2440
2441    return EINA_TRUE;
2442 }
2443 static Eina_Bool
2444 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2445 {
2446    struct _Ecore_X_Event_Xdnd_Position *pos;
2447    Ecore_X_Rectangle rect;
2448
2449    pos = ev;
2450
2451    /* Need to send a status back */
2452    /* FIXME: Should check I can drop here */
2453    /* FIXME: Should highlight widget */
2454    rect.x = pos->position.x - 5;
2455    rect.y = pos->position.y - 5;
2456    rect.width = 10;
2457    rect.height = 10;
2458    ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
2459
2460    return EINA_TRUE;
2461 }
2462
2463 /**
2464  * When dragging this is callback response from the destination.
2465  * The important thing we care about: Can we drop; thus update cursor
2466  * appropriately.
2467  */
2468 static Eina_Bool
2469 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
2470 {
2471    struct _Ecore_X_Event_Xdnd_Status *status = ev;
2472
2473    if (!status) return EINA_TRUE;
2474
2475    /* Only thing we care about: will accept */
2476    if (status->will_accept)
2477      {
2478         cnp_debug("Will accept\n");
2479      }
2480    else
2481      { /* Won't accept */
2482         cnp_debug("Won't accept accept\n");
2483      }
2484    return EINA_TRUE;
2485 }
2486
2487 /**
2488  * Add a widget as drop target.
2489  */
2490 Eina_Bool
2491 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2492 {
2493    Dropable *drop;
2494    Ecore_X_Window xwin;
2495    Eina_List *item;
2496    int first;
2497
2498    if (!obj) return EINA_FALSE;
2499    if (!_elm_cnp_init_count) _elm_cnp_init();
2500
2501    /* Is this the first? */
2502    first = (!drops) ? 1 : 0;
2503
2504    EINA_LIST_FOREACH(drops, item, drop)
2505      {
2506         if (drop->obj == obj)
2507           {
2508              /* Update: Not a new one */
2509              drop->dropcb = dropcb;
2510              drop->cbdata = cbdata;
2511              drop->types = format;
2512              return EINA_TRUE;
2513           }
2514      }
2515
2516    /* Create new drop */
2517    drop = calloc(1, sizeof(Dropable));
2518    if (!drop) return EINA_FALSE;
2519    /* FIXME: Check for eina's deranged error method */
2520    drops = eina_list_append(drops, drop);
2521
2522    if (!drops/* || or other error */)
2523      {
2524         free(drop);
2525         return EINA_FALSE;
2526      }
2527    drop->dropcb = dropcb;
2528    drop->cbdata = cbdata;
2529    drop->types = format;
2530    drop->obj = obj;
2531
2532    evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
2533                                   /* I love C and varargs */
2534                                   (Evas_Object_Event_Cb)elm_drop_target_del,
2535                                   obj);
2536    /* FIXME: Handle resizes */
2537
2538    /* If not the first: We're done */
2539    if (!first) return EINA_TRUE;
2540
2541    xwin = (Ecore_X_Window)ecore_evas_window_get
2542       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2543
2544    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
2545
2546    cnp_debug("Adding drop target calls\n");
2547    handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
2548                                            _dnd_enter, NULL);
2549    handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
2550                                          _dnd_position, NULL);
2551    handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
2552                                           _dnd_drop, NULL);
2553
2554    return EINA_TRUE;
2555 }
2556
2557 Eina_Bool
2558 elm_drop_target_del(Evas_Object *obj)
2559 {
2560    Dropable *drop,*del;
2561    Eina_List *item;
2562    Ecore_X_Window xwin;
2563
2564    del = NULL;
2565    EINA_LIST_FOREACH(drops, item, drop)
2566      {
2567         if (drop->obj == obj)
2568           {
2569              drops = eina_list_remove_list(drops, item);
2570              del = drop;
2571              break;
2572           }
2573      }
2574    if (!del) return EINA_FALSE;
2575
2576    evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
2577                                   (Evas_Object_Event_Cb)elm_drop_target_del);
2578    free(drop);
2579    /* If still drops there: All fine.. continue */
2580    if (drops) return EINA_TRUE;
2581
2582    cnp_debug("Disabling DND\n");
2583    xwin = (Ecore_X_Window)ecore_evas_window_get
2584       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
2585    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
2586
2587    ecore_event_handler_del(handler_pos);
2588    ecore_event_handler_del(handler_drop);
2589    ecore_event_handler_del(handler_enter);
2590
2591    if (savedtypes.pi)
2592      {
2593         pasteimage_free(savedtypes.pi);
2594         savedtypes.pi = NULL;
2595      }
2596
2597    return EINA_TRUE;
2598 }
2599
2600
2601 static void
2602 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
2603 {
2604    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
2605    ecore_x_dnd_drop();
2606    if (dragdonecb)
2607      {
2608         dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
2609         dragdonecb = NULL;
2610      }
2611    if (dragwin)
2612      {
2613         evas_object_del(dragwin);
2614         dragwin = NULL;
2615      }
2616 }
2617
2618 static void
2619 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
2620 {
2621    evas_object_move(dragwin,
2622                     pos->position.x - _dragx,
2623                     pos->position.y - _dragy);
2624 }
2625
2626
2627 Eina_Bool
2628 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
2629 {
2630    Ecore_X_Window xwin;
2631    Cnp_Selection *sel;
2632    Elm_Sel_Type xdnd = ELM_SEL_XDND;
2633    Ecore_Evas *ee;
2634    int x, y, x2, y2, x3, y3;
2635    Evas_Object *icon;
2636    int w, h;
2637
2638    if (!_elm_cnp_init_count) _elm_cnp_init();
2639
2640    xwin = elm_win_xwindow_get(obj);
2641
2642    cnp_debug("starting drag...\n");
2643
2644    ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
2645    sel = selections + ELM_SEL_XDND;
2646    sel->active = 1;
2647    sel->widget = obj;
2648    sel->format = format;
2649    sel->selbuf = data ? strdup(data) : NULL;
2650    dragdonecb = dragdone;
2651    dragdonedata = donecbdata;
2652
2653    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
2654    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
2655    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
2656                                   _drag_mouse_up, NULL);
2657
2658    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
2659                                             _dnd_status, NULL);
2660
2661    dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
2662    elm_win_override_set(dragwin, 1);
2663
2664    /* FIXME: Images only */
2665    icon = elm_icon_add(dragwin);
2666    elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
2667    elm_win_resize_object_add(dragwin,icon);
2668    evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2669    evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
2670
2671    /* Position subwindow appropriately */
2672    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
2673    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
2674    evas_object_geometry_get(obj, &x2, &y2, &w, &h);
2675    x += x2;
2676    y += y2;
2677    evas_object_move(dragwin, x, y);
2678    evas_object_resize(icon, w, h);
2679    evas_object_resize(dragwin, w, h);
2680
2681    evas_object_show(icon);
2682    evas_object_show(dragwin);
2683
2684    evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
2685    _dragx = x3 - x2;
2686    _dragy = y3 - y2;
2687
2688    return EINA_TRUE;
2689 }
2690
2691 static Tmp_Info *
2692 elm_cnp_tempfile_create(int size)
2693 {
2694    Tmp_Info *info;
2695    const char *tmppath;
2696    int len;
2697
2698    info = malloc(sizeof(Tmp_Info));
2699    if (!info) return NULL;
2700
2701    tmppath = getenv("TMP");
2702    if (!tmppath) tmppath = P_tmpdir;
2703    if (!tmppath) tmppath = "/tmp";
2704    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
2705    if (len < 0)
2706      {
2707         free(info);
2708         return NULL;
2709      }
2710    len++;
2711    info->filename = malloc(len);
2712    if (!info->filename)
2713      {
2714         free(info);
2715         return NULL;
2716      }
2717    snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
2718
2719    info->fd = mkstemp(info->filename);
2720
2721 # ifdef __linux__
2722      {
2723         char *tmp;
2724         /* And before someone says anything see POSIX 1003.1-2008 page 400 */
2725         long pid;
2726
2727         pid = (long)getpid();
2728         /* Use pid instead of /proc/self: That way if can be passed around */
2729         len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
2730         len++;
2731         tmp = malloc(len);
2732         if (tmp)
2733           {
2734              snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
2735              unlink(info->filename);
2736              free(info->filename);
2737              info->filename = tmp;
2738           }
2739      }
2740 # endif
2741
2742    cnp_debug("filename is %s\n", info->filename);
2743    if (size < 1)
2744      {
2745         /* Set map to NULL and return */
2746         info->map = NULL;
2747         info->len = 0;
2748         return info;
2749      }
2750
2751    /* Map it in */
2752    if (ftruncate(info->fd, size))
2753      {
2754         perror("ftruncate");
2755         info->map = NULL;
2756         info->len = 0;
2757         return info;
2758      }
2759
2760    eina_mmap_safety_enabled_set(EINA_TRUE);
2761
2762    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
2763    if (info->map == MAP_FAILED)
2764      {
2765         perror("mmap");
2766         info->map = NULL;
2767         info->len = 0;
2768      }
2769
2770    return info;
2771 }
2772
2773
2774 static int
2775 tmpinfo_free(Tmp_Info *info)
2776 {
2777    if (!info) return 0;
2778    free(info->filename);
2779    free(info);
2780    return 0;
2781 }
2782
2783 #else
2784 /* Stubs for windows */
2785 Eina_Bool
2786 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
2787 {
2788    return EINA_FALSE;
2789 }
2790
2791 Eina_Bool
2792 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2793 {
2794    return EINA_FALSE;
2795 }
2796
2797 Eina_Bool
2798 elm_drop_target_del(Evas_Object *o)
2799 {
2800    return EINA_TRUE;
2801 }
2802 #endif
2803
2804 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/