summaryrefslogtreecommitdiff
path: root/src/lib/gui_parser.c
blob: 689061e512e54c583a878068e2b67c52b2264ed8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include "database.h"
#include "gui_widget.h"
#include "eina_json.h"
#include "egui_log.h"
#include "Ecore_File.h"

/*
 * Get file as one string.
 * */
char*
gui_parser_file_string_get(const char *filename)
{
   char *file_data = NULL;
   int file_size;
   FILE *fp = fopen(filename, "r");
   if (!fp)
     {
        printf("Can not open file: \"%s\".\n", filename);
        return NULL;
     }

   fseek(fp, 0, SEEK_END);
   file_size = ftell(fp);
   if (file_size == -1)
     {
        fclose(fp);
        printf("Can not ftell file: \"%s\".\n", filename);
        return NULL;
     }
   rewind(fp);
   file_data = (char *) calloc(1, file_size + 1);
   if (!file_data)
     {
        fclose(fp);
        ERR("Calloc failed");
        return NULL;
     }
   int res = fread(file_data, file_size, 1, fp);
   fclose(fp);
   if (!res)
     {
        free(file_data);
        file_data = NULL;
        ERR("fread failed");
     }
   return file_data;
}

/*
 * Find first JSON comment in buffer
 *
 * */
int
gui_parser_string_first_match_get(const char *in_str, int *start_off, int *end_off)
{
   regex_t regex;
   regmatch_t offsets[1];
   int err;

   if (!in_str)
     {
        ERR("Input string is NULL\n");
        return 1;
     }

#define START_TOKEN "@egui"
#define END_TOKEN "@!egui"

   /* FIXME: search until close tag or end of comment*/
   err = regcomp(&regex, START_TOKEN"[^@]*"END_TOKEN, REG_EXTENDED);
   if (err)
     {
        ERR("Couldn't compile regex: err = %d", err);
        return err;
     }

   err = regexec(&regex, in_str, 1, offsets, 0);
   if (!err)
     {
        if (start_off) *start_off = offsets[0].rm_so + strlen(START_TOKEN);
        if (end_off) *end_off = offsets[0].rm_eo - strlen(END_TOKEN);
     }
   else
     {
        if (err == REG_ESPACE)
          ERR("\"regexec returned: REG_ESPACE\"");
        if (start_off) *start_off = 0;
        if (end_off) *end_off = 0;
     }
   regfree(&regex);
   return err;
#undef START_TOKEN
#undef END_TOKEN
}

/*
 * - remove all '*' from json string
 * - drop spaces until '{'
 *   */
void
_strip(char *p)
{
   char *p_orig = p;
   Eina_Bool can_change = EINA_TRUE;

   while (*p != '\0')
     {
        if ((*p != '*') && (*p != ' '))
          can_change = EINA_FALSE;
        if (*p == '\n')
          can_change = EINA_TRUE;

        if (*p == '*')
          {
             if (can_change)
               *p = ' ';
          }
        p++;
     }

   p = p_orig;
   while (*p != '{')
     {
        p++;
     }

   memmove(p_orig, p, strlen(p) + 1);
}

/*
 * Find all JSON comments in buffer
 *
 * */
Eina_List*
gui_parser_string_all_match_get(const char *in_str)
{
   int so, eo; /*start offset / end offset*/
   const char *cur = in_str;
   char *data;
   Eina_List *lst = NULL;

   while (!gui_parser_string_first_match_get(cur, &so, &eo))
     {
        data = strndup(cur + so, eo - so);
        _strip(data);
        lst = eina_list_append(lst, data);
        cur = cur + so;
     }

   return lst;
}

