2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 #include "PopupInvoker.h"
21 #include <dpl/log/log.h>
22 #include <dpl/waitable_handle.h>
23 #include <dpl/binary_queue.h>
24 #include <dpl/serialization.h>
27 #include "PopupEnum.h"
28 #include "PopupSerializer.h"
31 const char *POPUP_EXEC = "/usr/bin/wrt-popup-wrt-runtime";
37 PopupInvoker::PopupInvoker() :
38 m_inputName(tmpnam(NULL)),
39 m_outputName(tmpnam(NULL))
43 m_input.Create(m_inputName);
44 m_output.Create(m_outputName);
45 LogDebug("Pipes created");
47 Catch (DPL::Exception)
49 LogError("Cannot create pipes");
53 PopupInvoker::~PopupInvoker()
57 m_input.Destroy(m_inputName);
58 m_output.Destroy(m_outputName);
59 LogDebug("Pipes destroyed");
61 Catch (DPL::Exception)
63 LogError("Cannot destroy pipes");
67 bool PopupInvoker::askYesNo(const std::string& title, const std::string& message)
71 DPL::BinaryQueue data;
72 PopupSerializer::appendArg(YES_NO_PROMPT, data);
73 PopupSerializer::appendArg(title, data);
74 PopupSerializer::appendArg(message, data);
75 DPL::NamedInputPipe tmp;
76 tmp.Open(m_outputName);
77 m_output.Open(m_outputName);
78 m_input.Open(m_inputName);
79 m_output.Write(data, data.Size());
83 //Result from popup application is available. Read it.
84 DPL::BinaryQueueAutoPtr resultData =
85 m_input.Read(std::numeric_limits<std::size_t>::max());
86 const int result = PopupSerializer::getIntArg(*resultData);
88 LogDebug("Popup result is: " << result);
90 Assert(resultData->Empty());
100 LogError("error occured");
106 void PopupInvoker::showInfo(const std::string& title,
107 const std::string& message,
108 const std::string& buttonLabel)
112 DPL::BinaryQueue data;
113 PopupSerializer::appendArg(INFO_PROMPT, data);
114 PopupSerializer::appendArg(title, data);
115 PopupSerializer::appendArg(message, data);
116 PopupSerializer::appendArg(buttonLabel, data);
117 DPL::NamedInputPipe tmp;
118 tmp.Open(m_outputName);
119 m_output.Open(m_outputName);
120 m_input.Open(m_inputName);
121 m_output.Write(data, data.Size());
131 Catch(DPL::Exception)
133 LogError("error occured");
137 PopupResponse PopupInvoker::askYesNoCheckbox(const std::string& title,
138 const std::string& message, const std::string& checkboxLabel)
142 DPL::BinaryQueue data;
143 PopupSerializer::appendArg(YES_NO_CHECK_PROMPT, data);
144 PopupSerializer::appendArg(title, data);
145 PopupSerializer::appendArg(message, data);
146 PopupSerializer::appendArg(checkboxLabel, data);
147 DPL::NamedInputPipe tmp;
148 tmp.Open(m_outputName);
149 m_output.Open(m_outputName);
150 m_input.Open(m_inputName);
151 m_output.Write(data, data.Size());
155 //Result from popup application is available. Read it.
156 DPL::BinaryQueueAutoPtr resultData =
157 m_input.Read(std::numeric_limits<std::size_t>::max());
158 const int result = PopupSerializer::getIntArg(*resultData);
159 const int rememberResult = PopupSerializer::getIntArg(*resultData);
161 LogDebug("Popup result is: " << result << " remeber: " << rememberResult);
163 Assert(resultData->Empty());
169 if (rememberResult == 1) {
170 return YES_DO_REMEMBER;
172 return YES_DONT_REMEMBER;
175 if (rememberResult == 1) {
176 return NO_DO_REMEMBER;
178 return NO_DONT_REMEMBER;
182 Catch(DPL::Exception)
184 LogError("error occured");
186 return NO_DONT_REMEMBER;
189 void PopupInvoker::executePopup()
195 LogError("Cannot display popup!");
201 int ret = execl(POPUP_EXEC,
203 m_outputName.c_str(),
207 //execl returns -1 on error
208 LogError("Cannot display popup!");
213 DPL::WaitableHandle handle = m_input.WaitableReadHandle();
214 DPL::WaitForSingleHandle(handle);