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"
22 #include <dpl/log/log.h>
23 #include <dpl/assert.h>
24 #include <dpl/waitable_handle.h>
25 #include <dpl/binary_queue.h>
26 #include <dpl/serialization.h>
27 #include <dpl/exception.h>
30 #include "PopupEnum.h"
31 #include "PopupSerializer.h"
34 const char *POPUP_EXEC = "/usr/bin/wrt-popup-wrt-runtime";
39 PopupInvoker::PopupInvoker()
41 char tmp[L_tmpnam + 1];
42 if (NULL == tmpnam(tmp))
43 ThrowMsg(DPL::Exception, "Failed to get pipe name");
46 if (NULL == tmpnam(tmp))
47 ThrowMsg(DPL::Exception, "Failed to get pipe name");
50 m_input.Create(m_inputName);
51 m_output.Create(m_outputName);
52 LogDebug("Pipes created");
55 PopupInvoker::~PopupInvoker()
59 m_input.Destroy(m_inputName);
60 m_output.Destroy(m_outputName);
61 LogDebug("Pipes destroyed");
65 LogError("Cannot destroy pipes");
69 void PopupInvoker::showInfo(const std::string& title,
70 const std::string& message,
71 const std::string& buttonLabel)
76 DPL::BinaryQueue data;
77 PopupSerializer::appendArg(INFO_PROMPT, data);
78 PopupSerializer::appendArg(title, data);
79 PopupSerializer::appendArg(message, data);
80 PopupSerializer::appendArg(buttonLabel, data);
81 DPL::NamedInputPipe tmp;
82 tmp.Open(m_outputName);
83 m_output.Open(m_outputName);
84 m_input.Open(m_inputName);
85 m_output.Write(data, data.Size());
88 DPL::BinaryQueueAutoPtr resultData =
89 m_input.Read(std::numeric_limits<std::size_t>::max());
90 const int success = PopupSerializer::getIntArg(*resultData);
92 LogWarning("Failed to show popup.");
100 Catch(DPL::Exception)
102 LogError("error occured");
106 PopupResponse PopupInvoker::askYesNoCheckbox(const std::string& title,
107 const std::string& message,
108 const std::string& checkboxLabel)
112 DPL::BinaryQueue data;
113 PopupSerializer::appendArg(YES_NO_CHECK_PROMPT, data);
114 PopupSerializer::appendArg(title, data);
115 PopupSerializer::appendArg(message, data);
116 PopupSerializer::appendArg(checkboxLabel, 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());
125 //Result from popup application is available. Read it.
126 DPL::BinaryQueueAutoPtr resultData =
127 m_input.Read(std::numeric_limits<std::size_t>::max());
128 const int success = PopupSerializer::getIntArg(*resultData);
130 const int result = PopupSerializer::getIntArg(*resultData);
131 const int rememberResult = PopupSerializer::getIntArg(*resultData);
134 "Popup result is: " << result << " remeber: " << rememberResult);
136 Assert(resultData->Empty());
142 if (rememberResult == 1) {
143 return YES_DO_REMEMBER;
145 return YES_DONT_REMEMBER;
148 if (rememberResult == 1) {
149 return NO_DO_REMEMBER;
151 return NO_DONT_REMEMBER;
155 LogWarning("Popup failed to execute.");
159 return NO_DONT_REMEMBER;
162 Catch(DPL::Exception)
164 LogError("error occured");
166 return NO_DONT_REMEMBER;
169 void PopupInvoker::executePopup()
175 LogError("Failed to create popup process.");
180 int ret = execl(POPUP_EXEC,
182 m_outputName.c_str(),
186 //execl returns -1 on error
187 LogError("Failed to set popup binary");
188 //write something to pipe to unblock caller process
189 DPL::NamedOutputPipe errOut;
190 errOut.Open(m_inputName);
191 DPL::BinaryQueue data;
192 PopupSerializer::appendArg(false, data);
193 errOut.Write(data, data.Size());
200 DPL::WaitableHandle handle = m_input.WaitableReadHandle();
201 DPL::WaitForSingleHandle(handle);