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