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

Source Code for Module media_root

  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 
 34   
35 -class MediaWidget(wx.Panel, widgets.BitPimWidget):
36 - def __init__(self, mainwindow, parent):
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() 59
60 - def DoMediaSummary(self):
61 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()
88
89 - def GetNiceSizeString(self, size):
90 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)
99
100 - def GetWidgetSummary(self, widget, res):
101 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 res
109
110 - def GetRightClickMenuItems(self, node):
111 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 result
115
116 - def GetRinger(self):
117 return self.ringerwidget
118
119 - def GetWallpaper(self):
120 return self.wallpaperwidget
121
122 - def OnInit(self):
123 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()
129
130 - def OnPhoneModelChanged(self, msg):
131 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()
140
141 - def SaveToDir(self, directory):
142 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)
147
148 - def SaveWidgetToDir(self, directory, widget, filter=""):
149 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 169
170 - def SaveToZip(self, zip_file):
171 # 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())
181
182 - def SaveWidgetToZip(self, zip, widget, filter=""):
183 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)
202
203 - def GetNodeList(self, widget):
204 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 res
213
214 - def AddMediaNode(self, node_name, widget, icon=None):
215 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)
233
234 - def GetNodeName(self, widget, node):
235 if widget==self.ringerwidget: 236 for name in self.ringernodes: 237 if self.ringernodes[name]==node: 238 return name 239 else: 240 for name in self.wallpapernodes: 241 if self.wallpapernodes[name]==node: 242 return name
243 244 245 #------------------------------------------------------------------------------
246 -class ExportMediaToDirDialog(wx.DirDialog):
247 - def __init__(self, parent, title):
248 super(ExportMediaToDirDialog, self).__init__(parent, message=title, style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON) 249 self.media_root=parent.GetActiveMediaWidget()
250
251 - def DoDialog(self):
252 # do export 253 rc=self.ShowModal() 254 if rc==wx.ID_OK: 255 self.media_root.SaveToDir(self.GetPath())
256
257 -class ExportMediaToZipDialog(wx.FileDialog):
258 - def __init__(self, parent, title):
259 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)
268
269 - def DoDialog(self):
270 # do export 271 rc=self.ShowModal() 272 if rc==wx.ID_OK: 273 self.media_root.SaveToZip(self.GetPath())
274