#!/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 = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("destroy", self.destroy)
        window.set_title(sbname)
        window.set_border_width(0)
        ##window.set_size_request(480, 700)
        window.set_size_request(480, 640)

        # 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)

        vbox = gtk.VBox(False,0)
	##vbox.add(scrolled_window)
        ###window.add(scrolled_window)
	vbox.pack_start(scrolled_window, True, True, 0)
        
	# add a dummy button to make others load correctly
	dummyb = gtk.Button()
	dummyb.set_size_request(100,1)
	vbox_main.pack_start(dummyb, False, False, 0)

        # 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)
		  button.set_size_request(100,84)
		  icon = gtk.Image()
		  try:
		    icon.set_from_pixbuf(gtk.gdk.pixbuf_new_from_file_at_size(bufferIcon, 84, 84))
		  except:
		    icon.set_from_file(bufferIcon)
                  button.connect_object("clicked",self.execandgo,entry.getExec())
		  button.set_image(icon)

		  vbox_main.pack_start(button, True, True, 0)
	 
                  button.show()
		  icon.show()

        vbox_main.show()          

        # add close button
        button_exit = gtk.Button("Close")
        button_exit.connect("clicked", self.destroy)
	button_exit.set_size_request(100,80)
	vbox.pack_end(button_exit, False, False, 5)

        window.add(vbox)

	window.show_all()

        
    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()

