Advertisement
Guest User

skype-notify-with-avatars

a guest
Mar 31st, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.65 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # Python script to make Skype co-operate with GNOME3 notifications.
  3. #
  4. #
  5. #
  6. # Copyright (c) 2011, John Stowers
  7. #
  8. # Adapted from skype-notify.py
  9. # Copyright (c) 2009, Lightbreeze
  10. #
  11. #
  12. # This program is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation, either version 3 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24. #
  25. #
  26. # to use this script: Open Skype -> Open the menu and press 'Options' or press Ctrl-O
  27. # -> hit the 'Advanced' button and check 'Execute the following script on _any_ event'
  28. # -> paste: python /path/to/skype-notify.py -e"%type" -n"%sname" -f"%fname" -p"%fpath" -m"%smessage" -s%fsize -u%sskype
  29. # -> disable or enable the notifications you want to receive.
  30.  
  31. import sys, os, textwrap
  32. from optparse import OptionParser
  33. from gi.repository import Notify, GLib, Gtk
  34.  
  35. # name :    summary, body, icon (optional, default skype),
  36. #           transient (optional, default True),
  37. #           urgency (optional, default NORMAL)
  38.  
  39. NOTIFICATIONS = {
  40.     'SkypeLogin':           ("Skype","You have logged into Skype with {contact}","skype"),
  41.     'SkypeLogout':          ("You have logged out of Skype",None,"user-offline"),
  42.     'SkypeLoginFailed':     ("Skype login failed",None,"user-offline"),
  43.     'CallConnecting':       ("Dailing... {contact}",None,"call-start"),
  44.     'CallRingingIn':        ("Brring..","{contact} is calling you","call-start", False, Notify.Urgency.CRITICAL),
  45.     'CallRingingOut':       ("Dididi.. dididi...","You are calling {contact}","call-start"),
  46.     'CallAnswered':         ("Call Answered",None,"call-start"),
  47.     'VoicemailReceived':    ("{contact}","Voicemail Received","skype"),
  48.     'VoicemailSent':        ("Voicemail Sent",None,"skype"),
  49.     'ContactOnline':        ("{contact} is now online",None,"user-online"),
  50.     'ContactOffline':       ("{contact} is now offline",None,"user-offline"),
  51.     'ContactDeleted':       False,
  52.     'ChatIncomingInitial':  ("{contact}","{message}","im-message-new"),
  53.     'ChatIncoming':         ("{contact}","{message}","im-message-new"),
  54.     'ChatOutgoing':         False,
  55.     'ChatJoined':           ("{contact} joined chat","{message}","emblem-people"),
  56.     'ChatParted':           ("{contact} left chat","{message}",None),
  57.     'TransferComplete':     ("Transfer Complete","{filename} saved to {path}{filename}","gtk-save"),
  58.     'TransferFailed':       ("Transfer Failed","{filename}","gtk-close"),
  59.     'Birthday':             ("{contact} has a birthday Tomorrow",None,"appointment-soon"),
  60.     '%type':                False, #we get %type at skype startup sometimes. ignore it
  61. }
  62.  
  63. class NotifyForSkype:
  64.     def __init__(self):
  65.         # Initiate pynotify
  66.         if not Notify.init("Skype Notifier"):
  67.             sys.exit(-1)
  68.  
  69.         # Add argument parser options
  70.         parser = OptionParser()
  71.         parser.add_option("-e", "--event", dest="type", help="type of SKYPE_EVENT")
  72.         parser.add_option("-n", "--sname", dest="sname", help="display-name of contact")
  73.         parser.add_option("-u", "--skype", dest="sskype", help="skype-username of contact")
  74.         parser.add_option("-m", "--smessage", dest="smessage", help="message body")
  75.         parser.add_option("-p", "--path", dest="fpath", help="path to file")
  76.         parser.add_option("-s", "--size", dest="fsize", help="incoming file size")
  77.         parser.add_option("-f", "--filename", dest="fname", help="file name", metavar="FILE")
  78.         (o, args) = parser.parse_args()
  79.  
  80.         try:
  81.             verb = "Showing"
  82.             self._show_notification(o, *NOTIFICATIONS[o.type])
  83.         except KeyError:
  84.             verb = "Unknown"
  85.             self._show_notification(o, "{type}", "{contact} ({user})")
  86.         except ValueError:
  87.             verb = "Skipped"
  88.  
  89.         print "%s notification: %s\n\targs: %s" % (verb, o.type, ", ".join(sys.argv))
  90.  
  91.     def _show_notification(self, o, summary, body, icon="/usr/share/icons/Faenza/apps/48/skype.png", transient=True, urgency=Notify.Urgency.NORMAL):
  92.         if summary == None:
  93.             summary = ""
  94.         if body == None:
  95.             body = ""
  96.         if o.sskype == None:
  97.             o.sskype = ""
  98.         if o.smessage == None:
  99.             o.smessage = ""
  100.         #o.smessage = "\n".join(textwrap.wrap(o.smessage, 10))
  101.        
  102.         #im-message-new is not a standard icon name, neither is user-*, but they are present
  103.         #in many themes that support empathy. lookup the supplied icon and fallback to skype
  104.         #if missing
  105.         #if not Gtk.IconTheme.get_default().has_icon(icon)
  106.  
  107.  
  108.        
  109.         filename = os.environ.get('HOME')+'/.Skype/avatars/'+o.sskype+'_small.jpg'
  110. #        os.system('notify-send '+filename)
  111.         if os.path.exists(filename):
  112.             icon = filename
  113.  
  114.         n = Notify.Notification.new(
  115.                 summary.format(filename=o.fname,path=o.fpath,contact=o.sname,message=o.smessage,type=o.type,user=o.sskype),
  116.                 body.format(filename=o.fname,path=o.fpath,contact=o.sname,message=o.smessage,type=o.type,user=o.sskype),
  117.                 icon)
  118.         n.set_hint("transient", GLib.Variant.new_boolean(transient))
  119.         n.set_urgency(urgency)
  120.         n.show()
  121.  
  122. cm = NotifyForSkype()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement