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 period )
52 // Create the implementation, temporarily owned on stack
53 IntrusivePtr< WidgetView > internalWidgetView = new WidgetView( widgetId, contentInfo, width, height, period );
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 period )
83 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
84 mWidgetId( widgetId ),
86 mContentInfo( contentInfo ),
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::GetPeriod() const
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 );
311 void WidgetView::RemoveObjectView()
314 Dali::WidgetView::WidgetView handle( GetOwner() );
315 mWidgetDeletedSignal.Emit( handle );
320 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAddedSignal()
322 return mWidgetAddedSignal;
325 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetDeletedSignal()
327 return mWidgetDeletedSignal;
330 void WidgetView::OnInitialize()
332 char* instanceId = NULL;
333 char* previewPath = NULL;
334 std::string previewImage;
335 widget_size_type_e sizeType;
337 if( !mContentInfo.empty() )
339 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: decode bundle\n" );
341 mBundle = bundle_decode( reinterpret_cast< const bundle_raw* >( mContentInfo.c_str() ), mContentInfo.length() );
344 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: Invalid bundle data.\n" );
348 bundle_get_str( mBundle, WIDGET_K_INSTANCE, &instanceId );
353 int ret = widget_instance_create( mWidgetId.c_str(), &instanceId );
354 if( ret < 0 || !instanceId )
356 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is failed [%s].\n", mWidgetId.c_str() );
360 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is called. [widget id = %s, instance id = %s]\n",
361 mWidgetId.c_str(), instanceId );
364 mInstanceId = instanceId;
367 widget_service_get_size_type( mWidth, mHeight, &sizeType );
369 previewPath = widget_service_get_preview_image_path( mWidgetId.c_str(), sizeType );
372 previewImage = previewPath;
377 previewImage = tzplatform_getenv( TZ_SYS_SHARE );
378 previewImage.append( WIDGET_VIEW_RESOURCE_DEFAULT_IMG );
381 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: preview image path = %s\n", previewImage.c_str() );
383 mPreviewImage = Toolkit::ImageView::New( previewImage );
385 mPreviewImage.SetParentOrigin( ParentOrigin::CENTER );
386 mPreviewImage.SetAnchorPoint( AnchorPoint::CENTER );
390 mPreviewImage.SetSize( mWidth, mHeight );
393 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
394 Self().Add( mPreviewImage );
397 // TODO: use po files
398 mStateText = Toolkit::TextLabel::New( "Loading..." );
400 mStateText.SetParentOrigin( ParentOrigin::CENTER );
401 mStateText.SetAnchorPoint( AnchorPoint::CENTER );
402 mStateText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
403 mStateText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
405 mPreviewImage.Add( mStateText );
408 mPid = widget_instance_launch( mWidgetId.c_str(), instanceId, mBundle, mWidth, mHeight );
411 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
415 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
418 } // namespace Internal
420 } // namespace WidgetView