Fix compile error. Non initialized variable.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / popup / confirmation-popup-impl.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
18 // CLASS HEADER
19 #include "confirmation-popup-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <cstring>
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal
33 {
34
35 namespace
36 {
37
38 /*
39  * This struct is used to define all details required about a dynamically created signal.
40  */
41 struct ControlDetailType
42 {
43   const char* signalName;
44   const char* controlName;
45   const char* connectSignalPropertyName;
46 };
47
48 /* A table of all control details. These details are kept in one place for maintainability.
49  *  Name of the signal     | Name of the control  | Name of the property which lets the
50  *  the app-developer      | which will provide   | app developer choose which signal
51  *  can connect to.        | the signal.          | within the control to connect to.    */
52 const ControlDetailType ControlDetails[] = {
53   { "controlSignalOk",       "controlOk",           "connectSignalOkSelected"     },
54   { "controlSignalCancel",   "controlCancel",       "connectSignalCancelSelected" },
55 };
56 const unsigned int ControlDetailsCount = sizeof( ControlDetails ) / sizeof( ControlDetails[0] );
57
58 // To give sensible default behaviour to save the connect signal properties being set.
59 const char* const DEFAULT_CONNECT_SIGNAL_NAME = "clicked";
60
61 BaseHandle Create()
62 {
63   return Toolkit::ConfirmationPopup::New();
64 }
65
66 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ConfirmationPopup, Toolkit::Popup, Create )
67
68 DALI_PROPERTY_REGISTRATION( Toolkit, ConfirmationPopup, ControlDetails[0].connectSignalPropertyName, STRING, CONNECT_SIGNAL_OK_SELECTED     )
69 DALI_PROPERTY_REGISTRATION( Toolkit, ConfirmationPopup, ControlDetails[1].connectSignalPropertyName, STRING, CONNECT_SIGNAL_CANCEL_SELECTED )
70
71 // Note: We do not use the macros for signal registration as we do not want to redefine the signal name strings.
72 // We have predefined them for optimal signal name to control name lookup.
73 SignalConnectorType signalConnector1( typeRegistration, ControlDetails[0].signalName, &Toolkit::Internal::ConfirmationPopup::DoConnectSignal );
74 SignalConnectorType signalConnector2( typeRegistration, ControlDetails[1].signalName, &Toolkit::Internal::ConfirmationPopup::DoConnectSignal );
75
76 DALI_TYPE_REGISTRATION_END()
77
78 } // Unnamed namespace
79
80 Dali::Toolkit::ConfirmationPopup ConfirmationPopup::New()
81 {
82   // Create the implementation, temporarily owned on stack.
83   IntrusivePtr< ConfirmationPopup > internalConfirmationPopup = new ConfirmationPopup();
84
85   // Pass ownership to CustomActor
86   Dali::Toolkit::ConfirmationPopup confirmationPopup( *internalConfirmationPopup );
87
88   // Second-phase initialisation of the implementation.
89   // This can only be done after the CustomActor connection has been made...
90   internalConfirmationPopup->Initialize();
91
92   return confirmationPopup;
93 }
94
95 ConfirmationPopup::ConfirmationPopup()
96 : Toolkit::Internal::Popup()
97 {
98   mControlSignals.reserve( MAXIMUM_NUMBER_OF_CONTROLS );
99   mControlSignalNames[ Toolkit::ConfirmationPopup::CONTROL_OK ] = DEFAULT_CONNECT_SIGNAL_NAME;
100   mControlSignalNames[ Toolkit::ConfirmationPopup::CONTROL_CANCEL ] = DEFAULT_CONNECT_SIGNAL_NAME;
101 }
102
103 ConfirmationPopup::~ConfirmationPopup()
104 {
105   for( SignalContainerType::iterator i = mControlSignals.begin(); i != mControlSignals.end(); ++i )
106   {
107     delete ( i->second );
108   }
109   mControlSignals.clear();
110 }
111
112 void ConfirmationPopup::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
113 {
114   Toolkit::ConfirmationPopup popup = Toolkit::ConfirmationPopup::DownCast( Dali::BaseHandle( object ) );
115
116   if ( popup )
117   {
118     ConfirmationPopup& popupImpl( GetDerivedImplementation( popup ) );
119
120     switch ( propertyIndex )
121     {
122       case Toolkit::ConfirmationPopup::Property::CONNECT_SIGNAL_OK_SELECTED:
123       {
124         popupImpl.SetControlSignalName( Toolkit::ConfirmationPopup::CONTROL_OK, value.Get< std::string >() );
125         break;
126       }
127       case Toolkit::ConfirmationPopup::Property::CONNECT_SIGNAL_CANCEL_SELECTED:
128       {
129         popupImpl.SetControlSignalName( Toolkit::ConfirmationPopup::CONTROL_CANCEL, value.Get< std::string >() );
130         break;
131       }
132     }
133   }
134 }
135
136 Property::Value ConfirmationPopup::GetProperty( BaseObject* object, Property::Index propertyIndex )
137 {
138   Property::Value value;
139
140   Toolkit::ConfirmationPopup popup = Toolkit::ConfirmationPopup::DownCast( Dali::BaseHandle( object ) );
141
142   if ( popup )
143   {
144     ConfirmationPopup& popupImpl( GetDerivedImplementation( popup ) );
145
146     switch ( propertyIndex )
147     {
148       case Toolkit::ConfirmationPopup::Property::CONNECT_SIGNAL_OK_SELECTED:
149       {
150         value = popupImpl.GetControlSignalName( Toolkit::ConfirmationPopup::CONTROL_OK );
151         break;
152       }
153       case Toolkit::ConfirmationPopup::Property::CONNECT_SIGNAL_CANCEL_SELECTED:
154       {
155         value = popupImpl.GetControlSignalName( Toolkit::ConfirmationPopup::CONTROL_CANCEL );
156         break;
157       }
158     }
159   }
160
161   return value;
162 }
163
164 void ConfirmationPopup::SetControlSignalName( const unsigned int controlNumber, const std::string& signalName )
165 {
166   if( controlNumber < ControlDetailsCount )
167   {
168     mControlSignalNames[ controlNumber ] = signalName;
169   }
170 }
171
172 std::string ConfirmationPopup::GetControlSignalName( unsigned int controlNumber ) const
173 {
174   if( controlNumber < ControlDetailsCount )
175   {
176     return mControlSignalNames[ controlNumber ];
177   }
178
179   return "";
180 }
181
182 bool ConfirmationPopup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
183 {
184   Dali::BaseHandle handle( object );
185   Toolkit::ConfirmationPopup popup = Toolkit::ConfirmationPopup::DownCast( handle );
186
187   // Look up the requested signal, attempting to create it dynamically if it doesn't exist.
188   SignalDelegate* signalDelegate = Dali::Toolkit::GetDerivedImplementation( popup ).GetControlSignal( signalName );
189   if( signalDelegate )
190   {
191     // The signal delegate was created successfully, attempt to connect it to a callback if specified.
192     // If none is specified, the creation is still successful as the signal delegate can connect at a later time.
193     if( functor )
194     {
195       signalDelegate->Connect( tracker, functor );
196     }
197     return true;
198   }
199
200   // The signal could not be created.
201   return false;
202 }
203
204 SignalDelegate* ConfirmationPopup::GetControlSignal( const std::string& signalName )
205 {
206   // Check if the specified signal name already exists.
207   SignalContainerType::iterator end = mControlSignals.end();
208   for( SignalContainerType::iterator iter = mControlSignals.begin(); iter != end; ++iter )
209   {
210     // Find the first non-connected signal by matching signal name.
211     if( ( signalName == iter->first ) && ( !iter->second->IsConnected() ) )
212     {
213       // The requested signal (delegate) already exists, just return it.
214       return iter->second;
215     }
216   }
217
218   // The signal doesn't exist, or it does but it's already connected to something else.
219   // To make a new connection to an existing signal, we need a new delegate,
220   // as delegates house a signal connection functor each.
221   // Check the signal name is valid and if so create the signal dynamically.
222   for( unsigned int i = 0; i < ControlDetailsCount; ++i )
223   {
224     if( 0 == strcmp( signalName.c_str(), ControlDetails[ i ].signalName ) )
225     {
226       // The signal name is valid, check the respective actor to connect to exists.
227       Actor connectActor = Self().FindChildByName( ControlDetails[ i ].controlName );
228       if( connectActor )
229       {
230         // The actor exists, set up a signal delegate that will allow the application developer
231         // to connect the actor signal directly to their callback.
232         // Note: We don't use the GetControlSignalName() here for speedup, as we know the array bound is capped.
233         SignalDelegate* signalDelegate = new SignalDelegate( connectActor, mControlSignalNames[ i ] );
234
235         // Store the delegate with the signal name so we know what signals have been dynamically created so far.
236         mControlSignals.push_back( std::make_pair( signalName, signalDelegate ) );
237
238         // Return the delegate to allow connection to the newly created signal.
239         return signalDelegate;
240       }
241
242       // Signal name valid but could not connect to the control,
243       return NULL;
244     }
245   }
246
247   // Signal name was not found (invalid).
248   return NULL;
249 }
250
251
252 } // namespace Internal
253
254 } // namespace Toolkit
255
256 } // namespace Dali