85c9d4303f92aeda12a57b26392c52f4b6da1d95
[framework/api/application.git] / src / app_resource.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <bundle.h>
27 #include <appcore-common.h>
28 #include <appcore-efl.h>
29 #include <aul.h>
30 #include <dlog.h>
31 #include <ail.h>
32 #include <Elementary.h>
33
34 #include <app_private.h>
35 #include <app_service_private.h>
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #endif
40
41 #define LOG_TAG "CAPI_APPFW_APPLICATION"
42
43 static const char *INSTALLED_PATH = "/opt/usr/apps";
44 static const char *RO_INSTALLED_PATH = "/usr/apps";
45 static const char *RES_DIRECTORY_NAME = "res";
46 static const char *DATA_DIRECTORY_NAME = "data";
47
48 static char * app_get_root_directory(char *buffer, int size)
49 {
50         char *appid = NULL;
51         char root_directory[TIZEN_PATH_MAX] = {0, };
52         char bin_directory[TIZEN_PATH_MAX] = {0, };
53         ail_appinfo_h ail_app_info;
54         char *pkgid;
55
56         if (app_get_id(&appid) != APP_ERROR_NONE)
57         {
58                 app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the appid");
59                 return NULL;
60         }
61
62         if (ail_get_appinfo(appid, &ail_app_info) != AIL_ERROR_OK)
63         {
64                 app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the appinfo");
65                 free(appid);
66                 return NULL;
67         }
68
69         if (ail_appinfo_get_str(ail_app_info, AIL_PROP_X_SLP_PKGID_STR, &pkgid) != AIL_ERROR_OK)
70         {
71                 app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the pkgid");
72                 free(appid);
73                 ail_destroy_appinfo(ail_app_info);
74                 return NULL;
75         }
76
77         if(pkgid)
78         {
79                 free(appid);
80                 appid = strdup(pkgid);
81         }
82
83         ail_destroy_appinfo(ail_app_info);
84
85         snprintf(root_directory, sizeof(root_directory), "%s/%s", INSTALLED_PATH, appid);
86         snprintf(bin_directory, sizeof(bin_directory), "%s/bin", root_directory);
87
88         if (access(bin_directory, R_OK) != 0) {
89                 snprintf(root_directory, sizeof(root_directory), "%s/%s", RO_INSTALLED_PATH, appid);
90         }
91
92         free(appid);
93
94         if (size < strlen(root_directory)+1)
95         {
96                 app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "the buffer is not big enough");
97                 return NULL;
98         }
99
100         snprintf(buffer, size, "%s", root_directory);
101
102         return buffer;
103 }
104
105 static char* app_get_resource_directory(char *buffer, int size)
106 {
107         char root_directory[TIZEN_PATH_MAX] = {0, };
108         char resource_directory[TIZEN_PATH_MAX] = {0, };
109
110         if (app_get_root_directory(root_directory, sizeof(root_directory)) == NULL)
111         {
112                 app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the root directory of the application");
113                 return NULL;
114         }
115
116         snprintf(resource_directory, sizeof(resource_directory), "%s/%s", root_directory, RES_DIRECTORY_NAME);
117
118         if (size < strlen(resource_directory) +1)
119         {
120                 app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "the buffer is not big enough");
121                 return NULL;
122         }
123
124         snprintf(buffer, size, "%s", resource_directory);
125
126         return buffer;
127 }
128
129 char* app_get_data_directory(char *buffer, int size)
130 {
131         static char data_directory[TIZEN_PATH_MAX] = {0, };
132         static int data_directory_length = 0;
133         ail_appinfo_h ail_app_info;
134         char *pkgid;
135
136         if (data_directory[0] == '\0')
137         {
138                 char *root_directory = NULL;
139                 char *appid = NULL;
140
141                 root_directory = calloc(1, TIZEN_PATH_MAX);
142
143                 if (root_directory == NULL)
144                 {
145                         app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
146                         return NULL;
147                 }
148
149                 if (app_get_id(&appid) != APP_ERROR_NONE)
150                 {
151                         app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package");
152                         free(root_directory);
153                         return NULL;
154                 }
155
156                 if (ail_get_appinfo(appid, &ail_app_info) != AIL_ERROR_OK)
157                 {
158                         app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package");
159                         free(root_directory);
160                         free(appid);
161                         return NULL;
162                 }
163
164                 if (ail_appinfo_get_str(ail_app_info, AIL_PROP_X_SLP_PKGID_STR, &pkgid) != AIL_ERROR_OK)
165                 {
166                         app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package");
167                         free(root_directory);
168                         free(appid);
169                         ail_destroy_appinfo(ail_app_info);
170                         return NULL;
171                 }
172
173                 if(pkgid)
174                 {
175                         free(appid);
176                         appid = strdup(pkgid);
177                 }
178
179                 ail_destroy_appinfo(ail_app_info);
180
181                 snprintf(root_directory, TIZEN_PATH_MAX, "%s/%s", INSTALLED_PATH, appid);
182
183                 free(appid);
184
185                 snprintf(data_directory, sizeof(data_directory), "%s/%s", root_directory, DATA_DIRECTORY_NAME);
186
187                 data_directory_length = strlen(data_directory);
188
189                 free(root_directory);
190         }
191
192         if (size < data_directory_length+1)
193         {
194                 app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "the buffer is not big enough");
195                 return NULL;
196         }
197
198         snprintf(buffer, size, "%s", data_directory);
199
200         return buffer;
201 }
202
203 char* app_get_resource(const char *resource, char *buffer, int size)
204 {
205         static char resource_directory[TIZEN_PATH_MAX] = {0, };
206         static int resource_directory_length = 0;
207
208         int resource_path_length = 0;
209
210         if (resource == NULL)
211         {
212                 app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
213                 return NULL;
214         }
215
216         if (buffer == NULL || size <= 0)
217         {
218                 app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
219                 return NULL;
220         }
221
222         if (resource_directory[0] == '\0')
223         {
224                 if (app_get_resource_directory(resource_directory, sizeof(resource_directory)) == NULL)
225                 {
226                         app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the path to the resource directory");
227                         return NULL;
228                 }
229
230                 resource_directory_length = strlen(resource_directory);
231         }
232
233         resource_path_length = resource_directory_length + strlen("/") + strlen(resource);
234
235         if (size < resource_path_length+1)
236         {
237                 app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "the buffer is not big enough");
238                 return NULL;
239         }
240
241         snprintf(buffer, size, "%s/%s", resource_directory, resource);
242
243         return buffer;
244 }
245
246
247 void app_set_reclaiming_system_cache_on_pause(bool enable)
248 {
249         appcore_set_system_resource_reclaiming(enable);
250 }
251
252 void* app_get_preinitizlized_window(void)
253 {
254         Evas_Object* win = (Evas_Object*)aul_get_preinit_window();
255
256         return win;
257 }