static void
_json_arr_to_prop_values(const Gui_Context *gui_ctx, Eina_Json_Value *jv, Gui_Widget_Property *prop)
{
   Eina_Json_Type type;
   Eina_Json_Value *v;
   Eina_Iterator *it = NULL;

   if (eina_json_type_get(jv) != EINA_JSON_TYPE_ARRAY)
     ERR("Json Value is not array");

   it = eina_json_array_iterator_new(jv);

   int i = 0;
   EINA_ITERATOR_FOREACH(it, v)
     {
        Gui_Value *g_val = prop_value_nth_get(prop, i);
        Gui_Type g_type;

        type = eina_json_type_get(v);
        if (!g_val)
          {
             ERR("Cannot create gui_value. Terminating...");
             exit(1);
          }
        g_type = gui_value_type_get(g_val);
        switch (type)
          {
           case EINA_JSON_TYPE_NUMBER:
                {
                   double d = eina_json_number_get(v);
                   if (g_type == GUI_TYPE_SINT)
                     {
                        gui_value_int_set(g_val, (int) d);
                     }
                   else if (g_type == GUI_TYPE_DOUBLE)
                     {
                        gui_value_double_set(g_val, d);
                     }
                   break;
                }
           case EINA_JSON_TYPE_STRING:
                {
                   const char *str = eina_json_string_get(v);
                   if (g_type == GUI_TYPE_STRING)
                     {
                        gui_value_string_set(g_val, strdup(str));
                     }
                   else if (g_type == GUI_TYPE_ENUM)
                     {
                        gui_value_enum_set(g_val, strdup(str));
                     }
                   else if ((g_type == GUI_TYPE_IMAGE) ||
                            (g_type == GUI_TYPE_CB) ||
                            (g_type == GUI_TYPE_OBJECT))
                     {
                        Dep_Id name_id = gui_context_dep_data_get_id_by_name(gui_ctx, str);
                        gui_value_name_id_set(g_val, g_type, name_id);
                     }
                   break;
                }
           case EINA_JSON_TYPE_BOOLEAN:
                {
                   Eina_Bool b = eina_json_boolean_get(v);
                   if (g_type == GUI_TYPE_BOOL)
                     {
                        gui_value_bool_set(g_val, b);
                     }
                   break;
                }
           case EINA_JSON_TYPE_NULL:
                {
                   gui_value_int_set(g_val, 0);
                   gui_value_type_set(g_val, g_type);
                   break;
                }
           default:
                {
                   ERR("default case");
                }
          }
        i++;
     }
   eina_iterator_free(it);
}

