2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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.
19 #include <internal/widget_view/widget_view_impl.h>
24 #include <dali/integration-api/debug.h>
26 #include <widget_service.h>
27 #include <widget_instance.h>
28 #include <tzplatform_config.h>
42 #define WIDGET_VIEW_RESOURCE_DEFAULT_IMG "/widget_viewer_dali/images/unknown.png"
44 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gWidgetViewLogging = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_WIDGET_VIEW" );
48 } // unnamed namespace
50 Dali::WidgetView::WidgetView WidgetView::New( const std::string& widgetId, const std::string& contentInfo, int width, int height, double updatePeriod )
52 // Create the implementation, temporarily owned on stack
53 IntrusivePtr< WidgetView > internalWidgetView = new WidgetView( widgetId, contentInfo, width, height, updatePeriod );
55 // Pass ownership to CustomActor
56 Dali::WidgetView::WidgetView widgetView( *internalWidgetView );
58 // Second-phase init of the implementation
59 // This can only be done after the CustomActor connection has been made...
60 internalWidgetView->Initialize();
65 WidgetView::WidgetView()
66 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
76 mPreviewEnabled( true ),
77 mStateTextEnabled( true ),
78 mPermanentDelete( true )
82 WidgetView::WidgetView( const std::string& widgetId, const std::string& contentInfo, int width, int height, double updatePeriod )
83 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
84 mWidgetId( widgetId ),
86 mContentInfo( contentInfo ),
92 mUpdatePeriod( updatePeriod ),
93 mPreviewEnabled( true ),
94 mStateTextEnabled( true ),
95 mPermanentDelete( true )
99 WidgetView::~WidgetView()
101 if( !mWidgetId.empty() && !mInstanceId.empty() )
103 widget_instance_terminate( mWidgetId.c_str(), mInstanceId.c_str() );
105 if( mPermanentDelete )
107 widget_instance_destroy( mWidgetId.c_str(), mInstanceId.c_str() );
113 bundle_free( mBundle );
117 const std::string& WidgetView::GetWidgetId() const
122 const std::string& WidgetView::GetInstanceId() const
127 const std::string& WidgetView::GetContentInfo()
129 widget_instance_h instance;
130 bundle* bundle = NULL;
131 bundle_raw* contentInfo = NULL;
132 int contentLength = 0;
134 mContentInfo.clear();
136 if( mWidgetId.empty() || mInstanceId.empty() )
138 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: Widget id (%s) or instance id (%s) is invalid.\n", mWidgetId.c_str(), mInstanceId.c_str() );
142 instance = widget_instance_get_instance( mWidgetId.c_str(), mInstanceId.c_str() );
145 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: widget_instance_get_instance is failed. [%s]\n", mInstanceId.c_str() );
149 if( widget_instance_get_content( instance, &bundle ) < 0 )
151 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: Failed to get content of widget. [%s]\n", mInstanceId.c_str() );
157 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: Cotent of widget [%s] is invalid.\n", mInstanceId.c_str() );
161 if( bundle_encode( bundle, &contentInfo, &contentLength ) < 0 )
163 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: bundle_encode is failed. [%s]\n", mInstanceId.c_str() );
167 mContentInfo = reinterpret_cast< char* >( contentInfo );
172 const std::string& WidgetView::GetTitle()
176 mTitle = mObjectView.GetTitle();
179 mTitle = widget_service_get_name( mWidgetId.c_str(), NULL );
183 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetTitle: title = %s\n", mTitle.c_str() );
188 double WidgetView::GetUpdatePeriod() const
190 return mUpdatePeriod;
193 void WidgetView::Show()
201 void WidgetView::Hide()
209 bool WidgetView::CancelTouchEvent()
213 return mObjectView.CancelTouchEvent();
219 void WidgetView::SetPreviewEnabled( bool enabled )
221 mPreviewEnabled = enabled;
225 mPreviewImage.SetVisible( enabled );
229 bool WidgetView::GetPreviewEnabled() const
231 return mPreviewEnabled;
234 void WidgetView::SetStateTextEnabled( bool enabled )
236 mStateTextEnabled = enabled;
240 mStateText.SetVisible( enabled );
244 bool WidgetView::GetStateTextEnabled() const
246 return mStateTextEnabled;
249 void WidgetView::ActivateFaultedWidget()
253 // Esable preview and text
254 if( mPreviewEnabled )
256 mPreviewImage.SetVisible( true );
259 if( mStateTextEnabled )
261 mStateText.SetVisible( true );
264 // launch widget again
265 mPid = widget_instance_launch( mWidgetId.c_str(), mInstanceId.c_str(), mBundle, mWidth, mHeight );
268 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
272 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
276 bool WidgetView::IsWidgetFaulted()
278 return mPid < 0 ? true : false;
281 void WidgetView::SetPermanentDelete( bool permanentDelete )
283 mPermanentDelete = permanentDelete;
286 void WidgetView::AddObjectView( Pepper::ObjectView objectView )
288 mObjectView = objectView;
290 mObjectView.SetParentOrigin( ParentOrigin::CENTER );
291 mObjectView.SetAnchorPoint( AnchorPoint::CENTER );
293 Self().Add( mObjectView );
295 // Disable preview and text
296 if( mPreviewEnabled )
298 mPreviewImage.SetVisible( false );
301 if( mStateTextEnabled )
303 mStateText.SetVisible( false );
307 Dali::WidgetView::WidgetView handle( GetOwner() );
308 mWidgetAddedSignal.Emit( handle );
310 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::AddObjectView: ObjectView is added.\n" );
313 void WidgetView::RemoveObjectView()
315 // Enable preview and text
316 if( mPreviewEnabled )
318 mPreviewImage.SetVisible( true );
321 if( mStateTextEnabled )
323 mStateText.SetVisible( true );
327 Dali::WidgetView::WidgetView handle( GetOwner() );
328 mWidgetDeletedSignal.Emit( handle );
332 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::RemoveObjectView: ObjectView is removed.\n" );
335 void WidgetView::SendWidgetEvent( int event )
337 Dali::WidgetView::WidgetView handle( GetOwner() );
339 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::SendWidgetEvent: event = %d widget = %s\n", event, mWidgetId.c_str() );
344 case WIDGET_INSTANCE_EVENT_UPDATE:
346 mWidgetContentUpdatedSignal.Emit( handle );
349 case WIDGET_INSTANCE_EVENT_PERIOD_CHANGED:
351 mWidgetUpdatePeriodChangedSignal.Emit( handle );
354 case WIDGET_INSTANCE_EVENT_SIZE_CHANGED:
356 mWidgetResizedSignal.Emit( handle );
359 case WIDGET_INSTANCE_EVENT_EXTRA_UPDATED:
361 mWidgetExtraInfoUpdatedSignal.Emit( handle );
371 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAddedSignal()
373 return mWidgetAddedSignal;
376 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetDeletedSignal()
378 return mWidgetDeletedSignal;
381 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAbortedSignal()
383 return mWidgetAbortedSignal;
386 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetResized()
388 return mWidgetResizedSignal;
391 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetContentUpdated()
393 return mWidgetContentUpdatedSignal;
396 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetExtraInfoUpdated()
398 return mWidgetExtraInfoUpdatedSignal;
401 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetUpdatePeriodChanged()
403 return mWidgetUpdatePeriodChangedSignal;
406 void WidgetView::OnInitialize()
408 char* instanceId = NULL;
409 char* previewPath = NULL;
410 std::string previewImage;
411 widget_size_type_e sizeType;
413 if( !mContentInfo.empty() )
415 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: decode bundle\n" );
417 mBundle = bundle_decode( reinterpret_cast< const bundle_raw* >( mContentInfo.c_str() ), mContentInfo.length() );
420 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: Invalid bundle data.\n" );
424 bundle_get_str( mBundle, WIDGET_K_INSTANCE, &instanceId );
429 int ret = widget_instance_create( mWidgetId.c_str(), &instanceId );
430 if( ret < 0 || !instanceId )
432 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is failed [%s].\n", mWidgetId.c_str() );
436 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is called. [widget id = %s, instance id = %s]\n",
437 mWidgetId.c_str(), instanceId );
440 mInstanceId = instanceId;
443 widget_service_get_size_type( mWidth, mHeight, &sizeType );
445 previewPath = widget_service_get_preview_image_path( mWidgetId.c_str(), sizeType );
448 previewImage = previewPath;
453 previewImage = tzplatform_getenv( TZ_SYS_SHARE );
454 previewImage.append( WIDGET_VIEW_RESOURCE_DEFAULT_IMG );
457 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: preview image path = %s\n", previewImage.c_str() );
459 mPreviewImage = Toolkit::ImageView::New( previewImage );
461 mPreviewImage.SetParentOrigin( ParentOrigin::CENTER );
462 mPreviewImage.SetAnchorPoint( AnchorPoint::CENTER );
466 mPreviewImage.SetSize( mWidth, mHeight );
469 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
470 Self().Add( mPreviewImage );
473 // TODO: use po files
474 mStateText = Toolkit::TextLabel::New( "Loading..." );
476 mStateText.SetParentOrigin( ParentOrigin::CENTER );
477 mStateText.SetAnchorPoint( AnchorPoint::CENTER );
478 mStateText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
479 mStateText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
481 mPreviewImage.Add( mStateText );
484 mPid = widget_instance_launch( mWidgetId.c_str(), instanceId, mBundle, mWidth, mHeight );
487 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
490 Dali::WidgetView::WidgetView handle( GetOwner() );
491 mWidgetAbortedSignal.Emit( handle );
496 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
499 } // namespace Internal
501 } // namespace WidgetView