summaryrefslogtreecommitdiff
path: root/email-core/email-core-event.c
blob: 97f32c0180d209938cef0e99178a8c69af760d2f (plain)
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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
/*
*  email-service
*
* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/



/******************************************************************************
 * File :  email-core-event_data.h
 * Desc :  Mail Engine Event
 *
 * Auth :
 *
 * History :
 *    2006.08.16  :  created
 *****************************************************************************/
#include <stdio.h>
#include <glib.h>
#include <malloc.h>
#include <pthread.h>
#include <vconf.h>
#include <signal.h>
#include <contacts.h>
#include "c-client.h"
#include "email-convert.h"
#include "email-storage.h"
#include "email-network.h"
#include "email-device.h"
#include "email-utilities.h"
#include "email-daemon-auto-poll.h"
#include "email-core-global.h"
#include "email-core-account.h"
#include "email-core-event.h"
#include "email-core-utils.h"
#include "email-core-mailbox.h"
#include "email-core-imap-mailbox.h"
#include "email-core-mail.h"
#include "email-core-mailbox-sync.h"
#include "email-core-smtp.h"
#include "email-core-utils.h"
#include "email-core-sound.h"
#include "email-core-signal.h"
#include "email-debug-log.h"


#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__

/*[h.gahlaut] - All static variable declaration for partial body download thread are done here */

#define TOTAL_PARTIAL_BODY_EVENTS 100

static email_event_partial_body_thd g_partial_body_thd_event_que[TOTAL_PARTIAL_BODY_EVENTS];

static int g_partial_body_thd_next_event_idx = 0;			/* Index of Next Event to be processed in the queue*/
static int g_partial_body_thd_loop = 1;						/* Variable to make a continuos while loop */
static int g_partial_body_thd_queue_empty = true;			/* Variable to determine if event queue is empty.True means empty*/
static int g_partial_body_thd_queue_full = false;			/* Variable to determine if event queue is full. True means full*/
static int g_pb_thd_local_activity_continue = true;			/* Variable to control local activity sync */
int g_pbd_thd_state = false;								/* false :  thread is sleeping , true :  thread is working */

static pthread_mutex_t _partial_body_thd_event_queue_lock = PTHREAD_MUTEX_INITIALIZER;	/* Mutex to protect event queue */
static pthread_cond_t  _partial_body_thd_cond = PTHREAD_COND_INITIALIZER;				/* Condition variable on which partial body thread is waiting  */
thread_t g_partial_body_thd ;								/* Determines if thread is created or not. Non Null means thread is created */

email_event_partial_body_thd g_partial_body_bulk_dwd_que[BULK_PARTIAL_BODY_DOWNLOAD_COUNT];
static int g_partial_body_bulk_dwd_next_event_idx = 0;		/* Index of Next Event to be processed in the queue*/
static int g_partial_body_bulk_dwd_queue_empty = true;

static pthread_mutex_t _state_variables_lock;

/*[h.gahlaut] - All static function declaration for partial body download thread are done here */

static int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code);
static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code);
static void emcore_pb_thd_set_local_activity_continue(int flag);
static int emcore_set_pbd_thd_state(int flag);
static int emcore_clear_bulk_pbd_que(int *err_code);
int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code);

#endif

#ifdef ENABLE_IMAP_IDLE_THREAD
extern int g_imap_idle_thread_alive;
extern int imap_idle_thread;
#endif /* ENABLE_IMAP_IDLE_THREAD */
INTERNAL_FUNC int g_client_count = 0;
INTERNAL_FUNC int g_client_run = 0 ;

#ifdef __FEATURE_LOCAL_ACTIVITY__
INTERNAL_FUNC int g_local_activity_run = 0;
INTERNAL_FUNC int g_save_local_activity_run = 0;
#endif


typedef struct EVENT_CALLBACK_ELEM
{
	email_event_callback callback;
	void *event_data;
	struct EVENT_CALLBACK_ELEM *next;
} EVENT_CALLBACK_LIST;

static pthread_mutex_t _event_available_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  _event_available_signal = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t *_event_callback_table_lock = NULL;
static pthread_mutex_t *_event_queue_lock = NULL;
static EVENT_CALLBACK_LIST *_event_callback_table[EMAIL_ACTION_NUM];		/*  array of singly-linked list for event callbacks */

void *thread_func_branch_command(void *arg);

static email_event_t g_event_que[EVENT_QUEUE_MAX];

int send_thread_run = 0;
int recv_thread_run = 0;

static pthread_mutex_t _send_event_available_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t *_send_event_queue_lock = NULL;  /*  MUST BE "recursive" */
static pthread_cond_t  _send_event_available_signal = PTHREAD_COND_INITIALIZER;
static thread_t g_send_srv_thread;
static thread_t g_srv_thread;

static email_event_t g_send_event_que[EVENT_QUEUE_MAX];
static int g_send_event_que_idx = 1;
static int g_send_event_loop = 1;
static int g_send_active_que = 0;
static int g_event_que_idx = 1;
static int g_event_loop = 1;
static int g_active_que = 0;
static int g_sending_busy_cnt = 0;
static int g_receiving_busy_cnt = 0;


INTERNAL_FUNC int emcore_get_current_thread_type()
{
	EM_DEBUG_FUNC_BEGIN();
	thread_t thread_id = THREAD_SELF();
	int thread_type = -1;

	if (thread_id == g_srv_thread)
		thread_type = _SERVICE_THREAD_TYPE_RECEIVING;
	else if (thread_id == g_send_srv_thread)
		thread_type = _SERVICE_THREAD_TYPE_SENDING;
#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
	else if (thread_id == g_partial_body_thd)
		thread_type = _SERVICE_THREAD_TYPE_PBD;
#endif
	EM_DEBUG_FUNC_END("thread_type [%d]", thread_type);
	return thread_type;
}

static int is_gdk_lock_needed()
{
	if (g_event_loop)  {
		return (THREAD_SELF() == g_srv_thread);
	}
	return false;
}

static void _sending_busy_ref(void)
{
	g_sending_busy_cnt++;
}

static void _sending_busy_unref(void)
{
	g_sending_busy_cnt--;
}

static void _receiving_busy_ref(void)
{
	g_receiving_busy_cnt++;
}

static void _receiving_busy_unref(void)
{
	g_receiving_busy_cnt--;
}

static void waiting_status_notify(email_event_t *event_data, int queue_idx)
{
	EM_DEBUG_FUNC_BEGIN("event_data[%p], queue_idx[%d]", event_data, queue_idx);

	int account_id = event_data->account_id;
	int mail_id = event_data->event_param_data_4;		/*  NOT ALWAYS */

	switch (event_data->type)  {
		case EMAIL_EVENT_SEND_MAIL:
			emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_SYNC_HEADER:
			emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_SYNC_HEADER_OMA:
			emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER_OMA, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_DOWNLOAD_BODY:
			emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
			break;

#ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
		case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
			emcore_execute_event_callback(EMAIL_ACTION_SYNC_MAIL_FLAG_TO_SERVER, 0, 0, EMAIL_SYNC_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
			break;
#endif

		case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
			emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_WAITING, account_id, mail_id, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_DELETE_MAIL:
		case EMAIL_EVENT_DELETE_MAIL_ALL:
			emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_VALIDATE_ACCOUNT:
			emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
			emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
			emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT_EX, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_MOVE_MAIL:
			emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAIL, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_CREATE_MAILBOX:
			emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
			emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_UPDATE_MAIL:
			break;

		case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
			emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
			emcore_execute_event_callback(EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED, 0, 0, EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_WAITING, account_id, event_data->event_param_data_4, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_SEARCH_ON_SERVER:
			emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SYNC_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
			/* emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_WAITING, account_id, 0, queue_idx, EMAIL_ERROR_NONE); */
			break;

		default:
			break;
	}
	EM_DEBUG_FUNC_END();
}

static void fail_status_notify(email_event_t *event_data, int error)
{
	EM_DEBUG_FUNC_BEGIN();
	int account_id, mail_id;

	if(!event_data) {
		EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
		return;
	}
	account_id = event_data->account_id;
	mail_id  = event_data->event_param_data_4;

	EM_DEBUG_LOG("account_id[%d], mail_id[%d], error[%d]", account_id, mail_id, error);

	switch (event_data->type)  {
		case EMAIL_EVENT_SEND_MAIL:
			/* case EMAIL_EVENT_SEND_MAIL_SAVED:  */
			/* emcore_execute_event_callback(EMAIL_ACTION_SEND_MAIL, 0, 0, EMAIL_SEND_FAIL, account_id, mail_id, -1, error); */
			emcore_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, error);
			break;

		case EMAIL_EVENT_SYNC_HEADER:
			emcore_execute_event_callback(EMAIL_ACTION_SYNC_HEADER, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_SYNC_HEADER, error);
			break;

		case EMAIL_EVENT_DOWNLOAD_BODY:
			emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_BODY, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_BODY, error);
			break;

		case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
			emcore_execute_event_callback(EMAIL_ACTION_DOWNLOAD_ATTACHMENT, 0, 0, EMAIL_DOWNLOAD_FAIL, account_id, mail_id, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_DOWNLOAD_ATTACHMENT, error);
			break;

		case EMAIL_EVENT_DELETE_MAIL:
		case EMAIL_EVENT_DELETE_MAIL_ALL:
			emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAIL, 0, 0, EMAIL_DELETE_FAIL, account_id, 0, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, error);
			break;

		case EMAIL_EVENT_VALIDATE_ACCOUNT:
			emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_ACCOUNT, error);
			break;

		case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
			emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_CREATE_ACCOUNT, error);
			break;

		case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
			emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_ACCOUNT_EX, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_ACCOUNT_EX, error);
			break;

		case EMAIL_EVENT_CREATE_MAILBOX:
			emcore_execute_event_callback(EMAIL_ACTION_CREATE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_CREATE_MAILBOX, error);
			break;

		case EMAIL_EVENT_DELETE_MAILBOX:
			emcore_execute_event_callback(EMAIL_ACTION_DELETE_MAILBOX, 0, 0, EMAIL_LIST_FAIL, account_id, 0, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAILBOX, error);
			break;

		case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
			emcore_execute_event_callback(EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, 0, 0, EMAIL_VALIDATE_ACCOUNT_FAIL, account_id, 0, -1, error);
			emcore_show_user_message(account_id, EMAIL_ACTION_VALIDATE_AND_UPDATE_ACCOUNT, error);
			break;

		case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
			emcore_execute_event_callback(EMAIL_ACTION_SET_MAIL_SLOT_SIZE, 0, 0, EMAIL_SET_SLOT_SIZE_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_SEARCH_ON_SERVER:
			emcore_execute_event_callback(EMAIL_ACTION_SEARCH_ON_SERVER, 0, 0, EMAIL_SEARCH_ON_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
			emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
			break;

		case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
			emcore_execute_event_callback(EMAIL_ACTION_MOVE_MAILBOX, 0, 0, EMAIL_MOVE_MAILBOX_ON_IMAP_SERVER_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
			emcore_show_user_message(account_id, EMAIL_ACTION_SEARCH_ON_SERVER, error);
			break;

		case EMAIL_EVENT_UPDATE_MAIL:
			emcore_execute_event_callback(EMAIL_ACTION_UPDATE_MAIL, 0, 0, EMAIL_UPDATE_MAIL_FAIL, account_id, 0, -1, EMAIL_ERROR_NONE);
			break;

		case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
			emcore_execute_event_callback(EMAIL_ACTION_EXPUNGE_MAILS_DELETED_FLAGGED, 0, 0, EMAIL_EXPUNGE_MAILS_DELETED_FLAGGED_FAIL, account_id, event_data->event_param_data_4, -1, EMAIL_ERROR_NONE);
			break;

		default:
			break;
	}
	EM_DEBUG_FUNC_END();
}


static void emcore_initialize_event_callback_table()
{
	ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);

	int i;

	for (i = 0; i < EMAIL_ACTION_NUM; i++)
		_event_callback_table[i] = NULL;

	LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
}

int emcore_register_event_callback(email_action_t action, email_event_callback callback, void *event_data)
{
	EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p], event_data[%p]", action, callback, event_data);

	if (callback == NULL)
		return false;

	int ret = false;

	ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);

	EVENT_CALLBACK_LIST *node = _event_callback_table[action];

	while (node != NULL) {
		if (node->callback == callback && node->event_data == event_data)		/*  already registered */
			goto EXIT;

		node = node->next;
	}

	/*  not found, so keep going */

	node = em_malloc(sizeof(EVENT_CALLBACK_LIST));

	if (node == NULL)		/*  not enough memory */
		goto EXIT;

	node->callback = callback;
	node->event_data = event_data;
	node->next = _event_callback_table[action];

	_event_callback_table[action] = node;

	ret = true;

EXIT :
	LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
	EM_DEBUG_FUNC_END();
	return ret;
}

int emcore_unregister_event_callback(email_action_t action, email_event_callback callback)
{
	EM_DEBUG_FUNC_BEGIN("action[%d], callback[%p]", action, callback);

	if (callback == NULL)
		return false;

	int ret = false;

	ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);

	EVENT_CALLBACK_LIST *prev = NULL;
	EVENT_CALLBACK_LIST *node = _event_callback_table[action];

	while (node != NULL) {
		if (node->callback == callback) {
			if (prev != NULL)
				prev->next = node->next;
			else
				_event_callback_table[action] = node->next;

			free(node);

			ret = true;
			break;
		}

		prev = node;
		node = node->next;
	}

	LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);
	EM_DEBUG_FUNC_END();
	return ret;
}

void emcore_execute_event_callback(email_action_t action, int total, int done, int status, int account_id, int mail_id, int handle, int error)
{
	EM_DEBUG_FUNC_BEGIN("action[%d], total[%d], done[%d], status[%d], account_id[%d], mail_id[%d], handle[%d], error[%d]", action, total, done, status, account_id, mail_id, handle, error);

	int lock_needed = 0;
	lock_needed = is_gdk_lock_needed();

	if (lock_needed)  {
		/*  Todo  :  g_thread_yield */
	}

	ENTER_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);

	EVENT_CALLBACK_LIST *node = _event_callback_table[action];

	while (node != NULL)  {
		if (node->callback != NULL)
			node->callback(total, done, status, account_id, mail_id, (handle == -1 ? emcore_get_active_queue_idx()  :  handle), node->event_data, error);
		node = node->next;
	}

	LEAVE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);

	if (lock_needed)  {
	}
	EM_DEBUG_FUNC_END();
}