static void
_read_items_section(const Gui_Context *gui_ctx, Gui_Widget *wdg, Gui_Widget_Item *parent_wit, Eina_Json_Value *json)
{
   Eina_Iterator *it = eina_json_object_iterator_new(json);
   Eina_Json_Value *j;
   const char *class_name = wdg_class_name_get(wdg);

   const Op_Desc *op_desc = db_container_desc_op_desc_get(db_container_desc_get(class_name), CONTAINER_PACK);
   if (!op_desc)
     {
        ERR("Cannot find op description: CONTAINER_PACK for class: \"%s\"\n", class_name);
     }
   EINA_ITERATOR_FOREACH(it, j)
     {
        Eina_Json_Value *js_it, *tmp;
        Gui_Widget_Item_Type ntype = ITEM_INVALID;
        Gui_Widget_Property *prop = prop_create_for_op(op_desc);

        const char *it_name = eina_json_pair_name_get(j);
        js_it = eina_json_pair_value_get(j);

        tmp = EINA_JSON_OBJECT_VALUE_GET(js_it, "type");
        if (!strcmp(eina_json_string_get(tmp), ITEM_REGULAR_STR))
          {
             ntype = ITEM_REGULAR;
          }
        else
          {
             ntype = ITEM_SEPARATOR;
          }
        /* Create new Widget Item*/
        Gui_Widget_Item *wit = wdg_item_new(wdg_context_get(wdg), ntype, it_name);

        Gui_Value *val_parent_it = NULL, *val_label = NULL, *val_icon = NULL, *val_cb = NULL;
        Gui_Type val_icon_type;

        if (!strcmp(class_name, DB_DEF_MENU_CLASS))
          {
             val_parent_it = prop_value_nth_get(prop, 0);
             val_icon = prop_value_nth_get(prop, 1);
             val_icon_type = GUI_TYPE_STRING;
             val_label = prop_value_nth_get(prop, 2);
             val_cb = prop_value_nth_get(prop, 3);
          }
        else if (!strcmp(class_name, DB_DEF_DISKSELECTOR_CLASS) ||
                 !strcmp(class_name, DB_DEF_CTXPOPUP_CLASS))
          {
             val_icon = prop_value_nth_get(prop, 1);
             val_icon_type = GUI_TYPE_OBJECT;
             val_label = prop_value_nth_get(prop, 0);
             val_cb = prop_value_nth_get(prop, 2);
          }
        /* Put 0nt parameter - parent item
         * this parent exists for Menu widget. */
        if (val_parent_it)
          gui_value_pointer_set(val_parent_it, GUI_TYPE_OBJECT_ITEM, parent_wit);

        /* Read "icon" field from JSON. */
        tmp = EINA_JSON_OBJECT_VALUE_GET(js_it, "icon");
        if (tmp)
          {
             if (val_icon_type == GUI_TYPE_STRING)
               {
                  if (eina_json_type_get(tmp) == EINA_JSON_TYPE_STRING)
                    {
                       gui_value_string_set(val_icon, strdup(eina_json_string_get(tmp)));
                    }
                  else
                    {
                       gui_value_string_set(val_icon, NULL);
                    }
               }
             else if (val_icon_type == GUI_TYPE_OBJECT)
               {
                  if (eina_json_type_get(tmp) == EINA_JSON_TYPE_STRING)
                    {
                       const char *str = eina_json_string_get(tmp);
                       gui_value_name_id_set(val_icon, GUI_TYPE_OBJECT, gui_context_dep_data_get_id_by_name(gui_ctx, str));
                    }
                  else
                    {
                       gui_value_name_id_set(val_icon, GUI_TYPE_OBJECT, 0);
                    }
               }
          }

        /* Read "label" field from JSON. */
        tmp = EINA_JSON_OBJECT_VALUE_GET(js_it, "label");
        if (tmp)
          {
             gui_value_string_set(val_label, strdup(eina_json_string_get(tmp)));
          }

        /* Read "callback" field from JSON. */
        tmp = EINA_JSON_OBJECT_VALUE_GET(js_it, "callback");
        if (tmp)
          {
             Dep_Id name_id = 0;
             if (eina_json_type_get(tmp) == EINA_JSON_TYPE_STRING)
               {
                  const char *cb_name = eina_json_string_get(tmp);
                  name_id = gui_context_dep_data_get_id_by_name(gui_ctx, cb_name);
               }
             gui_value_name_id_set(val_cb, GUI_TYPE_CB, name_id);
          }

        /* Read "public" field from JSON. */
        tmp = EINA_JSON_OBJECT_VALUE_GET(js_it, "public");
        if (tmp)
          {
             wdg_item_public_set(wit, eina_json_boolean_get(tmp));
          }

        wdg_item_prop_set(wit, prop);
        wdg_item_append(wdg, parent_wit, wit);

        /* If current item has subitems, read "Items" section,
         * add add subitems to item. */
        tmp = EINA_JSON_OBJECT_VALUE_GET(js_it, "Items");
        if (tmp)
          {
             _read_items_section(gui_ctx, wdg, wit, tmp);
          }

     }
   eina_iterator_free(it);
}

