PyXR

c:\projects\bitpim\src \ setphone_wizard.py



0001 #!/usr/bin/env python
0002 
0003 ### BITPIM
0004 ###
0005 ### Copyright (C) 2006 Joe Pham <djpham@bitpim.org>
0006 ###
0007 ### This program is free software; you can redistribute it and/or modify
0008 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0009 ###
0010 ### $Id: setphone_wizard.py 3942 2007-01-27 00:37:20Z djpham $
0011 
0012 """ Handle setting phone wizard
0013 """
0014 
0015 # wx modules
0016 import wx
0017 import wx.wizard as wiz
0018 
0019 # BitPim modules
0020 import common
0021 import comscan
0022 import phone_detect
0023 import phones
0024 import usbscan
0025 
0026 #-------------------------------------------------------------------------------
0027 class MyPage(wiz.WizardPageSimple):
0028     """My Common Page Setup"""
0029     def __init__(self, parent, title, instruction=None):
0030         super(MyPage, self).__init__(parent)
0031         vs=wx.BoxSizer(wx.VERTICAL)
0032         _title = wx.StaticText(self, -1, title)
0033         _title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
0034         vs.Add(_title, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
0035         vs.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
0036         if instruction:
0037             _inst=wx.StaticText(self, -1, instruction)
0038             vs.Add(_inst, 0, wx.ALIGN_LEFT|wx.ALL, 5)
0039         # your own controls goes here
0040         _my_ctrl=self.GetMyControls()
0041         if _my_ctrl:
0042             vs.Add(_my_ctrl, 1, wx.EXPAND|wx.ALL, 5)
0043         self.SetSizer(vs)
0044         self.SetAutoLayout(True)
0045         vs.Fit(self)
0046 
0047     def GetMyControls(self):
0048         """Build your own controls here, which will be appended to the main
0049         sizer.  Default to None.
0050         """
0051         return None
0052 
0053     def ok(self):
0054         # ready to move to the next page?  By default, yes
0055         return True
0056     def get(self, data):
0057         # return data to the main wizard, data is a dict
0058         pass
0059     def set(self, data):
0060         # pass current data to this page
0061         pass
0062 
0063 #-------------------------------------------------------------------------------
0064 class CommPortPage(MyPage):
0065     detail_fields=(
0066         # dict key, label, translator
0067         ('name', 'Name', None),
0068         ('description', 'Description', None),
0069         ('hardwareinstance', 'Hardware Info', None),
0070         ('driverprovider', 'Driver Provider', None),
0071         ('driverversion', 'Driver Version', None),
0072         ('driverdescription', 'Driver Description', None),
0073         ('class', 'Class', None),
0074         ('active', 'Active', None),
0075         )
0076 
0077     def __init__(self, parent):
0078         super(CommPortPage, self).__init__(parent, 'Communication Port Setting',
0079                                            'Set the serial port through which BitPim communicates with the phone.')
0080         self._ports=[{'name': 'auto', 'description': 'BitPim will try to detect the correct port automatically when accessing your phone'}]+\
0081                      [x for x in comscan.comscan()+usbscan.usbscan() \
0082                       if x.get('available', False)]
0083         self._populate()
0084         self._set_max_size()
0085 
0086     def GetMyControls(self):
0087         hs=wx.BoxSizer(wx.HORIZONTAL)
0088         _sbs1=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Available Ports'),
0089                                 wx.VERTICAL)
0090         self._ports_lb=wx.ListBox(self, -1,
0091                                   style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB)
0092         wx.EVT_LISTBOX(self, self._ports_lb.GetId(),
0093                        self.OnPortSelected)
0094         _sbs1.Add(self._ports_lb, 1, wx.EXPAND|wx.ALL, 5)
0095         hs.Add(_sbs1, 0, wx.EXPAND|wx.ALL, 5)
0096         _sbs2=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Port Detail'),
0097                                 wx.VERTICAL)
0098         fgs=wx.FlexGridSizer(0, 2, 5, 5)
0099         fgs.AddGrowableCol(1)
0100         self._w=[]
0101         for e in CommPortPage.detail_fields:
0102             fgs.Add(wx.StaticText(self, -1, e[1]), 0, wx.EXPAND|wx.ALL, 0)
0103             w=wx.StaticText(self, -1, 100*' ')
0104             fgs.Add(w, 0, wx.EXPAND|wx.LEFT, 5)
0105             self._w.append(w)
0106         _sbs2.Add(fgs, 1, wx.EXPAND|wx.ALL, 5)
0107         self._port_details_bs=_sbs2
0108         hs.Add(_sbs2, 0, wx.EXPAND|wx.ALL, 5)
0109         return hs
0110 
0111     def ok(self):
0112         return self._ports_lb.GetSelection()!=wx.NOT_FOUND
0113     def get(self, data):
0114         data['com']=self._ports_lb.GetStringSelection()
0115 
0116     def _set_max_size(self):
0117         #go through all the ports and remember the biggest size
0118         _max_size=wx.Size(0,0)
0119         for e in self._ports:
0120             self._populate_each(e)
0121             _ms=self._port_details_bs.GetMinSize()
0122             _max_size[0]=max(_max_size[0], _ms[0])
0123             _max_size[1]=max(_max_size[1], _ms[1])
0124         self._port_details_bs.SetMinSize(_max_size)
0125         for w in self._w:
0126             w.SetLabel('')
0127 
0128     def Clear(self):
0129         self._ports_lb.Clear()
0130         for w in self._w:
0131             w.SetLabel('')
0132 
0133     def _populate(self):
0134         # populate the list box with available ports
0135         self.Clear()
0136         for e in self._ports:
0137             self._ports_lb.Append(e['name'])
0138     def _populate_each(self, data):
0139         for i,e in enumerate(CommPortPage.detail_fields):
0140             v=data.get(e[0], '')
0141             self._w[i].SetLabel(str(v))
0142         self._port_details_bs.Layout()
0143         
0144     def OnPortSelected(self, evt):
0145         self._populate_each(self._ports[evt.GetInt()])
0146         
0147 #-------------------------------------------------------------------------------
0148 class PhoneModelPage(MyPage):
0149     def __init__(self, parent):
0150         self._setbyme=False
0151         super(PhoneModelPage, self).__init__(parent, 'Phone Model Setting',
0152                                         'Set your phone model.')
0153         self._populate()
0154 
0155     def GetMyControls(self):
0156         hs=wx.BoxSizer(wx.HORIZONTAL)
0157         _sbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Carriers'),
0158                               wx.VERTICAL)
0159         self._carriers_lb=wx.ListBox(self, -1,
0160                                      style=wx.LB_MULTIPLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB)
0161         wx.EVT_LISTBOX(self, self._carriers_lb.GetId(), self.OnCarriersLB)
0162         _sbs.Add(self._carriers_lb, 1, wx.EXPAND|wx.ALL, 5)
0163         hs.Add(_sbs, 1, wx.EXPAND|wx.ALL, 5)
0164         _sbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Manufacturers'),
0165                                wx.VERTICAL)
0166         self._manuf_lb=wx.ListBox(self, -1,
0167                                   style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB)
0168         wx.EVT_LISTBOX(self, self._manuf_lb.GetId(), self.OnCarriersLB)
0169         _sbs.Add(self._manuf_lb, 1, wx.EXPAND|wx.ALL, 5)
0170         hs.Add(_sbs, 1, wx.EXPAND|wx.ALL, 5)
0171         _sbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Models'),
0172                                wx.VERTICAL)
0173         self._models_lb=wx.ListBox(self, -1,
0174                                    style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB)
0175         wx.EVT_LISTBOX(self, self._models_lb.GetId(), self.OnModelsLB)
0176         _sbs.Add(self._models_lb, 1, wx.EXPAND|wx.ALL, 5)
0177         self.help_btn=wx.Button(self, wx.ID_HELP)
0178         _sbs.Add(self.help_btn, 0, wx.ALL, 5)
0179         wx.EVT_BUTTON(self, self.help_btn.GetId(), self.OnHelp)
0180         self.help_btn.Enable(False)
0181         hs.Add(_sbs, 1, wx.EXPAND|wx.ALL, 5)
0182         return hs
0183 
0184     def _populate_models(self, carriers=None, brand=None):
0185         # populate the list of phones based on the carrier and/or brand
0186         self._models_lb.Clear()
0187         self.help_btn.Enable(False)
0188         _l=phones.phoneslist(brand, None)
0189         if carriers:
0190             for _c in carriers:
0191                 _l=[x for x in phones.phoneslist(brand, _c) if x in _l]
0192         for e in _l:
0193             self._models_lb.Append(e)
0194 
0195     def _populate_carriers(self, selection=None):
0196         self._carriers_lb.Clear()
0197         for e in ['All']+phones.phonecarriers:
0198             self._carriers_lb.Append(e)
0199         if selection is not None:
0200             self._carriers_lb.SetSelection(0)
0201 
0202     def _populate(self):
0203         self._manuf_lb.Clear()
0204         for e in ['All']+phones.phonemanufacturers:
0205             self._manuf_lb.Append(e)
0206         self._manuf_lb.SetSelection(0)
0207         self._populate_carriers(0)
0208         self._populate_models()
0209 
0210     def ok(self):
0211         return self._models_lb.GetSelection()!=wx.NOT_FOUND
0212     def get(self, data):
0213         data['phone']=self._models_lb.GetStringSelection()
0214 
0215     def OnCarriersLB(self, evt):
0216         if self._setbyme:
0217             return
0218         _s=self._carriers_lb.GetSelections()
0219         if _s==wx.NOT_FOUND:
0220             return
0221         self._setbyme=True
0222         if 0 in _s:
0223             _carriers=None
0224         else:
0225             _carriers=[self._carriers_lb.GetString(x) for x in _s]
0226         _s=self._manuf_lb.GetStringSelection()
0227         if not _s or _s=='All':
0228             _brand=None
0229         else:
0230             _brand=_s
0231         self._populate_models(_carriers, _brand)
0232         self._setbyme=False
0233 
0234     def OnModelsLB(self, evt):
0235         if self._setbyme:
0236             return
0237         self._setbyme=True
0238         _model=evt.GetString()
0239         self._manuf_lb.SetStringSelection(phones.manufacturer(_model))
0240         self._populate_carriers()
0241         for s in phones.carriers(_model):
0242             self._carriers_lb.SetStringSelection(s)
0243         self.help_btn.Enable(phones.helpid(_model) is not None)
0244         self._setbyme=False
0245 
0246     def OnHelp(self, _):
0247         model=self._models_lb.GetStringSelection()
0248         if not model:
0249             return
0250         helpid=phones.helpid(model)
0251         if helpid:
0252             wx.GetApp().displayhelpid(helpid)
0253 
0254 #-------------------------------------------------------------------------------
0255 class SummaryPage(MyPage):
0256     def __init__(self, parent):
0257         super(SummaryPage, self).__init__(parent, 'Summary')
0258 
0259     def GetMyControls(self):
0260         vs=wx.BoxSizer(wx.VERTICAL)
0261         _sbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'User Selection'),
0262                                wx.VERTICAL)
0263         _fgs=wx.FlexGridSizer(0, 2, 5, 5)
0264         _fgs.Add(wx.StaticText(self, -1, 'Phone Model:'),
0265                  0, wx.EXPAND|wx.ALL, 0)
0266         self._model=wx.StaticText(self, -1, '')
0267         _fgs.Add(self._model, 0, wx.EXPAND|wx.ALL, 0)
0268         _fgs.Add(wx.StaticText(self, -1, 'Port:'),
0269                  0, wx.EXPAND|wx.ALL, 0)
0270         self._port=wx.StaticText(self, -1, '')
0271         _fgs.Add(self._port, 0, wx.EXPAND|wx.ALL, 0)
0272         _fgs.Add(wx.StaticText(self, -1, 'Detection Status:'),
0273                  0, wx.EXPAND|wx.ALL, 0)
0274         self._status=wx.StaticText(self, -1, '')
0275         _fgs.Add(self._status, 0, wx.EXPAND|wx.ALL, 0)
0276 
0277         _sbs.Add(_fgs, 1, wx.EXPAND, 0)
0278         vs.Add(_sbs, 0, wx.EXPAND|wx.ALL, 5)
0279         self._det_btn=wx.Button(self, -1, 'Detect Phone')
0280         vs.Add(self._det_btn, 0, wx.ALL, 5)
0281         wx.EVT_BUTTON(self, self._det_btn.GetId(), self.OnDetect)
0282         return vs
0283 
0284     def set(self, data):
0285         self._status.SetLabel('')
0286         self._model_name=data.get('phone', '')
0287         self._model.SetLabel(self._model_name)
0288         self._com_port=data.get('com', '')
0289         self._port.SetLabel(self._com_port)
0290         _module=common.importas(phones.module(self._model_name))
0291         self._det_btn.Enable(bool(self._model_name and \
0292                                   (hasattr(_module.Phone,
0293                                            'detectphone') or \
0294                                    hasattr(_module.Profile,
0295                                            'phone_model'))))
0296 
0297     def OnDetect(self, _):
0298         if self._com_port=='auto':
0299             _port_name=None
0300         else:
0301             _port_name=str(self._com_port)
0302         _res=phone_detect.DetectPhone().detect(using_port=_port_name,
0303                                                using_model=self._model_name)
0304         _status='FAILED'
0305         if _res and _res.get('phone_name', '')==self._model_name:
0306             _status='PASSED'
0307         self._status.SetLabel(_status)
0308 
0309 #-------------------------------------------------------------------------------
0310 class SetPhoneWizard(wiz.Wizard):
0311     def __init__(self, parent):
0312         super(SetPhoneWizard, self).__init__(parent, -1,
0313                                              'Phone Setting Wizard')
0314         self._data={}
0315         commport_page=CommPortPage(self)
0316         phonemodel_page=PhoneModelPage(self)
0317         summary_page=SummaryPage(self)
0318         wiz.WizardPageSimple_Chain(phonemodel_page, commport_page)
0319         wiz.WizardPageSimple_Chain(commport_page, summary_page)
0320         self.first_page=phonemodel_page
0321         self.GetPageAreaSizer().Add(phonemodel_page, 1, wx.EXPAND|wx.ALL, 5)
0322         wiz.EVT_WIZARD_PAGE_CHANGING(self, self.GetId(), self.OnPageChanging)
0323         wiz.EVT_WIZARD_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged)
0324 
0325     def RunWizard(self, firstPage=None):
0326         return super(SetPhoneWizard, self).RunWizard(firstPage or self.first_page)
0327 
0328     def OnPageChanging(self, evt):
0329         pg=evt.GetPage()
0330         if not evt.GetDirection() or pg.ok():
0331             pg.get(self._data)
0332         else:
0333             evt.Veto()
0334 
0335     def OnPageChanged(self, evt):
0336         evt.GetPage().set(self._data)
0337 
0338     def get(self):
0339         return self._data
0340 
0341 #-------------------------------------------------------------------------------
0342 # Testing
0343 if __name__=="__main__":
0344     app=wx.PySimpleApp()
0345     f=wx.Frame(None, title='setphone_wizard')
0346     w=SetPhoneWizard(f)
0347     print w.RunWizard()
0348     print w._data
0349     w.Destroy()
0350 

Generated by PyXR 0.9.4