summaryrefslogtreecommitdiffstats
path: root/plugins/TorrentNotification/__init__.py
blob: bd8e3fc03201de5eefa886abc04d42e4fd178afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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 ###

plugin_name = _("Torrent Notification")
plugin_author = "Micah Bucy"
plugin_version = "0.1"
plugin_description = _("Make tray icon blink when torrent finishes downloading \
and/or popup a notification")

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
import deluge.common
import gtk

class TorrentNotification:

    def __init__(self, path, core, interface):
        print "Loading TorrentNotification plugin..."
        import os.path
        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)
        self.core.connect_event(self.core.constants['EVENT_FINISHED'], self.handle_event)

        # Create an options file and try to load existing Values
        self.config_file = deluge.common.CONFIG_DIR + "/notification.conf"
        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("~")})
        try:
            self.config.load()
        except IOError:
            pass
        
        self.glade = gtk.glade.XML(path + "/notification_preferences.glade")
        self.dialog = self.glade.get_widget("dialog")
        self.dialog.set_position(gtk.WIN_POS_CENTER)
        self.glade.signal_autoconnect({
                                        'toggle_ui': self.toggle_ui,
                                        'dialog_ok': self.dialog_ok,
                                        'dialog_cancel': self.dialog_cancel
                                      })
    
    def handle_event(self, event):
        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)
            if self.config.get("enable_sound"):
                self.play_sound()

    def unload(self):
        self.core.disconnect_event(self.core.constants['EVENT_FINISHED'], self.handle_event)
        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):
        if not deluge.common.windows_check():
            import pynotify
            file_info = self.interface.manager.get_torrent_file_info(event['unique_ID'])
            filelist = ""
            for file in file_info[:10]:
                filelist += file['path'] + "\n"
            if len(file_info) > 10:
                filelist += '...'
               
            if pynotify.init("Deluge"):
                n = pynotify.Notification(_("Torrent complete"), 
                    _("Files") + ":\n" + filelist)
                n.set_icon_from_pixbuf(deluge.common.get_logo(48))
                n.show()
        else:
            pass

    def configure(self, window):
        import os.path
        self.glade.get_widget("chk_tray_blink").set_active(self.config.get("enable_tray_blink"))
        self.glade.get_widget("chk_notification").set_active(self.config.get("enable_notification"))
        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"))
        self.dialog.set_transient_for(window)
        self.dialog.show()

    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):
        self.dialog.hide()

    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):
        if not deluge.common.windows_check():
            import pygame
            import os.path
            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:'
        else:
            pass