/* insert a event to event queue */
INTERNAL_FUNC int emcore_insert_event(email_event_t *event_data, int *handle, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);

	if (!event_data)  {
		EM_DEBUG_EXCEPTION("Invalid Parameter");
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_INVALID_PARAM;
		return false;
	}

	if (!g_srv_thread)  {
		EM_DEBUG_EXCEPTION("email-service is not ready");
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
		return false;
	}

	int ret = true;
	int error = EMAIL_ERROR_NONE;

	ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);

	if (!g_event_que[g_event_que_idx].type)  {	/*  if current buffer has not event, insert event data to current buffer  */
		EM_DEBUG_LOG("Current buffer has not a event. [%d]", g_event_que_idx);
		memcpy(g_event_que+g_event_que_idx, event_data, sizeof(email_event_t));
		g_event_que[g_event_que_idx].status = EMAIL_EVENT_STATUS_WAIT;
		waiting_status_notify(event_data, g_event_que_idx);
		if (handle)
			*handle = g_event_que_idx;
	}
	else  {	/*  if current buffer has event, find the empty buffer */
		EM_DEBUG_LOG("Current buffer has a event. [%d]", g_event_que_idx);
		int i, j = g_event_que_idx + 1;

		for (i = 1; i < EVENT_QUEUE_MAX; i++, j++)  {
			if (j >= EVENT_QUEUE_MAX)
				j = 1;

			if (!g_event_que[j].type)
					break;
		}

		if (i < EVENT_QUEUE_MAX)  {
			EM_DEBUG_LOG("I found available buffer. [%d]", g_event_que + j);
			memcpy(g_event_que+j, event_data, sizeof(email_event_t));
			g_event_que[j].status = EMAIL_EVENT_STATUS_WAIT;
			waiting_status_notify(event_data, j);

			if (handle)
				*handle = j;
		}
		else  {
			EM_DEBUG_EXCEPTION("event que is full...");
			error = EMAIL_ERROR_EVENT_QUEUE_FULL;
			ret = false;
		}
	}

	LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);

	if (ret == true)  {
		event_data->event_param_data_1 = NULL;		/*  MUST BE - to prevent double-free */

		switch (event_data->type)  {
			case EMAIL_EVENT_SEND_MAIL:
			case EMAIL_EVENT_SEND_MAIL_SAVED:
				_sending_busy_ref();
				break;

			case EMAIL_EVENT_SYNC_HEADER:
			case EMAIL_EVENT_DOWNLOAD_BODY:
			case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
			case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
			case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
			case EMAIL_EVENT_ISSUE_IDLE:
			case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
			case EMAIL_EVENT_VALIDATE_ACCOUNT:
			case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT:
			case EMAIL_EVENT_VALIDATE_ACCOUNT_EX:
			case EMAIL_EVENT_SAVE_MAIL:
			case EMAIL_EVENT_MOVE_MAIL:
			case EMAIL_EVENT_DELETE_MAIL:
			case EMAIL_EVENT_DELETE_MAIL_ALL:
			case EMAIL_EVENT_SYNC_HEADER_OMA:
			case EMAIL_EVENT_CREATE_MAILBOX:
			case EMAIL_EVENT_DELETE_MAILBOX:
			case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT:
			case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
			case EMAIL_EVENT_UPDATE_MAIL:
			case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
			case EMAIL_EVENT_SEARCH_ON_SERVER:
			case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
				_receiving_busy_ref();
				break;
			default:
				break;
		}

		ENTER_CRITICAL_SECTION(_event_available_lock);
		WAKE_CONDITION_VARIABLE(_event_available_signal);
		LEAVE_CRITICAL_SECTION(_event_available_lock);
	}

#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
	{
		int is_local_activity_event_inserted = false;

		if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &error))
			EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", error);
		else {
			if (true == is_local_activity_event_inserted)
				emcore_pb_thd_set_local_activity_continue(false);
		}
	}
#endif

	if (err_code != NULL)
		*err_code = error;

	EM_DEBUG_LOG("Finish with [%d]", ret);
	return ret;
}

/* get a event from event_data queue */
static int emcore_retrieve_event(email_event_t *event_data, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);

	int ret = false;
	int error = EMAIL_ERROR_NONE;

	ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);

	/*  get a event_data if this queue is not empty */
	if (g_event_que[g_event_que_idx].type)  {
		memcpy(event_data, g_event_que+g_event_que_idx, sizeof(email_event_t));

		if (event_data->status != EMAIL_EVENT_STATUS_WAIT)  {	/*  EMAIL_EVENT_STATUS_CANCELED */
			memset(g_event_que+g_event_que_idx, 0x00, sizeof(email_event_t));
			g_active_que = 0;
		}
		else  {
			EM_DEBUG_LINE;
			g_event_que[g_event_que_idx].status = EMAIL_EVENT_STATUS_STARTED;
			g_active_que = g_event_que_idx;
			ret = true;
		}

		if (++g_event_que_idx >= EVENT_QUEUE_MAX)
			g_event_que_idx = 1;

		EM_DEBUG_LOG("g_event_que[%d].type [%d]", g_active_que, g_event_que[g_active_que].type);
	}
	else  {
		g_active_que = 0;
		error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
	}

	LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);

	if (err_code != NULL)
		*err_code = error;

	EM_DEBUG_FUNC_END("ret [%d]", ret);
	return ret;
}

/* check that event_data loop is continuous */
static int emcore_event_loop_continue()
{
    return g_event_loop;
}



INTERNAL_FUNC int emcore_insert_event_for_sending_mails(email_event_t *event_data, int *handle, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN("event_data[%p], handle[%p], err_code[%p]", event_data, handle, err_code);

	if (!event_data)  {
		EM_DEBUG_EXCEPTION("\t event_data[%p], handle[%p]", event_data, handle);

		if (err_code != NULL)
			*err_code = EMAIL_ERROR_INVALID_PARAM;
		return false;
	}

	int ret = true;
	int error = EMAIL_ERROR_NONE;

	ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);

	if (!g_send_event_que[g_send_event_que_idx].type)  {
		/* if current buffer has not event_data, insert event_data data to current buffer */
		EM_DEBUG_LOG("Current buffer has not a event_data. [%d]", g_send_event_que_idx);
		memcpy(g_send_event_que+g_send_event_que_idx, event_data, sizeof(email_event_t));

		g_send_event_que[g_send_event_que_idx].status = EMAIL_EVENT_STATUS_WAIT;

		if (handle)
			*handle = g_send_event_que_idx;
	}
	else  {
		/* if current buffer has event_data, find the empty buffer */
		EM_DEBUG_LOG("Current buffer has a event_data. [%d]", g_send_event_que_idx);
		int i, j = g_send_event_que_idx + 1;

		for (i = 1; i < EVENT_QUEUE_MAX; i++, j++)  {
			if (j >= EVENT_QUEUE_MAX)
				j = 1;

			if (!g_send_event_que[j].type)
					break;
			}

		if (i < EVENT_QUEUE_MAX)  {
			EM_DEBUG_LOG("I found available buffer. [%d]", j);
			memcpy(g_send_event_que+j, event_data, sizeof(email_event_t));
			g_send_event_que[j].status = EMAIL_EVENT_STATUS_WAIT;
			if (handle) *handle = j;
		}
		else  {
			EM_DEBUG_EXCEPTION("event_data queue is full...");
			error = EMAIL_ERROR_EVENT_QUEUE_FULL;
			ret = false;
		}
	}

	LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);

	if (ret == true)  {
		ENTER_CRITICAL_SECTION(_send_event_available_lock);
		WAKE_CONDITION_VARIABLE(_send_event_available_signal);
		LEAVE_CRITICAL_SECTION(_send_event_available_lock);
	}

	if (handle)
	EM_DEBUG_LOG("emcore_insert_event_for_sending_mails-handle[%d]", *handle);

	if (err_code != NULL)
		*err_code = error;

	/* EM_DEBUG_FUNC_BEGIN(); */
	return ret;
}


static int emcore_retrieve_send_event(email_event_t *event_data, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN();

	int ret = false;
	int error = EMAIL_ERROR_NONE;

	ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
/* get a event_data if this queue is not empty */
	if (g_send_event_que[g_send_event_que_idx].type)  {
		memcpy(event_data, g_send_event_que+g_send_event_que_idx, sizeof(email_event_t));

		if (event_data->status != EMAIL_EVENT_STATUS_WAIT) {
			memset(g_send_event_que+g_send_event_que_idx, 0x00, sizeof(email_event_t));
			g_send_active_que = 0;
		}
		else  {
			g_send_event_que[g_send_event_que_idx].status = EMAIL_EVENT_STATUS_STARTED;
			EM_DEBUG_LOG("g_send_event_que_idx[%d]", g_send_event_que_idx);
			g_send_active_que = g_send_event_que_idx;

			ret = true;
		}

		if (++g_send_event_que_idx >= EVENT_QUEUE_MAX)
			g_send_event_que_idx = 1;

			EM_DEBUG_LOG("\t g_send_event_que[%d].type = %d", g_send_active_que, g_send_event_que[g_send_active_que].type);
	}
	else  {
			EM_DEBUG_LOG("\t send event_data queue is empty...");
			g_send_active_que = 0;
			error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
	}

	LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);

	if (err_code != NULL)
		*err_code = error;

	return ret;
}


void* thread_func_branch_command_for_sending_mails(void *arg)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	email_event_t event_data;
	email_session_t *session = NULL;

	if (!emstorage_open(&err))  {
		EM_DEBUG_EXCEPTION("\t emstorage_open falied - %d", err);
		return NULL;
	}

	while (g_send_event_loop) {
		if (!emcore_get_empty_session(&session))
			EM_DEBUG_EXCEPTION("\t SEND THREAD emcore_get_empty_session failed...");

		if (!emcore_retrieve_send_event(&event_data, NULL))  {
			EM_DEBUG_LOG(">>>> waiting for send event_data>>>>>>>>>");
#ifdef __FEATURE_LOCAL_ACTIVITY__
			if (send_thread_run && g_save_local_activity_run) {
				emstorage_account_tbl_t *account_list = NULL;
				int count = 0, i;
				if (!emstorage_get_account_list(&count, &account_list, true, true, &err)) {
					EM_DEBUG_LOG("\t emstorage_get_account_list failed - %d", err);
				}
				else {
					for (i = 0; i < count; i++) {
						if (emcore_save_local_activity_sync(account_list[i].account_id, &err)) {
							EM_DEBUG_LOG("Found local activity...!");
							EM_DEBUG_LOG("Resetting g_save_local_activity_run ");
							g_save_local_activity_run = 0;
							emcore_clear_session(session);
						}
					}

					emstorage_free_account(&account_list, count, &err);

					if (!g_save_local_activity_run) {
						continue;
					}
				}
			}
#endif
			send_thread_run = 0;

			ENTER_CRITICAL_SECTION(_send_event_available_lock);
			SLEEP_CONDITION_VARIABLE(_send_event_available_signal, _send_event_available_lock);
			LEAVE_CRITICAL_SECTION(_send_event_available_lock);
		}
		else {
			EM_DEBUG_LOG(">>>>>>>>>>>>>>Got SEND event_data>>>>>>>>>>>>>>>>");
			send_thread_run = 1;
			g_client_run = 1;

			if (!emnetwork_check_network_status( &err))  {
				EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);

				emcore_show_user_message(event_data.event_param_data_4, EMAIL_ACTION_SEND_MAIL, err);
				if (!emcore_notify_network_event(NOTI_SEND_FAIL, event_data.account_id, NULL , event_data.event_param_data_4, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_FAIL] Failed >>>> ");
				goto FINISH_OFF;
			}

			switch (event_data.type)  {

				case EMAIL_EVENT_SEND_MAIL:
					emdevice_set_dimming_on_off(false, NULL);

					if (!emcore_send_mail(event_data.event_param_data_4, &err))
						EM_DEBUG_EXCEPTION("emcore_send_mail failed [%d]", err);

					emdevice_set_dimming_on_off(true, NULL);
					break;

				case EMAIL_EVENT_SEND_MAIL_SAVED:  /* send mails to been saved in off-line mode */
					emdevice_set_dimming_on_off(false, NULL);

					if (!emcore_send_saved_mail(event_data.account_id, event_data.event_param_data_3, &err))
						EM_DEBUG_EXCEPTION("emcore_send_saved_mail failed - %d", err);

					emdevice_set_dimming_on_off(true, NULL);
					break;

#ifdef __FEATURE_LOCAL_ACTIVITY__

				case EMAIL_EVENT_LOCAL_ACTIVITY: {
					emdevice_set_dimming_on_off(false, NULL);
					emstorage_activity_tbl_t *local_activity = NULL;
					int activity_id_count = 0;
					int activity_chunk_count = 0;
					int *activity_id_list = NULL;
					int i = 0;

					if (false == emstorage_get_activity_id_list(event_data.account_id, &activity_id_list, &activity_id_count, ACTIVITY_SAVEMAIL, ACTIVITY_DELETEMAIL_SEND, true, &err)) {
						EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
					}
					else {
						for (i = 0; i < activity_id_count; ++i) {
							if ((false == emstorage_get_activity(event_data.account_id, activity_id_list[i], &local_activity, &activity_chunk_count, true,  &err)) || (NULL == local_activity) || (0 == activity_chunk_count)) {
								EM_DEBUG_EXCEPTION(" emstorage_get_activity Failed [ %d] or local_activity is NULL [%p] or activity_chunk_count is 0[%d]", err, local_activity, activity_chunk_count);
							}
							else {
								EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
								switch (local_activity[0].activity_type) {
									case ACTIVITY_SAVEMAIL:  {
										if (!emcore_sync_mail_from_client_to_server(event_data.account_id, local_activity[0].mail_id, &err)) {
											EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed - %d ", err);
										}
									}
									break;

									case ACTIVITY_DELETEMAIL_SEND: 				/* New Activity Type Added for Race Condition and Crash Fix */ {
										if (!emcore_delete_mail(local_activity[0].account_id,
																&local_activity[0].mail_id,
																EMAIL_DELETE_FOR_SEND_THREAD,
																true,
																EMAIL_DELETED_BY_COMMAND,
																false,
																&err))  {
											EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
										}
									}
									break;

									default:  {
										EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
									}
									break;
								}

								emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);

								if (g_save_local_activity_run == 1) {
									EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
									break;
								}
							}

						}
						if (false == emstorage_free_activity_id_list(activity_id_list, &err)) {
							EM_DEBUG_LOG("emstorage_free_activity_id_list failed");
						}
					}

					emdevice_set_dimming_on_off(true, NULL);
				}
				break;
#endif /* __FEATURE_LOCAL_ACTIVITY__ */
				default:
					EM_DEBUG_LOG("Others not supported by Send Thread..! [%d]", event_data.type);
					break;
			}

			ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
			memset(g_send_event_que+g_send_active_que, 0x00, sizeof(email_event_t));
			LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);

FINISH_OFF:
			;
		}
		emcore_clear_session(session);
	}

	if (!emstorage_close(&err))
		EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return NULL;
}


