smsearch是一个使用pys60编写的短信搜索程序,代码来自
wordmobi的开发者,wordmobi是一个很棒的运行在s60上的wordpress管理程序,使用pys60编写,代码非常值得一看,这个短信搜索程序是一个学习pys60编程的不错例子。介绍这个程序也是想说明,pys60编程不仅像吃奶酪一样简单,而且能吃的很优雅。
smsearch程序共有3个py文件:
window.py,
default.py,
smsearch.py。
sis文件:
SMSearch1.0.0.sis
window.py封装了一个优良的ui基础框架,这个例子在nokia forum上有一篇非常详细的介绍
Basic framework for creating user interface,值得一看,这是一个可高度重用的组件。
window模块包括三个类:
Window——一个基本的窗口组件,对菜单,标题,主体及事件处理作了良好的封装,同时具有对窗口进行锁定和解锁的功能。
Application——该类继承于Window,主要用来运行应用程序本身并负责处理程序终止事件。对于一个应用程序只允许一个Application实例
Dialog——继承于Window,一个对话框窗口,他有一个回调函数,在窗口关闭时该回调函数会被调用。
smsearch.py,基于上述window框架的短信搜索应用程序,从软键选择查询菜单,在查询框中输入关键字,查询,返回结果,点击单条短信查看内容,这就是他所能做的,够简单的。
他包括两个类:
SMSearch——继承于Application,主要工作就是启动运行主程序,打开输入框,执行查询逻辑,返回查询结果
ShowSMS——继承于Dialog,用来显示单条短信
from window import *
from appuifw import *
import inbox
import time
class ShowSMS(Dialog):
def __init__(self,cbk,title,msg):
Dialog.__init__(self,cbk,title,Text(msg))
class SMSearch(Application):
def __init__(self):
self.dlg = None
self.results = [u""]
self.terms = u""
body = Canvas()
menu = [(u"Search", self.get_pattern),
(u"About", self.about),
(u"Exit", self.close_app)]
Application.__init__(self,u"SMSearch",body,menu)
def about(self):
note( u"SMSearch.\nMarcelo Barros de Almeida\[email protected]", "info" )
def lst_cbk(self):
idx = self.body.current()
(sms_id,txt,tmr,addr,fn) = self.results[idx]
msg = u"Address: " + addr + \
u"\nDate: " + tmr + \
u"\nBox: " + fn + \
u"\n\n" + txt
self.dlg = ShowSMS(lambda:self.refresh(),unicode(sms_id),msg)
self.dlg.run()
def bmh_search(self, pattern, text):
# http://code.activestate.com/recipes/117223/
m = len(pattern)
n = len(text)
if m > n: return -1
skip = []
for k in range(256): skip.append(m)
for k in range(m - 1): skip[ord(pattern[k])] = m - k - 1
skip = tuple(skip)
k = m - 1
while k < n:
j = m - 1; i = k
while j >= 0 and text[i] == pattern[j]:
j -= 1; i -= 1
if j == -1: return i + 1
k += skip[ord(text[k])]
return -1
def search(self,pattern):
self.results = []
lst = []
folders = {inbox.EInbox:u"Inbox",
inbox.EOutbox:u"Outbox",
inbox.ESent:u"Sent",
inbox.EDraft:u"Draft"}
for f,fn in folders.iteritems():
ibx = inbox.Inbox(f)
for sms_id in ibx.sms_messages():
txt = ibx.content(sms_id)
txt_utf8 = txt.encode('utf-8')
pattern_utf8 = pattern.encode('utf-8')
if self.bmh_search(pattern_utf8.lower(),txt_utf8.lower()) > -1:
tm = ibx.time(sms_id)
dt = unicode(time.ctime(tm),'utf-8',errors='ignore')
self.results.append((sms_id,txt,dt,ibx.address(sms_id),fn))
lst.append((tm,dt,txt[:50]))
if self.results:
# order following unix time and remove it after
lst.sort(reverse=True)
lst = map(lambda x: x[1:],lst)
self.body = Listbox(lst,self.lst_cbk)
app.screen = 'normal' # avoid wrong screen redraw
self.refresh()
else:
note(u"No results for " + self.terms,"info")
def get_pattern(self):
pattern = query(u"Search terms:", "text", self.terms)
if pattern is not None:
if pattern:
pattern = pattern.strip()
self.terms = pattern
self.search(pattern)
if __name__ == "__main__":
sms = SMSearch()
sms.run()
default.py,
import e32
import sys
import os
# looking for install dir
DEFDIR = u""
for d in e32.drive_list():
appd = d + u"\\data\\python\\smsearch\\"
if os.path.exists(appd + u"smsearch.py"):
DEFDIR = appd
break
if DEFDIR:
sys.path.append(appd)
from smsearch import SMSearch
SMSearch().run()