The license change version 1.0 to version 1.1
[platform/framework/web/web-provider.git] / src / Core / Service / PeriodChanger.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.1 (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  * @file    PeriodChanger.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20
21 #include <string>
22 #include <Evas.h>
23 #include <Ecore_X.h>
24 #include <Elementary.h>
25 #include <livebox-service.h>
26 #include <Core/Util/Log.h>
27 #include "PeriodChanger.h"
28
29 #define UPDATE_PERIOD_MIN   60.0
30
31 namespace Service {
32
33 Evas_Object* PeriodChanger::s_window = NULL;
34
35 PeriodChanger::PeriodChanger(
36         std::string& boxId, std::string& instanceId,
37         float currentPeriod, float requestedPeriod)
38     : m_boxId(boxId)
39     , m_instanceId(instanceId)
40     , m_currentPeriod(currentPeriod)
41     , m_requestedPeriod(requestedPeriod)
42     , m_hour()
43 {
44     LogD("enter");
45 }
46
47 PeriodChanger::~PeriodChanger()
48 {
49     LogD("enter");
50 }
51
52 bool PeriodChanger::change()
53 {
54     LogD("enter");
55
56     if (m_requestedPeriod < 0) {
57         showPeriodPopup();
58         return true;
59     }
60
61     float newPeriod;
62     if (m_requestedPeriod == 0) {
63         newPeriod = 0.0;
64     } else if (m_requestedPeriod > 0) {
65         if (m_requestedPeriod > UPDATE_PERIOD_MIN) {
66             newPeriod = m_requestedPeriod;
67         } else {
68             newPeriod = UPDATE_PERIOD_MIN;
69         }
70     } else {
71         LogD("negative value can't be handled here");
72         newPeriod = 0.0;
73     }
74
75     // after selecting one among period list, the following should be executed
76     return requestToPlatform(newPeriod);
77 }
78
79 void PeriodChanger::showPeriodPopup()
80 {
81     LogD("enter");
82
83     Evas_Object* window = createWindow();
84     Evas_Object* periodList = elm_list_add(window);
85     Evas_Object* radio;
86
87     if (!periodList) {
88         LogD("failed to add elm_list_add");
89     }
90     elm_list_mode_set(periodList, ELM_LIST_EXPAND);
91
92     setPopupListData();
93     // TODO Language ID should be used, not static string
94     for(int i = 0 ; i < sizeof(m_hour) / sizeof(PopupListData); i++) {
95         radio = elm_radio_add(periodList);
96         elm_radio_state_value_set(radio,
97             m_currentPeriod == m_hour[i].newPeriod ? EINA_FALSE : EINA_TRUE);
98         elm_list_item_append(periodList,
99             m_hour[i].period,
100             radio,
101             NULL,
102             selectPeriodCallback, &m_hour[i]);
103     }
104
105     // create popup
106     Evas_Object *popup = elm_popup_add(window);
107     if (!popup) {
108         LogD("failed to add elm_popup_add");
109         return;
110     }
111     elm_object_style_set(popup, "popup-item list");
112     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
113     elm_object_part_text_set(popup, "title,text", "Update Period");
114     elm_object_content_set(popup, periodList);
115     evas_object_show(popup);
116     Evas_Object* cancelButton = elm_button_add(popup);
117     if (!cancelButton) {
118         LogD("failed to add elm_button_add");
119         return;
120     }
121     // TODO Language ID should be used, not static string
122     elm_object_text_set(cancelButton, "Cancel");
123     elm_object_style_set(cancelButton, "popup_button/default");
124     elm_object_part_content_set(popup, "button1", cancelButton);
125     evas_object_show(cancelButton);
126     evas_object_smart_callback_add(cancelButton, "clicked", cancelButtonCallback, this);
127 }
128
129 void PeriodChanger::destroyPeriodPopup(Evas_Object *obj)
130 {
131     LogD("enter");
132     Evas_Object* parent = elm_object_parent_widget_get(obj);
133     Evas_Object* popup = elm_popup_add(parent);
134     while (parent) {
135         const char* type = elm_object_widget_type_get(parent);
136         if (type && !strcmp(type, elm_object_widget_type_get(popup))) {
137             evas_object_del(parent);
138             break;
139         }
140         parent = elm_object_parent_widget_get(parent);
141     }
142     evas_object_del(popup);
143     destroyWindow();
144 }
145
146 void PeriodChanger::setPopupListData()
147 {
148     LogD("enter");
149
150     m_hour[0].periodChanger = this;
151     m_hour[0].newPeriod = 1.0 * 60.0 * 60.0;
152     m_hour[0].period = "1 hour";
153
154     m_hour[1].periodChanger = this;
155     m_hour[1].newPeriod = 3.0 * 60.0 * 60.0;
156     m_hour[1].period = "3 hour";
157
158     m_hour[2].periodChanger = this;
159     m_hour[2].newPeriod = 6.0 * 60.0 * 60.0;
160     m_hour[2].period = "6 hour";
161
162     m_hour[3].periodChanger = this;
163     m_hour[3].newPeriod = 12.0 * 60.0 * 60.0;
164     m_hour[3].period = "12 hour";
165
166     m_hour[4].periodChanger = this;
167     m_hour[4].newPeriod = 0.0;
168     m_hour[4].period = "Never";
169
170 }
171
172 bool PeriodChanger::requestToPlatform(float newPeriod)
173 {
174     int ret = livebox_service_change_period(
175                 m_boxId.c_str(), m_instanceId.c_str(), newPeriod);
176
177     if (ret < 0) {
178         LogD("during update period, error occurs");
179         return false;
180     }
181
182     LogD("Instance's period is set to %f", newPeriod);
183     return true;
184 }
185
186
187 Evas_Object* PeriodChanger::createWindow()
188 {
189     LogD("enter");
190
191     if (s_window) {
192         evas_object_show(s_window);
193         elm_win_raise(s_window);
194         return s_window;
195     }
196
197     s_window = elm_win_add(NULL, "web-provider-popup", ELM_WIN_BASIC);
198     elm_win_alpha_set(s_window, EINA_TRUE);
199     elm_win_title_set(s_window, "change update period");
200     elm_win_borderless_set(s_window, EINA_TRUE);
201
202     int width = 0;
203     int height = 0;
204     ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
205     evas_object_resize(s_window, width, height);
206     elm_win_indicator_mode_set(s_window, ELM_WIN_INDICATOR_SHOW);
207
208     evas_object_color_set(s_window, 255, 255, 255, 0);
209     evas_object_show(s_window);
210     return s_window;
211 }
212
213 void PeriodChanger::destroyWindow()
214 {
215     LogD("enter");
216
217     if (!s_window) {
218         return;
219     }
220     evas_object_hide(s_window);
221     elm_win_lower(s_window);
222 }
223
224 void PeriodChanger::selectPeriodCallback(void *data, Evas_Object *obj, void *event_info)
225 {
226     LogD("enter");
227     PopupListData* popupData = static_cast<PopupListData*>(data);
228
229     LogD("Update period is set to %f", popupData->newPeriod);
230     popupData->periodChanger->requestToPlatform(popupData->newPeriod);
231     popupData->periodChanger->destroyPeriodPopup(obj);
232 }
233
234 void PeriodChanger::cancelButtonCallback(void *data, Evas_Object *obj, void *event_info)
235 {
236     LogD("enter");
237     PeriodChanger* This = static_cast<PeriodChanger*>(data);
238     This->destroyPeriodPopup(obj);
239 }
240
241 } // Service