PyXR

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



0001 #!/usr/bin/env python
0002 
0003 ### BITPIM
0004 ###
0005 ### Copyright (C) 2007 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: newdb_wiz.py 4378 2007-08-27 17:47:50Z djpham $
0011 
0012 """
0013 Wizard to create a new BitPim storage area.
0014 """
0015 
0016 # system module
0017 from __future__ import with_statement
0018 import os
0019 import os.path
0020 
0021 # wx modules
0022 import wx
0023 import wx.wizard as wiz
0024 from wx.lib.expando import ExpandoTextCtrl
0025 
0026 # BitPim modules
0027 import bp_config
0028 import guihelper
0029 import setphone_wizard
0030 
0031 if guihelper.IsMSWindows():
0032     from win32com import client
0033 
0034 parentpage=setphone_wizard.MyPage
0035 #-------------------------------------------------------------------------------
0036 class NamePage(parentpage):
0037     def __init__(self, parent):
0038         super(NamePage, self).__init__(parent,
0039                                        'Select BitPim Storage Name')
0040     def GetMyControls(self):
0041         vbs=wx.BoxSizer(wx.VERTICAL)
0042         vbs.Add(wx.StaticText(self, -1, 'Storage Name:'), 0,
0043                 wx.EXPAND|wx.ALL, 5)
0044         self.name=wx.TextCtrl(self, -1, '')
0045         vbs.Add(self.name, 0, wx.EXPAND|wx.ALL, 5)
0046         return vbs
0047 
0048     def ok(self):
0049         return bool(self.name.GetValue())
0050     def get(self, data):
0051         data['name']=self.name.GetValue()
0052     def set(self, data):
0053         self.name.SetValue(data.get('name', ''))
0054 
0055 #-------------------------------------------------------------------------------
0056 class PathPage(parentpage):
0057     def __init__(self, parent):
0058         super(PathPage, self).__init__(parent,
0059                                        'Select New Storage Dir')
0060         if guihelper.IsMSWindows():
0061             shell=client.Dispatch("WScript.Shell")
0062             self.defaultdir=os.path.join(shell.SpecialFolders("MyDocuments"),
0063                                          'Phones')
0064         else:
0065             self.defaultdir=os.path.expanduser('~/Phones')
0066 
0067     def GetMyControls(self):
0068         vbs=wx.BoxSizer(wx.VERTICAL)
0069         vbs.Add(wx.StaticText(self, -1, 'Storage Dir:'),
0070                 0, wx.EXPAND|wx.ALL, 5)
0071         self.path=ExpandoTextCtrl(self, -1, '', style=wx.TE_READONLY)
0072         self.path.SetBackgroundColour(self.GetBackgroundColour())
0073         vbs.Add(self.path, 0, wx.EXPAND|wx.ALL, 5)
0074         btn=wx.Button(self, -1, 'Browse')
0075         wx.EVT_BUTTON(self, btn.GetId(), self.OnBrowse)
0076         vbs.Add(btn, 0, wx.ALL, 5)
0077         return vbs
0078 
0079     def ok(self):
0080         return bool(self.path.GetValue())
0081     def get(self, data):
0082         data['path']=self.path.GetValue()
0083     def set(self, data):
0084         path=data.get('path', '')
0085         if not path:
0086             path=os.path.join(self.defaultdir, data.get('name', ''))
0087         self.path.SetValue(path)
0088 
0089     def OnBrowse(self, _):
0090         with guihelper.WXDialogWrapper(wx.DirDialog(self, defaultPath=self.path.GetLabel(),
0091                                                     style=wx.DD_NEW_DIR_BUTTON),
0092                                        True) as (dlg, retcode):
0093             if retcode==wx.ID_OK:
0094                 self.path.SetValue(dlg.GetPath())
0095 
0096 #-------------------------------------------------------------------------------
0097 class OptionsPage(parentpage):
0098     def __init__(self, parent):
0099         super(OptionsPage, self).__init__(parent,
0100                                           'Select Options')
0101     def GetMyControls(self):
0102         vbs=wx.BoxSizer(wx.VERTICAL)
0103         self.setting=wx.RadioBox(self, -1, 'Initial Config Settings:',
0104                                  choices=['Use Default Settings',
0105                                           'Use Current Settings'],
0106                                  style=wx.RA_SPECIFY_ROWS)
0107         vbs.Add(self.setting, 0, wx.EXPAND|wx.ALL, 5)
0108         if guihelper.IsMSWindows():
0109             sbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Shortcut Options:'),
0110                                   wx.VERTICAL)
0111             self.desktop=wx.CheckBox(self, -1, 'Create a shortcut on your Desktop')
0112             sbs.Add(self.desktop, 0, wx.EXPAND|wx.ALL, 5)
0113             self.startmenu=wx.CheckBox(self, -1,
0114                                        'Create a shortcut in your Start Menu')
0115             sbs.Add(self.startmenu, 0, wx.EXPAND|wx.ALL, 5)
0116             vbs.Add(sbs, 0, wx.EXPAND|wx.ALL, 5)
0117         return vbs
0118 
0119     def get(self, data):
0120         data['currentsettings']=self.setting.GetSelection()==1
0121         if guihelper.IsMSWindows():
0122             data['desktop']=self.desktop.GetValue()
0123             data['startmenu']=self.startmenu.GetValue()
0124     def set(self, data):
0125         if data.get('currentsettings', False):
0126             self.setting.SetSelection(1)
0127         else:
0128             self.setting.SetSelection(0)
0129         if guihelper.IsMSWindows():
0130             self.desktop.SetValue(data.get('desktop', False))
0131             self.startmenu.SetValue(data.get('startmenu', False))
0132 
0133 #-------------------------------------------------------------------------------
0134 class SummaryPage(parentpage):
0135     def __init__(self, parent):
0136         super(SummaryPage, self).__init__(parent, 'Selection Summary')
0137     def GetMyControls(self):
0138         vbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Selection Summary:'),
0139                               wx.VERTICAL)
0140         self.summary=ExpandoTextCtrl(self, -1, '')
0141         self.summary.SetBackgroundColour(self.GetBackgroundColour())
0142         vbs.Add(self.summary, 0, wx.EXPAND|wx.ALL, 5)
0143         self._box=vbs
0144         return vbs
0145     def set(self, data):
0146         text=['Name:\t%s'%data.get('name', '')]
0147         text.append('Dir:\t%s'%data.get('path', ''))
0148         if data.get('currentsettings', False):
0149             text.append('Use current BitPim settings.')
0150         else:
0151             text.append('Use default BitPim settings.')
0152         if guihelper.IsMSWindows():
0153             if data.get('desktop', False):
0154                 text.append('Create a shortcut on your Desktop.')
0155             if data.get('startmenu', False):
0156                 text.append('Create a shortcut in your Start Menu.')
0157         self.summary.SetValue('\n\n'.join(text))
0158 
0159 #-------------------------------------------------------------------------------
0160 class NewDBWizard(wiz.Wizard):
0161     def __init__(self, parent):
0162         super(NewDBWizard, self).__init__(parent, -1,
0163                                           'New BitPim Storage Wizard')
0164         self.data={}
0165         namepage=NamePage(self)
0166         pathpage=PathPage(self)
0167         optionspage=OptionsPage(self)
0168         summarypage=SummaryPage(self)
0169         wiz.WizardPageSimple_Chain(namepage, pathpage)
0170         wiz.WizardPageSimple_Chain(pathpage, optionspage)
0171         wiz.WizardPageSimple_Chain(optionspage, summarypage)
0172         self.firstpage=namepage
0173         self.GetPageAreaSizer().Add(namepage, 1, wx.EXPAND|wx.ALL, 5)
0174         wiz.EVT_WIZARD_PAGE_CHANGING(self, self.GetId(), self.OnPageChanging)
0175         wiz.EVT_WIZARD_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged)
0176 
0177     def RunWizard(self, firstPage=None):
0178         return super(NewDBWizard, self).RunWizard(firstPage or self.firstpage)
0179 
0180     def OnPageChanging(self, evt):
0181         pg=evt.GetPage()
0182         if not evt.GetDirection() or pg.ok():
0183             pg.get(self.data)
0184         else:
0185             evt.Veto()
0186 
0187     def OnPageChanged(self, evt):
0188         evt.GetPage().set(self.data)
0189 
0190     def get(self):
0191         return self.data
0192 
0193 #-------------------------------------------------------------------------------
0194 def create_desktop_shortcut(name, filename):
0195     shell=client.Dispatch("WScript.Shell")
0196     desktopphones=os.path.join(shell.SpecialFolders("Desktop"), 'Phones')
0197     if not os.path.isdir(desktopphones):
0198         os.makedirs(desktopphones)
0199     target=os.path.join(desktopphones, name+'.lnk')
0200     try:
0201         os.remove(target)
0202     except:
0203         pass
0204     link=shell.CreateShortcut(target)
0205     link.TargetPath=filename
0206     link.Save()
0207 
0208 #-------------------------------------------------------------------------------
0209 def create_startmenu_shortcut(name, filename):
0210     shell=client.Dispatch("WScript.Shell")
0211     startmenu=os.path.join(shell.SpecialFolders("StartMenu"), 'Programs')
0212     startmenuphones=os.path.join(startmenu, 'Phones')
0213     if not os.path.isdir(startmenuphones):
0214         os.makedirs(startmenuphones)
0215     target=os.path.join(startmenuphones, name+'.lnk')
0216     try:
0217         os.remove(target)
0218     except:
0219         pass
0220     link=shell.CreateShortcut(target)
0221     link.TargetPath=filename
0222     link.Save()
0223 
0224 #-------------------------------------------------------------------------------
0225 def create_new_db(parent, config=None):
0226     # Create a new BitPim Storage area
0227     with guihelper.WXDialogWrapper(NewDBWizard(parent)) as wz:
0228         if wz.RunWizard():
0229             data=wz.get()
0230             name=data.get('name', '')
0231             # Dir should aleady exist, but check anyway
0232             path=data.get('path', '')
0233             if not os.path.isdir(path):
0234                 os.makedirs(path)
0235             # create a config file
0236             filename=os.path.join(path, '.bitpim')
0237             if data.get('currentsettings', False) and config:
0238                 config.write(file(filename, 'wt'))
0239             conf=bp_config.Config(filename)
0240             conf.Write('name', name)
0241             # and optionally create shortcuts (Windows only)
0242             if guihelper.IsMSWindows():
0243                 if data.get('desktop', False):
0244                     create_desktop_shortcut(name, filename)
0245                 if data.get('startmenu', False):
0246                     create_startmenu_shortcut(name, filename)
0247 
0248 #-------------------------------------------------------------------------------
0249 # Testing
0250 if __name__=="__main__":
0251     app=wx.PySimpleApp()
0252     f=wx.Frame(None, title='newdb_wizard')
0253     create_new_db(f)
0254 

Generated by PyXR 0.9.4