Source

gSharkDown / lib / guihelpers.py

import gtk
import os
import sys
import locale
import gettext
import glib
try:
    import appindicator
    HAVE_INDICATOR = True
except ImportError:
    print "You don't have python-appindicator installed!"
    print "StautsIcon will be used instead"
    HAVE_INDICATOR = False

from gettext import gettext as _

BASEPATH = os.path.abspath(os.path.dirname(sys.argv[0]).replace('locale/', ''))

APP = 'gsharkdown'
if os.path.exists("locale"):
    DIR = "%s/locale" % BASEPATH
else:
    DIR = os.path.join(sys.prefix, 'share', 'locale')

try:
    locale.setlocale(locale.LC_ALL, '')
except locale.Error:
    locale.setlocale(locale.LC_ALL, 'en_US.utf8')
locale.bindtextdomain(APP, DIR)
gettext.bindtextdomain(APP, DIR)
gettext.textdomain(APP)
gettext.install(APP, localedir = DIR, unicode = True)

class ErrorMessage(gtk.MessageDialog):
    """
    Error Message helper. Displays an Error message whit given
    text content. Used to be shown in Threads
    """

    def __init__(self, parent, message):
        """
        ErrorMessage accepts the the following parameter
        @param parent : The parent window of the error message
        @param message : The error message to be shown
        """
        gtk.MessageDialog.__init__(self, parent,
                            gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                            gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, message)
        self.set_default_response(gtk.RESPONSE_OK)
        self.set_title(_("Error"))
        self.connect('response', self.handle_clicked)

    def handle_clicked(self, *args):
        """
        Handler for the dedault dialog response.
        """
        self.destroy()

class InfoDialog(gtk.MessageDialog):
    """
    Informations dialog helper. Displays Information messages
    with given text.
    """

    def __init__(self, parent, message):
        """
        InfoDialog accepts the following parameters
        @param parent : The parent window of the Infor dialog
        @param message : The message to be shown
        """
        gtk.MessageDialog.__init__(self, parent,
                            gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                            gtk.MESSAGE_INFO, gtk.BUTTONS_OK, message)
        self.set_default_response(gtk.RESPONSE_OK)
        self.connect('response', self.handle_clicked)

    def handle_clicked(self, *args):
        """
        Handler of the default dialog response
        """
        self.destroy()

class UpdateDialog(gtk.MessageDialog):
    """
    Update dialog helper. Displays information when new version of
    gSharkDown is available.
    """
    def __init__(self, title, message, secondary):
        """
        Accepted parameters:
        @param title : The title of the update box
        @param message : The message of the update dialog
        @param secondary : The secondary message of the update dialog
        """
        gtk.MessageDialog.__init__(self, None,
                                   gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                                   gtk.MESSAGE_INFO, gtk.BUTTONS_OK)
        self.set_title(title)
        self.set_markup(message)
        self.format_secondary_markup(secondary)
        self.set_default_response(gtk.RESPONSE_OK)
        self.connect('response', self.handle_clicked)

    def handle_clicked(self, *args):
        """
        Handler to quit the update dialog
        """
        self.destroy()


class LyricsDialog:
    """
    Lyrics Dialog to be shown in a Thread. requieres the
    .ui file, the name of the artist and song, and the
    lyrics itself.
    """

    def __init__(self, _uifile, _artist, _song, _lyrics):
        """
        Accepted parameters in the Lyrics dialog
        @param _uifile : the path to the .ui file for the dialog
        @param _artist : The Artist string to search for
        @param _song : The Song title string to search for
        @param _lyrics : The lyrics as string to be shown in the dialog
        """
        self.builder = gtk.Builder()
        self.builder.add_from_file(_uifile)
        self.lyrics = self.builder.get_object('dialog1')
        self.songlabel = self.builder.get_object('label2')
        self.lyrics_view = self.builder.get_object('textview1')
        self.lyrics_buffer = self.lyrics_view.get_buffer()
        self.lyrics.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK)
        self.lyrics.set_default_response(gtk.RESPONSE_OK)
        self.lyrics.connect('response', self.handle_clicked)
        song_title = glib.markup_escape_text(_artist) + " - "
        song_title += glib.markup_escape_text(_song)

        self.songlabel.set_text(song_title)
        self.lyrics_buffer.set_text(glib.markup_escape_text(_lyrics))

    def show_all(self):
        """
        Wrap the default function of show_all for a dialog
        """
        self.lyrics.show_all()

    def handle_clicked(self, *args):
        """
        Handler for the default dialog response
        """
        self.lyrics.destroy()

