#!/usr/bin/env python

# 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

from optparse import OptionParser, SUPPRESS_HELP
    
optp = OptionParser(version='MoosicApplet 0.3', usage='%prog [options]')

optp.add_option('-a', '--application', action='store_true',
                  help='run as an application')
optp.add_option('-x', '--xembed', action='store', metavar='ID',
                  help='embed applet into the specified window')
optp.add_option('-p', '--perlpanel', action='store_true',
                  help='embed applet into PerlPanel')

# the following two options are needed by the bonobo factory

# this is a hack.  optparse is really annoying - one, the only documentation
# is a tutorial (which is really nice as a tutorial, but drives one nuts
# when looking for a MANUAL); two - the holier-than-thou attitude which
# says that options must be designed in a particular way, and if you need
# to do something different you are obviously misguided and the library
# will make your life as difficult as possible in the hope that that will
# make you mend your ways.

# anyway, what I really need here is a way to ignore unknown options, and then
# pass those on to bonobo factory.  maybe I should finally figure out getopt...

optp.add_option('--oaf-activate-iid', action='store', help=SUPPRESS_HELP)
optp.add_option('--oaf-ior-fd', action='store', help=SUPPRESS_HELP)

(opts, args) = optp.parse_args()

# these imports are here to reduce the startup time
# "moosic-aplet --help" is unnecessarily slow otherwise

import sys, os.path

import pygtk
pygtk.require('2.0')
import gtk, gnomeapplet

from moosic.client.applet.main import MoosicApplet

base_data_dir = os.path.normpath(os.path.join(__file__,'../../share/moosic-applet'))
def get_data_file_path(name):
    final_name = os.path.join(base_data_dir, name)
    if not os.path.exists(final_name):
        raise IOError('File not Found: ' + final_name)
    return final_name

def moosic_applet_factory(applet, iid):
    MoosicApplet(applet,iid)
    return True


# should check that only one of -a, -x, -p is specified

main_window = None
if opts.application:
    # Run as application
    main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    main_window.set_title("Moosic Applet")
elif opts.xembed:
    # Run within any given window that supports XEMBED.
    try:
        socketid = long(opts.xembed)
    except ValueError, e:
        sys.exit("Error: Invalid window-ID: %s" % opts.xembed)
    main_window = gtk.Plug(socketid)
elif opts.perlpanel:
    # Run within PerlPanel's Slot applet.
    socketid_file = os.path.join(os.getenv('HOME', ''), '.perlpanel', 'socketid')
    if not os.path.exists(socketid_file):
        sys.exit("%s doesn't exist. Is PerlPanel running with the Slot applet active?" % socketid_file)
    try:
        socketid = long(file(socketid_file).read())
    except IOError, e:
        sys.exit('Error: %s' % e)
    except ValueError, e:
        sys.exit("Error: Invalid data in %s" % socketid_file)
    main_window = gtk.Plug(socketid)

if main_window:
    main_window.connect("destroy", gtk.main_quit)
    app = gnomeapplet.Applet()
    moosic_applet_factory(app, None)
    app.reparent(main_window)
    main_window.show_all()
    gtk.main()
else:
    gnomeapplet.bonobo_factory("OAFIID:GNOME_MoosicApplet_Factory",
                                gnomeapplet.Applet.__gtype__,
                                "MoosicApplet", "0", moosic_applet_factory)

