Remove trivial unnecessary build dependency
[apps/core/preloaded/taskmanager.git] / src / util.c
1 /*
2  *  Task Manager
3  *
4  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <Elementary.h>
20 #include <app_manager.h>
21 #include <app_manager_extension.h>
22 #include <app_control.h>
23 #include <app_common.h>
24 #include <stdbool.h>
25
26 #include "log.h"
27 #include "util.h"
28
29
30
31 Eina_Bool util_kill_app(const char *appid)
32 {
33         app_context_h context = NULL;
34         Eina_Bool ret = EINA_TRUE;
35
36         retv_if(!appid, EINA_FALSE);
37
38         ret = app_manager_get_app_context(appid, &context);
39         if (ret != APP_MANAGER_ERROR_NONE) {
40                 _E("fail to app_manager_get_app_context(%d)", ret);
41                 return EINA_FALSE;
42         }
43
44         ret = app_manager_terminate_app(context);
45         app_context_destroy(context);
46
47         if (ret != APP_MANAGER_ERROR_NONE) {
48                 _E("fail to terminate_app (%d)", ret);
49                 return EINA_FALSE;
50         }
51
52         _D("terminate app = %s", appid);
53
54         return ret;
55 }
56
57
58
59 Eina_Bool util_launch_app(const char *appid)
60 {
61         int ret;
62         bool running = false;
63
64         if (!appid || strlen(appid) == 0) {
65                  _E("Fail to launch, due to Null appid.");
66                 return EINA_FALSE;
67         }
68         _D("Launching: %s", appid);
69         ret = app_manager_is_running(appid, &running);
70         retv_if(APP_MANAGER_ERROR_NONE != ret, EINA_FALSE);
71
72         if (running) {
73                 _D("THE APP IS RUNNING");
74
75                 app_context_h context = NULL;
76                 ret = app_manager_get_app_context(appid, &context);
77                 if (ret != APP_MANAGER_ERROR_NONE) {
78                         _E("fail to app_manager_get_app_context(%d)", ret);
79                         return EINA_FALSE;
80                 }
81
82                 ret = app_manager_resume_app(context);
83                 app_context_destroy(context);
84
85                 if (ret != APP_MANAGER_ERROR_NONE) {
86                         _E("fail to app_manager_resume_app(%d)", ret);
87                         return EINA_FALSE;
88                 }
89
90         } else {
91                 _D("THE APP IS NOT RUNNING");
92
93                 app_control_h service = NULL;
94                 retv_if(APP_CONTROL_ERROR_NONE != app_control_create(&service), EINA_FALSE);
95                 retv_if(!service, EINA_FALSE);
96
97                 app_control_set_operation(service, APP_CONTROL_OPERATION_MAIN);
98                 app_control_set_app_id(service, appid);
99
100                 ret = app_control_send_launch_request(service, NULL, NULL);
101                 (void)app_control_destroy(service);
102                 retv_if(APP_CONTROL_ERROR_NONE != ret, EINA_FALSE);
103         }
104
105         return EINA_TRUE;
106 }
107
108 const char *util_get_file_path(app_subdir dir, const char *relative)
109 {
110         static char buf[PATH_MAX];
111         char *prefix;
112
113         switch (dir) {
114         case APP_DIR_DATA:
115                 prefix = app_get_data_path();
116                 break;
117         case APP_DIR_CACHE:
118                 prefix = app_get_cache_path();
119                 break;
120         case APP_DIR_RESOURCE:
121                 prefix = app_get_resource_path();
122                 break;
123         case APP_DIR_SHARED_DATA:
124                 prefix = app_get_shared_data_path();
125                 break;
126         case APP_DIR_SHARED_RESOURCE:
127                 prefix = app_get_shared_resource_path();
128                 break;
129         case APP_DIR_SHARED_TRUSTED:
130                 prefix = app_get_shared_trusted_path();
131                 break;
132         case APP_DIR_EXTERNAL_DATA:
133                 prefix = app_get_external_data_path();
134                 break;
135         case APP_DIR_EXTERNAL_CACHE:
136                 prefix = app_get_external_cache_path();
137                 break;
138         case APP_DIR_EXTERNAL_SHARED_DATA:
139                 prefix = app_get_external_shared_data_path();
140                 break;
141         default:
142                 _E("Not handled directory type.");
143                 return NULL;
144         }
145         size_t res = eina_file_path_join(buf, sizeof(buf), prefix, relative);
146         free(prefix);
147         if (res > sizeof(buf)) {
148                 _E("Path exceeded PATH_MAX");
149                 return NULL;
150         }
151
152         return &buf[0];
153 }
154
155 //End of file