class GsharkIndicator:
    """
    AppIndicator for gSharkDown
    """

    def __init__(self, instance):
        """
        instance parameter is the main windows class for gSharkDown,
        so we can access the gSharkDown methods.
        """
        if HAVE_INDICATOR == True:
            if os.path.exists('/usr/share/pixmaps/gsharkdown'):
                icons_path = '/usr/share/pixmaps/gsharkdown'
            else:
                icons_path = "%s/_pixmaps/gsharkdown" % BASEPATH
            self.ind = appindicator.Indicator("gsharkdown-client",
                                    "gsharkdown-messages",
                                    appindicator.CATEGORY_APPLICATION_STATUS,
                                    icons_path)
            self.ind.set_status(appindicator.STATUS_ACTIVE)
            self.ind.set_attention_icon("gsharkdown_16_playing")
            self.ind.set_icon("gsharkdown_16")

            # Create the menu for the appindicator
            menu = gtk.Menu()

            # New menu entry to handle show/hide main window
            show_window = gtk.CheckMenuItem(_("Hide gSharkDown"))
            show_window.connect("toggled", instance.icon_clicked)

            about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
            about.connect("activate", instance.show_about)
            prefs = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
            prefs.connect("activate", instance.edit_preferences)
            self.playbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY)
            self.playbut.connect("activate", instance.play_selected)
            prebut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PREVIOUS)
            prebut.connect("activate", instance.play_previous)
            ffbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_NEXT)
            ffbut.connect("activate", instance.play_next)

            updimg = gtk.Image()
            updimg.set_from_icon_name('system-software-update',
                                     gtk.ICON_SIZE_MENU)
            updbut = gtk.ImageMenuItem(_("Check for update"))
            updbut.set_image(updimg)
            updbut.connect("activate", instance.check_for_update)
            quiter = gtk.ImageMenuItem(gtk.STOCK_QUIT)
            quiter.connect("activate", instance.quit_app)

            menu.append(show_window)
            menu.append(gtk.SeparatorMenuItem())
            menu.append(prebut)
            menu.append(self.playbut)
            menu.append(ffbut)
            menu.append(gtk.SeparatorMenuItem())
            menu.append(about)
            menu.append(updbut)
            menu.append(prefs)
            menu.append(gtk.SeparatorMenuItem())
            menu.append(quiter)
            menu.show_all()

            self.ind.set_menu(menu)
        else:
            self.instance = instance
            self.staticon = gtk.StatusIcon()
            self.staticon.set_from_file("%s/data/gsharkdown_16.png" % BASEPATH)
            self.staticon.connect("popup-menu", self.right_click_event)
            self.staticon.connect("activate", self.icon_clicked)
            self.staticon.set_tooltip(_("gSharkDown :: Player and Downloader"))

    def right_click_event(self, icon, button, time):
        '''
        Handler for the right click event on the StatIcon
        Not usable for the AppIndicator
        '''
        menu = gtk.Menu()

        about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
        about.connect("activate", self.instance.show_about)
        prefs = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
        prefs.connect("activate", self.instance.edit_preferences)
        playbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY)
        playbut.connect("activate", self.instance.play_selected)
        prebut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PREVIOUS)
        prebut.connect("activate", self.instance.play_previous)
        ffbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_NEXT)
        ffbut.connect("activate", self.instance.play_next)

        updimg = gtk.Image()
        updimg.set_from_icon_name('system-software-update',
                                 gtk.ICON_SIZE_MENU)
        updbut = gtk.ImageMenuItem(_("Check for update"))
        updbut.set_image(updimg)
        updbut.connect("activate", self.instance.check_for_update)
        quiter = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        quiter.connect("activate", self.instance.quit_app)

        menu.append(prebut)
        menu.append(playbut)
        menu.append(ffbut)
        menu.append(gtk.SeparatorMenuItem())
        menu.append(about)
        menu.append(updbut)
        menu.append(prefs)
        menu.append(gtk.SeparatorMenuItem())
        menu.append(quiter)
        menu.show_all()

        menu.popup(None, None, gtk.status_icon_position_menu,
                  button, time, icon)

    def icon_clicked(self, widget):
        """
        Handler for a click event on the StatIcon
        Not usable for the AppIndicator
        """
        if(self.instance.windowstate == 0):
            self.instance.window.show_all()
            self.instance.windowstate = 1
        else:
            self.instance.window.hide_on_delete()
            self.instance.windowstate = 0
            return True

    def change_status_playing(self):
        # Method to change the gSharkDown icon when playing
        if HAVE_INDICATOR == True:
            self.ind.set_status(appindicator.STATUS_ATTENTION)
        else:
            self.staticon.set_from_file("%s/data/gsharkdown_16.png" % BASEPATH)

    def change_status_stopped(self):
        # Method to change the gSharkDown icon to default
        # when not playing anything.
        if HAVE_INDICATOR == True:
            self.ind.set_status(appindicator.STATUS_ACTIVE)
        else:
            self.staticon.set_from_file(
                "%s/data/gsharkdown_16_playing.png" % BASEPATH)

    def change_playbutton(self, image):
        if HAVE_INDICATOR == True:
            self.playbut.set_image(image)