There is no Canonical copyright here.
[wammu.git] / Wammu / Main.py
blob5e14f5788ed30c5bf7b632ab16a4ec8bab589b7a
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Main Wammu window
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 wx.html
28 import sys
30 import os
31 import datetime
32 import time
33 import copy
34 import tempfile
35 import Wammu.Webbrowser
36 import locale
37 import Wammu
38 import re
40 # We can use dbus for some fancy stuff
41 try:
42 import dbus
43 import dbus.glib
44 HAVE_DBUS = True
45 except ImportError:
46 HAVE_DBUS = False
48 try:
49 import gammu
50 except SystemError, err:
51 Wammu.gammu_error = err
52 except ImportError, err:
53 Wammu.gammu_error = err
55 import Wammu.Events
56 import Wammu.Displayer
57 import Wammu.Browser
58 import Wammu.Editor
59 import Wammu.Error
60 import Wammu.Info
61 import Wammu.Utils
62 import Wammu.Logger
63 import Wammu.Message
64 import Wammu.Memory
65 import Wammu.Todo
66 import Wammu.Calendar
67 import Wammu.Settings
68 from Wammu.Paths import *
69 import wx.lib.wxpTag
70 import wx.lib.dialogs
71 import Wammu.Data
72 import Wammu.Composer
73 import Wammu.MessageDisplay
74 import Wammu.PhoneSearch
75 import Wammu.About
76 import Wammu.ErrorMessage
77 import Wammu.TalkbackDialog
78 import Wammu.WammuSettings
79 import Wammu.SMSExport
80 import Wammu.SMSXML
81 from Wammu.Locales import StrConv, ConsoleStrConv
83 TALKBACK_COUNT = 30
84 TALKBACK_DAYS = 30
86 def SortDataKeys(a, b):
87 if a == 'info':
88 return -1
89 elif b == 'info':
90 return 1
91 else:
92 return cmp(a,b)
94 def SortDataSubKeys(a, b):
95 if a == ' ':
96 return -1
97 elif b == ' ':
98 return 1
99 else:
100 return cmp(a,b)
102 displaydata = {}
103 displaydata['info'] = {}
104 displaydata['call'] = {}
105 displaydata['contact'] = {}
106 displaydata['message'] = {}
107 displaydata['todo'] = {}
108 displaydata['calendar'] = {}
110 #information
111 displaydata['info'][' '] = ('', _('Phone'), _('Phone Information'), 'phone', [
112 {'Name':_('Wammu version'), 'Value':Wammu.__version__, 'Synced': True},
114 if Wammu.gammu_error == None:
115 displaydata['info'][' '][4].append({'Name':_('Gammu version'), 'Value':gammu.Version()[0], 'Synced': True})
116 displaydata['info'][' '][4].append({'Name':_('python-gammu version'), 'Value':gammu.Version()[1], 'Synced': True})
118 # calls
119 displaydata['call'][' '] = ('info', _('Calls'), _('All Calls'), 'call', [])
120 displaydata['call']['RC'] = ('call', _('Received'), _('Received Calls'), 'call-received', [])
121 displaydata['call']['MC'] = ('call', _('Missed'), _('Missed Calls'), 'call-missed', [])
122 displaydata['call']['DC'] = ('call', _('Outgoing'), _('Outgoing Calls'), 'call-outgoing', [])
124 # contacts
125 displaydata['contact'][' '] = ('info', _('Contacts'), _('All Contacts'), 'contact', [])
126 displaydata['contact']['SM'] = ('contact', _('SIM'), _('SIM Contacts'), 'contact-sim', [])
127 displaydata['contact']['ME'] = ('contact', _('Phone'), _('Phone Contacts'), 'contact-phone', [])
129 # contacts
130 displaydata['message'][' '] = ('info', _('Messages'), _('All Messages'), 'message', [])
131 displaydata['message']['Read'] = ('message', _('Read'), _('Read Messages'), 'message-read', [])
132 displaydata['message']['UnRead'] = ('message', _('Unread'), _('Unread Messages'), 'message-unread', [])
133 displaydata['message']['Sent'] = ('message', _('Sent'), _('Sent Messages'), 'message-sent', [])
134 displaydata['message']['UnSent'] = ('message', _('Unsent'), _('Unsent Messages'), 'message-unsent', [])
136 #todos
137 displaydata['todo'][' '] = ('info', _('Todos'), _('All Todo Items'), 'todo', [])
139 #calendar
140 displaydata['calendar'][' '] = ('info', _('Calendar'), _('All Calendar Events'), 'calendar', [])
143 ## Create a new frame class, derived from the wxPython Frame.
144 class WammuFrame(wx.Frame):
146 def __init__(self, parent, id):
147 self.cfg = Wammu.WammuSettings.WammuConfig()
148 Wammu.configuration = self.cfg
149 if self.cfg.HasEntry('/Main/X') and self.cfg.HasEntry('/Main/Y'):
150 pos = wx.Point(self.cfg.ReadInt('/Main/X'), self.cfg.ReadInt('/Main/Y'))
151 else:
152 pos =wx.DefaultPosition
153 size = wx.Size(self.cfg.ReadInt('/Main/Width'), self.cfg.ReadInt('/Main/Height'))
155 wx.Frame.__init__(self, parent, id, 'Wammu', pos, size, wx.DEFAULT_FRAME_STYLE)
157 if sys.platform == 'win32':
158 img = wx.Image(AppIconPath('wammu'), wx.BITMAP_TYPE_ICO)
159 else:
160 img = wx.Image(AppIconPath('wammu'), wx.BITMAP_TYPE_PNG)
162 self.icon = wx.EmptyIcon()
163 self.icon.CopyFromBitmap(wx.BitmapFromImage(img))
165 if self.icon.GetWidth() == 16 and self.icon.GetHeight() == 16:
166 self.icon16 = self.icon
167 else:
168 img.Rescale(16, 16)
169 self.icon16 = wx.EmptyIcon()
170 self.icon16.CopyFromBitmap(wx.BitmapFromImage(img))
172 self.SetIcon(self.icon)
174 self.CreateStatusBar(2)
175 self.SetStatusWidths([-1,400])
177 # Associate some events with methods of this class
178 wx.EVT_CLOSE(self, self.CloseWindow)
179 Wammu.Events.EVT_PROGRESS(self, self.OnProgress)
180 Wammu.Events.EVT_SHOW_MESSAGE(self, self.OnShowMessage)
181 Wammu.Events.EVT_LINK(self, self.OnLink)
182 Wammu.Events.EVT_DATA(self, self.OnData)
183 Wammu.Events.EVT_SHOW(self, self.OnShow)
184 Wammu.Events.EVT_EDIT(self, self.OnEdit)
185 Wammu.Events.EVT_SEND(self, self.OnSend)
186 Wammu.Events.EVT_CALL(self, self.OnCall)
187 Wammu.Events.EVT_MESSAGE(self, self.OnMessage)
188 Wammu.Events.EVT_DUPLICATE(self, self.OnDuplicate)
189 Wammu.Events.EVT_REPLY(self, self.OnReply)
190 Wammu.Events.EVT_DELETE(self, self.OnDelete)
191 Wammu.Events.EVT_BACKUP(self, self.OnBackup)
192 Wammu.Events.EVT_EXCEPTION(self, self.OnException)
194 self.splitter = wx.SplitterWindow(self, -1)
195 il = wx.ImageList(16, 16)
197 self.tree = wx.TreeCtrl(self.splitter)
198 self.tree.AssignImageList(il)
200 self.treei = {}
201 self.values = {}
203 keys = displaydata.keys()
204 keys.sort(SortDataKeys)
205 for type in keys:
206 self.treei[type] = {}
207 self.values[type] = {}
208 subkeys = displaydata[type].keys()
209 subkeys.sort(SortDataSubKeys)
210 for subtype in subkeys:
211 self.values[type][subtype] = displaydata[type][subtype][4]
212 if displaydata[type][subtype][0] == '':
213 self.treei[type][subtype] = self.tree.AddRoot(
214 displaydata[type][subtype][1],
215 il.Add(wx.Bitmap(IconPath(displaydata[type][subtype][3]))))
216 else:
217 self.treei[type][subtype] = self.tree.AppendItem(
218 self.treei[displaydata[type][subtype][0]][' '],
219 displaydata[type][subtype][1],
220 il.Add(wx.Bitmap(IconPath(displaydata[type][subtype][3]))))
222 for type in keys:
223 self.tree.Expand(self.treei[type][' '])
225 wx.EVT_TREE_SEL_CHANGED(self, self.tree.GetId(), self.OnTreeSel)
227 # common border sizes (Gnome HIG)
228 self.separatorHalf = 3
229 self.separatorNormal = 6
230 self.separatorTwice = 12
232 # right frame
233 self.rightsplitter = wx.SplitterWindow(self.splitter, -1)
234 self.rightwin = wx.Panel(self.rightsplitter, -1)
235 self.rightwin.sizer = wx.BoxSizer(wx.VERTICAL)
237 # title text
238 self.righttitle = wx.StaticText(self.rightwin, -1, 'Wammu')
239 self.rightwin.sizer.Add(self.righttitle, 0, wx.LEFT|wx.ALL|wx.EXPAND, self.separatorNormal)
241 # line
242 self.rightwin.sizer.Add(wx.StaticLine(self.rightwin, -1), 0 , wx.EXPAND)
244 # search input
245 self.searchpanel = wx.Panel(self.rightwin, -1)
246 self.searchpanel.sizer = wx.BoxSizer(wx.HORIZONTAL)
247 self.searchpanel.sizer.Add(wx.StaticText(self.searchpanel, -1, _('Search: ')), 0, wx.LEFT | wx.CENTER)
248 self.searchinput = wx.TextCtrl(self.searchpanel, -1)
249 self.searchinput.SetToolTipString(_('Enter text to search for, please note that search type is selected next to this field. Matching is done over all fields.'))
250 self.searchpanel.sizer.Add(self.searchinput, 1, wx.CENTER | wx.ALIGN_CENTER_VERTICAL)
251 self.searchchoice = wx.Choice(self.searchpanel, choices = [_('Text'), _('Regexp'), _('Wildcard')])
252 self.searchchoice.SetToolTipString(_('Select search type'))
253 self.searchchoice.SetSelection(self.cfg.ReadInt('/Defaults/SearchType'))
254 self.searchpanel.sizer.Add(self.searchchoice, 0, wx.LEFT | wx.CENTER | wx.EXPAND, self.separatorNormal)
255 self.searchclear = wx.Button(self.searchpanel, wx.ID_CLEAR)
256 self.searchpanel.sizer.Add(self.searchclear, 0, wx.LEFT | wx.CENTER | wx.EXPAND, self.separatorNormal)
257 self.searchpanel.SetSizer(self.searchpanel.sizer)
258 self.rightwin.sizer.Add(self.searchpanel, 0, wx.LEFT | wx.ALL | wx.EXPAND, self.separatorNormal)
260 self.Bind(wx.EVT_CHOICE, self.OnSearch, self.searchchoice)
261 self.Bind(wx.EVT_TEXT, self.OnSearch, self.searchinput)
262 self.Bind(wx.EVT_BUTTON, self.ClearSearch, self.searchclear)
264 # item browser
265 self.browser = Wammu.Browser.Browser(self.rightwin, self, self.cfg)
266 self.rightwin.sizer.Add(self.browser, 1, wx.EXPAND)
267 self.rightwin.SetSizer(self.rightwin.sizer)
269 # values displayer
270 self.content = Wammu.Displayer.Displayer(self.rightsplitter, self)
272 self.splitter.SplitVertically(self.tree, self.rightsplitter, self.cfg.ReadInt('/Main/Split'))
273 self.rightsplitter.SplitHorizontally(self.rightwin, self.content, self.cfg.ReadInt('/Main/SplitRight'))
275 # initial content
276 self.content.SetContent('<font size=+1><b>%s</b></font>' % (_('Welcome to Wammu %s') % Wammu.__version__))
278 # Prepare the menu bar
279 self.menuBar = wx.MenuBar()
281 menu1 = wx.Menu()
282 menu1.Append(100, _('&Write data'), _('Write data (except messages) to file.'))
283 menu1.Append(101, _('W&rite message'), _('Write messages to file.'))
284 menu1.Append(102, _('&Read data'), _('Read data (except messages) from file (does not import to the phone).'))
285 menu1.Append(103, _('R&ead messages'), _('Read messages from file (does not import to the phone).'))
286 menu1.AppendSeparator()
287 menu1.Append(150, _('&Phone wizard'), _('Search for phone or configure it using guided wizard.'))
288 menu1.Append(151, _('Se&ttings'), _('Change Wammu settings.'))
289 menu1.AppendSeparator()
290 menu1.Append(199, '%s\tCtrl+Q' % _('E&xit'), _('Terminate Wammu.'))
291 # Add menu to the menu bar
292 self.menuBar.Append(menu1, _('&Wammu'))
294 menu2 = wx.Menu()
295 menu2.Append(201, _('&Connect'), _('Connect the device.'))
296 menu2.Append(202, _('&Disconnect'), _('Disconnect the device.'))
297 menu2.AppendSeparator()
298 menu2.Append(210, _('&Synchronise time'), _('Synchronise time in phone with PC.'))
299 menu2.AppendSeparator()
300 menu2.Append(250, _('Send &file'), _('Send file to phone.'))
301 # Add menu to the menu bar
302 self.menuBar.Append(menu2, _('&Phone'))
304 menu3 = wx.Menu()
305 menu3.Append(301, '%s\tCtrl+I' % _('&Info'), _('Retrieve phone information.'))
306 menu3.AppendSeparator()
307 menu3.Append(310, _('Contacts (&SIM)'), _('Retrieve contacts from SIM.'))
308 menu3.Append(311, _('Contacts (&phone)'), _('Retrieve contacts from phone memory.'))
309 menu3.Append(312, _('&Contacts (All)'), _('Retrieve contacts from phone and SIM memory.'))
310 menu3.AppendSeparator()
311 menu3.Append(320, _('C&alls'), _('Retrieve call history.'))
312 menu3.AppendSeparator()
313 menu3.Append(330, _('&Messages'), _('Retrieve messages.'))
314 menu3.AppendSeparator()
315 menu3.Append(340, _('&Todos'), _('Retrieve todos.'))
316 menu3.AppendSeparator()
317 menu3.Append(350, _('Calenda&r'), _('Retrieve calendar events.'))
318 # Add menu to the menu bar
319 self.menuBar.Append(menu3, _('&Retrieve'))
321 menu4 = wx.Menu()
322 menu4.Append(401, '%s\tCtrl+N' % _('&Contact'), _('Create new contact.'))
323 menu4.Append(402, '%s\tCtrl+E' % _('Calendar &event'), _('Create new calendar event.'))
324 menu4.Append(403, '%s\tCtrl+T' % _('&Todo'), _('Create new todo.'))
325 menu4.Append(404, '%s\tCtrl+M' % _('&Message'), _('Create new message.'))
326 # Add menu to the menu bar
327 self.menuBar.Append(menu4, _('&Create'))
329 menu5 = wx.Menu()
330 menu5.Append(501, _('&Save'), _('Save currently retrieved data (except messages) to backup.'))
331 menu5.Append(502, _('S&ave messages'), _('Save currently retrieved messages to backup.'))
332 menu5.Append(503, _('&Import'), _('Import data from backup to phone.'))
333 menu5.Append(504, _('I&mport messages'), _('Import messages from backup to phone.'))
334 menu5.AppendSeparator()
335 menu5.Append(510, _('Export messages to &emails'), _('Export messages to emails in storage you choose.'))
336 menu5.Append(511, _('Export messages to &XML'), _('Export messages to XML file you choose.'))
337 # Add menu to the menu bar
338 self.menuBar.Append(menu5, _('&Backups'))
340 menuhelp = wx.Menu()
341 menuhelp.Append(1001, _('&Website'), _('Visit Wammu website.'))
342 menuhelp.Append(1002, _('&Support'), _('Visit Wammu support website.'))
343 menuhelp.Append(1003, _('&Report bug'), _('Report bug in Wammu, please include saved debug log if possible.'))
344 menuhelp.Append(1004, _('&Save debug log'), _('Save a copy of debug log, please include this in bug report.'))
345 menuhelp.AppendSeparator()
346 menuhelp.Append(1010, _('&Gammu Phone Database'), _('Visit database of user experiences with phones.'))
347 menuhelp.Append(1011, _('&Talkback'), _('Report your experiences into Gammu Phone Database.'))
348 menuhelp.AppendSeparator()
349 menuhelp.Append(1020, _('&Donate'), _('Donate to Wammu project.'))
350 menuhelp.AppendSeparator()
351 menuhelp.Append(1100, _('&About'), _('Information about program.'))
352 # Add menu to the menu bar
353 self.menuBar.Append(menuhelp, _('&Help'))
355 # Set menu bar
356 self.SetMenuBar(self.menuBar)
358 # menu events
359 wx.EVT_MENU(self, 100, self.WriteData)
360 wx.EVT_MENU(self, 101, self.WriteSMSData)
361 wx.EVT_MENU(self, 102, self.ReadData)
362 wx.EVT_MENU(self, 103, self.ReadSMSData)
363 wx.EVT_MENU(self, 150, self.SearchPhone)
364 wx.EVT_MENU(self, 151, self.Settings)
365 wx.EVT_MENU(self, 199, self.CloseWindow)
367 wx.EVT_MENU(self, 201, self.PhoneConnect)
368 wx.EVT_MENU(self, 202, self.PhoneDisconnect)
369 wx.EVT_MENU(self, 210, self.SyncTime)
370 wx.EVT_MENU(self, 250, self.SendFile)
372 wx.EVT_MENU(self, 301, self.ShowInfo)
373 wx.EVT_MENU(self, 310, self.ShowContactsSM)
374 wx.EVT_MENU(self, 311, self.ShowContactsME)
375 wx.EVT_MENU(self, 312, self.ShowContacts)
376 wx.EVT_MENU(self, 320, self.ShowCalls)
377 wx.EVT_MENU(self, 330, self.ShowMessages)
378 wx.EVT_MENU(self, 340, self.ShowTodos)
379 wx.EVT_MENU(self, 350, self.ShowCalendar)
381 wx.EVT_MENU(self, 401, self.NewContact)
382 wx.EVT_MENU(self, 402, self.NewCalendar)
383 wx.EVT_MENU(self, 403, self.NewTodo)
384 wx.EVT_MENU(self, 404, self.NewMessage)
386 wx.EVT_MENU(self, 501, self.Backup)
387 wx.EVT_MENU(self, 502, self.BackupSMS)
388 wx.EVT_MENU(self, 503, self.Import)
389 wx.EVT_MENU(self, 504, self.ImportSMS)
390 wx.EVT_MENU(self, 510, self.SMSToMails)
391 wx.EVT_MENU(self, 511, self.SMSToXML)
394 wx.EVT_MENU(self, 1001, self.Website)
395 wx.EVT_MENU(self, 1002, self.Support)
396 wx.EVT_MENU(self, 1003, self.ReportBug)
397 wx.EVT_MENU(self, 1004, self.SaveLog)
398 wx.EVT_MENU(self, 1010, self.PhoneDB)
399 wx.EVT_MENU(self, 1011, self.Talkback)
400 wx.EVT_MENU(self, 1020, self.Donate)
401 wx.EVT_MENU(self, 1100, self.About)
403 self.timer = None
404 self.TogglePhoneMenus(False)
406 self.type = ['info',' ']
408 self.TimerId = wx.NewId()
410 if Wammu.gammu_error == None:
411 # create state machine
412 self.sm = gammu.StateMachine()
414 # create temporary file for logs
415 fd, self.logfilename = tempfile.mkstemp('.log', 'wammu')
417 # set filename to be used for error reports
418 Wammu.ErrorLog.DEBUG_LOG_FILENAME = self.logfilename
420 if sys.platform != 'win32':
421 print ConsoleStrConv(
422 _('Debug log created in temporary file <%s>. In case of crash please include it in bugreport!') % self.logfilename
425 self.logfilefd = os.fdopen(fd, 'w+')
426 # use temporary file for logs
427 gammu.SetDebugFile(self.logfilefd)
428 gammu.SetDebugLevel('textalldate')
430 if Wammu.debug:
431 self.loggerdebug = Wammu.Logger.LoggerDebug(self.logfilename)
432 self.loggerdebug.start()
434 # initialize variables
435 self.showdebug = ''
436 self.IMEI = ''
437 self.Manufacturer = ''
438 self.Model = ''
439 self.Version = ''
440 self.tbicon = None
442 def HandleGammuError(self):
444 Show error about gammu import failure. Try to help user with various
445 situation which could happened, so that he can solve this problem.
447 error = str(Wammu.gammu_error)
448 if error.find('Runtime libGammu version does not match compile time version') != -1:
449 result = re.match('Runtime libGammu version does not match compile time version \(runtime: (\S+), compiletime: (\S+)\)', error)
451 wx.MessageDialog(self,
452 _('Wammu could not import gammu module, program will be terminated.') + '\n\n' +
453 _('The import failed because python-gammu is compiled with different version of Gammu than it is now using (it was compiled with version %(compile)s and now it is using version %(runtime)s).') % {'compile': result.group(2), 'runtime': result.group(1)} + '\n\n' +
454 _('You can fix it by recompiling python-gammu against gammu library you are currently using.'),
455 _('Gammu module not working!'),
456 wx.OK | wx.ICON_ERROR).ShowModal()
457 elif error.find('No module named gammu') != -1:
458 wx.MessageDialog(self,
459 _('Wammu could not import gammu module, program will be terminated.') + '\n\n' +
460 _('Gammu module was not found, you probably don\'t have properly installed python-gammu for current python version.'),
461 _('Gammu module not working!'),
462 wx.OK | wx.ICON_ERROR).ShowModal()
463 else:
464 wx.MessageDialog(self,
465 _('Wammu could not import gammu module, program will be terminated.') + '\n\n' +
466 _('The import failed with following error:') + '\n\n%s' % error,
467 _('Gammu module not working!'),
468 wx.OK | wx.ICON_ERROR).ShowModal()
469 sys.exit()
471 def InitConfiguration(self):
473 Binds Wammu configuration to Gammu one. If at least one section
474 exists, use first one (same as Gammu), otherwise we suggest search to
475 user.
477 gammucfg = self.cfg.gammu.GetConfigs()
478 if len(gammucfg) == 0:
479 dlg = wx.MessageDialog(self,
480 _('Wammu configuration was not found and Gammu settings couldn\'t be read.') + '\n\n' +
481 _('Do you want to configure phone connection now?') + '\n',
482 _('Configuration not found'),
483 wx.YES_NO | wx.YES_DEFAULT | wx.ICON_WARNING)
484 if dlg.ShowModal() == wx.ID_YES:
485 self.SearchPhone()
486 elif not self.cfg.HasEntry('/Gammu/Section'):
487 # behave as Gammu
488 self.cfg.WriteInt('/Gammu/Section', 0)
490 def TalkbackCheck(self):
492 Do ask for talkback after month of usage and at least 30 executions.
494 firstrun = self.cfg.ReadFloat('/Wammu/FirstRun')
495 if firstrun == -1:
496 firstrun = time.time()
497 self.cfg.WriteFloat('/Wammu/FirstRun', firstrun)
498 runs = self.cfg.ReadInt('/Wammu/RunCounter')
499 self.cfg.WriteInt('/Wammu/RunCounter', runs + 1)
500 if self.cfg.Read('/Wammu/TalkbackDone') == 'no':
501 if (firstrun + (3600 * 24 * TALKBACK_DAYS) < time.time()
502 and runs > TALKBACK_COUNT):
503 dlg = wx.MessageDialog(self,
504 _('You are using Wammu for more than a month. We would like to hear from you how your phone is supported. Do you want to participate in this survey?') +
505 '\n\n' + _('Press Cancel to never show this question again.'),
506 _('Thanks for using Wammu'),
507 wx.YES_NO | wx.CANCEL | wx.ICON_INFORMATION)
508 ret = dlg.ShowModal()
509 if ret == wx.ID_YES:
510 self.Talkback()
511 elif ret == wx.ID_CANCEL:
512 self.cfg.Write('/Wammu/TalkbackDone', 'skipped')
514 def MigrateConfiguration(self):
516 Migrate configuration from pre-0.18 style one (Gammu was configured
517 inside Wammu configuration) to using .gammurc.
519 connection = self.cfg.Read('/Gammu/Connection')
520 device = self.cfg.Read('/Gammu/Device')
521 model = self.cfg.Read('/Gammu/Model')
522 gammucfg = self.cfg.gammu.GetConfigs()
523 if len(gammucfg) > 0:
524 for i in gammucfg:
525 cfg = self.cfg.gammu.GetConfig(i['Id'])
526 if cfg['Model'] == model and cfg['Connection'] == connection and cfg['Device'] == device:
527 self.cfg.WriteInt('/Gammu/Section', i['Id'])
528 break
529 if not self.cfg.HasEntry('/Gammu/Section'):
530 index = self.cfg.gammu.FirstFree()
531 self.cfg.gammu.SetConfig(index, device, connection, _('Migrated from older Wammu'), model)
532 self.cfg.WriteInt('/Gammu/Section', index)
534 def PostInit(self, appparent):
536 Do things which need window opened to behave correctly.
538 - Activate initial view.
539 - Show if something wrong has happened on gammu import.
540 - Initialize or migrate Gammu configuration.
541 - Connect to phone if required.
542 - Ask for talkback.
543 - Setup internal information.
545 self.ActivateView('info', ' ')
546 self.appparent = appparent
548 if Wammu.gammu_error != None:
549 self.HandleGammuError()
551 if not self.cfg.HasEntry('/Gammu/Section') and self.cfg.HasEntry('/Gammu/Connection'):
552 self.MigrateConfiguration()
554 self.InitConfiguration()
556 self.DoDebug(self.cfg.Read('/Debug/Show'))
558 if (self.cfg.Read('/Wammu/AutoConnect') == 'yes'):
559 self.PhoneConnect()
561 self.TalkbackCheck()
563 self.SetupNumberPrefix()
565 self.SetupStatusRefresh()
567 self.SetupTrayIcon()
569 self.InitDBUS()
571 def InitDBUS(self):
573 Initializes DBUS handlers if available.
575 self.dbus_notify = None
576 self.last_dbus_id = 0
577 if HAVE_DBUS:
578 try:
579 bus = dbus.SessionBus() #mainloop = self.appparent.MainLoop)
580 interface = 'org.freedesktop.Notifications'
581 path = '/org/freedesktop/Notifications'
582 if Wammu.Utils.DBUSServiceAvailable(bus, interface, True):
583 obj = bus.get_object(interface, path)
584 self.dbus_notify = dbus.Interface(obj, interface)
585 self.dbus_notify.connect_to_signal('ActionInvoked', self.DBUSActionCallback)
586 except dbus.DBusException:
587 self.dbus_notify = None
588 self.last_dbus_id = 0
590 def SetupTrayIcon(self):
591 if self.cfg.Read('/Wammu/TaskBarIcon') != 'yes':
592 if self.tbicon is not None:
593 self.tbicon.Destroy()
594 return
595 if self.tbicon is not None:
596 # Nothing to do
597 return
598 self.tbicon = wx.TaskBarIcon()
599 self.tbicon.SetIcon(self.icon16, 'Wammu')
600 self.tbicon.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.OnTaskBarRightClick)
601 self.tbicon.Bind(wx.EVT_TASKBAR_LEFT_UP, self.OnTaskBarLeftClick)
602 self.tbicon.Bind(wx.EVT_MENU, self.Settings, id=151)
603 self.tbicon.Bind(wx.EVT_MENU, self.PhoneConnect, id=201)
604 self.tbicon.Bind(wx.EVT_MENU, self.PhoneDisconnect, id=202)
605 self.tbicon.Bind(wx.EVT_MENU, self.OnTaskBarRestore, id=100000)
606 self.tbicon.Bind(wx.EVT_MENU, self.OnTaskBarMinimize, id=100001)
607 self.tbicon.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=100002)
609 def OnTaskBarRightClick(self, evt):
610 menutaskbar = wx.Menu()
611 menutaskbar.Append(201, _('Connect'))
612 menutaskbar.Append(202, _('Disconnect'))
613 menutaskbar.AppendSeparator()
614 menutaskbar.Append(151, _('Settings'))
615 menutaskbar.AppendSeparator()
616 menutaskbar.Append(100000, _('Restore'))
617 menutaskbar.Append(100001, _('Minimize'))
618 menutaskbar.AppendSeparator()
619 menutaskbar.Append(100002, _('Close'))
620 self.tbicon.PopupMenu(menutaskbar)
621 menutaskbar.Destroy()
623 def OnTaskBarLeftClick(self, evt):
624 if self.IsShown():
625 self.Show(False)
626 else:
627 self.Show(True)
629 def OnTaskBarRestore(self, evt):
630 self.Show(True)
632 def OnTaskBarMinimize(self, evt):
633 self.Show(False)
635 def OnTaskBarClose(self, evt):
636 self.CloseWindow(evt)
638 def OnTimer(self, evt = None):
639 if self.connected:
640 try:
641 s = self.sm.GetSignalQuality()
642 b = self.sm.GetBatteryCharge()
643 d = self.sm.GetDateTime()
645 # Parse power source
646 power = _('Unknown')
647 if b['ChargeState'] == 'BatteryPowered':
648 power = _('battery')
649 elif b['ChargeState'] == 'BatteryConnected':
650 power = _('AC')
651 elif b['ChargeState'] == 'BatteryNotConnected':
652 power = _('no battery')
653 elif b['ChargeState'] == 'PowerFault':
654 power = _('fault')
655 elif b['ChargeState'] == 'BatteryCharging':
656 power = _('charging')
657 elif b['ChargeState'] == 'BatteryFull':
658 power = _('charged')
660 # Time might be None if it is invalid (eg. 0.0.0000 date)
661 if d is None:
662 time = _('Unknown')
663 else:
664 time = StrConv(d.strftime('%c'))
666 # Detect unknown signal quality
667 if s['SignalPercent'] == -1:
668 signal = _('Unknown')
669 else:
670 # l10n: Formatting of signal percentage, usually you can keep this as it is.
671 signal = _('%d %%') % s['SignalPercent']
673 self.SetStatusText(_('Bat: %(battery_percent)d %% (%(power_source)s), Sig: %(signal_level)s, Time: %(time)s') %
675 'battery_percent':b['BatteryPercent'],
676 'power_source':power,
677 'signal_level':signal,
678 'time': time
679 }, 1)
680 except gammu.GSMError:
681 pass
683 def SetupNumberPrefix(self):
684 self.prefix = self.cfg.Read('/Wammu/PhonePrefix')
685 if self.prefix == 'Auto':
686 if self.connected:
687 self.prefix = None
688 try:
689 on = Wammu.Utils.ParseMemoryEntry(self.sm.GetMemory(Location = 1, Type = 'ON'), self.cfg)['Number']
690 self.prefix = Wammu.Utils.GrabNumberPrefix(on, Wammu.Data.InternationalPrefixes)
691 except gammu.GSMError:
692 pass
693 if self.prefix is None:
694 try:
695 smsc = self.sm.GetSMSC()['Number']
696 self.prefix = Wammu.Utils.GrabNumberPrefix(smsc, Wammu.Data.InternationalPrefixes)
697 except gammu.GSMError:
698 pass
699 if self.prefix is None:
700 self.prefix = self.cfg.Read('/Wammu/LastPhonePrefix')
701 else:
702 self.cfg.Write('/Wammu/LastPhonePrefix', self.prefix)
703 else:
704 self.prefix = self.cfg.Read('/Wammu/LastPhonePrefix')
705 Wammu.Utils.NumberPrefix = self.prefix
707 def SetupStatusRefresh(self):
708 repeat = self.cfg.ReadInt('/Wammu/RefreshState')
709 if repeat == 0:
710 self.timer = None
711 else:
712 self.OnTimer()
713 self.timer = wx.Timer(self, self.TimerId)
714 wx.EVT_TIMER(self, self.TimerId, self.OnTimer)
715 self.timer.Start(repeat)
717 def DoDebug(self, newdebug):
718 if newdebug != self.showdebug:
719 self.showdebug = newdebug
720 if self.showdebug == 'yes':
721 self.logwin = Wammu.Logger.LogFrame(self, self.cfg)
722 self.logwin.Show(True)
723 wx.EVT_CLOSE(self.logwin, self.LogClose)
724 self.logger = Wammu.Logger.Logger(self.logwin, self.logfilename)
725 self.logger.start()
726 else:
727 self.CloseLogWindow()
729 def SaveWinSize(self, win, key):
730 x,y = win.GetPositionTuple()
731 w,h = win.GetSizeTuple()
733 self.cfg.WriteInt('/%s/X' % key, x)
734 self.cfg.WriteInt('/%s/Y' % key, y)
735 self.cfg.WriteInt('/%s/Width' % key, w)
736 self.cfg.WriteInt('/%s/Height' % key, h)
738 def CloseLogWindow(self):
739 if hasattr(self, 'logwin'):
740 self.SaveWinSize(self.logwin, 'Debug')
741 if hasattr(self, 'logger'):
742 self.logger.canceled = True
743 del self.logger
744 if hasattr(self, 'logwin'):
745 self.logwin.Destroy()
746 del self.logwin
749 def LogClose(self, evt = None):
750 self.cfg.Write('/Debug/Show', 'no')
751 self.CloseLogWindow()
753 def TogglePhoneMenus(self, enable):
754 self.connected = enable
755 if enable:
756 self.SetStatusText(_('Connected'), 1)
757 if self.timer != None:
758 self.OnTimer()
759 else:
760 self.SetStatusText(_('Disconnected'), 1)
761 mb = self.menuBar
763 mb.Enable(201, not enable);
764 mb.Enable(202, enable);
766 mb.Enable(210, enable);
768 mb.Enable(250, enable);
770 mb.Enable(301, enable);
772 mb.Enable(310, enable);
773 mb.Enable(311, enable);
774 mb.Enable(312, enable);
776 mb.Enable(320, enable);
778 mb.Enable(330, enable);
780 mb.Enable(340, enable);
782 mb.Enable(350, enable);
784 mb.Enable(401, enable);
785 mb.Enable(402, enable);
786 mb.Enable(403, enable);
787 mb.Enable(404, enable);
789 mb.Enable(501, enable);
790 mb.Enable(502, enable);
791 mb.Enable(503, enable);
792 mb.Enable(504, enable);
794 mb.Enable(510, enable);
795 mb.Enable(511, enable);
797 def ActivateView(self, k1, k2):
798 self.tree.SelectItem(self.treei[k1][k2])
799 self.ChangeView(k1, k2)
801 def ChangeView(self, k1, k2):
802 self.ChangeBrowser(k1, k2)
803 self.righttitle.SetLabel(displaydata[k1][k2][2])
805 def ChangeBrowser(self, k1, k2):
806 self.type = [k1, k2]
807 if k2 == ' ':
808 data = []
809 for k3, v3 in self.values[k1].iteritems():
810 if k3 != '__':
811 data = data + v3
812 self.values[k1]['__'] = data
813 self.browser.Change(k1, data)
814 else:
815 self.browser.Change(k1, self.values[k1][k2])
816 self.browser.ShowRow(0)
818 def OnTreeSel(self, event):
819 item = event.GetItem()
820 for k1, v1 in self.treei.iteritems():
821 for k2, v2 in v1.iteritems():
822 if v2 == item:
823 self.ChangeView(k1, k2)
824 self.ClearSearch()
826 def OnSearch(self, event):
827 text = self.searchinput.GetValue()
828 type = self.searchchoice.GetSelection()
829 try:
830 self.browser.Filter(text, type)
831 self.searchinput.SetBackgroundColour(wx.NullColour)
832 except Wammu.Browser.FilterException:
833 self.searchinput.SetBackgroundColour(wx.RED)
835 def ClearSearch(self, event = None):
836 self.searchinput.SetValue('')
838 def Settings(self, event = None):
839 if self.connected:
840 connection_settings = {
841 'Connection': self.cfg.Read('/Gammu/Connection'),
842 'LockDevice': self.cfg.Read('/Gammu/LockDevice'),
843 'Device': self.cfg.Read('/Gammu/Device'),
844 'Model': self.cfg.Read('/Gammu/Model')
847 result = Wammu.Settings.Settings(self, self.cfg).ShowModal()
848 if result == wx.ID_OK:
849 if self.connected:
850 connection_settings_new = {
851 'Connection': self.cfg.Read('/Gammu/Connection'),
852 'LockDevice': self.cfg.Read('/Gammu/LockDevice'),
853 'Device': self.cfg.Read('/Gammu/Device'),
854 'Model': self.cfg.Read('/Gammu/Model')
857 if connection_settings != connection_settings_new:
858 wx.MessageDialog(self,
859 _('You changed parameters affecting phone connection, they will be used next time you connect to phone.'),
860 _('Notice'),
861 wx.OK | wx.ICON_INFORMATION).ShowModal()
862 self.DoDebug(self.cfg.Read('/Debug/Show'))
863 self.SetupNumberPrefix()
864 self.SetupStatusRefresh()
865 self.SetupTrayIcon()
867 def CloseWindow(self, event):
868 self.SaveWinSize(self, 'Main')
869 if hasattr(self, 'logwin'):
870 self.CloseLogWindow()
871 self.cfg.WriteInt('/Main/Split', self.splitter.GetSashPosition())
872 self.cfg.WriteInt('/Main/SplitRight', self.rightsplitter.GetSashPosition())
873 self.cfg.WriteInt('/Defaults/SearchType', self.searchchoice.GetCurrentSelection())
875 gammu.SetDebugFile(None)
876 gammu.SetDebugLevel('nothing')
878 self.logfilefd.close()
880 if hasattr(self, 'logger'):
881 self.logger.canceled = True
882 self.logger.join()
884 if hasattr(self, 'loggerdebug'):
885 self.loggerdebug.canceled = True
886 self.loggerdebug.join()
888 if self.tbicon is not None:
889 self.tbicon.Destroy()
891 if sys.platform != 'win32':
892 print ConsoleStrConv(
893 _('Looks like normal program termination, deleting log file.')
895 try:
896 os.unlink(self.logfilename)
897 except:
898 print ConsoleStrConv(
899 _('Failed to unlink temporary log file, please delete it yourself.')
901 print ConsoleStrConv(
902 _('Filename: %s') % self.logfilename
905 # Forcibily save configuration
906 self.cfg.Flush()
908 # tell the window to kill itself
909 self.Destroy()
911 def ShowError(self, info):
912 evt = Wammu.Events.ShowMessageEvent(
913 message = Wammu.Utils.FormatError(_('Error while communicating with phone'), info),
914 title = _('Error Occured'),
915 errortype = 'gammu',
916 type = wx.ICON_ERROR)
917 wx.PostEvent(self, evt)
919 def ShowProgress(self, text):
920 self.progress = wx.ProgressDialog(
921 _('Operation in progress'),
922 text,
923 100,
924 self,
925 wx.PD_CAN_ABORT | wx.PD_APP_MODAL | wx.PD_AUTO_HIDE | wx.PD_ELAPSED_TIME | wx.PD_REMAINING_TIME | wx.PD_ESTIMATED_TIME)
927 def OnProgress(self, evt):
928 if hasattr(self, 'progress'):
929 if not self.progress.Update(evt.progress):
930 try:
931 evt.cancel()
932 except:
933 pass
934 if (evt.progress == 100):
935 del self.progress
936 if hasattr(evt, 'lock'):
937 evt.lock.release()
939 def OnException(self, evt):
940 Wammu.Error.Handler(*evt.data)
942 def OnData(self, evt):
943 self.values[evt.type[0]][evt.type[1]] = evt.data
944 if evt.last:
945 if hasattr(self, 'progress'):
946 self.progress.Update(100)
947 del self.progress
949 if hasattr(self, 'nextfun'):
950 f = self.nextfun
951 a = self.nextarg
952 del self.nextfun
953 del self.nextarg
954 f (*a)
956 def ShowData(self, data):
957 text = u''
958 if data is not None:
959 for d in data:
960 if len(d) == 2:
961 text += u'<b>%s</b>: %s<br>' % (d[0], d[1])
962 else:
963 text += u'<p>%s</p>' % d[0]
964 self.content.SetContent(text)
966 def OnShow(self, evt):
967 data = v = evt.data
968 if data is None:
969 pass
970 elif self.type == ['info',' ']:
971 data = [(evt.data['Name'], evt.data['Value'])]
972 elif self.type[0] == 'contact' or self.type[0] == 'call':
973 data = [
974 (_('Location'), str(v['Location'])),
975 (_('Memory type'), v['MemoryType'])]
976 for i in v['Entries']:
977 s = Wammu.Utils.GetTypeString(i['Type'], i['Value'], self.values, linkphone = False)
978 try:
979 if i['VoiceTag']:
980 s += ', ' + (_('voice tag %x') % i['VoiceTag'])
981 except:
982 pass
983 data.append((i['Type'], s))
984 elif self.type[0] == 'message':
985 data = [
986 (_('Number'), Wammu.Utils.GetNumberLink([] + self.values['contact']['ME'] + self.values['contact']['SM'], v['Number'])),
987 (_('Date'), StrConv(v['DateTime'])),
988 (_('Location'), StrConv(v['Location'])),
989 (_('Folder'), StrConv(v['SMS'][0]['Folder'])),
990 (_('Memory'), StrConv(v['SMS'][0]['Memory'])),
991 (_('SMSC'), Wammu.Utils.GetNumberLink([] + self.values['contact']['ME'] + self.values['contact']['SM'], v['SMS'][0]['SMSC']['Number'])),
992 (_('State'), StrConv(v['State']))]
993 if v['Name'] != '':
994 data.append((_('Name'), StrConv(v['Name'])))
995 data.append((Wammu.MessageDisplay.SmsToHtml(self.cfg, v),))
996 elif self.type[0] == 'todo':
997 data = [
998 (_('Location'), str(v['Location'])),
999 (_('Priority'), v['Priority']),
1000 (_('Type'), v['Type']),
1002 for i in v['Entries']:
1003 data.append((i['Type'], Wammu.Utils.GetTypeString(i['Type'], i['Value'], self.values)))
1004 elif self.type[0] == 'calendar':
1005 data = [
1006 (_('Location'), str(v['Location'])),
1007 (_('Type'), v['Type']),
1009 for i in v['Entries']:
1010 data.append((i['Type'], Wammu.Utils.GetTypeString(i['Type'], i['Value'], self.values)))
1011 else:
1012 data = [('Show not yet implemented! (type = %s)' % self.type[0])]
1013 self.ShowData(data)
1015 def NewContact(self, evt):
1016 self.EditContact({})
1018 def NewCalendar(self, evt):
1019 self.EditCalendar({})
1021 def NewTodo(self, evt):
1022 self.EditTodo({})
1024 def NewMessage(self, evt):
1025 self.ComposeMessage({})
1027 def ComposeMessage(self, v, action = 'save'):
1028 if Wammu.Composer.SMSComposer(self, self.cfg, v, self.values, action).ShowModal() == wx.ID_OK:
1030 if len(v['Numbers']) == 0:
1031 v['Numbers'] = ['Wammu']
1033 for number in v['Numbers']:
1034 busy = wx.BusyInfo(_('Writing message(s)...'))
1035 time.sleep(0.1)
1036 wx.Yield()
1037 v['Number'] = number
1038 v['SMS'] = gammu.EncodeSMS(v['SMSInfo'])
1040 if v['Save']:
1041 result = {}
1042 result['SMS'] = []
1044 try:
1045 for msg in v['SMS']:
1046 msg['SMSC']['Location'] = 1
1048 msg['Folder'] = v['Folder']
1049 msg['Number'] = v['Number']
1050 msg['Type'] = v['Type']
1051 msg['State'] = v['State']
1053 if v['Save']:
1054 (msg['Location'], msg['Folder']) = self.sm.AddSMS(msg)
1055 if v['Send']:
1056 # When sending of saved message fails, send it directly:
1057 try:
1058 msg['MessageReference'] = self.sm.SendSavedSMS(0, msg['Location'])
1059 except gammu.GSMError:
1060 msg['MessageReference'] = self.sm.SendSMS(msg)
1061 try:
1062 result['SMS'].append(self.sm.GetSMS(0, msg['Location'])[0])
1063 except gammu.ERR_EMPTY:
1064 wx.MessageDialog(self, _('It was not possible to read saved message! There is most likely some bug in Gammu, please contact author with debug log of this operation. To see message in Wammu you need to reread all messsages.'), _('Could not read saved message!'), wx.OK | wx.ICON_ERROR).ShowModal()
1065 elif v['Send']:
1066 msg['MessageReference'] = self.sm.SendSMS(msg)
1068 if v['Save']:
1069 info = gammu.DecodeSMS(result['SMS'])
1070 if info != None:
1071 result['SMSInfo'] = info
1072 Wammu.Utils.ParseMessage(result, (info != None))
1073 result['Synced'] = True
1074 self.values['message'][result['State']].append(result)
1076 except gammu.GSMError, val:
1077 del busy
1078 self.ShowError(val[0])
1080 if v['Save']:
1081 try:
1082 self.ActivateView('message', result['State'])
1083 self.browser.ShowLocation(result['Location'])
1084 except KeyError:
1085 pass
1087 def EditContact(self, v):
1088 backup = copy.deepcopy(v)
1089 shoulddelete = (v == {} or v['Location'] == 0)
1090 if Wammu.Editor.ContactEditor(self, self.cfg, self.values, v).ShowModal() == wx.ID_OK:
1091 try:
1092 busy = wx.BusyInfo(_('Writing contact...'))
1093 time.sleep(0.1)
1094 wx.Yield()
1095 # was entry moved => delete it from internal list
1096 if not shoulddelete:
1097 for idx in range(len(self.values['contact'][backup['MemoryType']])):
1098 if self.values['contact'][backup['MemoryType']][idx] == v:
1099 del self.values['contact'][backup['MemoryType']][idx]
1100 break
1102 # have we specified location? => add or set
1103 if v['Location'] == 0:
1104 v['Location'] = self.sm.AddMemory(v)
1105 else:
1106 try:
1107 v['Location'] = self.sm.SetMemory(v)
1108 except (gammu.ERR_NOTSUPPORTED, gammu.ERR_NOTIMPLEMENTED):
1109 v['Location'] = self.sm.AddMemory(v)
1111 # was entry moved => delete it from phone
1112 if not shoulddelete:
1113 if v['MemoryType'] != backup['MemoryType'] or v['Location'] != backup['Location']:
1114 # delete from phone
1115 self.sm.DeleteMemory(backup['MemoryType'], backup['Location'])
1117 # reread entry (it doesn't have to contain exactly same data as entered, it depends on phone features)
1118 try:
1119 attempts = 0
1120 while attempts < 10:
1121 try:
1122 v = self.sm.GetMemory(v['MemoryType'], v['Location'])
1123 break
1124 except gammu.ERR_EMPTY:
1125 # some phones need time till entry appears
1126 attempts = attempts + 1
1127 time.sleep(0.2)
1128 except (gammu.ERR_NOTSUPPORTED, gammu.ERR_NOTIMPLEMENTED):
1129 wx.MessageDialog(self, _('It was not possible to read saved entry! It might be different than one saved in phone untill you reread all entries.'), _('Could not read saved entry!'), wx.OK | wx.ICON_WARNING).ShowModal()
1130 Wammu.Utils.ParseMemoryEntry(v, self.cfg)
1131 v['Synced'] = True
1132 # append new value to list
1133 self.values['contact'][v['MemoryType']].append(v)
1135 except gammu.GSMError, val:
1136 del busy
1137 v = backup
1138 self.ShowError(val[0])
1140 if (self.type[0] == 'contact' and self.type[1] == ' ') or not v.has_key('MemoryType'):
1141 self.ActivateView('contact', ' ')
1142 try:
1143 self.browser.ShowLocation(v['Location'], ('MemoryType', v['MemoryType']))
1144 except KeyError:
1145 pass
1146 else:
1147 self.ActivateView('contact', v['MemoryType'])
1148 try:
1149 self.browser.ShowLocation(v['Location'])
1150 except KeyError:
1151 pass
1153 def EditCalendar(self, v):
1154 backup = copy.deepcopy(v)
1155 shoulddelete = (v == {} or v['Location'] == 0)
1156 if Wammu.Editor.CalendarEditor(self, self.cfg, self.values, v).ShowModal() == wx.ID_OK:
1157 try:
1158 busy = wx.BusyInfo(_('Writing calendar...'))
1159 time.sleep(0.1)
1160 wx.Yield()
1161 # was entry moved => delete it from internal list
1162 if not shoulddelete:
1163 # delete from internal list
1164 for idx in range(len(self.values['calendar'][' '])):
1165 if self.values['calendar'][' '][idx] == v:
1166 del self.values['calendar'][' '][idx]
1167 break
1169 # have we specified location? => add or set
1170 if v['Location'] == 0:
1171 v['Location'] = self.sm.AddCalendar(v)
1172 else:
1173 try:
1174 v['Location'] = self.sm.SetCalendar(v)
1175 except (gammu.ERR_NOTSUPPORTED, gammu.ERR_NOTIMPLEMENTED):
1176 v['Location'] = self.sm.AddCalendar(v)
1178 # was entry moved => delete it from phone
1179 if not shoulddelete:
1180 if v['Location'] != backup['Location']:
1181 # delete from phone
1182 self.sm.DeleteCalendar(backup['Location'])
1184 # reread entry (it doesn't have to contain exactly same data as entered, it depends on phone features)
1185 try:
1186 v = self.sm.GetCalendar(v['Location'])
1187 except (gammu.ERR_NOTSUPPORTED, gammu.ERR_NOTIMPLEMENTED):
1188 wx.MessageDialog(self, _('It was not possible to read saved entry! It might be different than one saved in phone untill you reread all entries.'), _('Could not read saved entry!'), wx.OK | wx.ICON_WARNING).ShowModal()
1189 Wammu.Utils.ParseCalendar(v)
1190 v['Synced'] = True
1191 # append new value to list
1192 self.values['calendar'][' '].append(v)
1194 except gammu.GSMError, val:
1195 del busy
1196 v = backup
1197 self.ShowError(val[0])
1199 self.ActivateView('calendar', ' ')
1200 try:
1201 self.browser.ShowLocation(v['Location'])
1202 except KeyError:
1203 pass
1205 def EditTodo(self, v):
1206 backup = copy.deepcopy(v)
1207 shoulddelete = (v == {} or v['Location'] == 0)
1208 if Wammu.Editor.TodoEditor(self, self.cfg, self.values, v).ShowModal() == wx.ID_OK:
1209 try:
1210 busy = wx.BusyInfo(_('Writing todo...'))
1211 time.sleep(0.1)
1212 wx.Yield()
1213 # was entry moved => delete it from internal list
1214 if not shoulddelete:
1215 for idx in range(len(self.values['todo'][' '])):
1216 if self.values['todo'][' '][idx] == v:
1217 del self.values['todo'][' '][idx]
1218 break
1220 # have we specified location? => add or set
1221 if v['Location'] == 0:
1222 v['Location'] = self.sm.AddToDo(v)
1223 else:
1224 try:
1225 v['Location'] = self.sm.SetToDo(v)
1226 except (gammu.ERR_NOTSUPPORTED, gammu.ERR_NOTIMPLEMENTED):
1227 v['Location'] = self.sm.AddToDo(v)
1229 # was entry moved => delete it from phone
1230 if not shoulddelete:
1231 if v['Location'] != backup['Location']:
1232 # delete from phone
1233 self.sm.DeleteToDo(backup['Location'])
1235 # reread entry (it doesn't have to contain exactly same data as entered, it depends on phone features)
1236 try:
1237 v = self.sm.GetToDo(v['Location'])
1238 except (gammu.ERR_NOTSUPPORTED, gammu.ERR_NOTIMPLEMENTED):
1239 wx.MessageDialog(self, _('It was not possible to read saved entry! It might be different than one saved in phone untill you reread all entries.'), _('Could not read saved entry!'), wx.OK | wx.ICON_WARNING).ShowModal()
1240 Wammu.Utils.ParseTodo(v)
1241 v['Synced'] = True
1242 # append new value to list
1243 self.values['todo'][' '].append(v)
1244 except gammu.GSMError, val:
1245 del busy
1246 v = backup
1247 self.ShowError(val[0])
1249 self.ActivateView('todo', ' ')
1250 try:
1251 self.browser.ShowLocation(v['Location'])
1252 except KeyError:
1253 pass
1256 def OnEdit(self, evt):
1257 if evt.data != {} and not evt.data['Synced']:
1258 wx.MessageDialog(self, _('You can not work on this data, please retrieve it first from phone'), _('Data not up to date'), wx.OK | wx.ICON_ERROR).ShowModal()
1259 return
1260 if self.type[0] == 'contact':
1261 self.EditContact(evt.data)
1262 elif self.type[0] == 'calendar':
1263 self.EditCalendar(evt.data)
1264 elif self.type[0] == 'todo':
1265 self.EditTodo(evt.data)
1266 else:
1267 print 'Edit not yet implemented (type = %s)!' % self.type[0]
1269 def OnReply(self, evt):
1270 if self.type[0] == 'message':
1271 self.ComposeMessage({'Number': evt.data['Number']}, action = 'send')
1272 else:
1273 print 'Reply not yet implemented!'
1274 print evt.index
1276 def OnCall(self, evt):
1277 if self.type[0] in ['call', 'contact']:
1278 num = Wammu.Select.SelectContactNumber(self, evt.data)
1279 if num == None:
1280 return
1282 try:
1283 self.sm.DialVoice(num)
1284 except gammu.GSMError, val:
1285 self.ShowError(val[0])
1286 elif self.type[0] == 'message':
1287 try:
1288 self.sm.DialVoice(evt.data['Number'])
1289 except gammu.GSMError, val:
1290 self.ShowError(val[0])
1291 else:
1292 print 'Call not yet implemented (type = %s)!' % self.type[0]
1294 def OnMessage(self, evt):
1295 if self.type[0] in ['call', 'contact']:
1297 num = Wammu.Select.SelectContactNumber(self, evt.data)
1298 if num == None:
1299 return
1300 self.ComposeMessage({'Number': num}, action = 'send')
1301 elif self.type[0] == 'message':
1302 self.ComposeMessage({'Number': evt.data['Number']}, action = 'send')
1303 else:
1304 print 'Message send not yet implemented (type = %s)!' % self.type[0]
1306 def OnDuplicate(self, evt):
1307 if evt.data != {} and not evt.data['Synced']:
1308 wx.MessageDialog(self, _('You can not work on this data, please retrieve it first from phone'), _('Data not up to date'), wx.OK | wx.ICON_ERROR).ShowModal()
1309 return
1310 v = copy.deepcopy(evt.data)
1311 if self.type[0] == 'contact':
1312 v['Location'] = 0
1313 self.EditContact(v)
1314 elif self.type[0] == 'calendar':
1315 v['Location'] = 0
1316 self.EditCalendar(v)
1317 elif self.type[0] == 'todo':
1318 v['Location'] = 0
1319 self.EditTodo(v)
1320 elif self.type[0] == 'message':
1321 self.ComposeMessage(v)
1322 else:
1323 print 'Duplicate not yet implemented (type = %s)!' % self.type[0]
1326 def OnSend(self, evt):
1327 if evt.data != {} and not evt.data['Synced']:
1328 wx.MessageDialog(self, _('You can not work on this data, please retrieve it first from phone'), _('Data not up to date'), wx.OK | wx.ICON_ERROR).ShowModal()
1329 return
1330 if self.type[0] == 'message':
1331 v = evt.data
1332 try:
1333 try:
1334 for loc in v['Location'].split(', '):
1335 self.sm.SendSavedSMS(0, int(loc))
1336 except gammu.ERR_NOTSUPPORTED:
1337 for msg in v['SMS']:
1338 self.sm.SendSMS(msg)
1339 except gammu.GSMError, val:
1340 self.ShowError(val[0])
1342 def SMSToMails(self, evt):
1343 messages = self.values['message']['Read'] + \
1344 self.values['message']['UnRead'] + \
1345 self.values['message']['Sent'] + \
1346 self.values['message']['UnSent']
1347 contacts = self.values['contact']['ME'] + \
1348 self.values['contact']['SM']
1349 Wammu.SMSExport.SMSExport(self, messages, contacts)
1351 def SMSToXML(self, evt):
1352 messages = self.values['message']['Read'] + \
1353 self.values['message']['UnRead'] + \
1354 self.values['message']['Sent'] + \
1355 self.values['message']['UnSent']
1356 contacts = self.values['contact']['ME'] + \
1357 self.values['contact']['SM']
1358 Wammu.SMSXML.SMSExportXML(self, messages, contacts)
1360 def SelectBackupFile(self, type, save = True, data = False):
1361 wildcard = ''
1362 if type == 'message':
1363 wildcard += _('Gammu messages backup') + ' (*.smsbackup)|*.smsbackup|'
1364 exts = ['smsbackup']
1365 else:
1366 if not save:
1367 wildcard += _('All backup formats') + '|*.backup;*.lmb;*.vcf;*.ldif;*.vcs;*.ics|'
1369 wildcard += _('Gammu backup [all data]') + ' (*.backup)|*.backup|'
1370 exts = ['backup']
1372 if type in ['contact', 'all']:
1373 wildcard += _('Nokia backup [contacts]') + ' (*.lmb)|*.lmb|'
1374 exts.append('lmb')
1375 if type in ['contact', 'all']:
1376 wildcard += _('vCard [contacts]') + ' (*.vcf)|*.vcf|'
1377 exts.append('vcf')
1378 if type in ['contact', 'all']:
1379 wildcard += _('LDIF [contacts]') + ' (*.ldif)|*.ldif|'
1380 exts.append('ldif')
1381 if type in ['todo', 'calendar', 'all']:
1382 wildcard += _('vCalendar [todo,calendar]') + ' (*.vcs)|*.vcs|'
1383 exts.append('vcs')
1384 if type in ['todo', 'calendar', 'all']:
1385 wildcard += _('iCalendar [todo,calendar]') + ' (*.ics)|*.ics|'
1386 exts.append('ics')
1388 wildcard += _('All files') + ' (*.*)|*.*'
1389 exts.append(None)
1391 if data:
1392 if save:
1393 dlg = wx.FileDialog(self, _('Save data as...'), os.getcwd(), "", wildcard, wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
1394 else:
1395 dlg = wx.FileDialog(self, _('Read data'), os.getcwd(), "", wildcard, wx.OPEN|wx.CHANGE_DIR)
1396 else:
1397 if save:
1398 dlg = wx.FileDialog(self, _('Save backup as...'), os.getcwd(), "", wildcard, wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
1399 else:
1400 dlg = wx.FileDialog(self, _('Import backup'), os.getcwd(), "", wildcard, wx.OPEN|wx.CHANGE_DIR)
1401 if dlg.ShowModal() == wx.ID_OK:
1402 path = dlg.GetPath()
1403 if save:
1404 ext = exts[dlg.GetFilterIndex()]
1405 # Add automatic extension if we know one and file does not
1406 # have any
1407 if (os.path.splitext(path)[1] == '' and
1408 ext is not None):
1409 path += '.' + ext
1410 return Wammu.Locales.ConsoleStrConv(path)
1411 return None
1413 def ReadBackup(self, type, data = False):
1414 filename = self.SelectBackupFile(type, save = False, data = data)
1415 if filename == None:
1416 return (None, None)
1417 try:
1418 if type == 'message':
1419 backup = gammu.ReadSMSBackup(filename)
1420 else:
1421 backup = gammu.ReadBackup(filename)
1422 except gammu.GSMError, val:
1423 info = val[0]
1424 evt = Wammu.Events.ShowMessageEvent(
1425 message = Wammu.Utils.FormatError(_('Error while reading backup'), info),
1426 title = _('Error Occured'),
1427 errortype = 'gammu',
1428 type = wx.ICON_ERROR)
1429 wx.PostEvent(self, evt)
1430 return (None, None)
1431 return (filename, backup)
1433 def ReadData(self, evt):
1434 (filename, backup) = self.ReadBackup('all', True)
1435 if backup == None:
1436 return
1438 if len(backup['PhonePhonebook']) > 0:
1439 self.values['contact']['ME'] = map(Wammu.Utils.ParseMemoryEntry, backup['PhonePhonebook'], [self.cfg] * len(backup['PhonePhonebook']))
1440 if len(backup['SIMPhonebook']) > 0:
1441 self.values['contact']['SM'] = map(Wammu.Utils.ParseMemoryEntry, backup['SIMPhonebook'], [self.cfg] * len(backup['SIMPhonebook']))
1442 if len(backup['ToDo']) > 0:
1443 self.values['todo'][' '] = map(Wammu.Utils.ParseTodo, backup['ToDo'])
1444 if len(backup['Calendar']) > 0:
1445 self.values['calendar'][' '] = map(Wammu.Utils.ParseCalendar, backup['Calendar'])
1447 self.ActivateView('contact', ' ')
1449 self.SetStatusText(_('Data has been read from file "%s"') % StrConv(filename))
1451 def ReadSMSData(self, evt):
1452 (filename, backup) = self.ReadBackup('message', True)
1453 if backup == None:
1454 return
1456 res = Wammu.Utils.ProcessMessages(map(lambda x:[x], backup), False)
1458 self.values['message']['Sent'] = res['sent']
1459 self.values['message']['UnSent'] = res['unsent']
1460 self.values['message']['Read'] = res['read']
1461 self.values['message']['UnRead'] = res['unread']
1463 self.ActivateView('message', ' ')
1465 self.SetStatusText(_('Data has been read from file "%s"') % StrConv(filename))
1467 def ImportSMS(self, evt):
1468 (filename, backup) = self.ReadBackup('message')
1469 if backup == None:
1470 return
1471 choices = []
1472 values = []
1473 if len(backup) > 0:
1474 values.append('message')
1475 choices.append(_('%d messages') % len(backup))
1477 if len(values) == 0:
1478 wx.MessageDialog(self,
1479 _('No importable data were found in file "%s"') % strconv(filename),
1480 _('No data to import'),
1481 wx.OK | wx.ICON_INFORMATION).ShowModal()
1482 return
1484 dlg = wx.lib.dialogs.MultipleChoiceDialog(self, _('Following data was found in backup, select which of these do you want to be added into phone.'), _('Select what to import'),
1485 choices,style = wx.CHOICEDLG_STYLE | wx.RESIZE_BORDER,
1486 size = (600, 200))
1487 if dlg.ShowModal() != wx.ID_OK:
1488 return
1490 lst = dlg.GetValue()
1491 if len(lst) == 0:
1492 return
1494 try:
1495 busy = wx.BusyInfo(_('Importing data...'))
1496 time.sleep(0.1)
1497 wx.Yield()
1498 for i in lst:
1499 datatype = values[i]
1500 if datatype == 'message':
1501 smsl = []
1502 for v in backup:
1503 v['Folder'] = 2 # FIXME: this should be configurable
1504 v['SMSC']['Location'] = 1
1505 (v['Location'], v['Folder']) = self.sm.AddSMS(v)
1506 # reread entry (it doesn't have to contain exactly same data as entered, it depends on phone features)
1507 v = self.sm.GetSMS(0, v['Location'])
1508 smsl.append(v)
1510 res = Wammu.Utils.ProcessMessages(smsl, True)
1512 self.values['message']['Sent'] += res['sent']
1513 self.values['message']['UnSent'] += res['unsent']
1514 self.values['message']['Read'] += res['read']
1515 self.values['message']['UnRead'] += res['unread']
1517 self.ActivateView('message', ' ')
1519 del busy
1520 wx.Yield()
1522 wx.MessageDialog(self,
1523 _('Backup has been imported from file "%s"') % StrConv(filename),
1524 _('Backup imported'),
1525 wx.OK | wx.ICON_INFORMATION).ShowModal()
1527 except gammu.GSMError, val:
1528 self.ShowError(val[0])
1530 wx.MessageDialog(self,
1531 _('Restoring from file "%s" has failed, some parts of backup might have been stored to phone and some were not.') % StrConv(filename),
1532 _('Backup import failed'),
1533 wx.OK | wx.ICON_INFORMATION).ShowModal()
1535 def Import(self, evt):
1536 (filename, backup) = self.ReadBackup('all')
1537 if backup == None:
1538 return
1539 choices = []
1540 values = []
1541 if len(backup['PhonePhonebook']) > 0:
1542 values.append('PhonePhonebook')
1543 choices.append(_('%d phone contact entries') % len(backup['PhonePhonebook']))
1544 if len(backup['SIMPhonebook']) > 0:
1545 values.append('SIMPhonebook')
1546 choices.append(_('%d SIM contact entries') % len(backup['SIMPhonebook']))
1547 if len(backup['ToDo']) > 0:
1548 values.append('ToDo')
1549 choices.append(_('%d to do entries') % len(backup['ToDo']))
1550 if len(backup['Calendar']) > 0:
1551 values.append('Calendar')
1552 choices.append(_('%d calendar entries') % len(backup['Calendar']))
1554 if len(values) == 0:
1555 wx.MessageDialog(self,
1556 _('No importable data were found in file "%s"') % StrConv(filename),
1557 _('No data to import'),
1558 wx.OK | wx.ICON_INFORMATION).ShowModal()
1559 return
1561 msg = ''
1562 if backup['Model'] != '':
1563 msg = '\n \n' + _('Backup saved from phone %s') % backup['Model']
1564 if backup['IMEI'] != '':
1565 msg += _(', serial number %s') % backup['IMEI']
1566 if backup['Creator'] != '':
1567 msg += '\n \n' + _('Backup was created by %s') % backup['Creator']
1568 if backup['DateTime'] != None:
1569 msg += '\n \n' + _('Backup saved on %s') % str(backup['DateTime'])
1571 dlg = wx.lib.dialogs.MultipleChoiceDialog(self, _('Following data was found in backup, select which of these do you want to be added into phone.') + msg, _('Select what to import'),
1572 choices,style = wx.CHOICEDLG_STYLE | wx.RESIZE_BORDER,
1573 size = (600, 200))
1574 if dlg.ShowModal() != wx.ID_OK:
1575 return
1577 lst = dlg.GetValue()
1578 if len(lst) == 0:
1579 return
1581 try:
1582 busy = wx.BusyInfo(_('Importing data...'))
1583 time.sleep(0.1)
1584 wx.Yield()
1585 for i in lst:
1586 datatype = values[i]
1587 if datatype == 'PhonePhonebook':
1588 for v in backup['PhonePhonebook']:
1589 v['Location'] = self.sm.AddMemory(v)
1590 time.sleep(0.5)
1591 # reread entry (it doesn't have to contain exactly same data as entered, it depends on phone features)
1592 v = self.sm.GetMemory(v['MemoryType'], v['Location'])
1593 Wammu.Utils.ParseMemoryEntry(v, self.cfg)
1594 v['Synced'] = True
1595 # append new value to list
1596 self.values['contact'][v['MemoryType']].append(v)
1597 self.ActivateView('contact', 'ME')
1598 elif datatype == 'SIMPhonebook':
1599 for v in backup['SIMPhonebook']:
1600 v['Location'] = self.sm.AddMemory(v)
1601 time.sleep(0.5)
1602 # reread entry (it doesn't have to contain exactly same data as entered, it depends on phone features)
1603 v = self.sm.GetMemory(v['MemoryType'], v['Location'])
1604 Wammu.Utils.ParseMemoryEntry(v, self.cfg)
1605 v['Synced'] = True
1606 # append new value to list
1607 self.values['contact'][v['MemoryType']].append(v)
1608 self.ActivateView('contact', 'SM')
1609 elif datatype == 'ToDo':
1610 for v in backup['ToDo']:
1611 v['Location'] = self.sm.AddToDo(v)
1612 time.sleep(0.5)
1613 # reread entry (it doesn't have to contain exactly same data as entered, it depends on phone features)
1614 v = self.sm.GetToDo(v['Location'])
1615 Wammu.Utils.ParseTodo(v)
1616 v['Synced'] = True
1617 # append new value to list
1618 self.values['todo'][' '].append(v)
1619 self.ActivateView('todo', ' ')
1620 elif datatype == 'Calendar':
1621 for v in backup['Calendar']:
1622 v['Location'] = self.sm.AddCalendar(v)
1623 time.sleep(0.5)
1624 # reread entry (it doesn't have to contain exactly same data as entered, it depends on phone features)
1625 v = self.sm.GetCalendar(v['Location'])
1626 Wammu.Utils.ParseCalendar(v)
1627 v['Synced'] = True
1628 # append new value to list
1629 self.values['calendar'][' '].append(v)
1630 self.ActivateView('calendar', ' ')
1632 del busy
1633 wx.Yield()
1635 wx.MessageDialog(self,
1636 _('Backup has been imported from file "%s"') % StrConv(filename),
1637 _('Backup imported'),
1638 wx.OK | wx.ICON_INFORMATION).ShowModal()
1640 except gammu.GSMError, val:
1641 self.ShowError(val[0])
1643 wx.MessageDialog(self,
1644 _('Restoring from file "%s" has failed, some parts of backup might have been stored to phone and some were not.') % StrConv(filename),
1645 _('Backup import failed'),
1646 wx.OK | wx.ICON_INFORMATION).ShowModal()
1648 def WriteData(self, evt):
1649 self.DoBackup(True, 'all')
1651 def WriteSMSData(self, evt):
1652 self.DoBackup(True, 'message')
1654 def Backup(self, evt):
1655 self.DoBackup(False, 'all')
1657 def BackupSMS(self, evt):
1658 self.DoBackup(False, 'message')
1660 def PrepareBackup(self):
1661 backup = {}
1662 backup['Creator'] = 'Wammu ' + Wammu.__version__
1663 backup['IMEI'] = self.IMEI
1664 backup['Model'] = '%s %s %s' % ( self.Manufacturer, self.Model, self.Version)
1665 return backup
1667 def WriteBackup(self, filename, type, backup, data = False):
1668 try:
1669 if type == 'message':
1670 # Backup is here our internal SMS list: [{'SMS':[{sms1}, {sms2}]}, ...]
1671 data = map(lambda x:x['SMS'], backup)
1672 backup = []
1673 for x in data:
1674 backup += x
1675 gammu.SaveSMSBackup(filename, backup)
1676 else:
1677 gammu.SaveBackup(filename, backup)
1678 if data:
1679 self.SetStatusText(_('Backup has been saved to file "%s"') % StrConv(filename))
1680 else:
1681 self.SetStatusText(_('Data has been saved to file "%s"') % StrConv(filename))
1682 except gammu.GSMError, val:
1683 info = val[0]
1684 evt = Wammu.Events.ShowMessageEvent(
1685 message = Wammu.Utils.FormatError(_('Error while saving backup'), info),
1686 title = _('Error Occured'),
1687 errortype = 'gammu',
1688 type = wx.ICON_ERROR)
1689 wx.PostEvent(self, evt)
1690 except MemoryError, val:
1691 info = val[0]
1692 evt = Wammu.Events.ShowMessageEvent(
1693 message = _('Error while saving backup, probably some limit inside of Gammu exceeded.\n%s') % str(info),
1694 title = _('Error Occured'),
1695 type = wx.ICON_ERROR)
1696 wx.PostEvent(self, evt)
1698 def DoBackup(self, data, type):
1699 filename = self.SelectBackupFile(type, data = data)
1700 if filename == None:
1701 return
1702 ext = os.path.splitext(filename)[1].lower()
1704 if type == 'message':
1705 backup = self.values['message']['Read'] + self.values['message']['UnRead'] + self.values['message']['Sent'] + self.values['message']['UnSent']
1706 else:
1707 backup = self.PrepareBackup()
1708 if ext in ['.vcf', '.ldif']:
1709 # these support only one phonebook, so merged it
1710 backup['PhonePhonebook'] = self.values['contact']['ME'] + self.values['contact']['SM']
1711 else:
1712 backup['PhonePhonebook'] = self.values['contact']['ME']
1713 backup['SIMPhonebook'] = self.values['contact']['SM']
1715 backup['ToDo'] = self.values['todo'][' ']
1716 backup['Calendar'] = self.values['calendar'][' ']
1717 self.WriteBackup(filename, type, backup, data)
1720 def OnBackup(self, evt):
1721 filename = self.SelectBackupFile(self.type[0])
1722 if filename == None:
1723 return
1724 ext = os.path.splitext(filename)[1].lower()
1725 lst = evt.lst
1726 if self.type[0] == 'message':
1727 backup = lst
1728 else:
1729 backup = self.PrepareBackup()
1730 if self.type[0] == 'contact':
1731 if ext in ['.vcf', '.ldif']:
1732 # these support only one phonebook, so keep it merged
1733 backup['PhonePhonebook'] = lst
1734 else:
1735 sim = []
1736 phone = []
1737 for item in lst:
1738 if item['MemoryType'] == 'SM':
1739 sim.append(item)
1740 elif item['MemoryType'] == 'ME':
1741 phone.append(item)
1742 backup['PhonePhonebook'] = phone
1743 backup['SIMPhonebook'] = sim
1744 elif self.type[0] == 'todo':
1745 backup['ToDo'] = lst
1746 elif self.type[0] == 'calendar':
1747 backup['Calendar'] = lst
1749 self.WriteBackup(filename, self.type[0], backup)
1751 def OnDelete(self, evt):
1752 # first check on supported types
1753 if not self.type[0] in ['contact', 'call', 'message', 'todo', 'calendar']:
1754 print 'Delete not yet implemented! (items to delete = %s, type = %s)' % (str(evt.lst), self.type[0])
1755 return
1757 lst = evt.lst
1759 if len(lst) == 0:
1760 # nothing to delete
1761 return
1763 if not lst[0]['Synced']:
1764 wx.MessageDialog(self, _('You can not work on this data, please retrieve it first from phone'), _('Data not up to date'), wx.OK | wx.ICON_ERROR).ShowModal()
1765 return
1767 # check for confirmation
1768 if self.cfg.Read('/Wammu/ConfirmDelete') == 'yes':
1769 count = len(lst)
1770 if count == 1:
1771 v = lst[0]
1772 if self.type[0] == 'contact':
1773 txt = _('Are you sure you want to delete contact "%s"?') % v['Name']
1774 elif self.type[0] == 'call':
1775 txt = _('Are you sure you want to delete call from "%s"?') % v['Name']
1776 elif self.type[0] == 'message':
1777 txt = _('Are you sure you want to delete message from "%s"?') % v['Number']
1778 elif self.type[0] == 'todo':
1779 txt = _('Are you sure you want to delete todo entry "%s"?') % v['Text']
1780 elif self.type[0] == 'calendar':
1781 txt = _('Are you sure you want to delete calendar entry "%s"?') % v['Text']
1782 else:
1783 if self.type[0] == 'contact':
1784 txt = Wammu.Locales.ngettext(
1785 'Are you sure you want to delete %d contact?',
1786 'Are you sure you want to delete %d contacts?',
1787 count) % count
1788 elif self.type[0] == 'call':
1789 txt = Wammu.Locales.ngettext(
1790 'Are you sure you want to delete %d call?',
1791 'Are you sure you want to delete %d calls?',
1792 count) % count
1793 elif self.type[0] == 'message':
1794 txt = Wammu.Locales.ngettext(
1795 'Are you sure you want to delete %d message?',
1796 'Are you sure you want to delete %d messages?',
1797 count) % count
1798 elif self.type[0] == 'todo':
1799 txt = Wammu.Locales.ngettext(
1800 'Are you sure you want to delete %d todo entry?',
1801 'Are you sure you want to delete %d todo entries?',
1802 count) % count
1803 elif self.type[0] == 'calendar':
1804 txt = Wammu.Locales.ngettext(
1805 'Are you sure you want to delete %d calendar entry?',
1806 'Are you sure you want to delete %d calendar entries?',
1807 count) % count
1808 dlg = wx.MessageDialog(self,
1809 txt,
1810 _('Confirm deleting'),
1811 wx.OK | wx.CANCEL | wx.ICON_WARNING)
1812 if dlg.ShowModal() != wx.ID_OK:
1813 return
1815 # do real delete
1816 try:
1817 if self.type[0] == 'contact' or self.type[0] == 'call':
1818 busy = wx.BusyInfo(_('Deleting contact(s)...'))
1819 time.sleep(0.1)
1820 wx.Yield()
1821 for v in lst:
1822 self.sm.DeleteMemory(v['MemoryType'], v['Location'])
1823 for idx in range(len(self.values[self.type[0]][v['MemoryType']])):
1824 if self.values[self.type[0]][v['MemoryType']][idx] == v:
1825 del self.values[self.type[0]][v['MemoryType']][idx]
1826 break
1827 elif self.type[0] == 'message':
1828 busy = wx.BusyInfo(_('Deleting message(s)...'))
1829 time.sleep(0.1)
1830 wx.Yield()
1831 for v in lst:
1832 for loc in v['Location'].split(', '):
1833 self.sm.DeleteSMS(0, int(loc))
1834 for idx in range(len(self.values[self.type[0]][v['State']])):
1835 if self.values[self.type[0]][v['State']][idx] == v:
1836 del self.values[self.type[0]][v['State']][idx]
1837 break
1838 elif self.type[0] == 'todo':
1839 busy = wx.BusyInfo(_('Deleting todo(s)...'))
1840 time.sleep(0.1)
1841 wx.Yield()
1842 for v in lst:
1843 self.sm.DeleteToDo(v['Location'])
1844 for idx in range(len(self.values[self.type[0]][' '])):
1845 if self.values[self.type[0]][' '][idx] == v:
1846 del self.values[self.type[0]][' '][idx]
1847 break
1848 elif self.type[0] == 'calendar':
1849 busy = wx.BusyInfo(_('Deleting calendar event(s)...'))
1850 time.sleep(0.1)
1851 wx.Yield()
1852 for v in lst:
1853 self.sm.DeleteCalendar(v['Location'])
1854 for idx in range(len(self.values[self.type[0]][' '])):
1855 if self.values[self.type[0]][' '][idx] == v:
1856 del self.values[self.type[0]][' '][idx]
1857 break
1858 except gammu.GSMError, val:
1859 try:
1860 del busy
1861 finally:
1862 self.ShowError(val[0])
1864 self.ActivateView(self.type[0], self.type[1])
1866 def OnLink(self, evt):
1867 v = evt.link.split('://')
1868 if len(v) != 2:
1869 print 'Bad URL!'
1870 return
1871 if v[0] == 'memory':
1872 t = v[1].split('/')
1873 if len(t) != 2:
1874 print 'Bad URL!'
1875 return
1877 if t[0] in ['ME', 'SM']:
1878 self.ActivateView('contact', t[0])
1879 try:
1880 self.browser.ShowLocation(int(t[1]))
1881 except KeyError:
1882 pass
1884 elif t[0] in ['MC', 'RC', 'DC']:
1885 self.ActivateView('call', t[0])
1886 try:
1887 self.browser.ShowLocation(int(t[1]))
1888 except KeyError:
1889 pass
1891 else:
1892 print 'Not supported memory type "%s"' % t[0]
1893 return
1894 else:
1895 print 'This link not yet implemented: "%s"' % evt.link
1897 def OnShowMessage(self, evt):
1898 try:
1899 if self.progress.IsShown():
1900 parent = self.progress
1901 else:
1902 parent = self
1903 except:
1904 parent = self
1906 # Is is Gammu error?
1907 if hasattr(evt, 'errortype') and evt.errortype == 'gammu':
1908 Wammu.ErrorMessage.ErrorMessage(parent,
1909 StrConv(evt.message),
1910 StrConv(evt.title)).ShowModal()
1911 else:
1912 wx.MessageDialog(parent,
1913 StrConv(evt.message),
1914 StrConv(evt.title),
1915 wx.OK | evt.type).ShowModal()
1917 if hasattr(evt, 'lock'):
1918 evt.lock.release()
1920 def ShowInfo(self, event):
1921 self.ShowProgress(_('Reading phone information'))
1922 Wammu.Info.GetInfo(self, self.sm).start()
1923 self.nextfun = self.ActivateView
1924 self.nextarg = ('info', ' ')
1927 # Calls
1930 def ShowCalls(self, event):
1931 self.GetCallsType('MC')
1932 self.nextfun = self.ShowCalls2
1933 self.nextarg = ()
1935 def ShowCalls2(self):
1936 self.GetCallsType('DC')
1937 self.nextfun = self.ShowCalls3
1938 self.nextarg = ()
1940 def ShowCalls3(self):
1941 self.GetCallsType('RC')
1942 self.nextfun = self.ActivateView
1943 self.nextarg = ('call', ' ')
1945 def GetCallsType(self, type):
1946 self.ShowProgress(_('Reading calls of type %s') % type)
1947 Wammu.Memory.GetMemory(self, self.sm, 'call', type).start()
1950 # Contacts
1953 def ShowContacts(self, event):
1954 self.GetContactsType('SM')
1955 self.nextfun = self.ShowContacts2
1956 self.nextarg = ()
1958 def ShowContacts2(self):
1959 self.GetContactsType('ME')
1960 self.nextfun = self.ActivateView
1961 self.nextarg = ('contact', ' ')
1963 def ShowContactsME(self, event):
1964 self.GetContactsType('ME')
1965 self.nextfun = self.ActivateView
1966 self.nextarg = ('contact', 'ME')
1968 def ShowContactsSM(self, event):
1969 self.GetContactsType('SM')
1970 self.nextfun = self.ActivateView
1971 self.nextarg = ('contact', 'SM')
1973 def GetContactsType(self, type):
1974 self.ShowProgress(_('Reading contacts from %s') % type)
1975 Wammu.Memory.GetMemory(self, self.sm, 'contact', type).start()
1978 # Messages
1981 def ShowMessages(self, event):
1982 self.ShowProgress(_('Reading messages'))
1983 Wammu.Message.GetMessage(self, self.sm).start()
1984 self.nextfun = self.ActivateView
1985 self.nextarg = ('message', ' ')
1988 # Todos
1991 def ShowTodos(self, event):
1992 self.ShowProgress(_('Reading todos'))
1993 Wammu.Todo.GetTodo(self, self.sm).start()
1994 self.nextfun = self.ActivateView
1995 self.nextarg = ('todo', ' ')
1998 # Calendars
2001 def ShowCalendar(self, event):
2002 self.ShowProgress(_('Reading calendar'))
2003 Wammu.Calendar.GetCalendar(self, self.sm).start()
2004 self.nextfun = self.ActivateView
2005 self.nextarg = ('calendar', ' ')
2008 # Time
2011 def SyncTime(self, event):
2012 busy = wx.BusyInfo(_('Setting time in phone...'))
2013 time.sleep(0.1)
2014 wx.Yield()
2015 try:
2016 self.sm.SetDateTime(datetime.datetime.now())
2017 except gammu.GSMError, val:
2018 del busy
2019 self.ShowError(val[0])
2022 # Files
2025 def SendFile(self, event):
2027 Sends file to phone.
2029 @todo: Maybe we could add some wildcards for commonly used file types.
2031 dlg = wx.FileDialog(self, _('Send file to phone'), os.getcwd(), '', _('All files') + ' (*.*)|*.*', wx.OPEN|wx.CHANGE_DIR)
2032 if dlg.ShowModal() == wx.ID_OK:
2033 path = dlg.GetPath()
2034 try:
2035 file_data = open(path, 'r').read()
2036 file_f = {
2037 'ID_FullName': '',
2038 'Name': os.path.basename(path),
2039 'Folder': 0,
2040 'Level': 1,
2041 'Used': len(file_data),
2042 'Buffer': file_data,
2043 'Type': 'Other',
2044 'Protected': 0,
2045 'ReadOnly': 0,
2046 'Hidden': 0,
2047 'System': 0,
2048 'Handle': 0,
2049 'Pos': 0,
2050 'Finished': 0
2052 busy = wx.BusyInfo(_('Sending file to phone...'))
2053 time.sleep(0.1)
2054 wx.Yield()
2055 try:
2056 while (not file_f['Finished']):
2057 file_f = self.sm.SendFilePart(file_f)
2058 except gammu.ERR_PERMISSION:
2059 wx.MessageDialog(self,
2060 _('Transfer has been rejected by phone.'),
2061 _('Transfer rejected!'),
2062 wx.OK | wx.ICON_ERROR).ShowModal()
2063 except gammu.GSMError, val:
2064 del busy
2065 self.ShowError(val[0])
2066 except IOError:
2067 wx.MessageDialog(self,
2068 _('Selected file "%s" was not found, no data read.') % path,
2069 _('File not found!'),
2070 wx.OK | wx.ICON_ERROR).ShowModal()
2073 # Connecting / Disconnecting
2076 def PhoneConnect(self, event = None):
2077 busy = wx.BusyInfo(_('One moment please, connecting to phone...'))
2078 time.sleep(0.1)
2079 wx.Yield()
2080 section = self.cfg.ReadInt('/Gammu/Section')
2081 config = self.cfg.gammu.GetConfig(section)
2082 if config['Connection'] == '' or config['Device'] == '':
2083 wx.MessageDialog(self,
2084 _('Phone connection is not properly configured, can not connect to phone.'),
2085 _('Connection not configured!'),
2086 wx.OK | wx.ICON_ERROR).ShowModal()
2087 return
2088 cfg = {
2089 'StartInfo': self.cfg.Read('/Gammu/StartInfo'),
2090 'UseGlobalDebugFile': 1,
2091 'DebugFile': None, # Set on other place
2092 'SyncTime': self.cfg.Read('/Gammu/SyncTime'),
2093 'Connection': config['Connection'],
2094 'LockDevice': self.cfg.Read('/Gammu/LockDevice'),
2095 'DebugLevel': 'textalldate', # Set on other place
2096 'Device': config['Device'],
2097 'Model': config['Model'],
2099 if cfg['Model'] == 'auto':
2100 cfg['Model'] = ''
2101 self.sm.SetConfig(0, cfg)
2102 try:
2103 self.sm.Init()
2104 self.sm.SetIncomingCallback(self.IncomingEvent)
2105 try:
2106 self.sm.SetIncomingCall(True)
2107 except gammu.GSMError:
2108 pass
2109 self.TogglePhoneMenus(True)
2110 self.SetupNumberPrefix()
2111 try:
2112 self.IMEI = self.sm.GetIMEI()
2113 except gammu.GSMError:
2114 pass
2115 try:
2116 self.Manufacturer = self.sm.GetManufacturer()
2117 self.cfg.Write('/Phone-0/Manufacturer', self.Manufacturer)
2118 except gammu.GSMError:
2119 pass
2120 try:
2121 m = self.sm.GetModel()
2122 if m[0] == '' or m[0] == 'unknown':
2123 self.Model = m[1]
2124 else:
2125 self.Model = m[0]
2126 self.cfg.Write('/Phone-0/Model', self.Model)
2127 except gammu.GSMError:
2128 pass
2129 try:
2130 self.Version = self.sm.GetFirmware()[0]
2131 except:
2132 pass
2134 except gammu.GSMError, val:
2135 del busy
2136 self.ShowError(val[0])
2137 try:
2138 self.sm.Terminate()
2139 except gammu.GSMError, val:
2140 pass
2142 def DBUSActionCallback(self, id, action):
2144 Called when user does something on notification.
2146 self.dbus_notify.CloseNotification(self.last_dbus_id)
2147 if action == 'accept-call':
2148 self.sm.AnswerCall(0, True)
2149 elif action == 'reject-call':
2150 self.sm.CancelCall(0, True)
2151 else:
2152 print 'Unknown DBUS event: %s' % action
2154 def DBUSNotify(self, title, message, actions):
2156 Performs D-Bus notification if available.
2158 if self.dbus_notify is not None:
2159 self.last_dbus_id = self.dbus_notify.Notify(
2160 'Wammu', self.last_dbus_id, 'wammu',
2161 title, message, actions, {}, -1)
2164 def IncomingEvent(self, sm, type, data):
2166 Called on incoming event from phone.
2169 if type == 'Call':
2170 if data['Status'] != 'IncomingCall':
2171 # We care only about incoming calls
2172 return
2173 if data['Number'] == '':
2174 msg = _('Your phone has just received incoming call')
2175 else:
2176 msg = _('Your phone has just received incoming call from %s') % data['Number']
2177 self.DBUSNotify(_('Incoming call'), msg,
2178 ['reject-call', _('Reject'), 'accept-call', _('Accept')])
2180 def PhoneDisconnect(self, event = None):
2181 busy = wx.BusyInfo(_('One moment please, disconnecting from phone...'))
2182 time.sleep(0.1)
2183 wx.Yield()
2184 try:
2185 self.sm.Terminate()
2186 except gammu.ERR_NOTCONNECTED:
2187 pass
2188 except gammu.GSMError, val:
2189 del busy
2190 self.ShowError(val[0])
2191 self.TogglePhoneMenus(False)
2193 def SearchMessage(self, text):
2195 This has to send message as it is called from different thread.
2197 evt = Wammu.Events.TextEvent(text = text + '\n')
2198 wx.PostEvent(self.searchlog, evt)
2200 def SearchDone(self, lst):
2202 This has to send message as it is called from different thread.
2204 self.founddevices = lst
2205 evt = Wammu.Events.DoneEvent()
2206 wx.PostEvent(self.searchlog, evt)
2208 def SearchPhone(self, evt = None):
2209 index = self.cfg.gammu.FirstFree()
2210 result = Wammu.PhoneWizard.RunConfigureWizard(self, index)
2211 if result is not None:
2212 self.cfg.gammu.SetConfig(result['Position'], result['Device'], result['Connection'], result['Name'])
2213 self.cfg.WriteInt('/Gammu/Section', index)
2215 def About(self, evt = None):
2216 Wammu.About.AboutBox(self).ShowModal()
2218 def Website(self, evt = None):
2219 Wammu.Webbrowser.Open("http://%swammu.eu/?version=%s" % (Wammu.Utils.GetWebsiteLang(), Wammu.__version__))
2221 def Support(self, evt = None):
2222 Wammu.Webbrowser.Open("http://%swammu.eu/support?version=%s" % (Wammu.Utils.GetWebsiteLang(), Wammu.__version__))
2224 def ReportBug(self, evt = None):
2225 Wammu.Webbrowser.Open("http://bugs.cihar.com/set_project.php?ref=bug_report_page.php&project_id=1")
2227 def PhoneDB(self, evt = None):
2228 Wammu.Webbrowser.Open("http://%scihar.com/gammu/phonedb" % Wammu.Utils.GetWebsiteLang())
2230 def Talkback(self, evt = None):
2231 Wammu.TalkbackDialog.DoTalkback(self, self.cfg, 0)
2233 def Donate(self, evt = None):
2234 Wammu.Webbrowser.Open("http://%swammu.eu/donate?src=wammu" % Wammu.Utils.GetWebsiteLang())
2236 def SaveLog(self, evt = None):
2238 Saves debug log to file.
2240 dlg = wx.FileDialog(self,
2241 _('Save debug log as...'),
2242 os.getcwd(),
2243 'wammu.log',
2245 wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR)
2246 if dlg.ShowModal() == wx.ID_OK:
2247 Wammu.ErrorLog.SaveLog(filename = dlg.GetPath())