class DoItInBackground(GObject.GObject):
    __gsignals__ = {
        'started': (GObject.SIGNAL_RUN_FIRST, GObject.TYPE_NONE, (int,)),
        'ended': (GObject.SIGNAL_RUN_FIRST, GObject.TYPE_NONE, (bool,)),
        'start_one': (GObject.SIGNAL_RUN_FIRST, GObject.TYPE_NONE, (str,)),
        'end_one': (GObject.SIGNAL_RUN_FIRST, GObject.TYPE_NONE, (float,)),
    }

    def __init__(self, title, parent, elements):
        GObject.GObject.__init__(self)
        self.elements = elements
        self.stopit = False
        self.ok = True
        self.progreso = Progreso(title, parent)
        self.progreso.set_total_size(len(elements))
        self.progreso.connect('i-want-stop', self.stop)
        self.connect('start_one', self.progreso.set_element)
        self.connect('end_one', self.progreso.increase)
        self.connect('ended', self.progreso.close)
        self.downloaders = []

    def emit(self, *args):
        GLib.idle_add(GObject.GObject.emit, self, *args)

    def stop(self, *args):
        if len(self.downloaders) > 0:
            for downloader in self.downloaders:
                downloader.cancel()
                self.emit('end_one', 1.0)

    def run(self):
        try:
            executor = futures.ThreadPoolExecutor()
            for element in self.elements:
                downloader = executor.submit(download, element, self)
                self.downloaders.append(downloader)
            self.progreso.run()
        except Exception as e:
            self.ok = False
            print(e)