Update locales.
[wammu.git] / Wammu / EditContactList.py
bloba185d2300772484665720602bfc073d543790534
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Dialog for editing phone numbers list.
7 Created with help of wxGlade.
8 '''
9 __author__ = 'Michal Čihař'
10 __email__ = 'michal@cihar.com'
11 __license__ = '''
12 Copyright © 2003 - 2008 Michal Čihař
14 This program is free software; you can redistribute it and/or modify it
15 under the terms of the GNU General Public License version 2 as published by
16 the Free Software Foundation.
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 more details.
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc.,
25 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 '''
28 import sys
29 import os
30 import wx
31 import Wammu.Select
32 import Wammu.Utils
33 import Wammu.PhoneValidator
34 from Wammu.Locales import StrConv
36 class EditContactList(wx.Dialog):
37 def __init__(self, parent, contacts, current, *args, **kwds):
38 self.contacts = contacts
39 self.current = current
40 kwds['style'] = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER
41 wx.Dialog.__init__(self, parent, *args, **kwds)
42 self.__init_data()
43 self.all_label = wx.StaticText(self, -1, _('Available contacts:'))
44 self.all_contacts = wx.ListBox(self, -1, choices=self.optionslist)
45 self.add_button = wx.Button(self, wx.ID_ADD)
46 self.delete_button = wx.Button(self, wx.ID_DELETE)
47 self.current_label = wx.StaticText(self, -1, _('Current recipients:'))
48 self.current_contacts = wx.ListBox(self, -1, choices=self.currentlist)
49 self.save_button = wx.Button(self, wx.ID_SAVEAS)
50 # TODO: Load would be better
51 self.load_button = wx.Button(self, wx.ID_OPEN)
53 self.button_sizer = wx.StdDialogButtonSizer()
54 self.button_sizer.AddButton(wx.Button(self, wx.ID_OK))
55 self.button_sizer.AddButton(wx.Button(self, wx.ID_CANCEL))
57 self.__set_properties()
58 self.__do_layout()
59 self.__bind_events()
61 def __bind_events(self):
62 self.Bind(wx.EVT_BUTTON, self.Add, self.add_button)
63 self.Bind(wx.EVT_BUTTON, self.Delete, self.delete_button)
64 self.Bind(wx.EVT_BUTTON, self.Save, self.save_button)
65 self.Bind(wx.EVT_BUTTON, self.Load, self.load_button)
67 def __init_data(self):
68 self.numberlist = []
69 self.optionslist = []
71 for item in self.contacts:
72 numbers = []
73 texts = []
74 prefix = ''
75 if item['Name'] != '':
76 prefix = '%s: ' % item['Name']
77 for i in range(len(item['Entries'])):
78 if Wammu.Utils.GetItemType(item['Entries'][i]['Type']) == 'phone':
79 numbers.append(item['Entries'][i]['Value'])
80 texts.append(StrConv('%s (%s)' % (item['Entries'][i]['Value'], item['Entries'][i]['Type'])))
82 if len(numbers) == 0:
83 continue
85 for x in range(len(numbers)):
86 self.numberlist.append(numbers[x])
87 self.optionslist.append(prefix + texts[x])
89 self.currentlist = Wammu.PhoneValidator.SplitNumbers(self.current)
90 self.wildcard = ''
91 self.wildcard += _('Contact list') + ' (*.contactlist)|*.contactlist|'
92 self.wildcard += _('All files') + ' (*.*)|*.*'
94 def __set_properties(self):
95 self.SetTitle(_('Edit contacts list'))
97 def __do_layout(self):
98 sizer_1 = wx.BoxSizer(wx.VERTICAL)
99 sizer_1.Add((10, 10), 0, wx.ADJUST_MINSIZE, 0)
100 sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
101 sizer_2.Add((10, 10), 0, wx.ADJUST_MINSIZE, 0)
102 sizer_4 = wx.BoxSizer(wx.VERTICAL)
103 sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
104 sizer_6 = wx.BoxSizer(wx.VERTICAL)
105 sizer_3 = wx.BoxSizer(wx.VERTICAL)
106 sizer_6.Add(self.all_label, 0, wx.BOTTOM | wx.ADJUST_MINSIZE, 5)
107 sizer_6.Add(self.all_contacts, 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0)
108 sizer_2.Add(sizer_6, 1, wx.ALL | wx.EXPAND | wx.ADJUST_MINSIZE, 0)
109 sizer_3.Add(self.add_button, 0, wx.ADJUST_MINSIZE, 0)
110 sizer_3.Add((20, 20), 0, wx.EXPAND | wx.ADJUST_MINSIZE, 0)
111 sizer_3.Add(self.delete_button, 0, wx.ADJUST_MINSIZE, 0)
112 sizer_2.Add(sizer_3, 0, wx.ALL | wx.EXPAND | wx.ADJUST_MINSIZE, 10)
113 sizer_4.Add(self.current_label, 0, wx.BOTTOM | wx.ADJUST_MINSIZE, 5)
114 sizer_4.Add(self.current_contacts, 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0)
115 sizer_5.Add(self.save_button, 0, wx.ALL | wx.ADJUST_MINSIZE, 5)
116 sizer_5.Add(self.load_button, 0, wx.ALL | wx.ADJUST_MINSIZE, 5)
117 sizer_4.Add(sizer_5, 0, wx.EXPAND, 0)
118 sizer_2.Add(sizer_4, 1, wx.EXPAND, 0)
119 sizer_2.Add((10, 10), 0, wx.ADJUST_MINSIZE, 0)
120 sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
121 self.button_sizer.Realize()
122 sizer_1.Add(self.button_sizer, 0, wx.ALIGN_RIGHT, 0)
123 self.SetAutoLayout(True)
124 self.SetSizer(sizer_1)
125 sizer_1.Fit(self)
126 sizer_1.SetSizeHints(self)
127 self.Layout()
129 def GetNumbers(self):
130 return ' '.join(self.currentlist)
132 def Add(self, evt=None):
133 index = self.all_contacts.GetSelection()
134 if index == wx.NOT_FOUND:
135 return
136 newone = self.numberlist[index]
137 self.currentlist.append(newone)
138 self.current_contacts.Append(newone)
140 def Delete(self, evt=None):
141 index = self.current_contacts.GetSelection()
142 if index == wx.NOT_FOUND:
143 return
144 del self.currentlist[index]
145 self.current_contacts.Delete(index)
147 def Save(self, evt=None):
148 dlg = wx.FileDialog(self, _('Load contacts from file'), os.getcwd(), '', self.wildcard, wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
149 if dlg.ShowModal() == wx.ID_OK:
150 path = dlg.GetPath()
151 try:
152 data = file(path, 'w')
153 for line in self.currentlist:
154 data.write('%s\n' % line)
155 data.close()
156 except IOError:
157 wx.MessageDialog(self,
158 _('Selected file "%s" could not be written.') % path,
159 _('File can not be created!'),
160 wx.OK | wx.ICON_ERROR).ShowModal()
162 def Load(self, evt=None):
163 dlg = wx.FileDialog(self, _('Load contacts from file'), os.getcwd(), '', self.wildcard, wx.OPEN|wx.CHANGE_DIR)
164 if dlg.ShowModal() == wx.ID_OK:
165 path = dlg.GetPath()
166 try:
167 newlist = []
168 data = file(path, 'r')
169 for line in data:
170 newlist.append(line.strip())
171 data.close()
172 self.currentlist = newlist
173 self.current_contacts.Set(newlist)
174 except IOError:
175 wx.MessageDialog(self,
176 _('Selected file "%s" was not found, no data read.') % path,
177 _('File not found!'),
178 wx.OK | wx.ICON_ERROR).ShowModal()