ethumb: Invalid access upon ethumb_free
[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    evas_object_del(ethumb->o);
320    ecore_evas_free(ethumb->ee);
321    eina_stringshare_del(ethumb->thumb_dir);
322    eina_stringshare_del(ethumb->category);
323    if (ethumb->finished_idler)
324      ecore_idler_del(ethumb->finished_idler);
325    free(ethumb);
326 }
327
328 EAPI void
329 ethumb_thumb_fdo_set(Ethumb *e, Ethumb_Thumb_FDO_Size s)
330 {
331    EINA_SAFETY_ON_NULL_RETURN(e);
332    EINA_SAFETY_ON_FALSE_RETURN(s == ETHUMB_THUMB_NORMAL ||
333                                s == ETHUMB_THUMB_LARGE);
334    DBG("ethumb=%p, size=%d", e, s);
335
336    if (s == ETHUMB_THUMB_NORMAL)
337      {
338         e->tw = THUMB_SIZE_NORMAL;
339         e->th = THUMB_SIZE_NORMAL;
340      }
341    else
342      {
343         e->tw = THUMB_SIZE_LARGE;
344         e->th = THUMB_SIZE_LARGE;
345      }
346
347    e->format = ETHUMB_THUMB_FDO;
348    e->aspect = ETHUMB_THUMB_KEEP_ASPECT;
349    _ethumb_frame_free(e->frame);
350    e->frame = NULL;
351    eina_stringshare_del(e->thumb_dir);
352    eina_stringshare_del(e->category);
353    e->thumb_dir = NULL;
354    e->category = NULL;
355 }
356
357 EAPI void
358 ethumb_thumb_size_set(Ethumb *e, int tw, int th)
359 {
360    EINA_SAFETY_ON_NULL_RETURN(e);
361    EINA_SAFETY_ON_FALSE_RETURN(tw > 0);
362    EINA_SAFETY_ON_FALSE_RETURN(th > 0);
363
364    DBG("ethumb=%p, w=%d, h=%d", e, tw, th);
365    e->tw = tw;
366    e->th = th;
367 }
368
369 EAPI void
370 ethumb_thumb_size_get(const Ethumb *e, int *tw, int *th)
371 {
372    EINA_SAFETY_ON_NULL_RETURN(e);
373
374    if (tw) *tw = e->tw;
375    if (th) *th = e->th;
376 }
377
378 EAPI void
379 ethumb_thumb_format_set(Ethumb *e, Ethumb_Thumb_Format f)
380 {
381    EINA_SAFETY_ON_NULL_RETURN(e);
382    EINA_SAFETY_ON_FALSE_RETURN(f == ETHUMB_THUMB_FDO ||
383                                f == ETHUMB_THUMB_JPEG ||
384                                f == ETHUMB_THUMB_EET);
385
386    DBG("ethumb=%p, format=%d", e, f);
387    e->format = f;
388 }
389
390 EAPI Ethumb_Thumb_Format
391 ethumb_thumb_format_get(const Ethumb *e)
392 {
393    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
394    return e->format;
395 }
396
397 EAPI void
398 ethumb_thumb_aspect_set(Ethumb *e, Ethumb_Thumb_Aspect a)
399 {
400    EINA_SAFETY_ON_NULL_RETURN(e);
401    EINA_SAFETY_ON_FALSE_RETURN(a == ETHUMB_THUMB_KEEP_ASPECT ||
402                                a == ETHUMB_THUMB_IGNORE_ASPECT ||
403                                a == ETHUMB_THUMB_CROP);
404
405    DBG("ethumb=%p, aspect=%d", e, a);
406    e->aspect = a;
407 }
408
409 EAPI Ethumb_Thumb_Aspect
410 ethumb_thumb_aspect_get(const Ethumb *e)
411 {
412    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
413    return e->aspect;
414 }
415
416 EAPI void
417 ethumb_thumb_crop_align_set(Ethumb *e, float x, float y)
418 {
419    EINA_SAFETY_ON_NULL_RETURN(e);
420
421    DBG("ethumb=%p, x=%f, y=%f", e, x, y);
422    e->crop_x = x;
423    e->crop_y = y;
424 }
425
426 EAPI void
427 ethumb_thumb_crop_align_get(const Ethumb *e, float *x, float *y)
428 {
429    EINA_SAFETY_ON_NULL_RETURN(e);
430
431    if (x) *x = e->crop_x;
432    if (y) *y = e->crop_y;
433 }
434
435 EAPI void
436 ethumb_thumb_quality_set(Ethumb *e, int quality)
437 {
438    EINA_SAFETY_ON_NULL_RETURN(e);
439
440    DBG("ethumb=%p, quality=%d", e, quality);
441    e->quality = quality;
442 }
443
444 EAPI int
445 ethumb_thumb_quality_get(const Ethumb *e)
446 {
447    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
448    return e->quality;
449 }
450
451 EAPI void
452 ethumb_thumb_compress_set(Ethumb *e, int compress)
453 {
454    EINA_SAFETY_ON_NULL_RETURN(e);
455
456    DBG("ethumb=%p, compress=%d", e, compress);
457    e->compress = compress;
458 }
459
460 EAPI int
461 ethumb_thumb_compress_get(const Ethumb *e)
462 {
463    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
464    return e->compress;
465 }
466
467 EAPI Eina_Bool
468 ethumb_frame_set(Ethumb *e, const char *theme_file, const char *group, const char *swallow)
469 {
470    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
471
472    Ethumb_Frame *frame;
473    frame = e->frame;
474
475    DBG("ethumb=%p, theme_file=%s, group=%s, swallow=%s",
476        e, theme_file ? theme_file : "", group ? group : "",
477        swallow ? swallow : "");
478
479    if (frame)
480      {
481         edje_object_part_unswallow(frame->edje, e->img);
482         if (!theme_file)
483           _ethumb_frame_free(frame);
484      }
485
486    if (!theme_file)
487      {
488         e->frame = NULL;
489         return EINA_TRUE;
490      }
491
492    if (!frame)
493      {
494         frame = calloc(1, sizeof(Ethumb_Frame));
495         if (!frame)
496           {
497              ERR("could not allocate Ethumb_Frame structure.");
498              return EINA_FALSE;
499           }
500
501         frame->edje = edje_object_add(e->sub_e);
502         if (!frame->edje)
503           {
504              ERR("could not create edje frame object.");
505              _ethumb_frame_free(frame);
506              e->frame = NULL;
507              return EINA_FALSE;
508           }
509      }
510
511    if (!edje_object_file_set(frame->edje, theme_file, group))
512      {
513         ERR("could not load frame theme.");
514         _ethumb_frame_free(frame);
515         e->frame = NULL;
516         return EINA_FALSE;
517      }
518
519    edje_object_part_swallow(frame->edje, swallow, e->img);
520    if (!edje_object_part_swallow_get(frame->edje, swallow))
521      {
522         ERR("could not swallow image to edje frame.");
523         _ethumb_frame_free(frame);
524         e->frame = NULL;
525         return EINA_FALSE;
526      }
527
528    eina_stringshare_replace(&frame->file, theme_file);
529    eina_stringshare_replace(&frame->group, group);
530    eina_stringshare_replace(&frame->swallow, swallow);
531
532    e->frame = frame;
533
534    return EINA_TRUE;
535 }
536
537 EAPI void
538 ethumb_frame_get(const Ethumb *e, const char **theme_file, const char **group, const char **swallow)
539 {
540    EINA_SAFETY_ON_NULL_RETURN(e);
541
542    if (e->frame)
543      {
544         if (theme_file) *theme_file = e->frame->file;
545         if (group) *group = e->frame->group;
546         if (swallow) *swallow = e->frame->swallow;
547      }
548    else
549      {
550         if (theme_file) *theme_file = NULL;
551         if (group) *group = NULL;
552         if (swallow) *swallow = NULL;
553      }
554 }
555
556 static const char *
557 _ethumb_build_absolute_path(const char *path, char buf[PATH_MAX])
558 {
559    char *p;
560    int len;
561
562    if (!path)
563      return NULL;
564
565    p = buf;
566
567    if (path[0] == '/')
568      strcpy(p, path);
569    else if (path[0] == '~')
570      {
571         const char *home = getenv("HOME");
572         if (!home)
573           return NULL;
574         strcpy(p, home);
575         len = strlen(p);
576         p += len;
577         p[0] = '/';
578         p++;
579         strcpy(p, path + 2);
580      }
581    else
582      {
583         if (!getcwd(p, PATH_MAX))
584           return NULL;
585         len = strlen(p);
586         p += len;
587         p[0] = '/';
588         p++;
589         strcpy(p, path);
590      }
591
592    return buf;
593 }
594
595 EAPI void
596 ethumb_thumb_dir_path_set(Ethumb *e, const char *path)
597 {
598    char buf[PATH_MAX];
599    EINA_SAFETY_ON_NULL_RETURN(e);
600
601    DBG("ethumb=%p, path=%s", e, path ? path : "");
602    path = _ethumb_build_absolute_path(path, buf);
603    eina_stringshare_replace(&e->thumb_dir, path);
604 }
605
606 EAPI const char *
607 ethumb_thumb_dir_path_get(const Ethumb *e)
608 {
609    EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
610
611    return e->thumb_dir;
612 }
613
614 EAPI void
615 ethumb_thumb_category_set(Ethumb *e, const char *category)
616 {
617    EINA_SAFETY_ON_NULL_RETURN(e);
618
619    DBG("ethumb=%p, category=%s", e, category ? category : "");
620    eina_stringshare_replace(&e->category, category);
621 }
622
623 EAPI const char *
624 ethumb_thumb_category_get(const Ethumb *e)
625 {
626    EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
627
628    return e->category;
629 }
630
631 EAPI void
632 ethumb_video_start_set(Ethumb *e, float start)
633 {
634    EINA_SAFETY_ON_NULL_RETURN(e);
635    EINA_SAFETY_ON_FALSE_RETURN(start >= 0.0);
636    EINA_SAFETY_ON_FALSE_RETURN(start <= 1.0);
637
638    DBG("ethumb=%p, video_start=%f", e, start);
639    e->video.start = start;
640 }
641
642 EAPI float
643 ethumb_video_start_get(const Ethumb *e)
644 {
645    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
646
647    return e->video.start;
648 }
649
650 EAPI void
651 ethumb_video_time_set(Ethumb *e, float time)
652 {
653    EINA_SAFETY_ON_NULL_RETURN(e);
654
655    DBG("ethumb=%p, video_start=%f", e, time);
656    e->video.time = time;
657 }
658
659 EAPI float
660 ethumb_video_time_get(const Ethumb *e)
661 {
662    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
663
664    return e->video.time;
665 }
666
667 EAPI void
668 ethumb_video_interval_set(Ethumb *e, float interval)
669 {
670    EINA_SAFETY_ON_NULL_RETURN(e);
671
672    DBG("ethumb=%p, video_interval=%f", e, interval);
673    e->video.interval = interval;
674 }
675
676 EAPI float
677 ethumb_video_interval_get(const Ethumb *e)
678 {
679    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
680
681    return e->video.interval;
682 }
683
684 EAPI void
685 ethumb_video_ntimes_set(Ethumb *e, unsigned int ntimes)
686 {
687    EINA_SAFETY_ON_NULL_RETURN(e);
688    EINA_SAFETY_ON_FALSE_RETURN(ntimes > 0);
689
690    DBG("ethumb=%p, video_ntimes=%d", e, ntimes);
691    e->video.ntimes = ntimes;
692 }
693
694 EAPI unsigned int
695 ethumb_video_ntimes_get(const Ethumb *e)
696 {
697    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
698
699    return e->video.ntimes;
700 }
701
702 EAPI void
703 ethumb_video_fps_set(Ethumb *e, unsigned int fps)
704 {
705    EINA_SAFETY_ON_NULL_RETURN(e);
706    EINA_SAFETY_ON_FALSE_RETURN(fps > 0);
707
708    DBG("ethumb=%p, video_fps=%d", e, fps);
709    e->video.fps = fps;
710 }
711
712 EAPI unsigned int
713 ethumb_video_fps_get(const Ethumb *e)
714 {
715    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
716
717    return e->video.fps;
718 }
719
720 EAPI void
721 ethumb_document_page_set(Ethumb *e, unsigned int page)
722 {
723    EINA_SAFETY_ON_NULL_RETURN(e);
724
725    DBG("ethumb=%p, document_page=%d", e, page);
726    e->document.page = page;
727 }
728
729 EAPI unsigned int
730 ethumb_document_page_get(const Ethumb *e)
731 {
732    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
733
734    return e->document.page;
735 }
736
737 EAPI Eina_Bool
738 ethumb_file_set(Ethumb *e, const char *path, const char *key)
739 {
740    char buf[PATH_MAX];
741    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
742
743    DBG("ethumb=%p, path=%s, key=%s", e, path ? path : "", key ? key : "");
744    if (path && access(path, R_OK))
745      {
746         ERR("couldn't access file \"%s\"", path);
747         return EINA_FALSE;
748      }
749
750    path = _ethumb_build_absolute_path(path, buf);
751    eina_stringshare_replace(&e->src_path, path);
752    eina_stringshare_replace(&e->src_key, key);
753    eina_stringshare_replace(&e->thumb_path, NULL);
754    eina_stringshare_replace(&e->thumb_key, NULL);
755
756    return EINA_TRUE;
757 }
758
759 EAPI void
760 ethumb_file_get(const Ethumb *e, const char **path, const char **key)
761 {
762    EINA_SAFETY_ON_NULL_RETURN(e);
763
764    if (path) *path = e->src_path;
765    if (key) *key = e->src_key;
766 }
767
768 static const char ACCEPTABLE_URI_CHARS[96] = {
769      /*      !    "    #    $    %    &    '    (    )    *    +    ,    -    .    / */ 
770      0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
771      /* 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? */
772      0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
773      /* @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O */
774      0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
775      /* P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _ */
776      0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
777      /* `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o */
778      0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
779      /* p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~  DEL */
780      0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
781 };
782
783 static const char *
784 _ethumb_generate_hash(const char *file)
785 {
786   int n;
787   MD5_CTX ctx;
788   char md5out[(2 * MD5_HASHBYTES) + 1];
789   unsigned char hash[MD5_HASHBYTES];
790   static const char hex[] = "0123456789abcdef";
791
792   char *uri;
793   char *t;
794   const unsigned char *c;
795
796 #define _check_uri_char(c) \
797   ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
798
799   EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
800
801   uri = alloca(3 * strlen(file) + 9);
802   memcpy(uri, "file://", sizeof("file://") - 1);
803   t = uri + sizeof("file://") - 1;
804
805   for (c = (const unsigned char *)file; *c != '\0'; c++)
806     {
807        if (!_check_uri_char(*c))
808          {
809             *t++ = '%';
810             *t++ = hex[*c >> 4];
811             *t++ = hex[*c & 15];
812          }
813        else
814          *t++ = *c;
815     }
816   *t = '\0';
817
818 #undef _check_uri_char
819
820   MD5Init (&ctx);
821   MD5Update (&ctx, (unsigned char const*)uri, (unsigned)strlen (uri));
822   MD5Final (hash, &ctx);
823
824   for (n = 0; n < MD5_HASHBYTES; n++)
825     {
826       md5out[2 * n] = hex[hash[n] >> 4];
827       md5out[2 * n + 1] = hex[hash[n] & 0x0f];
828     }
829   md5out[2 * n] = '\0';
830
831   DBG("md5=%s, file=%s", md5out, file);
832   return eina_stringshare_add(md5out);
833 }
834
835 static int
836 _ethumb_file_check_fdo(Ethumb *e)
837 {
838    if (!((e->tw == THUMB_SIZE_NORMAL && e->th == THUMB_SIZE_NORMAL) ||
839        (e->tw == THUMB_SIZE_LARGE && e->th == THUMB_SIZE_LARGE)))
840      return 0;
841
842    if (e->format != ETHUMB_THUMB_FDO)
843      return 0;
844
845    if (e->aspect != ETHUMB_THUMB_KEEP_ASPECT)
846      return 0;
847
848    if (e->frame)
849      return 0;
850
851    return 1;
852 }
853
854 static const char *
855 _ethumb_file_generate_custom_category(Ethumb *e)
856 {
857    char buf[PATH_MAX];
858    const char *aspect, *format;
859    const char *frame;
860
861    if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
862      aspect = "keep_aspect";
863    else if (e->aspect == ETHUMB_THUMB_IGNORE_ASPECT)
864      aspect = "ignore_aspect";
865    else
866      aspect = "crop";
867
868    if (e->format == ETHUMB_THUMB_FDO)
869      format = "png";
870    else if (e->format == ETHUMB_THUMB_JPEG)
871      format = "jpg";
872    else
873      format = "eet";
874
875    if (e->frame)
876      frame = "-framed";
877    else
878      frame = "";
879
880    snprintf(buf, sizeof(buf), "%dx%d-%s%s-%s",
881             e->tw, e->th, aspect, frame, format);
882
883    return eina_stringshare_add(buf);
884 }
885
886 static void
887 _ethumb_file_generate_path(Ethumb *e)
888 {
889    char buf[PATH_MAX];
890    char *fullname;
891    const char *hash;
892    const char *thumb_dir, *category;
893    const char *ext;
894    int fdo_format;
895
896
897    fdo_format = _ethumb_file_check_fdo(e);
898
899    if (e->thumb_dir)
900      thumb_dir = eina_stringshare_ref(e->thumb_dir);
901    else
902      thumb_dir = eina_stringshare_ref(_home_thumb_dir);
903
904    if (e->category)
905      category = eina_stringshare_ref(e->category);
906    else if (!fdo_format)
907      category = _ethumb_file_generate_custom_category(e);
908    else
909      {
910         if (e->tw == THUMB_SIZE_NORMAL)
911           category = eina_stringshare_ref(_thumb_category_normal);
912         else if (e->tw == THUMB_SIZE_LARGE)
913           category = eina_stringshare_ref(_thumb_category_large);
914         else
915           {
916              ERR("fdo_format but size %d is not NORMAL (%d) or LARGE (%d)?",
917                  e->tw, THUMB_SIZE_NORMAL, THUMB_SIZE_LARGE);
918              category = "unknown";
919           }
920      }
921
922    if (e->format == ETHUMB_THUMB_FDO)
923      ext = "png";
924    else if (e->format == ETHUMB_THUMB_JPEG)
925      ext = "jpg";
926    else
927      ext = "eet";
928
929
930    fullname = ecore_file_realpath(e->src_path);
931    hash = _ethumb_generate_hash(fullname);
932    snprintf(buf, sizeof(buf), "%s/%s/%s.%s", thumb_dir, category, hash, ext);
933    free(fullname);
934    DBG("ethumb=%p, path=%s", e, buf);
935    eina_stringshare_replace(&e->thumb_path, buf);
936    if (e->format == ETHUMB_THUMB_EET)
937      eina_stringshare_replace(&e->thumb_key, "thumbnail");
938    else
939      {
940         eina_stringshare_del(e->thumb_key);
941         e->thumb_key = NULL;
942      }
943
944    eina_stringshare_del(thumb_dir);
945    eina_stringshare_del(category);
946    eina_stringshare_del(hash);
947 }
948
949 EAPI void
950 ethumb_file_free(Ethumb *e)
951 {
952    EINA_SAFETY_ON_NULL_RETURN(e);
953    DBG("ethumb=%p", e);
954
955    eina_stringshare_replace(&e->src_path, NULL);
956    eina_stringshare_replace(&e->src_key, NULL);
957    eina_stringshare_replace(&e->thumb_path, NULL);
958    eina_stringshare_replace(&e->thumb_key, NULL);
959 }
960
961 EAPI void
962 ethumb_thumb_path_set(Ethumb *e, const char *path, const char *key)
963 {
964    char buf[PATH_MAX];
965
966    EINA_SAFETY_ON_NULL_RETURN(e);
967    DBG("ethumb=%p, path=%s, key=%s", e, path ? path : "", key ? key : "");
968
969    if (!path)
970      {
971         eina_stringshare_replace(&e->thumb_path, NULL);
972         eina_stringshare_replace(&e->thumb_key, NULL);
973      }
974    else
975      {
976         path = _ethumb_build_absolute_path(path, buf);
977         eina_stringshare_replace(&e->thumb_path, path);
978         eina_stringshare_replace(&e->thumb_key, key);
979      }
980 }
981
982 EAPI void
983 ethumb_thumb_path_get(Ethumb *e, const char **path, const char **key)
984 {
985    EINA_SAFETY_ON_NULL_RETURN(e);
986    if (!e->thumb_path)
987      _ethumb_file_generate_path(e);
988
989    if (path) *path = e->thumb_path;
990    if (key) *key = e->thumb_key;
991 }
992
993 void
994 ethumb_calculate_aspect_from_ratio(Ethumb *e, float ia, int *w, int *h)
995 {
996    float a;
997
998    *w = e->tw;
999    *h = e->th;
1000
1001    if (ia == 0)
1002      return;
1003
1004    a = e->tw / (float)e->th;
1005
1006    if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
1007      {
1008         if ((ia > a && e->tw > 0) || e->th <= 0)
1009           *h = e->tw / ia;
1010         else
1011           *w = e->th * ia;
1012      }
1013 }
1014
1015 void
1016 ethumb_calculate_aspect(Ethumb *e, int iw, int ih, int *w, int *h)
1017 {
1018    float ia;
1019
1020    ia = iw / (float)ih;
1021
1022    ethumb_calculate_aspect_from_ratio(e, ia, w, h);
1023 }
1024
1025 void
1026 ethumb_calculate_fill_from_ratio(Ethumb *e, float ia, int *fx, int *fy, int *fw, int *fh)
1027 {
1028    float a;
1029
1030    *fw = e->tw;
1031    *fh = e->th;
1032    *fx = 0;
1033    *fy = 0;
1034
1035    if (ia == 0)
1036      return;
1037
1038    a = e->tw / (float)e->th;
1039
1040    if (e->aspect == ETHUMB_THUMB_CROP)
1041      {
1042         if ((ia > a && e->tw > 0) || e->th <= 0)
1043           *fw = e->th * ia;
1044         else
1045           *fh = e->tw / ia;
1046
1047         *fx = - e->crop_x * (*fw - e->tw);
1048         *fy = - e->crop_y * (*fh - e->th);
1049      }
1050    else if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
1051      {
1052         if ((ia > a && e->tw > 0) || e->th <= 0)
1053           *fh = e->tw / ia;
1054         else
1055           *fw = e->th * ia;
1056      }
1057 }
1058
1059 void
1060 ethumb_calculate_fill(Ethumb *e, int iw, int ih, int *fx, int *fy, int *fw, int *fh)
1061 {
1062    float ia;
1063    ia = iw / (float)ih;
1064
1065    ethumb_calculate_fill_from_ratio(e, ia, fx, fy, fw, fh);
1066 }
1067
1068 static Eina_Bool
1069 _ethumb_plugin_generate(Ethumb *e)
1070 {
1071    const char *extp;
1072    char ext[PATH_MAX];
1073    Ethumb_Plugin *plugin;
1074    int i;
1075
1076    extp = strrchr(e->src_path, '.');
1077    if (!extp)
1078      {
1079         ERR("could not get extension for file \"%s\"", e->src_path);
1080         return EINA_FALSE;
1081      }
1082
1083    for (i = 0; extp[i] != '\0'; i++)
1084         ext[i] = tolower(extp[i + 1]);
1085
1086    plugin = eina_hash_find(_plugins_ext, ext);
1087    if (!plugin)
1088      {
1089         DBG("no plugin for extension: \"%s\"", ext);
1090         return EINA_FALSE;
1091      }
1092
1093    if (e->frame)
1094      evas_object_hide(e->frame->edje);
1095    else
1096      evas_object_hide(e->img);
1097
1098    plugin->generate_thumb(e);
1099
1100    return EINA_TRUE;
1101 }
1102
1103 Eina_Bool
1104 ethumb_plugin_image_resize(Ethumb *e, int w, int h)
1105 {
1106    Evas_Object *img;
1107
1108    img = e->img;
1109
1110    if (e->frame)
1111      {
1112         edje_extern_object_min_size_set(img, w, h);
1113         edje_extern_object_max_size_set(img, w, h);
1114         edje_object_calc_force(e->frame->edje);
1115         evas_object_move(e->frame->edje, 0, 0);
1116         evas_object_resize(e->frame->edje, w, h);
1117      }
1118    else
1119      {
1120         evas_object_move(img, 0, 0);
1121         evas_object_resize(img, w, h);
1122      }
1123
1124    evas_object_image_size_set(e->o, w, h);
1125    ecore_evas_resize(e->sub_ee, w, h);
1126
1127    e->rw = w;
1128    e->rh = h;
1129
1130    return EINA_TRUE;
1131 }
1132
1133 Eina_Bool
1134 ethumb_image_save(Ethumb *e)
1135 {
1136    Eina_Bool r;
1137    char *dname;
1138    char flags[256];
1139
1140    evas_damage_rectangle_add(e->sub_e, 0, 0, e->rw, e->rh);
1141    evas_render(e->sub_e);
1142
1143    if (!e->thumb_path)
1144      _ethumb_file_generate_path(e);
1145
1146    if (!e->thumb_path)
1147      {
1148         ERR("could not create file path...");
1149         return EINA_FALSE;
1150      }
1151
1152    dname = ecore_file_dir_get(e->thumb_path);
1153    r = ecore_file_mkpath(dname);
1154    free(dname);
1155    if (!r)
1156      {
1157         ERR("could not create directory '%s'", dname);
1158         return EINA_FALSE;
1159      }
1160
1161    snprintf(flags, sizeof(flags), "quality=%d compress=%d",
1162             e->quality, e->compress);
1163    r = evas_object_image_save(e->o, e->thumb_path, e->thumb_key, flags);
1164
1165    if (!r)
1166      {
1167         ERR("could not save image: path=%s, key=%s", e->thumb_path,
1168             e->thumb_key);
1169         return EINA_FALSE;
1170      }
1171
1172    return EINA_TRUE;
1173 }
1174
1175 static int
1176 _ethumb_image_load(Ethumb *e)
1177 {
1178    int error;
1179    Evas_Coord w, h, ww, hh, fx, fy, fw, fh;
1180    Evas_Object *img;
1181
1182    img = e->img;
1183
1184    if (e->frame)
1185      evas_object_hide(e->frame->edje);
1186    else
1187      evas_object_hide(img);
1188    evas_object_image_file_set(img, NULL, NULL);
1189    evas_object_image_load_size_set(img, e->tw, e->th);
1190    evas_object_image_file_set(img, e->src_path, e->src_key);
1191
1192    if (e->frame)
1193      evas_object_show(e->frame->edje);
1194    else
1195      evas_object_show(img);
1196
1197    error = evas_object_image_load_error_get(img);
1198    if (error != EVAS_LOAD_ERROR_NONE)
1199      {
1200         ERR("could not load image '%s': %d", e->src_path, error);
1201         return 0;
1202      }
1203
1204    evas_object_image_size_get(img, &w, &h);
1205    if ((w <= 0) || (h <= 0))
1206      return 0;
1207
1208    ethumb_calculate_aspect(e, w, h, &ww, &hh);
1209
1210    if (e->frame)
1211      {
1212         edje_extern_object_min_size_set(img, ww, hh);
1213         edje_extern_object_max_size_set(img, ww, hh);
1214         edje_object_calc_force(e->frame->edje);
1215         evas_object_move(e->frame->edje, 0, 0);
1216         evas_object_resize(e->frame->edje, ww, hh);
1217      }
1218    else
1219      {
1220         evas_object_move(img, 0, 0);
1221         evas_object_resize(img, ww, hh);
1222      }
1223
1224    ethumb_calculate_fill(e, w, h, &fx, &fy, &fw, &fh);
1225    evas_object_image_fill_set(img, fx, fy, fw, fh);
1226
1227    evas_object_image_size_set(e->o, ww, hh);
1228    ecore_evas_resize(e->sub_ee, ww, hh);
1229
1230    e->rw = ww;
1231    e->rh = hh;
1232
1233    return 1;
1234 }
1235
1236 static Eina_Bool
1237 _ethumb_finished_idler_cb(void *data)
1238 {
1239    Ethumb *e = data;
1240
1241    e->finished_cb(e->cb_data, e, e->cb_result);
1242    if (e->cb_data_free)
1243      e->cb_data_free(e->cb_data);
1244    e->finished_idler = NULL;
1245    e->finished_cb = NULL;
1246    e->cb_data = NULL;
1247    e->cb_data_free = NULL;
1248
1249    return EINA_FALSE;
1250 }
1251
1252 void
1253 ethumb_finished_callback_call(Ethumb *e, int result)
1254 {
1255    EINA_SAFETY_ON_NULL_RETURN(e);
1256
1257    e->cb_result = result;
1258    if (e->finished_idler)
1259      ecore_idler_del(e->finished_idler);
1260    e->finished_idler = ecore_idler_add(_ethumb_finished_idler_cb, e);
1261 }
1262
1263 EAPI Eina_Bool
1264 ethumb_generate(Ethumb *e, Ethumb_Generate_Cb finished_cb, const void *data, Eina_Free_Cb free_data)
1265 {
1266    int r;
1267
1268    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
1269    EINA_SAFETY_ON_NULL_RETURN_VAL(finished_cb, 0);
1270    DBG("ethumb=%p, finished_cb=%p, data=%p, free_data=%p, path=%s, key=%s",
1271        e, finished_cb, data, free_data,
1272        e->src_path ? e->src_path : "", e->src_key ? e->src_key : "");
1273
1274    if (e->finished_idler)
1275      {
1276         ERR("thumbnail generation already in progress.");
1277         return EINA_FALSE;
1278      }
1279    e->finished_cb = finished_cb;
1280    e->cb_data = (void *)data;
1281    e->cb_data_free = free_data;
1282
1283    if (!e->src_path)
1284      {
1285         ERR("no file set.");
1286         ethumb_finished_callback_call(e, 0);
1287         return EINA_TRUE;
1288      }
1289
1290    r = _ethumb_plugin_generate(e);
1291    if (r)
1292      return EINA_TRUE;
1293
1294    if (!_ethumb_image_load(e))
1295      {
1296         ERR("could not load input image.");
1297         ethumb_finished_callback_call(e, 0);
1298         return EINA_TRUE;
1299      }
1300
1301    r = ethumb_image_save(e);
1302
1303    ethumb_finished_callback_call(e, r);
1304
1305    return EINA_TRUE;
1306 }
1307
1308 EAPI Eina_Bool
1309 ethumb_exists(Ethumb *e)
1310 {
1311    struct stat thumb, src;
1312    int r_thumb, r_src;
1313    Eina_Bool r = EINA_FALSE;
1314
1315    EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
1316    EINA_SAFETY_ON_NULL_RETURN_VAL(e->src_path, 0);
1317    DBG("ethumb=%p, path=%s", e, e->src_path ? e->src_path : "");
1318
1319    if (!e->thumb_path)
1320      _ethumb_file_generate_path(e);
1321
1322    EINA_SAFETY_ON_NULL_RETURN_VAL(e->thumb_path, 0);
1323
1324    r_thumb = stat(e->thumb_path, &thumb);
1325    r_src = stat(e->src_path, &src);
1326
1327    EINA_SAFETY_ON_TRUE_RETURN_VAL(r_src, 0);
1328
1329    if (r_thumb && errno != ENOENT)
1330      ERR("could not access file \"%s\": %s", e->thumb_path, strerror(errno));
1331    else if (!r_thumb && thumb.st_mtime > src.st_mtime)
1332      r = EINA_TRUE;
1333
1334    return r;
1335 }
1336
1337 Evas *
1338 ethumb_evas_get(const Ethumb *e)
1339 {
1340    EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
1341
1342    return e->sub_e;
1343 }
1344
1345 Ecore_Evas *
1346 ethumb_ecore_evas_get(const Ethumb *e)
1347 {
1348    EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
1349
1350    return e->sub_ee;
1351 }