import tempfile
import threading
import gtk
import gio
import gnomevfs
import gobject

gtk.gdk.threads_init()

class GIOCopyThread(threading.Thread):
    def __init__(self, uri):
        threading.Thread.__init__(self)
        self.source = gio.File(uri)
        self.dest = gio.File(tempfile.mktemp())

    def run(self):
        print "GIO START"
        self.source.copy(self.dest)
        print "GIO FINISHED: %s" % self.dest

class GnomeVfsCopyThread(threading.Thread):
    def __init__(self, uri):
        threading.Thread.__init__(self)
        self.source = gnomevfs.URI(uri)
        self.dest = gnomevfs.URI(tempfile.mktemp())

    def run(self):
        print "GnomeVfs START"
        gnomevfs.xfer_uri(
                    source_uri=self.source,
                    target_uri=self.dest,
                    xfer_options=gnomevfs.XFER_NEW_UNIQUE_DIRECTORY,
                    error_mode=gnomevfs.XFER_ERROR_MODE_ABORT,
                    overwrite_mode=gnomevfs.XFER_OVERWRITE_MODE_REPLACE)
        print "GnomeVfs FINISHED: %s" % self.dest


class UI:
    
    URL = "http://files.conduit-project.org/Conduit-0.3.0-screencast-small.ogg"

    def __init__(self):
        w = gtk.Window()
        hb = gtk.HBox()

        btn = gtk.Button("GIO Copy")
        btn.connect("clicked", self._on_gio_copy)
        hb.pack_start(btn)
        btn = gtk.Button("GnomeVfs Copy")
        btn.connect("clicked", self._on_gnomevfs_copy)
        hb.pack_start(btn)

        w.add(hb)
        w.show_all()

        gobject.timeout_add(300,self._tick)

    def _on_gio_copy(self, btn):
        t = GIOCopyThread(self.URL)
        t.start()

    def _on_gnomevfs_copy(self, btn):
        t = GnomeVfsCopyThread(self.URL)
        t.start()


    def _tick(self):
        print "."
        return True

    def main(self):
        gtk.main()

if __name__ == "__main__":
    u = UI()
    u.main()


