tizen 2.3.1 release
[framework/appfw/pkgmgr-info.git] / src / pkgmgrinfo_client.c
1 /*
2  * pkgmgrinfo-client
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Junsuk Oh <junsuk77.oh@samsung.com>,
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <dlfcn.h>
25 #include <dirent.h>
26 #include <dbus/dbus.h>
27 #include <dbus/dbus-glib-lowlevel.h>
28
29 #include "pkgmgrinfo_private.h"
30 #include "pkgmgrinfo_debug.h"
31 #include "pkgmgr-info.h"
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36 #define LOG_TAG "PKGMGR_CLIENT"
37
38 #define SERVICE_NAME "org.tizen.system.deviced"
39 #define PATH_NAME "/Org/Tizen/System/DeviceD/Mmc"
40 #define INTERFACE_NAME "org.tizen.system.deviced.Mmc"
41 #define METHOD_NAME "RequestMountApp2ext"
42
43 static int __get_pkg_location(const char *pkgid)
44 {
45         retvm_if(pkgid == NULL, PMINFO_R_OK, "pkginfo handle is NULL");
46
47         FILE *fp = NULL;
48         char pkg_mmc_path[FILENAME_MAX] = { 0, };
49         snprintf(pkg_mmc_path, FILENAME_MAX, "%s%s", PKG_SD_PATH, pkgid);
50
51         /*check whether application is in external memory or not */
52         fp = fopen(pkg_mmc_path, "r");
53         if (fp != NULL) {
54                 fclose(fp);
55                 fp = NULL;
56                 return PMINFO_EXTERNAL_STORAGE;
57         }
58
59         return PMINFO_INTERNAL_STORAGE;
60 }
61
62 /* pkgmgrinfo client start*/
63 API pkgmgrinfo_client *pkgmgrinfo_client_new(pkgmgrinfo_client_type ctype)
64 {
65         char *errmsg = NULL;
66         void *pc = NULL;
67         void *handle = NULL;
68         pkgmgrinfo_client *(*__pkgmgr_client_new)(pkgmgrinfo_client_type ctype) = NULL;
69
70         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
71         retvm_if(!handle, NULL, "dlopen() failed. [%s]", dlerror());
72
73         __pkgmgr_client_new = dlsym(handle, "pkgmgr_client_new");
74         retvm_if(__pkgmgr_client_new == NULL, NULL, "__pkgmgr_client_new() failed");
75
76         errmsg = dlerror();
77         retvm_if(errmsg != NULL, NULL, "dlsym() failed. [%s]", errmsg);
78
79         pc = __pkgmgr_client_new(ctype);
80
81 //catch:
82 //      dlclose(handle);
83         return (pkgmgrinfo_client *) pc;
84 }
85
86 API int pkgmgrinfo_client_set_status_type(pkgmgrinfo_client *pc, int status_type)
87 {
88         int ret = 0;
89         char *errmsg = NULL;
90         void *handle = NULL;
91         int (*__pkgmgr_client_set_status_type)(pkgmgrinfo_client *pc, int status_type) = NULL;
92
93         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
94         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
95
96         __pkgmgr_client_set_status_type = dlsym(handle, "pkgmgr_client_set_status_type");
97         errmsg = dlerror();
98         tryvm_if((errmsg != NULL) || (__pkgmgr_client_set_status_type == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
99
100         ret = __pkgmgr_client_set_status_type(pc, status_type);
101         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
102
103 catch:
104 //      dlclose(handle);
105         return ret;
106 }
107
108 API int pkgmgrinfo_client_listen_status(pkgmgrinfo_client *pc, pkgmgrinfo_handler event_cb, void *data)
109 {
110         int ret = 0;
111         char *errmsg = NULL;
112         void *handle = NULL;
113         int (*__pkgmgr_client_listen_status)(pkgmgrinfo_client *pc, pkgmgrinfo_handler event_cb, void *data) = NULL;
114
115         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
116         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
117
118         __pkgmgr_client_listen_status = dlsym(handle, "pkgmgr_client_listen_status");
119         errmsg = dlerror();
120         tryvm_if((errmsg != NULL) || (__pkgmgr_client_listen_status == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
121
122         ret = __pkgmgr_client_listen_status(pc, event_cb, data);
123         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
124
125 catch:
126 //      dlclose(handle);
127         return ret;
128 }
129
130 API int pkgmgrinfo_client_free(pkgmgrinfo_client *pc)
131 {
132         int ret = 0;
133         char *errmsg = NULL;
134         void *handle = NULL;
135         int (*__pkgmgr_client_free)(pkgmgrinfo_client *pc) = NULL;
136
137         handle = dlopen("libpkgmgr-client.so.0", RTLD_LAZY | RTLD_GLOBAL);
138         retvm_if(!handle, PMINFO_R_ERROR, "dlopen() failed. [%s]", dlerror());
139
140         __pkgmgr_client_free = dlsym(handle, "pkgmgr_client_free");
141         errmsg = dlerror();
142         tryvm_if((errmsg != NULL) || (__pkgmgr_client_free == NULL), ret = PMINFO_R_ERROR, "dlsym() failed. [%s]", errmsg);
143
144         ret = __pkgmgr_client_free(pc);
145         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_client_new failed.");
146
147 catch:
148 //      dlclose(handle);
149         return ret;
150 }
151
152 API int pkgmgrinfo_client_request_enable_external_pkg(char *pkgid)
153 {
154         int ret = 0;
155         DBusConnection *bus;
156         DBusMessage *message;
157         DBusMessage *reply;
158
159         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
160
161         if(__get_pkg_location(pkgid) != PMINFO_EXTERNAL_STORAGE)
162                 return PMINFO_R_OK;
163
164         bus = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
165         retvm_if(bus == NULL, PMINFO_R_EINVAL, "dbus_bus_get() failed.");
166
167         message = dbus_message_new_method_call (SERVICE_NAME, PATH_NAME, INTERFACE_NAME, METHOD_NAME);
168         retvm_if(message == NULL, PMINFO_R_EINVAL, "dbus_message_new_method_call() failed.");
169
170         dbus_message_append_args(message, DBUS_TYPE_STRING, &pkgid, DBUS_TYPE_INVALID);
171
172         reply = dbus_connection_send_with_reply_and_block(bus, message, -1, NULL);
173         retvm_if(!reply, ret = PMINFO_R_EINVAL, "connection_send dbus fail");
174
175         dbus_connection_flush(bus);
176         dbus_message_unref(message);
177         dbus_message_unref(reply);
178
179         return PMINFO_R_OK;
180 }
181
182 /* pkgmgrinfo client end*/
183