int event_handler_EMAIL_EVENT_SYNC_HEADER(int input_account_id, int input_mailbox_id, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], handle_to_be_published [%d], error[%p]", input_account_id, input_mailbox_id, handle_to_be_published, error);

	int err = EMAIL_ERROR_NONE, sync_type = 0, ret = false;
	int mailbox_count = 0, account_count = 0;
	int counter, account_index;
	int unread = 0, total_unread = 0;
	emcore_uid_list *uid_list = NULL;
	emstorage_account_tbl_t *account_tbl_array = NULL;
	emstorage_mailbox_tbl_t *mailbox_tbl_target = NULL, *mailbox_tbl_list = NULL;
#ifndef __FEATURE_KEEP_CONNECTION__
	MAILSTREAM *stream = NULL;
#endif
	char mailbox_id_param_string[10] = {0,};
	char *input_mailbox_id_str = NULL;

	if (input_mailbox_id == 0)
		sync_type = EMAIL_SYNC_ALL_MAILBOX;
	else {
		if (!emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl_target) || !mailbox_tbl_target) {
			EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
			goto FINISH_OFF;
		}
	}
	if(mailbox_tbl_target)
		SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_target->mailbox_id);

	input_mailbox_id_str = (input_mailbox_id == 0)? NULL: mailbox_id_param_string;

	if (!emcore_notify_network_event(NOTI_DOWNLOAD_START, input_account_id, input_mailbox_id_str, handle_to_be_published, 0))
		EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_START] Failed >>>> ");

	if (!emnetwork_check_network_status(&err)) {
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);

		if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
			EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
	}
	else {
		if (sync_type != EMAIL_SYNC_ALL_MAILBOX) {	/* Sync only particular mailbox */

			if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
				EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);

			if (!emcore_sync_header(mailbox_tbl_target, NULL, &uid_list, &unread, &err)) {
				EM_DEBUG_EXCEPTION("emcore_sync_header failed [%d]", err);
				if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
			}
			else {
				EM_DEBUG_LOG("emcore_sync_header succeeded [%d]", err);
				if (!emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, mailbox_tbl_target->account_id, mailbox_id_param_string, handle_to_be_published, 0))
					EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FINISH] Failed >>>> ");
			}

			total_unread += unread;

			if (total_unread > 0 && (err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
				EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);

			if (!emcore_finalize_sync(input_account_id, &err))
				EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
		}
		else /*  All Foder */ {
			EM_DEBUG_LOG(">>>> SYNC ALL MAILBOX ");
			/*  Sync of all mailbox */

			if (input_account_id == ALL_ACCOUNT) {
				if ((err = emcore_update_sync_status_of_account(ALL_ACCOUNT, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
					EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);

				if (!emstorage_get_account_list(&account_count, &account_tbl_array , true, false, &err)) {
					EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [ %d ] ", err);
					if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, NULL,  handle_to_be_published, err))
						EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
					goto FINISH_OFF;
				}
			}
			else {
				EM_DEBUG_LOG("Sync all mailbox of an account[%d].", input_account_id);

				if ((err = emcore_update_sync_status_of_account(input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
					EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);

				if (!emstorage_get_account_by_id(input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl_array, true, &err)) {
					EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [ %d ] ", err);
					if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, input_account_id, input_mailbox_id_str, handle_to_be_published, err))
						EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");
					goto FINISH_OFF;
				}
				account_count = 1;
			}

			for (account_index = 0 ; account_index < account_count; account_index++) {
				if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
					EM_DEBUG_LOG("account[%d] is for ActiveSync. Skip  ", account_index);
					continue;
				}

				if (!emstorage_get_mailbox_list(account_tbl_array[account_index].account_id, 0, EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &mailbox_count, &mailbox_tbl_list, true, &err) || mailbox_count <= 0) {
					EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);

					if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, input_mailbox_id_str, handle_to_be_published, err))
						EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");

					continue;
				}

				EM_DEBUG_LOG("emcore_get_mailbox_list_to_be_sync returns [%d] mailboxes", mailbox_count);

				if (mailbox_count > 0) {
#ifndef __FEATURE_KEEP_CONNECTION__
					if (account_tbl_array[account_index].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
						memset(mailbox_id_param_string, 0, 10);
						SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[0].mailbox_id);
						if (!emcore_connect_to_remote_mailbox(account_tbl_array[account_index].account_id, mailbox_tbl_list[0].mailbox_id, (void **)&stream, &err))  {
							EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
							if (err == EMAIL_ERROR_LOGIN_FAILURE)
								EM_DEBUG_EXCEPTION("EMAIL_ERROR_LOGIN_FAILURE ");
							/* continue; */
							if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string,  handle_to_be_published, err))
								EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
							continue;
						}
						EM_DEBUG_LOG("emcore_connect_to_remote_mailbox returns [%d] : ", err);
					}
					else
						stream = NULL;
#endif
				}

				for (counter = 0; counter < mailbox_count; counter++) {

					EM_DEBUG_LOG("maiblox_name [%s], mailbox_id [%d], mailbox_type [%d]", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, mailbox_tbl_list[counter].mailbox_type);

					if ( mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_ALL_EMAILS
							|| mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_TRASH
						/*|| mailbox_tbl_list[counter].mailbox_type == EMAIL_MAILBOX_TYPE_SPAMBOX */)
						EM_DEBUG_LOG("Skipped for all emails or trash");
					else if (!mailbox_tbl_list[counter].local_yn) {
						EM_DEBUG_LOG("[%s] Syncing...", mailbox_tbl_list[counter].mailbox_name);
#ifdef __FEATURE_KEEP_CONNECTION__
						if (!emcore_sync_header((mailbox_tbl_list + counter) , NULL, &uid_list, &unread, &err)) {
#else /*  __FEATURE_KEEP_CONNECTION__ */
						if (!emcore_sync_header((mailbox_tbl_list + counter) , (void *)stream, &uid_list, &unread, &err)) {
#endif /*  __FEATURE_KEEP_CONNECTION__ */
							EM_DEBUG_EXCEPTION("emcore_sync_header for %s(mailbox_id = %d) failed [%d]", mailbox_tbl_list[counter].mailbox_name, mailbox_tbl_list[counter].mailbox_id, err);

#ifndef __FEATURE_KEEP_CONNECTION__
							if (err == EMAIL_ERROR_CONNECTION_BROKEN || err == EMAIL_ERROR_NO_SUCH_HOST || err == EMAIL_ERROR_SOCKET_FAILURE || err == EMAIL_ERROR_LOGIN_FAILURE)
								stream = NULL;    /*  Don't retry to connect for broken connection. It might cause crash.  */
#endif /*  __FEATURE_KEEP_CONNECTION__ */
							memset(mailbox_id_param_string, 0, 10);
							SNPRINTF(mailbox_id_param_string, 10, "%d", mailbox_tbl_list[counter].mailbox_id);
							if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_tbl_array[account_index].account_id, mailbox_id_param_string,  handle_to_be_published, err))
								EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FAIL] Failed >>>> ");

							break;
						}
					}
					total_unread  += unread;
				}

				EM_DEBUG_LOG("Sync for account_id(%d) is completed....!", account_tbl_array[account_index].account_id);
				if ((err == EMAIL_ERROR_NONE) && !emcore_notify_network_event(NOTI_DOWNLOAD_FINISH, account_tbl_array[account_index].account_id, NULL, handle_to_be_published, 0))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_FINISH] Failed >>>> ");

				if ((total_unread > 0) && (err = emcore_update_sync_status_of_account(account_tbl_array[account_index].account_id, SET_TYPE_UNION, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
					EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);

				if (!emcore_finalize_sync(account_tbl_array[account_index].account_id, &err))
					EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
#ifndef __FEATURE_KEEP_CONNECTION__
				if (stream)  {
					emcore_close_mailbox(0, stream);
					stream = NULL;
				}
#endif
				if (mailbox_tbl_list) {
					emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
					mailbox_tbl_list = NULL;
					mailbox_count = 0;
				}
			}
		}

		ret = true;

FINISH_OFF:

#ifndef __FEATURE_KEEP_CONNECTION__
		if (stream)
			emcore_close_mailbox(0, stream);
#endif
		if(mailbox_tbl_target)
			emstorage_free_mailbox(&mailbox_tbl_target, 1, NULL);

		if (mailbox_tbl_list)
			emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);

		if (account_tbl_array)
			emstorage_free_account(&account_tbl_array, account_count, NULL);
	}

	EM_DEBUG_FUNC_END();
	return ret;
}

int event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(email_account_t *account, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN("account [%p]", account);
	int err, ret = false;

	if(!account) {
		EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	EM_DEBUG_LOG("incoming_server_address  :  %s", account->incoming_server_address);

	if (!emnetwork_check_network_status(&err)) {
		emcore_delete_account_from_unvalidated_account_list(account->account_id);
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
		if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL,  handle_to_be_published, err))
			EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
		goto FINISH_OFF;
	}
	else {
		EM_DEBUG_LOG("incoming_server_address : %s", account->incoming_server_address);

		if (!emcore_validate_account_with_account_info(account, &err)) {
			emcore_delete_account_from_unvalidated_account_list(account->account_id);
			EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err :  %d", err);
			if (err == EMAIL_ERROR_CANCELLED) {
				EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
				if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL, account->account_id, NULL,  handle_to_be_published, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL] Failed");
				goto FINISH_OFF;
			}
			else
				goto FINISH_OFF;
		}
		else {
			emcore_delete_account_from_unvalidated_account_list(account->account_id);

			if (emcore_create_account(account, &err) == false)	 {
				EM_DEBUG_EXCEPTION(" emdaemon_create_account failed - %d", err);
				goto FINISH_OFF;
			}

			emcore_init_account_reference();

			EM_DEBUG_LOG("incoming_server_type [%d]", account->incoming_server_type);

			if ((EMAIL_SERVER_TYPE_IMAP4 == account->incoming_server_type)) {
				if (!emcore_sync_mailbox_list(account->account_id, "", handle_to_be_published, &err))  {
					EM_DEBUG_EXCEPTION("emcore_get_mailbox_list_to_be_sync failed [%d]", err);
					/*  delete account whose mailbox couldn't be obtained from server */
					emcore_delete_account(account->account_id, NULL);
					goto FINISH_OFF;
				}
			}

			EM_DEBUG_LOG("validating and creating an account are succeeded for account id  [%d]  err [%d]", account->account_id, err);
			if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FINISH, account->account_id, NULL,  handle_to_be_published, err))
				EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
		}
	}

	ret = true;

FINISH_OFF:
	if (ret == false && err != EMAIL_ERROR_CANCELLED && account) {
		if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, account->account_id, NULL,  handle_to_be_published, err))
			EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return ret;
}

int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(email_account_t *input_account, int input_handle_to_be_published)
{
	EM_DEBUG_FUNC_BEGIN("input_account [%p]", input_account);
	int err = EMAIL_ERROR_NONE;

	if(!input_account) {
		EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	EM_DEBUG_LOG("incoming_server_address [%s]", input_account->incoming_server_address);

	if (!emnetwork_check_network_status(&err)) {
		emcore_delete_account_from_unvalidated_account_list(input_account->account_id);
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
		if (!emcore_notify_network_event(NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL, input_account->account_id, NULL, input_handle_to_be_published, err))
			EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
		goto FINISH_OFF;
	}
	else {
		EM_DEBUG_LOG("incoming_server_address : %s", input_account->incoming_server_address);

		if (!emcore_validate_account_with_account_info(input_account, &err)) {
			emcore_delete_account_from_unvalidated_account_list(input_account->account_id);

			EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed err :  %d", err);

			if (err == EMAIL_ERROR_CANCELLED) {
				EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_ACCOUNT_CANCEL ");
				if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, input_account->account_id, NULL, input_handle_to_be_published, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed");
				goto FINISH_OFF;
			}
			else
				goto FINISH_OFF;
		}
		else {
			emcore_delete_account_from_unvalidated_account_list(input_account->account_id);

			EM_DEBUG_LOG("validating an account are succeeded for account id  [%d]  err [%d]", input_account->account_id, err);
			if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, input_account->account_id, NULL, input_handle_to_be_published, err))
				EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success");
		}
	}

FINISH_OFF:
	if (err != EMAIL_ERROR_NONE && err != EMAIL_ERROR_CANCELLED && input_account) {
		if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, input_account->account_id, NULL, input_handle_to_be_published, err))
			EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed");
	}

	EM_DEBUG_FUNC_END("err[%d]", err);
	return err;
}

int event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(int account_id, email_account_t *new_account_info, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN("account_id [%d], new_account_info [%p]", account_id, new_account_info);
	int err, ret = false;
	emstorage_account_tbl_t *old_account_tbl = NULL, *new_account_tbl = NULL;

	if (!new_account_info) {
		EM_DEBUG_EXCEPTION("Invalid Parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	if (!emnetwork_check_network_status(&err))  {
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);

		if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL,  handle_to_be_published, err))
			EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL] Failed >>>> ");
		goto FINISH_OFF;
	}
	else  {
		EM_DEBUG_LOG("incoming_server_address: (%s)", new_account_info->incoming_server_address);

		if (!emcore_validate_account_with_account_info(new_account_info, &err)) {
			EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info() failed err :  %d", err);
			if (err == EMAIL_ERROR_CANCELLED) {
				EM_DEBUG_EXCEPTION(" notify  :  NOTI_VALIDATE_AND_CREATE_ACCOUNT_CANCEL ");
				if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL, new_account_info->account_id, NULL,  handle_to_be_published, err))
					EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_AND_UPDATE_ACCOUNT_CANCEL] Failed");
				goto FINISH_OFF;
			}
			else {
				goto FINISH_OFF;
			}
		}
		else {
			if (!emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &old_account_tbl, true, &err)) {
				EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
				/* goto FINISH_OFF; */
			}

			new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
			if (!new_account_tbl) {
				EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
				goto FINISH_OFF;
			}

			em_convert_account_to_account_tbl(new_account_info, new_account_tbl);

			if (emstorage_update_account(account_id, new_account_tbl, true, &err)) {
				emcore_init_account_reference();
			}

			EM_DEBUG_LOG("validating and updating an account are succeeded for account id [%d], err [%d]", new_account_info->account_id, err);
			if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH, new_account_info->account_id, NULL,  handle_to_be_published, err))
				EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FINISH] Success");
		}
	}

	ret = true;

FINISH_OFF:
	if (old_account_tbl)
		emstorage_free_account(&old_account_tbl, 1, NULL);
	if (new_account_tbl)
		emstorage_free_account(&new_account_tbl, 1, NULL);

	if (ret == false && err != EMAIL_ERROR_CANCELLED && new_account_info) {
		if (!emcore_notify_network_event(NOTI_VALIDATE_AND_UPDATE_ACCOUNT_FAIL, new_account_info->account_id, NULL,  handle_to_be_published, err))
			EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_AND_CREATE_ACCOUNT_FAIL] Failed");
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return ret;
}

