/ lib / python / qtvcp / lib / notify.py
notify.py
  1  import sys
  2  # try to add a notify system so messages use the
  3  # nice intergrated pop-ups
  4  # Ubuntu kinda wrecks this be not following the
  5  # standard - you can't set how long the message stays up for.
  6  # I suggest fixing this with a PPA off the net
  7  # https://launchpad.net/~leolik/+archive/leolik?field.series_filter=lucid
  8      # callback work around:
  9      # http://stackoverflow.com/questions/8727937/callbacks-and-gtk-main-loop
 10  
 11  from qtvcp.core import Status
 12  from qtvcp.lib import sys_notify
 13  
 14  # Set up logging
 15  from qtvcp import logger
 16  log = logger.getLogger(__name__)
 17  
 18  STATUS = Status()
 19  sys_notify.init('notify')
 20  
 21  
 22  class Notify:
 23      def __init__(self):
 24          self.statusbar = None
 25          self.notify_list = []
 26          self.alarmpage = []
 27          STATUS.connect('shutdown', self.cleanup)
 28  
 29      # This prints a message in the status bar (if available)
 30      # the system notifier (if available)
 31      # adds an entry to the alarm page (if available)
 32      def notify(self, title, message,icon="", status_timeout=0, timeout=2):
 33          messageid = None
 34          try:
 35              self.show_status(message, status_timeout)
 36          except:
 37              pass
 38          try:
 39              n = self.show_notification(title, message, icon, timeout)
 40          except Exception as e:
 41                 log.warning('show_noficication error:', exc_info=e)
 42          return n
 43  
 44      # 
 45      def new_critical(self, icon=""):
 46          messageid = None
 47          try:
 48              n = self.build_error_notification(icon)
 49          except Exception as e:
 50                 log.warning('New_critical error:', exc_info=e)
 51          return n
 52  
 53  
 54      def notify_yn(self, title, message,icon, timeout, function_callback):
 55          try:
 56              self.show_yn_notification(title, message, icon, timeout,function_callback)
 57          except Exception as e:
 58                 log.warning('show_noficication_yn error:', exc_info=e)
 59  
 60      def notify_ok(self, title, message,icon, timeout, function_callback):
 61          try:
 62              self.show_ok_notification(title, message, icon, timeout,function_callback)
 63          except Exception as e:
 64                 log.warning('show_noficication_ok error:', exc_info=e)
 65  
 66      def build_error_notification(self, icon=None):
 67          n = sys_notify.Notification('', '', icon)
 68          n.setUrgency(sys_notify.Urgency.CRITICAL)
 69          n.setTimeout(0)
 70          n.addAction("action_click","Show All Messages", self.action_callback)
 71          n.onClose(self.handle_closed)
 72          n.addAction('Clear Messages', 'Clear', self.clearClicked)
 73          self.notify_list.append(n)
 74          return n
 75  
 76      def show_notification(self, title, message, icon=None, timeout=4):
 77          n = sys_notify.Notification(title, message, icon)
 78          n.setUrgency(sys_notify.Urgency.NORMAL)
 79          n.setTimeout(int(timeout * 1000))
 80          n.addAction("action_click","Show All Messages", self.action_callback)
 81          n.onClose(self.handle_closed)
 82          n.show()
 83          self.notify_list.append(n)
 84          return n
 85  
 86      def show_yn_notification(self, title, message, icon, timeout,callback):
 87          self._callback=callback
 88          n = sys_notify.Notification(title, message, icon)
 89          n.setUrgency(sys_notify.Urgency.CRITICAL)
 90          n.setTimeout(timeout* 1000)
 91          n.addAction("Yes", "Yes", self.yesClicked)
 92          n.onClose(self.handle_closed)
 93          n.addAction('No', 'No', self.noClicked)
 94          n.show()
 95          self.notify_list.append(n)
 96  
 97      def show_ok_notification(self, title, message, icon, timeout, callback):
 98          n = sys_notify.Notification(title, message, icon)
 99          n.setUrgency(sys_notify.Urgency.CRITICAL)
100          n.setTimeout(timeout* 1000)
101          n.addAction("Ok", "ok", self.okClicked, callback)
102          n.onClose(self.handle_closed)
103          n.addAction('Canel', 'canel', self.cancelClicked, callback)
104          n.show()
105          self.notify_list.append(n)
106  
107      def yesClicked(self, n, action, callback):
108          callback(True)
109  
110      def noClicked(self, n, action, callback):
111          callback(False)
112  
113      def okClicked(self, n, action, callback):
114          callback(True)
115  
116      def cancelClicked(self, n, action, callack):
117          callback(False)
118  
119      def handle_closed(self,n):
120          pass
121          #print self._n
122          #print n
123  
124      def clearClicked(self, n, text):
125          n.body = ''
126          n.close()
127  
128      def OnClicked(self, n, signal_text):
129          print '1: ' + str(n)
130          print '2: ' + str(signal_text)
131          n.close()
132  
133      def action_callback(self, *args, **kwds):
134          print '\nAll recorded messages:'
135          for num,i in enumerate(self.alarmpage):
136              print num,i
137  
138      def show_status(self, message, timeout=4):
139          if self.statusbar is not None:
140              try:
141                  messageid = self.statusbar.showMessage(message, timeout * 1000)
142              except Exception as e:
143                  log.warning('Error adding msg to  statusbar:', exc_info=e)
144  
145      def add_alarm_entry(self, message):
146          try:
147              self.alarmpage.append(message)
148          except:
149              pass
150  
151      def cleanup(self, w):
152          for i in self.notify_list:
153              i.close()
154  
155      def update(self, n, title='', message=''):
156          if title is not None:
157              n.title = title
158          n.body = n.body +'\n'+ title+'\n'+ message
159          n.show()
160          try:
161              self.show_status(message, 5)
162          except:
163              pass
164          try:
165              self.add_alarm_entry(message)
166          except:
167              pass
168