mmsengine/mmsmessage/src/mmsownnumber.cpp
changeset 70 a15d9966050f
parent 61 8ba0afbb4637
child 72 6f657153cbc5
equal deleted inserted replaced
61:8ba0afbb4637 70:a15d9966050f
     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:   Check id any of given numbers is caller's own number
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include    "mmsownnumber.h"
       
    23 #include    "mmsgenutils.h"
       
    24 #include    <e32std.h>
       
    25 #include    <badesca.h>
       
    26 #include    <centralrepository.h>
       
    27 #include    <telconfigcrkeys.h>
       
    28 
       
    29 #include    <CVPbkContactManager.h>
       
    30 #include    <VPbkContactStoreUris.h>
       
    31 #include    <CVPbkContactStoreUriArray.h>
       
    32 #include    <MVPbkContactLinkArray.h>
       
    33 #include    <MVPbkContactLink.h>
       
    34 #include    <TVPbkContactStoreUriPtr.h>
       
    35 #include    <MVPbkContactStoreList.h>
       
    36 #include    <MVPbkContactOperationBase.h>
       
    37 #include    <MVPbkStoreContact.h>
       
    38 #include    <MVPbkFieldType.h>
       
    39 #include    <MVPbkContactFieldTextData.h>
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ===============================
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CMmsOwnNumber::CMmsOwnNumber
       
    45 // C++ default constructor can NOT contain any code, that
       
    46 // might leave.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CMmsOwnNumber::CMmsOwnNumber()
       
    50     : CActive( EPriorityNormal ),
       
    51     iIndex( KErrNotFound )
       
    52     {
       
    53     CActiveScheduler::Add(this);
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CMmsOwnNumber::ConstructL
       
    58 // Symbian 2nd phase constructor can leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CMmsOwnNumber::ConstructL( RFs* aFs )
       
    62     {
       
    63     iFs = aFs;
       
    64     
       
    65     // The function is supposed to get a descriptor with packed URIs.
       
    66     // As we only have one URI we assume it is packed enough
       
    67     CVPbkContactStoreUriArray* storeArray = CVPbkContactStoreUriArray::NewLC();
       
    68     TVPbkContactStoreUriPtr uriPtr( VPbkContactStoreUris::SimGlobalOwnNumberUri() );
       
    69     storeArray->AppendL( uriPtr );
       
    70     
       
    71     // TEST TRY ALSO DEFAULT DATABASE AS SIM DATABASE DOES NOT WORK IN WINS
       
    72     
       
    73 /*    
       
    74     TVPbkContactStoreUriPtr uriPtr2( VPbkContactStoreUris::DefaultCntDbUri() );
       
    75     storeArray->AppendL( uriPtr2 );
       
    76 */    
       
    77     // END OF TEST
       
    78     
       
    79     iNumberOfStores = storeArray->Count();
       
    80     iNumberOfOpenStores = iNumberOfStores; // otimistic
       
    81     
       
    82     // Open the database with the URI that specifies our own number
       
    83     iContactManager = CVPbkContactManager::NewL( *storeArray, aFs );
       
    84     
       
    85 	CleanupStack::PopAndDestroy( storeArray );
       
    86 	
       
    87 	iContactList = &(iContactManager->ContactStoresL());
       
    88 	
       
    89 	//Let's find the number of digits to match
       
    90     iDigitsToMatch = KMmsNumberOfDigitsToMatch ;
       
    91 
       
    92     CRepository* repository = NULL;
       
    93     TInt error = KErrNone;
       
    94     TRAP( error, repository = CRepository::NewL( KCRUidTelConfiguration ));
       
    95     if ( error == KErrNone)
       
    96         {
       
    97         error = repository->Get( KTelMatchDigits, iDigitsToMatch );
       
    98         delete repository;
       
    99         if( error != KErrNone )
       
   100             {
       
   101             iDigitsToMatch=KMmsNumberOfDigitsToMatch;
       
   102             }
       
   103     	}
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CMmsOwnNumber::NewL
       
   108 // Two-phased constructor.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 CMmsOwnNumber* CMmsOwnNumber::NewL( RFs* aFs )
       
   112     {
       
   113     CMmsOwnNumber* self = new( ELeave ) CMmsOwnNumber;
       
   114     
       
   115     CleanupStack::PushL( self );
       
   116     self->ConstructL( aFs );
       
   117     CleanupStack::Pop( self );
       
   118 
       
   119     return self;
       
   120     }
       
   121 
       
   122     
       
   123 // Destructor
       
   124 CMmsOwnNumber::~CMmsOwnNumber()
       
   125     {
       
   126 	Cancel();
       
   127 	delete iContact;
       
   128 	delete iOperation;
       
   129 	delete iResultArray;
       
   130 	delete iContactManager;
       
   131     }
       
   132     
       
   133 // ---------------------------------------------------------
       
   134 // CMmsOwnNumber::DoCancel
       
   135 // ---------------------------------------------------------
       
   136 //
       
   137 void CMmsOwnNumber::DoCancel()
       
   138     {
       
   139 	delete iOperation;
       
   140 	iOperation = NULL;
       
   141 	TRequestStatus* s=&iStatus;
       
   142     User::RequestComplete(s, KErrCancel);
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------
       
   146 // CMmsOwnNumber::RunL
       
   147 // ---------------------------------------------------------
       
   148 //
       
   149 void CMmsOwnNumber::RunL()
       
   150     {
       
   151     
       
   152     delete iOperation;
       
   153     iOperation = NULL;
       
   154     
       
   155     if ( iStatus != KErrNone )
       
   156         {
       
   157 #ifdef _DEBUG
       
   158         TMmsGenUtils::Log( _L("OwnNumber: RunL status: %d "), iStatus.Int() );
       
   159 #endif
       
   160 
       
   161        	// Exit current scheduler loop, resuming execution of the StartL() function
       
   162       	iActiveSchedulerWait.AsyncStop();
       
   163       	return;
       
   164         }
       
   165     
       
   166     switch ( iState )
       
   167         {
       
   168         case EMmsOwnNumberOpening:
       
   169             {
       
   170             iState = EMmsOwnNumberSearching;
       
   171             iMatch = EFalse;
       
   172             SearchL();
       
   173             break;
       
   174             }
       
   175         case EMmsOwnNumberSearching:
       
   176             {
       
   177             iState = EMmsOwnNumberCheckingDuplicates;
       
   178             CheckDuplicatesL();
       
   179             break;
       
   180             }
       
   181         case EMmsOwnNumberCheckingDuplicates:
       
   182             {
       
   183             iState = EMmsOwnNumberIdle;
       
   184             iContactList->CloseAll( *this );
       
   185             break;
       
   186             }
       
   187         default:
       
   188             {
       
   189             break;
       
   190             }
       
   191         }
       
   192     if ( !IsActive() )
       
   193         {
       
   194        	// Exit current scheduler loop, resuming execution of the StartL() function
       
   195         delete iResultArray;
       
   196         iResultArray = NULL;
       
   197       	iActiveSchedulerWait.AsyncStop();
       
   198         }
       
   199     }
       
   200     
       
   201 // ---------------------------------------------------------
       
   202 // CMmsOwnNumber::RunError
       
   203 // ---------------------------------------------------------
       
   204 //
       
   205 
       
   206 #ifdef _DEBUG
       
   207 TInt CMmsOwnNumber::RunError( TInt aError )
       
   208 #else
       
   209 TInt CMmsOwnNumber::RunError( TInt /* aError */ )
       
   210 #endif
       
   211     {
       
   212 #ifdef _DEBUG
       
   213     TMmsGenUtils::Log( _L("OwnNumber: RunError: %d "), aError );
       
   214     TMmsGenUtils::Log( _L("OwnNumber: state: %d "), iState );
       
   215 #endif
       
   216     delete iOperation;
       
   217     iOperation = NULL;
       
   218     delete iResultArray;
       
   219     iResultArray = NULL;
       
   220   	iActiveSchedulerWait.AsyncStop();	//Exit current scheduler loop, resuming execution of the Start() function
       
   221   	return KErrNone;
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------
       
   225 // CMmsOwnNumber::FindCompleteL
       
   226 // ---------------------------------------------------------
       
   227 //
       
   228 void CMmsOwnNumber::FindCompleteL( MVPbkContactLinkArray* aResults )
       
   229     {
       
   230 #ifdef _DEBUG
       
   231     TMmsGenUtils::Log( _L("OwnNumber::FindCompleteL ") );
       
   232 #endif
       
   233     iResultArray = aResults;
       
   234     iContactToMatch = iResultArray->Count(); // point beyound the array
       
   235     iMatch = EFalse;
       
   236     
       
   237     // check the result array for matches and set our index accordingly
       
   238     if ( iResultArray->Count() == 0 )
       
   239         {
       
   240         // we are responsible for the results array if we did not leave.
       
   241         delete iResultArray;
       
   242         iResultArray = 0;
       
   243         // no match found - this is not our own number
       
   244         iNumberToMatch++;
       
   245         if ( iNumberToMatch < iNumberList->MdcaCount() )
       
   246             {
       
   247             // try the next one
       
   248             iState = EMmsOwnNumberOpening;
       
   249             }
       
   250         }
       
   251     else
       
   252         {
       
   253         // we found at least one match.
       
   254         // What do we do now...
       
   255         // Analyze the result in detail or just say "This is our own number"...
       
   256         
       
   257         // First approximation: Say this is our own.
       
   258         // Actually exact match cases should be checked, too (short number)
       
   259         // And possible email match, but that needs a different search function
       
   260                 
       
   261         // save the match and return to RunL
       
   262         // It will stop the active scheduler and allow the code to return to caller
       
   263         iIndex = iNumberToMatch;
       
   264         if ( iNumberList->MdcaPoint( iNumberToMatch ).Length() < iDigitsToMatch ||
       
   265             iResultArray->Count() > 1 )
       
   266             {
       
   267             // need exact match, start from first item in the array
       
   268             iContactToMatch = 0;
       
   269             }
       
   270         }
       
   271     TRequestStatus* s=&iStatus;
       
   272     User::RequestComplete( s, KErrNone );
       
   273     }
       
   274     
       
   275 // ---------------------------------------------------------
       
   276 // CMmsOwnNumber::FindFailed
       
   277 // ---------------------------------------------------------
       
   278 //
       
   279 void CMmsOwnNumber::FindFailed(TInt aError)
       
   280     {
       
   281 #ifdef _DEBUG
       
   282     TMmsGenUtils::Log( _L("OwnNumber: FindFailed: %d "), aError );
       
   283 #endif
       
   284     iIndex = aError;
       
   285  	TRequestStatus* s=&iStatus;
       
   286     iNumberToMatch++;
       
   287     if ( iNumberToMatch < iNumberList->MdcaCount() )
       
   288         {
       
   289         // try the next one
       
   290         iState = EMmsOwnNumberOpening;
       
   291         }
       
   292     User::RequestComplete( s, KErrNone );
       
   293     }
       
   294     
       
   295 // ---------------------------------------------------------
       
   296 // CMmsOwnNumber::OpenComplete
       
   297 // ---------------------------------------------------------
       
   298 //
       
   299 void CMmsOwnNumber::OpenComplete()
       
   300     {
       
   301     // if opening fails, StoreUnavailable is called
       
   302 #ifdef _DEBUG
       
   303     TMmsGenUtils::Log( _L("OwnNumber::OpenCompleteL") );
       
   304 #endif
       
   305 	TRequestStatus* s=&iStatus;
       
   306     User::RequestComplete( s, KErrNone );
       
   307     }
       
   308     
       
   309 // ---------------------------------------------------------
       
   310 // CMmsOwnNumber::StoreReady
       
   311 // ---------------------------------------------------------
       
   312 //
       
   313 void CMmsOwnNumber::StoreReady(MVPbkContactStore& /*aContactStore*/)
       
   314     {
       
   315     // Nothing to do here
       
   316 #ifdef _DEBUG
       
   317     TMmsGenUtils::Log( _L("OwnNumber::StoreReady") );
       
   318 #endif
       
   319     }
       
   320     
       
   321 // ---------------------------------------------------------
       
   322 // CMmsOwnNumber::StoreUnavailable
       
   323 // ---------------------------------------------------------
       
   324 //
       
   325 void CMmsOwnNumber::StoreUnavailable(MVPbkContactStore& /*aContactStore*/, TInt /*aReason*/)
       
   326     {
       
   327 #ifdef _DEBUG
       
   328     TMmsGenUtils::Log( _L("OwnNumber::StoreUnavailable") );
       
   329 #endif
       
   330     iNumberOfOpenStores--;
       
   331     }
       
   332     
       
   333 // ---------------------------------------------------------
       
   334 // CMmsOwnNumber::HandleStoreEventL
       
   335 // ---------------------------------------------------------
       
   336 //
       
   337 void CMmsOwnNumber::HandleStoreEventL(
       
   338     MVPbkContactStore& /*aContactStore*/, 
       
   339     TVPbkContactStoreEvent /*aStoreEvent*/)
       
   340     {
       
   341     
       
   342     }
       
   343     
       
   344 // ---------------------------------------------------------
       
   345 // CMmsOwnNumber::VPbkSingleContactOperationComplete
       
   346 // ---------------------------------------------------------
       
   347 //
       
   348 void CMmsOwnNumber::VPbkSingleContactOperationComplete(
       
   349     MVPbkContactOperationBase& /*aOperation*/,
       
   350     MVPbkStoreContact* aContact)    
       
   351     {
       
   352 #ifdef _DEBUG
       
   353     TMmsGenUtils::Log( _L("OwnNumber::VPbkSingleContactOperationComplete") );
       
   354 #endif
       
   355     iContact = aContact;
       
   356     
       
   357     MVPbkBaseContactFieldCollection& fields = iContact->Fields();
       
   358     
       
   359     TInt fieldCount = fields.FieldCount();
       
   360     
       
   361     TInt i;
       
   362     
       
   363 /*    
       
   364     const TInt maxMatchPriority = iContactManager->FieldTypes().MaxMatchPriority();
       
   365 */    
       
   366     for ( i = 0; i < fieldCount && !iMatch ; i++ )
       
   367         {
       
   368         const MVPbkBaseContactField& field = fields.FieldAt( i );
       
   369 /*        
       
   370         const MVPbkFieldType* fieldType = field.MatchFieldType( maxMatchPriority );
       
   371 */        
       
   372         const MVPbkContactFieldData& fieldData =  field.FieldData();
       
   373         
       
   374         // I could not figure out how to check the actual field type
       
   375         // (phone number or email or what), we must try all text fields
       
   376         if ( fieldData.DataType() == EVPbkFieldStorageTypeText )
       
   377             {
       
   378             const MVPbkContactFieldTextData& textData = MVPbkContactFieldTextData::Cast(fieldData);
       
   379             // Now we finally have the data we can compare with our own data
       
   380             if ( textData.Text().Compare( iNumberList->MdcaPoint( iNumberToMatch ) ) == 0 )
       
   381                 {
       
   382                 // exact match.
       
   383                 iMatch = ETrue;
       
   384                 }
       
   385             }
       
   386         }
       
   387     
       
   388     delete iContact;
       
   389     iContact = NULL;
       
   390     
       
   391     iContactToMatch++;
       
   392     
       
   393     if ( !iMatch && iContactToMatch < iResultArray->Count() )
       
   394         {
       
   395         iState = EMmsOwnNumberSearching;
       
   396         }
       
   397     else if ( !iMatch )
       
   398         {
       
   399         // This was not an exact match
       
   400         iIndex = KErrNotFound;
       
   401         delete iResultArray;
       
   402         iResultArray = 0;
       
   403         iNumberToMatch++;
       
   404         if ( iNumberToMatch < iNumberList->MdcaCount() )
       
   405             {
       
   406             // try the next one if any left
       
   407             iState = EMmsOwnNumberOpening;
       
   408             }
       
   409         }
       
   410     else
       
   411         {
       
   412         // keep LINT happy.
       
   413         }
       
   414         
       
   415  	TRequestStatus* s=&iStatus;
       
   416     User::RequestComplete( s, KErrNone );
       
   417     }
       
   418 
       
   419 // ---------------------------------------------------------
       
   420 // CMmsOwnNumber::
       
   421 // ---------------------------------------------------------
       
   422 //
       
   423 void CMmsOwnNumber::VPbkSingleContactOperationFailed(
       
   424     MVPbkContactOperationBase& /*aOperation*/, 
       
   425 #ifdef _DEBUG
       
   426     TInt aError)
       
   427 #else    
       
   428     TInt /*aError*/)
       
   429 #endif
       
   430     {
       
   431 #ifdef _DEBUG
       
   432     TMmsGenUtils::Log( _L("OwnNumber: ContactOperationFailed: %d "), aError );
       
   433 #endif
       
   434     // If not found iMatch stays false
       
   435     iContactToMatch++;
       
   436     
       
   437  	TRequestStatus* s=&iStatus;
       
   438     User::RequestComplete( s, KErrNone );
       
   439     }
       
   440 
       
   441     
       
   442 // ---------------------------------------------------------
       
   443 // CMmsOwnNumber::StartL
       
   444 // ---------------------------------------------------------
       
   445 //
       
   446 TInt CMmsOwnNumber::StartL( const CDesCArray& aNumberList )
       
   447     {
       
   448     iIndex = KErrNotFound;
       
   449     delete iOperation;
       
   450     iOperation = NULL;
       
   451     
       
   452     if ( aNumberList.MdcaCount() <= 0)
       
   453         {
       
   454         return iIndex; // no contacts, not found
       
   455         }
       
   456     iNumberList = &aNumberList;
       
   457     iNumberToMatch = 0;
       
   458     iContactToMatch = 0;
       
   459         
       
   460     iContactList->OpenAllL( *this );
       
   461         
       
   462     // We must set ourselves to pending because we don't give our status to anybody
       
   463     // When search is complete, the functions call our callbacks, and we complete ourselves
       
   464     iState = EMmsOwnNumberOpening;
       
   465     iStatus = KRequestPending;
       
   466 	SetActive();
       
   467 	iActiveSchedulerWait.Start();	//Re-enter the active scheduler--execution halts here until RunL is called
       
   468 	
       
   469 	// after iActiveSchedulerWait has been stopped execution continues here
       
   470 	
       
   471 	return iIndex; // return the possible index of caller's number
       
   472     }
       
   473     
       
   474 // ---------------------------------------------------------
       
   475 // CMmsOwnNumber::SearchL
       
   476 // ---------------------------------------------------------
       
   477 //
       
   478 void CMmsOwnNumber::SearchL()
       
   479     {
       
   480 #ifdef _DEBUG
       
   481     TMmsGenUtils::Log( _L("OwnNumber::SearchL") );
       
   482 #endif
       
   483   	// Try checking number of open stores to see if anything was opened
       
   484     if ( iNumberOfOpenStores > 0 )
       
   485         {
       
   486         iOperation = iContactManager->MatchPhoneNumberL(
       
   487             iNumberList->MdcaPoint( iNumberToMatch ), iDigitsToMatch, *this );
       
   488     
       
   489         // We must set ourselves to pending because we don't give our status to anybody
       
   490         // When search is complete, the functions call our callbacks, and we complete ourselves
       
   491         iStatus = KRequestPending;
       
   492 	    SetActive();
       
   493         }
       
   494     // If we do not become active, RunL stops ActiveSchedulerWait
       
   495     }
       
   496     
       
   497 // ---------------------------------------------------------
       
   498 // CMmsOwnNumber::CheckDuplicatesL
       
   499 // ---------------------------------------------------------
       
   500 //
       
   501 void CMmsOwnNumber::CheckDuplicatesL()
       
   502     {
       
   503     // If more than one contact found or length of number is less than the 
       
   504     // match number length, check for exact match
       
   505     
       
   506     // The match array is in iResultArray
       
   507     
       
   508 #ifdef _DEBUG
       
   509     TMmsGenUtils::Log( _L("OwnNumber::CheckDuplicatesL") );
       
   510 #endif
       
   511     delete iContact;
       
   512     iContact = NULL;
       
   513     
       
   514     TInt count = 0;
       
   515     if ( iResultArray )
       
   516         {
       
   517         count = iResultArray->Count();
       
   518         }
       
   519     
       
   520     if (  iContactToMatch < count )
       
   521         {
       
   522         iMatch = EFalse;
       
   523         iState = EMmsOwnNumberSearching;
       
   524         iOperation = iContactManager->RetrieveContactL(
       
   525                 iResultArray->At( iContactToMatch ),
       
   526                 *this);
       
   527         iStatus = KRequestPending;
       
   528         SetActive();
       
   529         }
       
   530     else
       
   531         {
       
   532         delete iResultArray;
       
   533         iResultArray = NULL;
       
   534         iStatus = KRequestPending;
       
   535         SetActive();
       
   536  	    TRequestStatus* s=&iStatus;
       
   537         User::RequestComplete( s, KErrNone );
       
   538         }
       
   539     
       
   540     }
       
   541     
       
   542     
       
   543     
       
   544 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   545 
       
   546 //  End of File