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