fix llvm/clang and gcc errors.
[framework/uifw/ethumb.git] / src / lib / Ethumb.c
1 /**
2  * @file
3  *
4  * Copyright (C) 2009 by ProFUSION embedded systems
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,  but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19  * USA.
20  *
21  * @author Rafael Antognolli <antognolli@profusion.mobi>
22  */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #ifdef HAVE_ALLOCA_H
28 # include <alloca.h>
29 #elif defined __GNUC__
30 # define alloca __builtin_alloca
31 #elif defined _AIX
32 # define alloca __alloca
33 #elif defined _MSC_VER
34 # include <malloc.h>
35 # define alloca _alloca
36 #else
37 # include <stddef.h>
38 # ifdef  __cplusplus
39 extern "C"
40 # endif
41 void *alloca (size_t);
42 #endif
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <limits.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <errno.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <dirent.h>
53 #include <dlfcn.h>
54 #include <ctype.h>
55
56 #ifndef PATH_MAX
57 # define PATH_MAX 4096
58 #endif
59
60 #include <Eina.h>
61 #include <eina_safety_checks.h>
62 #include <Evas.h>
63 #include <Ecore.h>
64 #include <Ecore_Evas.h>
65 #include <Ecore_File.h>
66 #include <Edje.h>
67
68 #include "Ethumb.h"
69 #include "ethumb_private.h"
70 #include "Ethumb_Plugin.h"
71 #include "md5.h"
72
73 static int _log_dom = -1;
74 #define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)
75 #define INF(...) EINA_LOG_DOM_INFO(_log_dom, __VA_ARGS__)
76 #define WRN(...) EINA_LOG_DOM_WARN(_log_dom, __VA_ARGS__)
77 #define ERR(...) EINA_LOG_DOM_ERR(_log_dom, __VA_ARGS__)
78
79 static int initcount = 0;
80 static const char *_home_thumb_dir = NULL;
81 static const char *_thumb_category_normal = NULL;
82 static const char *_thumb_category_large = NULL;
83
84 static const int THUMB_SIZE_NORMAL = 128;
85 static const int THUMB_SIZE_LARGE = 256;
86
87 static Eina_Hash *_plugins_ext = NULL;
88 static Eina_Array *_plugins = NULL;
89
90 static Eina_Bool
91 _ethumb_plugin_list_cb(Eina_Module *m, void *data __UNUSED__)
92 {
93    const char *file;
94    const char **ext;
95    Ethumb_Plugin *plugin;
96    Ethumb_Plugin *(*plugin_get)(void);
97
98    file = eina_module_file_get(m);
99    if (!eina_module_load(m))
100      {
101         ERR("could not load module \"%s\": %s",
102             file, eina_error_msg_get(eina_error_get()));
103         return EINA_FALSE;
104      }
105
106    plugin_get = eina_module_symbol_get(m, "ethumb_plugin_get");
107    if (!plugin_get)
108      {
109         ERR("could not find ethumb_plugin_get() in module \"%s\": %s",
110             file, eina_error_msg_get(eina_error_get()));
111         eina_module_unload(m);
112         return EINA_FALSE;
113      }
114
115    plugin = plugin_get();
116    if (!plugin)
117      {
118         ERR("plugin \"%s\" failed to init.", file);
119         eina_module_unload(m);
120         return EINA_FALSE;
121      }
122
123    DBG("loaded plugin \"%s\" (%p) with extensions:", file, plugin);
124    for (ext = plugin->extensions; *ext; ext++)
125      {
126         DBG("   extension \"%s\"", *ext);
127         eina_hash_add(_plugins_ext, *ext, plugin);
128      }
129
130    return EINA_TRUE;
131 }
132
133 static void
134 _ethumb_plugins_load(void)
135 {
136    _plugins_ext = eina_hash_string_small_new(NULL);
137    EINA_SAFETY_ON_NULL_RETURN(_plugins_ext);
138
139    _plugins = eina_module_list_get(_plugins, PLUGINSDIR, 1,
140                                    &_ethumb_plugin_list_cb, NULL);
141 }
142
143 static void
144 _ethumb_plugins_unload(void)
145 {
146    eina_hash_free(_plugins_ext);
147    _plugins_ext = NULL;
148    eina_module_list_unload(_plugins);
149    eina_module_list_free(_plugins);
150    eina_array_free(_plugins);
151    _plugins = NULL;
152 }
153
154 EAPI int
155 ethumb_init(void)
156 {
157    const char *home;
158    char buf[PATH_MAX];
159
160    if (initcount)
161      return ++initcount;
162
163    if (!eina_init())
164      {
165         fprintf(stderr, "ERROR: Could not initialize eina.\n");
166         return 0;
167      }
168    _log_dom = eina_log_domain_register("ethumb", EINA_COLOR_GREEN);
169    if (_log_dom < 0)
170      {
171         EINA_LOG_ERR("Could not register log domain: ethumb");
172         eina_shutdown();
173         return 0;
174      }
175
176    evas_init();
177    ecore_init();
178    ecore_evas_init();
179    edje_init();
180
181    home = getenv("HOME");
182    snprintf(buf, sizeof(buf), "%s/.thumbnails", home);
183
184    _home_thumb_dir = eina_stringshare_add(buf);
185    _thumb_category_normal = eina_stringshare_add("normal");
186    _thumb_category_large = eina_stringshare_add("large");
187
188    _ethumb_plugins_load();
189    return ++initcount;
190 }
191
192 EAPI int
193 ethumb_shutdown(void)
194 {
195    initcount--;
196    if (initcount == 0)
197      {
198         _ethumb_plugins_unload();
199         eina_stringshare_del(_home_thumb_dir);
200         eina_stringshare_del(_thumb_category_normal);
201         eina_stringshare_del(_thumb_category_large);
202         evas_shutdown();
203         ecore_shutdown();
204         ecore_evas_shutdown();
205         edje_shutdown();
206         eina_log_domain_unregister(_log_dom);
207         _log_dom = -1;
208         eina_shutdown();
209      }
210
211    return initcount;
212 }
213
214 EAPI Ethumb *
215 ethumb_new(void)
216 {
217    Ethumb *ethumb;
218    Ecore_Evas *ee, *sub_ee;
219    Evas *e, *sub_e;
220    Evas_Object *o, *img;
221
222    ethumb = calloc(1, sizeof(Ethumb));
223    EINA_SAFETY_ON_NULL_RETURN_VAL(ethumb, NULL);
224
225    /* IF CHANGED, UPDATE DOCS in (Ethumb.c, Ethumb_Client.c, python...)!!! */
226    ethumb->tw = THUMB_SIZE_NORMAL;
227    ethumb->th = THUMB_SIZE_NORMAL;
228    ethumb->crop_x = 0.5;
229    ethumb->crop_y = 0.5;
230    ethumb->quality = 80;
231    ethumb->compress = 9;
232    ethumb->video.start = 0.1;
233    ethumb->video.time = 3;
234    ethumb->video.interval = 0.05;
235    ethumb->video.ntimes = 3;
236    ethumb->video.fps = 10;
237
238    ee = ecore_evas_buffer_new(1, 1);
239    e = ecore_evas_get(ee);
240    if (!e)
241      {
242         ERR("could not create ecore evas buffer");
243         return NULL;
244      }
245
246    evas_image_cache_set(e, 0);
247    evas_font_cache_set(e, 0);
248
249    o = ecore_evas_object_image_new(ee);
250    if (!o)
251      {
252         ERR("could not create sub ecore evas buffer");
253         ecore_evas_free(ee);
254         free(ethumb);
255         return NULL;
256      }
257
258    sub_ee = evas_object_data_get(o, "Ecore_Evas");
259    sub_e = ecore_evas_get(sub_ee);
260
261    evas_image_cache_set(sub_e, 0);
262    evas_font_cache_set(sub_e, 0);
263
264    img = evas_object_image_add(sub_e);
265    if (!img)
266      {
267         ERR("could not create source objects.");
268         ecore_evas_free(ee);
269         free(ethumb);
270         return NULL;
271      }
272
273    ethumb->ee = ee;
274    ethumb->e = e;
275    ethumb->sub_ee = sub_ee;
276    ethumb->sub_e = sub_e;
277    ethumb->o = o;
278    ethumb->img = img;
279
280    DBG("ethumb=%p", ethumb);
281
282    return ethumb;
283 }
284
285 static void
286 _ethumb_frame_free(Ethumb_Frame *frame)
287 {
288    Evas_Object *o;
289
290    if (!frame)
291      return;
292
293    if (frame->swallow && frame->edje)
294      {
295      o = edje_object_part_swallow_get(frame->edje, frame->swallow);
296      if (o)
297        edje_object_part_unswallow(frame->edje, o);
298      }
299    eina_stringshare_del(frame->file);
300    eina_stringshare_del(frame->group);
301    eina_stringshare_del(frame->swallow);
302
303    if (frame->edje)
304      evas_object_del(frame->edje);
305
306    free(frame);
307 }
308
309 EAPI void
310 ethumb_free(Ethumb *ethumb)
311 {
312    EINA_SAFETY_ON_NULL_RETURN(ethumb);
313
314    DBG("ethumb=%p", ethumb);
315
316    if (ethumb->frame)
317      _ethumb_frame_free(ethumb->frame);
318    ethumb_file_free(ethumb);
319    ecore_evas_free(ethumb->ee);
320    eina_stringshare_del(ethumb->thumb_dir);
321    eina_stringshare_del(ethumb->category);
322    if (ethumb->finished_idler)
323      ecore_idler_del(ethumb->finished_idler);
324    free(ethumb);
325 }
326
327 EAPI void
328 ethumb_thumb_fdo_set(Ethumb *e, Ethumb_Thumb_FDO_Size s)
329 {
330    EINA_SAFETY_ON_NULL_RETURN(e);
331    EINA_SAFETY_ON_FALSE_RETURN(s == ETHUMB_THUMB_NORMAL ||
332                                s == ETHUMB_THUMB_LARGE);
333    DBG("ethumb=%p, size=%d", e, s);
334
335    if (s == ETHUMB_THUMB_NORMAL)
336      {
337         e->tw = THUMB_SIZE_NORMAL;
338         e->th = THUMB_SIZE_NORMAL;
339      }
340    else
341      {
342         e->tw = THUMB_SIZE_LARGE;
343         e->th = THUMB_SIZE_LARGE;
344      }
345
346    e->format = ETHUMB_THUMB_FDO;
347    e->aspect = ETHUMB_THUMB_KEEP_ASPECT;
348    _ethumb_frame_free(e->frame);
349    e->frame = NULL;
350    eina_stringshare_del(e->thumb_dir);
351    eina_stringshare_del(e->category);
352    e->thumb_dir = NULL;
353    e->category = NULL;
354 }
355
356 EAPI void
357 ethumb_thumb_size_set(Ethumb *e, int tw, int th)
358 {
359    EINA_SAFETY_ON_NULL_RETURN(e);
360    EINA_SAFETY_ON_FALSE_RETURN(tw > 0);
361    EINA_SAFETY_ON_FALSE_RETURN(th > 0);
362
363    DBG("ethumb=%p, w=%d, h=%d", e, tw, th);
364    e->tw = tw;
365    e->th = th;
366 }
367
368 EAPI void
369 ethumb_thumb_size_get(const Ethumb *e, int *tw, int *th)
370 {
371    EINA_SAFETY_ON_NULL_RETURN(e);
372
373    if (tw) *tw = e->tw;
374    if (th) *th = e->th;
375 }
376
377 EAPI void
378 ethumb_thumb_format_set(Ethumb *e, Ethumb_Thumb_Format f)
379 {
380    EINA_SAFETY_ON_NULL_RETURN(e);
381    EINA_SAFETY_ON_FALSE_RETURN(f == ETHUMB_THUMB_FDO ||
382                                f == ETHUMB_THUMB_JPEG ||
383                                f == ETHUMB_THUMB_EET);
384
385    DBG("ethumb=%p, format=%d", e, f);
386    e->format = f;
387 }
388
389 EAPI Ethumb_Thumb_Format
390 ethumb_thumb_format_get(const Ethumb *e)
391 {
392    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
393    return e->format;
394 }
395
396 EAPI void
397 ethumb_thumb_aspect_set(Ethumb *e, Ethumb_Thumb_Aspect a)
398 {
399    EINA_SAFETY_ON_NULL_RETURN(e);
400    EINA_SAFETY_ON_FALSE_RETURN(a == ETHUMB_THUMB_KEEP_ASPECT ||
401                                a == ETHUMB_THUMB_IGNORE_ASPECT ||
402                                a == ETHUMB_THUMB_CROP);
403
404    DBG("ethumb=%p, aspect=%d", e, a);
405    e->aspect = a;
406 }
407
408 EAPI Ethumb_Thumb_Aspect
409 ethumb_thumb_aspect_get(const Ethumb *e)
410 {
411    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
412    return e->aspect;
413 }
414
415 EAPI void
416 ethumb_thumb_crop_align_set(Ethumb *e, float x, float y)
417 {
418    EINA_SAFETY_ON_NULL_RETURN(e);
419
420    DBG("ethumb=%p, x=%f, y=%f", e, x, y);
421    e->crop_x = x;
422    e->crop_y = y;
423 }
424
425 EAPI void
426 ethumb_thumb_crop_align_get(const Ethumb *e, float *x, float *y)
427 {
428    EINA_SAFETY_ON_NULL_RETURN(e);
429
430    if (x) *x = e->crop_x;
431    if (y) *y = e->crop_y;
432 }
433
434 EAPI void
435 ethumb_thumb_quality_set(Ethumb *e, int quality)
436 {
437    EINA_SAFETY_ON_NULL_RETURN(e);
438
439    DBG("ethumb=%p, quality=%d", e, quality);
440    e->quality = quality;
441 }
442
443 EAPI int
444 ethumb_thumb_quality_get(const Ethumb *e)
445 {
446    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
447    return e->quality;
448 }
449
450 EAPI void
451 ethumb_thumb_compress_set(Ethumb *e, int compress)
452 {
453    EINA_SAFETY_ON_NULL_RETURN(e);
454
455    DBG("ethumb=%p, compress=%d", e, compress);
456    e->compress = compress;
457 }
458
459 EAPI int
460 ethumb_thumb_compress_get(const Ethumb *e)
461 {
462    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
463    return e->compress;
464 }
465
466 EAPI Eina_Bool
467 ethumb_frame_set(Ethumb *e, const char *theme_file, const char *group, const char *swallow)
468 {
469    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
470
471    Ethumb_Frame *frame;
472    frame = e->frame;
473
474    DBG("ethumb=%p, theme_file=%s, group=%s, swallow=%s",
475        e, theme_file ? theme_file : "", group ? group : "",
476        swallow ? swallow : "");
477
478    if (frame)
479      {
480         edje_object_part_unswallow(frame->edje, e->img);
481         if (!theme_file)
482           _ethumb_frame_free(frame);
483      }
484
485    if (!theme_file)
486      {
487         e->frame = NULL;
488         return EINA_TRUE;
489      }
490
491    if (!frame)
492      {
493         frame = calloc(1, sizeof(Ethumb_Frame));
494         if (!frame)
495           {
496              ERR("could not allocate Ethumb_Frame structure.");
497              return EINA_FALSE;
498           }
499
500         frame->edje = edje_object_add(e->sub_e);
501         if (!frame->edje)
502           {
503              ERR("could not create edje frame object.");
504              _ethumb_frame_free(frame);
505              e->frame = NULL;
506              return EINA_FALSE;
507           }
508      }
509
510    if (!edje_object_file_set(frame->edje, theme_file, group))
511      {
512         ERR("could not load frame theme.");
513         _ethumb_frame_free(frame);
514         e->frame = NULL;
515         return EINA_FALSE;
516      }
517
518    edje_object_part_swallow(frame->edje, swallow, e->img);
519    if (!edje_object_part_swallow_get(frame->edje, swallow))
520      {
521         ERR("could not swallow image to edje frame.");
522         _ethumb_frame_free(frame);
523         e->frame = NULL;
524         return EINA_FALSE;
525      }
526
527    eina_stringshare_replace(&frame->file, theme_file);
528    eina_stringshare_replace(&frame->group, group);
529    eina_stringshare_replace(&frame->swallow, swallow);
530
531    e->frame = frame;
532
533    return EINA_TRUE;
534 }
535
536 EAPI void
537 ethumb_frame_get(const Ethumb *e, const char **theme_file, const char **group, const char **swallow)
538 {
539    EINA_SAFETY_ON_NULL_RETURN(e);
540
541    if (e->frame)
542      {
543         if (theme_file) *theme_file = e->frame->file;
544         if (group) *group = e->frame->group;
545         if (swallow) *swallow = e->frame->swallow;
546      }
547    else
548      {
549         if (theme_file) *theme_file = NULL;
550         if (group) *group = NULL;
551         if (swallow) *swallow = NULL;
552      }
553 }
554
555 static const char *
556 _ethumb_build_absolute_path(const char *path, char buf[PATH_MAX])
557 {
558    char *p;
559    int len;
560
561    if (!path)
562      return NULL;
563
564    p = buf;
565
566    if (path[0] == '/')
567      strcpy(p, path);
568    else if (path[0] == '~')
569      {
570         const char *home = getenv("HOME");
571         if (!home)
572           return NULL;
573         strcpy(p, home);
574         len = strlen(p);
575         p += len;
576         p[0] = '/';
577         p++;
578         strcpy(p, path + 2);
579      }
580    else
581      {
582         if (!getcwd(p, PATH_MAX))
583           return NULL;
584         len = strlen(p);
585         p += len;
586         p[0] = '/';
587         p++;
588         strcpy(p, path);
589      }
590
591    return buf;
592 }
593
594 EAPI void
595 ethumb_thumb_dir_path_set(Ethumb *e, const char *path)
596 {
597    char buf[PATH_MAX];
598    EINA_SAFETY_ON_NULL_RETURN(e);
599
600    DBG("ethumb=%p, path=%s", e, path ? path : "");
601    path = _ethumb_build_absolute_path(path, buf);
602    eina_stringshare_replace(&e->thumb_dir, path);
603 }
604
605 EAPI const char *
606 ethumb_thumb_dir_path_get(const Ethumb *e)
607 {
608    EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
609
610    return e->thumb_dir;
611 }
612
613 EAPI void
614 ethumb_thumb_category_set(Ethumb *e, const char *category)
615 {
616    EINA_SAFETY_ON_NULL_RETURN(e);
617
618    DBG("ethumb=%p, category=%s", e, category ? category : "");
619    eina_stringshare_replace(&e->category, category);
620 }
621
622 EAPI const char *
623 ethumb_thumb_category_get(const Ethumb *e)
624 {
625    EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
626
627    return e->category;
628 }
629
630 EAPI void
631 ethumb_video_start_set(Ethumb *e, float start)
632 {
633    EINA_SAFETY_ON_NULL_RETURN(e);
634    EINA_SAFETY_ON_FALSE_RETURN(start >= 0.0);
635    EINA_SAFETY_ON_FALSE_RETURN(start <= 1.0);
636
637    DBG("ethumb=%p, video_start=%f", e, start);
638    e->video.start = start;
639 }
640
641 EAPI float
642 ethumb_video_start_get(const Ethumb *e)
643 {
644    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
645
646    return e->video.start;
647 }
648
649 EAPI void
650 ethumb_video_time_set(Ethumb *e, float time)
651 {
652    EINA_SAFETY_ON_NULL_RETURN(e);
653
654    DBG("ethumb=%p, video_start=%f", e, time);
655    e->video.time = time;
656 }
657
658 EAPI float
659 ethumb_video_time_get(const Ethumb *e)
660 {
661    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
662
663    return e->video.time;
664 }
665
666 EAPI void
667 ethumb_video_interval_set(Ethumb *e, float interval)
668 {
669    EINA_SAFETY_ON_NULL_RETURN(e);
670
671    DBG("ethumb=%p, video_interval=%f", e, interval);
672    e->video.interval = interval;
673 }
674
675 EAPI float
676 ethumb_video_interval_get(const Ethumb *e)
677 {
678    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
679
680    return e->video.interval;
681 }
682
683 EAPI void
684 ethumb_video_ntimes_set(Ethumb *e, unsigned int ntimes)
685 {
686    EINA_SAFETY_ON_NULL_RETURN(e);
687    EINA_SAFETY_ON_FALSE_RETURN(ntimes > 0);
688
689    DBG("ethumb=%p, video_ntimes=%d", e, ntimes);
690    e->video.ntimes = ntimes;
691 }
692
693 EAPI unsigned int
694 ethumb_video_ntimes_get(const Ethumb *e)
695 {
696    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
697
698    return e->video.ntimes;
699 }
700
701 EAPI void
702 ethumb_video_fps_set(Ethumb *e, unsigned int fps)
703 {
704    EINA_SAFETY_ON_NULL_RETURN(e);
705    EINA_SAFETY_ON_FALSE_RETURN(fps > 0);
706
707    DBG("ethumb=%p, video_fps=%d", e, fps);
708    e->video.fps = fps;
709 }
710
711 EAPI unsigned int
712 ethumb_video_fps_get(const Ethumb *e)
713 {
714    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
715
716    return e->video.fps;
717 }
718
719 EAPI void
720 ethumb_document_page_set(Ethumb *e, unsigned int page)
721 {
722    EINA_SAFETY_ON_NULL_RETURN(e);
723
724    DBG("ethumb=%p, document_page=%d", e, page);
725    e->document.page = page;
726 }
727
728 EAPI unsigned int
729 ethumb_document_page_get(const Ethumb *e)
730 {
731    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
732
733    return e->document.page;
734 }
735
736 EAPI Eina_Bool
737 ethumb_file_set(Ethumb *e, const char *path, const char *key)
738 {
739    char buf[PATH_MAX];
740    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
741
742    DBG("ethumb=%p, path=%s, key=%s", e, path ? path : "", key ? key : "");
743    if (path && access(path, R_OK))
744      {
745         ERR("couldn't access file \"%s\"", path);
746         return EINA_FALSE;
747      }
748
749    path = _ethumb_build_absolute_path(path, buf);
750    eina_stringshare_replace(&e->src_path, path);
751    eina_stringshare_replace(&e->src_key, key);
752    eina_stringshare_replace(&e->thumb_path, NULL);
753    eina_stringshare_replace(&e->thumb_key, NULL);
754
755    return EINA_TRUE;
756 }
757
758 EAPI void
759 ethumb_file_get(const Ethumb *e, const char **path, const char **key)
760 {
761    EINA_SAFETY_ON_NULL_RETURN(e);
762
763    if (path) *path = e->src_path;
764    if (key) *key = e->src_key;
765 }
766
767 static const char ACCEPTABLE_URI_CHARS[96] = {
768      /*      !    "    #    $    %    &    '    (    )    *    +    ,    -    .    / */ 
769      0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
770      /* 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? */
771      0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
772      /* @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O */
773      0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
774      /* P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _ */
775      0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
776      /* `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o */
777      0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
778      /* p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~  DEL */
779      0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
780 };
781
782 static const char *
783 _ethumb_generate_hash(const char *file)
784 {
785   int n;
786   MD5_CTX ctx;
787   char md5out[(2 * MD5_HASHBYTES) + 1];
788   unsigned char hash[MD5_HASHBYTES];
789   static const char hex[] = "0123456789abcdef";
790
791   char *uri;
792   char *t;
793   const unsigned char *c;
794
795 #define _check_uri_char(c) \
796   ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
797
798   EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
799
800   uri = alloca(3 * strlen(file) + 9);
801   memcpy(uri, "file://", sizeof("file://") - 1);
802   t = uri + sizeof("file://") - 1;
803
804   for (c = (const unsigned char *)file; *c != '\0'; c++)
805     {
806        if (!_check_uri_char(*c))
807          {
808             *t++ = '%';
809             *t++ = hex[*c >> 4];
810             *t++ = hex[*c & 15];
811          }
812        else
813          *t++ = *c;
814     }
815   *t = '\0';
816
817 #undef _check_uri_char
818
819   MD5Init (&ctx);
820   MD5Update (&ctx, (unsigned char const*)uri, (unsigned)strlen (uri));
821   MD5Final (hash, &ctx);
822
823   for (n = 0; n < MD5_HASHBYTES; n++)
824     {
825       md5out[2 * n] = hex[hash[n] >> 4];
826       md5out[2 * n + 1] = hex[hash[n] & 0x0f];
827     }
828   md5out[2 * n] = '\0';
829
830   DBG("md5=%s, file=%s", md5out, file);
831   return eina_stringshare_add(md5out);
832 }
833
834 static int
835 _ethumb_file_check_fdo(Ethumb *e)
836 {
837    if (!((e->tw == THUMB_SIZE_NORMAL && e->th == THUMB_SIZE_NORMAL) ||
838        (e->tw == THUMB_SIZE_LARGE && e->th == THUMB_SIZE_LARGE)))
839      return 0;
840
841    if (e->format != ETHUMB_THUMB_FDO)
842      return 0;
843
844    if (e->aspect != ETHUMB_THUMB_KEEP_ASPECT)
845      return 0;
846
847    if (e->frame)
848      return 0;
849
850    return 1;
851 }
852
853 static const char *
854 _ethumb_file_generate_custom_category(Ethumb *e)
855 {
856    char buf[PATH_MAX];
857    const char *aspect, *format;
858    const char *frame;
859
860    if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
861      aspect = "keep_aspect";
862    else if (e->aspect == ETHUMB_THUMB_IGNORE_ASPECT)
863      aspect = "ignore_aspect";
864    else
865      aspect = "crop";
866
867    if (e->format == ETHUMB_THUMB_FDO)
868      format = "png";
869    else if (e->format == ETHUMB_THUMB_JPEG)
870      format = "jpg";
871    else
872      format = "eet";
873
874    if (e->frame)
875      frame = "-framed";
876    else
877      frame = "";
878
879    snprintf(buf, sizeof(buf), "%dx%d-%s%s-%s",
880             e->tw, e->th, aspect, frame, format);
881
882    return eina_stringshare_add(buf);
883 }
884
885 static void
886 _ethumb_file_generate_path(Ethumb *e)
887 {
888    char buf[PATH_MAX];
889    char *fullname;
890    const char *hash;
891    const char *thumb_dir, *category;
892    const char *ext;
893    int fdo_format;
894
895
896    fdo_format = _ethumb_file_check_fdo(e);
897
898    if (e->thumb_dir)
899      thumb_dir = eina_stringshare_ref(e->thumb_dir);
900    else
901      thumb_dir = eina_stringshare_ref(_home_thumb_dir);
902
903    if (e->category)
904      category = eina_stringshare_ref(e->category);
905    else if (!fdo_format)
906      category = _ethumb_file_generate_custom_category(e);
907    else
908      {
909         if (e->tw == THUMB_SIZE_NORMAL)
910           category = eina_stringshare_ref(_thumb_category_normal);
911         else if (e->tw == THUMB_SIZE_LARGE)
912           category = eina_stringshare_ref(_thumb_category_large);
913         else
914           {
915              ERR("fdo_format but size %d is not NORMAL (%d) or LARGE (%d)?",
916                  e->tw, THUMB_SIZE_NORMAL, THUMB_SIZE_LARGE);
917              category = "unknown";
918           }
919      }
920
921    if (e->format == ETHUMB_THUMB_FDO)
922      ext = "png";
923    else if (e->format == ETHUMB_THUMB_JPEG)
924      ext = "jpg";
925    else
926      ext = "eet";
927
928
929    fullname = ecore_file_realpath(e->src_path);
930    hash = _ethumb_generate_hash(fullname);
931    snprintf(buf, sizeof(buf), "%s/%s/%s.%s", thumb_dir, category, hash, ext);
932    free(fullname);
933    DBG("ethumb=%p, path=%s", e, buf);
934    eina_stringshare_replace(&e->thumb_path, buf);
935    if (e->format == ETHUMB_THUMB_EET)
936      eina_stringshare_replace(&e->thumb_key, "thumbnail");
937    else
938      {
939         eina_stringshare_del(e->thumb_key);
940         e->thumb_key = NULL;
941      }
942
943    eina_stringshare_del(thumb_dir);
944    eina_stringshare_del(category);
945    eina_stringshare_del(hash);
946 }
947
948 EAPI void
949 ethumb_file_free(Ethumb *e)
950 {
951    EINA_SAFETY_ON_NULL_RETURN(e);
952    DBG("ethumb=%p", e);
953
954    eina_stringshare_replace(&e->src_path, NULL);
955    eina_stringshare_replace(&e->src_key, NULL);
956    eina_stringshare_replace(&e->thumb_path, NULL);
957    eina_stringshare_replace(&e->thumb_key, NULL);
958 }
959
960 EAPI void
961 ethumb_thumb_path_set(Ethumb *e, const char *path, const char *key)
962 {
963    char buf[PATH_MAX];
964
965    EINA_SAFETY_ON_NULL_RETURN(e);
966    DBG("ethumb=%p, path=%s, key=%s", e, path ? path : "", key ? key : "");
967
968    if (!path)
969      {
970         eina_stringshare_replace(&e->thumb_path, NULL);
971         eina_stringshare_replace(&e->thumb_key, NULL);
972      }
973    else
974      {
975         path = _ethumb_build_absolute_path(path, buf);
976         eina_stringshare_replace(&e->thumb_path, path);
977         eina_stringshare_replace(&e->thumb_key, key);
978      }
979 }
980
981 EAPI void
982 ethumb_thumb_path_get(Ethumb *e, const char **path, const char **key)
983 {
984    EINA_SAFETY_ON_NULL_RETURN(e);
985    if (!e->thumb_path)
986      _ethumb_file_generate_path(e);
987
988    if (path) *path = e->thumb_path;
989    if (key) *key = e->thumb_key;
990 }
991
992 void
993 ethumb_calculate_aspect_from_ratio(Ethumb *e, float ia, int *w, int *h)
994 {
995    float a;
996
997    *w = e->tw;
998    *h = e->th;
999
1000    if (ia == 0)
1001      return;
1002
1003    a = e->tw / (float)e->th;
1004
1005    if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
1006      {
1007         if ((ia > a && e->tw > 0) || e->th <= 0)
1008           *h = e->tw / ia;
1009         else
1010           *w = e->th * ia;
1011      }
1012 }
1013
1014 void
1015 ethumb_calculate_aspect(Ethumb *e, int iw, int ih, int *w, int *h)
1016 {
1017    float ia;
1018
1019    ia = iw / (float)ih;
1020
1021    ethumb_calculate_aspect_from_ratio(e, ia, w, h);
1022 }
1023
1024 void
1025 ethumb_calculate_fill_from_ratio(Ethumb *e, float ia, int *fx, int *fy, int *fw, int *fh)
1026 {
1027    float a;
1028
1029    *fw = e->tw;
1030    *fh = e->th;
1031    *fx = 0;
1032    *fy = 0;
1033
1034    if (ia == 0)
1035      return;
1036
1037    a = e->tw / (float)e->th;
1038
1039    if (e->aspect == ETHUMB_THUMB_CROP)
1040      {
1041         if ((ia > a && e->tw > 0) || e->th <= 0)
1042           *fw = e->th * ia;
1043         else
1044           *fh = e->tw / ia;
1045
1046         *fx = - e->crop_x * (*fw - e->tw);
1047         *fy = - e->crop_y * (*fh - e->th);
1048      }
1049    else if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
1050      {
1051         if ((ia > a && e->tw > 0) || e->th <= 0)
1052           *fh = e->tw / ia;
1053         else
1054           *fw = e->th * ia;
1055      }
1056 }
1057
1058 void
1059 ethumb_calculate_fill(Ethumb *e, int iw, int ih, int *fx, int *fy, int *fw, int *fh)
1060 {
1061    float ia;
1062    ia = iw / (float)ih;
1063
1064    ethumb_calculate_fill_from_ratio(e, ia, fx, fy, fw, fh);
1065 }
1066
1067 static Eina_Bool
1068 _ethumb_plugin_generate(Ethumb *e)
1069 {
1070    const char *extp;
1071    char ext[PATH_MAX];
1072    Ethumb_Plugin *plugin;
1073    int i;
1074
1075    extp = strrchr(e->src_path, '.');
1076    if (!extp)
1077      {
1078         ERR("could not get extension for file \"%s\"", e->src_path);
1079         return EINA_FALSE;
1080      }
1081
1082    for (i = 0; extp[i] != '\0'; i++)
1083         ext[i] = tolower(extp[i + 1]);
1084
1085    plugin = eina_hash_find(_plugins_ext, ext);
1086    if (!plugin)
1087      {
1088         DBG("no plugin for extension: \"%s\"", ext);
1089         return EINA_FALSE;
1090      }
1091
1092    if (e->frame)
1093      evas_object_hide(e->frame->edje);
1094    else
1095      evas_object_hide(e->img);
1096
1097    plugin->generate_thumb(e);
1098
1099    return EINA_TRUE;
1100 }
1101
1102 Eina_Bool
1103 ethumb_plugin_image_resize(Ethumb *e, int w, int h)
1104 {
1105    Evas_Object *img;
1106
1107    img = e->img;
1108
1109    if (e->frame)
1110      {
1111         edje_extern_object_min_size_set(img, w, h);
1112         edje_extern_object_max_size_set(img, w, h);
1113         edje_object_calc_force(e->frame->edje);
1114         evas_object_move(e->frame->edje, 0, 0);
1115         evas_object_resize(e->frame->edje, w, h);
1116      }
1117    else
1118      {
1119         evas_object_move(img, 0, 0);
1120         evas_object_resize(img, w, h);
1121      }
1122
1123    evas_object_image_size_set(e->o, w, h);
1124    ecore_evas_resize(e->sub_ee, w, h);
1125
1126    e->rw = w;
1127    e->rh = h;
1128
1129    return EINA_TRUE;
1130 }
1131
1132 Eina_Bool
1133 ethumb_image_save(Ethumb *e)
1134 {
1135    Eina_Bool r;
1136    char *dname;
1137    char flags[256];
1138
1139    evas_damage_rectangle_add(e->sub_e, 0, 0, e->rw, e->rh);
1140    evas_render(e->sub_e);
1141
1142    if (!e->thumb_path)
1143      _ethumb_file_generate_path(e);
1144
1145    if (!e->thumb_path)
1146      {
1147         ERR("could not create file path...");
1148         return EINA_FALSE;
1149      }
1150
1151    dname = ecore_file_dir_get(e->thumb_path);
1152    r = ecore_file_mkpath(dname);
1153    free(dname);
1154    if (!r)
1155      {
1156         ERR("could not create directory '%s'", dname);
1157         return EINA_FALSE;
1158      }
1159
1160    snprintf(flags, sizeof(flags), "quality=%d compress=%d",
1161             e->quality, e->compress);
1162    r = evas_object_image_save(e->o, e->thumb_path, e->thumb_key, flags);
1163
1164    if (!r)
1165      {
1166         ERR("could not save image: path=%s, key=%s", e->thumb_path,
1167             e->thumb_key);
1168         return EINA_FALSE;
1169      }
1170
1171    return EINA_TRUE;
1172 }
1173
1174 static int
1175 _ethumb_image_load(Ethumb *e)
1176 {
1177    int error;
1178    Evas_Coord w, h, ww, hh, fx, fy, fw, fh;
1179    Evas_Object *img;
1180
1181    img = e->img;
1182
1183    if (e->frame)
1184      evas_object_hide(e->frame->edje);
1185    else
1186      evas_object_hide(img);
1187    evas_object_image_file_set(img, NULL, NULL);
1188    evas_object_image_load_size_set(img, e->tw, e->th);
1189    evas_object_image_file_set(img, e->src_path, e->src_key);
1190
1191    if (e->frame)
1192      evas_object_show(e->frame->edje);
1193    else
1194      evas_object_show(img);
1195
1196    error = evas_object_image_load_error_get(img);
1197    if (error != EVAS_LOAD_ERROR_NONE)
1198      {
1199         ERR("could not load image '%s': %d", e->src_path, error);
1200         return 0;
1201      }
1202
1203    evas_object_image_size_get(img, &w, &h);
1204    if ((w <= 0) || (h <= 0))
1205      return 0;
1206
1207    ethumb_calculate_aspect(e, w, h, &ww, &hh);
1208
1209    if (e->frame)
1210      {
1211         edje_extern_object_min_size_set(img, ww, hh);
1212         edje_extern_object_max_size_set(img, ww, hh);
1213         edje_object_calc_force(e->frame->edje);
1214         evas_object_move(e->frame->edje, 0, 0);
1215         evas_object_resize(e->frame->edje, ww, hh);
1216      }
1217    else
1218      {
1219         evas_object_move(img, 0, 0);
1220         evas_object_resize(img, ww, hh);
1221      }
1222
1223    ethumb_calculate_fill(e, w, h, &fx, &fy, &fw, &fh);
1224    evas_object_image_fill_set(img, fx, fy, fw, fh);
1225
1226    evas_object_image_size_set(e->o, ww, hh);
1227    ecore_evas_resize(e->sub_ee, ww, hh);
1228
1229    e->rw = ww;
1230    e->rh = hh;
1231
1232    return 1;
1233 }
1234
1235 static int
1236 _ethumb_finished_idler_cb(void *data)
1237 {
1238    Ethumb *e = data;
1239
1240    e->finished_cb(e->cb_data, e, e->cb_result);
1241    if (e->cb_data_free)
1242      e->cb_data_free(e->cb_data);
1243    e->finished_idler = NULL;
1244    e->finished_cb = NULL;
1245    e->cb_data = NULL;
1246    e->cb_data_free = NULL;
1247
1248    return 0;
1249 }
1250
1251 void
1252 ethumb_finished_callback_call(Ethumb *e, int result)
1253 {
1254    EINA_SAFETY_ON_NULL_RETURN(e);
1255
1256    e->cb_result = result;
1257    if (e->finished_idler)
1258      ecore_idler_del(e->finished_idler);
1259    e->finished_idler = ecore_idler_add(_ethumb_finished_idler_cb, e);
1260 }
1261
1262 EAPI Eina_Bool
1263 ethumb_generate(Ethumb *e, Ethumb_Generate_Cb finished_cb, const void *data, Eina_Free_Cb free_data)
1264 {
1265    int r;
1266
1267    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
1268    EINA_SAFETY_ON_NULL_RETURN_VAL(finished_cb, 0);
1269    DBG("ethumb=%p, finished_cb=%p, data=%p, free_data=%p, path=%s, key=%s",
1270        e, finished_cb, data, free_data,
1271        e->src_path ? e->src_path : "", e->src_key ? e->src_key : "");
1272
1273    if (e->finished_idler)
1274      {
1275         ERR("thumbnail generation already in progress.");
1276         return EINA_FALSE;
1277      }
1278    e->finished_cb = finished_cb;
1279    e->cb_data = (void *)data;
1280    e->cb_data_free = free_data;
1281
1282    if (!e->src_path)
1283      {
1284         ERR("no file set.");
1285         ethumb_finished_callback_call(e, 0);
1286         return EINA_TRUE;
1287      }
1288
1289    r = _ethumb_plugin_generate(e);
1290    if (r)
1291      return EINA_TRUE;
1292
1293    if (!_ethumb_image_load(e))
1294      {
1295         ERR("could not load input image.");
1296         ethumb_finished_callback_call(e, 0);
1297         return EINA_TRUE;
1298      }
1299
1300    r = ethumb_image_save(e);
1301
1302    ethumb_finished_callback_call(e, r);
1303
1304    return EINA_TRUE;
1305 }
1306
1307 EAPI Eina_Bool
1308 ethumb_exists(Ethumb *e)
1309 {
1310    struct stat thumb, src;
1311    int r_thumb, r_src;
1312    Eina_Bool r = EINA_FALSE;
1313
1314    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
1315    EINA_SAFETY_ON_NULL_RETURN_VAL(e->src_path, 0);
1316    DBG("ethumb=%p, path=%s", e, e->src_path ? e->src_path : "");
1317
1318    if (!e->thumb_path)
1319      _ethumb_file_generate_path(e);
1320
1321    EINA_SAFETY_ON_NULL_RETURN_VAL(e->thumb_path, 0);
1322
1323    r_thumb = stat(e->thumb_path, &thumb);
1324    r_src = stat(e->src_path, &src);
1325
1326    EINA_SAFETY_ON_TRUE_RETURN_VAL(r_src, 0);
1327
1328    if (r_thumb && errno != ENOENT)
1329      ERR("could not access file \"%s\": %s", e->thumb_path, strerror(errno));
1330    else if (!r_thumb && thumb.st_mtime > src.st_mtime)
1331      r = EINA_TRUE;
1332
1333    return r;
1334 }
1335
1336 Evas *
1337 ethumb_evas_get(const Ethumb *e)
1338 {
1339    EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
1340
1341    return e->sub_e;
1342 }
1343
1344 Ecore_Evas *
1345 ethumb_ecore_evas_get(const Ethumb *e)
1346 {
1347    EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
1348
1349    return e->sub_ee;
1350 }