import pygtk
#pygtk.require("2.4")
from gtk import *
from gtk.glade import *
import gobject


class FileChoose(object):
    def __init__(self, wt):
        self.filechooser = wt.get_widget("filechooser")
        self.allFilter = FileFilter()
        self.allFilter.add_pattern("*")
        self.allFilter.set_name("All Files")
        self.filechooser.add_filter(self.allFilter)

    def run(self, action, title, multiple = False, filters = None):
        self.filechooser.set_action(action)
        self.filechooser.set_select_multiple(multiple)
        self.filechooser.set_title(title)
        if filters != None:
            for x in filters:
                self.filechooser.add_filter(x)
                self.filechooser.set_filter(x)
        response = self.filechooser.run()
        self.filechooser.hide()
        if filters != None:
            for x in filters:
                self.filechooser.remove_filter(x)

        if response == RESPONSE_OK:
            retval = None
            if multiple:
                retval = self.filechooser.get_filenames()
            else:
                retval = self.filechooser.get_filename()

            return retval
        else:
            return None
        


def do_file_chooser():
    wt = XML('fc.glade')
    fc = FileChoose(wt)
    filename = fc.run(FILE_CHOOSER_ACTION_SELECT_FOLDER, "Select folder to open saved results")
    if filename != None:
        print filename

    else:
        print "User clicked Cancel"



if __name__ == "__main__":
    do_file_chooser()
