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>
25 #include <dpl/exception.h>
28 #include "PopupEnum.h"
29 #include "PopupSerializer.h"
32 const char *POPUP_EXEC = "/usr/bin/wrt-popup-wrt-runtime";
37 PopupInvoker::PopupInvoker()
39 char tmp[L_tmpnam + 1];
40 if (NULL == tmpnam(tmp))
41 ThrowMsg(DPL::Exception, "Failed to get pipe name");
44 if (NULL == tmpnam(tmp))
45 ThrowMsg(DPL::Exception, "Failed to get pipe name");
48 m_input.Create(m_inputName);
49 m_output.Create(m_outputName);
50 LogDebug("Pipes created");
53 PopupInvoker::~PopupInvoker()
57 m_input.Destroy(m_inputName);
58 m_output.Destroy(m_outputName);
59 LogDebug("Pipes destroyed");
63 LogError("Cannot destroy pipes");
67 bool PopupInvoker::askYesNo(const std::string& title,
68 const std::string& message)
72 DPL::BinaryQueue data;
73 PopupSerializer::appendArg(YES_NO_PROMPT, data);
74 PopupSerializer::appendArg(title, data);
75 PopupSerializer::appendArg(message, data);
76 DPL::NamedInputPipe tmp;
77 tmp.Open(m_outputName);
78 m_output.Open(m_outputName);
79 m_input.Open(m_inputName);
80 m_output.Write(data, data.Size());
84 //Result from popup application is available. Read it.
85 DPL::BinaryQueueAutoPtr resultData =
86 m_input.Read(std::numeric_limits<std::size_t>::max());
87 const int success = PopupSerializer::getIntArg(*resultData);
90 const int result = PopupSerializer::getIntArg(*resultData);
92 LogDebug("Popup result is: " << result);
94 Assert(resultData->Empty());
97 LogWarning("Failed to show popup.");
106 Catch(DPL::Exception)
108 LogError("error occured");
114 void PopupInvoker::showInfo(const std::string& title,
115 const std::string& message,
116 const std::string& buttonLabel)
120 DPL::BinaryQueue data;
121 PopupSerializer::appendArg(INFO_PROMPT, data);
122 PopupSerializer::appendArg(title, data);
123 PopupSerializer::appendArg(message, data);
124 PopupSerializer::appendArg(buttonLabel, data);
125 DPL::NamedInputPipe tmp;
126 tmp.Open(m_outputName);
127 m_output.Open(m_outputName);
128 m_input.Open(m_inputName);
129 m_output.Write(data, data.Size());
132 DPL::BinaryQueueAutoPtr resultData =
133 m_input.Read(std::numeric_limits<std::size_t>::max());
134 const int success = PopupSerializer::getIntArg(*resultData);
136 LogWarning("Failed to show popup.");
144 Catch(DPL::Exception)
146 LogError("error occured");
150 PopupResponse PopupInvoker::askYesNoCheckbox(const std::string& title,
151 const std::string& message,
152 const std::string& checkboxLabel)
156 DPL::BinaryQueue data;
157 PopupSerializer::appendArg(YES_NO_CHECK_PROMPT, data);
158 PopupSerializer::appendArg(title, data);
159 PopupSerializer::appendArg(message, data);
160 PopupSerializer::appendArg(checkboxLabel, data);
161 DPL::NamedInputPipe tmp;
162 tmp.Open(m_outputName);
163 m_output.Open(m_outputName);
164 m_input.Open(m_inputName);
165 m_output.Write(data, data.Size());
169 //Result from popup application is available. Read it.
170 DPL::BinaryQueueAutoPtr resultData =
171 m_input.Read(std::numeric_limits<std::size_t>::max());
172 const int success = PopupSerializer::getIntArg(*resultData);
174 const int result = PopupSerializer::getIntArg(*resultData);
175 const int rememberResult = PopupSerializer::getIntArg(*resultData);
178 "Popup result is: " << result << " remeber: " << rememberResult);
180 Assert(resultData->Empty());
186 if (rememberResult == 1) {
187 return YES_DO_REMEMBER;
189 return YES_DONT_REMEMBER;
192 if (rememberResult == 1) {
193 return NO_DO_REMEMBER;
195 return NO_DONT_REMEMBER;
199 LogWarning("Popup failed to execute.");
203 return NO_DONT_REMEMBER;
206 Catch(DPL::Exception)
208 LogError("error occured");
210 return NO_DONT_REMEMBER;
213 void PopupInvoker::executePopup()
218 LogError("Failed to create popup process.");
223 int ret = execl(POPUP_EXEC,
225 m_outputName.c_str(),
229 //execl returns -1 on error
230 LogError("Failed to set popup binary");
231 //write something to pipe to unblock caller process
232 DPL::NamedOutputPipe errOut;
233 errOut.Open(m_inputName);
234 DPL::BinaryQueue data;
235 PopupSerializer::appendArg(false, data);
236 errOut.Write(data, data.Size());
243 DPL::WaitableHandle handle = m_input.WaitableReadHandle();
244 DPL::WaitForSingleHandle(handle);