static Eina_Bool
_widget_parse(Gui_Context *gui_ctx, Dep_Id dep_id, Eina_Json_Value *widget_js)
{
   /* Creation of widget */
   const char *class_name = NULL, *parent_name = NULL;
   Gui_Widget *wdg = NULL;

   /* Get "class" from json */
   Eina_Json_Value *j = EINA_JSON_OBJECT_VALUE_GET(widget_js, "Desc", "class");
   Eina_Json_Value *tmp = NULL;
   if (!j) goto bad_end;
   class_name = eina_json_string_get(j);

   const char *widget_name = gui_context_dep_data_get(gui_ctx, dep_id);

   wdg = wdg_new(gui_ctx, class_name, dep_id);
   if (!wdg)
     {
        ERR("Cannot create widget. Terminating...");
        return EINA_FALSE;
     }

   /* Get "parent" from json */
   j = EINA_JSON_OBJECT_VALUE_GET(widget_js, "Desc", "parent");
   /* if json value in "parent" key is not string, parent name will be set to NULL. */
   if ((j) && (eina_json_type_get(j) == EINA_JSON_TYPE_STRING))
      parent_name = eina_json_string_get(j);

   if (!wdg_parent_set(wdg, parent_name))
     {
        goto bad_end;
     }

   /* Get "public" from json */
   j = EINA_JSON_OBJECT_VALUE_GET(widget_js, "Desc", "public");
   if (j) wdg_public_set(wdg, eina_json_boolean_get(j));

   /* Find "Properties" section */
   tmp = EINA_JSON_OBJECT_VALUE_GET(widget_js, "Properties");
   if (!tmp)
     {
        ERR("section \"Properties\" not found for widget: %s", widget_name);
     }
   else
     {
        /* Iterate over all properties of "Properties" section*/
        Eina_Iterator *it = eina_json_object_iterator_new(tmp);
        EINA_ITERATOR_FOREACH(it, j)
          {
             Eina_Json_Value *cur;
             const char *prop_name = eina_json_pair_name_get(j);
             Op_Desc *op;
             op = db_mro_op_desc_get(class_name, prop_name);
             if (!op)
               {
                  ERR("Cannot find op description: \"%s\" for class: \"%s\"\n"
                      "Property will not take effect.", prop_name, class_name);
                  continue;
               }
             Gui_Widget_Property *prop = prop_create_for_op(op);
             cur = eina_json_pair_value_get(j);

             unsigned int db_param_count = 0, json_arr_param_count = 0;
             db_param_count = db_op_desc_param_number_get(op);
             json_arr_param_count = eina_json_array_count_get(cur);
             if (db_param_count != json_arr_param_count)
               {
                  ERR("Parsing error! Widget: \"%s\", property: \"%s\". Param count: %d, should be: %d.",
                       widget_name, prop_name, json_arr_param_count, db_param_count);
                  goto bad_end;
               }

             /* current json object must be an array,
              * so convert it to Eina_List */
             _json_arr_to_prop_values(gui_ctx, cur, prop);
             wdg_prop_add(wdg, prop);
          }
        eina_iterator_free(it);
     }
   /* Find "Contains" section */
   tmp = EINA_JSON_OBJECT_VALUE_GET(widget_js, "Contains");
   if (tmp)
     {
        if (wdg_is_container(wdg, DB_DEF_BOX_CLASS))
          {
             const Op_Desc *op_desc = db_container_desc_op_desc_get(db_container_desc_get(DB_DEF_BOX_CLASS), CONTAINER_PACK);
             if (!op_desc)
               {
                  ERR("Cannot find op description: CONTAINER_PACK for class: \"%s\"\n", class_name);
               }
             if ((eina_json_type_get(tmp) == EINA_JSON_TYPE_ARRAY))
               {
                  Eina_Json_Value *v;
                  Eina_Iterator *it = NULL;
                  it = eina_json_array_iterator_new(tmp);

                  const char *s;
                  Gui_Widget_Property *prop;
                  Gui_Value *val;
                  EINA_ITERATOR_FOREACH(it, v)
                    {
                       s = eina_json_string_get(v);

                       prop = prop_create_for_op(op_desc);
                       val = prop_value_nth_get(prop, 0);

                       gui_value_name_id_set(val, GUI_TYPE_OBJECT, gui_context_dep_data_get_id_by_name(gui_ctx, s));
                       wdg_container_content_add(wdg, prop);
                    }
                  eina_iterator_free(it);
               }
          }
        if (wdg_is_container(wdg, DB_DEF_TABLE_CLASS))
          {
             const Op_Desc *op_desc = db_container_desc_op_desc_get(db_container_desc_get(DB_DEF_TABLE_CLASS), CONTAINER_PACK);
             if (!op_desc)
               {
                  ERR("Cannot find op description: CONTAINER_PACK for class: \"%s\"\n", class_name);
               }
             Eina_Iterator *it = eina_json_object_iterator_new(tmp);
             EINA_ITERATOR_FOREACH(it, j)
               {
                  Eina_Json_Value *cur = NULL;
                  Gui_Value *val;
                  Gui_Widget_Property *prop = prop_create_for_op(op_desc);

                  const char *wdg_name = eina_json_pair_name_get(j);
                  val = prop_value_nth_get(prop, 0);
                  gui_value_name_id_set(val, GUI_TYPE_OBJECT, gui_context_dep_data_get_id_by_name(gui_ctx, wdg_name));

                  cur = eina_json_pair_value_get(j);
                  if ((eina_json_type_get(cur) != EINA_JSON_TYPE_ARRAY))
                    {
                       ERR("Erron in \"Contains\" section at \"%s\"", wdg_name_get(wdg));
                       break;
                    }
                  /* current json object must be an array,
                   * so convert it to Eina_List */
                  Eina_Iterator *arr_itr = NULL;
                  Eina_Json_Value *arr_val;
                  arr_itr = eina_json_array_iterator_new(cur);
                  int i = 1;
                  EINA_ITERATOR_FOREACH(arr_itr, arr_val)
                    {
                       if (eina_json_type_get(arr_val) != EINA_JSON_TYPE_NUMBER)
                         {
                            ERR("Element in  \"Contains\" section at \"%s\" widget at \"%s\" element is not NUMBER", wdg_name_get(wdg), wdg_name);
                         }
                       int d = (int) eina_json_number_get(arr_val);
                       val = prop_value_nth_get(prop, i);
                       gui_value_int_set(val, d);
                       i++;
                    }
                  wdg_container_content_add(wdg, prop);
                  eina_iterator_free(arr_itr);
               }
             eina_iterator_free(it);
          }
        if (wdg_is_container(wdg, DB_DEF_CONTAINER_CLASS))
          {
             const Op_Desc *op_desc = db_container_desc_op_desc_get(db_container_desc_get(DB_DEF_CONTAINER_CLASS), CONTAINER_PACK);
             if (!op_desc)
               {
                  ERR("Cannot find op description: CONTAINER_PACK for class: \"%s\"\n", class_name);
               }
             Eina_Iterator *it = eina_json_object_iterator_new(tmp);
             EINA_ITERATOR_FOREACH(it, j)
               {
                  Eina_Json_Value *cur = NULL;
                  Gui_Value *val;
                  Gui_Widget_Property *prop = prop_create_for_op(op_desc);

                  const char *wdg_name = eina_json_pair_name_get(j);
                  /* Object is the 2nd parameter in content_set. */
                  val = prop_value_nth_get(prop, 1);
                  gui_value_name_id_set(val, GUI_TYPE_OBJECT, gui_context_dep_data_get_id_by_name(gui_ctx, wdg_name));

                  cur = eina_json_pair_value_get(j);
                  if ((eina_json_type_get(cur) != EINA_JSON_TYPE_ARRAY))
                    {
                       ERR("Erron in \"Contains\" section at \"%s\"", wdg_name_get(wdg));
                       break;
                    }
                  /* Here only one element is in array - part. */
                  Eina_Json_Value *arr_val = eina_json_array_nth_get(cur, 0);
                  if (eina_json_type_get(arr_val) != EINA_JSON_TYPE_NULL)
                    {
                       /* Part is the 1st parameter in content_set. */
                       val = prop_value_nth_get(prop, 0);
                       gui_value_string_set(val, strdup(eina_json_string_get(arr_val)));
                    }
                  wdg_container_content_add(wdg, prop);
               }
             eina_iterator_free(it);
          }
     }
   /* Look for "Items" section. */
   tmp = EINA_JSON_OBJECT_VALUE_GET(widget_js, "Items");
   if (tmp)
     {
        _read_items_section(gui_ctx, wdg, NULL, tmp);
     }
   /* Find "Callbacks" section */
   tmp = EINA_JSON_OBJECT_VALUE_GET(widget_js, "Callbacks");
   if (tmp)
     {
        Eina_Iterator *it = eina_json_object_iterator_new(tmp);
        EINA_ITERATOR_FOREACH(it, j)
          {
             /* Here j is " "type" : "action" "*/
             const char *type = eina_json_pair_name_get(j);
             Eina_Json_Value *action_js = eina_json_pair_value_get(j);
             const char *action_name = eina_json_string_get(action_js);

             /* action name will be converted into dep_id, in wdg_validate(). */
             Dep_Id name_id = gui_context_dep_data_get_id_by_name(gui_ctx, action_name);
             Gui_Widget_Callback *cb = cb_new(type, name_id);
             if (cb) wdg_cb_add(wdg, cb);
          }
        eina_iterator_free(it);
     }
   return EINA_TRUE;
bad_end:
   if (wdg) wdg_del(wdg);
   return EINA_FALSE;
}