int event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(int account_id, int mailbox_id, int new_slot_size, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN();

	emcore_set_mail_slot_size(account_id, mailbox_id, new_slot_size, error);

	EM_DEBUG_FUNC_END();
	return true;
}

static int event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(int input_account_id, int input_mailbox_id)
{
	EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d]", input_account_id, input_mailbox_id);
	int err = EMAIL_ERROR_NONE;

	if ( (err = emcore_expunge_mails_deleted_flagged_from_remote_server(input_account_id, input_mailbox_id)) != EMAIL_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_remote_server failed [%d]", err);
		goto FINISH_OFF;
	}

	if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
		goto FINISH_OFF;
	}

FINISH_OFF:

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

#ifdef __FEATURE_LOCAL_ACTIVITY__
int event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(int account_id, int *error)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;
	email_mailbox_t mailbox;
	emstorage_activity_tbl_t *local_activity = NULL;
	int activity_id_count = 0;
	int activity_chunk_count = 0;
	int *activity_id_list = NULL;
	int i = 0;

	if (!emnetwork_check_network_status(&err))
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
	else {
		if (false == emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_id_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
			EM_DEBUG_EXCEPTION("emstorage_get_activity_id_list failed [%d]", err);
		}
		else {
			for (i = 0; i < activity_id_count; ++i) {
				if ((false == emstorage_get_activity(account_id , activity_id_list[i], &local_activity, &activity_chunk_count, true,  &err)) || (NULL == local_activity) || (0 == activity_chunk_count))
					EM_DEBUG_EXCEPTION(" emstorage_get_activity Failed [ %d] or local_activity is NULL [%p] or activity_chunk_count is 0[%d]", err, local_activity, activity_chunk_count);
				else {
					EM_DEBUG_LOG("Found local activity type - %d", local_activity[0].activity_type);
					switch (local_activity[0].activity_type) {
						case ACTIVITY_MODIFYFLAG:  {
							if (emcore_sync_flag_with_server(local_activity[0].mail_id , &err))  {
								if (!emcore_delete_activity(&local_activity[0], &err))
									EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
							}
						}
						break;

						case ACTIVITY_DELETEMAIL:
						case ACTIVITY_MOVEMAIL:
						case ACTIVITY_MODIFYSEENFLAG:
						case ACTIVITY_COPYMAIL:  {

							int j = 0, k = 0;
							int total_mail_ids = activity_chunk_count;

							int *mail_id_list = NULL;

							mail_id_list = (int *)em_malloc(sizeof(int) * total_mail_ids);

							if (NULL == mail_id_list) {
								EM_DEBUG_EXCEPTION("malloc failed... ");
								break;
							}

							do {

								for (j = 0; j < BULK_OPERATION_COUNT && (k < total_mail_ids); ++j, ++k)
									mail_id_list[j] = local_activity[k].mail_id;

								switch (local_activity[k-1].activity_type) {
									case ACTIVITY_DELETEMAIL:  {
										if (!emcore_delete_mail(local_activity[k-1].account_id,
																mail_id_list,
																j,
																EMAIL_DELETE_LOCAL_AND_SERVER,
																EMAIL_DELETED_BY_COMMAND,
																false,
																&err))
											EM_DEBUG_LOG("\t emcore_delete_mail failed - %d", err);
									}
									break;

									case ACTIVITY_MOVEMAIL:  {
										if (!emcore_move_mail_on_server_ex(local_activity[k-1].account_id ,
																		   local_activity[k-1].src_mbox,
																		   mail_id_list,
																		   j,
																		   local_activity[k-1].dest_mbox,
																		   &err))
											EM_DEBUG_LOG("\t emcore_move_mail_on_server_ex failed - %d", err);
									}
									break;
									case ACTIVITY_MODIFYSEENFLAG:  {
										int seen_flag = atoi(local_activity[0].src_mbox);
										if (!emcore_sync_seen_flag_with_server_ex(mail_id_list, j , seen_flag , &err)) /* local_activity[0].src_mbox points to the seen flag */
											EM_DEBUG_EXCEPTION("\t emcore_sync_seen_flag_with_server_ex failed - %d", err);
									}
									break;
								}

							} while (k < total_mail_ids);

							EM_SAFE_FREE(mail_id_list);
						}

						break;

						default:
							EM_DEBUG_LOG(">>>> No such Local Activity Handled by this thread [ %d ] >>> ", local_activity[0].activity_type);
						break;
					}

					emstorage_free_local_activity(&local_activity, activity_chunk_count, NULL);

					if (g_local_activity_run == 1) {
						EM_DEBUG_LOG(" Network event_data found.. Local sync Stopped..! ");
						break;
					}
				}
			}
		}
	}
	if (activity_id_list) {
		if (false == emstorage_free_activity_id_list(activity_id_list, &err))
			EM_DEBUG_EXCEPTION("emstorage_free_activity_id_list failed");
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();

	return true;
}
#endif /* __FEATURE_LOCAL_ACTIVITY__ */

int event_handler_EMAIL_EVENT_DOWNLOAD_BODY(int account_id, int mail_id, int option, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;
	email_mailbox_t mailbox;

	memset(&mailbox, 0x00, sizeof(mailbox));
	mailbox.account_id = account_id;

	if (!emnetwork_check_network_status(&err))  {
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);

		emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, handle_to_be_published, err);
	}
	else  {
		if (!emcore_download_body_multi_sections_bulk(NULL,
			mailbox.account_id,
			mail_id,
			option >> 1, 		/*  0 :  silent, 1 :  verbose */
			option & 0x01, 		/*  0 :  without attachments, 1 :  with attachments */
			NO_LIMITATION,
			handle_to_be_published,
			&err))
			EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed - %d", err);
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

int event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(int account_id, int mail_id, int attachment_no, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;

	EM_DEBUG_LOG("attachment_no is %d", attachment_no);

	if (!emnetwork_check_network_status(&err))  {
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
		emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
	}
	else  {

#ifdef __ATTACHMENT_OPTI__
		if (!emcore_download_attachment_bulk(account_id, mail_id, attachment_no, &err))
			EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
#else
		if (!emcore_download_attachment(account_id, mail_id, attachment_no, &err))
			EM_DEBUG_EXCEPTION("\t emcore_download_attachment failed [%d]", err);
#endif
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

int event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER(int mail_ids[], int num, email_flags_field_type field_type, int value, int *error)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;

	if (!emnetwork_check_network_status(&err))
		EM_DEBUG_EXCEPTION("dnet_init failed [%d]", err);
	else if (!emcore_sync_flags_field_with_server(mail_ids, num, field_type, value, &err))
		EM_DEBUG_EXCEPTION("emcore_sync_flags_field_with_server failed [%d]", err);

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

int event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(int account_id, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;

	if (!emnetwork_check_network_status(&err))  {
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);

		if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
			EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>>");
	}
	else  {

		if (!emcore_validate_account(account_id, &err)) {
			EM_DEBUG_EXCEPTION("emcore_validate_account failed account id  :  %d  err :  %d", account_id, err);

			if (err == EMAIL_ERROR_CANCELLED) {
				EM_DEBUG_EXCEPTION("notify  :  NOTI_VALIDATE_ACCOUNT_CANCEL ");
				if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, account_id, NULL,  handle_to_be_published, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
			}
			else {
				if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FAIL, account_id, NULL,  handle_to_be_published, err))
					EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FAIL] Failed >>>> ");
			}
		}
		else {
			email_account_t *account_ref = NULL;
			account_ref = emcore_get_account_reference(account_id);

			if (account_ref) {
				EM_DEBUG_LOG("account_ref->incoming_server_type[%d]", account_ref->incoming_server_type);
				if ( EMAIL_SERVER_TYPE_IMAP4 == account_ref->incoming_server_type ) {
					if (!emcore_check_thread_status())
						err = EMAIL_ERROR_CANCELLED;
					else if (!emcore_sync_mailbox_list(account_id, "", handle_to_be_published, &err))
						EM_DEBUG_EXCEPTION("\t emcore_get_mailbox_list_to_be_sync falied - %d", err);
				}

				if (err > 0) {
					EM_DEBUG_EXCEPTION("emcore_validate_account succeeded account id  :  %d  err :  %d", account_id, err);
					if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_FINISH, account_id, NULL,  handle_to_be_published, err))
						EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_FINISH] Success >>>>");
				}

				emcore_free_account(account_ref);
				EM_SAFE_FREE(account_ref);
			}
		}
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

