Initialize Tizen 2.3
[framework/web/wrt.git] / src_mobile / wrt-client / client_submode_support.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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       client_submode_support.cpp
18  * @author     Jihoon Chung (jihoon.chung@samsung.com)
19  * @version    1.0
20  */
21
22 #include "client_submode_support.h"
23
24 #include <memory>
25 #include <sstream>
26 #include <Ecore.h>
27 #include <Elementary.h>
28 #include <bundle.h>
29
30 #include <dpl/assert.h>
31 #include <dpl/log/secure_log.h>
32 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
33
34 #include <application_data.h>
35
36 namespace ClientModule {
37 namespace {
38 const unsigned int EMPTY = 0;
39 const unsigned int INLINE_MODE = 1;
40 const unsigned int TRANSIENT_WINDOW = 1 << 1;
41 }
42
43  //Implementation class
44 class SubmodeSupportImplementation
45 {
46   private:
47     bool m_initialized;
48     WrtDB::TizenAppId m_appId;
49     unsigned int m_mode;
50
51     void setMode(const int mode)
52     {
53         m_mode |= mode;
54     }
55
56     bool getMode(const int mode)
57     {
58         return m_mode & mode;
59     }
60
61     static Eina_Bool destoryCallback(void* data, int /*type*/, void* event)
62     {
63         _D("called");
64         Ecore_X_Window callerId = reinterpret_cast<Ecore_X_Window>(data);
65
66         Assert(event);
67         Ecore_X_Event_Window_Hide* ev =
68             static_cast<Ecore_X_Event_Window_Hide*>(event);
69
70         if(ev->win == callerId) {
71             elm_exit();
72         }
73         return ECORE_CALLBACK_CANCEL;
74     }
75
76   public:
77     SubmodeSupportImplementation() :
78         m_initialized(false),
79         m_mode(EMPTY)
80     {
81     }
82
83     void initialize(WrtDB::TizenAppId appId)
84     {
85         _D("called");
86
87         m_appId = appId;
88         WrtDB::WidgetDAOReadOnly dao(m_appId);
89         WrtDB::WidgetAppControlList widgetApplicationControlList;
90         dao.getAppControlList(widgetApplicationControlList);
91         FOREACH(it, widgetApplicationControlList) {
92             if (it->disposition ==
93                 WrtDB::WidgetAppControl::Disposition::INLINE)
94             {
95                 _D("disposition");
96                 setMode(INLINE_MODE);
97             }
98         }
99
100         m_initialized = true;
101     }
102
103     void deinitialize(void)
104     {
105         _D("called");
106         m_initialized = false;
107     }
108
109     bool isInlineMode(void)
110     {
111         return getMode(INLINE_MODE);
112     }
113
114     bool isNeedTerminateOnSuspend(void)
115     {
116         if (isInlineMode()) {
117             return !getMode(TRANSIENT_WINDOW);
118         }
119         return false;
120     }
121
122     bool transientWindow(Ecore_X_Window calleeId)
123     {
124         _D("called");
125         if (!m_initialized) {
126             _E("not initialized");
127             return false;
128         }
129
130         bundle* b = ApplicationDataSingleton::Instance().getBundle();
131         if (!b) {
132             _W("Service data is empty");
133             return false;
134         }
135         const char* callerIdPtr = bundle_get_val(b, "__APP_SVC_K_WIN_ID__");
136         if (callerIdPtr) {
137             Ecore_X_Window callerId = atoi(callerIdPtr);
138             _D("Caller x handle = %u", callerId);
139             ecore_x_icccm_transient_for_set(calleeId, callerId);
140             ecore_x_window_client_manage(callerId);
141             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_DESTROY,
142                                     destoryCallback,
143                                     reinterpret_cast<void*>(callerId));
144             setMode(TRANSIENT_WINDOW);
145         } else {
146             _W("Service data is empty");
147             return false;
148         }
149
150         return true;
151     }
152 };
153
154 SubmodeSupport::SubmodeSupport() :
155     m_impl(new SubmodeSupportImplementation())
156 {
157 }
158
159 SubmodeSupport::~SubmodeSupport()
160 {
161 }
162
163 void SubmodeSupport::initialize(WrtDB::TizenAppId appId)
164 {
165     m_impl->initialize(appId);
166 }
167
168 void SubmodeSupport::deinitialize(void)
169 {
170     m_impl->deinitialize();
171 }
172
173 bool SubmodeSupport::isInlineMode(void)
174 {
175     return m_impl->isInlineMode();
176 }
177
178 bool SubmodeSupport::isNeedTerminateOnSuspend(void)
179 {
180     return m_impl->isNeedTerminateOnSuspend();
181 }
182
183 bool SubmodeSupport::transientWindow(Ecore_X_Window calleeId)
184 {
185     return m_impl->transientWindow(calleeId);
186 }
187 } // namespace ClientModule