Trees | Indices | Help |
|
---|
|
1 ### BITPIM 2 ### 3 ### Copyright (C) 2006 Simon Capper <scapper@sbcglobal.net> 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 9 10 # standard modules 11 from __future__ import with_statement 12 import contextlib 13 import os 14 import cStringIO 15 import copy 16 import sha 17 import time 18 import string 19 import zipfile 20 21 22 # wx modules 23 import wx 24 25 # BitPim modules 26 import database 27 import common 28 import guiwidgets 29 import guihelper 30 import pubsub 31 import widgets 32 import wallpaper 33 import ringers 34243 244 245 #------------------------------------------------------------------------------37 super(MediaWidget, self).__init__(parent, -1) 38 self._main_window=mainwindow 39 self.call_history_tree_nodes={} 40 self._parent=parent 41 # main box sizer 42 self.vbs=wx.BoxSizer(wx.VERTICAL) 43 # main stats display 44 self.vbs.Add(wx.StaticText(self, -1, 'Media summary'), 0, wx.ALIGN_LEFT|wx.ALL, 2) 45 # all done 46 self.SetSizer(self.vbs) 47 self.SetAutoLayout(True) 48 self.vbs.Fit(self) 49 self.ringernodes={} 50 self.wallpapernodes={} 51 self.widget_to_save=None 52 self.origin_to_save="" 53 self.SetBackgroundColour(wx.WHITE) 54 self.ringerwidget=ringers.RingerView(self._main_window, parent, self) 55 self.wallpaperwidget=wallpaper.WallpaperView(self._main_window, parent, self) 56 pubsub.subscribe(self.OnPhoneModelChanged, pubsub.PHONE_MODEL_CHANGED)57 # populate data 58 #self._populate() 5961 summary=[] 62 summary=self.GetWidgetSummary(self.ringerwidget, summary) 63 summary=self.GetWidgetSummary(self.wallpaperwidget, summary) 64 self.vbs.Clear(deleteWindows=True) 65 self.vbs.Add(wx.StaticText(self, -1, 'Media summary'), 0, wx.ALIGN_LEFT|wx.ALL, 2) 66 hbs=wx.BoxSizer(wx.HORIZONTAL) 67 name=wx.BoxSizer(wx.VERTICAL) 68 count=wx.BoxSizer(wx.VERTICAL) 69 size=wx.BoxSizer(wx.VERTICAL) 70 name.Add(wx.StaticText(self, -1, 'Origin'), 0, wx.ALIGN_LEFT|wx.ALL, 2) 71 count.Add(wx.StaticText(self, -1, 'Number Files'), 0, wx.ALIGN_LEFT|wx.ALL, 2) 72 size.Add(wx.StaticText(self, -1, 'Size'), 0, wx.ALIGN_LEFT|wx.ALL, 2) 73 total_files=0 74 total_size=0 75 for entry in summary: 76 name.Add(wx.StaticText(self, -1, entry[0]), 0, wx.ALIGN_LEFT|wx.ALL, 2) 77 count.Add(wx.StaticText(self, -1, str(entry[1])), 0, wx.ALIGN_LEFT|wx.ALL, 2) 78 size.Add(wx.StaticText(self, -1, self.GetNiceSizeString(entry[2])), 0, wx.ALIGN_LEFT|wx.ALL, 2) 79 total_files+=entry[1] 80 total_size+=entry[2] 81 hbs.Add(name, 0, wx.ALIGN_LEFT|wx.ALL, 2) 82 hbs.Add(count, 0, wx.ALIGN_LEFT|wx.ALL, 2) 83 hbs.Add(size, 0, wx.ALIGN_LEFT|wx.ALL, 2) 84 self.vbs.Add(hbs, 0, wx.ALIGN_LEFT|wx.ALL, 2) 85 self.vbs.Add(wx.StaticText(self, -1, "Total number of media files: %d" % total_files), 0, wx.ALIGN_LEFT|wx.ALL, 2) 86 self.vbs.Add(wx.StaticText(self, -1, "Total size of media files: %s" % self.GetNiceSizeString(total_size)), 0, wx.ALIGN_LEFT|wx.ALL, 2) 87 self.vbs.Layout()8890 orig=size 91 size=float(size) 92 if size < 1024: 93 return "%d bytes" % size 94 size=size/1024 95 if size < 1024: 96 return "%.2f KB (%d bytes)" % (size, orig) 97 size=size/1024 98 return "%.2f MB (%d bytes)" % (size, orig)99101 for k,e in widget.sections: 102 num_files=0 103 total_size=0 104 for item in e: 105 total_size+=item.size 106 num_files+=1 107 res.append((k.label, num_files, total_size)) 108 return res109111 result=[] 112 result.append((widgets.BitPimWidget.MENU_NORMAL, guihelper.ID_EXPORT_MEDIA_TO_DIR, "Export to Folder ..." , "Export the media to a folder on your hard drive")) 113 result.append((widgets.BitPimWidget.MENU_NORMAL, guihelper.ID_EXPORT_MEDIA_TO_ZIP, "Export to Zip File ..." , "Export the media to a zip file")) 114 return result115 118 121123 self.AddMediaNode("ringers", self.ringerwidget, self._tree.ringers) 124 self.AddMediaNode("sounds", self.ringerwidget, self._tree.sounds) 125 self.AddMediaNode("images", self.wallpaperwidget, self._tree.image) 126 self.ringerwidget.updateprofilevariables(self._main_window.phoneprofile) 127 self.wallpaperwidget.updateprofilevariables(self._main_window.phoneprofile) 128 self.DoMediaSummary()129131 for name in self.ringernodes: 132 self._tree.DeletePage(self.ringernodes[name]) 133 self.ringernodes={} 134 for name in self.wallpapernodes: 135 self._tree.DeletePage(self.wallpapernodes[name]) 136 self.wallpapernodes={} 137 self.OnInit() 138 self.ringerwidget.OnRefresh() 139 self.wallpaperwidget.OnRefresh()140142 if self.widget_to_save==None: 143 self.SaveWidgetToDir(directory, self.ringerwidget) 144 self.SaveWidgetToDir(directory, self.wallpaperwidget) 145 else: 146 self.SaveWidgetToDir(directory, self.widget_to_save, self.origin_to_save)147149 for k,e in widget.sections: 150 # skip unrequested origins 151 if filter!="" and filter!=k.label: 152 continue 153 opath=self._main_window._fixup(os.path.join(directory, k.label)) 154 try: 155 os.makedirs(opath) 156 except: 157 pass 158 if not os.path.isdir(opath): 159 raise Exception("Unable to create export directory "+opath) 160 for item in e: 161 me=widget._data[item.datakey][item.key] 162 if me.mediadata!=None and me.mediadata!='': 163 fpath=self._main_window._fixup(os.path.join(opath, me.name)) 164 with file(fpath, "wb") as f: 165 f.write(me.mediadata) 166 if me.timestamp!=None: 167 os.utime(fpath, (me.timestamp, me.timestamp))168 169171 # create the zipfile in a buffer 172 # and write to disk after it is all created 173 op=cStringIO.StringIO() 174 with contextlib.closing(zipfile.ZipFile(op, "w", zipfile.ZIP_DEFLATED)) as zip: 175 if self.widget_to_save==None: 176 self.SaveWidgetToZip(zip, self.ringerwidget) 177 self.SaveWidgetToZip(zip, self.wallpaperwidget) 178 else: 179 self.SaveWidgetToZip(zip, self.widget_to_save, self.origin_to_save) 180 open(zip_file, "wb").write(op.getvalue())181183 for k,e in widget.sections: 184 # skip unrequested origins 185 if filter!="" and filter!=k.label: 186 continue 187 for item in e: 188 me=widget._data[item.datakey][item.key] 189 if me.mediadata!=None and me.mediadata!='': 190 zi=zipfile.ZipInfo() 191 # zipfile does not like unicode. cp437 works on windows well, may be 192 # a better choice than ascii, but no phones currently support anything 193 # other than ascii for filenames 194 name=k.label+"/"+me.name 195 zi.filename=common.get_ascii_string(name, 'ignore') 196 if me.timestamp==None: 197 zi.date_time=(0,0,0,0,0,0) 198 else: 199 zi.date_time=time.localtime(me.timestamp)[:6] 200 zi.compress_type=zipfile.ZIP_DEFLATED 201 zip.writestr(zi, me.mediadata)202204 res=[] 205 if widget==self.ringerwidget: 206 for name in self.ringernodes: 207 res.append(name) 208 else: 209 for name in self.wallpapernodes: 210 res.append(name) 211 res.sort() 212 return res213215 if widget==self.ringerwidget: 216 if node_name not in self.ringernodes: 217 if icon==None: 218 if string.find(node_name, "sound")!=-1: 219 icon=self._tree.sounds 220 else: 221 icon=self._tree.ringers 222 self.ringernodes[node_name]=self.AddSubPage(widget, node_name, icon) 223 else: 224 if node_name not in self.wallpapernodes: 225 if icon==None: 226 if string.find(node_name, "video")!=-1: 227 icon=self._tree.video 228 elif string.find(node_name, "camera")!=-1: 229 icon=self._tree.camera 230 else: 231 icon=self._tree.image 232 self.wallpapernodes[node_name]=self.AddSubPage(widget, node_name, icon)233256248 super(ExportMediaToDirDialog, self).__init__(parent, message=title, style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON) 249 self.media_root=parent.GetActiveMediaWidget()250252 # do export 253 rc=self.ShowModal() 254 if rc==wx.ID_OK: 255 self.media_root.SaveToDir(self.GetPath())274259 self.media_root=parent.GetActiveMediaWidget() 260 ext="Zip files (*.zip)|*.zip|All Files (*.*)|*" 261 if self.media_root.widget_to_save!=None: 262 default_file=self.media_root.origin_to_save+".zip" 263 print "here 1 "+default_file 264 else: 265 default_file="media.zip" 266 print "here 2 "+default_file 267 super(ExportMediaToZipDialog, self).__init__(parent, title, defaultFile=default_file, wildcard=ext, style=wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)268270 # do export 271 rc=self.ShowModal() 272 if rc==wx.ID_OK: 273 self.media_root.SaveToZip(self.GetPath())
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sun Jan 24 16:22:36 2010 | http://epydoc.sourceforge.net |