static Eina_Bool
_sequence_parse(Gui_Context *gui_ctx, Dep_Id seq_id, Eina_Json_Value *sequence_js)
{
   Eina_Bool ret = EINA_FALSE;
   const char *seq_name = gui_context_dep_data_get(gui_ctx, seq_id);
   Gui_Sequence *seq = sequence_new(gui_ctx, seq_id);
   Eina_Iterator *it = NULL;
   if (!seq)
     {
        ERR("Cannot create sequence %s. Terminating...", seq_name);
        return EINA_FALSE;
     }

   /* Check if sequence is public */
   Eina_Json_Value *tmp = EINA_JSON_OBJECT_VALUE_GET(sequence_js, "Desc", "public");
   if (tmp)
     {
        Eina_Bool v = eina_json_boolean_get(tmp);
        sequence_public_set(seq, v);
     }

   /* Creation of action */
   tmp = EINA_JSON_OBJECT_VALUE_GET(sequence_js, "Actions");
   if (!tmp)
     {
        ERR("section \"Actions\" not found for sequence: %s", seq_name);
     }
   else
     {
        /* Iterate over all actions of "Actions" section*/
        it = eina_json_object_iterator_new(tmp);
        Eina_Json_Value *j;
        EINA_ITERATOR_FOREACH(it, j)
          {
             const char *action_name;

             action_name = eina_json_pair_name_get(j);
             Gui_Action *action = action_new(seq);
             if (!action)
               {
                  ERR("Cannot create action. Terminating...");
                  goto end;
               }
             if (!strcmp(action_name, "Creation"))
               {
                  Eina_Json_Value *cur = eina_json_pair_value_get(j);
                  if (eina_json_type_get(cur) != EINA_JSON_TYPE_STRING)
                     ERR("Json Value is not string");
                  else
                    {
                       const char *wdg_create_name = eina_json_string_get(cur);
                       Dep_Id wdg_id = gui_context_dep_data_get_id_by_name(gui_ctx, wdg_create_name);
                       action_creation_set(action, wdg_id);
                    }
               }
             else if (!strcmp(action_name, "Modification"))
               {
                  Eina_Json_Value *cur = eina_json_pair_value_get(j);
                  if (eina_json_type_get(cur) != EINA_JSON_TYPE_ARRAY)
                     ERR("Json Value is not array");
                  else
                    {
                       // The first element is the widget name.
                       const char *wdg_modif_name = eina_json_string_get(eina_json_array_nth_get(cur, 0));
                       // check if modif widget is already created
                       Dep_Id wdg_id = gui_context_dep_data_get_id_by_name(gui_ctx, wdg_modif_name);
                       Gui_Widget *modif_wdg = wdg_get(gui_ctx, wdg_id);
                       if (!modif_wdg)
                         {
                            goto end;
                         }

                       // The second element is the property name.
                       // The third element is the list of parameters.
                       const char *prop_name = eina_json_string_get(eina_json_array_nth_get(cur, 1));
                       Gui_Widget_Property *prop;

                       Op_Desc *op;
                       op = db_mro_op_desc_get(wdg_class_name_get(modif_wdg), prop_name);
                       if (!op)
                         {
                            ERR("Cannot find op description for property: \"%s\". Crash is possible.", prop_name);
                         }
                       prop = prop_create_for_op(op);
                       _json_arr_to_prop_values(gui_ctx, eina_json_array_nth_get(cur, 2), prop);
                       action_modif_set(action, wdg_id, prop);
                    }
               }
             else if (!strcmp(action_name, "User"))
               {
                  Eina_Json_Value *cur = eina_json_pair_value_get(j);
                  if (eina_json_type_get(cur) != EINA_JSON_TYPE_ARRAY)
                     ERR("Json Value is not array");
                  else
                    {
                       // The first element is the function name.
                       // The second element is the user data.
                       const char *func_name = eina_json_string_get(eina_json_array_nth_get(cur, 0));
                       const char *user_data = eina_json_string_get(eina_json_array_nth_get(cur, 1));
                       Dep_Id dep_id = gui_context_dep_data_get_id_by_name(gui_ctx, func_name);
                       action_user_set(action, dep_id, user_data);
                    }
               }
             else
               {
                  ERR("Action not recognized: %s", action_name);
                  action_del(action);
                  goto end;
               }
          }
     }
   ret = EINA_TRUE;

end:
   if (!ret)
     {
        if (seq) sequence_del(seq);
     }
   eina_iterator_free(it);
   return ret;
}


