#!/usr/bin/env python
# coding=utf-8
#
# GnomePanelApplet.py
#
# Copyright © 2008 Steven Brown <steven.w.j.brown@gmail.com>
#
# This file is part of File List Applet.
#
# FileListApplet 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 3 of the
# License, or (at your option) any later version.

# FileListApplet 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 FileListApplet.  If not, see <http://www.gnu.org/licenses/>.

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

import sys
import gnomeapplet
import gobject
from gettext import gettext as _



class GnomePanelApplet(gnomeapplet.Applet):
    baiid = "OAFIID:GnomePanelApplet_Factory"
    description = "Simple Python Applet"
    version = "0.0"

    def __init__(self, *args, **kwargs):         
        #super(GnomePanelApplet, self).__init__(*args, **kwargs) #causes a runtime error, cannot subclass
        self.__gobject_init__()
        print "GnomePanelApplet __init__" # Never called by the bonobo factory, only when instantiating (for windowed mode)

        
    def applet_factory(self, iid):
        "Dummy method. Override.  BUT be sure to connect your applet to 'destroy'."
        #print "GnomePanelApplet applet_factory()"
        #self.applet = applet

        self.set_applet_flags(gnomeapplet.EXPAND_MINOR)
        self.connect("destroy", self._on_applet_destroy)

        label = gtk.Label("Success");
        self.add(label)
        self.show_all()

        return True
     

    
#     def _connect_signals(self):
#         pass

    # Private UI update methods //
    

#     def _update_popup_menu(self):
#         pass

    #def _update_XXX(self):
    #    pass

    # Applet callbacks //
#     def _on_applet_change_background(self, applet, background_type, color, pixmap):
#         pass

#     def _on_applet_change_orient(self, applet, orient):
#         pass

#     def _on_applet_change_size(self, applet, size):
#         pass

#     def _on_applet_move_focus_out_of_applet(self, applet, direction):
#         pass

    def _cleanup_before_destroy(self):
        "Dummy.  Override with custom pre-shutdown cleanup."
        print "_cleanup_before_destroy()"
    
    def _on_applet_destroy(self, sender, data=None):
        "Clean shutdown here."
        print "_on_applet_destroy()"
        self._cleanup_before_destroy()
        print "applet cleanup"
        del self
        print "finished safe cleanup"


#     def _on_window_destroy(self, sender, data=None):
#         print "on_window_destroy"
#         print "sender: ", sender
#         self._cleanup_before_destroy()
        


    # GConf callbacks
#     def _on_gconf_changed(self, gconf_value, data=None):
#         pass

    # Preferences Dialog callbacks
    #def _on_prefs_XXX_changed

####

gobject.type_register(GnomePanelApplet)

def applet_init(applet, iid):
    print "applet_init()"
    print applet, iid
    return applet.applet_factory(iid)

def run_applet(windowed=False):
    print "run_applet()"
    print "windowed = ",
    if windowed:
            print windowed
            applet = GnomePanelApplet()
            main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
            main_window.set_title("Python Applet")
            main_window.set_default_size(200,24)
            main_window.connect("destroy", gtk.main_quit)
            applet.applet_factory(None)
            applet.reparent(main_window)
            main_window.show_all()
            gtk.gdk.threads_init() # Important
            gtk.main()
            sys.exit()

    else:
        print windowed
        gtk.gdk.threads_init() # Important
        gnomeapplet.bonobo_factory( GnomePanelApplet.baiid, \
                                            GnomePanelApplet.__gtype__, \
                                            GnomePanelApplet.description, \
                                            GnomePanelApplet.version, \
                                            applet_init)
    
if __name__ == "__main__":

    if len(sys.argv) > 2:
        # It's either the bonobo factory, or an error
        if "--oaf-activate-iid="+GnomePanelApplet.baiid in sys.argv:
            run_applet(windowed=False)
        else:
            print "Too many arguments. Either 'window' as an argument or none at all."
            sys.exit()

    elif len(sys.argv) == 2:
        if "window" in sys.argv[1].lower():
            run_applet(windowed=True)
        
        else:
            print "Invalid argument: ", sys.argv[1]
            sys.exit()

    else: #len(sys.argv) == 1:
            run_applet(windowed=False)


