Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / wrt-popup / wrt / popup-bin / renderer / popup_controller.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 /**
17  * @file    popup_controller.cpp
18  * @author  Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version 1.0
20  * @bref    Implementation file for popup controller
21  */
22
23 #include "popup_controller.h"
24 #include <stddef.h>
25 #include <dpl/assert.h>
26 #include <dpl/log/log.h>
27 #include <dpl/singleton_impl.h>
28
29 IMPLEMENT_SINGLETON(Wrt::Popup::Renderer::PopupController)
30
31 namespace Wrt {
32 namespace Popup {
33 namespace Renderer {
34 void CtrlPopup::SetTitle(const std::string &title)
35 {
36     Assert(m_popup);
37     m_popup->SetTitle(title);
38 }
39
40 void CtrlPopup::Append(PopupObject::IPopupObject *object)
41 {
42     Assert(m_popup);
43     m_popup->Append(object);
44 }
45
46 CtrlPopup::CtrlPopup(IPopupPtr popup) :
47     m_popup(popup),
48     m_callback()
49 {
50     Assert(m_popup);
51 }
52
53 CtrlPopup::~CtrlPopup()
54 {}
55
56 void CtrlPopup::EmitAnswer(const AnswerCallbackData & answer)
57 {
58     AnswerCallbackData l_answer = answer;
59     PopupAnswerEvent event(shared_from_this(), m_callback, l_answer);
60     DPL::Event::EventSupport<PopupAnswerEvent>::EmitEvent(event);
61 }
62
63 PopupController::PopupController() : m_canvas(NULL)
64 {}
65
66 CtrlPopupPtr PopupController::CreatePopup() const
67 {
68     return CtrlPopupPtr(
69                new CtrlPopup(PopupManagerSingleton::Instance().CreatePopup()));
70 }
71
72 void PopupController::OnEventReceived(const ShowPopupEventShort& event)
73 {
74     CtrlPopupPtr popup = event.GetArg0();
75     popup->m_callback = event.GetArg1();
76
77     //pass canvas from controller to manager
78     //canvas is not passed earlier because value wasn't set properly
79     PopupManagerSingleton::Instance().setExternalCanvas(getExternalCanvas());
80
81     PopupManagerSingleton::Instance().RunAsyncWithArgType(
82         popup->m_popup,
83         &PopupController::StaticOnAnswerReceived,
84         new CtrlPopupPtr(popup));
85 }
86
87 void PopupController::StaticOnAnswerReceived(const AnswerCallbackData & answer,
88                                              CtrlPopupPtr* popup)
89 {
90     Assert(popup != NULL);
91     (*popup)->EmitAnswer(answer);
92     delete popup; // Just SharedPtr is destroyed, not the popup itself
93 }
94
95 void PopupControllerUser::OnEventReceived(const PopupAnswerEvent& event)
96 {
97     //Here we are in a proper context to call the callback
98     PopupAnswerCallback answerCall = event.GetArg1();
99     AnswerCallbackData answer = event.GetArg2();
100     answerCall.Call(answer);
101     event.GetArg0()->DPL::Event::EventSupport<PopupAnswerEvent>::RemoveListener(
102         this);
103 }
104
105 void PopupControllerUser::ListenForAnswer(CtrlPopupPtr popup)
106 {
107     popup->DPL::Event::EventSupport<PopupAnswerEvent>::AddListener(this);
108 }
109 }
110 } //namespace Popup
111 } //namespace Wrt