static Eina_Bool
_resource_parse(Gui_Context *gui_ctx, const char *res_name EINA_UNUSED, Eina_Json_Value *res_js)
{
   Eina_Bool ret = EINA_FALSE;
   Gui_Resource_Type res_type = RESOURCE_STRING;
   /* Order is important. */

   for(; res_type < RESOURCE_SENTINEL; res_type++)
     {
        const char* section_name = resource_section_name_get(res_type);
        Eina_Json_Value *tmp = EINA_JSON_OBJECT_VALUE_GET(res_js, section_name);
        if (tmp)
          {
             Eina_Iterator *it = eina_json_object_iterator_new(tmp);
             Eina_Json_Value *j;
             EINA_ITERATOR_FOREACH(it, j)
               {
                  const char *id = NULL, *value = NULL;
                  id = eina_json_pair_name_get(j);
                  Eina_Json_Value *cur = eina_json_pair_value_get(j);
                  if (eina_json_type_get(cur) != EINA_JSON_TYPE_STRING)
                    {
                       ERR("Json Value is not string");
                    }
                  else
                    {
                       value = eina_json_string_get(cur);
                    }
                  Gui_Resource *gui_res = gui_context_resource_new(gui_ctx, res_type, id);
                  if (!gui_res)
                    {
                       ERR("Cannot create RESOURCE: type: %s, id: %s", section_name, id);
                       continue;
                    }
                  resource_value_set(gui_res, value);
               }
             eina_iterator_free(it);
          }
     }

   ret = EINA_TRUE;

   return ret;
}

