bug fix(StatusBar): Clock is not displayed at the time of the first start
[profile/ivi/ico-uxf-homescreen.git] / src / homescreen / CicoHSLifeCycleController.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 #include <string>
11 #include <vector>
12 #include <cstdio>
13 #include <cstdlib>
14 #include <glib.h>
15 #include <ail.h>
16 #include <aul/aul.h>
17 #include <sys/stat.h>
18
19 #include "ico_syc_error.h"
20 #include <ico_log.h>
21 #include "CicoAulItems.h"
22 #include "CicoAilItems.h"
23 #include "CicoHSLifeCycleController.h"
24 #include "CicoConf.h"
25 #include "CicoSystemConfig.h"
26 #include "Cico_aul_listen_app.h"
27
28 using namespace std;
29
30
31 #define ICO_SYC_EOK              0  /* OK                           */
32 #define ICO_SYC_EPERM           -1  /* Operation not permitted      */
33 #define ICO_SYC_ENOENT          -2  /* No such object               */
34 #define ICO_SYC_ESRCH           -3  /* Not initialized              */
35 #define ICO_SYC_EIO             -5  /* I/O(send/receive) error      */
36 #define ICO_SYC_ENXIO           -6  /* Not exist                    */
37 #define ICO_SYC_E2BIG           -7  /* Buffer size too smale        */
38 #define ICO_SYC_EBADF           -9  /* Illegal data type            */
39 #define ICO_SYC_EAGAIN          -11 /* Try again                    */
40 #define ICO_SYC_ENOMEM          -12 /* Out of memory                */
41 #define ICO_SYC_EFAULT          -14 /* Bad address                  */
42 #define ICO_SYC_EBUSY           -16 /* Not available now            */
43 #define ICO_SYC_EEXIST          -17 /* Multiple define              */
44 #define ICO_SYC_EINVAL          -22 /* Invalid argument             */
45 #define ICO_SYC_ENOSYS          -38 /* System error                 */
46
47
48 CicoHSLifeCycleController* CicoHSLifeCycleController::ms_myInstance = NULL;
49
50 const char* DAilTypeFilPrm_Menu="menu";
51 const char* DAilTypeFilPrm_App="Application";
52 const char* DNil = "(nil)";
53 const char* sectAppAttr = "app-attributes";
54 const char* DNull = "(null)";
55
56 #define APP_CONF_AIL_NULL_STR   DNull
57
58 /**
59  * @brief
60  * @param req_id
61  * @param pkg_type
62  * @param pkg_name package name
63  * @param key "start"/"end"/"install_percent"/"command" strings
64  * @param val "ok"/"error"/"0.000000"-"100.000000" strings
65  * @param pmsg pkgmgr message
66  * @param data user data
67  */
68 static int CSCLCCpkgmgr_handler(int req_id, const char *pkg_type,
69                                 const char *pkg_name, const char *key,
70                                 const char *val, const void *pmsg, void *data)
71 {
72     CicoHSLifeCycleController* x = (CicoHSLifeCycleController*)data;
73     return
74         CSCLCCpkgmgr_handlerX(req_id, pkg_type, pkg_name, key, val, pmsg, x);
75 }
76
77 /**
78  * @brief ail_filter_list_appinfo_foreach callback function
79  * @param appinfo appinfo_h the appinfo's handle
80  * @param data user data
81  * @retval AIL_CB_RET_CONTINUE return if you continue iteration
82  * @retval AIL_CB_RET_CANCEL return if you cancel iteration
83  */
84 static ail_cb_ret_e CSCLCCail_list_appinfo_cb(const ail_appinfo_h appinfo,
85                                               void *data)
86 {
87     CicoHSLifeCycleController* x = (CicoHSLifeCycleController*)data;
88     return CSCLCCail_list_appinfo_cbX(appinfo, x);
89 }
90
91 /**
92  * @brief applications are launched. callback function
93  * @param pid
94  * @param data user data
95  */
96 static int CSCLCCapp_launch_handler(int pid, void *data)
97 {
98     CicoHSLifeCycleController* x = (CicoHSLifeCycleController*)data;
99     return CSCLCCapp_launch_handlerX(pid, x);
100 }
101
102 /**
103  * @brief applications die. callback function
104  * @param pid
105  * @param data user data
106  */
107 static int CSCLCCapp_dead_handler(int pid, void *data)
108 {
109     CicoHSLifeCycleController* x = (CicoHSLifeCycleController*)data;
110     return CSCLCCapp_dead_handlerX(pid, x);
111 }
112
113
114 /**
115  * @brief This API get application appid by pid
116  * @param pid
117  * @param appid store appid string
118  * @param len appid buffer size
119  * @ret aul_app_get_appid_bypid return value
120  * @retval AUL_R_OK    - success
121  * @retval AUL_R_ERROR - no such a appid
122  */
123 int Xaul_app_get_appid_bypid(int pid, char *appid, int len)
124 {
125     int r = aul_app_get_appid_bypid(pid, appid, len);
126     if (AUL_R_OK == r) {
127         return r;
128     }
129     char fn[128];
130     sprintf(fn, "/proc/%d/cmdline", pid);
131     string tmp;
132     ifstream ifs(fn);
133     ifs >> tmp;
134     ifs.close();
135     const char* p = tmp.c_str();
136     int sz = strlen(p);
137     int i = sz - 2;
138     while (0 <= i) {
139         if ('/' == p[i]) {
140             strncpy(appid, &p[i+1], len);
141             break; // break of while i
142         }
143         i--;
144     }
145     return r;
146 }
147
148 /**
149  * @brief contractor
150  */
151 CicoHSLifeCycleController::CicoHSLifeCycleController()
152 {
153     m_gconf = (GKeyFile*)NULL;
154     m_pc = (pkgmgr_client*)NULL;
155     ailRenewFlagOff();
156     initAIL();
157     initAUL();
158 }
159
160 /**
161  * @brief destructor
162  */
163 CicoHSLifeCycleController::~CicoHSLifeCycleController()
164 {
165
166 }
167
168 CicoHSLifeCycleController*
169 CicoHSLifeCycleController::getInstance(void)
170 {
171     if (NULL == ms_myInstance) {
172         ms_myInstance = new CicoHSLifeCycleController();
173     }
174     return ms_myInstance;
175 }
176
177 /**
178  * @brief application launch
179  * @param appid application id
180  * @retval ICO_SYC_EOK   success
181  * @retval ICO_SYC_ESRCH error(not initialized)
182  * @retval ICO_SYC_ENOENT error(does not exist)
183  * @retval ICO_SYC_EBUSY error(already launch)
184  * @retval ICO_SYC_EPERM error(no authority)
185  */
186 int CicoHSLifeCycleController::launch(const char* appid)
187 {
188     ICO_TRA("start %s", appid);
189     // appid check AIL table exist
190     if (NULL == findAIL(appid)) {
191         ICO_TRA("end fail(not find)");
192         return ICO_SYC_ENOSYS;
193     }
194     ICO_PRF("CHG_APP_STA 2.request app=%s", appid);
195     int r = aul_launch_app(appid, NULL);
196     if (0 > r) {
197         ICO_TRA("end fail(%d)", r);
198         return ICO_SYC_ENOSYS;
199     }
200     ICO_TRA("end success(%d)", r);
201     return ICO_SYC_EOK;
202 }
203
204 /**
205  * @brief application launch
206  * @param appid application id
207  * @retval ICO_SYC_EOK   success
208  * @retval ICO_SYC_ESRCH error(not initialized)
209  * @retval ICO_SYC_ENOENT error(does not exist)
210  * @retval ICO_SYC_EBUSY error(already launch)
211  * @retval ICO_SYC_EPERM error(no authority)
212  */
213 int CicoHSLifeCycleController::launch(const std::string& appid)
214 {
215     return launch((const char*)appid.c_str());
216 }
217
218 /**
219  * @brief application terminate
220  * @param appid application id
221  * @retval ICO_SYC_EOK   success
222  * @retval ICO_SYC_ESRCH error(not initialized)
223  * @retval ICO_SYC_ENOENT error(does not exist)
224  * @retval ICO_SYC_EBUSY error(already launch)
225  * @retval ICO_SYC_EPERM error(no authority)
226  */
227 int CicoHSLifeCycleController::terminate(const char* appid)
228 {
229     ICO_TRA("CicoHSLifeCycleController::terminate %s", appid);
230     int r = ICO_SYC_EOK;
231     vector<int> pids;
232     if (true == getPIDs(appid, pids)) {
233         vector<int>::iterator it;
234         for(it = pids.begin(); it != pids.end(); ++it) {
235             int tr = terminateR(*it);
236             if (ICO_SYC_EOK != tr) {
237                 r = tr;
238             }
239         }
240     }
241     else {
242         ICO_TRA("end ICO_SYC_EPERM");
243         return ICO_SYC_EPERM;
244     }
245     ICO_TRA("end %d", r);
246     return r;
247 }
248
249 /**
250  * @brief application terminate
251  * @param appid application id
252  * @retval ICO_SYC_EOK   success
253  * @retval ICO_SYC_ESRCH error(not initialized)
254  * @retval ICO_SYC_ENOENT error(does not exist)
255  * @retval ICO_SYC_EBUSY error(already launch)
256  * @retval ICO_SYC_EPERM error(no authority)
257  */
258 int CicoHSLifeCycleController::terminate(const std::string& appid)
259 {
260     return terminate((const char*)appid.c_str());
261 }
262
263 /**
264  * @brief application terminate
265  * @param pid
266  * @retval ICO_SYC_EOK   success
267  * @retval ICO_SYC_ESRCH error(not initialized)
268  * @retval ICO_SYC_ENOENT error(does not exist)
269  * @retval ICO_SYC_EBUSY error(already launch)
270  * @retval ICO_SYC_EPERM error(no authority)
271  */
272 int CicoHSLifeCycleController::terminate(int pid)
273 {
274     ICO_TRA("CicoHSLifeCycleController::terminate %d", pid);
275     const CicoAulItems* t = findAUL(pid);
276     if ((NULL == t) || (0 == t)) {
277         ICO_TRA("not find");
278         return ICO_SYC_EPERM;
279     }
280     return terminateR(pid);
281 }
282
283 /**
284  * @brief application terminate
285  * @param pid
286  * @retval ICO_SYC_EOK   success
287  * @retval ICO_SYC_ENOSYS error
288  */
289 int CicoHSLifeCycleController::terminateR(int pid)
290 {
291     ICO_TRA("CicoHSLifeCycleController::terminateR %d", pid);
292     int r = aul_terminate_pid(pid);
293     if (r < 0) {
294         ICO_DBG("terminate error pid:%d, r:%d", pid, r);
295         return ICO_SYC_ENOSYS;
296     }
297     return ICO_SYC_EOK;
298 }
299
300 /**
301  * @brief application suspend
302  * @param appid application id
303  * @retval ICO_SYC_EOK   success
304  * @retval ICO_SYC_ESRCH error(not initialized)
305  * @retval ICO_SYC_ENOENT error(does not exist)
306  * @retval ICO_SYC_EBUSY error(already launch)
307  * @retval ICO_SYC_EPERM error(no authority)
308  */
309 int CicoHSLifeCycleController::suspend(const char* appid)
310 {
311     int r = ICO_SYC_EOK;
312     vector<int> pids;
313     if (true == getPIDs(appid, pids)) {
314         vector<int>::iterator it;
315         for(it = pids.begin(); it != pids.end(); ++it) {
316             int tr = suspend(*it);
317             if (ICO_SYC_EOK != tr) {
318                 r = tr;
319             }
320         }
321     }
322     else {
323         ICO_TRA("end ICO_SYC_EPERM");
324         return ICO_SYC_EPERM;
325     }
326     ICO_TRA("end %d", r);
327     return r;
328 }
329
330 /**
331  * @brief application suspend
332  * @param appid application id
333  * @retval ICO_SYC_EOK   success
334  * @retval ICO_SYC_ESRCH error(not initialized)
335  * @retval ICO_SYC_ENOENT error(does not exist)
336  * @retval ICO_SYC_EBUSY error(already launch)
337  * @retval ICO_SYC_EPERM error(no authority)
338  */
339 int CicoHSLifeCycleController::suspend(const std::string& appid)
340 {
341     return suspend((const char*)appid.c_str());
342 }
343
344 /**
345  * @brief application suspend
346  * @param pid
347  * @retval ICO_SYC_EOK   success
348  * @retval ICO_SYC_ESRCH error(not initialized)
349  * @retval ICO_SYC_ENOENT error(does not exist)
350  * @retval ICO_SYC_EBUSY error(already launch)
351  * @retval ICO_SYC_EPERM error(no authority)
352  */
353 int CicoHSLifeCycleController::suspend(int pid)
354 {
355 // TODO mk_k
356 // TODO mk_k
357     return ICO_SYC_EPERM;
358 }
359
360 /**
361  * @brief appid is running check
362  * @param appid package
363  * @return running status
364  * @retval true is running
365  * @retval false not running
366  */
367 bool CicoHSLifeCycleController::isRunning(const char* appid)
368 {
369     bool r = false;
370 #if 0 // TODO mk_k
371     vector<CicoAulItems>::iterator it = m_aul.begin();
372     while (it != m_aul.end()) {
373         it->update_appid();
374         ICO_DBG("%s == %s", it->m_appid.c_str(), appid);
375         if (it->m_appid == appid) {
376             r = true;
377             break;  // break of while it
378         }
379         ++it;
380     }
381 #else
382     int sz = m_aul.size();
383     for (int i = 0; i < sz; i++) {
384         m_aul[i].update_appid();
385 //      ICO_DBG("%s == %s", m_aul[i].m_appid.c_str(), appid);
386         if (m_aul[i].m_appid == appid) {
387             r = true;
388             break;
389         }
390     }
391 #endif
392     ICO_TRA("CicoHSLifeCycleController::isRunning %s=%s", appid, r ? "true" : "false");
393     return r;
394 }
395
396 /**
397  * @brief appid is running check
398  * @param appid package
399  * @return running status
400  * @retval true is running
401  * @retval false not running
402  */
403 bool CicoHSLifeCycleController::isRunning(const std::string& appid)
404 {
405     return isRunning((const char*)appid.c_str());
406 }
407
408 /**
409  * @brief find ail data
410  * @param appid find key
411  * @return CicoAilItems class pointer
412  * @retval NULL is not find
413  */
414 const CicoAilItems* CicoHSLifeCycleController::findAIL(const char* appid) const
415 {
416     ICO_TRA("start");
417     const CicoAilItems* r = NULL;
418     int sz = m_ail.size();
419     for (int i = 0; i < sz; i++) {
420         if (0 == strcasecmp(m_ail[i].m_appid.c_str(), appid)) {
421             r = m_ail[i].p();
422             break; // break of for
423         }
424     }
425     ICO_TRA("end %x", r);
426     return r;
427 }
428
429 /**
430  * @brief find ail data
431  * @param appid find key
432  * @return CicoAilItems class pointer
433  * @retval NULL is not find
434  */
435 const CicoAilItems* CicoHSLifeCycleController::findAIL(const std::string& appid) const
436 {
437     ICO_TRA("call findAIL(const chr*)");
438     return findAIL((const char*)appid.c_str());
439 }
440
441 /**
442  * @brief get launched apprication pid
443  * @param appid target application appid
444  * @return pid
445  */
446 bool CicoHSLifeCycleController::getPIDs(const char* appid, std::vector<int>& pids) const
447 {
448     ICO_TRA("start %s", appid);
449     bool r = false;
450 #if 0 // TODO mk_k
451     vector<CicoAulItems>::iterator it;
452     for (it = m_aul.begin(); it != m_aul.end(); ++it) {
453         it->update_appid();
454         if (0 == strcasecmp(it->m_appid.c_str(), appid)) {
455             pids.push_back(it->m_pid);
456             r = true;
457         }
458     }
459 #else
460     int sz = m_aul.size();
461     for (int i = 0; i < sz; i++) {
462         CicoAulItems* t = (CicoAulItems*)m_aul[i].p();
463         t->update_appid();
464         if (0 == strcasecmp(t->m_appid.c_str(), appid)) {
465             pids.push_back(t->m_pid);
466             r = true;
467         }
468     }
469 #endif
470     ICO_TRA("end %d %s", pids.size(), r? "true": "false");
471     return r;
472 }
473
474 /**
475  * @brief get launched apprication pid
476  * @param appid target application appid
477  * @return pid
478  */
479 bool CicoHSLifeCycleController::getPIDs(std::string& appid, std::vector<int>& pids) const
480 {
481     return getPIDs((const char*)appid.c_str(), pids);
482 }
483
484 /**
485  * @brief get launched apprication pid
486  * @param appid target application appid
487  * @return pid
488  */
489 const CicoAulItems* CicoHSLifeCycleController::findAUL(int pid)
490 {
491     ICO_TRA("start %d", pid);
492     const CicoAulItems* r = NULL;
493     int sz = m_aul.size();
494     for (int i = 0; i < sz; i++) {
495         CicoAulItems* t = (CicoAulItems*)m_aul[i].p();
496         if (t->m_pid == pid) {
497             t->update_appid();
498             r = t;
499             ICO_DBG("find %d -> %s", pid, t->m_appid.c_str());
500             break; // break of for
501         }
502     }
503     ICO_TRA("end %x", r);
504     return r;
505 }
506
507
508 /**
509  * @brief ail information data initialization
510  */
511 void CicoHSLifeCycleController::initAIL()
512 {
513     ICO_TRA("start %x", m_pc);
514     createAilItems();
515     if ((pkgmgr_client*)NULL == m_pc) {
516         m_pc = pkgmgr_client_new(PC_LISTENING);
517         int r = pkgmgr_client_listen_status(m_pc, CSCLCCpkgmgr_handler,
518                                             (void*)this);
519         ICO_TRA("pkgmgr_xxxx %x, %d", m_pc, r);
520 //        if (PKGMGR_R_OK != r)
521         // pkgmgr_client_listen_status return is
522         // request_id (>0) if success, error code(<0) if fail
523         // PKGMGR_R_OK success (PKGMGR_R_OK is 0)
524         if (0 > r) {
525             pkgmgr_client_free(m_pc);
526             m_pc = NULL;
527         }
528     }
529     ICO_TRA("end %x", m_pc);
530 }
531
532 /**
533  * @brief
534  */
535 int CSCLCCpkgmgr_handlerX(int req_id, const char *pkg_type, const char *pkg_name,
536                          const char *key, const char *val, const void *pmsg,
537                          CicoHSLifeCycleController* x)
538 {
539 // TODO mk_k
540     ICO_TRA("pkgmgr t:%s, n:%s, k:%s, v:%s, m:%s",pkg_type, pkg_name, key,
541             val, pmsg);
542     if ((NULL == x) || (0 == x)) {
543         ICO_TRA("end user data none");
544         return -1;
545     }
546     if (strcasecmp(key, "end") == 0) {
547         if (strcasecmp(val, "ok") == 0) {
548             ICO_TRA("end::ok");
549             x->renewAIL();
550         }
551     }
552     return 0;
553 // TODO mk_k
554 }
555
556 /**
557  * @brief AIL list renew
558  */
559 void CicoHSLifeCycleController::renewAIL()
560 {
561     vector<CicoAilItems> old = m_ail;
562     m_ail.clear();
563     int cnt =0;
564     while (false == createAilItems()) {
565         if (cnt > 500) {
566             m_ail.clear();
567             m_ail = old;
568             break;
569         }
570         cnt++;
571         usleep(10000);
572     }
573     if (cnt > 500) {
574         ICO_TRA("error AIL I/F");
575         return ;
576     }
577 #if 0 // TODO mk_k
578     vector<string> add_appid;
579     vector<string> remove_appid;
580
581     vector<CicoAulItems>::iterator it_n;
582     vector<CicoAulItems>::iterator it_o;
583     for(it_n = m_aul.begin(); it_n != m_aul.end(); ++it_n) {
584         bool bingo = false;
585         for(it_o = old.begin(); it_o != old.end(); ++it_o) {
586             if (it_n->m_appid.compaire(it_o->m_appid)) {
587                 bingo = true;
588             }
589         }
590         if (false == bingo) {
591             add_appid.push_back(it_n->m_appid);
592         }
593     }
594     for(it_o = old.begin(); it_o != old.end(); ++it_o) {
595         bool bingo = false;
596         for(it_n = m_aul.begin(); it_n != m_aul.end(); ++it_n) {
597             if (it_o->m_appid.compaire(it_n->m_appid)) {
598                 bingo = true;
599             }
600         }
601         if (false == bingo) {
602             remove_appid.push_back(it_o->m_appid);
603         }
604     }
605
606     if (0 != add_appid.size()) {
607 // TODO mk_k install
608     }
609     if (0 != remove_appid.size()) {
610 // TODO mk_k uninstall
611     }
612 #endif
613     old.clear();
614     ailRenewFlagOn();
615     return;
616 }
617
618 /**
619  * @brief ail_filter_list_appinfo_foreach callback function
620  * @param appinfo appinfo_h the appinfo's handle
621  * @param x CicoHSLifeCycleController class pointer
622  * @retval AIL_CB_RET_CONTINUE return if you continue iteration
623  * @retval AIL_CB_RET_CANCEL return if you cancel iteration
624  *
625  */
626 ail_cb_ret_e CSCLCCail_list_appinfo_cbX(const ail_appinfo_h appinfo,
627                                         CicoHSLifeCycleController* x)
628 {
629     ICO_TRA("start");
630     if ((NULL == x) || (0 == x)) {
631         ICO_TRA("end bad end")
632         return AIL_CB_RET_CANCEL;
633     }
634     char   *pkg;
635     char   *icn;
636     char   *nm;
637     char   *ctgry;
638     char   *typ;
639     char   *exe;
640     bool   bndsp = false;
641     /* get package name for appid */
642     ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &pkg);
643     if (strcmp(pkg, APP_CONF_AIL_NULL_STR) == 0) {
644         pkg = NULL;
645     }
646     /* get icon path */
647     ail_appinfo_get_str(appinfo, AIL_PROP_ICON_STR, &icn);
648     if (strcmp(icn, APP_CONF_AIL_NULL_STR) == 0) {
649         icn = NULL;
650     }
651     else if (icn != NULL) {
652         struct stat buff;
653         /* file check */
654         memset(&buff, 0, sizeof(buff));
655         if (stat(icn, &buff) == 0) {
656             if (S_ISDIR(buff.st_mode)) {
657                 /* is directory */
658                 ICO_DBG("%s is directory", icn);
659                 icn = NULL;
660             }
661         }
662         else {
663             /* is not exist */
664             icn = NULL;
665         }
666     }
667     /* get name */
668     ail_appinfo_get_str(appinfo, AIL_PROP_NAME_STR, &nm);
669     if (strcmp(nm, APP_CONF_AIL_NULL_STR) == 0) {
670         nm = NULL;
671     }
672     /* get category */
673     ail_appinfo_get_str(appinfo, AIL_PROP_CATEGORIES_STR, &ctgry);
674     if (strcmp(ctgry, APP_CONF_AIL_NULL_STR) == 0) {
675         ctgry = NULL;
676     }
677     /* get type */
678     ail_appinfo_get_str(appinfo, AIL_PROP_TYPE_STR, &typ);
679     if (strcmp(typ, APP_CONF_AIL_NULL_STR) == 0) {
680         typ = NULL;
681     }
682     /* get exec */
683     ail_appinfo_get_str(appinfo, AIL_PROP_EXEC_STR, &exe);
684     if (strcmp(exe, APP_CONF_AIL_NULL_STR) == 0) {
685         exe = NULL;
686     }
687     ail_appinfo_get_bool(appinfo, AIL_PROP_NODISPLAY_BOOL, &bndsp);
688     ICO_DBG("pkg:%s icn:%s nm:%s ctg:%s ty:%s exe:%s ds:%s", pkg? pkg:DNil,
689             icn? icn:DNil, nm? nm:DNil, ctgry? ctgry:DNil, typ? typ:DNil,
690             exe? exe:DNil, bndsp? "true":"false");
691     if (false == x->addAIL(pkg, icn, nm, ctgry, typ, exe, bndsp)) {
692         ICO_TRA("end bad end")
693         return AIL_CB_RET_CANCEL;
694     }
695     ICO_TRA("end")
696     return AIL_CB_RET_CONTINUE;
697 }
698
699 /**
700  * @brief create information ail data
701  * @retval true success
702  * @retval false fail create
703  */
704 bool CicoHSLifeCycleController::createAilItems()
705 {
706     ICO_TRA("start");
707     m_ail.clear(); // clear items
708     if (NULL == m_gconf) {
709         m_gconf = g_key_file_new();
710         GString* gsfp = g_string_new("xx");
711         CicoSystemConfig* CSCSC = CicoSystemConfig::getInstance();
712         g_string_printf(gsfp, "%s/%s", CSCSC->getDefaultConf()->confdir.c_str(),
713                         ICO_SYC_CONFIG_APPATTR);
714         GError  *gerr = NULL;
715         int flg;
716         flg = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
717         if (!g_key_file_load_from_file(m_gconf, gsfp->str, (GKeyFileFlags)flg, &gerr)) {
718             ICO_ERR("load error conf:%s %s", (char*)gsfp->str, gerr->message);
719             g_key_file_free(m_gconf);
720             m_gconf = NULL;
721             g_string_free(gsfp, TRUE);
722             return false;
723         }
724         g_string_free(gsfp, TRUE);
725     }
726
727     int r;
728     ail_filter_h fil;
729     ail_filter_new(&fil);
730     r = ail_filter_add_str(fil, AIL_PROP_TYPE_STR, DAilTypeFilPrm_Menu);
731     r = ail_filter_list_appinfo_foreach(fil, CSCLCCail_list_appinfo_cb,
732                                         (void*)this);
733     ail_filter_destroy(fil);
734     if (r != AIL_ERROR_OK) {
735         if (m_gconf) {
736             g_key_file_free(m_gconf);
737             m_gconf = NULL;
738         }
739         ICO_TRA("end menu read NG(AIL)=%d", r);
740         return false;
741     }
742
743     ail_filter_new(&fil);
744     r = ail_filter_add_str(fil, AIL_PROP_TYPE_STR, DAilTypeFilPrm_App);
745     r = ail_filter_list_appinfo_foreach(fil, CSCLCCail_list_appinfo_cb,
746                                         (void*)this);
747     ail_filter_destroy(fil);
748     if (r != AIL_ERROR_OK) {
749         if (m_gconf) {
750             g_key_file_free(m_gconf);
751             m_gconf = NULL;
752         }
753         ICO_TRA("end Application read NG(AIL)=%d", r);
754         return false;
755     }
756     if (m_gconf) {
757         g_key_file_free(m_gconf);
758         m_gconf = NULL;
759     }
760     ICO_TRA("end");
761     return true;
762 }
763
764 /**
765  * @brief ail information data add one item
766  * @param sPkg string package (appid)
767  * @param sIco string icon full path
768  * @param sNm string name
769  * @param sCtg string categoies
770  * @param sTyp string type
771  * @param sExe string exec name
772  * @param bndsp bool nodisplay value
773  * @return method status
774  * @retval true success added item
775  * @retval false fail added item
776  */
777 bool CicoHSLifeCycleController::addAIL(const char* sPkg, const char* sIco,
778                                        const char* sNm, const char* sCtg,
779                                        const char* sTyp, const char* sExe,
780                                        bool bndsp)
781 {
782     string category;
783     getCategory(sPkg, sNm, sCtg, category);
784     CicoAilItems CSCAI(sPkg, sIco, sNm, sCtg, sTyp, sExe, category, bndsp);
785     m_ail.push_back(CSCAI);
786     return true;
787 }
788
789 /**
790  * @brief get category data
791  * @param sPkg ail package(appid) (get key)
792  * @param sNm ail name (get key)
793  * @param sCtg ail categories (get key)
794  * @param category read config file category string define
795  */
796 void CicoHSLifeCycleController::getCategory(const char* sPkg, const char* sNm,
797                                             const char* sCtg,
798                                             std::string& category)
799 {
800     category.clear();
801     GError *gerr;
802     char *appCtgry;
803     int  appCtgryType;
804     char addCtgry[400];
805     int  addCtgryLen;
806     char work[PATH_MAX];
807
808     /* get default category of this application */
809     addCtgry[0] = 0;
810     gerr = NULL;
811     appCtgry = g_key_file_get_string(m_gconf, sectAppAttr, sPkg, &gerr);
812     if (gerr == NULL)  {
813         appCtgryType = 0;
814     }
815     else {
816         g_clear_error(&gerr);
817         gerr = NULL;
818         appCtgry = g_key_file_get_string(m_gconf, sectAppAttr, sNm, &gerr);
819         if (gerr == NULL) {
820             appCtgryType = 1;
821         }
822     }
823     addCtgryLen = 0;
824     if (gerr != NULL) {
825         g_clear_error(&gerr);
826     }
827     else {
828         for (int i = 1;; i++) {
829             strncpy(&addCtgry[addCtgryLen],
830             appCtgry, sizeof(addCtgry)-addCtgryLen-2);
831             addCtgry[sizeof(addCtgry)-2] = 0;
832             addCtgryLen = strlen(addCtgry);
833             if (addCtgryLen > 0) {
834                 if (addCtgry[addCtgryLen-1] != ';') {
835                     strcpy(&addCtgry[addCtgryLen++], ";");
836                 }
837             }
838             snprintf(work, sizeof(work)-1, "%s.%d",
839                      appCtgryType == 0 ? sPkg : sNm, i);
840             gerr = NULL;
841             appCtgry = g_key_file_get_string(m_gconf, sectAppAttr, work, &gerr);
842             if (gerr != NULL) {
843                 g_clear_error(&gerr);
844                 break;
845             }
846         }
847     }
848     g_clear_error(&gerr);
849
850     /* get category */
851     if (NULL != sCtg) {
852         strncpy(&addCtgry[addCtgryLen], sCtg, sizeof(addCtgry)-addCtgryLen-1);
853         addCtgry[sizeof(addCtgry)-1] = 0;
854     }
855     if (addCtgry[0]) {
856          category.assign(addCtgry);
857     }
858 }
859
860 /**
861  * @brief aul information data initialization
862  */
863 void CicoHSLifeCycleController::initAUL()
864 {
865     ICO_TRA("start");
866     aul_listen_app_launch_signal_add(CSCLCCapp_launch_handler, (void*)this);
867     aul_listen_app_dead_signal_add(CSCLCCapp_dead_handler, (void*)this);
868     ICO_TRA("end");
869 }
870
871 /**
872  * @brief
873  */
874 void CicoHSLifeCycleController::enterAUL(const char* appid, int pid,
875                                          const CicoSCWindow* obj, int aulstt)
876 {
877     ICO_TRA("start");
878     bool bPushBack = true;
879     int sz = m_aul.size();
880     for (int i = 0; i < sz; i++) {
881         CicoAulItems* t = (CicoAulItems*)m_aul[i].p();
882         if (t->m_pid == pid) {
883             bPushBack = false;  // push_back flag off
884             if (NULL == obj) {
885                 // Do not replace to NULL
886                 ICO_DBG("Do not replace to NULL %d", t->m_CSCWptrs.size());
887                 break; // break of for
888             }
889             else {
890                 ICO_DBG("add window info. %d, %x", t->m_CSCWptrs.size(), obj);
891                 t->enterWindow(obj);
892                 break; // break of for
893             }
894         }
895     }
896     if (true == bPushBack) { // push back flag on?
897         // AUL list Unregistered
898         ICO_DBG("push_back(AUL(%s, %d, %d, %x))", appid, pid, aulstt, obj);
899         int ctgry = DINITm_categoryID;
900         const CicoAilItems* ailObj = findAIL(appid);
901         if ((NULL != ailObj) && (0 != ailObj)) {
902             ctgry = ailObj->m_categoryID;
903         }
904         m_aul.push_back(CicoAulItems(appid, pid, ctgry, aulstt, obj));
905     }
906     ICO_TRA("end");
907 }
908
909 /**
910  * @brief
911  */
912 bool CicoHSLifeCycleController::removeAUL(int pid)
913 {
914     ICO_TRA("start %d", pid);
915     bool r = false;
916     vector<CicoAulItems>::iterator it;
917     for(it = m_aul.begin(); it != m_aul.end(); ++it) {
918         if (pid == it->m_pid) {
919             ICO_DBG("erase appid=%s", it->m_appid.c_str());
920             m_aul.erase(it);
921             r = true;
922             break;
923         }
924     }
925     ICO_TRA("end %s", r? "true":"false");
926     return r;
927 }
928
929 /**
930  * @brief
931  * @param x CicoHSLifeCycleController class pointer
932  */
933 int CSCLCCapp_launch_handlerX(int pid, CicoHSLifeCycleController* x)
934 {
935     ICO_TRA("start %d, %x", pid, x);
936     if ((NULL == x) || (0 == x)) {
937         ICO_TRA("end fail(user data is NULL)");
938         return -1;
939     }
940     char appid[255];
941     memset(appid, 0, sizeof(appid));
942     int iR = Xaul_app_get_appid_bypid(pid, appid, sizeof(appid));
943     ICO_PRF("CHG_APP_STA   notice  app=%s, pid=%d, rval=%d", appid, pid, iR);
944     x->enterAUL(appid, pid, NULL, iR);
945     ICO_TRA("end success(%d, %s)", pid, appid);
946     return 0;
947 }
948
949 /**
950  * @brief applications die. callback function
951  * @param pid
952  * @param data user data
953  * @param x CicoHSLifeCycleController class pointer
954  */
955 int CSCLCCapp_dead_handlerX(int pid, CicoHSLifeCycleController* x)
956 {
957     ICO_TRA("start");
958     if ((NULL == x) || (0 == x)) {
959         ICO_TRA("end user data is NULL");
960         return -1;
961     }
962     if (false == x->removeAUL(pid)) {
963         ICO_TRA("end dead %d fail", pid);
964         return -1;
965     }
966     ICO_TRA("end dead %d success", pid);
967     return 0;
968 }
969
970 /**
971  * @brief AUL information list
972  * @return AUL information item list Container
973  */
974 const vector<CicoAulItems>& CicoHSLifeCycleController::getAulList()
975 {
976     int sz = m_aul.size();
977     for (int i = 0; i < sz; i++) {
978         CicoAulItems* t = (CicoAulItems*)m_aul[i].p();
979         t->update_appid();
980     }
981     return m_aul;
982 }
983 // vim: set expandtab ts=4 sw=4: