#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Python script to make Skype co-operate with GNOME3 notifications.
#
# Copyright (c) 2012, Mike Kazantsev
# Copyright (c) 2011, John Stowers
# Copyright (c) 2009, Lightbreeze
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# To use this script:
# -> open Skype
# -> open the menu and press 'Options' or press Ctrl-O
# -> go to 'Notifications' tab
# -> hit the 'Advanced' button and check 'Execute the following script on _any_ event'
# -> paste: skype_notify "%type" "%sname" "%fname" "%fpath" "%smessage" "%fsize" "%sskype"
# -> disable or enable the notifications you want to receive.

import argparse
parser = argparse.ArgumentParser(
	description='Bridge netween skype and desktop-notification daemon.')
for arg in 'type sname fname fpath smessage fsize sskype'.split(): parser.add_argument(arg)
optz = parser.parse_args()

import itertools as it, operator as op, functools as ft
from gi.repository import Gtk, Notify
import os, sys

# Ripped from https://gist.github.com/1361027
notifications = dict(
	SkypeLogin=dict(summary='Skype', body='You have logged into Skype with {sname}'),
	SkypeLogout=dict(summary='You have logged out of Skype', icon_name='user-offline'),
	SkypeLoginFailed=dict(summary='Skype login failed', icon_name='user-offline'),
	CallConnecting=dict(summary='Dailing... {sname}', icon_name='call-start'),
	CallRingingIn=dict(summary='Brring..', body='{sname} is calling you', icon_name='call-start'),
	CallRingingOut=dict(summary='Dididi.. dididi...',
		body='You are calling {sname}', icon_name='call-start'),
	CallAnswered=dict(summary='Call Answered', icon_name='call-start'),
	VoicemailReceived=dict(summary='{sname}', body='Voicemail Received'),
	VoicemailSent=dict(summary='Voicemail Sent'),
	ContactOnline=dict(summary='{sname} is now online', icon_name='user-online'),
	ContactOffline=dict(summary='{sname} is now offline', icon_name='user-offline'),
	ContactDeleted=None,
	ChatIncomingInitial=dict(summary='{sname}', body='{smessage}', icon_name='im-message-new'),
	ChatIncoming=dict(summary='{sname}', body='{smessage}', icon_name='im-message-new'),
	ChatOutgoing=None,
	ChatJoined=dict(summary='{sname} joined chat', body='{smessage}', icon_name='emblem-people'),
	ChatParted=dict(summary='{sname} left chat', body='{smessage}'),
	TransferComplete=dict(summary='Transfer Complete',
		body='{fname} saved to {fpath}{fname}', icon_name='gtk-save'),
	TransferFailed=dict(summary='Transfer Failed', body='{fname}', icon_name='gtk-close'),
	Birthday=dict(summary='{sname} has a birthday Tomorrow', icon_name='appointment-soon') )
notifications['%type'] = None # we get %type at skype startup sometimes. ignore it

Notify.init('skype')
try:
	note = notifications.get( optz.type,
		dict(summary='{type}', body='{sname} ({sskype})') )
	if note is None: sys.exit()

	if 'icon_name' not in note: note['icon_name'] = 'skype'
	if not Gtk.IconTheme.get_default().has_icon(note['icon_name']):
		note['icon_name'] = 'skype'

	note_data = vars(optz)
	for k in 'summary', 'body':
		if k not in note: continue
		note[k] = note[k].format(**note_data)
	note_data = note

	note = Notify.Notification()
	note.set_properties(**note_data)
	note.show()

except Exception as err:
	import traceback
	note = Notify.Notification()
	note.set_properties(
		summary='Failed to display skype notification',
		body='Error: {}\n\n{}'.format(err, traceback.format_exc().strip()) )
	note.set_urgency(Notify.Urgency.CRITICAL)
	note.show()
