home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1308 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.0 KB  |  187 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. from calibre.constants import islinux, isosx
  9.  
  10. class Notifier(object):
  11.     DEFAULT_TIMEOUT = 5000
  12.     
  13.     def get_msg_parms(self, timeout, body, summary):
  14.         if summary is None:
  15.             summary = 'calibre'
  16.         
  17.         if timeout == 0:
  18.             timeout = self.DEFAULT_TIMEOUT
  19.         
  20.         return (timeout, body, summary)
  21.  
  22.     
  23.     def __call__(self, body, summary = None, replaces_id = None, timeout = 0):
  24.         raise NotImplementedError
  25.  
  26.  
  27.  
  28. class DBUSNotifier(Notifier):
  29.     ICON = I('notify.png')
  30.     
  31.     def __init__(self, server, path):
  32.         self.ok = True
  33.         self.err = None
  34.         
  35.         try:
  36.             import dbus
  37.             self.dbus = dbus
  38.             self._notify = dbus.SessionBus().get_object(server, path)
  39.         except Exception:
  40.             err = None
  41.             self.ok = False
  42.             self.err = str(err)
  43.  
  44.  
  45.  
  46.  
  47. class KDENotifier(DBUSNotifier):
  48.     
  49.     def __init__(self):
  50.         DBUSNotifier.__init__(self, 'org.kde.VisualNotifications', '/VisualNotifications')
  51.  
  52.     
  53.     def __call__(self, body, summary = None, replaces_id = None, timeout = 0):
  54.         if replaces_id is None:
  55.             replaces_id = self.dbus.UInt32()
  56.         
  57.         event_id = ''
  58.         (timeout, body, summary) = self.get_msg_parms(timeout, body, summary)
  59.         
  60.         try:
  61.             self._notify.Notify('calibre', replaces_id, event_id, self.ICON, summary, body, self.dbus.Array(signature = 's'), self.dbus.Dictionary(signature = 'sv'), timeout)
  62.         except:
  63.             import traceback
  64.             traceback.print_exc()
  65.  
  66.  
  67.  
  68.  
  69. class FDONotifier(DBUSNotifier):
  70.     
  71.     def __init__(self):
  72.         DBUSNotifier.__init__(self, 'org.freedesktop.Notifications', '/org/freedesktop/Notifications')
  73.  
  74.     
  75.     def __call__(self, body, summary = None, replaces_id = None, timeout = 0):
  76.         if replaces_id is None:
  77.             replaces_id = self.dbus.UInt32()
  78.         
  79.         (timeout, body, summary) = self.get_msg_parms(timeout, body, summary)
  80.         
  81.         try:
  82.             self._notify.Notify('calibre', replaces_id, self.ICON, summary, body, self.dbus.Array(signature = 's'), self.dbus.Dictionary(signature = 'sv'), timeout)
  83.         except:
  84.             import traceback
  85.             traceback.print_exc()
  86.  
  87.  
  88.  
  89.  
  90. class QtNotifier(Notifier):
  91.     
  92.     def __init__(self, systray = None):
  93.         self.systray = systray
  94.         if self.systray is not None:
  95.             pass
  96.         self.ok = self.systray.supportsMessages()
  97.  
  98.     
  99.     def __call__(self, body, summary = None, replaces_id = None, timeout = 0):
  100.         (timeout, body, summary) = self.get_msg_parms(timeout, body, summary)
  101.         if self.systray is not None:
  102.             
  103.             try:
  104.                 hide = False
  105.                 
  106.                 try:
  107.                     if not isinstance(body, unicode):
  108.                         body = body.decode('utf-8')
  109.                     
  110.                     if isosx and not self.systray.isVisible():
  111.                         self.systray.show()
  112.                         hide = True
  113.                     
  114.                     self.systray.showMessage(summary, body, self.systray.Information, timeout)
  115.                 finally:
  116.                     if hide:
  117.                         self.systray.hide()
  118.                     
  119.  
  120.  
  121.         
  122.  
  123.  
  124.  
  125. class GrowlNotifier(Notifier):
  126.     notification_type = 'All notifications'
  127.     
  128.     def __init__(self):
  129.         
  130.         try:
  131.             import Growl
  132.             self.icon = Growl.Image.imageFromPath(I('notify.png'))
  133.             self.growl = Growl.GrowlNotifier(applicationName = 'calibre', applicationIcon = self.icon, notifications = [
  134.                 self.notification_type])
  135.             self.growl.register()
  136.             self.ok = True
  137.         except:
  138.             self.ok = False
  139.  
  140.  
  141.     
  142.     def encode(self, msg):
  143.         if isinstance(msg, unicode):
  144.             msg = msg.encode('utf-8')
  145.         
  146.         return msg
  147.  
  148.     
  149.     def __call__(self, body, summary = None, replaces_id = None, timeout = 0):
  150.         (timeout, body, summary) = self.get_msg_parms(timeout, body, summary)
  151.         if self.ok:
  152.             
  153.             try:
  154.                 self.growl.notify(self.notification_type, self.encode(summary), self.encode(body))
  155.             import traceback
  156.             traceback.print_exc()
  157.  
  158.         
  159.  
  160.  
  161.  
  162. def get_notifier(systray = None):
  163.     ans = None
  164.     if islinux:
  165.         ans = KDENotifier()
  166.         if not ans.ok:
  167.             ans = FDONotifier()
  168.             if not ans.ok:
  169.                 ans = None
  170.             
  171.         
  172.     
  173.     if ans is None:
  174.         ans = QtNotifier(systray)
  175.         if not ans.ok:
  176.             ans = None
  177.         
  178.     
  179.     return ans
  180.  
  181. if __name__ == '__main__':
  182.     n = KDENotifier()
  183.     n('hello')
  184.     n = FDONotifier()
  185.     n('hello')
  186.  
  187.