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

Source Code for Module imp_cal_wizard

  1  #!/usr/bin/env python 
  2   
  3  ### BITPIM 
  4  ### 
  5  ### Copyright (C) 2006 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: imp_cal_wizard.py 4377 2007-08-27 04:58:33Z djpham $ 
 11   
 12  """ Handle Import Calendar wizard 
 13  """ 
 14   
 15  # System 
 16  from __future__ import with_statement 
 17  # wx 
 18  import wx 
 19  import wx.wizard as wiz 
 20  import  wx.lib.scrolledpanel as scrolled 
 21  from wx.lib.expando import ExpandoTextCtrl 
 22   
 23  # BitPim 
 24  import common_calendar 
 25  import guihelper 
 26  import importexport 
 27  import setphone_wizard 
 28   
 29  # modules constants 
 30  IMP_OPTION_REPLACEALL=0 
 31  IMP_OPTION_ADD=1 
 32  IMP_OPTION_MERGE=2 
33 34 #------------------------------------------------------------------------------- 35 -class ImportTypePage(setphone_wizard.MyPage):
36 - def __init__(self, parent):
37 self._data=importexport.GetCalendarImports() 38 super(ImportTypePage, self).__init__(parent, 39 'Select Calendar Import Type') 40 self._populate()
41
42 - def GetMyControls(self):
43 vbs=wx.BoxSizer(wx.VERTICAL) 44 vbs.Add(wx.StaticText(self, -1, 'Import Calendar Type:'), 0, 45 wx.EXPAND|wx.ALL, 5) 46 self._type_lb=wx.ListBox(self, -1, 47 style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB) 48 vbs.Add(self._type_lb, 0, wx.EXPAND|wx.ALL, 5) 49 return vbs
50
51 - def _populate(self):
52 # populate the listbox with data 53 self._type_lb.Clear() 54 for _l in self._data: 55 self._type_lb.Append(_l['type'], _l)
56
57 - def ok(self):
58 # ready to move to the next page? By default, yes 59 return self._type_lb.GetSelection()!=wx.NOT_FOUND
60
61 - def get(self, data):
62 # return data to the main wizard, data is a dict 63 _idx=self._type_lb.GetSelection() 64 _type=self._type_lb.GetString(_idx) 65 _new_type=data.get('type', None)!=_type 66 if _new_type or not data.has_key('data'): 67 data['type']=_type 68 _info=self._type_lb.GetClientData(_idx) 69 data['data']=_info['data'] 70 data['data_obj']=_info['data']() 71 data['source_class']=_info['source'] 72 data['source_obj']=_info['source']() 73 if _new_type: 74 # new type selected 75 data['type']=_type 76 if data.has_key('source_id'): 77 del data['source_id']
78
79 - def set(self, data):
80 # pass current data to this page 81 if data.get('type', None): 82 self._type_lb.SetStringSelection(data['type'])
83
84 #------------------------------------------------------------------------------- 85 -class ImportSourcePage(setphone_wizard.MyPage):
86 - def __init__(self, parent):
87 self._source=None 88 super(ImportSourcePage, self).__init__(parent, 89 'Select Import Source')
90 - def GetMyControls(self):
91 vbs=wx.BoxSizer(wx.VERTICAL) 92 vbs.Add(wx.StaticText(self, -1, 'Source of data:'), 0, 93 wx.ALL|wx.EXPAND, 5) 94 self._source_lbl=ExpandoTextCtrl(self, -1, '', style=wx.TE_READONLY) 95 self._source_lbl.SetBackgroundColour(self.GetBackgroundColour()) 96 vbs.Add(self._source_lbl, 0, wx.ALL|wx.EXPAND, 5) 97 98 _btn=wx.Button(self, -1, 'Browse') 99 wx.EVT_BUTTON(self, _btn.GetId(), self._OnBrowse) 100 vbs.Add(_btn, 0, wx.ALL, 5) 101 return vbs
102
103 - def setlabel(self):
104 self._source_lbl.SetValue(self._source.name())
105 106 @guihelper.BusyWrapper
107 - def _OnBrowse(self, _=None):
108 if not self._source: 109 return 110 self._source.browse(self) 111 self.setlabel()
112
113 - def ok(self):
114 return self._source and self._source.get()
115 - def get(self, data):
116 data['source_obj']=self._source 117 data['source_id']=self._source.id 118 data['imported']=False
119 - def set(self, data):
120 self._source=data['source_obj'] 121 if self._source: 122 if data.has_key('source_id'): 123 self._source.id=data['source_id'] 124 self.setlabel()
125 - def GetActiveDatabase(self):
126 return self.GetParent().GetActiveDatabase()
127
128 #------------------------------------------------------------------------------- 129 -class ImportDataAll(setphone_wizard.MyPage):
130 - def __init__(self, parent):
131 self._type=None 132 self._source=None 133 super(ImportDataAll, self).__init__(parent, 'Import Data Preview')
134
135 - def GetMyControls(self):
136 vbs=wx.BoxSizer(wx.VERTICAL) 137 self._data_lb=wx.ListBox(self, -1, 138 style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB) 139 vbs.Add(self._data_lb, 1, wx.EXPAND|wx.ALL, 5) 140 _btn=wx.Button(self, -1, 'Data Filtering Parameters') 141 vbs.Add(_btn, 0, wx.ALL, 5) 142 wx.EVT_BUTTON(self, _btn.GetId(), self._OnFilter) 143 return vbs
144
145 - def _populate_lb(self):
146 self._data_lb.Clear() 147 for _key, _entry in self._type.get_display_data().items(): 148 self._data_lb.Append('%s - %s'%(common_calendar.bp_date_str(_entry, _entry['start']), 149 _entry['description']))
150 @guihelper.BusyWrapper
151 - def _populate(self):
152 self._data_lb.Clear() 153 if not self._type or not self._source: 154 # nothing to import 155 return 156 with guihelper.WXDialogWrapper(wx.ProgressDialog('Calendar Data Import', 157 'Importing data, please wait ...', 158 parent=self)) as dlg: 159 self._type.read(self._source.get(), dlg) 160 self._populate_lb()
161
162 - def _OnFilter(self, _):
163 cat_list=self._type.get_category_list() 164 with guihelper.WXDialogWrapper(common_calendar.FilterDialog(self, -1, 'Filtering Parameters', 165 cat_list), 166 True) as (dlg, retcode): 167 if retcode==wx.ID_OK: 168 self._type.set_filter(dlg.get()) 169 self._populate_lb()
170
171 - def set(self, data):
172 self._type=data.get('data_obj', None) 173 self._source=data.get('source_obj', None) 174 if not data.get('imported', False): 175 self._populate()
176 - def get(self, data):
177 data['imported']=True
178
179 #------------------------------------------------------------------------------- 180 -class ImportOptionPage(setphone_wizard.MyPage):
181 _choices=('Replace All', 'Add', 'Merge')
182 - def __init__(self, parent):
183 super(ImportOptionPage, self).__init__(parent, 184 'Import Options')
185 - def GetMyControls(self):
186 vbs=wx.BoxSizer(wx.VERTICAL) 187 self._option_rb=wx.RadioBox(self, -1, 'Import Options', 188 choices=self._choices, 189 style=wx.RA_SPECIFY_ROWS) 190 vbs.Add(self._option_rb, 0, wx.EXPAND|wx.ALL, 5) 191 return vbs
192
193 - def set(self, data):
194 self._option_rb.SetSelection(data.get('option', 0))
195 - def get(self, data):
196 data['option']=self._option_rb.GetSelection()
197
198 #------------------------------------------------------------------------------- 199 -class ImportCalendarWizard(wiz.Wizard):
200 ID_ADD=wx.NewId() 201 ID_MERGE=wx.NewId()
202 - def __init__(self, parent, id=-1, title='Calendar Import Wizard'):
203 super(ImportCalendarWizard, self).__init__(parent, id, title) 204 self._data={} 205 _import_type_page=ImportTypePage(self) 206 _import_source_page=ImportSourcePage(self) 207 _import_data_all=ImportDataAll(self) 208 _import_option=ImportOptionPage(self) 209 210 wiz.WizardPageSimple_Chain(_import_type_page, _import_source_page) 211 wiz.WizardPageSimple_Chain(_import_source_page, _import_data_all) 212 wiz.WizardPageSimple_Chain(_import_data_all, _import_option) 213 self.first_page=_import_type_page 214 self.GetPageAreaSizer().Add(self.first_page, 1, wx.EXPAND|wx.ALL, 5) 215 wiz.EVT_WIZARD_PAGE_CHANGING(self, self.GetId(), self.OnPageChanging) 216 wiz.EVT_WIZARD_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged)
217
218 - def RunWizard(self, firstPage=None):
219 return super(ImportCalendarWizard, self).RunWizard(firstPage or self.first_page)
220
221 - def OnPageChanging(self, evt):
222 pg=evt.GetPage() 223 if not evt.GetDirection() or pg.ok(): 224 pg.get(self._data) 225 else: 226 evt.Veto()
227
228 - def OnPageChanged(self, evt):
229 evt.GetPage().set(self._data)
230
231 - def get(self):
232 if self._data.get('data_obj', None): 233 return self._data['data_obj'].get() 234 return {}
235
236 - def GetActiveDatabase(self):
237 return self.GetParent().GetActiveDatabase()
238 - def get_categories(self):
239 if self._data.get('data_obj', None): 240 return self._data['data_obj'].get_category_list() 241 return []
242
243 - def ShowModal(self):
244 global IMP_OPTION_REPLACEALL 245 # run the wizard and return a code 246 if self.RunWizard(): 247 return [wx.ID_OK, self.ID_ADD, self.ID_MERGE][self._data.get('option', 248 IMP_OPTION_REPLACEALL)] 249 return wx.ID_CANCEL
250 251 #------------------------------------------------------------------------------- 252 # Testing 253 if __name__=="__main__": 254 app=wx.PySimpleApp() 255 f=wx.Frame(None, title='imp_cal_wizard') 256 w=ImportCalendarWizard(f) 257 print w.RunWizard() 258 print w.get() 259 w.Destroy() 260