ut_telno_epoc.cpp

Go to the documentation of this file.
00001 /*
00002  !concept {:name => "Phone number to contact name",
00003    :desc => "Resolving a phone number to the name of a matching contact on Symbian."}
00004 */
00005 
00006 #include "ut_telno_epoc.hpp"
00007 
00008 #include "ac_app_context.h"
00009 #include "er_errors.h"
00010 #include "utils_cl2.h"
00011 
00012 #include <cntdb.h>
00013 #include <cntfield.h>
00014 #include <cntfldst.h> 
00015 #include <cntitem.h>
00016 
00017 // The caller is to free any returned object.
00018 static CContactItem* FindContactItemByPhoneNoL(const TDesC& phoneNo)
00019 {
00020   CContactItem* item = NULL;
00021 
00022   if (phoneNo.Length() > 0)
00023   {
00024     // This ought to (typically) be enough for a single match. If not, too bad.
00025     const TInt KNumberOfDigitsToMatch = 8;
00026 
00027     //CContactDatabase* database = CContactDatabase::OpenL(); // xxx use a global copy
00028     //CleanupStack::PushL(database);
00029 
00030     CContactDatabase& database = ac_ContactDatabase(ac_get_global_AppContext());
00031 
00032     CContactIdArray* idArray = database.MatchPhoneNumberL(phoneNo, KNumberOfDigitsToMatch);
00033     CleanupStack::PushL(idArray);
00034     logg("number of contact matches: %d", idArray->Count());
00035     if (idArray->Count() == 1) {
00036       TContactItemId itemId = (*idArray)[0];
00037       TRAPD(errCode, item = database.ReadMinimalContactL(itemId));
00038       if (errCode) {
00039   if (errCode != KErrNotFound) User::Leave(errCode);
00040       }
00041     }
00042     CleanupStack::PopAndDestroy(idArray);
00043 
00044     //CleanupStack::PopAndDestroy(database);
00045   }
00046 
00047   return item; // may be NULL
00048 }
00049 
00050 static gchar* GetNameFromContactItemL(const CContactItem& item)
00051 {
00052   CContactItemFieldSet& fieldSet = item.CardFields();
00053 
00054   TInt givenIx = fieldSet.Find(KUidContactFieldGivenName);
00055   TBool gotGiven = (givenIx != KErrNotFound);
00056 
00057   TInt familyIx = fieldSet.Find(KUidContactFieldFamilyName);
00058   TBool gotFamily = (familyIx != KErrNotFound);
00059 
00060   logg("got given name %d, surname %d", gotGiven, gotFamily);
00061 
00062   _LIT(KSpace, " ");
00063 
00064   TInt bufLen = 0;
00065   if (gotGiven)
00066     bufLen += fieldSet[givenIx].TextStorage()->Text().Length();
00067   if (gotGiven && gotFamily)
00068     bufLen += KSpace().Length();
00069   if (gotFamily)
00070     bufLen += (fieldSet[familyIx].TextStorage()->Text().Length());
00071 
00072   HBufC* buf = HBufC::NewLC(bufLen);
00073   TPtr s(buf->Des());
00074 
00075   if (gotGiven)
00076     s.Append(fieldSet[givenIx].TextStorage()->Text());
00077   if (gotGiven && gotFamily)
00078     s.Append(KSpace);
00079   if (gotFamily)
00080     s.Append(fieldSet[familyIx].TextStorage()->Text());
00081 
00082   gchar* ret = ConvToUtf8CStringL(s);
00083 
00084   CleanupStack::PopAndDestroy(buf);
00085 
00086   return ret;
00087 }
00088 
00089 // The caller is to free any returned buffer.
00090 gchar* GetContactNameByPhoneNoL(const TDesC& phoneNo)
00091 {
00092   gchar* name = NULL;
00093   CContactItem* item = FindContactItemByPhoneNoL(phoneNo);
00094   if (!item) return NULL;
00095   CleanupStack::PushL(item);
00096   name = GetNameFromContactItemL(*item);
00097   if (name) { logg("got contact name '%s'", name); }
00098   else { logt("got no contact name"); }
00099   CleanupStack::PopAndDestroy(item);
00100   return name;
00101 }
00102 
00103 gchar* GetContactNameByPhoneNo(const TDesC& phoneNo)
00104 {
00105   gchar* name = NULL;
00106   TRAP_IGNORE(name = GetContactNameByPhoneNoL(phoneNo));
00107   return name;
00108 }
00109 
00110 /**
00111 
00112 ut_telno_epoc.cpp
00113 
00114 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00115 and the authors. All rights reserved.
00116 
00117 Authors: Tero Hasu <tero.hasu@hut.fi>
00118 
00119 Permission is hereby granted, free of charge, to any person
00120 obtaining a copy of this software and associated documentation files
00121 (the "Software"), to deal in the Software without restriction,
00122 including without limitation the rights to use, copy, modify, merge,
00123 publish, distribute, sublicense, and/or sell copies of the Software,
00124 and to permit persons to whom the Software is furnished to do so,
00125 subject to the following conditions:
00126 
00127 The above copyright notice and this permission notice shall be
00128 included in all copies or substantial portions of the Software.
00129 
00130 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00131 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00132 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00133 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00134 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00135 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00136 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00137 SOFTWARE.
00138 
00139  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1