Initialize Tizen 2.3
[framework/api/application.git] / app_common / app_package.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 <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21
22 #include <aul.h>
23 #include <ail.h>
24
25 #include <app_internal.h>
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "CAPI_APPFW_APPLICATION"
32
33 int app_get_package_app_name(const char *appid, char **name)
34 {
35         char *name_token = NULL;
36
37         if (appid == NULL)
38         {
39                 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
40         }
41
42         // com.vendor.name -> name
43         name_token = strrchr(appid, '.');
44
45         if (name_token == NULL)
46         {
47                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
48         }
49
50         name_token++;
51
52         *name = strdup(name_token);
53
54         if (*name == NULL)
55         {
56                 return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
57         }
58
59         return APP_ERROR_NONE;
60 }
61
62 int app_get_package(char **package)
63 {
64         return app_get_id(package);
65 }
66
67 int app_get_id(char **id)
68 {
69         static char id_buf[TIZEN_PATH_MAX] = {0, };
70         int ret = -1;
71
72         if (id == NULL)
73         {
74                 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
75         }
76
77         if (id_buf[0] == '\0')
78         {
79                 ret = aul_app_get_appid_bypid(getpid(), id_buf, sizeof(id_buf));
80                 if (ret < 0) {
81                         return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the application ID");
82                 }
83         }
84
85         if (id_buf[0] == '\0')
86         {
87                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the application ID");
88         }
89
90         *id = strdup(id_buf);
91
92         if (*id == NULL)
93         {
94                 return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
95         }
96
97         return APP_ERROR_NONE;
98 }
99
100 static int app_get_appinfo(const char *package, const char *property, char **value)
101 {
102         ail_appinfo_h appinfo;
103         char *appinfo_value;
104         char *appinfo_value_dup;
105
106         if (ail_get_appinfo(package, &appinfo) != 0)
107         {
108                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get app-info");
109         }
110
111         if (ail_appinfo_get_str(appinfo, property, &appinfo_value) != 0)
112         {
113                 ail_destroy_appinfo(appinfo);
114                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get app-property");
115         }
116
117         appinfo_value_dup = strdup(appinfo_value);
118
119         ail_destroy_appinfo(appinfo);
120
121         if (appinfo_value_dup == NULL)
122         {
123                 return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
124         }
125
126         *value = appinfo_value_dup;
127
128         return APP_ERROR_NONE;
129 }
130
131 int app_get_name(char **name)
132 {
133         char *package = NULL;
134         int retval;
135
136         if(name == NULL)
137         {
138                 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
139         }
140
141         if (app_get_id(&package) != 0)
142         {
143                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package");
144         }
145
146         retval = app_get_appinfo(package, AIL_PROP_NAME_STR, name);
147
148         if (package != NULL)
149         {
150                 free(package);
151         }
152
153         return retval;
154 }
155
156 int app_get_version(char **version)
157 {
158         char *package;
159         int retval;
160
161         if(version == NULL)
162         {
163                 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
164         }
165
166         if (app_get_id(&package) != 0)
167         {
168                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package");
169         }
170
171         retval = app_get_appinfo(package, AIL_PROP_VERSION_STR, version);
172
173         if (package != NULL)
174         {
175                 free(package);
176         }
177
178         return retval;
179 }
180