[elm_cnp_helper, elm_entry] modify for CBHM refactoring
[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_ATOM,
39    CNP_ATOM_LISTING_ATOMS = CNP_ATOM_ATOM,
40    CNP_ATOM_text_uri,
41    CNP_ATOM_text_urilist,
42    CNP_ATOM_text_x_vcard,
43    CNP_ATOM_image_png,
44    CNP_ATOM_image_jpeg,
45    CNP_ATOM_image_bmp,
46    CNP_ATOM_image_gif,
47    CNP_ATOM_image_tiff,
48    CNP_ATOM_image_svg,
49    CNP_ATOM_image_xpm,
50    CNP_ATOM_image_tga,
51    CNP_ATOM_image_ppm,
52    CNP_ATOM_XELM,
53    CNP_ATOM_text_html_utf8,
54    CNP_ATOM_text_html,
55    CNP_ATOM_UTF8STRING,
56    CNP_ATOM_STRING,
57    CNP_ATOM_TEXT,
58    CNP_ATOM_text_plain_utf8,
59    CNP_ATOM_text_plain,
60
61    CNP_N_ATOMS,
62 };
63
64 struct _Paste_Image
65 {
66    Evas_Object *entry;
67    const char  *tag;
68    const char  *file;
69    Evas_Object *img;
70 };
71
72 struct _Cnp_Selection
73 {
74    const char      *debug;
75    Evas_Object     *widget;
76    char            *selbuf;
77    Evas_Object     *requestwidget;
78    void            *udata;
79    Elm_Sel_Format   requestformat;
80    Elm_Drop_Cb      datacb;
81    Eina_Bool      (*set)     (Ecore_X_Window, const void *data, int size);
82    Eina_Bool      (*clear)   (void);
83    void           (*request) (Ecore_X_Window, const char *target);
84
85    Elm_Sel_Format    format;
86    Ecore_X_Selection ecore_sel;
87
88    Eina_Bool         active : 1;
89 };
90
91 struct _Escape
92 {
93    const char *escape;
94    const char  *value;
95 };
96
97 struct _Tmp_Info
98 {
99    char *filename;
100    void *map;
101    int   fd;
102    int   len;
103 };
104
105 struct _Cnp_Atom
106 {
107    const char          *name;
108    Elm_Sel_Format       formats;
109    /* Called by ecore to do conversion */
110    Converter_Fn_Cb      converter;
111    Response_Handler_Cb  response;
112    Notify_Handler_Cb    notify;
113    /* Atom */
114    Ecore_X_Atom         atom;
115 };
116
117 struct _Saved_Type
118 {
119    const char  **types;
120    Paste_Image  *pi;
121    int           ntypes;
122    int           x, y;
123    Eina_Bool     textreq: 1;
124 };
125
126 struct _Dropable
127 {
128    Evas_Object     *obj;
129    /* FIXME: Cache window */
130    Elm_Sel_Format   types;
131    Elm_Drop_Cb      dropcb;
132    void            *cbdata;
133 };
134
135 static Tmp_Info *elm_cnp_tempfile_create(int size);
136 static int tmpinfo_free(Tmp_Info *tmp);
137
138 static Eina_Bool _elm_cnp_init(void);
139 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
140 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
141 static char *remove_tags(const char *p, int *len);
142 static char *mark_up(const char *start, int inlen, int *lenp);
143
144 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
145 static void entry_deleted(void *images, Evas *e, Evas_Object *entry, void *unused);
146
147 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
148 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
149 static Eina_Bool general_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 #define _PARAGRAPH_SEPARATOR "\xE2\x80\xA9"
169
170 /* Optimisation: Turn this into a 256 byte table:
171  *      then can lookup in one index, not N checks */
172 static const Escape escapes[] = {
173   { "<ps>",  _PARAGRAPH_SEPARATOR },
174   { "<br>",  "\n" },
175   { "<\t>",  "\t" },
176   { "&gt;",   ">" },
177   { "&lt;",    "<" },
178   { "&amp;",   "&" },
179   { "&quot;",  "'" },
180   { "&dquot;", "\"" }
181 };
182 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
183
184 static Cnp_Atom atoms[CNP_N_ATOMS] = {
185      [CNP_ATOM_TARGETS] = {
186           "TARGETS",
187           (Elm_Sel_Format) -1, // everything
188           targets_converter,
189           response_handler_targets,
190           notify_handler_targets,
191           0
192      },
193      [CNP_ATOM_ATOM] = {
194           "ATOM",
195           (Elm_Sel_Format) -1, // everything
196           targets_converter,
197           response_handler_targets,
198           notify_handler_targets,
199           0
200      },
201      [CNP_ATOM_XELM] =  {
202           "application/x-elementary-markup",
203           ELM_SEL_FORMAT_MARKUP,
204           general_converter,
205           NULL,
206           notify_handler_edje,
207           0
208      },
209      [CNP_ATOM_text_uri] = {
210           "text/uri",
211           ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
212           general_converter,
213           NULL,
214           notify_handler_uri,
215           0
216      },
217      [CNP_ATOM_text_urilist] = {
218           "text/uri-list",
219           ELM_SEL_FORMAT_IMAGE,
220           general_converter,
221           NULL,
222           notify_handler_uri,
223           0
224      },
225      [CNP_ATOM_text_x_vcard] = {
226           "text/x-vcard",
227           ELM_SEL_FORMAT_VCARD,
228           vcard_send, NULL,
229           vcard_receive, 0
230      },
231      [CNP_ATOM_image_png] = {
232           "image/png",
233           ELM_SEL_FORMAT_IMAGE,
234           image_converter,
235           NULL,
236           notify_handler_image,
237           0
238      },
239      [CNP_ATOM_image_jpeg] = {
240           "image/jpeg",
241           ELM_SEL_FORMAT_IMAGE,
242           image_converter,
243           NULL,
244           notify_handler_image,/* Raw image data is the same */
245           0
246      },
247      [CNP_ATOM_image_bmp] = {
248           "image/x-ms-bmp",
249           ELM_SEL_FORMAT_IMAGE,
250           image_converter,
251           NULL,
252           notify_handler_image,/* Raw image data is the same */
253           0
254      },
255      [CNP_ATOM_image_gif] = {
256           "image/gif",
257           ELM_SEL_FORMAT_IMAGE,
258           image_converter,
259           NULL,
260           notify_handler_image,/* Raw image data is the same */
261           0
262      },
263      [CNP_ATOM_image_tiff] = {
264           "image/tiff",
265           ELM_SEL_FORMAT_IMAGE,
266           image_converter,
267           NULL,
268           notify_handler_image,/* Raw image data is the same */
269           0
270      },
271      [CNP_ATOM_image_svg] = {
272           "image/svg+xml",
273           ELM_SEL_FORMAT_IMAGE,
274           image_converter,
275           NULL,
276           notify_handler_image,/* Raw image data is the same */
277           0
278      },
279      [CNP_ATOM_image_xpm] = {
280           "image/x-xpixmap",
281           ELM_SEL_FORMAT_IMAGE,
282           image_converter,
283           NULL,
284           notify_handler_image,/* Raw image data is the same */
285           0
286      },
287      [CNP_ATOM_image_tga] = {
288           "image/x-tga",
289           ELM_SEL_FORMAT_IMAGE,
290           image_converter,
291           NULL,
292           notify_handler_image,/* Raw image data is the same */
293           0
294      },
295      [CNP_ATOM_image_ppm] = {
296           "image/x-portable-pixmap",
297           ELM_SEL_FORMAT_IMAGE,
298           image_converter,
299           NULL,
300           notify_handler_image,/* Raw image data is the same */
301           0
302      },
303      [CNP_ATOM_text_html_utf8] = {
304           "text/html;charset=utf-8",
305           ELM_SEL_FORMAT_HTML,
306           general_converter,
307           NULL,
308           notify_handler_html,
309           0
310      },
311      [CNP_ATOM_text_html] = {
312           "text/html",
313           ELM_SEL_FORMAT_HTML,
314           general_converter,
315           NULL,
316           notify_handler_html, /* No encoding: Webkit only */
317           0
318      },
319      [CNP_ATOM_UTF8STRING] = {
320           "UTF8_STRING",
321           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
322           text_converter,
323           NULL,
324           notify_handler_text,
325           0
326      },
327      [CNP_ATOM_STRING] = {
328           "STRING",
329           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
330           text_converter,
331           NULL,
332           notify_handler_text,
333           0
334      },
335      [CNP_ATOM_TEXT] = {
336           "TEXT",
337           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
338           text_converter,
339           NULL,
340           NULL,
341           0
342      },
343      [CNP_ATOM_text_plain_utf8] = {
344           "text/plain;charset=utf-8",
345           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
346           text_converter,
347           NULL,
348           NULL,
349           0
350      },
351      [CNP_ATOM_text_plain] = {
352           "text/plain",
353           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
354           text_converter,
355           NULL,
356           NULL,
357           0
358      },
359 };
360
361 static Cnp_Selection selections[ELM_SEL_MAX] = {
362      ARRAYINIT(ELM_SEL_PRIMARY) {
363           .debug = "Primary",
364           .ecore_sel = ECORE_X_SELECTION_PRIMARY,
365           .set = ecore_x_selection_primary_set,
366           .clear = ecore_x_selection_primary_clear,
367           .request = ecore_x_selection_primary_request,
368      },
369      ARRAYINIT(ELM_SEL_SECONDARY) {
370           .debug = "Secondary",
371           .ecore_sel = ECORE_X_SELECTION_SECONDARY,
372           .set = ecore_x_selection_secondary_set,
373           .clear = ecore_x_selection_secondary_clear,
374           .request = ecore_x_selection_secondary_request,
375      },
376      ARRAYINIT(ELM_SEL_CLIPBOARD) {
377           .debug = "Clipboard",
378           .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
379           .set = ecore_x_selection_clipboard_set,
380           .clear = ecore_x_selection_clipboard_clear,
381           .request = ecore_x_selection_clipboard_request,
382      },
383      ARRAYINIT(ELM_SEL_XDND) {
384           .debug = "XDnD",
385           .ecore_sel = ECORE_X_SELECTION_XDND,
386           .request = ecore_x_selection_xdnd_request,
387      },
388 };
389
390 /* Data for DND in progress */
391 static Saved_Type savedtypes =  { NULL, NULL, 0, 0, 0, EINA_FALSE };
392
393 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
394 static void *dragdonedata = NULL;
395
396 static int _elm_cnp_init_count = 0;
397
398 static Eina_List *pastedimages = NULL;
399
400 /**
401  * Drag & Drop functions
402  */
403
404 /* FIXME: Way too many globals */
405 static Eina_List *drops = NULL;
406 static Evas_Object *dragwin = NULL;
407 static int _dragx = 0, _dragy = 0;
408 static Ecore_Event_Handler *handler_pos = NULL;
409 static Ecore_Event_Handler *handler_drop = NULL;
410 static Ecore_Event_Handler *handler_enter = NULL;
411 static Ecore_Event_Handler *handler_status = NULL;
412
413 #endif
414
415 /* Stringshared, so I can just compare pointers later */
416 static const char *text_uri;
417
418 Eina_Bool
419 elm_selection_selection_has_owner(void)
420 {
421 #ifdef HAVE_ELEMENTARY_X
422    return !!ecore_x_selection_owner_get(ECORE_X_ATOM_SELECTION_CLIPBOARD);
423 #else
424    return EINA_FALSE;
425 #endif
426 }
427
428 Eina_Bool
429 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
430 {
431 #ifdef HAVE_ELEMENTARY_X
432    Evas_Object *top = elm_widget_top_get(widget);
433    Ecore_X_Window xwin;
434    Cnp_Selection *sel;
435
436    if (top) xwin = elm_win_xwindow_get(top);
437    else xwin = elm_win_xwindow_get(widget);
438    if (!xwin) return EINA_FALSE;
439    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
440    if (!_elm_cnp_init_count) _elm_cnp_init();
441    if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
442      return elm_selection_clear(selection, widget);
443
444    sel = selections + selection;
445
446    sel->active = 1;
447    sel->widget = widget;
448
449    sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
450    sel->format = format;
451    sel->selbuf = selbuf ? strdup(selbuf) : NULL;
452
453    return EINA_TRUE;
454 #else
455    return EINA_FALSE;
456 #endif
457 }
458
459 Eina_Bool
460 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
461 {
462 #ifdef HAVE_ELEMENTARY_X
463    Cnp_Selection *sel;
464
465    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
466    if (!_elm_cnp_init_count) _elm_cnp_init();
467
468    sel = selections + selection;
469
470    /* No longer this selection: Consider it gone! */
471    if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
472
473    sel->active = 0;
474    sel->widget = NULL;
475    sel->clear();
476
477    return EINA_TRUE;
478 #else
479    return EINA_FALSE;
480 #endif
481 }
482
483 Eina_Bool
484 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
485                   Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
486 {
487 #ifdef HAVE_ELEMENTARY_X
488    Evas_Object *top;
489    Cnp_Selection *sel;
490
491    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
492    if (!_elm_cnp_init_count) _elm_cnp_init();
493
494    sel = selections + selection;
495    top = elm_widget_top_get(widget);
496    if (!top) return EINA_FALSE;
497
498    sel->requestformat = format;
499    sel->requestwidget = widget;
500    sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
501    sel->datacb = datacb;
502    sel->udata = udata;
503
504    return EINA_TRUE;
505 #else
506    return EINA_FALSE;
507 #endif
508 }
509
510 #ifdef HAVE_ELEMENTARY_X
511
512 static Eina_Bool
513 _elm_cnp_init(void)
514 {
515    int i;
516
517    if (_elm_cnp_init_count++) return EINA_TRUE;
518    for (i = 0; i < CNP_N_ATOMS; i++)
519      {
520         atoms[i].atom = ecore_x_atom_get(atoms[i].name);
521         ecore_x_selection_converter_atom_add(atoms[i].atom,
522                                              atoms[i].converter);
523      }
524
525    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
526    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
527
528    text_uri = eina_stringshare_add("text/uri-list");
529    return EINA_TRUE;
530 }
531
532 static Eina_Bool
533 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
534 {
535    Ecore_X_Event_Selection_Clear *ev = event;
536    Cnp_Selection *sel;
537    int i;
538
539    for (i = 0; i < ELM_SEL_MAX; i++)
540      {
541         if (selections[i].ecore_sel == ev->selection) break;
542      }
543    cnp_debug("selection %d clear\n", i);
544    /* Not me... Don't care */
545    if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
546
547    sel = selections + i;
548    sel->active = 0;
549    sel->widget = NULL;
550    sel->selbuf = NULL;
551
552    return ECORE_CALLBACK_PASS_ON;
553 }
554
555
556 /*
557  * Response to a selection notify:
558  *      - So we have asked for the selection list.
559  *      - If it's the targets list, parse it, and fire of what we want,
560  *      else it's the data we want.
561  */
562 static Eina_Bool
563 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
564 {
565    Ecore_X_Event_Selection_Notify *ev = event;
566    Cnp_Selection *sel;
567    int i;
568
569    cnp_debug("selection notify callback: %d\n",ev->selection);
570    switch (ev->selection)
571      {
572       case ECORE_X_SELECTION_CLIPBOARD:
573          sel = selections + ELM_SEL_CLIPBOARD;
574          break;
575       case ECORE_X_SELECTION_PRIMARY:
576          sel = selections + ELM_SEL_PRIMARY;
577          break;
578       case ECORE_X_SELECTION_SECONDARY:
579          sel = selections + ELM_SEL_SECONDARY;
580          break;
581       case ECORE_X_SELECTION_XDND:
582          sel = selections + ELM_SEL_XDND;
583          break;
584       default:
585          return ECORE_CALLBACK_PASS_ON;
586      }
587    cnp_debug("Target is %s\n", ev->target);
588
589    for (i = 0; i < CNP_N_ATOMS; i++)
590      {
591         if (!strcmp(ev->target, atoms[i].name))
592           {
593              if (atoms[i].notify)
594                {
595                   cnp_debug("Found something: %s\n", atoms[i].name);
596                   atoms[i].notify(sel, ev);
597                }
598              else
599                {
600                   cnp_debug("Ignored: No handler!\n");
601                }
602           }
603      }
604
605    return ECORE_CALLBACK_PASS_ON;
606 }
607
608
609
610 static Eina_Bool
611 targets_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
612 {
613    int i,count;
614    Ecore_X_Atom *aret;
615    Cnp_Selection *sel;
616
617    if (!data_ret) return EINA_FALSE;
618
619    if (size != sizeof(int))
620      {
621         if (data_ret)
622           {
623              *data_ret = malloc(size * sizeof(char) + 1);
624              memcpy(*data_ret, data, size);
625              ((char**)(data_ret))[0][size] = 0;
626           }
627         if (size_ret) *size_ret = size;
628         return EINA_TRUE;
629      }
630
631    if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
632      return EINA_FALSE;
633    sel = selections + *((int *)data);
634
635    for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
636      {
637         if (sel->format & atoms[i].formats) count++;
638      }
639
640    aret = malloc(sizeof(Ecore_X_Atom) * count);
641    for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
642      {
643         if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
644      }
645
646    *data_ret = aret;
647    if (typesize) *typesize = 32 /* urk */;
648    if (ttype) *ttype = ECORE_X_ATOM_ATOM;
649    if (size_ret) *size_ret = count;
650
651    return EINA_TRUE;
652 }
653
654 static Eina_Bool
655 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__)
656 {
657    cnp_debug("Image converter called\n");
658    return EINA_TRUE;
659 }
660
661 static Eina_Bool
662 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__)
663 {
664    Cnp_Selection *sel;
665
666    cnp_debug("Vcard send called\n");
667
668    if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
669      return EINA_FALSE;
670    sel = selections + *((int *)data);
671
672    if (data_ret) *data_ret = strdup(sel->selbuf);
673    if (size_ret) *size_ret = strlen(sel->selbuf);
674
675    return EINA_TRUE;
676 }
677
678 #if 0
679 static Eina_Bool
680 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
681 {
682    Ecore_X_Selection_Data *data;
683    char *p;
684
685    data = notify->data;
686    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
687    if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
688    else p = (char *)data->data;
689
690    if (!p) return EINA_TRUE;
691    cnp_debug("Got %s\n", p);
692    if (strncmp(p, "file://", 7))
693      {
694         if (*p != '/') return EINA_FALSE;
695      }
696
697    return EINA_TRUE;
698 }
699 #endif
700 /*
701  * Callback to handle a targets response on a selection request:
702  * So pick the format we'd like; and then request it.
703  */
704 static int
705 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
706 {
707    Ecore_X_Selection_Data_Targets *targets;
708    Ecore_X_Atom *atomlist;
709    Evas_Object *top;
710    int i, j;
711
712    targets = notify->data;
713    atomlist = (Ecore_X_Atom *)(targets->data.data);
714
715    for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
716      {
717         cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
718         if (!(atoms[j].formats & sel->requestformat)) continue;
719         for (i = 0; i < targets->data.length; i++)
720           {
721              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
722                {
723 #if 0
724                   if ((j == CNP_ATOM_text_uri) ||
725                       (j == CNP_ATOM_text_urilist))
726                     {
727                       if(!is_uri_type_data(sel, notify)) continue;
728                     }
729 #endif
730                   cnp_debug("Atom %s matches\n",atoms[j].name);
731                   goto done;
732                }
733           }
734      }
735
736    cnp_debug("Couldn't find anything that matches\n");
737    return ECORE_CALLBACK_PASS_ON;
738
739 done:
740    top = elm_widget_top_get(sel->requestwidget);
741    if (!top) top = sel->requestwidget;
742    cnp_debug("Sending request for %s\n", atoms[j].name);
743    sel->request(elm_win_xwindow_get(top), atoms[j].name);
744
745    return ECORE_CALLBACK_PASS_ON;
746 }
747
748 static int
749 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
750 {
751    Ecore_X_Selection_Data_Targets *targets;
752    Ecore_X_Atom *atomlist;
753    Evas_Object *top;
754    int i,j;
755
756    targets = notify->data;
757    atomlist = (Ecore_X_Atom *)(targets->data.data);
758
759    /* Start from 1: Skip targets */
760    for (j = 1 ; j < CNP_N_ATOMS ; j ++)
761      {
762         if (!(atoms[j].formats & sel->requestformat)) continue;
763         for (i = 0 ; i < targets->data.length ; i ++)
764           {
765              if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
766                {
767                   /* Found a match: Use it */
768                   goto found;
769                }
770           }
771      }
772 found:
773    if (j == CNP_N_ATOMS)
774      {
775         cnp_debug("No matching type found\n");
776         return 0;
777      }
778
779    top = elm_widget_top_get(sel->requestwidget);
780    if (!top) return 0;
781
782    sel->request(elm_win_xwindow_get(top), atoms[j].name);
783    return 0;
784 }
785
786 static void
787 entry_insert_filter(Evas_Object* entry, char* str)
788 {
789    if (!entry || !str)
790      return;
791
792    char *insertStr = str;
793    // if entry has text only set then remove item tags
794    if (elm_entry_cnp_mode_get(entry) != ELM_CNP_MODE_MARKUP)
795      {
796         while (EINA_TRUE)
797           {
798              char *startTag = NULL;
799              char *endTag = NULL;
800
801              startTag = strstr(insertStr, "<item");
802              if (!startTag)
803                startTag = strstr(insertStr, "</item");
804              if (startTag)
805                endTag = strstr(startTag, ">");
806              else
807                break;
808              if (!endTag || startTag > endTag)
809                {
810                   cnp_debug("Broken tag: %s\n", str);
811                   break;
812                }
813
814              size_t sindex = startTag - insertStr;
815              size_t eindex = endTag - insertStr + 1;
816
817              Eina_Strbuf *buf = eina_strbuf_new();
818              if (buf)
819                {
820                   eina_strbuf_append(buf, insertStr);
821                   eina_strbuf_remove(buf, sindex, eindex);
822                   insertStr = eina_strbuf_string_steal(buf);
823                   eina_strbuf_free(buf);
824                }
825           }
826      }
827    cnp_debug("remove item tag: %s\n", insertStr);
828
829    // if entry has single line set then remove <br> & <ps> tags
830    if (elm_entry_single_line_get(entry))
831      {
832         Eina_Strbuf *buf = eina_strbuf_new();
833         if (buf)
834           {
835              eina_strbuf_append(buf, insertStr);
836              eina_strbuf_replace_all(buf, "<br>", "");
837              eina_strbuf_replace_all(buf, "<ps>", "");
838              insertStr = eina_strbuf_string_steal(buf);
839              eina_strbuf_free(buf);
840           }
841      }
842    cnp_debug("remove break tag: %s\n", insertStr);
843
844    elm_entry_entry_insert(entry, insertStr);
845    // TODO:
846    //_elm_entry_entry_paste//
847
848    if (insertStr != str)
849      free(insertStr);
850 }
851
852 static int
853 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
854 {
855    Ecore_X_Selection_Data *data;
856    char *str;
857
858    data = notify->data;
859
860    if (sel->datacb)
861      {
862         Elm_Selection_Data ddata;
863         char *str = NULL;
864         str = malloc(sizeof(char) * (data->length + 1));
865         strncpy(str, (char *)data->data, data->length);
866         str[data->length] = '\0';
867
868         ddata.x = ddata.y = 0;
869         ddata.format = ELM_SEL_FORMAT_TEXT;
870         ddata.data = str;
871         ddata.len = data->length;
872         sel->datacb(sel->udata, sel->widget, &ddata);
873         free(str);
874         return 0;
875      }
876
877    cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
878    str = mark_up((char *)data->data, data->length, NULL);
879    cnp_debug("String is %s (from %s)\n", str, data->data);
880    entry_insert_filter(sel->requestwidget, str);
881    //elm_entry_entry_insert(sel->requestwidget, str);
882    free(str);
883    return 0;
884 }
885
886
887 /**
888  * So someone is pasting an image into my entry or widget...
889  */
890 static int
891 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
892 {
893    Ecore_X_Selection_Data *data;
894    Ecore_X_Selection_Data_Files *files;
895    Paste_Image *pi;
896    char *p, *stripstr;
897
898    data = notify->data;
899    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
900    if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
901      {
902         cnp_debug("got a files list\n");
903         files = notify->data;
904         if (files->num_files > 1)
905           {
906              /* Don't handle many items */
907              cnp_debug("more then one file: Bailing\n");
908              return 0;
909           }
910         stripstr = p = strdup(files->files[0]);
911      }
912    else
913      {
914         stripstr = p = malloc(data->length * sizeof(char) + 1);
915         memcpy(stripstr, data->data, data->length);
916         stripstr[data->length] = 0;
917      }
918
919    if (!p)
920      {
921         cnp_debug("Couldn't find a file\n");
922         return 0;
923      }
924    cnp_debug("Got %s\n",p);
925    if (sel->datacb)
926      {
927         Elm_Selection_Data ddata;
928
929         ddata.x = ddata.y = 0;
930         ddata.format = ELM_SEL_FORMAT_IMAGE;
931         ddata.data = p;
932         ddata.len = data->length;
933         sel->datacb(sel->udata, sel->widget, &ddata);
934         free(p);
935         return 0;
936      }
937    if (strncmp(p, "file://", 7))
938      {
939         /* Try and continue if it looks sane */
940         if (*p != '/')
941           {
942              free(p);
943              return 0;
944           }
945      }
946    else
947      {
948         p += strlen("file://");
949      }
950
951    if (savedtypes.pi) pasteimage_free(savedtypes.pi);
952    pi = pasteimage_alloc(p, strlen(p));
953    if (savedtypes.textreq)
954      {
955         savedtypes.textreq = 0;
956         savedtypes.pi = pi;
957      }
958    else
959      {
960         pasteimage_append(pi, sel->requestwidget);
961         savedtypes.pi = NULL;
962      }
963    free(stripstr);
964    return 0;
965 }
966
967 /**
968  * Just receieved an vcard, either through cut and paste, or dnd.
969  */
970 static int
971 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
972 {
973    Dropable *dropable;
974    Eina_List *l;
975    Ecore_X_Selection_Data *data;
976
977    data = notify->data;
978    cnp_debug("vcard receive\n");
979
980    if (sel == (selections + ELM_SEL_XDND))
981      {
982         Elm_Selection_Data ddata;
983
984         cnp_debug("drag & drop\n");
985         /* FIXME: this needs to be generic: Used for all receives */
986         EINA_LIST_FOREACH(drops, l, dropable)
987           {
988              if (dropable->obj == sel->requestwidget) break;
989           }
990         if (!dropable)
991           {
992              cnp_debug("Unable to find drop object");
993              ecore_x_dnd_send_finished();
994              return 0;
995           }
996         dropable = eina_list_data_get(l);
997         ddata.x = savedtypes.x;
998         ddata.y = savedtypes.y;
999         ddata.format = ELM_SEL_FORMAT_VCARD;
1000         ddata.data = data->data;
1001         ddata.len = data->length;
1002         dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1003         ecore_x_dnd_send_finished();
1004      }
1005    else if (sel->datacb)
1006      {
1007         Elm_Selection_Data ddata;
1008         ddata.x = ddata.y = 0;
1009         ddata.format = ELM_SEL_FORMAT_IMAGE;
1010         ddata.data = data->data;
1011         ddata.len = data->length;
1012         sel->datacb(sel->udata, sel->widget, &ddata);
1013      }
1014    else
1015      {
1016         cnp_debug("Paste request\n");
1017      }
1018
1019    return 0;
1020
1021 }
1022
1023 static int
1024 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1025 {
1026    Ecore_X_Selection_Data *data;
1027    Tmp_Info *tmp;
1028    Paste_Image *pi;
1029
1030    cnp_debug("got a png (or a jpeg)!\n");
1031    data = notify->data;
1032
1033    cnp_debug("Size if %d\n", data->length);
1034
1035    if (sel->datacb)
1036      {
1037         Elm_Selection_Data ddata;
1038
1039         ddata.x = ddata.y = 0;
1040         ddata.format = ELM_SEL_FORMAT_IMAGE;
1041         ddata.data = data->data;
1042         ddata.len = data->length;
1043         sel->datacb(sel->udata, sel->widget, &ddata);
1044         return 0;
1045      }
1046
1047    /* generate tmp name */
1048    tmp = elm_cnp_tempfile_create(data->length);
1049    memcpy(tmp->map, data->data, data->length);
1050    munmap(tmp->map,data->length);
1051
1052    /* FIXME: Add to paste image data to clean up */
1053    pi = pasteimage_alloc(tmp->filename, strlen(tmp->filename));
1054    pasteimage_append(pi, sel->requestwidget);
1055
1056    tmpinfo_free(tmp);
1057    return 0;
1058 }
1059
1060 static int
1061 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1062 {
1063    Ecore_X_Selection_Data *data;
1064
1065    data = notify->data;
1066
1067    char *stripstr = NULL;
1068    stripstr = malloc(sizeof(char) * (data->length + 1));
1069    strncpy(stripstr, (char *)data->data, data->length);
1070    stripstr[data->length] = '\0';
1071
1072    if (sel->datacb)
1073      {
1074         Elm_Selection_Data ddata;
1075         ddata.x = ddata.y = 0;
1076         ddata.format = ELM_SEL_FORMAT_MARKUP;
1077         ddata.data = stripstr;
1078         ddata.len = data->length;
1079         sel->datacb(sel->udata, sel->widget, &ddata);
1080      }
1081    else
1082      entry_insert_filter(sel->requestwidget, stripstr);
1083      //elm_entry_entry_insert(sel->requestwidget, stripstr);
1084
1085    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1086    free(stripstr);
1087    return 0;
1088 }
1089
1090 /**
1091  *    Warning: Generic text/html can';t handle it sanely.
1092  *    Firefox sends ucs2 (i think).
1093  *       chrome sends utf8... blerg
1094  */
1095 static int
1096 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1097 {
1098    Ecore_X_Selection_Data *data;
1099
1100    cnp_debug("Got some HTML: Checking encoding is useful\n");
1101    data = notify->data;
1102
1103    char *stripstr = NULL;
1104    stripstr = malloc(sizeof(char) * (data->length + 1));
1105    strncpy(stripstr, (char *)data->data, data->length);
1106    stripstr[data->length] = '\0';
1107
1108    if (sel->datacb)
1109      {
1110         Elm_Selection_Data ddata;
1111         ddata.x = ddata.y = 0;
1112         ddata.format = ELM_SEL_FORMAT_HTML;
1113         ddata.data = stripstr;
1114         ddata.len = data->length;
1115         sel->datacb(sel->udata, sel->widget, &ddata);
1116      }
1117    else
1118      entry_insert_filter(sel->requestwidget, stripstr);
1119      //elm_entry_entry_insert(sel->requestwidget, stripstr);
1120
1121    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1122    free(stripstr);
1123    return 0;
1124 }
1125
1126
1127 static Eina_Bool
1128 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1129 {
1130    Cnp_Selection *sel;
1131
1132    cnp_debug("text converter\n");
1133    if (size != sizeof(int))
1134      {
1135         if (data_ret)
1136           {
1137              *data_ret = malloc(size * sizeof(char) + 1);
1138              memcpy(*data_ret, data, size);
1139              ((char**)(data_ret))[0][size] = 0;
1140           }
1141         if (size_ret) *size_ret = size;
1142         return EINA_TRUE;
1143      }
1144    if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
1145      return EINA_FALSE;
1146    sel = selections + *((int *)data);
1147    if (!sel->active) return EINA_TRUE;
1148
1149    if (sel->format & ELM_SEL_FORMAT_MARKUP)
1150      *data_ret = remove_tags(sel->selbuf, size_ret);
1151    else if (sel->format & ELM_SEL_FORMAT_HTML)
1152      {
1153         char *text = NULL;
1154         Eina_Strbuf *buf = eina_strbuf_new();
1155         if (buf)
1156           {
1157              eina_strbuf_append(buf, sel->selbuf);
1158              eina_strbuf_replace_all(buf, "&nbsp;", " ");
1159              text = eina_strbuf_string_steal(buf);
1160              eina_strbuf_free(buf);
1161              *data_ret = remove_tags(text, size_ret);
1162              free(text);
1163           }
1164         else
1165           *data_ret = remove_tags(sel->selbuf, size_ret);
1166      }
1167    else if (sel->format & ELM_SEL_FORMAT_TEXT)
1168      {
1169         *data_ret = strdup(sel->selbuf);
1170         *size_ret = strlen(sel->selbuf);
1171      }
1172    else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1173      {
1174         cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1175         cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1176         evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1177         if (!*data_ret) *data_ret = strdup("No file");
1178         else *data_ret = strdup(*data_ret);
1179         *size_ret = strlen(*data_ret);
1180      }
1181    return EINA_TRUE;
1182 }
1183
1184 static Eina_Bool
1185 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1186 {
1187    if (size == sizeof(int))
1188      {
1189         Cnp_Selection *sel;
1190         if (!data || (*((unsigned int *)data) >= ELM_SEL_MAX))
1191           return EINA_FALSE;
1192
1193         sel = selections + *((int *)data);
1194         if (data_ret) *data_ret = strdup(sel->selbuf);
1195         if (size_ret) *size_ret = strlen(sel->selbuf);
1196      }
1197    else if (size)
1198      {
1199         if (data_ret)
1200           {
1201              *data_ret = malloc(size * sizeof(char) + 1);
1202              memcpy(*data_ret, data, size);
1203              ((char**)(data_ret))[0][size] = 0;
1204           }
1205         if (size_ret) *size_ret = size;
1206      }
1207    return EINA_TRUE;
1208 }
1209
1210 /*
1211  * Image paste provide
1212  */
1213
1214 /* FIXME: Should add provider for each pasted item: Use data to store it
1215  * much easier */
1216 static Evas_Object *
1217 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
1218 {
1219    Paste_Image *pi;
1220    Eina_List *l;
1221
1222    cnp_debug("image provider for %s called\n", item);
1223    EINA_LIST_FOREACH(pastedimages, l, pi)
1224      {
1225         cnp_debug("is it %s?\n",pi->tag);
1226         if (!strcmp(pi->tag, item))
1227           {
1228              /* Found it */
1229              Evas_Object *o;
1230              o = evas_object_image_filled_add(evas_object_evas_get(entry));
1231              /* FIXME: Handle eets */
1232              cnp_debug("file is %s (object is %p)\n", pi->file, o);
1233              evas_object_image_file_set(o, pi->file, NULL);
1234              evas_object_show(o);
1235              return o;
1236           }
1237      }
1238    return NULL;
1239 }
1240
1241
1242 static Paste_Image *
1243 pasteimage_alloc(const char *file, int pathlen)
1244 {
1245    Paste_Image *pi;
1246    int len;
1247    char *buf, *filebuf;
1248    int prefixlen = strlen("file://");
1249
1250    pi = calloc(1, sizeof(Paste_Image));
1251    if (!pi) return NULL;
1252
1253    len = snprintf(NULL, 0, "pasteimage-%p", pi);
1254    len++;
1255    buf = malloc(len);
1256    if (!buf)
1257      {
1258         free(pi);
1259         return NULL;
1260      }
1261    snprintf(buf, len, "pasteimage-%p", pi);
1262    pi->tag = buf;
1263
1264    if (file)
1265      {
1266         if (strstr(file,"file://")) file += prefixlen;
1267         filebuf = alloca(pathlen + 1);
1268         strncpy(filebuf, file, pathlen);
1269         filebuf[pathlen] = 0;
1270         pi->file = strdup(filebuf);
1271      }
1272
1273    return pi;
1274 }
1275
1276 static void
1277 pasteimage_free(Paste_Image *pi)
1278 {
1279    if (!pi) return;
1280    if (pi->file) free((void*)pi->file);
1281    if (pi->tag) free((void*)pi->tag);
1282    free(pi);
1283 }
1284
1285 static Eina_Bool
1286 pasteimage_provider_set(Evas_Object *entry)
1287 {
1288    void *v;
1289    const char *type;
1290
1291    if (!entry) return EINA_FALSE;
1292    type = elm_widget_type_get(entry);
1293    cnp_debug("type is %s\n", type);
1294    if ((!type) || (strcmp(type, "entry"))) return EINA_FALSE;
1295
1296    v = evas_object_data_get(entry, PROVIDER_SET);
1297    if (!v)
1298      {
1299         evas_object_data_set(entry, PROVIDER_SET, pasteimage_provider_set);
1300         elm_entry_item_provider_append(entry, image_provider, NULL);
1301         evas_object_event_callback_add(entry, EVAS_CALLBACK_FREE,
1302                                        entry_deleted, NULL);
1303      }
1304    return EINA_TRUE;
1305 }
1306
1307
1308 static Eina_Bool
1309 pasteimage_append(Paste_Image *pi, Evas_Object *entry)
1310 {
1311    char *entrytag;
1312    int len;
1313    static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1314
1315    if (!pi) return EINA_FALSE;
1316    if (!entry) return EINA_FALSE;
1317    if (elm_entry_cnp_mode_get(entry) != ELM_CNP_MODE_MARKUP) return EINA_FALSE;
1318
1319    pasteimage_provider_set(entry);
1320
1321    len = strlen(tagstring)+strlen(pi->file);
1322
1323    pastedimages = eina_list_append(pastedimages, pi);
1324    entrytag = alloca(len + 1);
1325    snprintf(entrytag, len + 1, tagstring, pi->file);
1326    elm_entry_entry_insert(entry, entrytag);
1327
1328    return EINA_TRUE;
1329 }
1330
1331 static void
1332 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
1333 {
1334    Paste_Image *pi;
1335    Eina_List *l,*next;
1336
1337    EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
1338      {
1339         if (pi->entry == entry)
1340           pastedimages = eina_list_remove_list(pastedimages, l);
1341      }
1342 }
1343
1344
1345 static char *
1346 remove_tags(const char *p, int *len)
1347 {
1348    char *q,*ret;
1349    int i;
1350    if (!p) return NULL;
1351
1352    q = malloc(strlen(p) + 1);
1353    if (!q) return NULL;
1354    ret = q;
1355
1356    while (*p)
1357      {
1358         Eina_Bool esc = EINA_TRUE;
1359         const char *x;
1360         switch (p[0])
1361           {
1362            case '<':
1363              x = strchr(p + 3, '>');
1364              if (!x)
1365                {
1366                   strcpy(q, p);
1367                   if (len) *len = strlen(ret);
1368                   return ret;
1369                }
1370              if (memcmp(p + 1, "br", 2) && memcmp(p + 1, "ps", 2))
1371                {
1372                   strncpy(q, p, x - p + 1);
1373                   p = x + 1;
1374                   break;
1375                }
1376              i = x - p - 1;
1377              if (p[i] == '/') i--;
1378              for (; i > 2; i++)
1379                {
1380                   if (p[i] != ' ')
1381                     {
1382                        esc = EINA_FALSE;
1383                        break;
1384                     }
1385                }
1386              if (!esc)
1387                {
1388                   strncpy(q, p, x - p + 1);
1389                   p = x + 1;
1390                   break;
1391                }
1392              if (p[1] == 'b')
1393                *q++ = '\n';
1394              else
1395                {
1396                   strcpy(q, _PARAGRAPH_SEPARATOR);
1397                   q += sizeof(_PARAGRAPH_SEPARATOR) - 1;
1398                }
1399              p = x + 1;
1400              break;
1401            case '&':
1402              for (i = 3 ; i < N_ESCAPES ; i++)
1403                {
1404                   if (strncmp(p, escapes[i].escape, strlen(escapes[i].escape)))
1405                     continue;
1406                   p += strlen(escapes[i].escape);
1407                   strcpy(q, escapes[i].value);
1408                   q += strlen(escapes[i].value);
1409                   break;
1410                }
1411              if (i == N_ESCAPES) *q ++= '&';
1412              break;
1413            default:
1414              *q++ = *p++;
1415           }
1416      }
1417    *q = 0;
1418    if (len) *len = q - ret;
1419    return ret;
1420 }
1421
1422 /* Mark up */
1423 static char *
1424 mark_up(const char *start, int inlen, int *lenp)
1425 {
1426    int l, i;
1427    const char *p;
1428    char *q, *ret;
1429    const char *endp = NULL;
1430
1431    if (!start) return NULL;
1432    if (inlen >= 0) endp = start + inlen;
1433    /* First pass: Count characters */
1434    for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1435      {
1436         for (i = 0 ; i < N_ESCAPES ; i ++)
1437           {
1438              if (*p == escapes[i].value[0])
1439                {
1440                   if (!strncmp(p, escapes[i].value, strlen(escapes[i].value)))
1441                     l += strlen(escapes[i].escape);
1442                   break;
1443                }
1444           }
1445         if (i == N_ESCAPES) l++;
1446      }
1447
1448    q = ret = malloc(l + 1);
1449
1450    /* Second pass: Change characters */
1451    for (p = start; ((!endp) || (p < endp)) && (*p); )
1452      {
1453         for (i = 0; i < N_ESCAPES; i++)
1454           {
1455              if (*p == escapes[i].value[0])
1456                {
1457                   if (!strncmp(p, escapes[i].value, strlen(escapes[i].value)))
1458                     {
1459                        strcpy(q, escapes[i].escape);
1460                        q += strlen(escapes[i].escape);
1461                        p += strlen(escapes[i].value);
1462                     }
1463                   break;
1464                }
1465           }
1466         if (i == N_ESCAPES) *q++ = *p++;
1467      }
1468    *q = 0;
1469
1470    if (lenp) *lenp = l;
1471    return ret;
1472 }
1473
1474
1475 static Eina_Bool
1476 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1477 {
1478    Ecore_X_Event_Xdnd_Enter *enter = ev;
1479    int i;
1480
1481    /* Skip it */
1482    if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1483
1484    cnp_debug("Types\n");
1485    savedtypes.ntypes = enter->num_types;
1486    if (savedtypes.types) free(savedtypes.types);
1487    savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1488    if (!savedtypes.types) return EINA_FALSE;
1489
1490    for (i = 0; i < enter->num_types; i++)
1491      {
1492         savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1493         cnp_debug("Type is %s %p %p\n", enter->types[i],
1494                   savedtypes.types[i],text_uri);
1495         if (savedtypes.types[i] == text_uri)
1496           {
1497              /* Request it, so we know what it is */
1498              cnp_debug("Sending uri request\n");
1499              savedtypes.textreq = 1;
1500              if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1501              savedtypes.pi = NULL; /* FIXME: Free? */
1502              ecore_x_selection_xdnd_request(enter->win, text_uri);
1503           }
1504      }
1505
1506    /* FIXME: Find an object and make it current */
1507    return EINA_TRUE;
1508 }
1509
1510 static Eina_Bool
1511 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1512 {
1513    struct _Ecore_X_Event_Xdnd_Drop *drop;
1514    Dropable *dropable;
1515    Eina_List *l;
1516    Ecore_Evas *ee;
1517    Ecore_X_Window xwin;
1518    Elm_Selection_Data ddata;
1519    int x, y, w, h;
1520    int i, j;
1521
1522    drop = ev;
1523
1524    // check we still have something to drop
1525    if (!drops) return EINA_TRUE;
1526
1527    /* Find any widget in our window; then work out geometry rel to our window */
1528    for (l = drops; l; l = l->next)
1529      {
1530         dropable = l->data;
1531         xwin = (Ecore_X_Window)ecore_evas_window_get
1532            (ecore_evas_ecore_evas_get(evas_object_evas_get
1533                                       (dropable->obj)));
1534         if (xwin == drop->win) break;
1535      }
1536    /* didn't find a window */
1537    if (!l) return EINA_TRUE;
1538
1539    /* Calculate real (widget relative) position */
1540    // - window position
1541    // - widget position
1542    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1543    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1544    savedtypes.x = drop->position.x - x;
1545    savedtypes.y = drop->position.y - y;
1546
1547    cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1548
1549    for (; l; l = l->next)
1550      {
1551         dropable = l->data;
1552         evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1553         if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1554             (savedtypes.x < x + w) && (savedtypes.y < y + h))
1555           break; /* found! */
1556      }
1557
1558    if (!l) return EINA_TRUE; /* didn't find one */
1559
1560    evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1561    savedtypes.x -= x;
1562    savedtypes.y -= y;
1563
1564    /* Find our type from the previous list */
1565    for (i = 0; i < CNP_N_ATOMS; i++)
1566      {
1567         for (j = 0; j < savedtypes.ntypes; j++)
1568           {
1569              if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1570           }
1571      }
1572
1573    cnp_debug("Didn't find a target\n");
1574    return EINA_TRUE;
1575
1576 found:
1577    cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1578    cnp_debug("0x%x\n",xwin);
1579
1580    if (i == CNP_ATOM_text_urilist)
1581      {
1582         cnp_debug("We found a URI... (%scached) %s\n",
1583                   savedtypes.pi ? "" : "not ",
1584                   savedtypes.pi->file);
1585         if (savedtypes.pi)
1586           {
1587              char *entrytag;
1588              static const char *tagstring = "<item absize=240x180 href="
1589                 "file://%s></item>";
1590              ddata.x = savedtypes.x;
1591              ddata.y = savedtypes.y;
1592
1593              /* If it's markup that also supports images */
1594              if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1595                  (dropable->types & ELM_SEL_FORMAT_IMAGE))
1596                {
1597                   int len;
1598                   ddata.format = ELM_SEL_FORMAT_MARKUP;
1599                   pasteimage_provider_set(dropable->obj);
1600
1601                   pastedimages = eina_list_append(pastedimages, savedtypes.pi);
1602                   len = strlen(tagstring) + strlen(savedtypes.pi->file);
1603                   entrytag = alloca(len + 1);
1604                   snprintf(entrytag, len + 1, tagstring, savedtypes.pi->file);
1605                   ddata.data = entrytag;
1606                   cnp_debug("Insert %s\n", (char *)ddata.data);
1607                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1608                   ecore_x_dnd_send_finished();
1609
1610                   if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1611                   savedtypes.pi = NULL;
1612                   return EINA_TRUE;
1613                }
1614              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1615                {
1616                   cnp_debug("Doing image insert (%s)\n", savedtypes.pi->file);
1617                   ddata.format = ELM_SEL_FORMAT_IMAGE;
1618                   ddata.data = (char *)savedtypes.pi->file;
1619                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1620                   ecore_x_dnd_send_finished();
1621
1622                   if (savedtypes.pi) pasteimage_free(savedtypes.pi);
1623                   savedtypes.pi = NULL;
1624
1625                   return EINA_TRUE;
1626                }
1627              else
1628                {
1629                   cnp_debug("Item doesn't support images... passing\n");
1630                   pasteimage_free(savedtypes.pi);
1631                   return EINA_TRUE;
1632                }
1633           }
1634         else if (savedtypes.textreq)
1635           {
1636              /* Already asked: Pretend we asked now, and paste immediately when
1637               * it comes in */
1638              savedtypes.textreq = 0;
1639              ecore_x_dnd_send_finished();
1640              return EINA_TRUE;
1641           }
1642      }
1643
1644    cnp_debug("doing a request then\n");
1645    selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1646    selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1647    selections[ELM_SEL_XDND].active = EINA_TRUE;
1648
1649    ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1650
1651    return EINA_TRUE;
1652 }
1653 static Eina_Bool
1654 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1655 {
1656    struct _Ecore_X_Event_Xdnd_Position *pos;
1657    Ecore_X_Rectangle rect;
1658
1659    pos = ev;
1660
1661    /* Need to send a status back */
1662    /* FIXME: Should check I can drop here */
1663    /* FIXME: Should highlight widget */
1664    rect.x = pos->position.x - 5;
1665    rect.y = pos->position.y - 5;
1666    rect.width = 10;
1667    rect.height = 10;
1668    ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1669
1670    return EINA_TRUE;
1671 }
1672
1673 /**
1674  * When dragging this is callback response from the destination.
1675  * The important thing we care about: Can we drop; thus update cursor
1676  * appropriately.
1677  */
1678 static Eina_Bool
1679 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1680 {
1681    struct _Ecore_X_Event_Xdnd_Status *status = ev;
1682
1683    if (!status) return EINA_TRUE;
1684
1685    /* Only thing we care about: will accept */
1686    if (status->will_accept)
1687      {
1688         cnp_debug("Will accept\n");
1689      }
1690    else
1691      { /* Won't accept */
1692         cnp_debug("Won't accept accept\n");
1693      }
1694    return EINA_TRUE;
1695 }
1696
1697 /**
1698  * Add a widget as drop target.
1699  */
1700 Eina_Bool
1701 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1702 {
1703    Dropable *drop;
1704    Ecore_X_Window xwin;
1705    Eina_List *item;
1706    int first;
1707    Evas_Object *top;
1708
1709    if (!obj) return EINA_FALSE;
1710    top = elm_widget_top_get(obj);
1711    if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1712    if (!_elm_cnp_init_count) _elm_cnp_init();
1713
1714    /* Is this the first? */
1715    first = (!drops) ? 1 : 0;
1716
1717    EINA_LIST_FOREACH(drops, item, drop)
1718      {
1719         if (drop->obj == obj)
1720           {
1721              /* Update: Not a new one */
1722              drop->dropcb = dropcb;
1723              drop->cbdata = cbdata;
1724              drop->types = format;
1725              return EINA_TRUE;
1726           }
1727      }
1728
1729    /* Create new drop */
1730    drop = calloc(1, sizeof(Dropable));
1731    if (!drop) return EINA_FALSE;
1732    /* FIXME: Check for eina's deranged error method */
1733    drops = eina_list_append(drops, drop);
1734
1735    if (!drops/* || or other error */)
1736      {
1737         free(drop);
1738         return EINA_FALSE;
1739      }
1740    drop->dropcb = dropcb;
1741    drop->cbdata = cbdata;
1742    drop->types = format;
1743    drop->obj = obj;
1744
1745    evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1746                                   /* I love C and varargs */
1747                                   (Evas_Object_Event_Cb)elm_drop_target_del,
1748                                   obj);
1749    /* FIXME: Handle resizes */
1750
1751    /* If not the first: We're done */
1752    if (!first) return EINA_TRUE;
1753
1754    xwin = (Ecore_X_Window)ecore_evas_window_get
1755       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1756
1757    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1758
1759    cnp_debug("Adding drop target calls\n");
1760    handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1761                                            _dnd_enter, NULL);
1762    handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1763                                          _dnd_position, NULL);
1764    handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1765                                           _dnd_drop, NULL);
1766
1767    return EINA_TRUE;
1768 }
1769
1770 Eina_Bool
1771 elm_drop_target_del(Evas_Object *obj)
1772 {
1773    Dropable *drop,*del;
1774    Eina_List *item;
1775    Ecore_X_Window xwin;
1776
1777    del = NULL;
1778    EINA_LIST_FOREACH(drops, item, drop)
1779      {
1780         if (drop->obj == obj)
1781           {
1782              drops = eina_list_remove_list(drops, item);
1783              del = drop;
1784              break;
1785           }
1786      }
1787    if (!del) return EINA_FALSE;
1788
1789    evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1790                                   (Evas_Object_Event_Cb)elm_drop_target_del);
1791    free(drop);
1792    /* If still drops there: All fine.. continue */
1793    if (drops) return EINA_TRUE;
1794
1795    cnp_debug("Disabling DND\n");
1796    xwin = (Ecore_X_Window)ecore_evas_window_get
1797       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1798    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1799
1800    ecore_event_handler_del(handler_pos);
1801    ecore_event_handler_del(handler_drop);
1802    ecore_event_handler_del(handler_enter);
1803
1804    if (savedtypes.pi)
1805      {
1806         pasteimage_free(savedtypes.pi);
1807         savedtypes.pi = NULL;
1808      }
1809
1810    return EINA_TRUE;
1811 }
1812
1813
1814 static void
1815 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1816 {
1817    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1818    ecore_x_dnd_drop();
1819    if (dragdonecb)
1820      {
1821         dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1822         dragdonecb = NULL;
1823      }
1824    if (dragwin)
1825      {
1826         evas_object_del(dragwin);
1827         dragwin = NULL;
1828      }
1829 }
1830
1831 static void
1832 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1833 {
1834    evas_object_move(dragwin,
1835                     pos->position.x - _dragx,
1836                     pos->position.y - _dragy);
1837 }
1838
1839
1840 Eina_Bool
1841 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1842 {
1843    Ecore_X_Window xwin;
1844    Cnp_Selection *sel;
1845    Elm_Sel_Type xdnd = ELM_SEL_XDND;
1846    Ecore_Evas *ee;
1847    int x, y, x2, y2, x3, y3;
1848    Evas_Object *icon;
1849    int w, h;
1850
1851    if (!_elm_cnp_init_count) _elm_cnp_init();
1852
1853    xwin = elm_win_xwindow_get(obj);
1854
1855    cnp_debug("starting drag...\n");
1856
1857    ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1858    sel = selections + ELM_SEL_XDND;
1859    sel->active = 1;
1860    sel->widget = obj;
1861    sel->format = format;
1862    sel->selbuf = data ? strdup(data) : NULL;
1863    dragdonecb = dragdone;
1864    dragdonedata = donecbdata;
1865
1866    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1867    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1868    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1869                                   _drag_mouse_up, NULL);
1870
1871    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1872                                             _dnd_status, NULL);
1873
1874    dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1875    elm_win_override_set(dragwin, 1);
1876
1877    /* FIXME: Images only */
1878    icon = elm_icon_add(dragwin);
1879    elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1880    elm_win_resize_object_add(dragwin,icon);
1881    evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1882    evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1883
1884    /* Position subwindow appropriately */
1885    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1886    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1887    evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1888    x += x2;
1889    y += y2;
1890    evas_object_move(dragwin, x, y);
1891    evas_object_resize(icon, w, h);
1892    evas_object_resize(dragwin, w, h);
1893
1894    evas_object_show(icon);
1895    evas_object_show(dragwin);
1896
1897    evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1898    _dragx = x3 - x2;
1899    _dragy = y3 - y2;
1900
1901    return EINA_TRUE;
1902 }
1903
1904 static Tmp_Info *
1905 elm_cnp_tempfile_create(int size)
1906 {
1907    Tmp_Info *info;
1908    const char *tmppath;
1909    int len;
1910
1911    info = malloc(sizeof(Tmp_Info));
1912    if (!info) return NULL;
1913
1914    tmppath = getenv("TMP");
1915    if (!tmppath) tmppath = P_tmpdir;
1916    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1917    if (len < 0)
1918      {
1919         free(info);
1920         return NULL;
1921      }
1922    len++;
1923    info->filename = malloc(len);
1924    if (!info->filename)
1925      {
1926         free(info);
1927         return NULL;
1928      }
1929    snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1930
1931    info->fd = mkstemp(info->filename);
1932
1933 # ifdef __linux__
1934      {
1935         char *tmp;
1936         /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1937         long pid;
1938
1939         pid = (long)getpid();
1940         /* Use pid instead of /proc/self: That way if can be passed around */
1941         len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1942         len++;
1943         tmp = malloc(len);
1944         if (tmp)
1945           {
1946              snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1947              unlink(info->filename);
1948              free(info->filename);
1949              info->filename = tmp;
1950           }
1951      }
1952 # endif
1953
1954    cnp_debug("filename is %s\n", info->filename);
1955    if (size < 1)
1956      {
1957         /* Set map to NULL and return */
1958         info->map = NULL;
1959         info->len = 0;
1960         return info;
1961      }
1962
1963    /* Map it in */
1964    if (ftruncate(info->fd, size))
1965      {
1966         perror("ftruncate");
1967         info->map = NULL;
1968         info->len = 0;
1969         return info;
1970      }
1971
1972    eina_mmap_safety_enabled_set(EINA_TRUE);
1973
1974    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1975    if (info->map == MAP_FAILED)
1976      {
1977         perror("mmap");
1978         info->map = NULL;
1979         info->len = 0;
1980      }
1981
1982    return info;
1983 }
1984
1985
1986 static int
1987 tmpinfo_free(Tmp_Info *info)
1988 {
1989    if (!info) return 0;
1990    free(info->filename);
1991    free(info);
1992    return 0;
1993 }
1994
1995 #else
1996 /* Stubs for windows */
1997 Eina_Bool
1998 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1999 {
2000    return EINA_FALSE;
2001 }
2002
2003 Eina_Bool
2004 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
2005 {
2006    return EINA_FALSE;
2007 }
2008
2009 Eina_Bool
2010 elm_drop_target_del(Evas_Object *o)
2011 {
2012    return EINA_TRUE;
2013 }
2014 #endif
2015
2016 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/