#!/usr/bin/python

#Fat and Dirty Submenu applet
#David Samblas Martinez
#More info about all this FDOM stuff in: 
#http://wiki.openmoko.org/wiki/FDOM_-_a_Fat_and_Dirty_OM_based_distribution 
#Under GPL licence
from xdg.DesktopEntry import *
import pygtk
pygtk.require('2.0')
import gtk, os, sys, time


class FDSubmenu():
    def destroy(self, widget):
        gtk.main_quit()

    def __init__(self,path,sbname):
        # Create a new dialog window for the scrolled window to be
        # packed into. 
        window = gtk.Dialog()
        window.connect("destroy", self.destroy)
        window.set_title(sbname)
        window.set_border_width(0)
        window.set_size_request(480, 700)

        # create a new scrolled window.
	try:
            import mokoui
            scrolled_window = mokoui.FingerScroll()
	except:
            scrolled_window = gtk.ScrolledWindow()
            # the policy is one of POLICY AUTOMATIC, or POLICY_ALWAYS.
            # POLICY_AUTOMATIC will automatically decide whether you need
            # scrollbars, whereas POLICY_ALWAYS will always leave the scrollbars
            # there. The first one is the horizontal scrollbar, the second, the
            # vertical.
            scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)

        scrolled_window.set_border_width(5)


        # The dialog window is created with a vbox packed into it.
        window.vbox.pack_start(scrolled_window, True, True, 0)
        scrolled_window.show()
    
        # create a table of 10 by 10 squares.
        vbox_main = gtk.VBox(False,0)
	
        
        # pack the the vbox into the scrolled window
        scrolled_window.add_with_viewport(vbox_main)
        
        # look in to the given dir and genrate a buton for each desktop file found
        entry = DesktopEntry()
        ls = os.listdir(path)
        for file in ls:
                if not(os.path.isdir(file)):
                  entry.parse(os.path.join(path, file))
                  buffer =  entry.getName()
		  bufferIcon =  entry.getIcon()
		  
		  		  
		  if '/usr/share/' in bufferIcon:
		    bufferIcon=bufferIcon
		  else:
		    #categories 
		    if '-' in bufferIcon:
		      bufferIcon='/usr/share/icons/openmoko-standard/128x128/categories/' + bufferIcon + '.png'
		    else:
		      bufferIcon='/usr/share/pixmaps/'+ bufferIcon
		  
		  if 'png' in bufferIcon:
		    bufferIcon=bufferIcon
		  else:
		    bufferIcon=bufferIcon+ '.png'
		  
		   
                  button = gtk.Button(buffer)
		  icon = gtk.Image()
		  #button.set_size_request(100,54)
		  button.set_size_request(100,84)
		  button.set_image(icon)
		  icon.set_from_file(bufferIcon)
                  button.connect_object("clicked",self.execandgo,entry.getExec())
		  vbox_main.pack_start(button, True, True, 0)

	 
                  button.show()
		  icon.show()
        vbox_main.show()          
        # Add a "close" button to the bottom of the dialog
        button = gtk.Button("close")
        button.connect_object("clicked", self.destroy, window)

        # this makes it so the button is the default.
        button.set_flags(gtk.CAN_DEFAULT)
        window.action_area.pack_start( button, True, True, 0)

        # This grabs this button to be the default button. Simply hitting
        # the "Enter" key will cause this button to activate.
        button.grab_default()
        button.show()
        window.show()
        
    def execandgo(self, strcommand):
    #the intetions is to autoclose de submenu once the app is launche but right now it only lauch the app.
    # ptt makes it closing
        pid = os.fork()
        if not pid:
            os.system(strcommand + " &")
	time.sleep(5)
        gtk.main_quit()
    
    def destroy(self, widget):
        gtk.main_quit()
    
def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    FDSubmenu(sys.argv[1], sys.argv[2])
    main()

