messagingapp/msgutils/unidatamodel/unimmsdataplugin/src/UniSmilUtils.cpp
changeset 23 238255e8b033
equal deleted inserted replaced
5:4697dfb2d7ad 23:238255e8b033
       
     1 /*
       
     2 * Copyright (c) 2005 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: 
       
    15 *       Static utility class for manipulating SMIL DOM
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include <e32std.h>
       
    22 #include <gdi.h>
       
    23 
       
    24 #include <gmxmldocument.h>
       
    25 #include <gmxmlnode.h>
       
    26 #include <gmxmlelement.h>
       
    27 
       
    28 #include <ConformanceChecker.h>
       
    29 #include "UniSmilUtils.h"
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------
       
    33 // UniSmilUtils::StringToIntValue
       
    34 // Convert a descriptor to TInt
       
    35 // ---------------------------------------------------------
       
    36 //
       
    37 TInt UniSmilUtils::StringToIntValue( const TDesC& aString,
       
    38                                                     TInt aDefault )
       
    39     {
       
    40     TInt value = aDefault;
       
    41     if( aString.Length() != 0 )
       
    42         {
       
    43         TChar c = aString[0];
       
    44         if( c.IsDigit() )
       
    45             {
       
    46             TLex luthor( aString );
       
    47             luthor.Val( value );
       
    48             }
       
    49         else
       
    50             {
       
    51             value = aDefault;
       
    52             }
       
    53         }
       
    54     return value;
       
    55     }
       
    56 
       
    57 
       
    58 // ---------------------------------------------------------
       
    59 // UniSmilUtils::IsMediaElem
       
    60 // ---------------------------------------------------------
       
    61 
       
    62 TBool UniSmilUtils::IsMediaElem( CMDXMLNode* aNodePtr )
       
    63 {
       
    64     TBool ret = EFalse;
       
    65 
       
    66     if ( aNodePtr->NodeType() == CMDXMLNode::EElementNode &&
       
    67          UniSmilUtils::MediaElemType( aNodePtr) != EMsgMediaUnknown )
       
    68         {
       
    69         ret = ETrue;
       
    70         }
       
    71 
       
    72     return ret;
       
    73 }
       
    74 
       
    75 // ---------------------------------------------------------
       
    76 // UniSmilUtils::NextNode
       
    77 // ---------------------------------------------------------
       
    78 
       
    79 CMDXMLNode* UniSmilUtils::NextNode(CMDXMLNode* aRefNode)
       
    80 {
       
    81     if( aRefNode->FirstChild() )
       
    82         {
       
    83         aRefNode = aRefNode->FirstChild();
       
    84         }
       
    85     else
       
    86         {
       
    87         if( aRefNode->NextSibling() )
       
    88             {
       
    89             // move to the next sibling if exists
       
    90             aRefNode = aRefNode->NextSibling();
       
    91             }
       
    92         else
       
    93             {
       
    94             // No siblings at this level. Go up to parent.
       
    95             // might need to go up several layers so WHILE rather than IF
       
    96             while ( aRefNode && !aRefNode->NextSibling() )
       
    97                 {
       
    98                 // no siblings, move back to parent.
       
    99                 aRefNode = aRefNode->ParentNode();
       
   100                 }
       
   101 
       
   102             // Check if we stopped because node has siblings.
       
   103             if ( aRefNode && aRefNode->NextSibling() )
       
   104                 {
       
   105                 aRefNode = aRefNode->NextSibling();
       
   106                 }
       
   107             }
       
   108         }
       
   109 
       
   110     return aRefNode;
       
   111 }
       
   112 
       
   113 // ---------------------------------------------------------
       
   114 // UniSmilUtils::RemoveChilds
       
   115 // ---------------------------------------------------------
       
   116 
       
   117 void UniSmilUtils::RemoveChilds(
       
   118     CMDXMLNode* aParent )
       
   119     {
       
   120     CMDXMLNode* child = NULL;
       
   121 
       
   122     if ( aParent )
       
   123         {
       
   124         child = aParent->FirstChild();
       
   125         }
       
   126 
       
   127     while ( child )
       
   128         {
       
   129         if ( child->HasChildNodes() )
       
   130             {
       
   131             UniSmilUtils::RemoveChilds( child );
       
   132             }
       
   133         else
       
   134             {
       
   135             CMDXMLNode* n = child;
       
   136             child = child->NextSibling();
       
   137             aParent->RemoveChild( n );
       
   138             delete n;
       
   139             }
       
   140         }
       
   141     }
       
   142 
       
   143 
       
   144 // ---------------------------------------------------------
       
   145 // UniSmilUtils::ElemType
       
   146 // ---------------------------------------------------------
       
   147 
       
   148 TMsgMediaType UniSmilUtils::MediaElemType(CMDXMLNode* aNodePtr)
       
   149 {
       
   150     TMsgMediaType type(EMsgMediaUnknown);
       
   151 
       
   152     if ( aNodePtr )
       
   153         {
       
   154         TPtrC nName;
       
   155         nName.Set(aNodePtr->NodeName());
       
   156 
       
   157         if ( nName.Compare(KSMILWREleAnimation) == 0 )
       
   158             {
       
   159             type = EMsgMediaAnimation;
       
   160             }
       
   161         else if (nName.Compare(KSMILWREleAudio) == 0 )
       
   162             {
       
   163             type = EMsgMediaAudio;
       
   164             }
       
   165         // SVG included. No special SVG element tag exist.
       
   166         else if (nName.Compare(KSMILWREleImg) == 0 )
       
   167             {
       
   168             type = EMsgMediaImage;
       
   169             }
       
   170         else if (nName.Compare(KSMILWREleRef) == 0 )
       
   171             {
       
   172             type = EMsgMediaRef;
       
   173             }
       
   174         else if ( nName.Compare(KSMILWREleText) == 0 )
       
   175             {
       
   176             type = EMsgMediaText;
       
   177             }
       
   178         else if ( nName.Compare(KSMILWREleTextstream) == 0 )
       
   179             {
       
   180             type = EMsgMediaTextStream;
       
   181             }
       
   182         else if ( nName.Compare(KSMILWREleVideo) == 0 )
       
   183             {
       
   184             type = EMsgMediaVideo;
       
   185             }
       
   186         }
       
   187 
       
   188     return type;
       
   189 }
       
   190 
       
   191 // ---------------------------------------------------------
       
   192 // UniSmilUtils::GetId
       
   193 // ---------------------------------------------------------
       
   194 
       
   195 TInt UniSmilUtils::GetId( CMDXMLNode* aNodePtr, TPtrC& aId )
       
   196     {
       
   197     TInt ret = KErrNotSupported;
       
   198 
       
   199     if( aNodePtr && aNodePtr->NodeType() == CMDXMLNode::EElementNode )
       
   200         {
       
   201         CMDXMLElement* e =(CMDXMLElement*) aNodePtr;
       
   202         if ( e->IsAttributeSpecified( KSMILWRParaId ))
       
   203             {
       
   204             e->GetAttribute(KSMILWRParaId, aId);
       
   205             ret = KErrNone;
       
   206             }
       
   207         else
       
   208             {
       
   209             ret = KErrNotFound;
       
   210             }
       
   211         }
       
   212     return ret;
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------
       
   216 // UniSmilUtils::GetMetaTag
       
   217 // ---------------------------------------------------------
       
   218 
       
   219 TInt UniSmilUtils::GetMetaTag( CMDXMLNode* aNodePtr,
       
   220                                    TPtrC& aName,
       
   221                                    TPtrC& aContent )
       
   222     {
       
   223     TInt ret = KErrNotFound;
       
   224 
       
   225     if( aNodePtr &&
       
   226         aNodePtr->NodeType() == CMDXMLNode::EElementNode &&
       
   227         aNodePtr->NodeName().Compare(KSMILWREleMeta) == 0)
       
   228         {
       
   229         CMDXMLElement* e = (CMDXMLElement*) aNodePtr;
       
   230 
       
   231         if ( e->IsAttributeSpecified( KSMILWRParaName ) &&
       
   232              e->IsAttributeSpecified( KSMILWRParaContent ))
       
   233             {
       
   234             if (!e->GetAttribute(KSMILWRParaName, aName) &&
       
   235                 !e->GetAttribute(KSMILWRParaContent, aContent))
       
   236                 {
       
   237                 ret = KErrNone;
       
   238                 }
       
   239             }
       
   240         }
       
   241     return ret;
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------
       
   245 // UniSmilUtils::RegionId
       
   246 // ---------------------------------------------------------
       
   247 
       
   248 TInt UniSmilUtils::GetRegionId( CMDXMLNode* aNodePtr, TPtrC& aId )
       
   249     {
       
   250     TInt ret = KErrNotSupported;
       
   251 
       
   252     if ( aNodePtr )
       
   253         {
       
   254         if ( UniSmilUtils::IsMediaElem( aNodePtr ) )
       
   255             {
       
   256             ret = ((CMDXMLElement*)aNodePtr)->GetAttribute(KSMILWRParaRegion, aId);
       
   257             }
       
   258         }
       
   259     return ret;
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------
       
   263 // UniSmilUtils::GetSrc
       
   264 // ---------------------------------------------------------
       
   265 
       
   266 TPtrC UniSmilUtils::GetSrcL( CMDXMLNode* aNodePtr )
       
   267 {
       
   268     TPtrC src = TPtrC();
       
   269     if ( aNodePtr )
       
   270             {
       
   271         CMDXMLElement* e = (CMDXMLElement*) aNodePtr;
       
   272 
       
   273         if ( e && e->IsAttributeSpecified(KSMILWRParaSrc) )
       
   274             {
       
   275             User::LeaveIfError( e->GetAttribute(KSMILWRParaSrc, src) );
       
   276             }
       
   277         else
       
   278             {
       
   279             // Ok not to have src i.e. no default content.
       
   280             }
       
   281         }
       
   282 
       
   283     return src;
       
   284 }
       
   285 
       
   286 
       
   287 // ---------------------------------------------------------
       
   288 // UniSmilUtils::GetSmilTypeL
       
   289 //
       
   290 // ---------------------------------------------------------
       
   291 TUniSmilType UniSmilUtils::GetSmilTypeL( CMDXMLDocument* aDom )
       
   292     {
       
   293 
       
   294     if ( UniSmilUtils::IsTemplateSmil( aDom ) )
       
   295         {
       
   296         return ETemplateSmil;
       
   297         }
       
   298     else if ( !aDom ||
       
   299               !aDom->DocumentElement() ||
       
   300               !aDom->DocumentElement()->FirstChild() )
       
   301         {
       
   302         return ENoSmil;
       
   303         }
       
   304     else
       
   305         {
       
   306         CConformanceChecker* checker = CConformanceChecker::NewL();
       
   307         CleanupStack::PushL( checker );
       
   308         TBool isConf( EFalse );
       
   309 
       
   310         isConf = checker->Check( aDom, EMmsSmil_v2_0,
       
   311             EAllowVideoTag |
       
   312             EAllowNonMilliseconds |
       
   313             EAllowAllAttributes |
       
   314             EAllowMixedRegionDimensions |
       
   315             EAllowAnyRegionNames );
       
   316 
       
   317         CleanupStack::PopAndDestroy( checker );
       
   318 
       
   319         if( isConf )
       
   320             {
       
   321             return EMmsSmil;
       
   322             }
       
   323 
       
   324         }
       
   325     return E3GPPSmil;
       
   326     }
       
   327 
       
   328 
       
   329 // ---------------------------------------------------------
       
   330 // UniSmilUtils::IsTemplateSmil
       
   331 //
       
   332 // ---------------------------------------------------------
       
   333 
       
   334 TBool UniSmilUtils::IsTemplateSmil( CMDXMLDocument* aDom )
       
   335     {
       
   336     TBool ret( EFalse );
       
   337 
       
   338     CMDXMLNode* n = aDom->DocumentElement()->FirstChild();
       
   339 
       
   340     while ( n )
       
   341         {
       
   342         if ( n->NodeName().Compare( KSMILWREleMeta ) == 0)
       
   343             {
       
   344             TPtrC v1, v2;
       
   345             if ( !UniSmilUtils::GetMetaTag( n, v1, v2 ) )
       
   346                 {
       
   347                 if ( v1.Compare( KSMILMetaSmilProfile ) == 0 &&
       
   348                      v2.Compare( KSMILMetaSmilProfileValue ) == 0)
       
   349                     {
       
   350                     ret = ETrue;
       
   351                     break;
       
   352                     }
       
   353                 }
       
   354             }
       
   355         else if ( n->NodeName().Compare( KSMILWREleBody ) == 0 )
       
   356             {
       
   357             // Cannot anymore have metatags
       
   358             break;
       
   359             }
       
   360         n = UniSmilUtils::NextNode( n );
       
   361         }
       
   362     return ret;
       
   363     }
       
   364 
       
   365 
       
   366 // EOF