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

Source Code for Module call_history_export

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2004 Joe Pham <djpham@bitpim.org> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: call_history_export.py 4377 2007-08-27 04:58:33Z djpham $ 
  9   
 10  "Deals with Call History import/export stuff" 
 11  # System Module 
 12  from __future__ import with_statement 
 13  # wxPython modules 
 14  import wx 
 15   
 16  # BitPim Modules 
 17  import bptime 
 18  import guihelper 
 19  import phonenumber 
 20   
 21  #------------------------------------------------------------------------------ 
22 -class ExportCallHistoryDialog(wx.Dialog):
23 - def __init__(self, parent, title):
24 super(ExportCallHistoryDialog, self).__init__(parent, -1, title) 25 self._chwidget=parent.GetActiveCallHistoryWidget() 26 self._sel_data=self._chwidget.get_selected_data() 27 self._data=self._chwidget.get_data() 28 # make the ui 29 vbs=wx.BoxSizer(wx.VERTICAL) 30 hbs=wx.BoxSizer(wx.HORIZONTAL) 31 hbs.Add(wx.StaticText(self, -1, "File"), 0, wx.ALL|wx.ALIGN_CENTRE, 5) 32 self.filenamectrl=wx.TextCtrl(self, -1, "callhistory_export.csv") 33 hbs.Add(self.filenamectrl, 1, wx.ALL|wx.EXPAND, 5) 34 self.browsectrl=wx.Button(self, wx.NewId(), "Browse...") 35 hbs.Add(self.browsectrl, 0, wx.ALL|wx.EXPAND, 5) 36 vbs.Add(hbs, 0, wx.EXPAND|wx.ALL, 5) 37 # selection GUI 38 vbs.Add(self.GetSelectionGui(self), 5, wx.EXPAND|wx.ALL, 5) 39 # the buttons 40 vbs.Add(wx.StaticLine(self, -1, style=wx.LI_HORIZONTAL), 0, wx.EXPAND|wx.ALL,5) 41 vbs.Add(self.CreateButtonSizer(wx.OK|wx.CANCEL), 0, wx.ALIGN_CENTER|wx.ALL, 5) 42 # event handlers 43 wx.EVT_BUTTON(self, self.browsectrl.GetId(), self.OnBrowse) 44 wx.EVT_BUTTON(self, wx.ID_OK, self.OnOk) 45 # all done 46 self.SetSizer(vbs) 47 self.SetAutoLayout(True) 48 vbs.Fit(self)
49
50 - def GetSelectionGui(self, parent):
51 hbs=wx.BoxSizer(wx.HORIZONTAL) 52 rbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, "Call History"), wx.VERTICAL) 53 lsel=len(self._sel_data) 54 lall=len(self._data) 55 self.rows_selected=wx.RadioButton(self, wx.NewId(), "Selected (%d)" % (lsel,), style=wx.RB_GROUP) 56 self.rows_all=wx.RadioButton(self, wx.NewId(), "All (%d)" % (lall,)) 57 if lsel==0: 58 self.rows_selected.Enable(False) 59 self.rows_selected.SetValue(0) 60 self.rows_all.SetValue(1) 61 rbs.Add(self.rows_selected, 0, wx.EXPAND|wx.ALL, 2) 62 hbs.Add(rbs, 3, wx.EXPAND|wx.ALL, 5) 63 rbs.Add(self.rows_all, 0, wx.EXPAND|wx.ALL, 2) 64 return hbs
65
66 - def OnBrowse(self, _):
67 with guihelper.WXDialogWrapper(wx.FileDialog(self, defaultFile=self.filenamectrl.GetValue(), 68 wildcard="CSV files (*.csv)|*.csv", style=wx.SAVE|wx.CHANGE_DIR), 69 True) as (dlg, retcode): 70 if retcode==wx.ID_OK: 71 self.filenamectrl.SetValue(dlg.GetPath())
72
73 - def OnOk(self, _):
74 # do export 75 filename=self.filenamectrl.GetValue() 76 try: 77 _fp=file(filename, 'wt') 78 except: 79 _fp=None 80 if _fp is None: 81 guihelper.MessageDialog(self, 'Failed to open file ['+filename+']', 82 'Export Error') 83 self.EndModal(wx.ID_OK) 84 if self.rows_all.GetValue(): 85 _data=self._data 86 else: 87 _data=self._sel_data 88 self._export_csv(_fp, _data) 89 _fp.close() 90 self.EndModal(wx.ID_OK)
91
92 - def _datetime_str(self, v):
93 _dt=bptime.BPTime(v) 94 return _dt.date_str()+' '+_dt.time_str()
95 - def _phonenumber_str(self, v):
96 return phonenumber.format(v)
97 - def _hms(self, v):
98 if v is None or not isinstance(v, int): 99 return '' 100 else: 101 return '%02d:%02d:%02d'%(v/3600, v/60, v%60)
102 103 _csv_template=( 104 ('Date', 'datetime', _datetime_str), 105 ('Number', 'number', _phonenumber_str), 106 ('Name', 'name', None), 107 ('Duration', 'duration', _hms), 108 ('Type', 'folder', None))
109 - def _export_csv(self, fp, ch):
110 # export Call History data to CSV format 111 # print out the header 112 fp.write(','.join(['"'+e[0]+'"' for e in self._csv_template])+'\n') 113 # and the entries 114 _keys=ch.keys() 115 _keys.sort() 116 for k in _keys: 117 try: 118 e=ch[k] 119 _l=[] 120 for _c in self._csv_template: 121 if _c[2] is None: 122 _s=str(getattr(e, _c[1], '')) 123 else: 124 _s=_c[2](self, getattr(e, _c[1], None)) 125 _l.append('"'+_s.replace('"', '')+'"') 126 fp.write(','.join(_l)+'\n') 127 except: 128 if __debug__: 129 raise
130