import sys, pygtk, os
pygtk.require('2.0')
import gtk
from hachoir_core.cmd_line import unicodeFilename
from hachoir_parser import createParser
from hachoir_metadata import extractMetadata
from hachoir_metadata.metadata import MultipleMetadata

class Gui:
    def __init__(self):
        self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.main_window.set_border_width(5)
        self.main_window.connect("destroy", self._destroy)

        self.main_vbox = gtk.VBox()

        self.select_hbox = gtk.HBox()
        self.select_button = gtk.Button("Select")
        self.select_button.connect("clicked", self._select_clicked)
        self.select_hbox.pack_start(self.select_button, False)
        self.file_combo = gtk.combo_box_new_text()
        self.file_combo.connect("changed", self._file_combo_changed)
        self.select_hbox.pack_start(self.file_combo)
        self.main_vbox.pack_start(self.select_hbox, False)

        self.metadata_table = gtk.Table(1, 1)
        self.metadata_table.attach(gtk.Label("Select a file to view metadata information..."), 0, 1, 0, 1)
        self.main_vbox.pack_start(self.metadata_table)

        self.main_window.add(self.main_vbox)
        self.main_window.show_all()

    def add_file(self, filename):
        self.file_combo.append_text(filename)

    def _select_clicked(self, widget):
        file_chooser = gtk.FileChooserDialog("Ouvrir..", None,
            gtk.FILE_CHOOSER_ACTION_OPEN,
            (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
            gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        file_chooser.set_default_response(gtk.RESPONSE_OK)
        file_chooser.show()

        reponse = file_chooser.run()
        if reponse == gtk.RESPONSE_OK:
            selected_file = file_chooser.get_filename()
            self.add_file(selected_file)
        file_chooser.destroy()

    def _file_combo_changed(self, widget):
        self.main_vbox.remove(self.metadata_table)

        real_filename = self.file_combo.get_active_text()
        filename = unicodeFilename(real_filename)
        parser = createParser(filename, real_filename=real_filename)
        metadata = extractMetadata(parser)

        self.metadata_table = gtk.Table(1, 2)
        self.main_vbox.pack_start(self.metadata_table)

        if metadata is None:
                self.metadata_table.attach(gtk.Label("Unknown file format"), 0, 1, 0, 1)
        else:
            total = 1
            for data in sorted(metadata):
                if not data.values:
                    continue
                title = data.description
                for item in data.values:
                    self.metadata_table.resize(total, 2)
                    value = item.text
                    self.metadata_table.attach(gtk.Label(title + ":"), 0, 1, total-1, total)
                    self.metadata_table.attach(gtk.Label(value), 1, 2, total-1, total)
                    total += 1
        self.metadata_table.show_all()

    def _destroy(self, widget, data=None):
        gtk.main_quit()

    def main(self):
        has_file = False
        for arg in sys.argv[1:]:
            if os.path.isdir(arg):
                for file in os.listdir(arg):
                    path = os.path.join(arg, file)
                    if os.path.isfile(path):
                        self.add_file(path)
                        has_file = True
            elif os.path.isfile(arg):
                self.add_file(arg)
                has_file = True
        if has_file:
            self.file_combo.set_active(0)
        gtk.main()

if __name__ == "__main__":
    Gui().main()

