Merge branch 'master' of /home/git/wammu
[wammu.git] / Wammu / PhoneValidator.py
blob8ea546f242b95e262e2dab703c040f8da4911b8e
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Phone number validator.
6 '''
7 __author__ = 'Michal Čihař'
8 __email__ = 'michal@cihar.com'
9 __license__ = '''
10 Copyright © 2003 - 2009 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 import wx
27 import re
29 MATCHER_NORMAL = re.compile('^[0-9*#+]+$')
30 MATCHER_PAUSE = re.compile('^[Pp0-9*#+]+$')
31 MATCH_SPLIT = re.compile('[\s;,]+')
33 def SplitNumbers(text):
34 '''
35 Splits text to list of phone numbers.
36 '''
37 lst = MATCH_SPLIT.split(text)
38 if lst[0] == '':
39 del lst[0]
40 if len(lst) > 0 and lst[len(lst) - 1] == '':
41 del lst[len(lst) - 1]
42 return lst
44 class PhoneValidator(wx.PyValidator):
45 '''
46 Validator for phone numbers.
47 '''
48 def __init__(self, multi=False, pause=False, empty=False):
49 wx.PyValidator.__init__(self)
50 self.multi = multi
51 self.pause = pause
52 self.empty = empty
53 self.Bind(wx.EVT_CHAR, self.OnChar)
55 def Clone(self):
56 return PhoneValidator(self.multi, self.pause, self.empty)
58 def TransferToWindow(self):
59 return True
61 def TransferFromWindow(self):
62 return True
64 def CheckText(self, text, immediate = False):
65 '''
66 Verifies whether enterd text is correct.
67 '''
68 if self.multi:
69 values = SplitNumbers(text)
70 else:
71 values = [text]
72 for val in values:
73 if val == '':
74 result = self.empty
75 elif self.pause and MATCHER_PAUSE.match(val) == None:
76 return False
77 elif not self.pause and MATCHER_NORMAL.match(val) == None:
78 return False
79 elif immediate and val == '+':
80 continue
81 return True
83 def Validate(self, win = None):
84 textcontrol = self.GetWindow()
85 val = textcontrol.GetValue()
87 result = self.CheckText(val)
89 if not result and win != None:
90 wx.MessageDialog(win,
91 _('You did not specify valid phone number.'),
92 _('Invalid phone number'),
93 wx.OK | wx.ICON_WARNING).ShowModal()
94 textcontrol.SetFocus()
96 return result
98 def OnChar(self, event):
99 key = event.GetKeyCode()
101 # control chars
102 if (key < wx.WXK_SPACE or
103 key == wx.WXK_DELETE or
104 key > 255 or
105 event.AltDown() or
106 event.CmdDown() or
107 event.ControlDown() or
108 event.MetaDown()):
109 event.Skip()
110 return
112 try:
113 char = chr(key)
114 textcontrol = self.GetWindow()
115 pos = textcontrol.GetInsertionPoint()
116 val = textcontrol.GetValue()
117 newval = val[0:pos] + char + val[pos:len(val)]
118 if self.CheckText(newval, immediate = True):
119 event.Skip()
120 return
121 except UnicodeDecodeError:
122 pass
124 # should we bell?
125 if not wx.Validator_IsSilent():
126 wx.Bell()
128 # Returning without calling even.Skip eats the event before it
129 # gets to the text control
130 return