Tizen 2.1 release
[platform/core/uifw/e17.git] / src / bin / e_import_config_dialog.c
1 #include "e.h"
2
3 #define IMPORT_STRETCH          0
4 #define IMPORT_TILE             1
5 #define IMPORT_CENTER           2
6 #define IMPORT_SCALE_ASPECT_IN  3
7 #define IMPORT_SCALE_ASPECT_OUT 4
8 #define IMPORT_PAN              5
9
10 static void      _import_edj_gen(E_Import_Config_Dialog *import);
11 static Eina_Bool _import_cb_edje_cc_exit(void *data, int type, void *event);
12
13
14 static void
15 _import_edj_gen(E_Import_Config_Dialog *import)
16 {
17    Evas *evas;
18    Evas_Object *img;
19    Eina_Bool anim = EINA_FALSE;
20    int fd, num = 1;
21    int w = 0, h = 0;
22    const char *file, *locale;
23    char buf[PATH_MAX], cmd[PATH_MAX], tmpn[PATH_MAX], ipart[PATH_MAX], enc[128];
24    char *imgdir = NULL, *fstrip;
25    int cr, cg, cb, ca;
26    FILE *f;
27    size_t len, off;
28
29    evas = e_win_evas_get(import->dia->win);
30    file = ecore_file_file_get(import->file);
31    fstrip = ecore_file_strip_ext(file);
32    if (!fstrip) return;
33    len = e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s.edj", fstrip);
34    if (len >= sizeof(buf))
35      {
36         free(fstrip);
37         return;
38      }
39    off = len - (sizeof(".edj") - 1);
40    if ((int)off < 0)
41      {
42         free(fstrip);
43         return;
44      }
45    for (num = 1; ecore_file_exists(buf) && num < 100; num++)
46      snprintf(buf + off, sizeof(buf) - off, "-%d.edj", num);
47    free(fstrip);
48    cr = import->color.r;
49    cg = import->color.g;
50    cb = import->color.b;
51    ca = import->color.a;
52
53    if (num == 100)
54      {
55         printf("Couldn't come up with another filename for %s\n", buf);
56         return;
57      }
58
59    strcpy(tmpn, "/tmp/e_bgdlg_new.edc-tmp-XXXXXX");
60    fd = mkstemp(tmpn);
61    if (fd < 0)
62      {
63         printf("Error Creating tmp file: %s\n", strerror(errno));
64         return;
65      }
66
67    f = fdopen(fd, "w");
68    if (!f)
69      {
70         printf("Cannot open %s for writing\n", tmpn);
71         return;
72      }
73
74    anim = eina_str_has_extension(import->file, "gif");
75    imgdir = ecore_file_dir_get(import->file);
76    if (!imgdir) ipart[0] = '\0';
77    else
78      {
79         snprintf(ipart, sizeof(ipart), "-id %s", e_util_filename_escape(imgdir));
80         free(imgdir);
81      }
82
83    img = evas_object_image_add(evas);
84    evas_object_image_file_set(img, import->file, NULL);
85    evas_object_image_size_get(img, &w, &h);
86    evas_object_del(img);
87
88    if (import->external)
89      {
90         fstrip = strdupa(e_util_filename_escape(import->file));
91         snprintf(enc, sizeof(enc), "USER");
92      }
93    else
94      {
95         fstrip = strdupa(e_util_filename_escape(file));
96         if (import->quality == 100)
97           snprintf(enc, sizeof(enc), "COMP");
98         else
99           snprintf(enc, sizeof(enc), "LOSSY %i", import->quality);
100      }
101    switch (import->method)
102      {
103       case IMPORT_STRETCH:
104         fprintf(f,
105                 "images { image: \"%s\" %s; }\n"
106                 "collections {\n"
107                 "group { name: \"e/desktop/background\";\n"
108                 "%s"
109                 "data { item: \"style\" \"0\"; }\n"
110                 "max: %i %i;\n"
111                 "parts {\n"
112                 "part { name: \"bg\"; mouse_events: 0;\n"
113                 "description { state: \"default\" 0.0;\n"
114                 "image { normal: \"%s\"; scale_hint: STATIC; }\n"
115                 "} } } } }\n"
116                 , fstrip, enc, anim ? "" : "data.item: \"noanimation\" \"1\";\n", w, h, fstrip);
117         break;
118
119       case IMPORT_TILE:
120         fprintf(f,
121                 "images { image: \"%s\" %s; }\n"
122                 "collections {\n"
123                 "group { name: \"e/desktop/background\";\n"
124                 "data { item: \"style\" \"1\"; }\n"
125                 "%s"
126                 "max: %i %i;\n"
127                 "parts {\n"
128                 "part { name: \"bg\"; mouse_events: 0;\n"
129                 "description { state: \"default\" 0.0;\n"
130                 "image { normal: \"%s\"; }\n"
131                 "fill { size {\n"
132                 "relative: 0.0 0.0;\n"
133                 "offset: %i %i;\n"
134                 "} } } } } } }\n"
135                 , fstrip, enc, anim ? "" : "data.item: \"noanimation\" \"1\";\n", w, h, fstrip, w, h);
136         break;
137
138       case IMPORT_CENTER:
139         fprintf(f,
140                 "images { image: \"%s\" %s; }\n"
141                 "collections {\n"
142                 "group { name: \"e/desktop/background\";\n"
143                 "data { item: \"style\" \"2\"; }\n"
144                 "%s"
145                 "max: %i %i;\n"
146                 "parts {\n"
147                 "part { name: \"col\"; type: RECT; mouse_events: 0;\n"
148                 "description { state: \"default\" 0.0;\n"
149                 "color: %i %i %i %i;\n"
150                 "} }\n"
151                 "part { name: \"bg\"; mouse_events: 0;\n"
152                 "description { state: \"default\" 0.0;\n"
153                 "min: %i %i; max: %i %i;\n"
154                 "image { normal: \"%s\"; }\n"
155                 "} } } } }\n"
156                 , fstrip, enc, anim ? "" : "data.item: \"noanimation\" \"1\";\n", w, h, cr, cg, cb, ca, w, h, w, h, fstrip);
157         break;
158
159       case IMPORT_SCALE_ASPECT_IN:
160         locale = e_intl_language_get();
161         setlocale(LC_NUMERIC, "C");
162         fprintf(f,
163                 "images { image: \"%s\" %s; }\n"
164                 "collections {\n"
165                 "group { name: \"e/desktop/background\";\n"
166                 "data { item: \"style\" \"3\"; }\n"
167                 "%s"
168                 "max: %i %i;\n"
169                 "parts {\n"
170                 "part { name: \"col\"; type: RECT; mouse_events: 0;\n"
171                 "description { state: \"default\" 0.0;\n"
172                 "color: %i %i %i %i;\n"
173                 "} }\n"
174                 "part { name: \"bg\"; mouse_events: 0;\n"
175                 "description { state: \"default\" 0.0;\n"
176                 "aspect: %1.9f %1.9f; aspect_preference: BOTH;\n"
177                 "image { normal: \"%s\";  scale_hint: STATIC; }\n"
178                 "} } } } }\n"
179                 , fstrip, enc, anim ? "" : "data.item: \"noanimation\" \"1\";\n",
180                 w, h, cr, cg, cb, ca, (double)w / (double)h, (double)w / (double)h, fstrip);
181         setlocale(LC_NUMERIC, locale);
182         break;
183
184       case IMPORT_SCALE_ASPECT_OUT:
185         locale = e_intl_language_get();
186         setlocale(LC_NUMERIC, "C");
187         fprintf(f,
188                 "images { image: \"%s\" %s; }\n"
189                 "collections {\n"
190                 "group { name: \"e/desktop/background\";\n"
191                 "data { item: \"style\" \"4\"; }\n"
192                 "%s"
193                 "max: %i %i;\n"
194                 "parts {\n"
195                 "part { name: \"bg\"; mouse_events: 0;\n"
196                 "description { state: \"default\" 0.0;\n"
197                 "aspect: %1.9f %1.9f; aspect_preference: NONE;\n"
198                 "image { normal: \"%s\";  scale_hint: STATIC; }\n"
199                 "} } } } }\n"
200                 , fstrip, enc, anim ? "" : "data.item: \"noanimation\" \"1\";\n",
201                 w, h, (double)w / (double)h, (double)w / (double)h, fstrip);
202         setlocale(LC_NUMERIC, locale);
203         break;
204
205       case IMPORT_PAN:
206         locale = e_intl_language_get();
207         setlocale(LC_NUMERIC, "C");
208         fprintf(f,
209                 "images { image: \"%s\" %s; }\n"
210                 "collections {\n"
211                 "group { name: \"e/desktop/background\";\n"
212                 "data { item: \"style\" \"4\"; }\n"
213                 "%s"
214                 "max: %i %i;\n"
215                 "script {\n"
216                 "public cur_anim; public cur_x; public cur_y; public prev_x;\n"
217                 "public prev_y; public total_x; public total_y; \n"
218                 "public pan_bg(val, Float:v) {\n"
219                 "new Float:x, Float:y, Float:px, Float: py;\n"
220
221                 "px = get_float(prev_x); py = get_float(prev_y);\n"
222                 "if (get_int(total_x) > 1) {\n"
223                 "x = float(get_int(cur_x)) / (get_int(total_x) - 1);\n"
224                 "x = px - (px - x) * v;\n"
225                 "} else { x = 0.0; v = 1.0; }\n"
226                 "if (get_int(total_y) > 1) {\n"
227                 "y = float(get_int(cur_y)) / (get_int(total_y) - 1);\n"
228                 "y = py - (py - y) * v;\n"
229                 "} else { y = 0.0; v = 1.0; }\n"
230
231                 "set_state_val(PART:\"bg\", STATE_ALIGNMENT, x, y);\n"
232
233                 "if (v >= 1.0) {\n"
234                 "set_int(cur_anim, 0); set_float(prev_x, x);\n"
235                 "set_float(prev_y, y); return 0;\n"
236                 "}\n"
237                 "return 1;\n"
238                 "}\n"
239                 "public message(Msg_Type:type, id, ...) {\n"
240                 "if ((type == MSG_FLOAT_SET) && (id == 0)) {\n"
241                 "new ani;\n"
242
243                 "get_state_val(PART:\"bg\", STATE_ALIGNMENT, prev_x, prev_y);\n"
244                 "set_int(cur_x, round(getfarg(3))); set_int(total_x, round(getfarg(4)));\n"
245                 "set_int(cur_y, round(getfarg(5))); set_int(total_y, round(getfarg(6)));\n"
246
247                 "ani = get_int(cur_anim); if (ani > 0) cancel_anim(ani);\n"
248                 "ani = anim(getfarg(2), \"pan_bg\", 0); set_int(cur_anim, ani);\n"
249                 "} } }\n"
250                 "parts {\n"
251                 "part { name: \"bg\"; mouse_events: 0;\n"
252                 "description { state: \"default\" 0.0;\n"
253                 "aspect: %1.9f %1.9f; aspect_preference: NONE;\n"
254                 "image { normal: \"%s\";  scale_hint: STATIC; }\n"
255                 "} } }\n"
256                 "programs { program {\n"
257                 " name: \"init\";\n"
258                 " signal: \"load\";\n"
259                 " source: \"\";\n"
260                 " script { custom_state(PART:\"bg\", \"default\", 0.0);\n"
261                 " set_state(PART:\"bg\", \"custom\", 0.0);\n"
262                 " set_float(prev_x, 0.0); set_float(prev_y, 0.0);\n"
263                 "} } } } }\n"
264                 , fstrip, enc, anim ? "" : "data.item: \"noanimation\" \"1\";\n",
265                 w, h, (double)w / (double)h, (double)w / (double)h, fstrip);
266         setlocale(LC_NUMERIC, locale);
267         break;
268
269       default:
270         /* won't happen */
271         break;
272      }
273
274    fclose(f);
275
276    snprintf(cmd, sizeof(cmd), "edje_cc -v %s %s %s",
277             ipart, tmpn, e_util_filename_escape(buf));
278
279    import->tmpf = strdup(tmpn);
280    import->fdest = eina_stringshare_add(buf);
281    import->exe_handler =
282      ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
283                              _import_cb_edje_cc_exit, import);
284    import->exe = ecore_exe_run(cmd, import);
285 }
286
287 static Eina_Bool
288 _import_cb_edje_cc_exit(void *data, __UNUSED__ int type, void *event)
289 {
290    E_Import_Config_Dialog *import;
291    Ecore_Exe_Event_Del *ev;
292    int r = 1;
293
294    ev = event;
295    import = data;
296    if (ecore_exe_data_get(ev->exe) != import) return ECORE_CALLBACK_PASS_ON;
297
298    if (ev->exit_code != 0)
299      {
300         e_util_dialog_show(_("Picture E_Import_Config_Dialog Error"),
301                            _("Enlightenment was unable to import the picture<br>"
302                              "due to conversion errors."));
303         r = 0;
304      }
305
306    if (r && import->ok)
307      {
308         e_object_ref(E_OBJECT(import));
309         import->ok((void*)import->fdest, import);
310         e_object_del(E_OBJECT(import));
311         e_object_unref(E_OBJECT(import));
312      }
313    else
314      e_object_del(E_OBJECT(import));
315
316    return ECORE_CALLBACK_DONE;
317 }
318
319 static void
320 _import_cb_close(void *data, E_Dialog *dia __UNUSED__)
321 {
322    E_Import_Config_Dialog *import = data;
323
324    e_object_ref(data);
325    if (import->cancel) import->cancel(import);
326    e_object_del(data);
327    e_object_unref(data);
328 }
329
330 static void
331 _import_cb_ok(void *data, E_Dialog *dia __UNUSED__)
332 {
333    E_Import_Config_Dialog *import = data;
334    const char *file;
335    char buf[PATH_MAX];
336    int is_bg, is_theme, r;
337
338    r = 0;
339    if (!import->file) return;
340    file = ecore_file_file_get(import->file);
341    if (!eina_str_has_extension(file, "edj"))
342      {
343         _import_edj_gen(import);
344         e_win_hide(import->dia->win);
345         return;
346      }
347    e_user_dir_snprintf(buf, sizeof(buf), "backgrounds/%s", file);
348
349    is_bg = edje_file_group_exists(import->file, "e/desktop/background");
350    is_theme = edje_file_group_exists(import->file,
351                                      "e/widgets/border/default/border");
352
353    if ((is_bg) && (!is_theme))
354      {
355         if (!ecore_file_cp(import->file, buf))
356           {
357              e_util_dialog_show(_("Import Error"),
358                                 _("Enlightenment was unable to "
359                                   "import the image<br>due to a "
360                                   "copy error."));
361           }
362         else
363           r = 1;
364      }
365    else
366      {
367         e_util_dialog_show(_("Import Error"),
368                            _("Enlightenment was unable to "
369                              "import the image.<br><br>"
370                              "Are you sure this is a valid "
371                              "image?"));
372      }
373
374    if (r)
375      {
376         e_object_ref(E_OBJECT(import));
377         if (import->ok) import->ok((void*)buf, import);
378         e_object_del(E_OBJECT(import));
379         e_object_unref(E_OBJECT(import));
380      }
381    else
382      _import_cb_close(import, NULL);
383 }
384
385 static void
386 _e_import_config_preview_size_get(int size, int w, int h,int *tw, int *th)
387 {
388    if (size <= 0) return;
389    double aspect;
390    aspect = (double)w/h;
391    
392    if(w > size)
393      {
394         w = size;
395         h = (w/aspect);
396      }
397    *tw = w;
398    *th = h;
399 }
400
401 static void
402 _e_import_config_dia_del(void *data)
403 {
404    E_Dialog *dia = data;
405
406    e_object_del(dia->data);
407 }
408
409 static void
410 _e_import_config_dialog_del(void *data)
411 {
412    E_Import_Config_Dialog *import = data;
413
414    if (import->exe_handler) ecore_event_handler_del(import->exe_handler);
415    import->exe_handler = NULL;
416    if (import->tmpf) unlink(import->tmpf);
417    free(import->tmpf);
418    eina_stringshare_del(import->fdest);
419    import->exe = NULL;
420    eina_stringshare_del(import->file);
421    e_object_del(E_OBJECT(import->dia));
422    free(import);
423 }
424
425 static void
426 _e_import_config_dialog_win_del(E_Win *win)
427 {
428    E_Dialog *dia;
429    E_Import_Config_Dialog *import;
430
431    dia = win->data;
432    import = dia->data;
433    e_object_ref(E_OBJECT(import));
434    if (import->cancel) import->cancel(import);
435    e_object_del(E_OBJECT(import));
436    e_object_unref(E_OBJECT(import));
437 }
438 ///////////////////////////////////////////////////////////////////////////////////
439
440
441 EAPI E_Import_Config_Dialog *
442 e_import_config_dialog_show(E_Container *con, const char *path, Ecore_End_Cb ok, Ecore_Cb cancel)
443 {
444    Evas *evas;
445    E_Dialog *dia;
446    E_Import_Config_Dialog *import;
447    Evas_Object *o, *of, *ord, *ot, *ol, *preview, *frame;
448    E_Radio_Group *rg;
449    int w, h, tw, th;
450
451    if (!path) return NULL;
452
453    import = E_OBJECT_ALLOC(E_Import_Config_Dialog, E_IMPORT_CONFIG_DIALOG_TYPE, _e_import_config_dialog_del);
454    if (!import) return NULL;
455
456    dia = e_dialog_new(con, "E", "_import_config_dialog");
457    e_dialog_title_set(dia, _("Import Settings..."));
458    dia->data = import;
459    import->dia = dia;
460    import->ok = ok, import->cancel = cancel;
461    import->path = eina_stringshare_add(path);
462    e_object_del_attach_func_set(E_OBJECT(dia), _e_import_config_dia_del);
463    e_win_delete_callback_set(dia->win, _e_import_config_dialog_win_del);
464
465    import->method = IMPORT_SCALE_ASPECT_OUT;
466    import->external = 0;
467    import->quality = 90;
468    import->file = eina_stringshare_add(path);
469
470    evas = e_win_evas_get(dia->win);
471
472    o = e_widget_list_add(evas, 0, 0);
473
474    ot = e_widget_list_add(evas, 0, 0);
475    frame = e_widget_frametable_add(evas, _("Preview"), 1);
476    
477    preview = evas_object_image_add(evas);
478    evas_object_image_file_set(preview, path, NULL);
479    evas_object_image_size_get(preview,&w, &h);
480    evas_object_del(preview);
481
482    _e_import_config_preview_size_get(320, w, h, &tw, &th);
483    
484    preview = e_widget_preview_add(evas, tw, th);
485    e_widget_preview_thumb_set(preview, path, NULL, tw, th);
486
487    e_widget_frametable_object_append(frame, preview, 0, 0, 1, 1, 1, 1, 1, 0);
488    e_widget_list_object_append(ot, frame, 1, 1, 0);
489    of = e_widget_frametable_add(evas, _("Fill and Stretch Options"), 1);
490    rg = e_widget_radio_group_new(&import->method);
491    ord = e_widget_radio_icon_add(evas, _("Stretch"),
492                                  "enlightenment/wallpaper_stretch",
493                                  24, 24, IMPORT_STRETCH, rg);
494    e_widget_frametable_object_append(of, ord, 0, 0, 1, 1, 1, 0, 1, 0);
495    ord = e_widget_radio_icon_add(evas, _("Center"),
496                                  "enlightenment/wallpaper_center",
497                                  24, 24, IMPORT_CENTER, rg);
498    e_widget_frametable_object_append(of, ord, 1, 0, 1, 1, 1, 0, 1, 0);
499    ord = e_widget_radio_icon_add(evas, _("Tile"),
500                                  "enlightenment/wallpaper_tile",
501                                  24, 24, IMPORT_TILE, rg);
502    e_widget_frametable_object_append(of, ord, 2, 0, 1, 1, 1, 0, 1, 0);
503    ord = e_widget_radio_icon_add(evas, _("Within"),
504                                  "enlightenment/wallpaper_scale_aspect_in",
505                                  24, 24, IMPORT_SCALE_ASPECT_IN, rg);
506    e_widget_frametable_object_append(of, ord, 3, 0, 1, 1, 1, 0, 1, 0);
507    ord = e_widget_radio_icon_add(evas, _("Fill"),
508                                  "enlightenment/wallpaper_scale_aspect_out",
509                                  24, 24, IMPORT_SCALE_ASPECT_OUT, rg);
510    e_widget_frametable_object_append(of, ord, 4, 0, 1, 1, 1, 0, 1, 0);
511    ord = e_widget_radio_icon_add(evas, _("Pan"),
512                                  "enlightenment/wallpaper_pan",
513                                  24, 24, IMPORT_PAN, rg);
514    e_widget_frametable_object_append(of, ord, 5, 0, 1, 1, 1, 0, 1, 0);
515    e_widget_list_object_append(ot, of, 1, 1, 0);
516
517    ol = e_widget_list_add(evas, 0, 1);
518
519    of = e_widget_frametable_add(evas, _("File Quality"), 0);
520    ord = e_widget_check_add(evas, _("Use original file"), &(import->external));
521    e_widget_frametable_object_append(of, ord, 0, 0, 1, 1, 1, 0, 1, 0);
522    ord = e_widget_slider_add(evas, 1, 0, _("%3.0f%%"), 0.0, 100.0, 1.0, 0,
523                              NULL, &(import->quality), 150);
524    e_widget_frametable_object_append(of, ord, 0, 1, 1, 1, 1, 0, 1, 0);
525    e_widget_list_object_append(ol, of, 1, 1, 0);
526
527    of = e_widget_framelist_add(evas, _("Fill Color"), 0);
528    ord = e_widget_color_well_add(evas, &import->color, 1);
529    e_widget_framelist_object_append(of, ord);
530    e_widget_list_object_append(ol, of, 1, 0, 1);
531    e_widget_list_object_append(ot, ol, 1, 1, 0);
532
533    e_widget_list_object_append(o, ot, 0, 0, 0.5);
534
535    e_widget_size_min_get(o, &w, &h);
536    e_dialog_content_set(dia, o, w, h);
537    e_dialog_button_add(dia, _("OK"), NULL, _import_cb_ok, import);
538    e_dialog_button_add(dia, _("Cancel"), NULL, _import_cb_close, import);
539    e_win_centered_set(dia->win, 1);
540    e_dialog_border_icon_set(dia, "folder-image");
541    e_dialog_button_focus_num(dia, 0);
542    e_dialog_show(dia);
543
544    return import;
545 }