home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / gui / browser / webkit / imageloader.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  7.0 KB  |  124 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from util.primitives.funcs import Delegate
  5. from util.primitives.refs import better_ref
  6. from gui.browser.webkit.webkitwindow import WebKitWindow
  7. from simplejson import dumps as jsenc
  8. from rpc.jsonrpc import JSPythonBridge
  9. import wx
  10. _imageloader = None
  11.  
  12. class LazyWebKitImage(object):
  13.     
  14.     def __init__(self, url, default_image):
  15.         self.url = url
  16.         self.default_image = default_image
  17.  
  18.     
  19.     def lazy_load(self, refresh_cb = None):
  20.         img = None
  21.         if self.url:
  22.             img = load_image(self.url, refresh_cb)
  23.         
  24.         if img is None:
  25.             img = self.default_image
  26.         
  27.         return img
  28.  
  29.  
  30.  
  31. def load_image(url, refresh_cb = None):
  32.     global _imageloader
  33.     if _imageloader is None:
  34.         _imageloader = WebKitImageLoader()
  35.     
  36.     img = _imageloader.get_no_download(url)
  37.     if img is not None:
  38.         return img
  39.     return _imageloader.get(url, check_cache = False)
  40.  
  41.  
  42. class WebKitImageLoader(object):
  43.     
  44.     def __init__(self):
  45.         self.webview = None
  46.         self.on_load = Delegate()
  47.         self.on_error = Delegate()
  48.  
  49.     
  50.     def _webview_on_call(self, obj):
  51.         method = obj['method']
  52.         src = obj['params'][0]['src']
  53.         if method == 'onLoad':
  54.             img = self.webview.GetCachedBitmap(src)
  55.             self.on_load(img, src)
  56.         elif method == 'onError':
  57.             self.on_error(src)
  58.         else:
  59.             pformat = pformat
  60.             import pprint
  61.             raise AssertionError('unexpected JS call: ' + pformat(obj))
  62.         return method == 'onLoad'
  63.  
  64.     
  65.     def GetWebView(self):
  66.         if self.webview is not None:
  67.             return self.webview
  68.         self.frame = wx.Frame(None)
  69.         w.js_to_stderr = True
  70.         bridge.on_call += self._webview_on_call
  71.         import gui.skin as gui
  72.         
  73.         jslib = lambda name: jsenc((gui.skin.resourcedir() / 'html' / name).url())
  74.         html = '<!doctype html>\n<html>\n    <head>\n        <script type="text/javascript" src=%(utils)s></script>\n        <script type="text/javascript" src=%(pythonbridgelib)s></script>\n        <script type="text/javascript">\nfunction onLoad(img) {\n    img.load_state = \'loaded\';\n    D.notify(\'onLoad\', {src: img.src});\n}\n\nfunction onError(img) {\n    img.load_state = \'error\';\n    D.notify(\'onError\', {src: img.src});\n}\n\nfunction cmp(a, b) {\n    if (a < b) return -1;\n    else if (b > a) return 1;\n    else return 0;\n}\n\nfunction _byTime(a, b) {\n    return cmp(a.lastAccessTime, b.lastAccessTime);\n}\n\nfunction evictOld() {\n    // TODO: an insertion sort at access time would be more efficient, but this array is small so...eh\n    allImages.sort(_byTime);\n    var numToDelete = allImages.length - MAX_IMAGES;\n    var deleted = allImages.slice(0, numToDelete);\n    allImages = allImages.slice(numToDelete);\n\n    for (var i = 0; i < deleted.length; ++i) {\n        var img = deleted[i];\n        img.parentNode.removeChild(img);\n    }\n}\n\nwindow.allImages = [];\nwindow.MAX_IMAGES = 30;\n\n        </script>\n    </head>\n    <body>\n    </body>\n</html>' % dict(pythonbridgelib = jslib('pythonbridge.js'), utils = jslib('utils.js'))
  75.         self.bridge.SetPageSource(html, 'file://imageloader')
  76.         return self.webview
  77.  
  78.     
  79.     def get_no_download(self, url):
  80.         webview = self.GetWebView()
  81.         img = webview.GetCachedBitmap(url)
  82.         if img.Ok():
  83.             return img
  84.  
  85.     
  86.     def get(self, url, check_cache = True):
  87.         if check_cache:
  88.             img = self.get_no_download(url)
  89.             if img is not None:
  90.                 return img
  91.         
  92.         html = "\n(function() {\n\nvar img = document.getElementById(%(hashed_url)s);\nif (!img) {\n    img = document.createElement('img');\n    document.body.appendChild(img);\n    window.allImages.push(img);\n    img.setAttribute('id', %(hashed_url)s);\n    img.setAttribute('onLoad', 'onLoad(this);');\n    img.setAttribute('onError', 'onError(this);');\n    img.src = %(url)s;\n} else {\n    if (img.load_state === 'loaded')\n        onLoad(img);\n    else if (img.load_state === 'error')\n        onError(img);\n}\n\nimg.lastAccessTime = new Date().getTime();\nif (window.allImages.length > window.MAX_IMAGES)\n    evictOld();\n\n})();\n    " % dict(url = jsenc(url), hashed_url = jsenc(urlhash(url)))
  93.         self.GetWebView().RunScript(html)
  94.  
  95.  
  96. import hashlib
  97.  
  98. def urlhash(url, algo = hashlib.md5):
  99.     return algo(url.encode('utf8')).hexdigest()
  100.  
  101.  
  102. def main():
  103.     testapp = testapp
  104.     import tests.testapp
  105.     app = testapp()
  106.     il = WebKitImageLoader()
  107.     
  108.     def test_onLoad(img, url):
  109.         print 'onLoad', img, url
  110.  
  111.     
  112.     def test_onError(url):
  113.         print 'onError', url
  114.  
  115.     il.on_load += test_onLoad
  116.     il.on_error += test_onError
  117.     il.get('http://img.digsby.com/logos/digsby_196x196.png')
  118.     il.frame.Show()
  119.     app.MainLoop()
  120.  
  121. if __name__ == '__main__':
  122.     main()
  123.  
  124.