There is no Canonical copyright here.
[wammu.git] / Wammu / Select.py
blob07202d8195dc67e387c9d281e5e02e16fd9668c0
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Contact and phone number select dialogs
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 Wammu.Utils
28 from Wammu.Locales import StrConv
30 def SortName(item1, item2):
31 '''
32 Comparator function for sorting by name.
33 '''
34 return cmp(item1['Name'], item2['Name'])
36 def SelectContact(parent, contactlist, index = False):
37 '''
38 Dialog for selecting contact.
39 '''
40 contactlist.sort(SortName)
41 choices = []
42 for entry in contactlist:
43 if entry['Name'] == '':
44 choices.append(StrConv(entry['Number']))
45 else:
46 choices.append(StrConv(entry['Name']))
48 dlg = wx.SingleChoiceDialog(
49 parent,
50 _('Select contact from below list'),
51 _('Select contact'),
52 choices,
53 wx.CHOICEDLG_STYLE | wx.RESIZE_BORDER)
54 if dlg.ShowModal() == wx.ID_OK and len(choices) > 0:
55 result = dlg.GetSelection()
56 if not index:
57 result = contactlist[result]['Location']
58 else:
59 result = -1
60 del dlg
61 return result
63 def SelectNumber(parent, contactlist):
64 '''
65 Allows user to select number from phone list. First it asks for contact
66 and then which number to use.
67 '''
68 i = SelectContact(parent, contactlist, True)
69 if i == -1:
70 return None
71 return SelectContactNumber(parent, contactlist[i])
73 def SelectContactNumber(parent, item):
74 '''
75 Selects number of chosen contact. If it has single number, it returns it
76 directly, otherwise user has to select which number to use.
77 '''
78 numbers = []
79 texts = []
80 for i in range(len(item['Entries'])):
81 if Wammu.Utils.GetItemType(item['Entries'][i]['Type']) == 'phone':
82 numbers.append(item['Entries'][i]['Value'])
83 texts.append(StrConv(u'%s : %s' % (
84 item['Entries'][i]['Type'],
85 item['Entries'][i]['Value'])))
87 if len(numbers) == 0:
88 return None
89 elif len(numbers) == 1:
90 return numbers[0]
91 dlg = wx.SingleChoiceDialog(
92 parent,
93 _('Select number for selected contact'),
94 _('Select phone number'),
95 texts,
96 wx.CHOICEDLG_STYLE | wx.RESIZE_BORDER)
97 if dlg.ShowModal() == wx.ID_OK:
98 result = numbers[dlg.GetSelection()]
99 else:
100 result = None
101 del dlg
102 return result