int event_handler_EMAIL_EVENT_UPDATE_MAIL(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas, int handle_to_be_published)
{
	EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], input_meeting_request[%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
	int err = EMAIL_ERROR_NONE;

	if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE)
		EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);

	if(input_mail_data)
		emcore_free_mail_data_list(&input_mail_data, 1);

	if(input_attachment_data_list)
		emcore_free_attachment_data(&input_attachment_data_list, input_attachment_count, NULL);

	if(input_meeting_request)
		emstorage_free_meeting_request(input_meeting_request);

	EM_DEBUG_FUNC_END("err [%d", err);
	return err;
}
int event_handler_EMAIL_EVENT_SAVE_MAIL(int input_account_id, int input_mail_id, int input_handle_to_be_published)
{
	EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mail_id [%d] input_handle_to_be_published [%d]", input_account_id, input_mail_id, input_handle_to_be_published);
	int err = EMAIL_ERROR_NONE;

	err = emcore_sync_mail_from_client_to_server(input_mail_id);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

int event_handler_EMAIL_EVENT_MOVE_MAIL(int account_id, int *mail_ids, int mail_id_count, int dest_mailbox_id, int src_mailbox_id, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE, ret = false;
	email_account_t *account_ref = NULL;

	if (!(account_ref = emcore_get_account_reference(account_id))) {
		EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
		err = EMAIL_ERROR_INVALID_ACCOUNT;
		goto FINISH_OFF;
	}

	/* Move mail local */
	/*
	if (!emcore_mail_move(mail_ids, mail_id_count, dest_mailbox.mailbox_name, EMAIL_MOVED_BY_COMMAND, 0, &err)) {
		EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
		goto FINISH_OFF;
	}
	*/

	if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
		/* Move mail on server */
		if (!emnetwork_check_network_status(&err))
			EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
		else {
#ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
			if (!emcore_move_mail_on_server_ex(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
				EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex failed - %d", err);
#else
			if (!emcore_move_mail_on_server(account_id , src_mailbox_id, mail_ids, mail_id_count, dest_mailbox_id, &err))
				EM_DEBUG_EXCEPTION("\t emcore_move_mail_on_server failed - %d", err);
#endif
		}
	}

	ret = true;
FINISH_OFF:
	EM_SAFE_FREE(mail_ids); /*prevent 33693*/

	if (account_ref) {
		emcore_free_account(account_ref);
		EM_SAFE_FREE(account_ref);
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return ret;
}

int event_handler_EMAIL_EVENT_DELETE_MAILBOX(int mailbox_id, int on_server, int recursive, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN("mailbox_id[%d] on_server[%d] recursive[%d] handle_to_be_published[%d] error[%p]",  mailbox_id, on_server, recursive, handle_to_be_published, error);
	int err = EMAIL_ERROR_NONE;

	if (!emnetwork_check_network_status(&err))
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
	else  {
		if (( err = emcore_delete_mailbox(mailbox_id, on_server, recursive)) != EMAIL_ERROR_NONE)
			EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

int event_handler_EMAIL_EVENT_CREATE_MAILBOX(int account_id, char *mailbox_name, char *mailbox_alias, int mailbox_type, int on_server, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	email_mailbox_t mailbox;

	memset(&mailbox, 0x00, sizeof(mailbox));

	mailbox.account_id = account_id;
	mailbox.mailbox_name = mailbox_name;
	mailbox.alias = mailbox_alias;
	mailbox.mailbox_type = mailbox_type;

	if (!emnetwork_check_network_status(&err))  {
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
	}
	else  {
		if (!emcore_create_mailbox(&mailbox, on_server, &err))
			EM_DEBUG_EXCEPTION("emcore_create failed - %d", err);
	}


	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

int event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER(int mail_id, int *error)
{
	EM_DEBUG_FUNC_BEGIN("mail_id [%d], error [%p]", mail_id, error);

	int err = EMAIL_ERROR_NONE;

	if (!emnetwork_check_network_status(&err))
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
	else {
		if (!emcore_sync_flag_with_server(mail_id, &err))
			EM_DEBUG_EXCEPTION("emcore_sync_flag_with_server failed [%d]", err);
#ifdef __FEATURE_LOCAL_ACTIVITY__
		else {
			emstorage_activity_tbl_t new_activity;
			memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
			new_activity.activity_type = ACTIVITY_MODIFYFLAG;
			new_activity.account_id    = event_data.account_id;
			new_activity.mail_id	   = event_data.event_param_data_4;
			new_activity.dest_mbox	   = NULL;
			new_activity.server_mailid = NULL;
			new_activity.src_mbox	   = NULL;

			if (!emcore_delete_activity(&new_activity, &err))
				EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_MODIFYFLAG] [%d] ", err);
		}
#endif /*  __FEATURE_LOCAL_ACTIVITY__ */
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

int event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(int input_account_id, int input_mailbox_id, int input_from_server, int *error)
{
	EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id [%d], input_from_server [%d], error [%p]", input_account_id, input_mailbox_id, input_from_server, error);
	int err = EMAIL_ERROR_NONE;

	if (!emcore_delete_all_mails_of_mailbox(input_account_id, input_mailbox_id, input_from_server, &err))
		EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END("err [%d]", err);
	return true;
}

int event_handler_EMAIL_EVENT_DELETE_MAIL(int account_id, int *mail_id_list, int mail_id_count, int *error)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	int ret = false;
	email_account_t *account_ref = NULL;

	if (!(account_ref = emcore_get_account_reference(account_id))) {
		EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
		err = EMAIL_ERROR_INVALID_ACCOUNT;
		goto FINISH_OFF;
	}

	if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_FROM_SERVER, EMAIL_DELETED_BY_COMMAND, false, &err)) {
		EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
		goto FINISH_OFF;
	}

	ret = true;
FINISH_OFF:

	if (account_ref) {
		emcore_free_account(account_ref);
		EM_SAFE_FREE(account_ref);
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return ret;
}

int event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(int account_id, char *maibox_name, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;

	if (!emnetwork_check_network_status(&err))  {
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
		if (!emcore_notify_network_event(NOTI_DOWNLOAD_FAIL, account_id, maibox_name,  0, err))
			EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
	}
	else  {
		EM_DEBUG_LOG("Sync of all mailbox");
		if (!emcore_sync_mailbox_list(account_id, "", handle_to_be_published, &err))
			EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
	}

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

int event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(int account_id, int mailbox_id, char *criteria, int handle_to_be_published, int *error)
{
	EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_id : [%d], criteria : [%s]", account_id, mailbox_id, criteria);

	int err = EMAIL_ERROR_NONE;
	int i = 0;
	int mail_id = 0;
	int thread_id = 0;
	char temp_uid_string[20] = {0,};

	emcore_uid_list uid_elem;
	emstorage_mailbox_tbl_t *search_mailbox = NULL;
	emstorage_mail_tbl_t *new_mail_tbl_data = NULL;

	MAILSTREAM *stream = NULL;
	MESSAGECACHE *mail_cache_element = NULL;
	ENVELOPE *env = NULL;
	emstorage_mailbox_tbl_t* local_mailbox = NULL;
	char mailbox_id_param_string[10] = {0,};

	if (account_id < 0 || mailbox_id == 0) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	if ( (err = emstorage_get_mailbox_by_id(mailbox_id, &local_mailbox)) != EMAIL_ERROR_NONE || !local_mailbox) {
		EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
		goto FINISH_OFF;
	}

	SNPRINTF(mailbox_id_param_string, 10, "%d", local_mailbox->mailbox_id);

	if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_START, account_id, mailbox_id_param_string, handle_to_be_published, 0))
		EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_START] failed >>>>");

	if (!emnetwork_check_network_status(&err)) {
		EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
		if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string,  0, err))
			EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed");
		goto FINISH_OFF;
	}

	if (!emcore_connect_to_remote_mailbox(account_id, mailbox_id, (void **)&stream, &err)) {
		EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed");
		if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
			EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
		goto FINISH_OFF;
	}

	if (!mail_search_full(stream, NIL, mail_criteria(criteria), SE_FREE)) {
		EM_DEBUG_EXCEPTION("mail_search failed");
		if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
			EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
		goto FINISH_OFF;
	}

	for (i = 1; i <= stream->nmsgs; ++i) {
		mail_cache_element = mail_elt(stream, i);
		if (mail_cache_element->searched) {
			env = mail_fetchstructure_full(stream, i, NULL, FT_PEEK);

			memset(&uid_elem, 0x00, sizeof(uid_elem));

			uid_elem.msgno = mail_cache_element->msgno;
			SNPRINTF(temp_uid_string, 20, "%4lu", mail_cache_element->private.uid);
			uid_elem.uid = temp_uid_string;
			uid_elem.flag.seen = mail_cache_element->seen;

			if (!emcore_make_mail_tbl_data_from_envelope(stream, env, &uid_elem, &new_mail_tbl_data, &err) || !new_mail_tbl_data) {
				EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
				if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
					EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
				goto FINISH_OFF;
			}

			search_mailbox = em_malloc(sizeof(emstorage_mailbox_tbl_t));
			if (search_mailbox == NULL) {
				EM_DEBUG_EXCEPTION("em_malloc failed");
				err = EMAIL_ERROR_OUT_OF_MEMORY;
				if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
					EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
				goto FINISH_OFF;
			}

			search_mailbox->account_id = account_id;
			search_mailbox->mailbox_id = mailbox_id;
			search_mailbox->mailbox_name = EM_SAFE_STRDUP(EMAIL_SEARCH_RESULT_MAILBOX_NAME);
			search_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;

			if ((err = emcore_add_mail_to_mailbox(search_mailbox, new_mail_tbl_data, &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
				EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox failed [%d]", err);
				if (!emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FAIL, account_id, mailbox_id_param_string, handle_to_be_published, err))
					EM_DEBUG_EXCEPTION("emcore_notify_network_event [NOTI_SEARCH_ON_SERVER_FAIL] Failed >>>>");
				goto FINISH_OFF;
			}
			memset(mailbox_id_param_string, 0, 10);
			SNPRINTF(mailbox_id_param_string, 10, "%d", search_mailbox->mailbox_id);
			if (!emcore_notify_storage_event(NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_param_string, thread_id)) {
				EM_DEBUG_EXCEPTION("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
			}

			if (new_mail_tbl_data) {
				emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
				new_mail_tbl_data = NULL;
			}
		}
	}

	if (err == EMAIL_ERROR_NONE && !emcore_notify_network_event(NOTI_SEARCH_ON_SERVER_FINISH, account_id, NULL, handle_to_be_published, 0))
		EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_SEARCH_ON_SERVER_FINISH] Failed >>>>>");

FINISH_OFF:

	if (search_mailbox != NULL)
		emstorage_free_mailbox(&search_mailbox, 1, NULL);

	if (new_mail_tbl_data)
		emstorage_free_mail(&new_mail_tbl_data, 1, NULL);

	if (local_mailbox)
		emstorage_free_mailbox(&local_mailbox, 1, NULL);

	if (error)
		*error = err;

	EM_DEBUG_FUNC_END();
	return true;
}

static int event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, char *input_new_mailbox_alias, int handle_to_be_published)
{
	EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path %s], input_new_mailbox_path [%s], input_new_mailbox_alias [%s], handle_to_be_published [%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, input_new_mailbox_alias, handle_to_be_published);
	int err = EMAIL_ERROR_NONE;


	if (err == EMAIL_ERROR_NONE) {
		if ((err = emcore_rename_mailbox(input_mailbox_id, input_new_mailbox_path, input_new_mailbox_alias, true, true, handle_to_be_published)) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("emcore_rename_mailbox failed [%d]", err);
		}
	}

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

void* thread_func_branch_command(void *arg)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;
	int is_storage_full = false;
	int noti_id = 0;
	email_event_t event_data;
	email_session_t *session = NULL;
	emstorage_account_tbl_t *account_tbl = NULL;
	int handle_to_be_published = 0;

	if (!emstorage_open(&err))  {
		EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
		return false;
	}

	/* check that event_data loop is continuous */
	while (emcore_event_loop_continue())  {
		if (!emcore_get_empty_session(&session))
			EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");

		/* get a event_data from event_data queue */
		if (!emcore_retrieve_event(&event_data, NULL))  {	/*  no event_data pending */
			EM_DEBUG_LOG("For handle g_event_que[g_event_que_idx].type [%d], g_event_que_idx [%d]", g_event_que[g_event_que_idx].type, g_event_que_idx);
#ifdef ENABLE_IMAP_IDLE_THREAD
			if ( !emnetwork_check_network_status(&err))  {
				EM_DEBUG_LOG(">>>> Data Networking ON ");
				if (g_client_run) {
					if (g_imap_idle_thread_alive) {
						/* emcore_kill_imap_idle_thread(NULL); */
						/* emcore_create_imap_idle_thread(NULL); */
					}
					else {
						if (!send_thread_run)
							emcore_create_imap_idle_thread(event_data.account_id, NULL);
					}
				}
			}
#endif /*  ENABLE_IMAP_IDLE_THREAD */
#ifdef __FEATURE_LOCAL_ACTIVITY__
			/*  Local activity sync */
			if (g_client_run && g_local_activity_run) {
				emstorage_account_tbl_t *account_list = NULL;
				int count = 0, i;
				if (!emstorage_get_account_list(&count, &account_list, true, true, &err))
					EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
				else {
					for (i = 0; i < count; i++) {
						if (emcore_local_activity_sync(account_list[i].account_id, &err)) {
							EM_DEBUG_LOG("Found local activity...!");
							EM_DEBUG_LOG("Resetting g_local_activity_run ");
							g_local_activity_run = 0;
							emcore_clear_session(session);
						}
					}

					emstorage_free_account(&account_list, count, &err);

					if (!g_local_activity_run)
						continue;
				}
			}
#endif

			recv_thread_run = 0;

			ENTER_CRITICAL_SECTION(_event_available_lock);
			SLEEP_CONDITION_VARIABLE(_event_available_signal, _event_available_lock);
			EM_DEBUG_LOG("Wake up by _event_available_signal");
			LEAVE_CRITICAL_SECTION(_event_available_lock);
		}
		else  {
			EM_DEBUG_LOG(">>>>>>>>>>>>>>> Got event_data !!! <<<<<<<<<<<<<<<");
			EM_DEBUG_LOG("For handle g_event_que_idx - %d", g_event_que_idx);

			if (g_event_que_idx == 1)
				handle_to_be_published = 31;
			else
				handle_to_be_published = g_event_que_idx - 1 ;

			EM_DEBUG_LOG("Handle to be Published  [%d]", handle_to_be_published);
			recv_thread_run = 1;
			g_client_run = 1;

			/*  Handling storage full */
			is_storage_full = false;
			if (event_data.type == EMAIL_EVENT_SYNC_HEADER || event_data.type == EMAIL_EVENT_SYNC_HEADER_OMA ||
				event_data.type == EMAIL_EVENT_DOWNLOAD_BODY || event_data.type == EMAIL_EVENT_DOWNLOAD_ATTACHMENT) {
				if (emcore_is_storage_full(&err) == true) {
					EM_DEBUG_EXCEPTION("Storage is full");
					switch (event_data.type) {
						case EMAIL_EVENT_SYNC_HEADER:
						case EMAIL_EVENT_SYNC_HEADER_OMA:
							noti_id = NOTI_DOWNLOAD_FAIL;
							break;
						case EMAIL_EVENT_DOWNLOAD_BODY:
							noti_id = NOTI_DOWNLOAD_BODY_FAIL;
							break;
						case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
							noti_id = NOTI_DOWNLOAD_ATTACH_FAIL;
							break;
						default:
							break;
					}

					if (!emcore_notify_network_event(noti_id, event_data.account_id, NULL,  handle_to_be_published, err))
						EM_DEBUG_EXCEPTION(" emcore_notify_network_event [NOTI_DOWNLOAD_FAIL] Failed >>>> ");
					is_storage_full = true;
				}
			}

			emdevice_set_dimming_on_off(false, NULL);

			if (event_data.account_id > 0) {
				if (!emstorage_get_account_by_id(event_data.account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
					EM_DEBUG_EXCEPTION("emstorage_get_account_by_id [%d]", err);
				}
			}

			if (account_tbl)
				EM_DEBUG_LOG("account_id : [%d], sync_disabled : [%d]", event_data.account_id, account_tbl->sync_disabled);

			if (!account_tbl || account_tbl->sync_disabled == 0) {
				switch (event_data.type)  {
					case EMAIL_EVENT_SYNC_IMAP_MAILBOX:  /*  get imap mailbox list  */
						if (!emcore_sync_mailbox_list(event_data.account_id, event_data.event_param_data_3, handle_to_be_published, &err))
							EM_DEBUG_EXCEPTION("emcore_sync_mailbox_list failed [%d]", err);
						EM_SAFE_FREE(event_data.event_param_data_3);
						break;

					case EMAIL_EVENT_SYNC_HEADER:  /*  synchronize mail header  */
						if (is_storage_full == false)
							event_handler_EMAIL_EVENT_SYNC_HEADER(event_data.account_id, event_data.event_param_data_5, handle_to_be_published,  &err);
						break;

					case EMAIL_EVENT_SYNC_HEADER_OMA:  /*  synchronize mail header for OMA */
						if (is_storage_full == false)
							event_hanlder_EMAIL_EVENT_SYNC_HEADER_OMA(event_data.account_id, event_data.event_param_data_1, handle_to_be_published, &err);
						EM_SAFE_FREE(event_data.event_param_data_1);
						break;

					case EMAIL_EVENT_DOWNLOAD_BODY:  /*  download mail body  */
						if (is_storage_full == false)
							event_handler_EMAIL_EVENT_DOWNLOAD_BODY(event_data.account_id, (int)event_data.event_param_data_4, (int)event_data.event_param_data_3, handle_to_be_published, &err);
						event_data.event_param_data_3 = NULL;		/*  MUST BE */
						break;

					case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:  /*  download attachment */
						if (is_storage_full == false)
							event_handler_EMAIL_EVENT_DOWNLOAD_ATTACHMENT(event_data.account_id, (int)event_data.event_param_data_4, event_data.event_param_data_5, handle_to_be_published, &err);
						break;

					case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:  /*  Sync flags field */
						event_handler_EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER((int*)event_data.event_param_data_3, event_data.event_param_data_4 , event_data.event_param_data_5, event_data.event_param_data_6, &err);
						EM_SAFE_FREE(event_data.event_param_data_3);
						break;

					case EMAIL_EVENT_DELETE_MAIL:  /*  delete mails */
						event_handler_EMAIL_EVENT_DELETE_MAIL(event_data.account_id, (int*)event_data.event_param_data_3, event_data.event_param_data_4, &err);
						EM_SAFE_FREE(event_data.event_param_data_3);
						break;

					case EMAIL_EVENT_DELETE_MAIL_ALL:  /*  delete all mails */
						event_handler_EMAIL_EVENT_DELETE_MAIL_ALL(event_data.account_id, event_data.event_param_data_4, (int)event_data.event_param_data_5, &err);
						break;
#ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
					case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
						event_handler_EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER((int)event_data.event_param_data_4, &err);
						break;
#endif

					case EMAIL_EVENT_CREATE_MAILBOX:
						event_handler_EMAIL_EVENT_CREATE_MAILBOX(event_data.account_id, event_data.event_param_data_1, event_data.event_param_data_2, GPOINTER_TO_INT(event_data.event_param_data_3), event_data.event_param_data_4, handle_to_be_published, &err);
						EM_SAFE_FREE(event_data.event_param_data_1);
						EM_SAFE_FREE(event_data.event_param_data_2);
						break;

					case EMAIL_EVENT_DELETE_MAILBOX:
						event_handler_EMAIL_EVENT_DELETE_MAILBOX(event_data.event_param_data_4, event_data.event_param_data_5, event_data.event_param_data_6,handle_to_be_published, &err);
						break;

					case EMAIL_EVENT_SAVE_MAIL:
						err = event_handler_EMAIL_EVENT_SAVE_MAIL(event_data.account_id, event_data.event_param_data_4, handle_to_be_published);
						break;

					case EMAIL_EVENT_MOVE_MAIL:
						event_handler_EMAIL_EVENT_MOVE_MAIL(event_data.account_id, (int  *)event_data.event_param_data_3, event_data.event_param_data_4, event_data.event_param_data_5, event_data.event_param_data_8, handle_to_be_published, &err);
						event_data.event_param_data_3 = NULL; /*prevent 33693*/
						break;

					case EMAIL_EVENT_VALIDATE_ACCOUNT:
						event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT(event_data.account_id, handle_to_be_published, &err);
						break;

					case EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT: {
							email_account_t *account = (email_account_t *)event_data.event_param_data_1;
							event_handler_EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT(account, handle_to_be_published, &err);
							if(account) {
								emcore_free_account(account);
								EM_SAFE_FREE(account);
							}
						}
						break;

					case EMAIL_EVENT_VALIDATE_ACCOUNT_EX: {
							email_account_t *account = (email_account_t *)event_data.event_param_data_1;
							err = event_handler_EMAIL_EVENT_VALIDATE_ACCOUNT_EX(account, handle_to_be_published);
							if(account) {
								emcore_free_account(account);
								EM_SAFE_FREE(account);
							}
						}
						break;

					case EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT: {
							email_account_t *account = (email_account_t *)event_data.event_param_data_1;
							event_handler_EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT(event_data.account_id, account, handle_to_be_published, &err);
							emcore_free_account(account);
							EM_SAFE_FREE(account);
						}
						break;

					case EMAIL_EVENT_UPDATE_MAIL:
						event_handler_EMAIL_EVENT_UPDATE_MAIL((email_mail_data_t*)event_data.event_param_data_1, (email_attachment_data_t*)event_data.event_param_data_2, event_data.event_param_data_4, (email_meeting_request_t*)event_data.event_param_data_3, event_data.event_param_data_5, handle_to_be_published);

						event_data.event_param_data_1 = NULL;
						event_data.event_param_data_2 = NULL;
						event_data.event_param_data_3 = NULL;
						break;

					case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
						event_handler_EMAIL_EVENT_SET_MAIL_SLOT_SIZE(event_data.account_id, event_data.event_param_data_4, event_data.event_param_data_5, handle_to_be_published, &err);
						break;

					case EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED:
						err = event_handler_EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED(event_data.account_id, event_data.event_param_data_4);
						break;

	#ifdef __FEATURE_LOCAL_ACTIVITY__
					case EMAIL_EVENT_LOCAL_ACTIVITY:
						event_handler_EMAIL_EVENT_LOCAL_ACTIVITY(event_data.account_id, &err);
						break;
	#endif /* __FEATURE_LOCAL_ACTIVITY__*/

					case EMAIL_EVENT_SEARCH_ON_SERVER:
						event_handler_EMAIL_EVENT_SEARCH_ON_SERVER(event_data.account_id, event_data.event_param_data_4, (char *)event_data.event_param_data_1, handle_to_be_published, &err);
						EM_SAFE_FREE(event_data.event_param_data_1);
						break;

					case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
						err = event_handler_EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER(event_data.account_id, event_data.event_param_data_4, (char*)event_data.event_param_data_1, (char*)event_data.event_param_data_2, (char*)event_data.event_param_data_3, handle_to_be_published);
						EM_SAFE_FREE(event_data.event_param_data_1);
						EM_SAFE_FREE(event_data.event_param_data_2);
						EM_SAFE_FREE(event_data.event_param_data_3);
						break;

					default:
						break;
				}
			}

			if (account_tbl) {
				emstorage_free_account(&account_tbl, 1, NULL);
				account_tbl = NULL;
			}

			if (!emcore_notify_response_to_api(event_data.type, handle_to_be_published, err))
				EM_DEBUG_EXCEPTION("emcore_notify_response_to_api failed");

			emdevice_set_dimming_on_off(true, NULL);
			em_flush_memory();

			switch (event_data.type)  {
				case EMAIL_EVENT_SEND_MAIL:
				case EMAIL_EVENT_SEND_MAIL_SAVED:
					_sending_busy_unref();
					break;

				case EMAIL_EVENT_SYNC_HEADER:
				case EMAIL_EVENT_SYNC_HEADER_OMA:
				case EMAIL_EVENT_DOWNLOAD_BODY:
				case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
				case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
				case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
				case EMAIL_EVENT_DELETE_MAIL:
				case EMAIL_EVENT_DELETE_MAIL_ALL:
				case EMAIL_EVENT_VALIDATE_ACCOUNT:
				case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
				case EMAIL_EVENT_SAVE_MAIL:
				case EMAIL_EVENT_MOVE_MAIL:
				case EMAIL_EVENT_CREATE_MAILBOX:
				case EMAIL_EVENT_DELETE_MAILBOX:
				case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
				case EMAIL_EVENT_SEARCH_ON_SERVER:
				case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
					_receiving_busy_unref();
					break;

				default:
					break;
			}

			event_data.type = 0;

			ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
			memset(g_event_que+g_active_que, 0x00, sizeof(email_event_t));
			LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
		}

		emcore_clear_session(session);
	}

	if (!emstorage_close(&err))
		EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);

	EM_DEBUG_FUNC_END();
	return SUCCESS;
}
/*Send event_data loop*/
INTERNAL_FUNC int emcore_start_event_loop_for_sending_mails(int *err_code)
{
	EM_DEBUG_FUNC_BEGIN();
	int thread_error = -1;

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;

	memset(&g_send_event_que, 0x00, sizeof(g_send_event_que));

	if (g_send_srv_thread)  {
		EM_DEBUG_EXCEPTION("\t send service thread is already running...");
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_UNKNOWN;
		return true;
	}

	g_send_event_que_idx = 1;
	g_send_event_loop = 1;
	g_send_active_que = 0;

	/* initialize lock */
	/*  INITIALIZE_CRITICAL_SECTION(_send_event_available_lock); */
	INITIALIZE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
	INITIALIZE_CONDITION_VARIABLE(_send_event_available_signal);

	/* create thread */
	THREAD_CREATE_JOINABLE(g_send_srv_thread, thread_func_branch_command_for_sending_mails, thread_error);

	if (thread_error != 0) {
		EM_DEBUG_EXCEPTION("cannot make thread...");
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_UNKNOWN;
		return FAILURE;
	}

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;
	EM_DEBUG_FUNC_END();
	return SUCCESS;
}

