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