Tizen 2.1 base
[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                 elm_win_title_set(eo, name);
58                 elm_win_borderless_set(eo, EINA_TRUE);
59                 elm_win_alpha_set(eo, EINA_TRUE);
60                 evas_object_smart_callback_add(eo, "delete,request", _win_del, NULL);
61                 ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
62                 evas_object_resize(eo, w, h);
63                 evas_object_raise(eo);
64         }
65
66         return eo;
67 }
68
69 static int _app_create(void *data)
70 {
71         __WDPOP_LOG_FUNC_ENTER__;
72         wfd_appdata_t *ad = wfd_get_appdata();
73
74         if (data == NULL) {
75                 WDPOP_LOGD( "Incorrect parameter\n");
76                 return -1;
77         }
78
79         bindtextdomain(LOCALE_FILE_NAME, LOCALEDIR);
80
81         ad->popup_data = (wfd_popup_t *) malloc(sizeof(wfd_popup_t));
82         if (!ad->popup_data) {
83                 WDPOP_LOGE("malloc failed\n");
84                 return -1;
85         }
86
87         memset(ad->popup_data, 0x0, sizeof(wfd_popup_t));
88         ad->win = _create_win(NULL, PACKAGE);
89         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
90
91         int r;
92
93         if (!ecore_x_display_get()) {
94                 return -1;
95         }
96
97         r = appcore_set_i18n(PACKAGE, NULL);
98         if (r != 0) {
99                 WDPOP_LOGD( "appcore_set_i18n error\n");
100                 return -1;
101         }
102
103         if (init_wfd_popup_client(ad) == FALSE) {
104                 WDPOP_LOGE("init_wfd_popup_client error\n");
105                 wfd_prepare_popup(WFD_POP_FAIL_INIT, NULL);
106         }
107
108         __WDPOP_LOG_FUNC_EXIT__;
109         return 0;
110 }
111
112 static int _app_terminate(void *data)
113 {
114         __WDPOP_LOG_FUNC_ENTER__;
115
116         if (data == NULL) {
117                 WDPOP_LOGE("Incorrect parameter\n");
118                 return -1;
119         }
120
121         wfd_appdata_t *ad = (wfd_appdata_t *) data;
122
123         if (deinit_wfd_popup_client(ad) == FALSE) {
124                 WDPOP_LOGE("deinit_wfd_popup_client error\n");
125         } else {
126                 if (ad->popup) {
127                         evas_object_del(ad->popup);
128                         ad->popup = NULL;
129                 }
130                 if (ad->win) {
131                         evas_object_del(ad->win);
132                         ad->win = NULL;
133                 }
134                 if (ad->discovered_peers) {
135                         free(ad->discovered_peers);
136                         ad->discovered_peers = NULL;
137                 }
138         }
139
140         __WDPOP_LOG_FUNC_EXIT__;
141         return 0;
142 }
143
144 static int _app_pause(void *data)
145 {
146         __WDPOP_LOG_FUNC_ENTER__;
147         __WDPOP_LOG_FUNC_EXIT__;
148         return 0;
149 }
150
151 static int _app_resume(void *data)
152 {
153         __WDPOP_LOG_FUNC_ENTER__;
154         __WDPOP_LOG_FUNC_EXIT__;
155         return 0;
156 }
157
158 static int _app_reset(bundle *b, void *data)
159 {
160         __WDPOP_LOG_FUNC_ENTER__;
161
162         if (b == NULL) {
163                 WDPOP_LOGD( "Bundle is NULL");
164                 return -1;
165         }
166
167         // From Notification
168         char *noti_type = NULL;
169         noti_type = (char *)appsvc_get_data(b, NOTIFICATION_BUNDLE_PARAM);
170
171         if (noti_type == NULL) {
172                 WDPOP_LOGD( "Notification type is wrong.");
173                 return -1;
174         }
175
176         WDPOP_LOGD( "Notification type is [%s]", noti_type);
177         if (strncmp(noti_type, NOTIFICATION_BUNDLE_VALUE, strlen(NOTIFICATION_BUNDLE_PARAM)) == 0) {
178                 WDPOP_LOGD( "Launch wifidirect-ugapp");
179                 service_h service;
180                 service_create(&service);
181                 service_set_operation(service, SERVICE_OPERATION_DEFAULT);
182                 service_set_package(service, "org.tizen.wifi-direct-ugapp");
183                 service_send_launch_request(service, NULL, NULL);
184                 service_destroy(service);
185         }
186
187         __WDPOP_LOG_FUNC_EXIT__;
188         return 0;
189 }
190
191 int main(int argc, char *argv[])
192 {
193         wfd_appdata_t ad;
194         struct appcore_ops ops = {
195                 .create = _app_create,
196                 .terminate = _app_terminate,
197                 .pause = _app_pause,
198                 .resume = _app_resume,
199                 .reset = _app_reset,
200         };
201
202         memset(&ad, 0x0, sizeof(wfd_appdata_t));
203         ops.data = &ad;
204         g_wfd_ad = &ad;
205
206         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
207 }