/* finish api event_data loop */
INTERNAL_FUNC int emcore_send_event_loop_stop(int *err_code)
{
    EM_DEBUG_FUNC_BEGIN();

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;

	if (!g_send_srv_thread) 	 {
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_UNKNOWN;
		return false;
	}

    /* stop event_data loop */
    g_send_event_loop = 0;

	emcore_cancel_send_mail_thread(g_send_active_que, NULL, err_code);
	ENTER_CRITICAL_SECTION(_send_event_available_lock);
	WAKE_CONDITION_VARIABLE(_send_event_available_signal);		/*  MUST BE HERE */
	LEAVE_CRITICAL_SECTION(_send_event_available_lock);

	/* wait for thread finished */
	THREAD_JOIN(g_send_srv_thread);

	g_send_srv_thread = 0;

	DELETE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);
	DELETE_CRITICAL_SECTION(_send_event_available_lock);
	DELETE_CONDITION_VARIABLE(_send_event_available_signal);

	g_send_event_que_idx = 1;
	g_send_active_que = 0;

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;

    return true;
}

/* start api event_data loop */
INTERNAL_FUNC int emcore_start_event_loop(int *err_code)
{
    EM_DEBUG_FUNC_BEGIN();
	int thread_error;

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;

    memset(&g_event_que, 0x00, sizeof(g_event_que));

	if (g_srv_thread) {
		EM_DEBUG_EXCEPTION("service thread is already running...");
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_UNKNOWN;
		return true;
	}

	g_event_que_idx = 1;
	g_event_loop = 1;
	g_active_que = 0;

    /* initialize lock */
	INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
	INITIALIZE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);

	emcore_initialize_event_callback_table();

    /* create thread */
	THREAD_CREATE(g_srv_thread, thread_func_branch_command, NULL, thread_error);

	if (thread_error != 0) {
        EM_DEBUG_EXCEPTION("cannot create thread");
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_SYSTEM_FAILURE;
        return FAILURE;
    }

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;

    return false;
}

/* finish api event_data loop */
INTERNAL_FUNC int emcore_stop_event_loop(int *err_code)
{
    EM_DEBUG_FUNC_BEGIN();

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;

	if (!g_srv_thread)  {
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_UNKNOWN;
		return false;
	}

    /* stop event_data loop */
    g_event_loop = 0;

	/* 	pthread_kill(g_srv_thread, SIGINT); */
	emcore_cancel_thread(g_active_que, NULL, err_code);

	ENTER_CRITICAL_SECTION(_event_available_lock);
	WAKE_CONDITION_VARIABLE(_event_available_signal);
	LEAVE_CRITICAL_SECTION(_event_available_lock);

	/* wait for thread finished */
	THREAD_JOIN(g_srv_thread);

	g_srv_thread = 0;

	DELETE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);
	DELETE_CRITICAL_SECTION(_event_available_lock);
	DELETE_CONDITION_VARIABLE(_event_available_signal);
	DELETE_RECURSIVE_CRITICAL_SECTION(_event_callback_table_lock);

	g_event_que_idx = 1;
	g_active_que = 0;

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;
	EM_DEBUG_FUNC_END();
    return true;
}


int emcore_get_active_queue_idx()
{
	return g_send_active_que;
}

/* check thread status
* 0 : stop job 1 : continue job
*/
INTERNAL_FUNC int emcore_check_thread_status()
{
	if (g_active_que <= 0)
		return true;

	return (g_event_que[g_active_que].status == EMAIL_EVENT_STATUS_STARTED);
}

