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 bool WidgetView::CancelTouchEvent()
197 return mObjectView.CancelTouchEvent();
203 void WidgetView::SetPreviewEnabled( bool enabled )
205 mPreviewEnabled = enabled;
209 mPreviewImage.SetVisible( enabled );
213 bool WidgetView::GetPreviewEnabled() const
215 return mPreviewEnabled;
218 void WidgetView::SetStateTextEnabled( bool enabled )
220 mStateTextEnabled = enabled;
224 mStateText.SetVisible( enabled );
228 bool WidgetView::GetStateTextEnabled() const
230 return mStateTextEnabled;
233 void WidgetView::ActivateFaultedWidget()
237 // Esable preview and text
238 if( mPreviewEnabled )
240 mPreviewImage.SetVisible( true );
243 if( mStateTextEnabled )
245 mStateText.SetVisible( true );
248 // launch widget again
249 mPid = widget_instance_launch( mWidgetId.c_str(), mInstanceId.c_str(), mBundle, mWidth, mHeight );
252 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
256 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
260 bool WidgetView::IsWidgetFaulted()
262 return mPid < 0 ? true : false;
265 void WidgetView::SetPermanentDelete( bool permanentDelete )
267 mPermanentDelete = permanentDelete;
270 void WidgetView::AddObjectView( Pepper::ObjectView objectView )
272 mObjectView = objectView;
274 mObjectView.SetParentOrigin( ParentOrigin::CENTER );
275 mObjectView.SetAnchorPoint( AnchorPoint::CENTER );
277 Self().Add( mObjectView );
279 // Disable preview and text
280 if( mPreviewEnabled )
282 mPreviewImage.SetVisible( false );
285 if( mStateTextEnabled )
287 mStateText.SetVisible( false );
291 Dali::WidgetView::WidgetView handle( GetOwner() );
292 mWidgetAddedSignal.Emit( handle );
295 void WidgetView::RemoveObjectView()
298 Dali::WidgetView::WidgetView handle( GetOwner() );
299 mWidgetDeletedSignal.Emit( handle );
304 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAddedSignal()
306 return mWidgetAddedSignal;
309 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetDeletedSignal()
311 return mWidgetDeletedSignal;
314 void WidgetView::OnInitialize()
316 char* instanceId = NULL;
317 char* previewPath = NULL;
318 std::string previewImage;
319 widget_size_type_e sizeType;
321 if( !mContentInfo.empty() )
323 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: decode bundle\n" );
325 mBundle = bundle_decode( reinterpret_cast< const bundle_raw* >( mContentInfo.c_str() ), mContentInfo.length() );
328 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: Invalid bundle data.\n" );
332 bundle_get_str( mBundle, WIDGET_K_INSTANCE, &instanceId );
337 int ret = widget_instance_create( mWidgetId.c_str(), &instanceId );
338 if( ret < 0 || !instanceId )
340 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is failed [%s].\n", mWidgetId.c_str() );
344 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is called. [widget id = %s, instance id = %s]\n",
345 mWidgetId.c_str(), instanceId );
348 mInstanceId = instanceId;
351 widget_service_get_size_type( mWidth, mHeight, &sizeType );
353 previewPath = widget_service_get_preview_image_path( mWidgetId.c_str(), sizeType );
356 previewImage = previewPath;
361 previewImage = tzplatform_getenv( TZ_SYS_SHARE );
362 previewImage.append( WIDGET_VIEW_RESOURCE_DEFAULT_IMG );
365 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: preview image path = %s\n", previewImage.c_str() );
367 mPreviewImage = Toolkit::ImageView::New( previewImage );
369 mPreviewImage.SetParentOrigin( ParentOrigin::CENTER );
370 mPreviewImage.SetAnchorPoint( AnchorPoint::CENTER );
374 mPreviewImage.SetSize( mWidth, mHeight );
377 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
378 Self().Add( mPreviewImage );
381 // TODO: use po files
382 mStateText = Toolkit::TextLabel::New( "Loading..." );
384 mStateText.SetParentOrigin( ParentOrigin::CENTER );
385 mStateText.SetAnchorPoint( AnchorPoint::CENTER );
386 mStateText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
387 mStateText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
389 mPreviewImage.Add( mStateText );
392 mPid = widget_instance_launch( mWidgetId.c_str(), instanceId, mBundle, mWidth, mHeight );
395 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
399 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
402 } // namespace Internal
404 } // namespace WidgetView