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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
# -*- coding: utf-8 -*-
# This file is part of emesene.
#
# Emesene 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 of the License, or
# (at your option) any later version.
#
# emesene 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 emesene; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import gtk
if os.name == 'nt':
import winsound
import Plugin
from emesenecommon import PATH
try:
import gst
GSTREAMER = True
except:
GSTREAMER = False
class Sound:
'''A plugin to play sounds using the available modules on the system'''
def __init__(self, theme):
'''class constructor'''
self.theme = theme
self.beep = False
self.command = ''
self.canPlay = False
self.canGstreamer = False
if os.name == "posix":
self.checkAvailability()
if self.canGstreamer:
self.player = gst.element_factory_make("playbin", "player")
bus = self.player.get_bus()
bus.enable_sync_message_emission()
bus.add_signal_watch()
bus.connect('message', self.gst_on_message)
else:
self.canPlay = True
def gst_on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
self.player.set_state(gst.STATE_NULL)
def checkAvailability(self):
if self.beep:
self.canPlay = True
elif GSTREAMER:
self.canPlay = True
self.canGstreamer = True
elif self.is_on_path('aplay'):
self.canPlay = True
self.command = 'aplay'
elif self.is_on_path('play'):
self.canPlay = True
self.command = 'play'
def play(self, sound_theme, sound):
if self.beep:
gtk.gdk.beep()
return
for theme in (sound_theme, 'default'):
soundPath = PATH.SOUNDS_PATH + os.sep + sound_theme + os.sep + \
sound + ".wav"
if os.path.exists(soundPath):
break
else:
soundPath = ''
if not soundPath:
return
if os.name == "nt":
winsound.PlaySound(soundPath,
winsound.SND_FILENAME | winsound.SND_ASYNC)
elif os.name == "posix":
if self.canGstreamer:
loc = "file://" + soundPath
self.player.set_property('uri', loc)
self.player.set_state(gst.STATE_PLAYING)
else:
os.popen4(self.command + " " + soundPath)
def getCommand(self):
return self.command
def setCommand(self, string):
self.command = string
def is_on_path(self, fname):
for p in os.environ['PATH'].split(os.pathsep):
if os.path.isfile(os.path.join(p, fname)):
return True
class MainClass(Plugin.Plugin):
'''Main plugin class'''
def __init__(self, controller, msn):
'''Contructor'''
Plugin.Plugin.__init__(self, controller, msn)
self.theme = controller.theme
self.description = _('Play sounds for common events.')
self.authors = { 'Mark Baas' : 'mark.baas123 at gmail dot com' }
self.website = 'http://www.emesene.org'
self.displayName = _('Sound')
self.name = 'Sound'
self.sound = Sound(self.theme)
self.config = controller.config
self.config.readPluginConfig(self.name)
self.playOnline = int(self.config.getPluginValue(self.name,
'playOnline', '1'))
self.playMessage = int(self.config.getPluginValue(self.name,
'playMessage', '1') )
self.playNudge = int(self.config.getPluginValue(self.name,
'playNudge', '1'))
self.playInactive = int(self.config.getPluginValue(self.name,
'playInactive', '1'))
self.playSend = int(self.config.getPluginValue(self.name,
'playSend', '0'))
self.disableBusy = int(self.config.getPluginValue(self.name,
'disableBusy', '0'))
self.sound_theme = self.config.getPluginValue(self.name,
'theme', 'default')
self.sound.beep = int(self.config.getPluginValue(self.name,
'beep', '0'))
self.onlineId = None
self.messageId = None
self.nudgeId = None
def start(self):
'''start the plugin'''
self.enabled = True
self.onlineId = self.msn.connect('user-online', self.online)
self.messageId = self.msn.connect('message-received', self.message)
self.nudgeId = self.msn.connect('nudge-received', self.nudge)
self.sendMessageId = self.controller.conversationManager.connect(
'send-message', self.send)
def stop(self):
'''stop the plugin'''
self.msn.disconnect(self.onlineId)
self.msn.disconnect(self.messageId)
self.msn.disconnect(self.nudgeId)
self.msn.disconnect(self.sendMessageId)
self.enabled = False
def action(self):
pass
def check(self):
'''
check if everything is OK to start the plugin
return a tuple whith a boolean and a message
if OK -> (True, 'some message')
else -> (False, 'error message')
'''
if not self.sound.canPlay:
return (False, _('gstreamer, play and aplay not found.'))
return (True, 'Ok')
def online(self, msnp, email, oldStatus):
self.playOnline = int(self.config.getPluginValue(self.name,
'playOnline', '1'))
self.sound_theme = self.config.getPluginValue(self.name, 'theme',
'default')
if oldStatus == 'FLN' and self.playOnline and self.soundsEnabled():
self.sound.play(self.sound_theme, 'online')
def message(self, msnp, email):
self.playMessage = int(self.config.getPluginValue(self.name,
'playMessage', '1') )
self.sound_theme = self.config.getPluginValue(self.name, 'theme',
'default')
if self.playMessage and self.soundsEnabled():
result = self.controller.conversationManager\
.getOpenConversation(email)
if self.playInactive and result != None:
window, conversation = result
windowFocus = window.is_active()
tabFocus = (window.conversation == conversation)
if not (windowFocus and tabFocus):
self.sound.play(self.sound_theme, 'type')
else:
self.sound.play(self.sound_theme, 'type')
def nudge(self, *args):
self.playNudge = int(self.config.getPluginValue(self.name,
'playNudge', '1'))
self.sound_theme = self.config.getPluginValue(self.name,
'theme', 'default')
if self.playNudge and self.soundsEnabled():
self.sound.play(self.sound_theme, 'nudge')
def send(self, *args):
self.playSend = int(self.config.getPluginValue(self.name,
'playSend', '0'))
self.sound_theme = self.config.getPluginValue(self.name,
'theme', 'default')
if self.playSend and self.soundsEnabled():
self.sound.play(self.sound_theme, 'send')
def soundsEnabled(self):
'''checks if sounds are enabled'''
if not self.enabled:
return False
if self.disableBusy and self.controller.contacts.get_status() == 'BSY':
return False
return True
def configure(self):
'''display a configuration dialog'''
l = []
#sound theme
themes = os.listdir(PATH.APP_PATH + os.sep + 'sound_themes')
themes = [x for x in themes if not x.startswith('.')]
# name, optionType, label, description, value, options
l.append(Plugin.Option('theme', list, _('Theme'), '',
self.config.getPluginValue(self.name, 'theme', ''), themes))
l.append(Plugin.Option('playOnline', bool,
_('Play online sound'),
_('Play a sound when someone gets online'),
(self.config.getPluginValue(self.name, 'playOnline', '1') \
== '1')))
l.append(Plugin.Option('playMessage', bool,
_('Play message sound'),
_('Play a sound when someone sends you a message'),
(self.config.getPluginValue(self.name, 'playMessage', '1') \
== '1')))
l.append(Plugin.Option('playNudge', bool,
_('Play nudge sound'),
_('Play a sound when someone sends you a nudge'),
(self.config.getPluginValue(self.name, 'playNudge', '1') \
== '1')))
l.append(Plugin.Option('playSend', bool,
_('Play sound when you send a message'),
_('Play sound when you send a message'),
(self.config.getPluginValue(self.name, 'playSend', '0') \
== '1')))
l.append(Plugin.Option('playInactive', bool,
_('Only play message sound when window is inactive'),
_('Play the message sound only when the window is inactive'),
(self.config.getPluginValue(self.name, 'playInactive', '1') \
== '1')))
l.append(Plugin.Option('disableBusy', bool,
_('Disable sounds when busy'),
_('Disable sounds when busy'),
(self.config.getPluginValue(self.name, 'disableBusy', '1') \
== '1')))
l.append(Plugin.Option('beep', bool,
_('Use system beep'),
_('Play the system beep instead of sound files'),
(self.config.getPluginValue(self.name, 'beep', '0') \
== '1')))
response = Plugin.ConfigWindow(_('Config Sound Plugin'), l).run()
if response != None:
if response.has_key('playOnline'):
self.config.setPluginValue(self.name, 'playOnline',
str(int(response['playOnline'].value)))
if response.has_key('playMessage'):
self.config.setPluginValue(self.name, 'playMessage',
str(int(response['playMessage'].value)))
if response.has_key('playNudge'):
self.config.setPluginValue(self.name, 'playNudge',
str(int(response['playNudge'].value)))
if response.has_key('playInactive'):
self.config.setPluginValue(self.name, 'playInactive',
str(int(response['playInactive'].value)))
if response.has_key('playSend'):
self.config.setPluginValue(self.name, 'playSend',
str(int(response['playSend'].value)))
if response.has_key('beep'):
self.config.setPluginValue(self.name, 'beep',
str(int(response['beep'].value)))
if response.has_key('theme'):
self.config.setPluginValue(self.name, 'theme',
response['theme'].value)
if response.has_key('disableBusy'):
self.config.setPluginValue(self.name, 'disableBusy',
str(int(response['disableBusy'].value)))
self.playOnline = (self.config.getPluginValue(self.name,
'playOnline', '1') == '1')
self.playMessage = (self.config.getPluginValue(self.name,
'playMessage', '1') == '1')
self.playNudge = (self.config.getPluginValue(self.name,
'playNudge', '1') == '1')
self.playInactive = (self.config.getPluginValue(self.name,
'playInactive', '1') == '1')
self.disableBusy = (self.config.getPluginValue(self.name,
'disableBusy', '1') == '1')
self.sound.beep = int(self.config.getPluginValue(self.name,
'beep', '0'))
return True
|