import hal
import subprocess



class HandlerClass:

    def __init__(self, halcomp, builder, useropts):
        # make a class reference to builder 
        # (self.ANYTHING is available anywhere in the HandlerClas class)
        self.builder = builder
        self.halcomp = halcomp

        #create a hal pin to trigger the code in .axisrc to switch tabs to the preview 
        halcomp.newpin('tab-trigger', hal.HAL_BIT, hal.HAL_IO)

        # connect to a signal - hopefully this isn't a race condition and works
        # the signal was made in the .axisrc file
        hal.connect("gladevcp.tab-trigger","preview-request-signal")

        # make a class reference to the open vcp_action
        self.open = builder.get_object('open')

        # change a property in the iconchooser so all programs show
        # this shouldn't be required - it's a bug
        # builder.get_object('file').set_property("filetypes", '*')

    # This is a callback function from the icon chooser selected in the GLADE signal editor
    # in GTK the first variable is always the widget that owned the signal that called this function
    # any other variables depend on the widget, in this case icon view sends the path

    def on_file_selected(self, widget, path):
        if path:
            print path
            subprocess.call('axis-remote %s' % path, shell=True)
            self.halcomp['tab-trigger'] = True
 
# boiler code
def get_handlers(halcomp,builder,useropts):
    return [HandlerClass(halcomp,builder,useropts)] 
