2007-07-14 01:38:31 +00:00
|
|
|
# Copyright (C) 2007 - Micah Bucy <eternalsword@gmail.com>
|
|
|
|
#
|
|
|
|
# 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 2, 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, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
### Initialization ###
|
|
|
|
|
2007-11-22 00:02:06 +00:00
|
|
|
plugin_name = "Torrent Notification"
|
2007-07-14 01:38:31 +00:00
|
|
|
plugin_author = "Micah Bucy"
|
|
|
|
plugin_version = "0.1"
|
2007-08-27 06:03:16 +00:00
|
|
|
plugin_description = _("Make tray icon blink when torrent finishes downloading \
|
|
|
|
and/or popup a notification")
|
2007-07-14 01:38:31 +00:00
|
|
|
|
|
|
|
def deluge_init(deluge_path):
|
|
|
|
global path
|
|
|
|
path = deluge_path
|
|
|
|
|
|
|
|
def enable(core, interface):
|
|
|
|
global path
|
|
|
|
return TorrentNotification(path, core, interface)
|
|
|
|
|
|
|
|
### The Plugin ###
|
|
|
|
import deluge
|
2007-10-18 23:15:52 +00:00
|
|
|
import deluge.common
|
2007-07-14 01:38:31 +00:00
|
|
|
import gtk
|
2007-12-14 01:19:39 +00:00
|
|
|
import os.path
|
2007-07-14 01:38:31 +00:00
|
|
|
|
|
|
|
class TorrentNotification:
|
|
|
|
|
|
|
|
def __init__(self, path, core, interface):
|
2008-01-06 19:53:47 +00:00
|
|
|
print "Found TorrentNotification plugin..."
|
2007-08-30 02:04:26 +00:00
|
|
|
import os.path
|
2007-07-14 01:38:31 +00:00
|
|
|
self.path = path
|
|
|
|
self.core = core
|
|
|
|
self.interface = interface
|
|
|
|
self.window = self.interface.window
|
|
|
|
self.window.connect("focus_in_event", self.set_tray_flashing_off)
|
2007-07-16 11:51:12 +00:00
|
|
|
self.core.connect_event(self.core.constants['EVENT_FINISHED'], self.handle_event)
|
2007-07-14 01:38:31 +00:00
|
|
|
|
|
|
|
# Create an options file and try to load existing Values
|
2007-12-14 01:19:39 +00:00
|
|
|
self.config_file = os.path.join(deluge.common.CONFIG_DIR, "notification.conf")
|
2007-10-18 23:15:52 +00:00
|
|
|
if deluge.common.windows_check():
|
|
|
|
self.config = deluge.pref.Preferences(self.config_file, False,
|
|
|
|
defaults={'enable_tray_blink' : True,
|
|
|
|
'enable_notification' : False,
|
|
|
|
'enable_sound' : False,
|
|
|
|
'sound_path' : os.path.expanduser("~")})
|
|
|
|
else:
|
|
|
|
self.config = deluge.pref.Preferences(self.config_file, False,
|
|
|
|
defaults={'enable_tray_blink' : True,
|
|
|
|
'enable_notification' : True,
|
|
|
|
'enable_sound' : False,
|
|
|
|
'sound_path' : os.path.expanduser("~")})
|
2007-07-14 01:38:31 +00:00
|
|
|
try:
|
2007-07-19 04:42:51 +00:00
|
|
|
self.config.load()
|
2007-07-14 01:38:31 +00:00
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
2007-12-14 01:19:39 +00:00
|
|
|
self.glade = gtk.glade.XML(os.path.join(path, "notification_preferences.glade"))
|
2007-07-14 01:38:31 +00:00
|
|
|
self.dialog = self.glade.get_widget("dialog")
|
2007-08-05 20:13:24 +00:00
|
|
|
self.dialog.set_position(gtk.WIN_POS_CENTER)
|
2007-07-19 04:42:51 +00:00
|
|
|
self.glade.signal_autoconnect({
|
2007-08-05 20:13:24 +00:00
|
|
|
'toggle_ui': self.toggle_ui,
|
|
|
|
'dialog_ok': self.dialog_ok,
|
|
|
|
'dialog_cancel': self.dialog_cancel
|
2007-07-19 04:42:51 +00:00
|
|
|
})
|
2007-07-14 01:38:31 +00:00
|
|
|
|
|
|
|
def handle_event(self, event):
|
2007-07-18 14:19:39 +00:00
|
|
|
if event['message'] == "torrent has finished downloading":
|
|
|
|
if self.config.get("enable_tray_blink"):
|
|
|
|
self.set_tray_flashing_on()
|
|
|
|
if self.config.get("enable_notification"):
|
|
|
|
self.show_notification(event)
|
2007-07-19 04:42:51 +00:00
|
|
|
if self.config.get("enable_sound"):
|
|
|
|
self.play_sound()
|
2007-07-14 01:38:31 +00:00
|
|
|
|
|
|
|
def unload(self):
|
2007-07-16 11:51:12 +00:00
|
|
|
self.core.disconnect_event(self.core.constants['EVENT_FINISHED'], self.handle_event)
|
2007-07-14 01:38:31 +00:00
|
|
|
self.config.save(self.config_file)
|
|
|
|
|
|
|
|
def set_tray_flashing_off(self, focusdata1, focusdata2):
|
|
|
|
self.interface.tray_icon.set_blinking(False)
|
|
|
|
|
|
|
|
def set_tray_flashing_on(self):
|
|
|
|
if self.window.has_toplevel_focus() is not True:
|
|
|
|
self.interface.tray_icon.set_blinking(True)
|
|
|
|
|
|
|
|
def show_notification(self, event):
|
2007-10-18 23:15:52 +00:00
|
|
|
if not deluge.common.windows_check():
|
2007-10-31 05:45:07 +00:00
|
|
|
try:
|
|
|
|
import pynotify
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
2007-12-25 03:02:07 +00:00
|
|
|
state = self.interface.manager.get_torrent_state(event['unique_ID'])
|
2007-10-31 05:45:07 +00:00
|
|
|
if pynotify.init("Deluge"):
|
2007-12-25 03:02:07 +00:00
|
|
|
n = pynotify.Notification(_("Torrent complete"), state['name'])
|
2007-10-31 05:45:07 +00:00
|
|
|
n.set_icon_from_pixbuf(deluge.common.get_logo(48))
|
2008-01-06 19:53:47 +00:00
|
|
|
if not n.show():
|
|
|
|
print "Failed to send notification"
|
2007-07-14 01:38:31 +00:00
|
|
|
else:
|
2007-09-11 19:20:41 +00:00
|
|
|
pass
|
2007-07-14 01:38:31 +00:00
|
|
|
|
2007-08-06 00:40:18 +00:00
|
|
|
def configure(self, window):
|
2007-08-30 02:04:26 +00:00
|
|
|
self.glade.get_widget("chk_tray_blink").set_active(self.config.get("enable_tray_blink"))
|
2007-10-18 23:25:26 +00:00
|
|
|
if deluge.common.windows_check():
|
|
|
|
self.glade.get_widget("chk_notification").set_active(False)
|
|
|
|
self.glade.get_widget("chk_notification").set_sensitive(False)
|
2007-10-31 03:52:55 +00:00
|
|
|
self.glade.get_widget("chk_sound").set_active(False)
|
2007-10-31 11:40:56 +00:00
|
|
|
self.glade.get_widget("chk_sound").set_sensitive(False)
|
2007-10-31 03:52:55 +00:00
|
|
|
self.glade.get_widget("sound_path_button").set_sensitive(False)
|
2007-10-18 23:25:26 +00:00
|
|
|
else:
|
|
|
|
self.glade.get_widget("chk_notification").set_active(self.config.get("enable_notification"))
|
2007-10-31 03:52:55 +00:00
|
|
|
self.glade.get_widget("chk_sound").set_active(self.config.get("enable_sound"))
|
|
|
|
self.glade.get_widget("sound_path_button").set_sensitive(self.config.get("enable_sound"))
|
|
|
|
self.glade.get_widget("sound_path_button").set_filename(self.config.get("sound_path"))
|
2007-08-06 00:40:18 +00:00
|
|
|
self.dialog.set_transient_for(window)
|
2007-07-14 01:38:31 +00:00
|
|
|
self.dialog.show()
|
2007-08-05 20:13:24 +00:00
|
|
|
|
|
|
|
def dialog_ok(self, source):
|
|
|
|
self.dialog.hide()
|
|
|
|
self.config.set("enable_tray_blink", self.glade.get_widget("chk_tray_blink").get_active())
|
|
|
|
self.config.set("enable_notification", self.glade.get_widget("chk_notification").get_active())
|
|
|
|
self.config.set("enable_sound", self.glade.get_widget("chk_sound").get_active())
|
|
|
|
self.config.set("sound_path", self.glade.get_widget("sound_path_button").get_filename())
|
|
|
|
|
|
|
|
def dialog_cancel(self, source):
|
2007-07-14 01:38:31 +00:00
|
|
|
self.dialog.hide()
|
2007-07-19 04:42:51 +00:00
|
|
|
|
|
|
|
def toggle_ui(self, widget):
|
|
|
|
value = widget.get_active()
|
|
|
|
if widget == self.glade.get_widget("chk_sound"):
|
|
|
|
self.glade.get_widget("sound_path_button").set_sensitive(value)
|
|
|
|
|
|
|
|
def play_sound(self):
|
2007-10-18 23:15:52 +00:00
|
|
|
if not deluge.common.windows_check():
|
2007-09-11 19:20:41 +00:00
|
|
|
try:
|
2007-10-31 05:46:49 +00:00
|
|
|
import pygame
|
2007-09-11 19:20:41 +00:00
|
|
|
except:
|
2007-10-31 05:46:49 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
import sys
|
|
|
|
pygame.init()
|
|
|
|
try:
|
|
|
|
name = self.config.get("sound_path")
|
|
|
|
except:
|
|
|
|
print "no file set"
|
|
|
|
try:
|
|
|
|
alert_sound = pygame.mixer.music
|
|
|
|
alert_sound.load(name)
|
|
|
|
alert_sound.play()
|
|
|
|
except pygame.error, message:
|
|
|
|
print 'Cannot load sound:'
|
2007-09-11 19:20:41 +00:00
|
|
|
else:
|
|
|
|
pass
|