# -*- coding: UTF-8 -*-
# Copyright (c) Kanru Chen <koster@debian.org.tw>
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

__module_name__ = "xchat-notify"
__module_version__ = "0.1"
__module_description__ = "Freedesktop Notification Framework interation plugin"

import xchat
import dbus

class Notification:
	def __init__(self):
		self.app_name = ""
		self.replaces_id = 0
		self.app_icon = ""
		self.summary = ""
		self.body = ""
		self.actions = []
		self.hints = {}
		self.expire_timeout = 1000

		try:
			session_bus = dbus.SessionBus()
			obj = session_bus.get_object("org.freedesktop.Notifications","/org/freedesktop/Notifications")
			self.interface = dbus.Interface(obj, "org.freedesktop.Notifications")
		except Exception:
			self.interface = None

	def setAppName(self, app_name):
		self.app_name = app_name

	def setReplacesId(self, replaces_id):
		self.replaces_id = replaces_id

	def setIcon(self, app_icon):
		self.app_icon = app_icon

	def setSummary(self, summary):
		self.summary = summary

	def setBody(self, body):
		self.body = body

	def addAction(self, action):
		t.append(action)

	def addHint(self, hint):
		pass # TODO

	def setTimeout(self, expire_timeout):
		self.expire_timeout = expire_timeout * 1000

	def setMSTimeout(self, expire_timeout):
		self.expire_timeout = expire_timeout

	def notify(self):
		self.interface.Notify(self.app_name, self.replaces_id, self.app_icon, self.summary, self.body, self.actions, self.hints, self.expire_timeout)


notif = Notification()
notif.setAppName("XChat")
notif.setIcon("/usr/share/pixmaps/xchat.png")
notif.setTimeout(4)

def highlight_cb(word, word_eol, userdata):
	if xchat.get_info("win_status") == "hidden":
		userdata.setSummary(xchat.get_info("channel")+": "+word[0].decode('utf8'))
		userdata.setBody(word[1].decode('utf8'))
		userdata.notify()
	return xchat.EAT_NONE

def private_cb(word, word_eol, userdata):
	if xchat.get_info("win_status") == "hidden":
		userdata.setSummary(xchat.get_info("channel")+": "+word[0].decode('utf8'))
		userdata.setBody(word[1].decode('utf8'))
		userdata.notify()
	return xchat.EAT_NONE

xchat.hook_print("Channel Action Hilight", highlight_cb, notif)
xchat.hook_print("Channel Msg Hilight", highlight_cb, notif)
xchat.hook_print("Private Message", private_cb, notif)
xchat.hook_print("Private Message to Dialog", private_cb, notif)
xchat.prnt(__module_description__ + " loaded")
