codhandler/codrecog/src/CodRecog.cpp
changeset 0 dd21522fd290
child 26 cb62a4f66ebe
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2002 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 the License "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 *      Implementation of class CCodRecog.   
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include <s32std.h>
       
    24 #include <apmrec.h>
       
    25 #include <apmstd.h>
       
    26 #include <f32file.h>
       
    27 #include <CodDefs.h>
       
    28 #include <CharConv.h>
       
    29 #include <ImplementationProxy.h>
       
    30 #include "CodRecog.h"
       
    31 #include "CodRecogLogger.h"
       
    32 
       
    33 // ================= CONSTANTS =======================
       
    34 
       
    35 /// UID of COD Viewer Application.
       
    36 LOCAL_D const TUid KCodUidCodViewerApp = { COD_VIEWER_UID };
       
    37 /// Preferred recognition buffer size.
       
    38 const TInt KCodPreferedBufferSize = 1024;
       
    39 /// COD MIME type string.
       
    40 _LIT8( KCodMimeType, COD_MIME_TYPE );
       
    41 /// COD file extension.
       
    42 _LIT( KCodExt,".cod" );
       
    43 /// COD content recognition pattern (8 bit).
       
    44 _LIT8( KCod8,"COD-");
       
    45 /// COD content recognition pattern (16 bit).
       
    46 _LIT( KCod16,"COD-");
       
    47 
       
    48 /// COD recog ecom plugin implementation UID.
       
    49 LOCAL_D const TInt KCodRecogImplementationUid = 0x10008d49;
       
    50 /// ECOM implementation table.
       
    51 LOCAL_D const TImplementationProxy ImplementationTable[] =
       
    52     {
       
    53     IMPLEMENTATION_PROXY_ENTRY\
       
    54         ( KCodRecogImplementationUid, CCodRecog::CreateRecognizerL )
       
    55     };
       
    56 
       
    57 // ================= MEMBER FUNCTIONS =======================
       
    58 
       
    59 // ---------------------------------------------------------
       
    60 // CCodRecog::CCodRecog()
       
    61 // ---------------------------------------------------------
       
    62 //
       
    63 CCodRecog::CCodRecog()
       
    64 : CApaDataRecognizerType
       
    65                     ( KCodUidCodViewerApp, CApaDataRecognizerType::ENormal )
       
    66     {
       
    67     iCountDataTypes = 1;
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------
       
    71 // CCodRecog::PreferredBufSize()
       
    72 // ---------------------------------------------------------
       
    73 //
       
    74 TUint CCodRecog::PreferredBufSize()
       
    75     {
       
    76     return KCodPreferedBufferSize;
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------
       
    80 // CCodRecog::SupportedDataTypeL()
       
    81 // ---------------------------------------------------------
       
    82 //
       
    83 TDataType CCodRecog::SupportedDataTypeL( TInt aIndex ) const
       
    84     {
       
    85     switch ( aIndex )
       
    86         {
       
    87         case 0:
       
    88             {
       
    89             return TDataType( KCodMimeType );
       
    90             }
       
    91 
       
    92         default:
       
    93             {
       
    94             return TDataType();
       
    95             }
       
    96         }
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CCodRecog::CreateRecognizerL()
       
   101 // ---------------------------------------------------------
       
   102 //
       
   103 CApaDataRecognizerType* CCodRecog::CreateRecognizerL()
       
   104     {
       
   105     return new (ELeave) CCodRecog();
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------
       
   109 // CCodRecog::DoRecognizeL()
       
   110 // ---------------------------------------------------------
       
   111 //
       
   112 void CCodRecog::DoRecognizeL( const TDesC& aName, const TDesC8& aBuffer )
       
   113     {
       
   114     CLOG_ENTERFN( "CCodRecog::DoRecognizeL" );
       
   115     CLOG_WRITE_FORMAT( "DoRecognize: name: %S", &aName );
       
   116 
       
   117     iConfidence = ENotRecognized;
       
   118 
       
   119     // Check extension. We cannot use TParse* classes, as the recog is
       
   120     // called with generated filenames which contain invalid chars and
       
   121     // panic TParsePtrC.
       
   122     TInt len( KCodExt().Length() );
       
   123     if( aName.Length() >= len && !aName.Right( len ).CompareF( KCodExt ) )
       
   124         {
       
   125         CLOG_WRITE( "DoRecognize: extension is '.cod'" );
       
   126         // Named ".cod"
       
   127         iDataType = TDataType( KCodMimeType );
       
   128         iConfidence = ECertain;
       
   129         }
       
   130     else
       
   131         {
       
   132         CLOG_WRITE( "DoRecognize: pattern search" );
       
   133         // Not named ".cod". Recognition by pattern-matching.
       
   134         // Prepare pointers to buffer (both 8 and 16 bits).
       
   135         TPtrC8 buf8( aBuffer );
       
   136         if ( aBuffer.Length() > KCodPreferedBufferSize )
       
   137             {
       
   138             buf8.Set( aBuffer.Left( KCodPreferedBufferSize ) );
       
   139             }
       
   140         TPtrC buf16( (TText*)buf8.Ptr(), buf8.Length() / 2 );
       
   141         // Find COD-specific pattern in buffer (both 8 and 16 bits).
       
   142         if ( buf8.FindF( KCod8 ) >= 0 || buf16.FindF( KCod16 ) >= 0 )
       
   143             {
       
   144             iDataType = TDataType( KCodMimeType );
       
   145             iConfidence = ECertain;
       
   146             }
       
   147         }
       
   148     CLOG_WRITE_FORMAT( "DoRecognize: confidence: %d", iConfidence );
       
   149     CLOG_LEAVEFN( "CCodRecog::DoRecognizeL" );
       
   150     }
       
   151 
       
   152 /**
       
   153 * Implementation group proxy.
       
   154 * @param aTableCount Table count is returned here.
       
   155 * @return Implementation table is returned here.
       
   156 */
       
   157 EXPORT_C const TImplementationProxy* ImplementationGroupProxy
       
   158 ( TInt& aTableCount )
       
   159     {
       
   160     aTableCount =
       
   161         sizeof( ImplementationTable ) / sizeof( TImplementationProxy );
       
   162     return ImplementationTable;
       
   163     }