Get contacts from Evolution
This commit is contained in:
parent
221daf0363
commit
0240cbbb66
1 changed files with 67 additions and 13 deletions
80
smstool.py
80
smstool.py
|
@ -1,10 +1,42 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
from wxPython.wx import *
|
||||
from os import system
|
||||
from re import sub
|
||||
import bsddb
|
||||
import os
|
||||
import re
|
||||
|
||||
# Constants
|
||||
|
||||
SMSLEN = 160
|
||||
DB_PATH = "~/.evolution/addressbook/local/system/addressbook.db"
|
||||
|
||||
# EvolutionsCells gets cell phone numbers out of the Evolution database
|
||||
|
||||
class EvolutionCells:
|
||||
def __init__(self):
|
||||
contacts = {}
|
||||
self.contacts = contacts
|
||||
|
||||
db = bsddb.hashopen(os.path.expanduser(DB_PATH),"r")
|
||||
self.db = db
|
||||
|
||||
for k in db.keys():
|
||||
name = ""
|
||||
cell = ""
|
||||
for e in db[k].split('\r\n'):
|
||||
if re.search("^FN:", e):
|
||||
name = re.sub("^FN:", "", e)
|
||||
if re.search("^TEL;TYPE=CELL:", e):
|
||||
cell = re.sub("^TEL;TYPE=CELL:", "", e)
|
||||
if cell != "":
|
||||
contacts[name] = cell
|
||||
|
||||
db.close
|
||||
|
||||
def getContacts(self):
|
||||
return self.contacts
|
||||
|
||||
# SimserFrame is the application
|
||||
|
||||
class SimserFrame(wxFrame):
|
||||
def __init__(self, parent, id, title):
|
||||
|
@ -18,7 +50,7 @@ class SimserFrame(wxFrame):
|
|||
|
||||
wxStaticText(panel, -1, "Text:",
|
||||
wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
|
||||
wxStaticText(panel, -1, "Nummer:",
|
||||
wxStaticText(panel, -1, "Contact:",
|
||||
wxDLG_PNT(panel, wxPoint(4, 20)), wxDefaultSize)
|
||||
|
||||
self.textCtrl = wxTextCtrl(panel, 20, "",
|
||||
|
@ -27,11 +59,7 @@ class SimserFrame(wxFrame):
|
|||
self.textCtrl.SetMaxLength(SMSLEN);
|
||||
self.textCtrl.SetFocus()
|
||||
|
||||
self.nrCtrl = wxTextCtrl(panel, -1, "",
|
||||
wxDLG_PNT(panel, wxPoint(30, 20)),
|
||||
wxDLG_SZE(panel, wxSize(60, -1)))
|
||||
|
||||
self.sendCtrl = wxButton(panel, 10, "Senden",
|
||||
self.sendCtrl = wxButton(panel, 10, "Send",
|
||||
wxDLG_PNT(panel, wxPoint(100, 20)),
|
||||
wxDLG_SZE(panel, wxSize(30, -1)))
|
||||
EVT_BUTTON(self, 10, self.OnSendClick)
|
||||
|
@ -41,22 +69,48 @@ class SimserFrame(wxFrame):
|
|||
|
||||
EVT_TEXT(self, 20, self.OnChange)
|
||||
|
||||
# Get contacts
|
||||
cells = EvolutionCells()
|
||||
self.contacts = cells.getContacts()
|
||||
|
||||
# Fill contact list and the combobox
|
||||
|
||||
contactlist = self.contacts.keys()
|
||||
contactlist.sort()
|
||||
self.contactCtrl = wxComboBox(panel, -1, contactlist[0],
|
||||
wxDLG_PNT(panel, wxPoint(30, 20)),
|
||||
wxDLG_SZE(panel, wxSize(60, -1)),
|
||||
contactlist)
|
||||
|
||||
def OnCloseWindow(self, event):
|
||||
self.Destroy()
|
||||
|
||||
def OnSendClick(self, event):
|
||||
text = self.textCtrl.GetValue()
|
||||
nr = self.nrCtrl.GetValue()
|
||||
contact = self.contactCtrl.GetValue()
|
||||
|
||||
# Figure out if we have a number or a name
|
||||
if re.search('^[+0-9]+$', contact):
|
||||
nr = contact
|
||||
else:
|
||||
if self.contacts.has_key(contact):
|
||||
nr = self.contacts[contact]
|
||||
else:
|
||||
self.foodialog = wxMessageDialog(self.panel,
|
||||
"Contact '" + contact + "' is unknown!",
|
||||
"Erros", wxOK, wxPoint(100, 100))
|
||||
self.foodialog.Show(true)
|
||||
return
|
||||
|
||||
# Quote text
|
||||
text = sub("'", "''", text)
|
||||
nr = sub("'", "''", nr)
|
||||
text = re.sub("'", "''", text)
|
||||
nr = re.sub("'", "''", nr)
|
||||
|
||||
# Send SMS and report back
|
||||
rc = system("echo '" + text + "' | gsmsendsms '" + nr + "'")
|
||||
rc = os.system("echo '" + text + "' | gsmsendsms '" + nr + "'")
|
||||
if(rc == 0):
|
||||
self.foodialog = wxMessageDialog(self.panel,
|
||||
"Message sent",
|
||||
"Message sent to " + nr,
|
||||
"Success", wxOK, wxPoint(100, 100))
|
||||
else:
|
||||
self.foodialog = wxMessageDialog(self.panel,
|
||||
|
|
Reference in a new issue