plugins/contacts/symbian/plugin/src/filtering/cntfilterdetail.cpp
changeset 5 603d3f8b6302
parent 0 876b1a06bc25
equal deleted inserted replaced
3:e4ebb16b39ea 5:603d3f8b6302
    42 #include <centralrepository.h>
    42 #include <centralrepository.h>
    43 #include <cntfldst.h>
    43 #include <cntfldst.h>
    44 
    44 
    45 #include "cntfilterdetail.h"
    45 #include "cntfilterdetail.h"
    46 #include "cntfilterdetaildisplaylabel.h" //todo rename class to follow naming pattern CntFilterDetailDisplayLabel
    46 #include "cntfilterdetaildisplaylabel.h" //todo rename class to follow naming pattern CntFilterDetailDisplayLabel
    47 #include "cntsqlsearch.h"
       
    48 #include "cntsymbianengine.h"
    47 #include "cntsymbianengine.h"
    49 #include "cnttransformphonenumber.h"
    48 #include "cnttransformphonenumber.h"
    50 
    49 
    51 // Telephony Configuration API
    50 // Telephony Configuration API
    52 // Keys under this category are used in defining telephony configuration.
    51 // Keys under this category are used in defining telephony configuration.
    74         const QContactFilter &filter,
    73         const QContactFilter &filter,
    75         const QList<QContactSortOrder> &sortOrders,
    74         const QList<QContactSortOrder> &sortOrders,
    76         bool &filterSupportedflag,
    75         bool &filterSupportedflag,
    77         QContactManager::Error* error)
    76         QContactManager::Error* error)
    78 {
    77 {
    79     Q_UNUSED(filterSupportedflag);
       
    80     //Check if any invalid filter is passed 
    78     //Check if any invalid filter is passed 
    81     if (!filterSupported(filter) ) {
    79     if (!filterSupported(filter) ) {
    82         *error =  QContactManager::NotSupportedError;
    80         *error =  QContactManager::NotSupportedError;
       
    81         filterSupportedflag = false;
    83         return QList<QContactLocalId>();
    82         return QList<QContactLocalId>();
    84     }
    83     }
    85     QList<QContactLocalId> idList;
    84     QList<QContactLocalId> idList;
    86     QContactDetailFilter detailFilter(filter);
    85     QContactDetailFilter detailFilter(filter);
    87     QString sqlQuery;
    86     QString sqlQuery;
   116 
   115 
   117 bool CntFilterDetail::filterSupported(const QContactFilter& filter) 
   116 bool CntFilterDetail::filterSupported(const QContactFilter& filter) 
   118 {
   117 {
   119     bool result = false;
   118     bool result = false;
   120     if (QContactFilter::ContactDetailFilter == filter.type()) {
   119     if (QContactFilter::ContactDetailFilter == filter.type()) {
   121         result = true;
   120         QContactDetailFilter detailFilter = static_cast<QContactFilter>(filter);
       
   121         if (m_dbInfo.SupportsDetail(detailFilter.detailDefinitionName(),
       
   122                 detailFilter.detailFieldName())) {
       
   123             result = true;
       
   124         }
       
   125         if (detailFilter.detailDefinitionName() == QContactPhoneNumber::DefinitionName &&
       
   126             detailFilter.detailFieldName() == QContactPhoneNumber::FieldNumber) {
       
   127             //cpecial case - phone number matching 
       
   128             result = true;
       
   129         }
   122     }
   130     }
   123     return result;
   131     return result;
   124 }
   132 }
   125 
   133 
   126 void CntFilterDetail::createSelectQuery(const QContactFilter& filter,
   134 void CntFilterDetail::createSelectQuery(const QContactFilter& filter,
   253 }
   261 }
   254 
   262 
   255 QList<QContactLocalId>  CntFilterDetail::HandlePredictiveSearchFilter(const QContactFilter& filter,
   263 QList<QContactLocalId>  CntFilterDetail::HandlePredictiveSearchFilter(const QContactFilter& filter,
   256                                                                       QContactManager::Error* error)
   264                                                                       QContactManager::Error* error)
   257 {
   265 {
   258     QString sqlQuery;
       
   259     
       
   260     if (filter.type() == QContactFilter::ContactDetailFilter) {
   266     if (filter.type() == QContactFilter::ContactDetailFilter) {
   261        const QContactDetailFilter detailFilter(filter);
   267         const QContactDetailFilter detailFilter(filter);
   262        if (detailFilter.matchFlags() == QContactFilter::MatchKeypadCollation) {
   268         if (detailFilter.matchFlags() == QContactFilter::MatchKeypadCollation) {
   263            CntSqlSearch sqlSearch;
       
   264            //convert string to numeric format
       
   265             QString pattern = detailFilter.value().toString();
   269             QString pattern = detailFilter.value().toString();
   266             sqlQuery = sqlSearch.CreatePredictiveSearch(pattern);
   270             //if ( detailFilter.detailFieldName() == QContactEmailAddress::FieldEmailAddress ) {
   267             return  m_srvConnection.searchContacts(sqlQuery, error);  
   271                 return  m_srvConnection.searchOnServer(
   268        }
   272                         pattern, CntSymbianSrvConnection::CntPredictiveSearchList, error);
   269        else {
   273             /*} else {
   270            return QList<QContactLocalId>();
   274                 QString sqlQuery;
   271        }
   275                 CntSqlSearch sqlSearch;
   272     }
   276                 //convert string to numeric format
   273     else {
   277                 sqlQuery = sqlSearch.CreatePredictiveSearch(pattern);
   274         return QList<QContactLocalId>();
   278                 return  m_srvConnection.searchContacts(sqlQuery, error);
   275     }
   279             }*/
   276 }
   280         }
       
   281     }
       
   282     return QList<QContactLocalId>();
       
   283 }
       
   284 
   277 
   285 
   278 /*
   286 /*
   279  * Creates an sql query to fetch contact item IDs for all the contact items
   287  * Creates an sql query to fetch contact item IDs for all the contact items
   280  * which may contain the specified telephone number in a telephone, fax
   288  * which may contain the specified telephone number in a telephone, fax
   281  * or SMS type field.
   289  * or SMS type field.
   337             // Matching 7 or less digits...
   345             // Matching 7 or less digits...
   338             sqlQuery = "SELECT contact_id FROM comm_addr" + whereClause;
   346             sqlQuery = "SELECT contact_id FROM comm_addr" + whereClause;
   339         }
   347         }
   340         else {
   348         else {
   341             // Checking the upper digits...
   349             // Checking the upper digits...
   342             TMatch phoneNumber = createPhoneMatchNumber(
   350         
   343                                   numberPtr, numLowerDigits, numUpperDigits, error);
   351             // select fields for contacts that match phone lookup
   344             QString fieldToMatch = QString(" LIKE '%1").arg(phoneNumber.iUpperDigits) + "%'"  ;
   352             //  SELECT contact_id, extra_value FROM comm_addr
   345             whereClause += " AND extra_value" + fieldToMatch;
   353             //      WHERE value = [value string] AND type = [type value];
   346             sqlQuery = "SELECT contact_id FROM comm_addr" + whereClause;
   354             //
       
   355             QString type =  QString(" type = %1").arg(CntDbInfo::EPhoneNumber);
       
   356             QString value =  QString(" value = %1").arg(phoneDigits.iLowerSevenDigits);
       
   357             QString whereClause = " WHERE" + value + " AND" + type;
       
   358             sqlQuery = "SELECT contact_id, extra_value FROM comm_addr" + whereClause;
       
   359             
       
   360             QList<QPair<QContactLocalId, QString> > contactMatches =  m_srvConnection.searchPhoneNumbers(sqlQuery, error);
       
   361             
       
   362             // Check if search query was successful
       
   363             if (*error != QContactManager::NoError) {
       
   364                   return;
       
   365                 }
       
   366             
       
   367             QStringList list;
       
   368             for (int i=0; i<contactMatches.count(); ++i) {
       
   369                 // Check the upper digits...
       
   370                 TInt32 storedUpperDigits(0);
       
   371                 QString extraValue = contactMatches.at(i).second;
       
   372                 TPtrC extValString(reinterpret_cast<const TUint16*>(extraValue.utf16()));
       
   373                 if (TLex(extValString).Val(storedUpperDigits) == KErrNone) {
       
   374                 
       
   375                     const TInt KDigitsToRemove = KMaxPhoneMatchLength - KLowerSevenDigits - phoneDigits.iNumUpperDigits;
       
   376                     for (TInt j = 0; j < KDigitsToRemove; ++j) {
       
   377                         // repeatedly divide by 10 to lop off the appropriate number of digits from the right
       
   378                         storedUpperDigits /= 10;
       
   379                     }
       
   380                 
       
   381                     storedUpperDigits = TMatch::padOutPhoneMatchNumber(storedUpperDigits, KDigitsToRemove);
       
   382                 
       
   383                     if (phoneDigits.iUpperDigits == storedUpperDigits) {
       
   384                         list.append(QString("%1").arg(contactMatches.at(i).first));
       
   385                     }
       
   386                 }
       
   387                 else {
       
   388                     *error = QContactManager::UnspecifiedError;
       
   389                 }
       
   390             }
       
   391             // Recreate query to fetch all match ids
       
   392             // SELECT DISTINCT contact_id FROM contact WHERE contact_id in (
       
   393             //      ..
       
   394             // )  
       
   395             QString ids = list.join(" ,");
       
   396             sqlQuery = "SELECT DISTINCT contact_id FROM contact WHERE contact_id in (";
       
   397             sqlQuery += ids;
       
   398             sqlQuery += ')';
   347         }
   399         }
   348       
   400       
   349         // refine search
   401         // refine search
   350         if (bestMatchingEnabled()) {
   402         if (bestMatchingEnabled()) {
   351             QList<QContactLocalId> list =  m_srvConnection.searchContacts(sqlQuery,error);
   403             QList<QContactLocalId> list =  m_srvConnection.searchContacts(sqlQuery,error);
   437                                             QContactManager::Error* error)
   489                                             QContactManager::Error* error)
   438 {
   490 {
   439     TMatch phoneNumber = createPhoneMatchNumber(
   491     TMatch phoneNumber = createPhoneMatchNumber(
   440                                             number, numLowerDigits, numUpperDigits, error);
   492                                             number, numLowerDigits, numUpperDigits, error);
   441     if (*error == QContactManager::NoError) {
   493     if (*error == QContactManager::NoError) {
   442         if (phoneNumber.iNumLowerDigits + phoneNumber.iUpperDigits == 0) {
   494         if (phoneNumber.iNumLowerDigits + phoneNumber.iNumUpperDigits == 0) {
   443             // No digits, do nothing
   495             // No digits, do nothing
   444         }
   496         }
   445         else if (phoneNumber.iNumLowerDigits < KLowerSevenDigits) {
   497         else if (phoneNumber.iNumLowerDigits < KLowerSevenDigits) {
   446             // Only the lower-digits hash is used, pad out the number to
   498             // Only the lower-digits hash is used, pad out the number to
   447             // KLowerSevenDigits.
   499             // KLowerSevenDigits.