Translated using Weblate.
[wammu.git] / Wammu / EditContactList.py
blob19f7bffcc7e9774a73dd5ea29e0ae8693863ae36
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 - 2010 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, style=wx.LB_EXTENDED)
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, style=wx.LB_EXTENDED)
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.GetSelections()
134 if index == wx.NOT_FOUND:
135 return
136 for i in index:
137 newone = self.numberlist[i]
138 if newone not in self.currentlist:
139 self.currentlist.append(newone)
140 self.current_contacts.Append(newone)
142 def Delete(self, evt=None):
143 index = self.current_contacts.GetSelections()
144 if index == wx.NOT_FOUND:
145 return
146 for i in reversed(index):
147 del self.currentlist[i]
148 self.current_contacts.Delete(i)
150 def Save(self, evt=None):
151 dlg = wx.FileDialog(self, _('Load contacts from file'), os.getcwd(), '', self.wildcard, wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
152 if dlg.ShowModal() == wx.ID_OK:
153 path = dlg.GetPath()
154 try:
155 data = file(path, 'w')
156 for line in self.currentlist:
157 data.write('%s\n' % line)
158 data.close()
159 except IOError:
160 wx.MessageDialog(self,
161 _('Selected file "%s" could not be written.') % path,
162 _('File can not be created!'),
163 wx.OK | wx.ICON_ERROR).ShowModal()
165 def Load(self, evt=None):
166 dlg = wx.FileDialog(self, _('Load contacts from file'), os.getcwd(), '', self.wildcard, wx.OPEN|wx.CHANGE_DIR)
167 if dlg.ShowModal() == wx.ID_OK:
168 path = dlg.GetPath()
169 try:
170 newlist = []
171 data = file(path, 'r')
172 for line in data:
173 newlist.append(line.strip())
174 data.close()
175 self.currentlist = newlist
176 self.current_contacts.Set(newlist)
177 except IOError:
178 wx.MessageDialog(self,
179 _('Selected file "%s" was not found, no data read.') % path,
180 _('File not found!'),
181 wx.OK | wx.ICON_ERROR).ShowModal()