Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / wrt-popup / wrt / popup-bin / wrt-popup.cpp
1 /*
2  * Copyright (c) 2011 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 #include "wrt-popup.h"
17 #include <limits>
18 #include <memory>
19
20 #include <aul.h>
21 #include <dpl/log/log.h>
22 #include <dpl/exception.h>
23 #include <dpl/assert.h>
24
25 #include "PopupEnum.h"
26 #include "PopupSerializer.h"
27 #include "YesNoPopup.h"
28 #include "InfoPopup.h"
29 #include "YesNoCheckPopup.h"
30
31 namespace Wrt {
32 namespace Popup {
33 bool WrtPopup::openPipes()
34 {
35     Try
36     {
37         if (m_argc != 3) {
38             LogError("Wrong arguments!");
39             return false;
40         }
41         m_input.Open(m_argv[1]);
42         //open output pipe
43         m_output.Open(m_argv[2]);
44
45         DPL::WaitableHandleWatchSupport::InheritedContext()->
46             AddWaitableHandleWatch(this,
47                                    m_input.WaitableReadHandle(),
48                                    DPL::WaitMode::Read);
49         m_pipesOpened = true;
50         return true;
51     }
52     Catch(DPL::Exception)
53     {
54         LogError("cannot open pipes");
55     }
56     return false;
57 }
58
59 void WrtPopup::closePipes()
60 {
61     Try
62     {
63         if (m_pipesOpened) {
64             DPL::WaitableHandleWatchSupport::InheritedContext()->
65                 RemoveWaitableHandleWatch(this,
66                                           m_input.WaitableReadHandle(),
67                                           DPL::WaitMode::Read);
68             m_input.Close();
69             m_output.Close();
70             m_pipesOpened = false;
71         }
72     }
73     Catch(DPL::Exception)
74     {
75         LogError("cannot close pipes");
76     }
77 }
78
79 void WrtPopup::OnEventReceived(const QuitEvent & /* event */)
80 {
81     LogDebug("Quiting");
82     closePipes();
83     Quit();
84 }
85
86 void WrtPopup::OnWaitableHandleEvent(DPL::WaitableHandle waitableHandle,
87                                      DPL::WaitMode::Type /*mode*/)
88 {
89     if (waitableHandle == m_input.WaitableReadHandle()) {
90         readInputData();
91     }
92 }
93
94 void WrtPopup::readInputData()
95 {
96     DPL::BinaryQueueAutoPtr data =
97         m_input.Read(std::numeric_limits<std::size_t>::max());
98     int popupType = PopupSerializer::getIntArg(*data);
99     LogDebug("popup type " << popupType);
100     switch (popupType) {
101     case YES_NO_PROMPT:
102         m_popup.reset(new YesNoPopup());
103         m_popup->show(data, this);
104         break;
105     case INFO_PROMPT:
106         m_popup.reset(new InfoPopup());
107         m_popup->show(data, this);
108         break;
109     case YES_NO_CHECK_PROMPT:
110         m_popup.reset(new YesNoCheckPopup());
111         m_popup->show(data, this);
112         break;
113     default:
114         Assert(false);
115     }
116 }
117
118 void WrtPopup::response(DPL::BinaryQueue result)
119 {
120     m_output.Write(result, result.Size());
121     PostEvent(QuitEvent());
122 }
123
124 void WrtPopup::OnStop()
125 {
126     LogDebug("On Stop");
127 }
128
129 void WrtPopup::OnCreate()
130 {
131     if (!openPipes()) {
132         PostEvent(QuitEvent());
133     }
134     LogDebug("On Create");
135 }
136
137 void WrtPopup::OnResume()
138 {
139     LogDebug("OnResume");
140 }
141
142 void WrtPopup::OnPause()
143 {
144     LogDebug("OnPause");
145 }
146
147 void WrtPopup::OnReset(bundle */*b*/)
148 {
149     LogDebug("OnReset");
150 }
151
152 void WrtPopup::OnTerminate()
153 {
154     LogDebug("Wrt Shutdown now");
155 }
156
157 WrtPopup::WrtPopup(int argc, char **argv) :
158     Application(argc, argv, "wrt-popup", false),
159     m_pipesOpened(false)
160 {
161     Touch();
162     Renderer::PopupControllerSingleton::Instance().Touch();
163     Renderer::PopupManagerSingleton::Instance().Initialize(
164         Renderer::PopupRendererPtr(new Renderer::PopupRenderer));
165     LogDebug("App Created");
166 }
167
168 WrtPopup::~WrtPopup()
169 {
170     Renderer::PopupManagerSingleton::Instance().Deinitialize();
171     LogDebug("App Finished");
172 }
173 }
174 }
175
176 int main(int argc, char *argv[])
177 {
178     UNHANDLED_EXCEPTION_HANDLER_BEGIN
179     {
180         // Output on stdout will be flushed after every newline character,
181         // even if it is redirected to a pipe. This is useful for running
182         // from a script and parsing output.
183         // (Standard behavior of stdlib is to use full buffering when
184         // redirected to a pipe, which means even after an end of line
185         // the output may not be flushed).
186         setlinebuf(stdout);
187
188         DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-POPUP");
189         Wrt::Popup::WrtPopup app(argc, argv);
190         int ret = app.Exec();
191         LogDebug("App returned: " << ret);
192         return ret;
193     }
194     UNHANDLED_EXCEPTION_HANDLER_END
195 }