Tizen 2.0 Release
[apps/core/preloaded/myfiles.git] / src / common / mf-s-beam.c
1 /*
2  * Copyright 2013         Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <nfc.h>
19 #include <app_service.h>
20 #include <vconf.h>
21
22 #include "mf-s-beam.h"
23 #include "mf-dlog.h"
24 #include "mf-util.h"
25
26 #define S_BEAM_SEPERATOR        "|"
27
28 typedef struct {
29         bool initialized;
30         bool enabled;
31
32         Mf_Sbeam_Cb callback;
33         void *user_data;
34 } mf_sbeam_s;
35
36 static mf_sbeam_s *g_sbeam_data = NULL;
37
38 bool __mf_sbeam_is_on()
39 {
40         bool ready = false;
41
42         int value = 0;
43         if (vconf_get_bool(VCONFKEY_NFC_SBEAM, &value) == 0) {
44                 if (value == VCONFKEY_NFC_SBEAM_ON)
45                         ready = true;
46                 else
47                         mf_debug("sbeam is off");
48         } else {
49                 mf_error("vconf_get_int()");
50         }
51
52         return ready;
53 }
54
55 static void
56 __mf_sbeam_target_discovered_cb(nfc_discovered_type_e type, nfc_p2p_target_h target, void *user_data)
57 {
58         MF_TRACE_BEGIN;
59         MF_CHECK(g_sbeam_data);
60         MF_CHECK(g_sbeam_data->enabled);
61
62         if (type == NFC_DISCOVERED_TYPE_ATTACHED && g_sbeam_data->callback)
63         {
64                 mf_debug("NFC_DISCOVERED_TYPE_ATTACHED");
65                 g_sbeam_data->callback(g_sbeam_data->user_data);
66         }
67         else if (type == NFC_DISCOVERED_TYPE_DETACHED)
68         {
69                 mf_debug("NFC_DISCOVERED_TYPE_DETACHED");
70         }
71 }
72
73
74 static bool
75 __mf_sbeam_nfc_mgr_init()
76 {
77         MF_CHECK_FALSE(g_sbeam_data);
78
79         int ret = 0;
80         if (!g_sbeam_data->initialized) {
81                 ret = nfc_manager_initialize(NULL, NULL);
82                 if (ret == NFC_ERROR_NONE) {
83                         mf_debug("NFC initializing is success. activating");
84                         g_sbeam_data->initialized = true;
85                 } else {
86                         mf_error("nfc_manager_initialize().. [0x%x]");
87                         return false;
88                 }
89         }
90
91         ret = nfc_manager_set_p2p_target_discovered_cb(__mf_sbeam_target_discovered_cb, g_sbeam_data);
92         if (ret != NFC_ERROR_NONE) {
93                 mf_debug("nfc_manager_set_p2p_target_discovered_cb()..[0x%x]", ret);
94                 return false;
95         }
96
97         return true;
98 }
99
100 static bool
101 __mf_sbeam_nfc_mgr_deinit()
102 {
103         MF_CHECK_FALSE(g_sbeam_data);
104
105         int ret = 0;
106         if (g_sbeam_data->initialized) {
107                 ret = nfc_manager_deinitialize();
108                 if (ret != NFC_ERROR_NONE) {
109                         mf_error("nfc_manager_deinitialize().. [0x%x]", ret);
110                         return false;
111                 }
112                 g_sbeam_data->initialized = false;
113         }
114
115         return true;
116 }
117
118 static void
119 __mf_sbeam_state_changed_cb(keynode_t *node, void *user_data)
120 {
121         MF_TRACE_BEGIN;
122         mf_sbeam_s *sbeam_data = user_data;
123         MF_CHECK(sbeam_data);
124
125         if (__mf_sbeam_is_on() && sbeam_data->enabled)
126                 __mf_sbeam_nfc_mgr_init();
127         else
128                 __mf_sbeam_nfc_mgr_deinit();
129 }
130
131 bool
132 mf_sbeam_init()
133 {
134         MF_TRACE_BEGIN;
135         g_sbeam_data = calloc(1, sizeof(mf_sbeam_s));
136         MF_CHECK_VAL(g_sbeam_data, false);
137
138         if (vconf_notify_key_changed(VCONFKEY_NFC_SBEAM, __mf_sbeam_state_changed_cb, g_sbeam_data) < 0) {
139                 mf_error("fail to vconf_notify_key_changed()");
140                 mf_sbeam_finalize();
141                 return false;
142         }
143
144         if (__mf_sbeam_is_on())
145                 mf_sbeam_enable();
146
147         return true;
148 }
149
150 void
151 mf_sbeam_finalize()
152 {
153         MF_TRACE_BEGIN;
154         mf_sbeam_disable();
155         SAFE_FREE_CHAR(g_sbeam_data);
156 }
157
158 bool
159 mf_sbeam_enable()
160 {
161         MF_TRACE_BEGIN;
162         MF_CHECK_FALSE(g_sbeam_data);
163
164         g_sbeam_data->enabled = true;
165
166         return __mf_sbeam_nfc_mgr_init();
167 }
168 bool mf_sbeam_disable()
169 {
170         MF_TRACE_BEGIN;
171         MF_CHECK_FALSE(g_sbeam_data);
172
173         g_sbeam_data->enabled = false;
174
175         return __mf_sbeam_nfc_mgr_deinit();
176 }
177
178 bool mf_sbeam_set_callback(Mf_Sbeam_Cb callback, void *user_data)
179 {
180         MF_TRACE_BEGIN;
181         MF_CHECK_FALSE(g_sbeam_data);
182
183         g_sbeam_data->callback = callback;
184         g_sbeam_data->user_data = user_data;
185
186         return true;
187 }
188
189 bool mf_sbeam_share_files(Evas_Object *win, Eina_List *file_list)
190 {
191         MF_CHECK_FALSE(win);
192
193         int ret = 0;
194         service_h svc_handle;
195         ret = service_create(&svc_handle);
196         if (ret != SERVICE_ERROR_NONE) {
197                 mf_error("service_create().. [0x%x]", ret);
198                 goto exception;
199         }
200
201         ret = service_set_operation(svc_handle, "http://tizen.org/appcontrol/operation/nfc_sbeam_send");
202         if (ret != SERVICE_ERROR_NONE) {
203                 mf_error("service_set_operation().. [0x%x]", ret);
204                 goto exception;
205         }
206
207         ret = service_set_mime(svc_handle, "text/DirectShareFile");
208         if (ret != SERVICE_ERROR_NONE) {
209                 mf_error("service_set_mime().. [0x%x]", ret);
210                 goto exception;
211         }
212         ret = service_set_window(svc_handle, elm_win_xwindow_get(win));
213         if (ret != SERVICE_ERROR_NONE) {
214                 mf_error("service_set_window().. [0x%x]", ret);
215                 goto exception;
216         }
217
218         char *uri = NULL;
219         if (file_list) {
220                 Eina_List *current = file_list;
221                 Eina_List *l = NULL;
222                 GString *pNode = NULL;
223
224
225                 EINA_LIST_FOREACH(current, l, pNode) {
226                         if (pNode != NULL) {
227                                 if (uri == NULL) {
228                                         uri = g_strconcat(pNode->str, NULL);
229                                 } else {
230                                         gchar *temp = uri;
231                                         uri = g_strconcat(uri, S_BEAM_SEPERATOR, pNode->str, NULL);
232                                         g_free(temp);
233                                 }
234                         }
235
236                 }
237         }
238         mf_debug("files = [%s]", uri);
239         ret = service_set_uri(svc_handle, uri);
240         if (ret != SERVICE_ERROR_NONE) {
241                 mf_error("service_set_uri().. [0x%x]", ret);
242                 goto exception;
243         }
244         SAFE_FREE_CHAR(uri);
245
246
247         ret = service_send_launch_request(svc_handle, NULL, NULL);
248         if (ret != SERVICE_ERROR_NONE) {
249                 mf_error("service_send_launch_request().. [0x%x]", ret);
250                 goto exception;
251         }
252
253         service_destroy(svc_handle);
254
255         return true;
256
257 exception:
258         service_destroy(svc_handle);
259         svc_handle = NULL;
260
261         return false;
262 }
263