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