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