×
Namespaces

Variants
Actions
(Redirected from Search in contacts)

Archived:Search in contacts using PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

Article Metadata
Tested with
Devices(s): Nokia N95 8GB
Compatibility
Platform(s): S60 3rd Edition
Article
Keywords: e32, contacts, keycodes
Created: pythonkid (27 Oct 2008)
Last edited: hamishwillee (31 May 2013)

Introduction

Contacts database is a useful data storage in phones, Sometimes it has hundreds of entries. But what if you want to find something other than names in it? This code makes you search your contacts using email address, URLs etc.

Code Snippet

Here is a code example that collects all data in Contacts database and categorizes them by their contents to enable users to search in numbers, E-mail addresses, URLs and so on: All using PySymbian

import e32
import appuifw
import contacts
import telephone
from key_codes import EScancode0, EScancode1, EScancode2, EScancode3, EScancode4, EScancode5
 
digits='0123456789+'
 
currentList=[]
mailList=[]
nameList=[]
numList=[]
otherList=[]
wwwList=[]
myDic={}
 
db=contacts.open()
 
def FillLists(): #loop thru entire database ignoring field names to collect data. runs only once at startup
global currentList, mailList, nameList, numList, otherList, wwwList, myDic
for recordKey in db:
nameList.append(db[recordKey].title) # collect names for start
myDic[db[recordKey].title]=recordKey # to get record key having title
for field in db[recordKey]:
data=field.value
if data[0] in digits: # is it a number?
str=data[:-7]+" "+data[-7:-4]+" "+data[-4:] # break it to tree parts
numList.append(str)
elif '@' in data: # is it a E-mail address?
str=data[:data.find('@')+1]+" "+data[data.find('@')+1:] # two parts (enable users to search domain names too)
mailList.append(str)
elif 'www.' in data: # is it a URL?
str=data[:data.find('.')+1]+" "+data[data.find('.')+1:data.rfind('.')+1]+" "+data[data.rfind('.')+1:]
wwwList.append(str)
elif 'http:' in data: # is it a URL without www?
str=data[:data.find('/')+2]+" "+data[data.find('/')+2:data.rfind('.')+1]+" "+data[data.rfind('.')+1:]
wwwList.append(str)
else: # or just a note?
str=data
otherList.append(str)
myDic[str]=recordKey # to get record key having any type of data
 
def Call(): # just to give ability to call a person instantly
oldTitle=appuifw.app.title
appuifw.app.title=u'Dial'
tmpList=[]
for i in db[myDic[currentList[mainList.current()]]]: # loop thru specified record's fields
if i.value[0] in digits: # is field content a number?
tmpList.append(i.value) #add it to list
if len(tmpList)==0:
appuifw.note(u'No numbers found' ,'error')
elif len(tmpList)==1:
telephone.dial(tmpList[0])
else:
index=appuifw.selection_list(tmpList)
telephone.dial(tmpList[index])
appuifw.app.title=oldTitle
 
def Search(inList): # this prepares a selection list using given list
oldTitle=appuifw.app.title
appuifw.app.title=u'Search'
index=appuifw.multi_selection_list(inList,'checkbox',1)
tmpList=[]
for i in index: # collect titles of selected records
dbID=myDic[inList[i]]
tmpList.append(db[dbID].title)
global currentList
if len(tmpList)>0:
currentList=tmpList
mainList.set_list(currentList)
appuifw.app.title=oldTitle
 
APP_LOCK=e32.Ao_lock()
mainList=appuifw.Listbox([u'Collecting data...', u'Please wait']) # may take several seconds
mainList.bind(EScancode1, lambda:Search(nameList)) # define shortcut keys
mainList.bind(EScancode2, lambda:Search(numList))
mainList.bind(EScancode3, lambda:Search(mailList))
mainList.bind(EScancode4, lambda:Search(wwwList))
mainList.bind(EScancode5, lambda:Search(otherList))
mainList.bind(EScancode0, lambda:mainList.set_list(nameList))
appuifw.app.body=mainList
appuifw.app.exit_key_handler=APP_LOCK.signal
appuifw.app.title=u'Contact search'
appuifw.app.menu=[(u"Call", lambda:Call()),
(u"[1] Search in names", lambda:Search(nameList)),
(u"[2] Search in numbers", lambda:Search(numList)),
(u"[3] Search in mails", lambda:Search(mailList)),
(u"[4] Search in URLs", lambda:Search(wwwList)),
(u"[5] Search in others", lambda:Search(otherList)),
(u"[0] View all", lambda:mainList.set_list(nameList))]
FillLists()
currentList=nameList # start with names
mainList.set_list(currentList)
APP_LOCK.wait()


Advantages

Maybe a smaller code that gives ability to search in specified field be useful, but what if you (for example) enter an E-mail address in Notes field? that's why this program reads all data ignoring field names and categorized them by their contents.

This page was last modified on 31 May 2013, at 04:05.
56 page views in the last 30 days.
×