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