Add 3.0 APIs and sync APIs same as 2.4
[platform/core/convergence/service-adaptor.git] / common / ipc-server / sal_ipc_server_storage.c
1 /*
2  * Service Adaptor IPC Server
3  *
4  * Copyright (c) 2014 - 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
20 #include <stdint.h>
21 #include <glib.h>
22 #include <gio/gio.h>
23
24 #include "service_adaptor_errors.h"
25 #include "service_adaptor_internal.h"
26 #include "sal.h"
27 #include "sal_ipc_server.h"
28 #include "sal_ipc_server_storage.h"
29 #include "storage_adaptor.h"
30 #include "sal_service_storage.h"
31 #include "sal_service_storage_internal.h"
32
33 //******************************************************************************
34 //* Global variables and defines
35 //******************************************************************************
36
37 //******************************************************************************
38 //* Private interface
39 //******************************************************************************
40
41 //******************************************************************************
42 //* Private interface definition
43 //******************************************************************************
44
45 void _cloud_remove_file_cb(int result, cloud_file_h file, void *user_data)
46 {
47         SAL_FN_CALL;
48
49         ipc_reply_data_h reply = (ipc_reply_data_h) user_data;
50
51         int ipc_ret = SERVICE_ADAPTOR_ERROR_NONE;
52         char *ipc_msg = NULL;
53         GVariant *ipc_data = NULL;
54
55         GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE(file_list_type));
56
57         ipc_create_error_msg(ipc_ret, &ipc_msg);
58         ipc_data = g_variant_new(ipc_make_return_type(reply->type), file->is_dir, SAL_IPC_STR(file->dir_path), SAL_IPC_STR(file->local_path), SAL_IPC_STR(file->cloud_path), file->size, SAL_IPC_STR(file->operation), builder, ipc_ret, SAL_IPC_STR(ipc_msg));
59         g_dbus_method_invocation_return_value(reply->invocation, ipc_data);
60
61         SAL_FREE(ipc_msg);
62         ipc_free_reply_data(reply);
63         g_variant_builder_unref(builder);
64
65         SAL_FN_END;
66 }
67
68 int _get_cloud_file(GVariant *reply_info, service_storage_cloud_file_h *file)
69 {
70         SAL_FN_CALL;
71
72         service_storage_cloud_file_h cloud_file = (service_storage_cloud_file_h) g_malloc0(sizeof(service_storage_cloud_file_s));
73
74         int info_size = service_storage_cloud_file_s_type_length;
75         GVariant *info[info_size];
76         ipc_create_variant_info(reply_info, info_size, (GVariant ***) &info);
77
78         int idx = 0;
79         cloud_file->is_dir = g_variant_get_boolean(info[idx++]);
80         cloud_file->dir_path = ipc_insure_g_variant_dup_string(info[idx++]);
81         cloud_file->local_path = ipc_insure_g_variant_dup_string(info[idx++]);
82         cloud_file->cloud_path = ipc_insure_g_variant_dup_string(info[idx++]);
83         cloud_file->size = g_variant_get_uint64(info[idx++]);
84         cloud_file->operation = ipc_insure_g_variant_dup_string(info[idx++]);
85
86         ipc_destroy_variant_info(info, info_size);
87
88         *file = cloud_file;
89
90         return SERVICE_ADAPTOR_ERROR_NONE;
91 }
92
93 int _cloud_execute_operation(storage_plugin_h plugin, service_storage_cloud_file_h file, ipc_reply_data_h reply)
94 {
95         SAL_FN_CALL;
96
97         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
98         RETV_IF(NULL == plugin->cloud, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
99         RETV_IF(NULL == file, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
100
101         int ret = SERVICE_ADAPTOR_ERROR_NONE;
102
103         if (0 == strcmp(file->operation, SERVICE_STORAGE_CLOUD_REMOVE_FILE_URI))
104         {
105                 ret = plugin->cloud->cloud_remove_file(plugin, file->cloud_path, _cloud_remove_file_cb, reply);
106
107                 return ret;
108         }
109         else if (0 == strcmp(file->operation, SERVICE_STORAGE_CLOUD_DOWNLOAD_FILE_URI))
110         {
111                 return ret;
112         }
113         else if (0 == strcmp(file->operation, SERVICE_STORAGE_CLOUD_UPLOAD_FILE_URI))
114         {
115                 return ret;
116         }
117         else if (0 == strcmp(file->operation, SERVICE_STORAGE_CLOUD_DOWNLOAD_FILE_THUMBNAIL_URI))
118         {
119                 return ret;
120         }
121         else if (0 == strcmp(file->operation, SERVICE_STORAGE_CLOUD_GET_FILE_LIST_URI))
122         {
123                 return ret;
124         }
125
126         return SERVICE_ADAPTOR_ERROR_INTERNAL;
127 }
128
129 //******************************************************************************
130 //* Public interface definition
131 //******************************************************************************
132
133 API void service_storage_method_call(GDBusConnection *connection,
134                 const gchar *sender,
135                 const gchar *object_path,
136                 const gchar *interface_name,
137                 const gchar *method_name,
138                 GVariant *parameters,
139                 GDBusMethodInvocation *invocation,
140                 gpointer user_data)
141 {
142         SAL_FN_CALL;
143
144         int ipc_ret = SERVICE_ADAPTOR_ERROR_NONE;
145         char *ipc_msg = NULL;
146         char *ipc_type = NULL;
147         GVariant *ipc_data = NULL;
148
149         char *uri = NULL;
150
151         GVariant *in_parameters = g_variant_get_child_value(parameters, 0);
152
153         if (0 == g_strcmp0(method_name, DBUS_SERVICE_STORAGE_CLOUD_FILE_METHOD))
154         {
155                 int idx = 0;
156                 int size = service_storage_cloud_file_req_s_type_length;
157                 GVariant *req_info[size];
158
159                 ipc_create_variant_info(in_parameters, size, (GVariant ***) &req_info);
160
161                 char *uri = ipc_insure_g_variant_dup_string(req_info[idx++]);
162                 service_storage_cloud_file_h file = NULL;
163                 _get_cloud_file(req_info[idx++], &file);
164
165                 SAL_INFO("uri: %s", uri);
166
167                 ipc_ret = SERVICE_ADAPTOR_ERROR_INTERNAL;
168                 ipc_type = strdup(service_storage_cloud_file_res_s_type);
169
170                 sal_h sal = sal_get_handle();
171                 TRYVM_IF(NULL == sal, ipc_ret = SERVICE_ADAPTOR_ERROR_INTERNAL, "sal_get_handle() Failed");
172
173                 storage_plugin_h plugin = storage_adaptor_get_plugin(sal->storage, uri);
174
175                 ipc_reply_data_h reply = (ipc_reply_data_h) g_malloc0(sizeof(ipc_reply_data_s));
176                 reply->invocation = invocation;
177                 reply->type = strdup(ipc_type);
178
179                 ipc_ret = _cloud_execute_operation(plugin, file, reply);
180                 TRY_IF(SERVICE_ADAPTOR_ERROR_NONE == ipc_ret, "cloud_execute_operation() Request Successed");
181
182                 GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE(file_list_type));
183                 ipc_create_error_msg(ipc_ret, &ipc_msg);
184                 ipc_data = g_variant_new(ipc_make_return_type(ipc_type), false, "", "", 0, 0, builder, ipc_ret, SAL_IPC_STR(ipc_msg));
185
186                 g_variant_builder_unref(builder);
187                 ipc_destroy_variant_info(req_info, size);
188         }
189
190         g_dbus_method_invocation_return_value(invocation, ipc_data);
191
192 catch:
193         SAL_FREE(uri);
194         SAL_FREE(ipc_msg);
195         SAL_FREE(ipc_type);
196
197         SAL_FN_END;
198 }