inc/msgcontacthandler.h
changeset 37 518b245aa84c
child 56 f42d9a78f435
equal deleted inserted replaced
25:84d9eb65b26f 37:518b245aa84c
       
     1 /*
       
     2  * Copyright (c) 2009 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: This header handles the phonebook manager resolve number static
       
    15  *              functions.
       
    16  *
       
    17  */
       
    18 
       
    19 #ifndef MSGCONTACTHANDLER_H_
       
    20 #define MSGCONTACTHANDLER_H_
       
    21 
       
    22 #include <qmobilityglobal.h>
       
    23 
       
    24 #include <qtcontacts.h>
       
    25 #include <qcontactfilter.h>
       
    26 #include <qcontactdetailfilter.h>
       
    27 #include <QFile>
       
    28 #include <qversitcontactimporter.h>
       
    29 #include <qversitdocument.h>
       
    30 #include <qversitreader.h>
       
    31 
       
    32 QTM_BEGIN_NAMESPACE
       
    33 class QContactManager;
       
    34 QTM_END_NAMESPACE
       
    35 
       
    36 QTM_USE_NAMESPACE
       
    37 
       
    38 class MsgContactHandler
       
    39 {
       
    40     
       
    41 public:
       
    42 
       
    43     /**
       
    44      * This shall resolve contact number with display name
       
    45      * @param contactNumber number to resolve
       
    46      * @param displayName resolved name
       
    47      * @param countPhoneNumber specifies number of contacts inside 
       
    48      * the resolved contact ex mobile, home, office etc
       
    49      * @return contacts unique localId
       
    50      */
       
    51     static int resolveContactDisplayName(const QString& contactNumber,
       
    52                                          QString& displayName,
       
    53                                          int& countPhoneNumber)
       
    54     {
       
    55         QContactManager phonebookManager;
       
    56         QVariant address(contactNumber);
       
    57 
       
    58         // apply filter on phone number field
       
    59         QContactDetailFilter phoneFilter;
       
    60         phoneFilter.setDetailDefinitionName(
       
    61                 QContactPhoneNumber::DefinitionName,
       
    62                 QContactPhoneNumber::FieldNumber);
       
    63 
       
    64         phoneFilter.setValue(address);
       
    65         phoneFilter.setMatchFlags(QContactFilter::MatchEndsWith);
       
    66         QList<QContact> matchingContacts =
       
    67                 phonebookManager.contacts(phoneFilter);
       
    68         if (matchingContacts.count() > 0)
       
    69         {
       
    70             // Fill the contact details
       
    71             QContact match = matchingContacts.at(0);
       
    72 
       
    73             displayName = match.displayLabel();
       
    74             QList<QContactPhoneNumber> numbers =
       
    75                     match.details<QContactPhoneNumber> ();
       
    76             countPhoneNumber = numbers.count();
       
    77             return match.localId();
       
    78         }
       
    79 
       
    80         // apply filter on email address field
       
    81         QContactDetailFilter emailFilter;
       
    82         emailFilter.setDetailDefinitionName(
       
    83                 QContactEmailAddress::DefinitionName,
       
    84                 QContactEmailAddress::FieldEmailAddress);
       
    85 
       
    86         emailFilter.setValue(address);
       
    87         emailFilter.setMatchFlags(QContactFilter::MatchExactly);
       
    88         matchingContacts = phonebookManager.contacts(emailFilter);
       
    89         if ( matchingContacts.count() > 0 )
       
    90         {
       
    91             // Fill the contact details
       
    92             QContact match = matchingContacts.at(0);
       
    93 
       
    94             displayName = match.displayLabel();
       
    95             QList<QContactEmailAddress> numbers =
       
    96                     match.details<QContactEmailAddress> ();
       
    97             countPhoneNumber = numbers.count();
       
    98             return match.localId();
       
    99         }
       
   100 
       
   101         // no matching contact
       
   102         displayName = contactNumber;
       
   103         return -1;
       
   104     }
       
   105 
       
   106     /**
       
   107      * This shall resolve contact number with display name
       
   108      * @param contactNumber number to resolve
       
   109      * @param fieldName specifies filter name
       
   110      * @param fieldType specifies filter type
       
   111      * @return contact unique local id
       
   112      */
       
   113     static int resolveContactDisplayName(const QString& contactNumber,
       
   114                                          const QString& fieldName,
       
   115                                          const QString& fieldType)
       
   116     {
       
   117         int contactId = -1;
       
   118 
       
   119         QContactManager phonebookManager("symbian");
       
   120 
       
   121         QContactDetailFilter phoneFilter;
       
   122         phoneFilter.setDetailDefinitionName(fieldName, fieldType);
       
   123         phoneFilter.setValue(contactNumber);
       
   124         phoneFilter.setMatchFlags(QContactFilter::MatchEndsWith);
       
   125 
       
   126         QList<QContact>
       
   127                 matchingContacts = phonebookManager.contacts(phoneFilter);
       
   128 
       
   129         if (matchingContacts.count() > 0)
       
   130         {
       
   131             contactId = matchingContacts.at(0).localId();
       
   132         }
       
   133         return contactId;
       
   134     }
       
   135 
       
   136     //---------------------------------------------------------------
       
   137     // findContactList
       
   138     // Finds the contact matching the phone number.
       
   139     // @param phoneNumber Phone number to be matchned.
       
   140     // @return List of matching contacts.
       
   141     //---------------------------------------------------------------
       
   142     static QList<QContact> findContactList(const QString &phoneNumber)
       
   143     {
       
   144         QContactManager phonebookManager("symbian");
       
   145 
       
   146         QContactDetailFilter phoneFilter;
       
   147         phoneFilter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName,
       
   148             QContactPhoneNumber::FieldNumber);
       
   149         phoneFilter.setValue(phoneNumber);
       
   150         phoneFilter.setMatchFlags(QContactFilter::MatchEndsWith);
       
   151         
       
   152         QList<QContact> matchingContacts = phonebookManager.contacts(phoneFilter);
       
   153 
       
   154         return matchingContacts;
       
   155     }
       
   156     
       
   157     /**
       
   158      * Get display-name of a contact from VCard.
       
   159      * @param filePath, VCard file-path
       
   160      * @return display-name
       
   161      */
       
   162 
       
   163     static QString getVCardDisplayName(QString& filePath)
       
   164     {
       
   165         QString displayName = QString("");
       
   166         //open file for parsing
       
   167         QFile file(filePath);
       
   168         if (!file.open(QIODevice::ReadOnly))
       
   169         {
       
   170             return displayName;
       
   171         }
       
   172         // parse contents
       
   173         QVersitReader reader;
       
   174         reader.setDevice(&file);
       
   175         if (reader.startReading())
       
   176         {
       
   177             if (reader.waitForFinished())
       
   178             {
       
   179                 QList<QVersitDocument> versitDocuments = reader.results();
       
   180                 // Use the resulting document
       
   181                 if (versitDocuments.count() > 0)
       
   182                 {
       
   183                     QVersitContactImporter importer;
       
   184                     bool import_docs = importer.importDocuments(versitDocuments);
       
   185                     if(import_docs)
       
   186                     {
       
   187                        QList<QContact> contacts = importer.contacts();
       
   188                        // get display-name
       
   189                        if (contacts.count() > 0)
       
   190                        {
       
   191                           //resolveSynthesizedDisplayLabel
       
   192                           QContactManager* contactManager =
       
   193                                 new QContactManager("symbian");
       
   194                           displayName
       
   195                                 = contactManager->synthesizedDisplayLabel(contacts[0]);
       
   196                           delete contactManager;
       
   197                        }
       
   198                     }
       
   199                 }
       
   200             }
       
   201         }
       
   202         file.close();
       
   203         return displayName;
       
   204     }
       
   205 };
       
   206 
       
   207 #endif /* MSGCONTACTHANDLER_H_ */