Translated using Weblate.
[wammu.git] / Wammu / TalkbackDialog.py
blob4de3f5f96b37dffd08d67bcd99b27a92661be5ca
1 # -*- coding: UTF-8 -*-
2 # generated by wxGlade 0.4.1 on Thu Feb 8 13:07:50 2007
3 # vim: expandtab sw=4 ts=4 sts=4:
4 '''
5 Wammu - Phone manager
6 Gammu Phone Database Talkback window
7 '''
8 __author__ = 'Michal Čihař'
9 __email__ = 'michal@cihar.com'
10 __license__ = '''
11 Copyright © 2003 - 2010 Michal Čihař
13 This program is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License version 2 as published by
15 the Free Software Foundation.
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 more details.
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 '''
28 import httplib
29 import urllib
30 import Wammu.Webbrowser
31 import re
32 import wx
33 import Wammu.TalkbackFeaturesDialog
34 import Wammu.Utils
35 import Wammu.Data
36 from Wammu.Locales import StrConv
37 if Wammu.gammu_error == None:
38 import gammu
40 OK_MATCHER = re.compile('Entry created, id=(\d*), url=(.*)')
41 FAIL_MATCHER = re.compile('Invalid values: (.*)')
43 # begin wxGlade: dependencies
44 # end wxGlade
46 class TalkbackDialog(wx.Dialog):
47 def __init__(self, parent, config, phoneid = 0, *args, **kwds):
48 # begin wxGlade: TalkbackDialog.__init__
49 kwds["style"] = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.THICK_FRAME
50 wx.Dialog.__init__(self, parent, *args, **kwds)
51 self.main_panel = wx.Panel(self, -1)
52 self.info_label = wx.StaticText(self, -1, _("Please share your experiences with Wammu and Gammu which is backend library. When you fill in following form, other users can benefit from your experiences in Gammu Phone Database. Only information you see here will be submited."))
53 self.top_static_line = wx.StaticLine(self, -1)
54 self.manufacturer_label = wx.StaticText(self.main_panel, -1, _("Manufacturer:"), style=wx.ALIGN_CENTRE)
55 self.manufacturer_choice = wx.Choice(self.main_panel, -1, choices=[])
56 self.model_label = wx.StaticText(self.main_panel, -1, _("Phone model:"))
57 self.model_text_ctrl = wx.TextCtrl(self.main_panel, -1, "")
58 self.connection_label = wx.StaticText(self.main_panel, -1, _("Connection type:"))
59 self.connection_combo_box = wx.ComboBox(self.main_panel, -1, choices=[], style=wx.CB_DROPDOWN)
60 self.gammu_model_label = wx.StaticText(self.main_panel, -1, _("Model in gammu configuration:"))
61 self.model_combo_box = wx.ComboBox(self.main_panel, -1, choices=[], style=wx.CB_DROPDOWN)
62 self.features_label = wx.StaticText(self.main_panel, -1, _("Working features:"))
63 self.features_button = wx.Button(self.main_panel, -1, _("Please select features..."))
64 self.gammu_version_text_label = wx.StaticText(self.main_panel, -1, _("Gammu version:"))
65 self.gammu_version_label = wx.StaticText(self.main_panel, -1, "")
66 self.note_label = wx.StaticText(self.main_panel, -1, _("Note:"))
67 self.note_text_ctrl = wx.TextCtrl(self.main_panel, -1, "", style=wx.TE_MULTILINE)
68 self.name_label = wx.StaticText(self.main_panel, -1, _("Your name:"))
69 self.name_text_ctrl = wx.TextCtrl(self.main_panel, -1, "")
70 self.email_label = wx.StaticText(self.main_panel, -1, _("Your email:"))
71 self.email_text_ctrl = wx.TextCtrl(self.main_panel, -1, "")
72 self.mangle_label = wx.StaticText(self.main_panel, -1, _("Email displaying:"), style=wx.ALIGN_CENTRE)
73 self.mangle_choice = wx.Choice(self.main_panel, -1, choices=[_("Use [at] and [dot]"), _("Insert NOSPAM text at random position"), _("Display it normally"), _("Don't show email at all")])
74 self.bottom_static_line = wx.StaticLine(self, -1)
76 self.__set_properties()
77 self.__do_layout()
79 self.Bind(wx.EVT_BUTTON, self.OnFeatures, self.features_button)
80 # end wxGlade
81 self.ns_string = '<%s>' % _('Not supported')
82 self.connection_combo_box.Append(self.ns_string)
83 for x in Wammu.Data.Connections:
84 self.connection_combo_box.Append(x)
85 for x in Wammu.Data.Models:
86 self.model_combo_box.Append(x)
87 for x in Wammu.Data.ManufacturerMap.keys():
88 self.manufacturer_choice.Append(x)
89 self.wammu_cfg = config
90 self.phoneid = phoneid
91 self.features = []
92 self.gammu_version_label.SetLabel(gammu.Version()[0])
93 self.note_text_ctrl.SetValue('Report has been created using Wammu %s.\n' % Wammu.__version__)
94 # Read phone name and manufacturer
95 manufacturer = self.wammu_cfg.Read('/Phone-%d/Manufacturer' % phoneid)
96 self.manufacturer_choice.SetStringSelection(manufacturer)
97 model = self.wammu_cfg.Read('/Phone-%d/Model' % phoneid)
98 self.model_text_ctrl.SetValue(model)
99 # Set connection type which is being used
100 section = self.wammu_cfg.ReadInt('/Gammu/Section')
101 config = self.wammu_cfg.gammu.GetConfig(section)
102 self.connection_combo_box.SetValue(config['Connection'])
103 self.model_combo_box.SetValue(config['Model'])
104 self.name_text_ctrl.SetValue(self.wammu_cfg.Read('/User/Name'))
105 self.email_text_ctrl.SetValue(self.wammu_cfg.Read('/User/Email'))
108 def __set_properties(self):
109 # begin wxGlade: TalkbackDialog.__set_properties
110 self.SetTitle(_("Gammu Phone Database Talkback"))
111 self.features_label.SetToolTipString(_("Select which features work correctly with your phone"))
112 self.gammu_version_label.SetToolTipString(_("This information is automatically included in report."))
113 self.note_text_ctrl.SetToolTipString(_("Describe some glitches of this phone or other experiences with Gammu."))
114 self.email_text_ctrl.SetToolTipString(_("Please enter valid mail here, choose display options below. Your email won't be given or sold to anybody."))
115 self.mangle_choice.SetToolTipString(_("If you don't want to display email clear text, please select one mangling option."))
116 self.mangle_choice.SetSelection(0)
117 # end wxGlade
119 def __do_layout(self):
120 self.button_sizer = wx.StdDialogButtonSizer()
121 self.button_sizer.AddButton(wx.Button(self, wx.ID_OK))
122 self.button_sizer.AddButton(wx.Button(self, wx.ID_CANCEL))
123 self.info_label.Wrap(400)
124 # begin wxGlade: TalkbackDialog.__do_layout
125 window_grid_sizer = wx.FlexGridSizer(3, 1, 0, 0)
126 main_grid_sizer = wx.FlexGridSizer(8, 2, 5, 5)
127 window_grid_sizer.Add(self.info_label, 2, wx.ALL|wx.EXPAND, 5)
128 window_grid_sizer.Add(self.top_static_line, 0, wx.ALL|wx.EXPAND, 5)
129 main_grid_sizer.Add(self.manufacturer_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
130 main_grid_sizer.Add(self.manufacturer_choice, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
131 main_grid_sizer.Add(self.model_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
132 main_grid_sizer.Add(self.model_text_ctrl, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
133 main_grid_sizer.Add(self.connection_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
134 main_grid_sizer.Add(self.connection_combo_box, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
135 main_grid_sizer.Add(self.gammu_model_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
136 main_grid_sizer.Add(self.model_combo_box, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
137 main_grid_sizer.Add(self.features_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
138 main_grid_sizer.Add(self.features_button, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
139 main_grid_sizer.Add(self.gammu_version_text_label, 0, wx.ADJUST_MINSIZE, 0)
140 main_grid_sizer.Add(self.gammu_version_label, 0, wx.ADJUST_MINSIZE, 0)
141 main_grid_sizer.Add(self.note_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
142 main_grid_sizer.Add(self.note_text_ctrl, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
143 main_grid_sizer.Add(self.name_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
144 main_grid_sizer.Add(self.name_text_ctrl, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
145 main_grid_sizer.Add(self.email_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
146 main_grid_sizer.Add(self.email_text_ctrl, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
147 main_grid_sizer.Add(self.mangle_label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
148 main_grid_sizer.Add(self.mangle_choice, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
149 self.main_panel.SetAutoLayout(True)
150 self.main_panel.SetSizer(main_grid_sizer)
151 main_grid_sizer.Fit(self.main_panel)
152 main_grid_sizer.SetSizeHints(self.main_panel)
153 main_grid_sizer.AddGrowableRow(5)
154 main_grid_sizer.AddGrowableCol(1)
155 window_grid_sizer.Add(self.main_panel, 0, wx.EXPAND, 0)
156 window_grid_sizer.Add(self.bottom_static_line, 0, wx.ALL|wx.EXPAND, 3)
157 self.SetAutoLayout(True)
158 self.SetSizer(window_grid_sizer)
159 window_grid_sizer.Fit(self)
160 window_grid_sizer.SetSizeHints(self)
161 window_grid_sizer.AddGrowableRow(1)
162 window_grid_sizer.AddGrowableCol(0)
163 self.Layout()
164 # end wxGlade
165 self.button_sizer.Realize()
166 window_grid_sizer.Add(self.button_sizer, 0, wx.ALIGN_RIGHT, 0)
167 window_grid_sizer.Fit(self)
168 self.Layout()
169 wx.EVT_BUTTON(self, wx.ID_OK, self.Okay)
171 def OnFeatures(self, event): # wxGlade: TalkbackDialog.<event_handler>
172 dlg = Wammu.TalkbackFeaturesDialog.TalkbackFeaturesDialog(self)
173 dlg.SetFeatures(self.features)
174 if dlg.ShowModal() == wx.ID_OK:
175 self.features = dlg.GetFeatures()
176 self.features_button.SetLabel(', '.join(self.features))
178 def Okay(self, evt):
179 connection = self.connection_combo_box.GetValue()
180 if connection == self.ns_string:
181 connection = 'NULL'
182 if len(self.features) == 0 and connection != 'NULL':
183 wx.MessageDialog(self,
184 _('Entry in Gammu Phone Database was not created, following fields are invalid:\n%s') % _('Supported features'),
185 _('Entry not created!'),
186 wx.OK | wx.ICON_ERROR).ShowModal()
187 return
188 elif len(self.features) != 0 and connection == 'NULL':
189 wx.MessageDialog(self,
190 _('Entry in Gammu Phone Database was not created, following fields are invalid:\n%s') % _('Supported features'),
191 _('Entry not created!'),
192 wx.OK | wx.ICON_ERROR).ShowModal()
193 return
194 man_str = self.manufacturer_choice.GetStringSelection()
195 try:
196 man_id = Wammu.Data.ManufacturerMap[man_str]
197 except:
198 wx.MessageDialog(self,
199 _('Entry in Gammu Phone Database was not created, following fields are invalid:\n%s') % _('Manufacturer'),
200 _('Entry not created!'),
201 wx.OK | wx.ICON_ERROR).ShowModal()
202 return
203 garble_id = self.mangle_choice.GetSelection()
204 try:
205 garble_text = Wammu.Data.GarbleMap[garble_id]
206 except:
207 wx.MessageDialog(self,
208 _('Entry in Gammu Phone Database was not created, following fields are invalid:\n%s') % _('Email displaying'),
209 _('Entry not created!'),
210 wx.OK | wx.ICON_ERROR).ShowModal()
211 return
213 # Remember user information for next run
214 self.wammu_cfg.Write('/User/Name', self.name_text_ctrl.GetValue())
215 self.wammu_cfg.Write('/User/Email', self.email_text_ctrl.GetValue())
217 # Prepare data to post
218 params_dict = {
219 'irobot': 'wammu',
220 'version': '2',
221 'manufacturer': man_id,
222 'name': self.model_text_ctrl.GetValue(),
223 'model': self.model_combo_box.GetValue(),
224 'connection': connection,
225 'note': self.note_text_ctrl.GetValue(),
226 'author_name': self.name_text_ctrl.GetValue(),
227 'author_email': self.email_text_ctrl.GetValue(),
228 'email_garble': garble_text,
229 'gammu_version': gammu.Version()[0],
231 for x in self.features:
232 params_dict['fts[%s]' % x] = 1
234 # Convert unicode to raw utf-8 strigns so that they can be properly
235 # handled by urllib and later by website
236 for x in params_dict.keys():
237 if type(params_dict[x]) == unicode:
238 params_dict[x] = params_dict[x].encode('utf-8')
240 # Encode request and prepare headers
241 params = urllib.urlencode(params_dict)
242 headers = {'Content-type': 'application/x-www-form-urlencoded',
243 'Accept': 'text/plain'}
245 # Perform request
246 conn = httplib.HTTPConnection('wammu.eu')
247 try:
248 conn.request('POST', '/api/phones/new/', params, headers)
250 # Check request response
251 response = conn.getresponse()
252 if response.status != 200:
253 wx.MessageDialog(self,
254 _('HTTP request failed with status %(code)d (%(text)s), please retry later or create entry manually.') % {
255 'code': response.status,
256 'text': response.reason,
258 _('Entry not created!'),
259 wx.OK | wx.ICON_ERROR).ShowModal()
260 return
261 except Exception, e:
262 if hasattr(e, 'message') and e.message != '':
263 msg = e.message
264 elif hasattr(e, 'args') and len(e.args) > 0:
265 msg = e.args[-1]
266 else:
267 msg = str(e)
268 wx.MessageDialog(self,
269 _('HTTP request failed with exception:\n%(exception)s\nPlease retry later or create entry manually.') % {
270 'exception': StrConv(msg),
272 _('Entry not created!'),
273 wx.OK | wx.ICON_ERROR).ShowModal()
274 return
276 # Verify acquired data
277 data = response.read()
278 conn.close()
279 ok_test = OK_MATCHER.match(data)
280 if ok_test is not None:
281 url = 'http://%swammu.eu%s' % (Wammu.Utils.GetWebsiteLang(), ok_test.groups()[1])
282 if wx.MessageDialog(self,
283 _('Entry in Gammu Phone Database has been created, you can see it on <%s> URL.\nDo you want to open it in browser now?') % url,
284 _('Entry created!'),
285 wx.YES_NO | wx.ICON_INFORMATION).ShowModal() == wx.ID_YES:
286 Wammu.Webbrowser.Open(url)
287 self.wammu_cfg.Write('/Wammu/TalkbackDone', 'yes')
289 self.EndModal(wx.ID_OK)
291 fail_test = FAIL_MATCHER.match(data)
292 if fail_test is not None:
293 wrong_fields = fail_test.groups()[0].split(',')
294 fields_msg = ''
295 for field in wrong_fields:
296 if field == 'manufacturer':
297 fields_msg += _('Manufacturer') + '\n'
298 elif field == 'name':
299 fields_msg += _('Phone model') + '\n'
300 elif field == 'model':
301 fields_msg += _('Model in gammu configuration') + '\n'
302 elif field == 'connection':
303 fields_msg += _('Connection type') + '\n'
304 elif field == 'note':
305 fields_msg += _('Note') + '\n'
306 elif field == 'author_name':
307 fields_msg += _('Your name') + '\n'
308 elif field == 'author_email':
309 fields_msg += _('Your email') + '\n'
310 elif field == 'email_garble':
311 fields_msg += _('Email displaying') + '\n'
312 elif field == 'gammu_version':
313 fields_msg += _('Gammu version') + '\n'
314 else:
315 fields_msg += _('Field: %s') % field + '\n'
317 wx.MessageDialog(self,
318 _('Entry in Gammu Phone Database was not created, following fields are invalid:\n%s') % fields_msg,
319 _('Entry not created!'),
320 wx.OK | wx.ICON_ERROR).ShowModal()
321 return
323 # end of class TalkbackDialog
325 def DoTalkback(parent, config, phoneid = 0):
326 dlg = TalkbackDialog(parent, config, phoneid)
327 dlg.Show()