static Eina_Bool
_settings_parse(Gui_Context *gui_ctx, const char *res_name EINA_UNUSED, Eina_Json_Value *res_js)
{
   Eina_Bool ret = EINA_FALSE;
   /* Order is important. */

   const char* project_name;
   Eina_Json_Value *tmp = EINA_JSON_OBJECT_VALUE_GET(res_js, "Settings", "project");
   if (tmp)
     {
        project_name = eina_json_string_get(tmp);
        gui_context_project_name_set(gui_ctx, project_name);
     }
   ret = EINA_TRUE;
   return ret;
}

Eina_List *widgets_lst = NULL, *seq_lst = NULL;

typedef struct
{
   Dep_Id name_id;
   Eina_Json_Value *jv;
} JS_Data;

#define JS_BUILD(__name_id, __jv) \
   ({ \
    JS_Data *_d = calloc(1, sizeof(JS_Data)); \
    _d->jv = __jv; \
    _d->name_id = __name_id; \
    _d; \
    })

/* Register name and save JSON in list. */
static Eina_Bool
_gui_parser_register_name(Gui_Context *gui_ctx, char *_data)
{
   Eina_Json_Context *ctx = eina_json_context_dom_new();
   Eina_Json_Type type;
   Eina_Bool err = eina_json_context_parse(ctx, _data);
   Eina_Bool parse_ret = EINA_FALSE;
   if (!err)
     {
        Eina_Json_Error e = -1;
        e = eina_json_context_error_get(ctx);
        ERR("json parsing error: %d", e);
        printf("%s", _data);
        return EINA_FALSE;
     }

   const char *name;
   Eina_Json_Value *jv, *c;

   jv = eina_json_context_dom_tree_take(ctx);
   type = eina_json_type_get(jv);
   /* check, that first json value is object, t.e. { win : {...} }*/
   if (type != EINA_JSON_TYPE_OBJECT)
     {
        ERR("Json Value is not Object");
        return EINA_FALSE;
     }
   c = eina_json_object_nth_get(jv, 0);
   type = eina_json_type_get(c);
   /* check, that current json value is pair, t.e. "win : {...}"*/
   if (type != EINA_JSON_TYPE_PAIR)
     {
        ERR("Json Value is not Pair");
        return EINA_FALSE;
     }
   name = eina_json_pair_name_get(c);
   /*Get object: {Properties :{..}, Callbacks: {..}}*/
   c = eina_json_pair_value_get(c);
     {
        Eina_Json_Value *tmp;
        tmp = EINA_JSON_OBJECT_VALUE_GET(c, "Desc");
        if (!tmp)
          {
             ERR("section \"Desc\" not found for widget: %s", name);
          }
        else
          {
             /* Get "type" from json */
             Eina_Json_Value *j = EINA_JSON_OBJECT_VALUE_GET(c, "Desc", "type");
             const char *entity_type = NULL;
             if (j) entity_type = eina_json_string_get(j);

             if ((!j) ||
                 (!strcmp(entity_type, "widget")))
               {
                  if (gui_context_dep_data_get_id_by_name(gui_ctx, name))
                    {
                       ERR("Name: %s already used", name);
                       parse_ret = EINA_FALSE;
                       eina_json_value_free(jv);
                       goto end;
                    }
                  Dep_Id name_id = gui_context_dep_data_set(gui_ctx, name);
                  JS_Data *d = JS_BUILD(name_id, jv);
                  widgets_lst = eina_list_append(widgets_lst, d);
                  parse_ret = EINA_TRUE;
               }
             else if (!strcmp(entity_type, "sequence"))
               {
                  Dep_Id name_id = gui_context_dep_data_get_id_by_name(gui_ctx, name);
                  if (!name_id)
                    {
                       ERR("Looks like sequence: \"%s\" was not registered in resources.", name);
                       parse_ret = EINA_FALSE;
                       eina_json_value_free(jv);
                       goto end;
                    }
                  seq_lst = eina_list_append(seq_lst, JS_BUILD(name_id, jv));
                  parse_ret = EINA_TRUE;
               }
             else if (!strcmp(entity_type, "resource"))
               {
                  parse_ret = _resource_parse(gui_ctx, name, c);
                  eina_json_value_free(jv);
               }
             else if (!strcmp(entity_type, "settings"))
               {
                  parse_ret = _settings_parse(gui_ctx, name, c);
                  eina_json_value_free(jv);
               }
          }
     }

end:
   eina_json_context_free(ctx);
   return parse_ret;
}

