tizen 2.0beta
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-main.c
1 /*
2 *  WiFi-Direct UG
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 /**
21  * This file implements wifi direct application main functions.
22  *
23  * @file    wfd-app-main.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27
28 #include <libintl.h>
29 #include <appcore-efl.h>
30 #include <appsvc.h>
31 #include <app_service.h>
32
33 #include "wfd-app.h"
34 #include "wfd-app-util.h"
35
36 wfd_appdata_t *g_wfd_ad;
37
38
39 wfd_appdata_t *wfd_get_appdata()
40 {
41     return g_wfd_ad;
42 }
43
44 static void _win_del(void *data, Evas_Object * obj, void *event)
45 {
46     elm_exit();
47 }
48
49 static Evas_Object *_create_win(Evas_Object * parent, const char *name)
50 {
51     Evas_Object *eo;
52     int w, h;
53
54     /* eo = elm_win_add(parent, name, ELM_WIN_BASIC); */
55     eo = elm_win_add(NULL, name, ELM_WIN_NOTIFICATION);
56     if (eo)
57     {
58         elm_win_title_set(eo, name);
59         elm_win_borderless_set(eo, EINA_TRUE);
60         elm_win_alpha_set(eo, EINA_TRUE);
61         evas_object_smart_callback_add(eo, "delete,request", _win_del, NULL);
62         ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
63         evas_object_resize(eo, w, h);
64         evas_object_raise(eo);
65     }
66
67     return eo;
68 }
69
70
71 static int _app_create(void *data)
72 {
73     __WFD_APP_FUNC_ENTER__;
74
75     wfd_appdata_t *ad = wfd_get_appdata();
76
77     if (data == NULL)
78     {
79         WFD_APP_LOG(WFD_APP_LOG_LOW, "Incorrect parameter\n");
80         return -1;
81     }
82
83     bindtextdomain(LOCALE_FILE_NAME, LOCALEDIR);
84
85     ad->popup_data = (wfd_popup_t *) malloc(sizeof(wfd_popup_t));
86     if (!ad->popup_data)
87     {
88         WFD_APP_LOG(WFD_APP_LOG_ERROR, "malloc failed\n");
89         return -1;
90     }
91     memset(ad->popup_data, 0x0, sizeof(wfd_popup_t));
92
93     ad->win = _create_win(NULL, PACKAGE);
94     elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
95
96     int r;
97
98     if (!ecore_x_display_get())
99         return -1;
100
101     r = appcore_set_i18n(PACKAGE, NULL);
102     if (r != 0)
103     {
104         WFD_APP_LOG(WFD_APP_LOG_LOW, "appcore_set_i18n error\n");
105         return -1;
106     }
107
108     if (init_wfd_popup_client(ad) == FALSE)
109     {
110         WFD_APP_LOG(WFD_APP_LOG_ERROR, "init_wfd_popup_client error\n");
111         wfd_prepare_popup(WFD_POP_FAIL_INIT, NULL);
112     }
113
114     __WFD_APP_FUNC_EXIT__;
115
116     return 0;
117 }
118
119 static int _app_terminate(void *data)
120 {
121         __WFD_APP_FUNC_ENTER__;
122
123         if (data == NULL)
124         {
125                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
126                 return -1;
127         }
128
129         wfd_appdata_t *ad = (wfd_appdata_t *) data;
130
131         if (deinit_wfd_popup_client(ad) == FALSE)
132         {
133                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "deinit_wfd_popup_client error\n");
134         }
135         else
136         {
137                 if (ad->popup)
138                 {
139                         evas_object_del(ad->popup);
140                         ad->popup = NULL;
141                 }
142                 if (ad->win)
143                 {
144                         evas_object_del(ad->win);
145                         ad->win = NULL;
146                 }
147                 if (ad->discovered_peers)
148                 {
149                         free(ad->discovered_peers);
150                         ad->discovered_peers = NULL;
151                 }
152         }
153
154         __WFD_APP_FUNC_EXIT__;
155
156         return 0;
157 }
158
159 static int _app_pause(void *data)
160 {
161     __WFD_APP_FUNC_ENTER__;
162     __WFD_APP_FUNC_EXIT__;
163     return 0;
164 }
165
166 static int _app_resume(void *data)
167 {
168     __WFD_APP_FUNC_ENTER__;
169     __WFD_APP_FUNC_EXIT__;
170     return 0;
171 }
172
173 static int _app_reset(bundle * b, void *data)
174 {
175     __WFD_APP_FUNC_ENTER__;
176
177     wfd_appdata_t *ad = (wfd_appdata_t*) data;
178
179     if(b == NULL)
180     {
181         WFD_APP_LOG(WFD_APP_LOG_LOW,"Bundle is NULL");
182         return -1;
183     }
184     // From Notification
185     char *noti_type = NULL;
186     noti_type = (char*) appsvc_get_data(b, NOTIFICATION_BUNDLE_PARAM);
187     if (noti_type == NULL)
188     {
189         WFD_APP_LOG(WFD_APP_LOG_LOW,"Notification type is wrong.");
190         return -1;
191     }
192     WFD_APP_LOG(WFD_APP_LOG_LOW,"Notification type is [%s]", noti_type);
193     if (strncmp(noti_type, NOTIFICATION_BUNDLE_VALUE, strlen(NOTIFICATION_BUNDLE_PARAM))==0)
194     {
195         WFD_APP_LOG(WFD_APP_LOG_LOW,"Launch wifidirect-ugapp");
196         service_h service;
197         service_create(&service);
198         service_set_operation(service, SERVICE_OPERATION_DEFAULT);
199         service_set_package(service, "org.tizen.wifi-direct-ugapp");
200         service_send_launch_request(service, NULL, NULL);
201         service_destroy(service);
202     }
203
204
205     __WFD_APP_FUNC_EXIT__;
206     return 0;
207 }
208
209 int main(int argc, char *argv[])
210 {
211     wfd_appdata_t ad;
212     struct appcore_ops ops = {
213         .create = _app_create,
214         .terminate = _app_terminate,
215         .pause = _app_pause,
216         .resume = _app_resume,
217         .reset = _app_reset,
218     };
219
220     memset(&ad, 0x0, sizeof(wfd_appdata_t));
221     ops.data = &ad;
222     g_wfd_ad = &ad;
223
224     return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
225 }