#!/usr/bin/env python
#
# applet.py - A GNOME applet that shows what Moosic is currently playing.
#
# Copyright (C) 2003 Daniel Pearson <daniel.pearson@umbc.edu>
# Copyright (C) 2003 Paramjit Oberoi <param@cs.wisc.edu>
#
# 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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import pygtk
pygtk.require('2.0')
import gtk, gnome.applet

import socket, errno, os.path, sys
import moosic_factory

from applet_ui import *
from applet_config import *

# Class representing the applet
# An instance of this class is instantiated for each applet

class MoosicApplet:
    def refresh(self):
        # Make sure that our connection to the server is working properly.
        try: self.moosic.no_op()
        except socket.error, e:
            if e[0] in (errno.ECONNREFUSED, errno.ENOENT):
                self.ui.update_tooltip('[moosicd not running]')
            else:
                self.ui.update_tooltip('[unexpected socket error]')
            return gtk.TRUE

        is_paused = bool(self.moosic.is_paused())
        if self.paused != is_paused:
            self.paused = is_paused
            if (self.paused): self.ui.paused()
            else: self.ui.unpaused()

        current_song = self.moosic.current().data.decode('latin-1').encode('utf-8')
        is_stopped = not bool(current_song)
        if self.stopped != is_stopped:
            self.stopped = is_stopped;
            if self.stopped: self.ui.stopped()
            else: self.ui.playing()

        self.update_song_name(current_song)

        if (self.stopped):
            self.ui.update_tooltip("[nothing playing]")
        elif (self.paused):
            self.ui.update_tooltip("[paused] " + current_song)
        else:
            self.ui.update_tooltip(current_song)

        #if self.moosic.is_looping(): is_looping="1"
        #else: is_looping = "0"
        #self.menu.set_property("/commands/Loop", {"state":is_looping})
        #print self.menu.get_prop("/commands/Loop")

        #pdb.set_trace()

        return gtk.TRUE

    def update_song_name(self, s):
        if s:
            if (self.config.strip_dir): s = os.path.basename(s)
            if (self.config.strip_ext): s = os.path.splitext(s)[0]
            if (self.config.strip_prefix):
                for pfx in self.config.prefixes:
                    n = s.rfind(pfx)
                    if n != -1: s = s[n+len(pfx):]
            if (self.config.replace_underscores): s = s.replace("_"," ")
            s = s.strip()
        else:
            s = "[nothing playing]"

        if self.config.display_qlength:
            qlen = self.moosic.length()
            self.ui.qlen_label.set_text(" %d" % qlen)

        self.ui.label.set_text(s)

    # ---- Utility functions for controlling moosicd

    def play_toggle(self):
        if self.stopped:
            self.moosic.run_queue()
        else:
            self.moosic.stop()

    def pause_toggle(self):
        self.moosic.toggle_pause()

    def prev(self):
        self.moosic.previous()

    def next(self):
        self.moosic.next()

    def sort(self):
        self.moosic.sort()

    def shuffle(self):
        self.moosic.shuffle()

    def qstop(self):
        self.moosic.halt_queue()

    def clear(self):
        self.moosic.clear()

    def wipe(self):
        self.moosic.halt_queue()
        self.moosic.next()
        self.moosic.clear()

    def __init__(self, applet, iid):
        self.stopped = True
        self.paused = False

        self.moosic = moosic_factory.LocalMoosicProxy()
        self.config = MoosicAppletConfig(self)
        self.ui = MoosicAppletUI(applet, self)


def moosic_applet_factory(applet, iid):
    MoosicApplet(applet,iid)
    return gtk.TRUE

# Run as application?
if len(sys.argv) == 2 and sys.argv[1] == "run-moosicapplet-as-application":
    main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    main_window.set_title("Moosic Applet")
    main_window.connect("destroy", gtk.mainquit) # quit the event loop on destruction
    app = gnome.applet.Applet()
    moosic_applet_factory(app, None)
    app.reparent(main_window)
    main_window.show_all()
    gtk.main()

gnome.applet.bonobo_factory("OAFIID:GNOME_MoosicApplet_Factory", gnome.applet.Applet.__gtype__, "hello", "0", moosic_applet_factory)