/* Read json file and fill context*/
Eina_Bool
gui_parser_json_file_read(Gui_Context *ctx, const char *filename)
{
   char *file_data = gui_parser_file_string_get(filename);

     {
        const char *project_name = NULL;
        char *dir = NULL;
        char **tok;
        dir = ecore_file_dir_get(filename);
        gui_context_project_path_set(ctx, dir);

        project_name = ecore_file_file_get(filename);
        tok = eina_str_split(project_name, ".", 0);

        gui_context_project_name_set(ctx, tok[0]);
        gui_context_project_filename_set(ctx, tok[0]);

        free(tok[0]);
        free(tok);
        free(dir);
     }
   if (file_data)
     {
        Eina_List *lst, *l, *l_next;
        void *lst_data;
        int count, i = 0;
        /* List of all jsons. */
        lst = gui_parser_string_all_match_get(file_data);

        /* Number of JSON descriptions in file. */
        count = eina_list_count(lst);

        EINA_LIST_FOREACH_SAFE(lst, l, l_next, lst_data)
          {
             /* Counter to avoid endless loop. */
             i++;
             if (!_gui_parser_register_name(ctx, lst_data))
               {
                  /* This demotion is relevant for sequences.
                   * If json of sequence is going before resources, it's name won't be registered,
                   * so out sequence in the end of list.*/
                  if (i <= count)
                    {
                       lst = eina_list_demote_list(lst, l);
                    }
                  else
                    {
                       ERR("Described sequence/widget cannot be created, likely parent doesn't exist ");
                       ERR("%s", (char *) lst_data);
                    }
                  continue;
               }
          }
        /* deleting list with json strings */
        EINA_LIST_FREE(lst, lst_data)
           free(lst_data);
        lst = NULL;

        JS_Data *js_data;
        /* Creating widgets. */
        EINA_LIST_FOREACH(widgets_lst, l, js_data)
          {
             Eina_Json_Value *jv = js_data->jv;
             Eina_Json_Value *c;
             c = eina_json_object_nth_get(jv, 0);
             c = eina_json_pair_value_get(c);
             Eina_Bool parse_ret = _widget_parse(ctx, js_data->name_id, c);
             if (!parse_ret)
               {
                  ERR("Parsing error: widget");
               }
          }

        /* Creating sequences. */
        EINA_LIST_FOREACH(seq_lst, l, js_data)
          {
             Eina_Json_Value *jv = js_data->jv;
             Eina_Json_Value *c;
             c = eina_json_object_nth_get(jv, 0);
             c = eina_json_pair_value_get(c);
             Eina_Bool parse_ret = _sequence_parse(ctx, js_data->name_id, c);
             if (!parse_ret)
               {
                  ERR("Parsing error: sequence");
               }
          }

        EINA_LIST_FREE(widgets_lst, js_data)
          {
             eina_json_value_free(js_data->jv);
             free(js_data);
          }
        EINA_LIST_FREE(seq_lst, js_data)
          {
             eina_json_value_free(js_data->jv);
             free(js_data);
          }
        lst = NULL;

        free(file_data);
        file_data = NULL;
     }
   return EINA_TRUE;
}