/* cancel a job  */
INTERNAL_FUNC int emcore_cancel_thread(int handle, void *arg, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);

	int ret = false;
	int err = EMAIL_ERROR_NONE;

	if (handle <= 0 || handle > (EVENT_QUEUE_MAX - 1))  {
		EM_DEBUG_EXCEPTION("handle[%d], arg[%p]", handle, arg);
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	ENTER_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);

	EM_DEBUG_LOG("status[%d], type[%d], handle[%d]", g_event_que[handle].status, g_event_que[handle].type, handle);

	if (g_event_que[handle].status == EMAIL_EVENT_STATUS_WAIT)  {
		fail_status_notify(&g_event_que[handle], EMAIL_ERROR_CANCELLED);

		switch (g_event_que[handle].type)  {
			case EMAIL_EVENT_SEND_MAIL:
			case EMAIL_EVENT_SEND_MAIL_SAVED:
				EM_DEBUG_LOG("EMAIL_EVENT_SEND_MAIL or EMAIL_EVENT_SEND_MAIL_SAVED");
				_sending_busy_unref();
				if (!emcore_notify_network_event(NOTI_SEND_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
				break;
			case EMAIL_EVENT_DOWNLOAD_BODY:
				EM_DEBUG_LOG("EMAIL_EVENT_DOWNLOAD_BODY");
				_receiving_busy_unref();
				if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
				break;

			case EMAIL_EVENT_SYNC_HEADER:
			case EMAIL_EVENT_SYNC_HEADER_OMA:
			case EMAIL_EVENT_DOWNLOAD_ATTACHMENT:
			case EMAIL_EVENT_SYNC_MAIL_FLAG_TO_SERVER:
			case EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER:
				EM_DEBUG_LOG("EMAIL_EVENT_SYNC_HEADER, EMAIL_EVENT_DOWNLOAD_ATTACHMENT");
				_receiving_busy_unref();
				break;

			case EMAIL_EVENT_VALIDATE_ACCOUNT:
				EM_DEBUG_LOG(" validate account waiting  :  cancel acc id  :  %d", g_event_que[handle].account_id);
				_receiving_busy_unref();
				if (!emcore_notify_network_event(NOTI_VALIDATE_ACCOUNT_CANCEL, g_event_que[handle].account_id, NULL , g_event_que[handle].event_param_data_4, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_VALIDATE_ACCOUNT_CANCEL] Failed >>>> ");
				break;

			case EMAIL_EVENT_DELETE_MAIL:
			case EMAIL_EVENT_DELETE_MAIL_ALL:
			case EMAIL_EVENT_SYNC_IMAP_MAILBOX:
			case EMAIL_EVENT_SAVE_MAIL:
			case EMAIL_EVENT_MOVE_MAIL:
			case EMAIL_EVENT_CREATE_MAILBOX:
			case EMAIL_EVENT_DELETE_MAILBOX:
			case EMAIL_EVENT_SET_MAIL_SLOT_SIZE:
			case EMAIL_EVENT_SEARCH_ON_SERVER:
			case EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER:
				EM_DEBUG_LOG("EMAIL_EVENT_DELETE_MAIL, EMAIL_EVENT_SYNC_IMAP_MAILBOX");
				_receiving_busy_unref();
				break;
 			default:
				break;
		}
	}

	memset(g_event_que+handle, 0x00, sizeof(email_event_t));
	g_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;

	LEAVE_RECURSIVE_CRITICAL_SECTION(_event_queue_lock);

	ret = true;

FINISH_OFF:
	if (err_code != NULL)
		*err_code = err;
	EM_DEBUG_FUNC_END();
	return ret;
}

/* check thread status
* 0 : stop job 1 : continue job
*/
int emcore_check_send_mail_thread_status()
{
	EM_DEBUG_FUNC_BEGIN();

	if (g_send_active_que <= 0)
		return true;
	EM_DEBUG_LOG("g_send_event_que[g_send_active_que[%d]].status[%d]", g_send_active_que, g_send_event_que[g_send_active_que].status);
	EM_DEBUG_FUNC_END();
	return (g_send_event_que[g_send_active_que].status == EMAIL_EVENT_STATUS_STARTED);
}

INTERNAL_FUNC int emcore_cancel_all_threads_of_an_account(int account_id)
{
	EM_DEBUG_FUNC_BEGIN();
	int error_code = EMAIL_ERROR_NONE;
	int i, event_count = EVENT_QUEUE_MAX, exit_flag = 0, sleep_count = 0;

	for (i = 0 ; i < event_count; i++) {
		if (g_event_que[i].type && g_event_que[i].status != EMAIL_EVENT_STATUS_UNUSED) {
			EM_DEBUG_LOG("There is a live thread. %d", i);
			if (g_event_que[i].account_id == account_id || g_event_que[i].account_id == ALL_ACCOUNT) {
				EM_DEBUG_LOG("And it is for account %d", g_event_que[i].account_id);
				emcore_cancel_thread(i, NULL, &error_code);
			}
		}
	}

	while (exit_flag == 0 && sleep_count < 30) {
		EM_DEBUG_LOG("Sleeping...");
		usleep(100000);
		EM_DEBUG_LOG("Wake up!");
		sleep_count++;
		exit_flag = 1;
		for (i = 0 ; i < event_count; i++) {
			if (g_event_que[i].type && g_event_que[i].status != EMAIL_EVENT_STATUS_UNUSED) {
				EM_DEBUG_LOG("There is still a live thread. %d", i);
				if (g_event_que[i].account_id == account_id || g_event_que[i].account_id == ALL_ACCOUNT) {
					EM_DEBUG_LOG("And it is for account %d. So, I should sleep for a while.", g_event_que[i].account_id);
					exit_flag = 0;
				}
			}
		}
	}

	EM_DEBUG_LOG("Sleep count %d", sleep_count);

	if (sleep_count >= 30)
		error_code = EMAIL_ERROR_CANNOT_STOP_THREAD;
	else
		error_code = EMAIL_ERROR_NONE;
	EM_DEBUG_FUNC_END();
	return error_code;
}


/* cancel send mail job  */
INTERNAL_FUNC int emcore_cancel_send_mail_thread(int handle, void *arg, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN("handle[%d], arg[%p], err_code[%p]", handle, arg, err_code);

	int ret = false;
	int err = EMAIL_ERROR_NONE;

	if (handle <= 0 || handle > (EVENT_QUEUE_MAX - 1))  {
		EM_DEBUG_EXCEPTION("handle[%d], arg[%p]", handle, arg);
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	ENTER_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);

	EM_DEBUG_LOG("event_data.status[%d], handle[%d]", g_send_event_que[handle].status, handle);

	if (g_send_event_que[handle].status == EMAIL_EVENT_STATUS_WAIT)  {
		fail_status_notify(&g_send_event_que[handle], EMAIL_ERROR_CANCELLED);

		switch (g_send_event_que[handle].type)  {
			case EMAIL_EVENT_SEND_MAIL:
			case EMAIL_EVENT_SEND_MAIL_SAVED:
				_sending_busy_unref();
				g_send_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;
				if (!emcore_notify_network_event(NOTI_SEND_CANCEL, g_send_event_que[handle].account_id, NULL , g_send_event_que[handle].event_param_data_4, err))
					EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_CANCEL] Failed >>>> ");
				break;
			default:
				break;
		}
	}

	EM_DEBUG_LOG("send_mail_cancel");
	memset(g_send_event_que+handle, 0x00, sizeof(email_event_t));
	g_send_event_que[handle].status = EMAIL_EVENT_STATUS_CANCELED;

	EM_DEBUG_LOG("event_data.status[%d], handle[%d]", g_send_event_que[handle].status, handle);


	LEAVE_RECURSIVE_CRITICAL_SECTION(_send_event_queue_lock);

	ret = true;

FINISH_OFF:
	if (err_code != NULL)
		*err_code = err;
	EM_DEBUG_FUNC_END("ret [%d]", ret);
	return ret;
}

INTERNAL_FUNC int emcore_get_task_information(email_task_information_t **output_task_information, int *output_task_information_count)
{
	EM_DEBUG_FUNC_BEGIN("output_task_information[%p] output_task_information_count[%p]", output_task_information, output_task_information_count);
	int err = EMAIL_ERROR_NONE;
	int i = 0;
	int count = 0;
	int index = 0;
	email_task_information_t *task_information = NULL;

	if (output_task_information == NULL || output_task_information_count == NULL) {
		EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	for(i = 0; i < EVENT_QUEUE_MAX; i++) {
		if(g_event_que[i].type != EMAIL_EVENT_NONE && g_event_que[i].status != EMAIL_EVENT_STATUS_CANCELED)
			count++;
	}

	if(count != 0) {
		if((task_information = em_malloc(sizeof(email_task_information_t) * count)) == NULL) {
			EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
			err = EMAIL_ERROR_OUT_OF_MEMORY;
			goto FINISH_OFF;
		}

		for(i = 0; i < EVENT_QUEUE_MAX; i++) {
			if(g_event_que[i].type != EMAIL_EVENT_NONE && g_event_que[i].status != EMAIL_EVENT_STATUS_CANCELED) {
				task_information[index].handle     = i;
				task_information[index].account_id = g_event_que[i].account_id;
				task_information[index].type       = g_event_que[i].type;
				task_information[index].status     = g_event_que[i].status;
				index++;
			}
		}
	}
	else {
		err = EMAIL_ERROR_DATA_NOT_FOUND;
	}

	*output_task_information_count = count;
	*output_task_information       = task_information;

FINISH_OFF:

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

INTERNAL_FUNC int emcore_free_event(email_event_t *event_data)
{
	EM_DEBUG_FUNC_BEGIN("event_data [%p]", event_data);

	if(event_data) {
		EM_SAFE_FREE(event_data->event_param_data_1);
		EM_SAFE_FREE(event_data->event_param_data_2);
		EM_SAFE_FREE(event_data->event_param_data_3);
	}

	EM_DEBUG_FUNC_END();
	return true;
}

#ifdef __FEATURE_KEEP_CONNECTION__
INTERNAL_FUNC unsigned int emcore_get_receiving_thd_id()
{
	return (unsigned int)g_srv_thread;
}
#endif /*  __FEATURE_KEEP_CONNECTION__ */

#ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__

INTERNAL_FUNC int emcore_get_pbd_thd_state()
{
	int pbd_thd_state = false;
	ENTER_CRITICAL_SECTION(_state_variables_lock);
	pbd_thd_state = g_pbd_thd_state;
	LEAVE_CRITICAL_SECTION(_state_variables_lock);
	return pbd_thd_state;
}

static int emcore_set_pbd_thd_state(int flag)
{
	ENTER_CRITICAL_SECTION(_state_variables_lock);
	g_pbd_thd_state = flag;
	LEAVE_CRITICAL_SECTION(_state_variables_lock);

	return g_pbd_thd_state;
}

INTERNAL_FUNC unsigned int emcore_get_partial_body_thd_id()
{
	EM_DEBUG_FUNC_BEGIN();
	EM_DEBUG_FUNC_END();
	return (unsigned int)g_partial_body_thd;
}

static int emcore_clear_bulk_pbd_que(int *err_code)
{
	EM_DEBUG_FUNC_BEGIN();

	int ret = true;
	int error = EMAIL_ERROR_NONE;
	int i = 0;

	for (i = 0; i < BULK_PARTIAL_BODY_DOWNLOAD_COUNT; ++i) {
		if (g_partial_body_bulk_dwd_que[i].event_type) {
			if (false == emcore_free_partial_body_thd_event(g_partial_body_bulk_dwd_que + i, &error))					 {
				EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
				ret = false;
				break;
			}
		}
	}

	if (NULL != err_code)
		*err_code = error;
	EM_DEBUG_FUNC_END();
	return ret;
}

static void emcore_pb_thd_set_local_activity_continue(int flag)
{
	EM_DEBUG_FUNC_BEGIN("flag [%d]", flag);

	ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	g_pb_thd_local_activity_continue = flag;

	if (true == flag) {
		WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);
	}

	LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
	EM_DEBUG_FUNC_END();
}

static
int emcore_pb_thd_can_local_activity_continue()
{
	EM_DEBUG_FUNC_BEGIN();

	int ret = false;

	ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	ret = g_pb_thd_local_activity_continue;

	LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
	EM_DEBUG_FUNC_END();
	return ret;

}

INTERNAL_FUNC int emcore_clear_partial_body_thd_event_que(int *err_code)
{
	EM_DEBUG_FUNC_BEGIN();

	int ret = true;
	int error = EMAIL_ERROR_NONE;
	int i = 0;

	ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	if (true == g_partial_body_thd_queue_empty) {
		EM_DEBUG_LOG(" Partial Body Thread Event Queue Already empty ");
	}
	else {
		for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
			if (g_partial_body_thd_event_que[i].event_type) {
				if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + i, &error))					 {
					EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
					ret = false;
					break;
				}
			}
		}

		g_partial_body_thd_queue_empty = true;
		g_partial_body_thd_queue_full = false;
	}
	LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	if (NULL != err_code)
		*err_code = error;
	EM_DEBUG_FUNC_END();
	return ret;
}

INTERNAL_FUNC int emcore_is_partial_body_thd_que_empty()
{
	EM_DEBUG_FUNC_BEGIN();

	int ret = false;

	ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	ret = g_partial_body_thd_queue_empty;

	LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
	EM_DEBUG_FUNC_END("ret [%d]", ret);
	return ret;
}
INTERNAL_FUNC int emcore_is_partial_body_thd_que_full()
{
	EM_DEBUG_FUNC_BEGIN();

	int ret = false;

	ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	ret = g_partial_body_thd_queue_full;

	LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
	EM_DEBUG_FUNC_END();
	return ret;
}

/*
Himanshu[h.gahalut] :  If either src pointer or dest pointer points to a cell of global partial body thread event_data queue,
then emcore_copy_partial_body_thd_event API should only be called from a portion of code which is protected
through _partial_body_thd_event_queue_lock mutex.

No mutex is used inside this API so that we can also use it to copy partial body events which are not a part of global event_data queue

Precautions :

_partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.

*/

static int emcore_copy_partial_body_thd_event(email_event_partial_body_thd *src, email_event_partial_body_thd *dest, int *error_code)
{
	EM_DEBUG_FUNC_BEGIN();
	int error = EMAIL_ERROR_NONE;
	int ret = false;

	if (NULL == src || NULL == dest) {
		EM_DEBUG_LOG(" Invalid Parameter src [%p] dest [%p]", src, dest);
		error = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	dest->account_id = src->account_id;
	dest->mail_id = src->mail_id;
	dest->server_mail_id = src->server_mail_id;
	dest->activity_id = src->activity_id;
	dest->mailbox_id = src->mailbox_id;
	dest->mailbox_name = EM_SAFE_STRDUP(src->mailbox_name);
	dest->activity_type = src->activity_type;
	dest->event_type = src->event_type;

	EM_DEBUG_LOG("dest->account_id[%d], dest->mail_id[%d], dest->server_mail_id [%lu]", dest->account_id, dest->mail_id , dest->server_mail_id);

	ret = true;

	FINISH_OFF:

	if (NULL != error_code)
		*error_code = error;

	return ret;

}

/*
Himanshu[h.gahalut] :  If emcore_free_partial_body_thd_event_cell API is used to free a cell of partial body thread event_data queue,
it should only be called from a portion of code which is protected through _partial_body_thd_event_queue_lock mutex.

No mutex is used inside this API so that we can also use it to free partial body events which are not a part of global event_data queue

Precautions :

_partial_body_thd_event_queue_lock mutex should never be used inside this API otherwise it will be a deadlock.
Also never call any function from this API which uses _partial_body_thd_event_queue_lock mutex.

*/

INTERNAL_FUNC int emcore_free_partial_body_thd_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
{
	EM_DEBUG_FUNC_BEGIN();

	if (NULL == partial_body_thd_event) {
		*error_code = EMAIL_ERROR_INVALID_PARAM;
		return false;
	}

	email_event_partial_body_thd *pbd_event = partial_body_thd_event;

	/*Free character pointers in event_data cell */
	EM_SAFE_FREE(pbd_event->mailbox_name);
	memset(pbd_event, 0x00, sizeof(email_event_partial_body_thd));
	EM_DEBUG_FUNC_END();
	return true;
}

INTERNAL_FUNC int emcore_insert_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
{
	EM_DEBUG_FUNC_BEGIN();

	if (NULL == partial_body_thd_event)  {
		EM_DEBUG_EXCEPTION("\t partial_body_thd_event [%p] ", partial_body_thd_event);

		if (error_code != NULL) {
			*error_code = EMAIL_ERROR_INVALID_PARAM;
		}
		return false;
	}

	int ret = false;
	int error = EMAIL_ERROR_NONE;
	int empty_cell_index = -1;
	int index = 0;
	int count = 0;

	ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	/* find a cell in queue which is empty */

	for (count = 0, index =  g_partial_body_thd_next_event_idx; count < TOTAL_PARTIAL_BODY_EVENTS;) {
		if  (g_partial_body_thd_event_que[index].event_type) {
			++index;
			++count;

			if (index == TOTAL_PARTIAL_BODY_EVENTS) {
				index = 0;
			}
		}
		else {
			/*Found empty Cell*/

			empty_cell_index =	 index;
			break;
		}
	}

	if (-1 != empty_cell_index) {
		if (false == emcore_copy_partial_body_thd_event(partial_body_thd_event, g_partial_body_thd_event_que+empty_cell_index , &error)) {
			EM_DEBUG_LOG("emcore_copy_partial_body_thd_event failed [%d]", error);
		}
		else {
			g_partial_body_thd_queue_empty = false;

			if (count == (TOTAL_PARTIAL_BODY_EVENTS - 1)) {
				/*This is the last event_data inserted in queue after its insertion, queue is full */
				g_partial_body_thd_queue_full = true;

			}

			WAKE_CONDITION_VARIABLE(_partial_body_thd_cond);

			ret = true;
		}
	}
	else {
		EM_DEBUG_LOG(" partial body thread event_data queue is full ");
		error = EMAIL_ERROR_EVENT_QUEUE_FULL;

		g_partial_body_thd_queue_full = true;
		g_partial_body_thd_queue_empty = false;

	}

	LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	if (NULL != error_code) {
		*error_code = error;
	}

	return ret;

}

/* h.gahlaut :  Return true only if event_data is retrieved successfully */

static int emcore_retrieve_partial_body_thread_event(email_event_partial_body_thd *partial_body_thd_event, int *error_code)
{
	EM_DEBUG_FUNC_BEGIN();

	int ret = false;
	int error = EMAIL_ERROR_NONE;
	int index = 0;

	/* Lock Mutex to protect event_data queue and associated global variables variables*/

	ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	index = g_partial_body_thd_next_event_idx;

	if (0 == g_partial_body_thd_event_que[index].event_type) {
		error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
		g_partial_body_thd_queue_empty = true;
		g_partial_body_thd_queue_full = false;
	}
	else {
		/*Copy the event_data from queue to return it and free the event_data in queue */
		if (false == emcore_copy_partial_body_thd_event(g_partial_body_thd_event_que + index, partial_body_thd_event, &error))
			EM_DEBUG_EXCEPTION("emcore_copy_partial_body_thd_event failed [%d]", error);
		else {
			if (false == emcore_free_partial_body_thd_event(g_partial_body_thd_event_que + index, &error))
				EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", error);
			else {

				g_partial_body_thd_queue_full = false;
				g_partial_body_thd_next_event_idx = ++index;

				if (g_partial_body_thd_next_event_idx == TOTAL_PARTIAL_BODY_EVENTS)
					g_partial_body_thd_next_event_idx = 0;

				/* If the event_data retrieved was the only event_data present in queue,
				we need to set g_partial_body_thd_queue_empty to true
				*/

				if (0 == g_partial_body_thd_event_que[g_partial_body_thd_next_event_idx].event_type) {
					g_partial_body_thd_queue_empty = true;
				}

				ret = true;
			}
		}
	}

	/* Unlock Mutex */

	LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

	if (error_code)
		*error_code = error;

	return ret;

}

gpointer partial_body_download_thread(gpointer data)
{
	EM_DEBUG_FUNC_BEGIN();

	int err = EMAIL_ERROR_NONE;
	email_session_t *session = NULL;
	email_event_partial_body_thd partial_body_thd_event;

	EM_DEBUG_LOG(" ************ PB THREAD ID IS ALIVE. ID IS [%d] ********************" , THREAD_SELF());

	/* Open connection with DB */

	if (false == emstorage_open(&err))  {
		EM_DEBUG_EXCEPTION("emstorage_open failed [%d]", err);
		return false;
	}

	/* Start the continuous loop */

	while (g_partial_body_thd_loop) {
		/*  Get an empty session  */
		/*  TODO :  Mutex should be used in session APIs */

		if (false == emcore_get_empty_session(&session))
			EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
		else {	/* Get and Event from the Partial Body thread Event Queue */
			memset(&partial_body_thd_event, 0x00, sizeof(email_event_partial_body_thd));

			if (false == emcore_retrieve_partial_body_thread_event(&partial_body_thd_event, &err)) {
				if (EMAIL_ERROR_EVENT_QUEUE_EMPTY != err)
					EM_DEBUG_EXCEPTION("emcore_retrieve_partial_body_thread_event failed [%d]", err);
				else {
					EM_DEBUG_LOG(" partial body thread event_data queue is empty.");

					/*  Flush the que before starting local activity sync to clear the events in queue which are less than 10 in count  */
					if (!g_partial_body_bulk_dwd_queue_empty) {
						partial_body_thd_event.event_type = 0;
						partial_body_thd_event.account_id = g_partial_body_bulk_dwd_que[0].account_id;
						partial_body_thd_event.mailbox_id = g_partial_body_bulk_dwd_que[0].mailbox_id;
						partial_body_thd_event.mailbox_name = EM_SAFE_STRDUP(g_partial_body_bulk_dwd_que[0].mailbox_name);

						if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
							EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);

						emcore_pb_thd_set_local_activity_continue(true);
					}

					if (true == emcore_pb_thd_can_local_activity_continue()) {
						/*Check for local Activities */
						int is_local_activity_event_inserted = false;

						if (false == emcore_partial_body_thd_local_activity_sync(&is_local_activity_event_inserted, &err)) {
							EM_DEBUG_EXCEPTION("emcore_partial_body_thd_local_activity_sync failed [%d]", err);
						}
						else {
							if (true == is_local_activity_event_inserted) {
								emcore_pb_thd_set_local_activity_continue(false);

								emcore_clear_session(session);
								continue;
							}
						}
					}

					EM_DEBUG_LOG(" Partial Body Thread is going to sleep");

					emcore_set_pbd_thd_state(false);

					ENTER_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);
					SLEEP_CONDITION_VARIABLE(_partial_body_thd_cond, _partial_body_thd_event_queue_lock);
					LEAVE_CRITICAL_SECTION(_partial_body_thd_event_queue_lock);

					EM_DEBUG_LOG(" Partial Body Thread wakes up ");

					emcore_set_pbd_thd_state(true);
				}

			}
			else {
				EM_DEBUG_LOG(" Event Received from Partial Body Event Queue ");

				/* Since all events are network operations dnet init and sleep control is
				done before entering switch block*/

				emdevice_set_dimming_on_off(false, NULL);

				if (!emnetwork_check_network_status( &err))  {
					EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);;
				}
				else {
					/*  Process events  */
					EM_DEBUG_LOG("partial_body_thd_event.account_id[%d]", partial_body_thd_event.account_id);

					switch (partial_body_thd_event.event_type) {
						case EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD:  {
							if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err)) {
								EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from event_data queue failed [%d]", err);
							}
							break;
						}
						case EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD:  {
							partial_body_thd_event.event_type = 0;

							/* Both the checks below make sure that before starting local activity there is no new/pending event_data in
							*   g_partial_body_thd_event_que and g_partial_body_bulk_dwd_que */
							if (false == emcore_is_partial_body_thd_que_empty())
								break;
							if (!g_partial_body_bulk_dwd_queue_empty)
								break;

							if (false == emcore_mail_partial_body_download(&partial_body_thd_event, &err))
								EM_DEBUG_EXCEPTION("emcore_mail_partial_body_download from activity table failed [%d]", err);
							break;
						}
						default:
							EM_DEBUG_EXCEPTION(" Warning :  Default case entered. This should not happen ");
							break;
					}
				}

				if (false == emcore_free_partial_body_thd_event(&partial_body_thd_event, &err))
					EM_DEBUG_EXCEPTION("emcore_free_partial_body_thd_event_cell failed [%d]", err);

				emdevice_set_dimming_on_off(true, NULL);
		       }

		       emcore_clear_session(session);
	       }
	}

	/* If something is added to end thread in future for any case then if thread is holding any resources
	define a function emcore_partial_body_thd_loop_stop to release resources and call it
	here to end thread */
	return SUCCESS;
}

