javaextensions/pim/framework/src.s60/cpimitemmatcher.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 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:  An item matcher.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include  "cpimitemmatcher.h"
       
    21 #include  "mpimadaptermanager.h"
       
    22 #include  "cpimvalidator.h"
       
    23 #include  "cpimitem.h"
       
    24 #include  "pimpanics.h"
       
    25 #include "logger.h"
       
    26 
       
    27 CPIMItemMatcher::CPIMItemMatcher(const CPIMValidator& aValidator,
       
    28                                  MPIMAdapterManager& aAdapterManager, const CPIMItem& aMatchingItem) :
       
    29         iValidator(aValidator), // not owned
       
    30         iAdapterManager(aAdapterManager), // not owned
       
    31         iMatchingItem(aMatchingItem) // not owned
       
    32 {
       
    33     JELOG2(EPim);
       
    34 }
       
    35 
       
    36 void CPIMItemMatcher::ConstructL()
       
    37 {
       
    38     JELOG2(EPim);
       
    39     iMatchingFields = iMatchingItem.GetFieldsL();
       
    40 }
       
    41 
       
    42 CPIMItemMatcher* CPIMItemMatcher::NewLC(const CPIMValidator& aValidator,
       
    43                                         MPIMAdapterManager& aAdapterManager, const CPIMItem& aMatchingItem)
       
    44 {
       
    45     JELOG2(EPim);
       
    46     CPIMItemMatcher* self =
       
    47         new(ELeave) CPIMItemMatcher(aValidator, aAdapterManager, aMatchingItem);
       
    48 
       
    49     CleanupStack::PushL(self);
       
    50     self->ConstructL();
       
    51     return self;
       
    52 }
       
    53 
       
    54 CPIMItemMatcher::~CPIMItemMatcher()
       
    55 {
       
    56     JELOG2(EPim);
       
    57     delete iMatchingFields;
       
    58 }
       
    59 
       
    60 TBool CPIMItemMatcher::MatchL(const CPIMItem& aTestedItem)
       
    61 {
       
    62     JELOG2(EPim);
       
    63     const TInt n = iMatchingFields->Count();
       
    64     for (TInt i = 0; i < n; i++)
       
    65     {
       
    66         const TPIMField matchingField = (*iMatchingFields)[i];
       
    67 
       
    68         if (!MatchAllMatchingValuesL(matchingField, aTestedItem))
       
    69         {
       
    70             return EFalse;
       
    71         }
       
    72     }
       
    73 
       
    74     return ETrue;
       
    75 }
       
    76 
       
    77 TBool CPIMItemMatcher::MatchAllMatchingValuesL(TPIMField aMatchingField,
       
    78         const CPIMItem& aTestedItem)
       
    79 {
       
    80     JELOG2(EPim);
       
    81     const TInt n = iMatchingItem.CountValuesL(aMatchingField);
       
    82     for (TInt i = 0; i < n; i++)
       
    83     {
       
    84         if (!FindAnyTestedValueL(aMatchingField, i, // _matching_ value index
       
    85                                  aTestedItem))
       
    86         {
       
    87             return EFalse;
       
    88         }
       
    89     }
       
    90 
       
    91     return ETrue;
       
    92 }
       
    93 
       
    94 TBool CPIMItemMatcher::FindAnyTestedValueL(TPIMField aMatchingField,
       
    95         TInt aMatchingValueIndex, const CPIMItem& aTestedItem)
       
    96 {
       
    97     JELOG2(EPim);
       
    98     const TInt n = aTestedItem.CountValuesL(aMatchingField);
       
    99     for (TInt i = 0; i < n; i++)
       
   100     {
       
   101         if (MatchValueAndAttributesL(aMatchingField, aMatchingValueIndex,
       
   102                                      aTestedItem, i))
       
   103         {
       
   104             return ETrue;
       
   105         }
       
   106     }
       
   107 
       
   108     return EFalse;
       
   109 }
       
   110 
       
   111 TBool CPIMItemMatcher::MatchValueAndAttributesL(TPIMField aMatchingField,
       
   112         TInt aMatchingValueIndex, const CPIMItem& aTestedItem,
       
   113         TInt aTestedValueIndex)
       
   114 {
       
   115     JELOG2(EPim);
       
   116     // Check attributes first
       
   117     if (!MatchAttributesL(aMatchingField, aMatchingValueIndex, aTestedItem,
       
   118                           aTestedValueIndex))
       
   119     {
       
   120         return EFalse;
       
   121     }
       
   122 
       
   123     TBool valueMatches = EFalse;
       
   124 
       
   125     TPIMFieldDataType matchingFieldDataType = iValidator.FieldDataType(
       
   126                 aMatchingField);
       
   127 
       
   128     switch (matchingFieldDataType)
       
   129     {
       
   130     case EPIMFieldBoolean:
       
   131     {
       
   132         valueMatches = MatchBooleanL(aMatchingField, aMatchingValueIndex,
       
   133                                      aTestedItem, aTestedValueIndex);
       
   134         break;
       
   135     }
       
   136 
       
   137     case EPIMFieldDate:
       
   138     {
       
   139         valueMatches = MatchDateL(aMatchingField, aMatchingValueIndex,
       
   140                                   aTestedItem, aTestedValueIndex);
       
   141         break;
       
   142     }
       
   143 
       
   144     case EPIMFieldInt:
       
   145     {
       
   146         valueMatches = MatchIntL(aMatchingField, aMatchingValueIndex,
       
   147                                  aTestedItem, aTestedValueIndex);
       
   148         break;
       
   149     }
       
   150 
       
   151     case EPIMFieldBinary:
       
   152     {
       
   153         valueMatches = MatchBinaryL(aMatchingField, aMatchingValueIndex,
       
   154                                     aTestedItem, aTestedValueIndex);
       
   155         break;
       
   156     }
       
   157 
       
   158     case EPIMFieldString:
       
   159     {
       
   160         valueMatches = MatchStringL(aMatchingField, aMatchingValueIndex,
       
   161                                     aTestedItem, aTestedValueIndex);
       
   162         break;
       
   163     }
       
   164 
       
   165     case EPIMFieldStringArray:
       
   166     {
       
   167         valueMatches = MatchStringArrayL(aMatchingField, aMatchingValueIndex,
       
   168                                          aTestedItem, aTestedValueIndex);
       
   169         break;
       
   170     }
       
   171 
       
   172     default:
       
   173     {
       
   174         // We should never end up here
       
   175         __ASSERT_DEBUG(EFalse, User::Panic(KPIMPanicCategory,
       
   176                                            EPIMPanicInvalidFieldType));
       
   177     }
       
   178     }
       
   179 
       
   180     return valueMatches;
       
   181 }
       
   182 
       
   183 TBool CPIMItemMatcher::MatchAttributesL(TPIMField aMatchingField,
       
   184                                         TInt aMatchingValueIndex, const CPIMItem& aTestedItem,
       
   185                                         TInt aTestedValueIndex)
       
   186 {
       
   187     JELOG2(EPim);
       
   188     // Get the attributes for the matching value.
       
   189     TPIMAttribute matchingAttributes = iMatchingItem.getAttributes(
       
   190                                            aMatchingField, aMatchingValueIndex);
       
   191 
       
   192     // Get rid of unsupported attributes.
       
   193     matchingAttributes &= iAdapterManager. GetSupportedAttributesCombinedL(
       
   194                               aMatchingField);
       
   195 
       
   196     const TPIMAttribute testedAttributes = aTestedItem.getAttributes(
       
   197                                                aMatchingField, aTestedValueIndex);
       
   198 
       
   199     if ((testedAttributes & matchingAttributes) != matchingAttributes)
       
   200     {
       
   201         // Not all matching attributes were contained.
       
   202         return EFalse;
       
   203     }
       
   204 
       
   205     return ETrue;
       
   206 }
       
   207 
       
   208 TBool CPIMItemMatcher::MatchBooleanL(TPIMField aMatchingField,
       
   209                                      TInt aMatchingValueIndex, const CPIMItem& aTestedItem,
       
   210                                      TInt aTestedValueIndex)
       
   211 {
       
   212     JELOG2(EPim);
       
   213     return iMatchingItem.GetBooleanL(aMatchingField, aMatchingValueIndex)
       
   214            == aTestedItem.GetBooleanL(aMatchingField, aTestedValueIndex);
       
   215 }
       
   216 
       
   217 TBool CPIMItemMatcher::MatchDateL(TPIMField aMatchingField,
       
   218                                   TInt aMatchingValueIndex, const CPIMItem& aTestedItem,
       
   219                                   TInt aTestedValueIndex)
       
   220 {
       
   221     JELOG2(EPim);
       
   222     return iMatchingItem.GetDateL(aMatchingField, aMatchingValueIndex)
       
   223            == aTestedItem.GetDateL(aMatchingField, aTestedValueIndex);
       
   224 }
       
   225 
       
   226 TBool CPIMItemMatcher::MatchIntL(TPIMField aMatchingField,
       
   227                                  TInt aMatchingValueIndex, const CPIMItem& aTestedItem,
       
   228                                  TInt aTestedValueIndex)
       
   229 {
       
   230     JELOG2(EPim);
       
   231     return iMatchingItem.getInt(aMatchingField, aMatchingValueIndex)
       
   232            == aTestedItem.getInt(aMatchingField, aTestedValueIndex);
       
   233 }
       
   234 
       
   235 TBool CPIMItemMatcher::MatchBinaryL(TPIMField aMatchingField,
       
   236                                     TInt aMatchingValueIndex, const CPIMItem& aTestedItem,
       
   237                                     TInt aTestedValueIndex)
       
   238 {
       
   239     JELOG2(EPim);
       
   240     // Loop through the byte arrays
       
   241     const CPIMByteArray& matchingByteArray = iMatchingItem.GetBinaryRawL(
       
   242                 aMatchingField, aMatchingValueIndex);
       
   243 
       
   244     const CPIMByteArray& testedByteArray = aTestedItem.GetBinaryRawL(
       
   245                                                aMatchingField, aTestedValueIndex);
       
   246 
       
   247     const TInt n = matchingByteArray.Count();
       
   248     if (testedByteArray.Count() != n)
       
   249     {
       
   250         return EFalse;
       
   251     }
       
   252 
       
   253     for (TInt i = 0; i < n; i++)
       
   254     {
       
   255         if (matchingByteArray[i] != testedByteArray[i])
       
   256         {
       
   257             return EFalse;
       
   258         }
       
   259     }
       
   260 
       
   261     return ETrue;
       
   262 }
       
   263 
       
   264 TBool CPIMItemMatcher::MatchStringL(TPIMField aMatchingField,
       
   265                                     TInt aMatchingValueIndex, const CPIMItem& aTestedItem,
       
   266                                     TInt aTestedValueIndex)
       
   267 {
       
   268     JELOG2(EPim);
       
   269     // Match substring, case-insensitive
       
   270 
       
   271     const TDesC& matchingString = iMatchingItem.GetStringL(aMatchingField,
       
   272                                   aMatchingValueIndex);
       
   273 
       
   274     const TDesC& testedString = aTestedItem.GetStringL(aMatchingField,
       
   275                                 aTestedValueIndex);
       
   276 
       
   277     // Find substring, collated (case-insensitive and
       
   278     // discarding punctuation)
       
   279     if (testedString.FindC(matchingString) == KErrNotFound)
       
   280     {
       
   281         return EFalse;
       
   282     }
       
   283 
       
   284     return ETrue;
       
   285 }
       
   286 
       
   287 TBool CPIMItemMatcher::MatchStringArrayL(TPIMField aMatchingField,
       
   288         TInt aMatchingValueIndex, const CPIMItem& aTestedItem,
       
   289         TInt aTestedValueIndex)
       
   290 {
       
   291     JELOG2(EPim);
       
   292     // Match supported, present elements as
       
   293     // string values
       
   294     const CDesCArray& matchingArray = iMatchingItem.GetStringArrayL(
       
   295                                           aMatchingField, aMatchingValueIndex);
       
   296 
       
   297     const CDesCArray& testedArray = aTestedItem.GetStringArrayL(aMatchingField,
       
   298                                     aTestedValueIndex);
       
   299 
       
   300     const TInt n = matchingArray.Count();
       
   301     for (TInt i = 0; i < n; i++)
       
   302     {
       
   303         const TPtrC& matchingElem = matchingArray[i];
       
   304 
       
   305         if (matchingElem == KPIMNullArrayElement)
       
   306         {
       
   307             continue; // "null" element - next element
       
   308         }
       
   309 
       
   310         const TPtrC& testedElem = testedArray[i];
       
   311 
       
   312         if (testedElem.FindC(matchingElem) == KErrNotFound)
       
   313         {
       
   314             return EFalse;
       
   315         }
       
   316     }
       
   317 
       
   318     return ETrue;
       
   319 }
       
   320 
       
   321 //  End of File