2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://floralicense.org/license/
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.
17 * @file PeriodChanger.cpp
18 * @author Yunchan Cho (yunchan.cho@samsung.com)
24 #include <Elementary.h>
25 #include <livebox-service.h>
26 #include <Core/Util/Log.h>
27 #include "PeriodChanger.h"
29 #define UPDATE_PERIOD_MIN 1800.0f
30 #define UPDATE_PERIOD_HOUR 60.0 * 60.0
31 #define U_(str) dgettext("web-provider", str)
35 Evas_Object* PeriodChanger::s_window = NULL;
36 bool PeriodChanger::s_checkPopup = false;
38 static const char * const TEXT_POPUP_TITLE = "IDS_CLOCK_BODY_UPDATE_INTERVAL";
39 static const char * const TEXT_POPUP_CANCEL_BUTTON = "IDS_ST_BUTTON_CANCEL";
40 static const char * const TEXT_POPUP_UPDATE_PERIOD_1 ="IDS_ST_BODY_1_HOUR";
41 static const char * const TEXT_POPUP_UPDATE_PERIOD_3 ="IDS_ST_BODY_3HOURS";
42 static const char * const TEXT_POPUP_UPDATE_PERIOD_6 ="IDS_ST_BODY_6_HOURS_TMO";
43 static const char * const TEXT_POPUP_UPDATE_PERIOD_12 ="IDS_ST_BODY_12_HOURS";
44 static const char * const TEXT_POPUP_UPDATE_PERIOD_NEVER ="IDS_ST_BODY_NEVER";
45 static const char * const MO_INSTALL_DIR = "/usr/share/res/locale";
46 static const char * const MO_PROJECT_NAME = "web-provider";
48 PeriodChanger::PeriodChanger(
49 std::string& boxId, std::string& instanceId,
50 float currentPeriod, float requestedPeriod)
52 , m_instanceId(instanceId)
53 , m_currentPeriod(currentPeriod)
54 , m_requestedPeriod(requestedPeriod)
60 PeriodChanger::~PeriodChanger()
65 bool PeriodChanger::change()
75 if (m_requestedPeriod < 0) {
81 if (m_requestedPeriod == 0) {
83 } else if (m_requestedPeriod > 0) {
84 if (m_requestedPeriod >= UPDATE_PERIOD_MIN) {
85 newPeriod = m_requestedPeriod;
87 newPeriod = UPDATE_PERIOD_MIN;
90 LogD("negative value can't be handled here");
94 // after selecting one among period list, the following should be executed
95 return requestToPlatform(newPeriod);
98 void PeriodChanger::showPeriodPopup()
102 Evas_Object* window = createWindow();
103 Evas_Object* periodList = elm_list_add(window);
107 LogD("failed to add elm_list_add");
109 elm_list_mode_set(periodList, ELM_LIST_EXPAND);
110 bindtextdomain(MO_PROJECT_NAME, MO_INSTALL_DIR);
112 // TODO Language ID should be used, not static string
113 for(int i = 0 ; i < sizeof(m_hour) / sizeof(PopupListData); i++) {
114 radio = elm_radio_add(periodList);
115 elm_radio_state_value_set(radio,
116 m_currentPeriod == m_hour[i].newPeriod ? EINA_FALSE : EINA_TRUE);
117 elm_list_item_append(periodList,
121 selectPeriodCallback, &m_hour[i]);
125 Evas_Object *popup = elm_popup_add(window);
127 LogD("failed to add elm_popup_add");
130 elm_object_style_set(popup, "popup-item list");
131 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
132 elm_object_part_text_set(popup, "title,text", U_(TEXT_POPUP_TITLE));
133 elm_object_content_set(popup, periodList);
134 evas_object_show(popup);
135 Evas_Object* cancelButton = elm_button_add(popup);
137 LogD("failed to add elm_button_add");
140 // TODO Language ID should be used, not static string
141 elm_object_text_set(cancelButton, U_(TEXT_POPUP_CANCEL_BUTTON));
142 elm_object_style_set(cancelButton, "popup_button/default");
143 elm_object_part_content_set(popup, "button1", cancelButton);
144 evas_object_show(cancelButton);
145 evas_object_smart_callback_add(cancelButton, "clicked", cancelButtonCallback, this);
148 void PeriodChanger::destroyPeriodPopup(Evas_Object *obj)
151 Evas_Object* parent = elm_object_parent_widget_get(obj);
152 Evas_Object* popup = elm_popup_add(parent);
154 const char* type = elm_object_widget_type_get(parent);
155 if (type && !strcmp(type, elm_object_widget_type_get(popup))) {
156 evas_object_del(parent);
159 parent = elm_object_parent_widget_get(parent);
161 evas_object_del(popup);
165 void PeriodChanger::setPopupListData()
169 m_hour[0].periodChanger = this;
170 m_hour[0].newPeriod = 1.0 * UPDATE_PERIOD_HOUR;
171 m_hour[0].period = U_(TEXT_POPUP_UPDATE_PERIOD_1);
173 m_hour[1].periodChanger = this;
174 m_hour[1].newPeriod = 3.0 * UPDATE_PERIOD_HOUR;
175 m_hour[1].period = U_(TEXT_POPUP_UPDATE_PERIOD_3);
177 m_hour[2].periodChanger = this;
178 m_hour[2].newPeriod = 6.0 * UPDATE_PERIOD_HOUR;
179 m_hour[2].period = U_(TEXT_POPUP_UPDATE_PERIOD_6);
181 m_hour[3].periodChanger = this;
182 m_hour[3].newPeriod = 12.0 * UPDATE_PERIOD_HOUR;
183 m_hour[3].period = U_(TEXT_POPUP_UPDATE_PERIOD_12);
185 m_hour[4].periodChanger = this;
186 m_hour[4].newPeriod = 0.0;
187 m_hour[4].period = U_(TEXT_POPUP_UPDATE_PERIOD_NEVER);
191 bool PeriodChanger::requestToPlatform(float newPeriod)
193 int ret = livebox_service_change_period(
194 m_boxId.c_str(), m_instanceId.c_str(), newPeriod);
197 LogD("during update period, error occurs");
201 LogD("Instance's period is set to %f", newPeriod);
206 Evas_Object* PeriodChanger::createWindow()
211 evas_object_show(s_window);
212 elm_win_raise(s_window);
216 s_window = elm_win_add(NULL, "web-provider-popup", ELM_WIN_BASIC);
217 elm_win_alpha_set(s_window, EINA_TRUE);
218 elm_win_title_set(s_window, "change update period");
219 elm_win_borderless_set(s_window, EINA_TRUE);
223 ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
224 evas_object_resize(s_window, width, height);
225 elm_win_indicator_mode_set(s_window, ELM_WIN_INDICATOR_SHOW);
227 evas_object_color_set(s_window, 255, 255, 255, 0);
228 evas_object_show(s_window);
232 void PeriodChanger::destroyWindow()
239 s_checkPopup = false;
240 evas_object_hide(s_window);
241 elm_win_lower(s_window);
244 void PeriodChanger::selectPeriodCallback(void *data, Evas_Object *obj, void *event_info)
247 PopupListData* popupData = static_cast<PopupListData*>(data);
249 LogD("Update period is set to %f", popupData->newPeriod);
250 popupData->periodChanger->requestToPlatform(popupData->newPeriod);
251 popupData->periodChanger->destroyPeriodPopup(obj);
254 void PeriodChanger::cancelButtonCallback(void *data, Evas_Object *obj, void *event_info)
257 PeriodChanger* This = static_cast<PeriodChanger*>(data);
258 This->destroyPeriodPopup(obj);