notification.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2008 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 # An example for this script was taken from:
00022 # John Dong <jdong@ubuntu.com>
00023 #
00024 # He states:
00025 # "Feel free to use/tweak/modify to your free will."
00026 # 
00027 
00028 import gtk
00029 import gobject
00030 try:
00031     import pynotify
00032     NOTIFICATIONS = True
00033 except ImportError:
00034     print "Module 'pynotify' not found"
00035     print "Not using notifications"
00036     NOTIFICATIONS = False
00037 
00038 import os
00039 import functions
00040 
00041 class TrayMenu(gtk.Menu):
00042     def __init__(self, parent):
00043         self.main = parent
00044         gtk.Menu.__init__(self)
00045 
00046         self.play_button_status = "play"
00047         self.draw_menu_items()
00048 
00049     def draw_menu_items(self, play=None):
00050         self.mnuShow = gtk.ImageMenuItem(_("Show Pyjama"))
00051         w, h = gtk.icon_size_lookup(gtk.ICON_SIZE_MENU)
00052         image = "pyjama.png"
00053         img = gtk.Image()
00054         if not os.path.exists(image):
00055             image = os.path.join(functions.install_dir(), "images", image)
00056             if not os.path.exists(image):
00057                 print ("Image not found")
00058             else:
00059                 pix = gtk.gdk.pixbuf_new_from_file_at_size(image, w, h)
00060                 img.set_from_pixbuf(pix)
00061                 self.mnuShow.set_image(img)
00062         self.mnuShow.connect('activate', self.main.window.show_window)
00063 
00064 
00065         self.mnuPlay = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY)
00066         self.mnuPlay.connect('activate', self.main.window.on_bPlay_clicked)
00067 
00068 
00069         self.mnuNext = gtk.ImageMenuItem(gtk.STOCK_MEDIA_NEXT)
00070         self.mnuNext.connect('activate', self.main.window.on_bNext_clicked)
00071 
00072         self.mnuPrev = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PREVIOUS)
00073         self.mnuPrev.connect('activate', self.main.window.on_bPrev_clicked)
00074 
00075         self.mnuQuit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
00076         self.mnuQuit.connect('activate', self.main.window.really_quit)
00077 
00078 
00079         self.append(self.mnuShow)
00080         self.append(self.mnuPlay)
00081         self.append(self.mnuNext)
00082         self.append(self.mnuPrev)
00083         self.append(self.mnuQuit)
00084         
00085     def switch_play_button(self, play):
00086         self.play_button_status = play
00087         self.mnuPlay.destroy()
00088         if play=="play":
00089             self.mnuPlay = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY)
00090             self.mnuPlay.connect('activate', self.main.window.on_bPlay_clicked)
00091         else:
00092             self.mnuPlay = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PAUSE)
00093             self.mnuPlay.connect('activate', self.main.window.on_bPlay_clicked)
00094 
00095         self.insert(self.mnuPlay, 1)
00096 
00097 
00098 class TrayIcon():
00099     def __init__(self, parent, title = "Pyjama", text = "Python Jamendo Audiocenter"):
00100         self.parent = parent
00101         self.menu = TrayMenu(parent)
00102         if NOTIFICATIONS:
00103             pynotify.init("pynotify")
00104         self.notify = None
00105 #        self.icon=gtk.status_icon_new_from_icon_name("warning")
00106         self.icon = gtk.status_icon_new_from_file(os.path.join(functions.install_dir(), "images", "pyjama.png"))
00107         self.icon.set_visible(False)
00108 #        self.icon.connect('activate', self.parent.switch_window_state)
00109         self.icon.connect('activate', self.parent.window.show_window)
00110         self.icon.connect('popup-menu', self.cb_popup_menu, self.menu)
00111         self.icon.set_tooltip("%s\n%s" % (title, text))
00112         self.icon.set_visible(True)
00113         
00114 
00115     def cb_popup_menu(self, widget, button, time, event):
00116         event.show_all()
00117         event.popup(None, None, None, 3, time)
00118 
00119 
00120 
00121 
00122     def show_notification(self, caption, text, img = "pyjama", size = None):
00123         if not NOTIFICATIONS: return
00124         if size == None: size = self.parent.settings.get_value("PYJAMA", "notification_cover_size")
00125         pixbuf = None
00126         if img != "pyjama":
00127             pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(img, size, size)
00128             if not self.notify:
00129                 self.notify = pynotify.Notification(caption, text)
00130             else:
00131                 self.notify.update(caption, text)
00132             self.notify.set_icon_from_pixbuf(pixbuf)
00133         else:
00134             if not self.notify:
00135                 self.notify = pynotify.Notification(caption, text, img)
00136             else:
00137                 self.notify.update(caption, text, img)
00138         self.notify.set_urgency(pynotify.URGENCY_NORMAL)
00139         self.notify.attach_to_status_icon(self.icon)
00140         self.notify.set_timeout(self.parent.settings.get_value("PYJAMA", "NOTIFICATION_DELAY"))
00141         self.notify.show()

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8