update for beta release
[framework/uifw/e17.git] / src / bin / e_bg.c
1 #include "e.h"
2
3 /* local subsystem functions */
4 static void _e_bg_signal(void *data, Evas_Object *obj, const char *emission, const char *source);
5 static void _e_bg_event_bg_update_free(void *data, void *event);
6
7 static void _e_bg_image_import_dialog_done(void *data, const char *path, Eina_Bool ok, Eina_Bool external, int quality, E_Image_Import_Mode mode);
8 static void _e_bg_image_import_done(void *data, Eina_Bool ok, const char *image_path, const char *edje_path);
9 static void _e_bg_handler_image_imported(void *data, const char *image_path);
10
11 /* local subsystem globals */
12 EAPI int E_EVENT_BG_UPDATE = 0;
13 static E_Fm2_Mime_Handler *bg_hdl = NULL;
14
15 struct _E_Bg_Image_Import_Handle
16 {
17    struct {
18       void (*func)(void *data, const char *edje_file);
19       void *data;
20    } cb;
21    E_Dialog *dia;
22    E_Util_Image_Import_Handle *importer;
23    Eina_Bool canceled:1;
24 };
25
26 /* externally accessible functions */
27 EINTERN int
28 e_bg_init(void)
29 {
30    Eina_List *l = NULL;
31    E_Config_Desktop_Background *cfbg = NULL;
32
33    /* Register mime handler */
34    bg_hdl = e_fm2_mime_handler_new(_("Set As Background"),
35                                    "preferences-desktop-wallpaper",
36                                    e_bg_handler_set, NULL,
37                                    e_bg_handler_test, NULL);
38    if (bg_hdl)
39      {
40         e_fm2_mime_handler_glob_add(bg_hdl, "*.edj");
41         e_fm2_mime_handler_mime_add(bg_hdl, "image/png");
42         e_fm2_mime_handler_mime_add(bg_hdl, "image/jpeg");
43      }
44
45    /* Register files in use */
46    if (e_config->desktop_default_background)
47      e_filereg_register(e_config->desktop_default_background);
48
49    EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
50      {
51         if (!cfbg) continue;
52         e_filereg_register(cfbg->file);
53      }
54
55    E_EVENT_BG_UPDATE = ecore_event_type_new();
56    return 1;
57 }
58
59 EINTERN int
60 e_bg_shutdown(void)
61 {
62    Eina_List *l = NULL;
63    E_Config_Desktop_Background *cfbg = NULL;
64
65    /* Deregister mime handler */
66    if (bg_hdl)
67      {
68         e_fm2_mime_handler_glob_del(bg_hdl, "*.edj");
69         e_fm2_mime_handler_free(bg_hdl);
70      }
71
72    /* Deregister files in use */
73    if (e_config->desktop_default_background)
74      e_filereg_deregister(e_config->desktop_default_background);
75
76    EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
77      {
78         if (!cfbg) continue;
79         e_filereg_deregister(cfbg->file);
80      }
81
82    return 1;
83 }
84
85 /**
86  * Find the configuration for a given desktop background
87  * Use -1 as a wild card for each parameter.
88  * The most specific match will be returned
89  */
90 EAPI const E_Config_Desktop_Background *
91 e_bg_config_get(int container_num, int zone_num, int desk_x, int desk_y)
92 {
93    Eina_List *l, *ll, *entries;
94    E_Config_Desktop_Background *bg = NULL, *cfbg = NULL;
95    const char *bgfile = "";
96    char *entry;
97    int current_spec = 0; /* how specific the setting is - we want the least general one that applies */
98
99    /* look for desk specific background. */
100    if (container_num >= 0 || zone_num >= 0 || desk_x >= 0 || desk_y >= 0)
101      {
102         EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
103           {
104              int spec;
105              
106              if (!cfbg) continue;
107              spec = 0;
108              if (cfbg->container == container_num) spec++;
109              else if (cfbg->container >= 0) continue;
110              if (cfbg->zone == zone_num) spec++;
111              else if (cfbg->zone >= 0) continue;
112              if (cfbg->desk_x == desk_x) spec++;
113              else if (cfbg->desk_x >= 0) continue;
114              if (cfbg->desk_y == desk_y) spec++;
115              else if (cfbg->desk_y >= 0) continue;
116
117              if (spec <= current_spec) continue;
118              bgfile = cfbg->file;
119              if (bgfile)
120                {
121                   if (bgfile[0] != '/')
122                     {
123                        const char *bf;
124
125                        bf = e_path_find(path_backgrounds, bgfile);
126                        if (bf) bgfile = bf;
127                     }
128                }
129              if (eina_str_has_extension(bgfile, ".edj"))
130                {
131                   entries = edje_file_collection_list(bgfile);
132                   if (entries)
133                     {
134                        EINA_LIST_FOREACH(entries, ll, entry)
135                          {
136                             if (!strcmp(entry, "e/desktop/background"))
137                               {
138                                  bg = cfbg;
139                                  current_spec = spec;
140                               }
141                          }
142                        edje_file_collection_list_free(entries);
143                     }
144                }
145              else
146                {
147                   bg = cfbg;
148                   current_spec = spec;
149                }
150           }
151      }
152    return bg;
153 }
154
155 EAPI const char *
156 e_bg_file_get(int container_num, int zone_num, int desk_x, int desk_y)
157 {
158    const E_Config_Desktop_Background *cfbg;
159    Eina_List *l, *entries;
160    const char *bgfile = "";
161    char *entry;
162    int ok = 0;
163
164    cfbg = e_bg_config_get(container_num, zone_num, desk_x, desk_y);
165
166    /* fall back to default */
167    if (cfbg)
168      {
169         bgfile = cfbg->file;
170         if (bgfile)
171           {
172              if (bgfile[0] != '/')
173                {
174                   const char *bf;
175
176                   bf = e_path_find(path_backgrounds, bgfile);
177                   if (bf) bgfile = bf;
178                }
179           }
180      }
181    else
182      {
183         bgfile = e_config->desktop_default_background;
184         if (bgfile)
185           {
186              if (bgfile[0] != '/')
187                {
188                   const char *bf;
189
190                   bf = e_path_find(path_backgrounds, bgfile);
191                   if (bf) bgfile = bf;
192                }
193           }
194         if (bgfile && eina_str_has_extension(bgfile, ".edj"))
195           {
196              entries = edje_file_collection_list(bgfile);
197              if (entries)
198                {
199                   EINA_LIST_FOREACH(entries, l, entry)
200                     {
201                        if (!strcmp(entry, "e/desktop/background"))
202                          {
203                             ok = 1;
204                             break;
205                          }
206                     }
207                   edje_file_collection_list_free(entries);
208                }
209           }
210         else if ((bgfile) && (bgfile[0])) ok = 1;
211         if (!ok)
212           bgfile = e_theme_edje_file_get("base/theme/background",
213                                          "e/desktop/background");
214      }
215
216    return bgfile;
217 }
218
219 EAPI void
220 e_bg_zone_update(E_Zone *zone, E_Bg_Transition transition)
221 {
222    Evas_Object *o;
223    const char *bgfile = "";
224    const char *trans = "";
225    E_Desk *desk;
226
227    if (transition == E_BG_TRANSITION_START) trans = e_config->transition_start;
228    else if (transition == E_BG_TRANSITION_DESK) trans = e_config->transition_desk;
229    else if (transition == E_BG_TRANSITION_CHANGE) trans = e_config->transition_change;
230    if ((!trans) || (!trans[0])) transition = E_BG_TRANSITION_NONE;
231
232    desk = e_desk_current_get(zone);
233    if (desk)
234      bgfile = e_bg_file_get(zone->container->num, zone->num, desk->x, desk->y);
235    else
236      bgfile = e_bg_file_get(zone->container->num, zone->num, -1, -1);
237
238    if (zone->bg_object)
239      {
240         const char *pfile = "";
241
242         edje_object_file_get(zone->bg_object, &pfile, NULL);
243         if (!e_util_strcmp(pfile, bgfile)) return;
244      }
245
246    if (transition == E_BG_TRANSITION_NONE)
247      {
248         if (zone->bg_object)
249           {
250              evas_object_del(zone->bg_object);
251              zone->bg_object = NULL;
252           }
253      }
254    else
255      {
256         char buf[4096];
257
258         if (zone->bg_object)
259           {
260              if (zone->prev_bg_object)
261                evas_object_del(zone->prev_bg_object);
262              zone->prev_bg_object = zone->bg_object;
263              if (zone->transition_object)
264                evas_object_del(zone->transition_object);
265              zone->transition_object = NULL;
266              zone->bg_object = NULL;
267           }
268         o = edje_object_add(zone->container->bg_evas);
269         zone->transition_object = o;
270         /* FIXME: segv if zone is deleted while up??? */
271         evas_object_data_set(o, "e_zone", zone);
272         snprintf(buf, sizeof(buf), "e/transitions/%s", trans);
273         e_theme_edje_object_set(o, "base/theme/transitions", buf);
274         edje_object_signal_callback_add(o, "e,state,done", "*", _e_bg_signal, zone);
275         evas_object_move(o, zone->x, zone->y);
276         evas_object_resize(o, zone->w, zone->h);
277         evas_object_layer_set(o, -1);
278         evas_object_clip_set(o, zone->bg_clip_object);
279         evas_object_show(o);
280      }
281    if (eina_str_has_extension(bgfile, ".edj"))
282      {
283         o = edje_object_add(zone->container->bg_evas);
284         evas_object_data_set(o, "e_zone", zone);
285         edje_object_file_set(o, bgfile, "e/desktop/background");
286      }
287    else
288      {
289         o = e_icon_add(zone->container->bg_evas);
290         evas_object_data_set(o, "e_zone", zone);
291         e_icon_file_key_set(o, bgfile, NULL);
292         e_icon_fill_inside_set(o, 0);
293      }
294    zone->bg_object = o;
295    if (transition == E_BG_TRANSITION_NONE)
296      {
297         evas_object_move(o, zone->x, zone->y);
298         evas_object_resize(o, zone->w, zone->h);
299         evas_object_layer_set(o, -1);
300      }
301    evas_object_clip_set(o, zone->bg_clip_object);
302    evas_object_show(o);
303
304    if (transition != E_BG_TRANSITION_NONE)
305      {
306         edje_extern_object_max_size_set(zone->prev_bg_object, 65536, 65536);
307         edje_extern_object_min_size_set(zone->prev_bg_object, 0, 0);
308         edje_object_part_swallow(zone->transition_object, "e.swallow.bg.old",
309                                  zone->prev_bg_object);
310         edje_extern_object_max_size_set(zone->bg_object, 65536, 65536);
311         edje_extern_object_min_size_set(zone->bg_object, 0, 0);
312         edje_object_part_swallow(zone->transition_object, "e.swallow.bg.new",
313                                  zone->bg_object);
314         edje_object_signal_emit(zone->transition_object, "e,action,start", "e");
315      }
316 }
317
318 EAPI void
319 e_bg_default_set(const char *file)
320 {
321    E_Event_Bg_Update *ev;
322    Eina_Bool changed;
323
324    file = eina_stringshare_add(file);
325    changed = file != e_config->desktop_default_background;
326
327    if (!changed)
328      {
329         eina_stringshare_del(file);
330         return;
331      }
332
333    if (e_config->desktop_default_background)
334      {
335         e_filereg_deregister(e_config->desktop_default_background);
336         eina_stringshare_del(e_config->desktop_default_background);
337      }
338
339    if (file)
340      {
341         e_filereg_register(file);
342         e_config->desktop_default_background = file;
343      }
344    else
345      e_config->desktop_default_background = NULL;
346
347    ev = E_NEW(E_Event_Bg_Update, 1);
348    ev->container = -1;
349    ev->zone = -1;
350    ev->desk_x = -1;
351    ev->desk_y = -1;
352    ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
353 }
354
355 EAPI void
356 e_bg_add(int container, int zone, int desk_x, int desk_y, const char *file)
357 {
358    const Eina_List *l;
359    E_Config_Desktop_Background *cfbg;
360    E_Event_Bg_Update *ev;
361
362    file = eina_stringshare_add(file);
363
364    EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
365      {
366         if ((cfbg) &&
367             (cfbg->container == container) &&
368             (cfbg->zone == zone) &&
369             (cfbg->desk_x == desk_x) &&
370             (cfbg->desk_y == desk_y) &&
371             (cfbg->file == file))
372           {
373              eina_stringshare_del(file);
374              return;
375           }
376      }
377
378    e_bg_del(container, zone, desk_x, desk_y);
379    cfbg = E_NEW(E_Config_Desktop_Background, 1);
380    cfbg->container = container;
381    cfbg->zone = zone;
382    cfbg->desk_x = desk_x;
383    cfbg->desk_y = desk_y;
384    cfbg->file = file;
385    e_config->desktop_backgrounds = eina_list_append(e_config->desktop_backgrounds, cfbg);
386
387    e_filereg_register(cfbg->file);
388
389    ev = E_NEW(E_Event_Bg_Update, 1);
390    ev->container = container;
391    ev->zone = zone;
392    ev->desk_x = desk_x;
393    ev->desk_y = desk_y;
394    ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
395 }
396
397 EAPI void
398 e_bg_del(int container, int zone, int desk_x, int desk_y)
399 {
400    Eina_List *l;
401    E_Config_Desktop_Background *cfbg;
402    E_Event_Bg_Update *ev;
403
404    EINA_LIST_FOREACH(e_config->desktop_backgrounds, l, cfbg)
405      {
406         if (!cfbg) continue;
407         if ((cfbg->container == container) && (cfbg->zone == zone) &&
408             (cfbg->desk_x == desk_x) && (cfbg->desk_y == desk_y))
409           {
410              e_config->desktop_backgrounds = eina_list_remove_list(e_config->desktop_backgrounds, l);
411              e_filereg_deregister(cfbg->file);
412              if (cfbg->file) eina_stringshare_del(cfbg->file);
413              free(cfbg);
414              break;
415           }
416      }
417
418    ev = E_NEW(E_Event_Bg_Update, 1);
419    ev->container = container;
420    ev->zone = zone;
421    ev->desk_x = desk_x;
422    ev->desk_y = desk_y;
423    ecore_event_add(E_EVENT_BG_UPDATE, ev, _e_bg_event_bg_update_free, NULL);
424 }
425
426 EAPI void
427 e_bg_update(void)
428 {
429    Eina_List *l, *ll, *lll;
430    E_Manager *man;
431    E_Container *con;
432    E_Zone *zone;
433
434    EINA_LIST_FOREACH(e_manager_list(), l, man)
435      {
436         EINA_LIST_FOREACH(man->containers, ll, con)
437           {
438              EINA_LIST_FOREACH(con->zones, lll, zone)
439                {
440                   e_zone_bg_reconfigure(zone);
441                }
442           }
443      }
444 }
445
446 static inline Eina_Bool
447 _e_bg_file_edje_check(const char *path)
448 {
449    const char *ext;
450    const size_t extlen = sizeof(".edj") - 1;
451    size_t len;
452
453    if (!path) return EINA_FALSE;
454
455    len = strlen(path);
456    if (len <= extlen) return EINA_FALSE;
457    ext = path + len - extlen;
458    return memcmp(ext, ".edj", extlen) == 0;
459 }
460
461 /**
462  * Go through process of importing an image as E background.
463  *
464  * This will go through process of importing an image as E
465  * background. It will ask fill/tile mode of the image, as well as
466  * quality to use.
467  *
468  * User can cancel operation at any time, and in this case callback is
469  * called with @c NULL as second parameter.
470  *
471  * The operation can be canceled by application/module as well using
472  * e_bg_image_import_cancel(). Even in this case the callback is called so user
473  * can free possibly allocated data.
474  *
475  * @param image_file source file to use, must be supported by Evas.
476  * @param cb callback to call when import process is done. The first
477  *        argument is the given data, while the second is the path to
478  *        the imported background file (edje) that can be used with
479  *        e_bg_add() or e_bg_default_set(). Note that if @a edje_file
480  *        is @c NULL, then the import process was canceled!
481  *        This function is @b always called and after it returns
482  *        E_Bg_Image_Import_Handle is deleted.
483  * @param data pointer to data to be given to callback.
484  *
485  * @return handle to the import process. It will die automatically
486  *         when user cancels the process or when code automatically
487  *         calls e_bg_image_import_cancel().  Before dying, callback
488  *         will always be called.
489  */
490 EAPI E_Bg_Image_Import_Handle *
491 e_bg_image_import(const char *image_file, void (*cb)(void *data, const char *edje_file), const void *data)
492 {
493    E_Bg_Image_Import_Handle *handle;
494
495    if (!image_file) return NULL;
496    if (!cb) return NULL;
497
498    handle = E_NEW(E_Bg_Image_Import_Handle, 1);
499    if (!handle) return NULL;
500    handle->cb.func = cb;
501    handle->cb.data = (void *)data;
502
503    handle->dia = e_util_image_import_settings_new
504      (image_file, _e_bg_image_import_dialog_done, handle);
505    if (!handle->dia)
506      {
507         free(handle);
508         return NULL;
509      }
510    e_dialog_show(handle->dia);
511
512    return handle;
513 }
514
515 /**
516  * Cancels previously started import process.
517  *
518  * Note that his handle will be deleted when process import, so don't
519  * call it after your callback is called!
520  */
521 EAPI void
522 e_bg_image_import_cancel(E_Bg_Image_Import_Handle *handle)
523 {
524    if (!handle) return;
525
526    handle->canceled = EINA_TRUE;
527
528    if (handle->cb.func)
529      {
530         handle->cb.func(handle->cb.data, NULL);
531         handle->cb.func = NULL;
532      }
533    if (handle->dia)
534      {
535         e_object_del(E_OBJECT(handle->dia));
536         handle->dia = NULL;
537      }
538    else if (handle->importer)
539      {
540         e_util_image_import_cancel(handle->importer);
541         handle->importer = NULL;
542      }
543    E_FREE(handle);
544 }
545
546 /**
547  * Set background to image, as required in e_fm2_mime_handler_new()
548  */
549 EAPI void
550 e_bg_handler_set(Evas_Object *obj __UNUSED__, const char *path, void *data __UNUSED__)
551 {
552    if (!path) return;
553
554    if (_e_bg_file_edje_check(path))
555      {
556         char buf[PATH_MAX];
557         int copy = 1;
558
559         E_Container *con = e_container_current_get(e_manager_current_get());
560         E_Zone *zone = e_zone_current_get(con);
561         E_Desk *desk = e_desk_current_get(zone);
562
563         /* if not in system dir or user dir, copy to user dir */
564         e_prefix_data_concat_static(buf, "data/backgrounds");
565         if (!strncmp(buf, path, strlen(buf)))
566            copy = 0;
567         if (copy)
568           {
569              e_user_dir_concat_static(buf, "backgrounds");
570              if (!strncmp(buf, path, strlen(buf)))
571                 copy = 0;
572           }
573         if (copy)
574           {
575              const char *file;
576              char *name;
577
578              file = ecore_file_file_get(path);
579              name = ecore_file_strip_ext(file);
580
581              e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s-%f.edj", name, ecore_time_unix_get());
582              free(name);
583
584              if (!ecore_file_exists(buf))
585                {
586                   ecore_file_cp(path, buf);
587                   e_bg_add(con->num, zone->num, desk->x, desk->y, buf);
588                }
589              else
590                 e_bg_add(con->num, zone->num, desk->x, desk->y, path);
591           }
592         else
593            e_bg_add(con->num, zone->num, desk->x, desk->y, path);
594
595         e_bg_update();
596         e_config_save_queue();
597         return;
598      }
599
600    e_bg_image_import(path, _e_bg_handler_image_imported, NULL);
601 }
602
603 /**
604  * Test if possible to set background to file, as required in
605  * e_fm2_mime_handler_new()
606  *
607  * This handler tests for files that would be acceptable for setting
608  * background.
609  *
610  * You should just register it with "*.edj" (glob matching extension)
611  * or "image/" (mimetypes)that are acceptable with Evas loaders.
612  *
613  * Just edje files with "e/desktop/background" group are used.
614  */
615 EAPI int
616 e_bg_handler_test(Evas_Object *obj __UNUSED__, const char *path, void *data __UNUSED__)
617 {
618
619    if (!path) return 0;
620
621    if (_e_bg_file_edje_check(path))
622      {
623         if (edje_file_group_exists(path, "e/desktop/background")) return 1;
624         return 0;
625      }
626
627    /* it's image/png or image/jpeg, we'll import it. */
628    return 1;
629 }
630
631 /* local subsystem functions */
632 static void
633 _e_bg_signal(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
634 {
635    E_Zone *zone = data;
636
637    if (zone->prev_bg_object)
638      {
639         evas_object_del(zone->prev_bg_object);
640         zone->prev_bg_object = NULL;
641      }
642    if (zone->transition_object)
643      {
644         evas_object_del(zone->transition_object);
645         zone->transition_object = NULL;
646      }
647    evas_object_move(zone->bg_object, zone->x, zone->y);
648    evas_object_resize(zone->bg_object, zone->w, zone->h);
649    evas_object_layer_set(zone->bg_object, -1);
650    evas_object_clip_set(zone->bg_object, zone->bg_clip_object);
651    evas_object_show(zone->bg_object);
652 }
653
654 static void
655 _e_bg_event_bg_update_free(void *data __UNUSED__, void *event)
656 {
657    free(event);
658 }
659
660 static void
661 _e_bg_image_import_dialog_done(void *data, const char *path, Eina_Bool ok, Eina_Bool external, int quality, E_Image_Import_Mode mode)
662 {
663    E_Bg_Image_Import_Handle *handle = data;
664    const char *file;
665    char *name;
666    char buf[PATH_MAX];
667    size_t used, off;
668    unsigned num;
669
670    if (!ok) goto aborted;
671
672    file = ecore_file_file_get(path);
673    if (!file) goto aborted;
674    name = ecore_file_strip_ext(file);
675    if (!name) goto aborted;
676
677    used = e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s.edj", name);
678    free(name);
679    if (used >= sizeof(buf)) goto aborted;
680
681    off = used - (sizeof(".edj") - 1);
682
683    for (num = 0; ecore_file_exists(buf); num++)
684      snprintf(buf + off, sizeof(buf) - off, "-%u.edj", num);
685
686    handle->importer = e_util_image_import
687      (path, buf, "e/desktop/background", external, quality, mode,
688       _e_bg_image_import_done, handle);
689    if (!handle->importer) goto aborted;
690
691    return;
692
693  aborted:
694    if (handle->cb.func)
695      {
696         handle->cb.func(handle->cb.data, NULL);
697         handle->cb.func = NULL;
698      }
699    if (!handle->canceled) E_FREE(handle);
700 }
701
702 static void
703 _e_bg_image_import_done(void *data, Eina_Bool ok, const char *image_path __UNUSED__, const char *edje_path)
704 {
705    E_Bg_Image_Import_Handle *handle = data;
706
707    if (!ok) edje_path = NULL;
708
709    if (handle->cb.func)
710      {
711         handle->cb.func(handle->cb.data, edje_path);
712         handle->cb.func = NULL;
713      }
714
715    if (!handle->canceled) E_FREE(handle);
716 }
717
718 static void
719 _e_bg_handler_image_imported(void *data __UNUSED__, const char *image_path)
720 {
721    E_Container *con = e_container_current_get(e_manager_current_get());
722    E_Zone *zone = e_zone_current_get(con);
723    E_Desk *desk = e_desk_current_get(zone);
724
725    if (!image_path) return;
726
727    e_bg_add(con->num, zone->num, desk->x, desk->y, image_path);
728    e_bg_update();
729    e_config_save_queue();
730 }