INTERNAL_FUNC int emcore_start_thread_for_downloading_partial_body(int *err_code)
{
	EM_DEBUG_FUNC_BEGIN();

	int i = 0, thread_error = -1;

	/* Clear Partial Body Event Queue*/
	memset(&g_partial_body_thd_event_que, 0x00, sizeof(g_partial_body_thd_event_que));

	for (i = 0; i < TOTAL_PARTIAL_BODY_EVENTS; ++i) {
		g_partial_body_thd_event_que[i].mailbox_name = NULL;
        g_partial_body_thd_event_que[i].mailbox_id = 0;
    }

	if (g_partial_body_thd)  {
		EM_DEBUG_EXCEPTION("partial body thread is already running...");
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_UNKNOWN;

		return true;
	}

	g_partial_body_thd_next_event_idx = 0;
	g_partial_body_thd_loop = 1;
	g_partial_body_thd_queue_empty = true;
	g_partial_body_thd_queue_full = false;

	INITIALIZE_CONDITION_VARIABLE(_partial_body_thd_cond);

	/* create thread */
	/* THREAD_CREATE_JOINABLE(g_partial_body_thd, partial_body_download_thread, thread_error); */
	THREAD_CREATE(g_partial_body_thd, partial_body_download_thread, NULL, thread_error);

	if (thread_error != 0) {
		EM_DEBUG_EXCEPTION("cannot make thread...");
		if (err_code != NULL)
			*err_code = EMAIL_ERROR_UNKNOWN;
		return FAILURE;
	}

	if (err_code != NULL)
		*err_code = EMAIL_ERROR_NONE;

	return false;

}

/*Function to flush the bulk partial body download queue [santosh.br@samsung.com]*/
static int emcore_partial_body_bulk_flush(int *error_code)
{
	EM_DEBUG_FUNC_BEGIN();
	int error = EMAIL_ERROR_NONE;
	int ret = false;
	MAILSTREAM *stream = NULL;
	void *tmp_stream = NULL;

	if (!emcore_connect_to_remote_mailbox(g_partial_body_bulk_dwd_que[0].account_id, g_partial_body_bulk_dwd_que[0].mailbox_id, (void **)&tmp_stream, &error) || (NULL == tmp_stream)) {
		EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
		goto FINISH_OFF;
	}
	stream = (MAILSTREAM *)tmp_stream;

	/*  Call bulk download here */
	if (false == emcore_download_bulk_partial_mail_body(stream, g_partial_body_bulk_dwd_que, g_partial_body_bulk_dwd_next_event_idx, &error)) {
		EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
		goto FINISH_OFF;
	}

	ret = true;
FINISH_OFF:

	emcore_close_mailbox(0, stream);
	stream = NULL;

	g_partial_body_bulk_dwd_next_event_idx = 0;
	g_partial_body_bulk_dwd_queue_empty = true;

	if (false == emcore_clear_bulk_pbd_que(&error))
		EM_DEBUG_EXCEPTION("emcore_clear_bulk_pbd_que failed [%d]", error);

	if (NULL != error_code)
		*error_code = error;

	EM_DEBUG_FUNC_END("ret [%d]", ret);
	return ret;
}


/* Function to pass UID list and Data for bulk partial body download [santosh.br@samsung.com]/[h.gahlaut@samsung.com] */
INTERNAL_FUNC int emcore_mail_partial_body_download(email_event_partial_body_thd *pbd_event, int *error_code)
{
	EM_DEBUG_FUNC_BEGIN();
	int error = EMAIL_ERROR_NONE;
	int num_activity = 0;
	int ret = false;
	int count = 0;
	int i = 0, m = 0;
	MAILSTREAM *stream = NULL;
	void *tmp_stream = NULL;
	email_event_partial_body_thd *activity_data_list = NULL;
	int *mailbox_list = NULL;

	if (NULL == pbd_event)
    {
	    EM_DEBUG_EXCEPTION("Invalid Parameter pbd_event [%p] ", pbd_event);

	    error = EMAIL_ERROR_INVALID_PARAM;
	    goto FINISH_OFF;
    }

	/*Check if the event_data is to flush the event_data que array */
	if (EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD == pbd_event->event_type) {
		EM_DEBUG_LOG("pbd_event->event_type is EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD");
		/*Check if the mailbox name and account id for this event_data is same as the mailbox name and account id for earlier events saved in download que array
		then append this event_data also to download que array */
		if ((0 != g_partial_body_bulk_dwd_que[0].mailbox_id) && g_partial_body_bulk_dwd_que[0].mailbox_id == pbd_event->mailbox_id && \
			(g_partial_body_bulk_dwd_que[0].account_id == pbd_event->account_id)) {
			EM_DEBUG_LOG("Event is for the same mailbox and same account as the already present events in download que");
			EM_DEBUG_LOG("Check if the download que reached its limit. If yes then first flush the que.");
			if (g_partial_body_bulk_dwd_next_event_idx == BULK_PARTIAL_BODY_DOWNLOAD_COUNT) {
				if (false == emcore_partial_body_bulk_flush(&error)) {
					EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
					goto FINISH_OFF;
				}
			}
		}
		else  {
			EM_DEBUG_LOG("Event is not for the same mailbox and same account as the already present events in download que");
			EM_DEBUG_LOG("Flush the current que if not empty");
			EM_DEBUG_LOG("g_partial_body_bulk_dwd_queue_empty [%d]", g_partial_body_bulk_dwd_queue_empty);
			if (!g_partial_body_bulk_dwd_queue_empty) {
				if (false == emcore_partial_body_bulk_flush(&error)) {
					EM_DEBUG_EXCEPTION("Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
					goto FINISH_OFF;
				}
			}
		}
		/*Add the event_data to the download que array */
		if (false == emcore_copy_partial_body_thd_event(pbd_event, g_partial_body_bulk_dwd_que+(g_partial_body_bulk_dwd_next_event_idx), &error))
			EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_copy_partial_body_thd_event failed - %d", error);
		else {
			g_partial_body_bulk_dwd_queue_empty = false;
			g_partial_body_bulk_dwd_next_event_idx++;
			EM_DEBUG_LOG("g_partial_body_bulk_dwd_next_event_idx [%d]", g_partial_body_bulk_dwd_next_event_idx);
		}
	}
	else if (pbd_event->activity_type) {
		int *account_list = NULL;
		int account_count = 0;

		EM_DEBUG_LOG("Event is coming from local activity.");
		/* Get all the accounts for which local activities are pending */
		if (false == emstorage_get_pbd_account_list(&account_list, &account_count, false, &error)) {
				EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
				error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
				goto FINISH_OFF;
		}

		for (m = 0; m < account_count; ++m) {
			/* Get the mailbox list for the account to start bulk partial body fetch for mails in each mailbox of accounts one by one*/
			if (false == emstorage_get_pbd_mailbox_list(account_list[m], &mailbox_list, &count, false, &error)) {
					EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_list failed.. [%d]", error);
					error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
					goto FINISH_OFF;
			}

			for (i = 0; i < count; i++) {
				int k = 0;
				int activity_count = 0;

				if (!emcore_connect_to_remote_mailbox(account_list[m], mailbox_list[i], (void **)&tmp_stream, &error))  {
					EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", error);
					stream = NULL;
					goto FINISH_OFF;
				}

				stream = (MAILSTREAM *)tmp_stream;

				if (false == emstorage_get_mailbox_pbd_activity_count(account_list[m], mailbox_list[i], &activity_count, false, &error)) {
					EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_pbd_activity_count failed.. [%d]", error);
					continue;
				}

				if (activity_count > 0) {
					int temp_error = EMAIL_ERROR_NONE;
					int j = 0;
					int iter = 0;
					int remainder = 0;
					int num = BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
					int index = 0;

					if (false == emstorage_get_pbd_activity_data(account_list[j], mailbox_list[i], &activity_data_list, &num_activity,  false, &error))
						EM_DEBUG_EXCEPTION(" emstorage_get_pbd_activity_data failed.. [%d]", error);

					if (NULL == activity_data_list) {
						EM_DEBUG_EXCEPTION(" activity_data_list is null..");
						continue;
					}

					remainder = num_activity%BULK_PARTIAL_BODY_DOWNLOAD_COUNT;
					iter = num_activity/BULK_PARTIAL_BODY_DOWNLOAD_COUNT + ((num_activity%BULK_PARTIAL_BODY_DOWNLOAD_COUNT) ? 1  :  0);

					for (j = 0; j < iter; j++) {
	 					if ((iter == (j+1)) && (remainder != 0))
							num = remainder;

						/*Call bulk download here */
						if (false == emcore_download_bulk_partial_mail_body(stream, activity_data_list+index, num, &error)) {
							EM_DEBUG_EXCEPTION(" emcore_download_bulk_partial_mail_body failed.. [%d]", error);
							temp_error = EMAIL_ERROR_NO_RESPONSE;
						}

						for (k = 0; k < num; k++) {
							if (activity_data_list[index + k].activity_type)
								emcore_free_partial_body_thd_event(activity_data_list + index + k, &error);
							else
								break;
						}
						index += num;

						if (false == emcore_is_partial_body_thd_que_empty()) {
							ret = true;
							goto FINISH_OFF;		/* Stop Local Activity Sync */
						}
						if (EMAIL_ERROR_NO_RESPONSE == temp_error) {
							temp_error = EMAIL_ERROR_NONE;
							break;
						}
					}
				}
			}
			emcore_close_mailbox(0, stream);
			stream = NULL;
			tmp_stream = NULL;
		}

		/* After completing one cycle of local activity sync ,
		local activity continue variable should be set to false. */

		emcore_pb_thd_set_local_activity_continue(false);
	}
	else /* Event-type is 0 which means Event is to flush the que when no more events are present in g_partial_body_thd_event_que */ {
		/*Check if events have arrived in g_partial_body_thd_event_que */
		if (false == emcore_is_partial_body_thd_que_empty()) {
			EM_DEBUG_LOG("emcore_is_partial_body_thd_que_empty retured true");
			ret = true;
			goto FINISH_OFF;		/* Stop Local Activity Sync */
		}
		if (false == emcore_partial_body_bulk_flush(&error)) {
			EM_DEBUG_EXCEPTION("\t Partial Body thread emcore_partial_body_bulk_flush failed - %d", error);
			goto FINISH_OFF;
		}
	}

	ret = true;

FINISH_OFF:

	EM_SAFE_FREE(mailbox_list);

	if (activity_data_list) {
		for (i = 0; i < num_activity; i++) {
			if (activity_data_list[i].activity_type)
				emcore_free_partial_body_thd_event(activity_data_list + i, &error);
			else
				break;
		}
	}

	emcore_close_mailbox(0, stream);
	stream = NULL;

	if (NULL != error_code)
		*error_code = error;
	EM_DEBUG_FUNC_END("ret [%d]", ret);
	return ret;
}

#endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */