Translated using Weblate.
[wammu.git] / Wammu / PhoneValidator.py
blob0b936667c833be7e92713bab13b9cd7b83bbf1f5
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 - 2010 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 == '' and not self.empty:
74 if immediate:
75 continue
76 else:
77 return False
78 elif self.pause and MATCHER_PAUSE.match(val) == None:
79 return False
80 elif not self.pause and MATCHER_NORMAL.match(val) == None:
81 return False
82 elif immediate and val == '+':
83 continue
84 return True
86 def Validate(self, win = None):
87 textcontrol = self.GetWindow()
88 val = textcontrol.GetValue()
90 result = self.CheckText(val)
92 if not result and win != None:
93 wx.MessageDialog(win,
94 _('You did not specify valid phone number.'),
95 _('Invalid phone number'),
96 wx.OK | wx.ICON_WARNING).ShowModal()
97 textcontrol.SetFocus()
99 return result
101 def OnChar(self, event):
102 key = event.GetKeyCode()
104 # control chars
105 if (key < wx.WXK_SPACE or
106 key == wx.WXK_DELETE or
107 key > 255 or
108 event.AltDown() or
109 event.CmdDown() or
110 event.ControlDown() or
111 event.MetaDown()):
112 event.Skip()
113 return
115 try:
116 char = chr(key)
117 textcontrol = self.GetWindow()
118 pos = textcontrol.GetInsertionPoint()
119 val = textcontrol.GetValue()
120 newval = val[0:pos] + char + val[pos:len(val)]
121 if self.CheckText(newval, immediate = True):
122 event.Skip()
123 return
124 except UnicodeDecodeError:
125 pass
127 # should we bell?
128 if not wx.Validator_IsSilent():
129 wx.Bell()
131 # Returning without calling even.Skip eats the event before it
132 # gets to the text control
133 return