widgetmodel/alfwidgetmodel/src/alfwidgetattributeownerimpl.cpp
changeset 17 3eca7e70b1b8
parent 3 4526337fb576
equal deleted inserted replaced
3:4526337fb576 17:3eca7e70b1b8
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation of the IAlfAttributeOwner interface 
       
    15 *                for AlfWidget.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <alf/alfexceptions.h>
       
    21 #include "alf/alfattribute.h"
       
    22 #include <alf/alfwidgetcontrol.h>
       
    23 #include <alf/ialfelement.h>
       
    24 #include <osn/ustring.h>
       
    25 #include "alf/attrproperty.h"
       
    26 #include "alf/alfattributecontainer.h" 
       
    27 #include <alf/alfwidgetcommand.h> 
       
    28 
       
    29 
       
    30 #include "alfwidgetattributeownerimpl.h"
       
    31 
       
    32 using namespace duiuimodel;
       
    33 
       
    34 namespace Alf
       
    35     {
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // Constructor.
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 AlfWidgetAttributeOwnerImpl::AlfWidgetAttributeOwnerImpl(
       
    42     CAlfWidgetControl* aControl) :
       
    43     mControl(aControl)
       
    44     {
       
    45     mAttributeList.setAutoDelete(true);    
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Destructor.
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 AlfWidgetAttributeOwnerImpl::~AlfWidgetAttributeOwnerImpl()
       
    53     {
       
    54     mAttributeList.clear();
       
    55     }
       
    56     
       
    57 // ---------------------------------------------------------------------------
       
    58 // From class IAlfInterfaceBase.
       
    59 // Getter for interfaces provided by the widget.
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 IAlfInterfaceBase* AlfWidgetAttributeOwnerImpl::makeInterface(
       
    63     const IfId& aType)
       
    64     {
       
    65     UString param(aType.mImplementationId);
       
    66     if (param == IAlfAttributeOwner::type().mImplementationId)
       
    67         {
       
    68     	return static_cast<IAlfAttributeOwner*>(this);
       
    69         }
       
    70     return 0;
       
    71     }    
       
    72         
       
    73 // ---------------------------------------------------------------------------
       
    74 // Get the number of available attributes.
       
    75 // From class IAlfAttributeOwner.
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 int AlfWidgetAttributeOwnerImpl::attributeCount() const
       
    79     {
       
    80     int count = mAttributeList.count();
       
    81     // Add control attribute count.
       
    82     IAlfAttributeOwner* control =
       
    83         IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(mControl);
       
    84     if (control)
       
    85         {
       
    86         count += control->attributeCount();
       
    87         // Add root element attribute count
       
    88         for (int i = 0; i < mControl->numElements(); i++)
       
    89             {
       
    90              IAlfElement& element = mControl->element(i);
       
    91              if (element.parentElement() == 0)
       
    92                 {
       
    93                 // Get the attribute interface
       
    94                 IAlfAttributeOwner* attributeOwner = 
       
    95                     IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(&element);
       
    96                     
       
    97                 if (attributeOwner)
       
    98                     {
       
    99                     count += attributeOwner->attributeCount();
       
   100                     }
       
   101                 }
       
   102             }
       
   103         }
       
   104     
       
   105     return count;
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // Gets the attribute with the given name.
       
   110 // From class IAlfAttributeOwner.
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 const AlfAttribute* AlfWidgetAttributeOwnerImpl::getAttribute(
       
   114     const UString& aAttribName) const
       
   115     {
       
   116     const AlfAttribute* attribute = 0;
       
   117     
       
   118     // Try to find from widget
       
   119  	for (int i = 0; i < mAttributeList.count(); i++)
       
   120  	    {
       
   121  		if (!strcmp(aAttribName.getUtf8(), mAttributeList[i]->name()))
       
   122  		    {
       
   123  			attribute = mAttributeList[i];
       
   124  			break;
       
   125  		    }
       
   126  	    }
       
   127  	    
       
   128     if (!attribute)
       
   129         {
       
   130         // Try to find from control
       
   131         IAlfAttributeOwner* control =
       
   132             IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(mControl);
       
   133             
       
   134         if (control)
       
   135             {
       
   136             attribute = control->getAttribute(aAttribName);
       
   137             }
       
   138             
       
   139         if (!attribute)
       
   140             {
       
   141             // Try to find from root elements
       
   142             for (int i = 0; i < mControl->numElements(); i++)
       
   143                 {
       
   144                  IAlfElement& element = mControl->element(i);
       
   145                  if (element.parentElement() == 0)
       
   146                     {
       
   147                     // Get the attribute interface
       
   148                     IAlfAttributeOwner* attributeOwner = 
       
   149                         IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(&element);
       
   150                         
       
   151                     if (attributeOwner)
       
   152                         {
       
   153                         attribute = attributeOwner->getAttribute(aAttribName);
       
   154                         if (attribute)
       
   155                             {
       
   156                             break;
       
   157                             }
       
   158                         }
       
   159                     }
       
   160                 }
       
   161             }
       
   162 
       
   163         }
       
   164          	    
       
   165     return attribute;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // Set a value of an attribute.
       
   170 // From class IAlfAttributeOwner.
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 bool AlfWidgetAttributeOwnerImpl::setAttribute(const AlfAttribute& aAttribute)
       
   174     {
       
   175     // The attribute must have at least one value set.
       
   176     if (!aAttribute.getTargetValueCount())
       
   177         {
       
   178         ALF_THROW(
       
   179             AlfAttributeException, EInvalidAttribute, "AlfWidgetAttributeOwnerImpl")
       
   180         }   
       
   181        
       
   182     if (canHandleAttribute(aAttribute.name()))
       
   183         {
       
   184          // Check whether the attribute exists already.
       
   185         int i = 0;
       
   186         for (i = 0; i < mAttributeList.count() ; i++)
       
   187      	    {
       
   188      		if (!strcmp(aAttribute.name(), mAttributeList[i]->name()))
       
   189      		    {
       
   190      		    // Attribute exists already, modify the contents.
       
   191      			*mAttributeList[i] = aAttribute;
       
   192      		    handleAttribute(*mAttributeList[i]);
       
   193      		    return true;
       
   194      		    }
       
   195             }
       
   196 
       
   197         // Attribute with the given name does not exist,
       
   198         // clone the given attribute and insert it in the list.
       
   199         mAttributeList.resize(mAttributeList.count() + 1);
       
   200         // Insert cannot fail because already resized.
       
   201         mAttributeList.insert(mAttributeList.count(),
       
   202             const_cast<AlfAttribute&>(aAttribute).clone());
       
   203         handleAttribute(*mAttributeList[i]);
       
   204         return true;
       
   205         }
       
   206     
       
   207     bool supported(false);
       
   208     bool sendevent(true);
       
   209     
       
   210     // Let the control try to handle attribute.
       
   211     IAlfAttributeOwner* control =
       
   212         IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(mControl);
       
   213     if (control)
       
   214         {
       
   215         supported = control->setAttribute(aAttribute);
       
   216         }
       
   217         
       
   218     // Let the root elements try to handle attribute
       
   219     if (!supported)
       
   220         {
       
   221         supported = handleElementAttribute(aAttribute);
       
   222         sendevent = false; //event already sent
       
   223         }
       
   224 
       
   225     //inform env of attribute setting
       
   226     if (supported && sendevent)
       
   227         {
       
   228         UString target;
       
   229         auto_ptr<AlfAttribute> attribute(new (EMM) AlfAttribute());
       
   230         *attribute.get() = aAttribute;
       
   231         TAlfWidgetAttributeCommand command(attribute.get(), target, mControl->widget());
       
   232         attribute.release();
       
   233         command.ExecuteL(mControl->Env());
       
   234         }
       
   235 
       
   236     return (supported);
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // Set a value of an attribute.
       
   241 // From class IAlfAttributeOwner.
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 bool AlfWidgetAttributeOwnerImpl::setAttribute(
       
   245     const UString& aAttribName, AlfAttributeValueType* aValue)
       
   246     {
       
   247     if (!aValue)
       
   248         {
       
   249         ALF_THROW(
       
   250             AlfAttributeException, EInvalidAttribute, "AlfWidgetAttributeOwnerImpl")
       
   251         }
       
   252         
       
   253     // Create a new attribute.
       
   254     auto_ptr<AlfAttribute> attribute(
       
   255         new (EMM) AlfAttribute(aAttribName.getUtf8(), AlfAttribute::EStatic));  
       
   256     attribute->addTargetValue(aValue);
       
   257 	 
       
   258     return setAttribute(*attribute.get());
       
   259     }   
       
   260 // ---------------------------------------------------------------------------
       
   261 // Set a value of an attribute.
       
   262 // From class IAlfAttributeOwner.
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 bool AlfWidgetAttributeOwnerImpl::setAttribute( const UString& aTargetId , 
       
   266 												AlfAttribute& aAttribute ) 
       
   267 	{
       
   268 	
       
   269  	bool supported(false);
       
   270  
       
   271 	 // The attribute must have at least one value set.
       
   272 	if (!aAttribute.getTargetValueCount())
       
   273 	    {
       
   274 	    ALF_THROW(
       
   275 	        AlfAttributeException, EInvalidAttribute, "AlfWidgetAttributeOwnerImpl")
       
   276 	    }
       
   277 
       
   278     if (!aTargetId.isEmpty()
       
   279          && mControl)
       
   280         {
       
   281 
       
   282         // Try to find from control
       
   283         IAlfAttributeOwner* control =
       
   284             IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(mControl);
       
   285         if (control)
       
   286             {
       
   287             supported = control->setAttribute(aTargetId, aAttribute);
       
   288             }
       
   289         
       
   290         if (!supported)
       
   291             {
       
   292 		    // Try finding element class using targetId
       
   293  		    IAlfElement* elem = mControl->findElement(aTargetId.getUtf8());
       
   294  		  	if (elem)	
       
   295 		  		{
       
   296 			    IAlfAttributeOwner* elementOwner =  IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(elem);
       
   297 				if (elementOwner)
       
   298 					{
       
   299 					supported = elementOwner->setAttribute(aTargetId, aAttribute);	
       
   300 					}
       
   301 		  		}
       
   302 	        }
       
   303 	    }
       
   304 
       
   305 	//inform env of attribute change
       
   306     if (supported)
       
   307         {
       
   308         UString target(aTargetId);
       
   309         auto_ptr<AlfAttribute> attribute(new (EMM) AlfAttribute());
       
   310         *attribute.get() = aAttribute;
       
   311         TAlfWidgetAttributeCommand command(attribute.get(), target, mControl->widget());
       
   312         attribute.release();
       
   313         command.ExecuteL(mControl->Env());
       
   314         }
       
   315  		        
       
   316     return supported;
       
   317    
       
   318 	}
       
   319 
       
   320 // ---------------------------------------------------------------------------
       
   321 // Set a value of an attribute.
       
   322 // From class IAlfAttributeOwner.
       
   323 // ---------------------------------------------------------------------------
       
   324 //
       
   325 bool AlfWidgetAttributeOwnerImpl::setAttribute( const UString& aTargetId, 
       
   326 											    const UString& aAttribName, 
       
   327 											    AlfAttributeValueType* aValue )
       
   328 	{
       
   329 	bool ret(false);
       
   330 	
       
   331     if (!aValue)
       
   332 	    {
       
   333 	    ALF_THROW(
       
   334 	        AlfAttributeException, EInvalidAttribute, "AlfWidgetAttributeOwnerImpl")
       
   335 	    }
       
   336 
       
   337     if (!aTargetId.isEmpty())
       
   338 	    {
       
   339 	    // Create a new static attribute.
       
   340 	    auto_ptr<AlfAttribute> attribute(
       
   341 	        new (EMM) AlfAttribute(aAttribName.getUtf8(), AlfAttribute::EStatic));  
       
   342 	    attribute->addTargetValue(aValue);
       
   343 	    ret = setAttribute(aTargetId, *attribute.get());
       
   344 			
       
   345 		}
       
   346 	    
       
   347 	return ret;    
       
   348     
       
   349 	}
       
   350 
       
   351 // ---------------------------------------------------------------------------
       
   352 // Gets the attribute with the given name.
       
   353 // From class IAlfAttributeOwner.
       
   354 // ---------------------------------------------------------------------------
       
   355 //
       
   356 const AlfAttribute* AlfWidgetAttributeOwnerImpl::getAttribute( const UString& aTargetId,
       
   357 															    const UString& aAttribName ) const
       
   358 	{
       
   359 	const AlfAttribute* attr(0);
       
   360 	
       
   361 	if (!aTargetId.isEmpty()
       
   362 	    && mControl)
       
   363         {
       
   364         
       
   365         // Try to find from control
       
   366         IAlfAttributeOwner* control =
       
   367             IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(mControl);
       
   368         if (control)
       
   369             {
       
   370             attr = control->getAttribute(aTargetId, aAttribName);
       
   371             }
       
   372         
       
   373         if (!attr)
       
   374             {
       
   375 	        // Try finding element class from control
       
   376 	        IAlfElement* element = mControl->findElement(aTargetId.getUtf8());
       
   377 	        
       
   378 	        if (element)
       
   379 		        {
       
   380 		        // Get the attribute interface
       
   381 		        IAlfAttributeOwner* attributeOwner = 
       
   382 		            IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(element);
       
   383 		            
       
   384 		        if (attributeOwner)
       
   385 		            {
       
   386 		            attr = attributeOwner->getAttribute(aTargetId, aAttribName); 
       
   387 		            }
       
   388 		        }
       
   389             }
       
   390 	    }
       
   391 	      	    
       
   392 	return attr;
       
   393 
       
   394 	}
       
   395 	
       
   396 // ---------------------------------------------------------------------------
       
   397 // Sets the attributes from container
       
   398 // From class IAlfAttributeOwner.
       
   399 // ---------------------------------------------------------------------------
       
   400 //
       
   401 bool AlfWidgetAttributeOwnerImpl::setAttributeContainer( AlfAttributeContainer& aAttributeContainer )
       
   402   	{
       
   403   	
       
   404   	bool ret(true);
       
   405 	
       
   406     for (int i=0;i<aAttributeContainer.attributeCount();i++)
       
   407         {
       
   408         AlfAttribute& attr = aAttributeContainer.getAttribute(i);
       
   409         bool supported = setAttribute(attr);
       
   410         if (!supported)
       
   411 	        {
       
   412 	        ret = false;	
       
   413 	        }
       
   414 	    }
       
   415 
       
   416 	return ret;
       
   417 	
       
   418  	}
       
   419 
       
   420 // ---------------------------------------------------------------------------
       
   421 // Sets the attributes from container using targetId
       
   422 // From class IAlfAttributeOwner.
       
   423 // ---------------------------------------------------------------------------
       
   424 //
       
   425 	
       
   426 bool AlfWidgetAttributeOwnerImpl::setAttributeContainer( const UString& aTargetId , 
       
   427                                AlfAttributeContainer& aAttributeContainer )
       
   428 	{
       
   429 	bool ret(true);
       
   430 	
       
   431 	if (!aTargetId.isEmpty() )
       
   432 		{
       
   433         for (int i=0;i<aAttributeContainer.attributeCount();i++)
       
   434 	        {
       
   435 	        AlfAttribute& attr = aAttributeContainer.getAttribute(i);
       
   436 	        bool supported = setAttribute(aTargetId, attr);
       
   437 	        if (!supported)
       
   438 		        {
       
   439 		        ret = false;	
       
   440 		        }
       
   441 	        }
       
   442 	    }
       
   443 
       
   444 	return ret;
       
   445 
       
   446 	}
       
   447     
       
   448 // ---------------------------------------------------------------------------
       
   449 // Handles the attribute, sets proper values.
       
   450 // ---------------------------------------------------------------------------
       
   451 //
       
   452 bool AlfWidgetAttributeOwnerImpl::handleElementAttribute(
       
   453     const AlfAttribute& aAttribute)
       
   454     {
       
   455     bool supported(false);
       
   456     
       
   457     // Let the root elements try to handle attribute
       
   458     const char* attrName = aAttribute.name();   // for debuging
       
   459     int elementCount = mControl->numElements();
       
   460     for (int i = 0; i < elementCount; i++)
       
   461         {
       
   462          IAlfElement& element = mControl->element(i);
       
   463          if (element.parentElement() == 0)
       
   464             {
       
   465             // Get the attribute interface
       
   466             IAlfAttributeOwner* attributeOwner = 
       
   467                 IAlfInterfaceBase::makeInterface<IAlfAttributeOwner>(&element);
       
   468                 
       
   469             if (attributeOwner)
       
   470                 {
       
   471                 // let all IAlfAttributeOwners set the attribute
       
   472                 if (attributeOwner->setAttribute(aAttribute))
       
   473                     {
       
   474                     UString target(element.name());
       
   475                     auto_ptr<AlfAttribute> attribute(new (EMM) AlfAttribute());
       
   476                     *attribute.get() = aAttribute;
       
   477                     TAlfWidgetAttributeCommand command(attribute.get(), target, mControl->widget());
       
   478                     attribute.release();
       
   479                     command.ExecuteL(mControl->Env());
       
   480                     supported = true;
       
   481                     }
       
   482                 }
       
   483             }
       
   484         }
       
   485         
       
   486     return supported;               
       
   487     }       
       
   488     
       
   489 // ---------------------------------------------------------------------------
       
   490 // Handles the attribute, sets proper values.
       
   491 // ---------------------------------------------------------------------------
       
   492 //
       
   493 void AlfWidgetAttributeOwnerImpl::handleAttribute(
       
   494     const AlfAttribute& /*aAttribute*/)
       
   495     {
       
   496     // Add common widget specific attribute handling here
       
   497     }
       
   498     
       
   499 // ---------------------------------------------------------------------------
       
   500 // Can attribute be handled with this widget.
       
   501 // ---------------------------------------------------------------------------
       
   502 //
       
   503 bool AlfWidgetAttributeOwnerImpl::canHandleAttribute(
       
   504     const char* /*aAttributeName*/)
       
   505     {
       
   506     bool canHandle(false);
       
   507     // Add common wigdet specific attribute checking here like:
       
   508 /*    if (!strcmp(aAttributeName, commonvisualattributes::KOpacity))  
       
   509         {
       
   510         canHandle = true;
       
   511         }*/
       
   512     return canHandle;        
       
   513     }
       
   514     
       
   515     } // namespace Alf