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::SetPreviewEnabled( bool enabled )
195 mPreviewEnabled = enabled;
199 mPreviewImage.SetVisible( enabled );
203 bool WidgetView::GetPreviewEnabled() const
205 return mPreviewEnabled;
208 void WidgetView::SetStateTextEnabled( bool enabled )
210 mStateTextEnabled = enabled;
214 mStateText.SetVisible( enabled );
218 bool WidgetView::GetStateTextEnabled() const
220 return mStateTextEnabled;
223 void WidgetView::ActivateFaultedWidget()
227 // Esable preview and text
228 if( mPreviewEnabled )
230 mPreviewImage.SetVisible( true );
233 if( mStateTextEnabled )
235 mStateText.SetVisible( true );
238 // launch widget again
239 mPid = widget_instance_launch( mWidgetId.c_str(), mInstanceId.c_str(), mBundle, mWidth, mHeight );
242 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
246 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
250 bool WidgetView::IsWidgetFaulted()
252 return mPid < 0 ? true : false;
255 void WidgetView::SetPermanentDelete( bool permanentDelete )
257 mPermanentDelete = permanentDelete;
260 void WidgetView::AddObjectView( Pepper::ObjectView objectView )
262 mObjectView = objectView;
264 mObjectView.SetParentOrigin( ParentOrigin::CENTER );
265 mObjectView.SetAnchorPoint( AnchorPoint::CENTER );
267 Self().Add( mObjectView );
269 // Disable preview and text
270 if( mPreviewEnabled )
272 mPreviewImage.SetVisible( false );
275 if( mStateTextEnabled )
277 mStateText.SetVisible( false );
281 Dali::WidgetView::WidgetView handle( GetOwner() );
282 mWidgetAddedSignal.Emit( handle );
285 void WidgetView::RemoveObjectView()
288 Dali::WidgetView::WidgetView handle( GetOwner() );
289 mWidgetDeletedSignal.Emit( handle );
294 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAddedSignal()
296 return mWidgetAddedSignal;
299 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetDeletedSignal()
301 return mWidgetDeletedSignal;
304 void WidgetView::OnInitialize()
306 char* instanceId = NULL;
307 char* previewPath = NULL;
308 std::string previewImage;
309 widget_size_type_e sizeType;
311 if( !mContentInfo.empty() )
313 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: decode bundle\n" );
315 mBundle = bundle_decode( reinterpret_cast< const bundle_raw* >( mContentInfo.c_str() ), mContentInfo.length() );
318 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: Invalid bundle data.\n" );
322 bundle_get_str( mBundle, WIDGET_K_INSTANCE, &instanceId );
327 int ret = widget_instance_create( mWidgetId.c_str(), &instanceId );
328 if( ret < 0 || !instanceId )
330 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is failed [%s].\n", mWidgetId.c_str() );
334 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is called. [widget id = %s, instance id = %s]\n",
335 mWidgetId.c_str(), instanceId );
338 mInstanceId = instanceId;
341 widget_service_get_size_type( mWidth, mHeight, &sizeType );
343 previewPath = widget_service_get_preview_image_path( mWidgetId.c_str(), sizeType );
346 previewImage = previewPath;
351 previewImage = tzplatform_getenv( TZ_SYS_SHARE );
352 previewImage.append( WIDGET_VIEW_RESOURCE_DEFAULT_IMG );
355 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: preview image path = %s\n", previewImage.c_str() );
357 mPreviewImage = Toolkit::ImageView::New( previewImage );
359 mPreviewImage.SetParentOrigin( ParentOrigin::CENTER );
360 mPreviewImage.SetAnchorPoint( AnchorPoint::CENTER );
364 mPreviewImage.SetSize( mWidth, mHeight );
367 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
368 Self().Add( mPreviewImage );
371 // TODO: use po files
372 mStateText = Toolkit::TextLabel::New( "Loading..." );
374 mStateText.SetParentOrigin( ParentOrigin::CENTER );
375 mStateText.SetAnchorPoint( AnchorPoint::CENTER );
376 mStateText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
377 mStateText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
379 mPreviewImage.Add( mStateText );
382 mPid = widget_instance_launch( mWidgetId.c_str(), instanceId, mBundle, mWidth, mHeight );
385 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
389 DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
392 } // namespace Internal
394 } // namespace WidgetView