Module newdb_wiz
[hide private]
[frames] | no frames]

Source Code for Module newdb_wiz

  1  #!/usr/bin/env python 
  2   
  3  ### BITPIM 
  4  ### 
  5  ### Copyright (C) 2007 Joe Pham <djpham@bitpim.org> 
  6  ### 
  7  ### This program is free software; you can redistribute it and/or modify 
  8  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  9  ### 
 10  ### $Id: newdb_wiz.py 4378 2007-08-27 17:47:50Z djpham $ 
 11   
 12  """ 
 13  Wizard to create a new BitPim storage area. 
 14  """ 
 15   
 16  # system module 
 17  from __future__ import with_statement 
 18  import os 
 19  import os.path 
 20   
 21  # wx modules 
 22  import wx 
 23  import wx.wizard as wiz 
 24  from wx.lib.expando import ExpandoTextCtrl 
 25   
 26  # BitPim modules 
 27  import bp_config 
 28  import guihelper 
 29  import setphone_wizard 
 30   
 31  if guihelper.IsMSWindows(): 
 32      from win32com import client 
 33   
 34  parentpage=setphone_wizard.MyPage 
 35  #------------------------------------------------------------------------------- 
36 -class NamePage(parentpage):
37 - def __init__(self, parent):
38 super(NamePage, self).__init__(parent, 39 'Select BitPim Storage Name')
40 - def GetMyControls(self):
41 vbs=wx.BoxSizer(wx.VERTICAL) 42 vbs.Add(wx.StaticText(self, -1, 'Storage Name:'), 0, 43 wx.EXPAND|wx.ALL, 5) 44 self.name=wx.TextCtrl(self, -1, '') 45 vbs.Add(self.name, 0, wx.EXPAND|wx.ALL, 5) 46 return vbs
47
48 - def ok(self):
49 return bool(self.name.GetValue())
50 - def get(self, data):
51 data['name']=self.name.GetValue()
52 - def set(self, data):
53 self.name.SetValue(data.get('name', ''))
54 55 #-------------------------------------------------------------------------------
56 -class PathPage(parentpage):
57 - def __init__(self, parent):
58 super(PathPage, self).__init__(parent, 59 'Select New Storage Dir') 60 if guihelper.IsMSWindows(): 61 shell=client.Dispatch("WScript.Shell") 62 self.defaultdir=os.path.join(shell.SpecialFolders("MyDocuments"), 63 'Phones') 64 else: 65 self.defaultdir=os.path.expanduser('~/Phones')
66
67 - def GetMyControls(self):
68 vbs=wx.BoxSizer(wx.VERTICAL) 69 vbs.Add(wx.StaticText(self, -1, 'Storage Dir:'), 70 0, wx.EXPAND|wx.ALL, 5) 71 self.path=ExpandoTextCtrl(self, -1, '', style=wx.TE_READONLY) 72 self.path.SetBackgroundColour(self.GetBackgroundColour()) 73 vbs.Add(self.path, 0, wx.EXPAND|wx.ALL, 5) 74 btn=wx.Button(self, -1, 'Browse') 75 wx.EVT_BUTTON(self, btn.GetId(), self.OnBrowse) 76 vbs.Add(btn, 0, wx.ALL, 5) 77 return vbs
78
79 - def ok(self):
80 return bool(self.path.GetValue())
81 - def get(self, data):
82 data['path']=self.path.GetValue()
83 - def set(self, data):
84 path=data.get('path', '') 85 if not path: 86 path=os.path.join(self.defaultdir, data.get('name', '')) 87 self.path.SetValue(path)
88
89 - def OnBrowse(self, _):
90 with guihelper.WXDialogWrapper(wx.DirDialog(self, defaultPath=self.path.GetLabel(), 91 style=wx.DD_NEW_DIR_BUTTON), 92 True) as (dlg, retcode): 93 if retcode==wx.ID_OK: 94 self.path.SetValue(dlg.GetPath())
95 96 #-------------------------------------------------------------------------------
97 -class OptionsPage(parentpage):
98 - def __init__(self, parent):
99 super(OptionsPage, self).__init__(parent, 100 'Select Options')
101 - def GetMyControls(self):
102 vbs=wx.BoxSizer(wx.VERTICAL) 103 self.setting=wx.RadioBox(self, -1, 'Initial Config Settings:', 104 choices=['Use Default Settings', 105 'Use Current Settings'], 106 style=wx.RA_SPECIFY_ROWS) 107 vbs.Add(self.setting, 0, wx.EXPAND|wx.ALL, 5) 108 if guihelper.IsMSWindows(): 109 sbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Shortcut Options:'), 110 wx.VERTICAL) 111 self.desktop=wx.CheckBox(self, -1, 'Create a shortcut on your Desktop') 112 sbs.Add(self.desktop, 0, wx.EXPAND|wx.ALL, 5) 113 self.startmenu=wx.CheckBox(self, -1, 114 'Create a shortcut in your Start Menu') 115 sbs.Add(self.startmenu, 0, wx.EXPAND|wx.ALL, 5) 116 vbs.Add(sbs, 0, wx.EXPAND|wx.ALL, 5) 117 return vbs
118
119 - def get(self, data):
120 data['currentsettings']=self.setting.GetSelection()==1 121 if guihelper.IsMSWindows(): 122 data['desktop']=self.desktop.GetValue() 123 data['startmenu']=self.startmenu.GetValue()
124 - def set(self, data):
125 if data.get('currentsettings', False): 126 self.setting.SetSelection(1) 127 else: 128 self.setting.SetSelection(0) 129 if guihelper.IsMSWindows(): 130 self.desktop.SetValue(data.get('desktop', False)) 131 self.startmenu.SetValue(data.get('startmenu', False))
132 133 #-------------------------------------------------------------------------------
134 -class SummaryPage(parentpage):
135 - def __init__(self, parent):
136 super(SummaryPage, self).__init__(parent, 'Selection Summary')
137 - def GetMyControls(self):
138 vbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Selection Summary:'), 139 wx.VERTICAL) 140 self.summary=ExpandoTextCtrl(self, -1, '') 141 self.summary.SetBackgroundColour(self.GetBackgroundColour()) 142 vbs.Add(self.summary, 0, wx.EXPAND|wx.ALL, 5) 143 self._box=vbs 144 return vbs
145 - def set(self, data):
146 text=['Name:\t%s'%data.get('name', '')] 147 text.append('Dir:\t%s'%data.get('path', '')) 148 if data.get('currentsettings', False): 149 text.append('Use current BitPim settings.') 150 else: 151 text.append('Use default BitPim settings.') 152 if guihelper.IsMSWindows(): 153 if data.get('desktop', False): 154 text.append('Create a shortcut on your Desktop.') 155 if data.get('startmenu', False): 156 text.append('Create a shortcut in your Start Menu.') 157 self.summary.SetValue('\n\n'.join(text))
158 159 #-------------------------------------------------------------------------------
160 -class NewDBWizard(wiz.Wizard):
161 - def __init__(self, parent):
162 super(NewDBWizard, self).__init__(parent, -1, 163 'New BitPim Storage Wizard') 164 self.data={} 165 namepage=NamePage(self) 166 pathpage=PathPage(self) 167 optionspage=OptionsPage(self) 168 summarypage=SummaryPage(self) 169 wiz.WizardPageSimple_Chain(namepage, pathpage) 170 wiz.WizardPageSimple_Chain(pathpage, optionspage) 171 wiz.WizardPageSimple_Chain(optionspage, summarypage) 172 self.firstpage=namepage 173 self.GetPageAreaSizer().Add(namepage, 1, wx.EXPAND|wx.ALL, 5) 174 wiz.EVT_WIZARD_PAGE_CHANGING(self, self.GetId(), self.OnPageChanging) 175 wiz.EVT_WIZARD_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged)
176
177 - def RunWizard(self, firstPage=None):
178 return super(NewDBWizard, self).RunWizard(firstPage or self.firstpage)
179
180 - def OnPageChanging(self, evt):
181 pg=evt.GetPage() 182 if not evt.GetDirection() or pg.ok(): 183 pg.get(self.data) 184 else: 185 evt.Veto()
186
187 - def OnPageChanged(self, evt):
188 evt.GetPage().set(self.data)
189
190 - def get(self):
191 return self.data
192 193 #-------------------------------------------------------------------------------
194 -def create_desktop_shortcut(name, filename):
195 shell=client.Dispatch("WScript.Shell") 196 desktopphones=os.path.join(shell.SpecialFolders("Desktop"), 'Phones') 197 if not os.path.isdir(desktopphones): 198 os.makedirs(desktopphones) 199 target=os.path.join(desktopphones, name+'.lnk') 200 try: 201 os.remove(target) 202 except: 203 pass 204 link=shell.CreateShortcut(target) 205 link.TargetPath=filename 206 link.Save()
207 208 #-------------------------------------------------------------------------------
209 -def create_startmenu_shortcut(name, filename):
210 shell=client.Dispatch("WScript.Shell") 211 startmenu=os.path.join(shell.SpecialFolders("StartMenu"), 'Programs') 212 startmenuphones=os.path.join(startmenu, 'Phones') 213 if not os.path.isdir(startmenuphones): 214 os.makedirs(startmenuphones) 215 target=os.path.join(startmenuphones, name+'.lnk') 216 try: 217 os.remove(target) 218 except: 219 pass 220 link=shell.CreateShortcut(target) 221 link.TargetPath=filename 222 link.Save()
223 224 #-------------------------------------------------------------------------------
225 -def create_new_db(parent, config=None):
226 # Create a new BitPim Storage area 227 with guihelper.WXDialogWrapper(NewDBWizard(parent)) as wz: 228 if wz.RunWizard(): 229 data=wz.get() 230 name=data.get('name', '') 231 # Dir should aleady exist, but check anyway 232 path=data.get('path', '') 233 if not os.path.isdir(path): 234 os.makedirs(path) 235 # create a config file 236 filename=os.path.join(path, '.bitpim') 237 if data.get('currentsettings', False) and config: 238 config.write(file(filename, 'wt')) 239 conf=bp_config.Config(filename) 240 conf.Write('name', name) 241 # and optionally create shortcuts (Windows only) 242 if guihelper.IsMSWindows(): 243 if data.get('desktop', False): 244 create_desktop_shortcut(name, filename) 245 if data.get('startmenu', False): 246 create_startmenu_shortcut(name, filename)
247 248 #------------------------------------------------------------------------------- 249 # Testing 250 if __name__=="__main__": 251 app=wx.PySimpleApp() 252 f=wx.Frame(None, title='newdb_wizard') 253 create_new_db(f) 254