示例#1
0
    def __init__(self, options, files):
        self.options = options
        self.files = files

        self.window = Gtk.Window(title='Gallery3 Uploader')
        self.window.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
        self.window.set_border_width(6)
        self.window.connect('delete-event', self.cancel_and_quit)
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.window.add(vbox)

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
        vbox.pack_start(hbox, False, False, 0)

        self.image = Gtk.Image()
        self.image.set_size_request(200, 200)
        hbox.pack_start(self.image, False, False, 0)

        self.progressbar = Gtk.ProgressBar()
        self.progressbar.set_size_request(400, 40)
        self.progressbar.set_show_text(True)
        hbox.pack_start(self.progressbar, False, False, 0)
        
        sep = Gtk.Separator()
        sep.orientation = Gtk.Orientation.HORIZONTAL
        vbox.pack_start(sep, False, False, 0)

        self.button = Gtk.Button(stock=Gtk.STOCK_CANCEL)
        self.button.connect('clicked', self.cancel_and_quit)
        vbox.pack_start(self.button, False, False, 0)

        g3 = Gallery3(options['host'], options['path'])
        g3.authenticate(options['username'], options['password'])
        self.uploader = UploaderThread(self, g3, files, int(options['size']))
        self.uploader.start()
        self.window.show_all()
示例#2
0
class G3Uploader:

    def __init__(self, options, files):
        self.options = options
        self.files = files

        self.window = Gtk.Window(title='Gallery3 Uploader')
        self.window.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
        self.window.set_border_width(6)
        self.window.connect('delete-event', self.cancel_and_quit)
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.window.add(vbox)

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
        vbox.pack_start(hbox, False, False, 0)

        self.image = Gtk.Image()
        self.image.set_size_request(200, 200)
        hbox.pack_start(self.image, False, False, 0)

        self.progressbar = Gtk.ProgressBar()
        self.progressbar.set_size_request(400, 40)
        self.progressbar.set_show_text(True)
        hbox.pack_start(self.progressbar, False, False, 0)
        
        sep = Gtk.Separator()
        sep.orientation = Gtk.Orientation.HORIZONTAL
        vbox.pack_start(sep, False, False, 0)

        self.button = Gtk.Button(stock=Gtk.STOCK_CANCEL)
        self.button.connect('clicked', self.cancel_and_quit)
        vbox.pack_start(self.button, False, False, 0)

        g3 = Gallery3(options['host'], options['path'])
        g3.authenticate(options['username'], options['password'])
        self.uploader = UploaderThread(self, g3, files, int(options['size']))
        self.uploader.start()
        self.window.show_all()

    def picture_info(self, arg):
        c = arg.split('|')
        file = c[0]
        title = c[1]
        subject = c[2]
        palbum = c[3]
        return file, title, subject, palbum


    def update_uploading_info(self, file, i, title, subject, palbum, tries):
        if tries == 1:
            message = _('Uploading {0} ({1} of {2})\n'
                        '"{3}"\nFrom the album "{4}".\n'
                        'Into the parent album "{5}".'
                        ).format(os.path.basename(file),
                                 i, len(self.files),
                                 title, subject, palbum)
        else:
            message = _('Uploading {0} ({1} of {2})\n'
                        'There was a timeout in the connection.\n'
                        'This is the try number {3}.'
                        ).format(os.path.basename(file), i,
                                 len(self.files), tries)
        fraction = float(i-1) / len(self.files)
        self.show_uploading(file, message, fraction)
        return False


    def report_error(self, state, args):
        if state == UploaderError.ERROR_DUPLICATED:
            if len(args) == 2:
                self.duplicated_album(args[0], args[1])
            else:
                self.duplicated_album(args[0])
        elif state == UploaderError.ERROR_ALBUM:
            self.album_error(args[0], args[1], args[2])
        elif state == UploaderError.ERROR_MISSING_TAGS:
            self.missing_tags_error(args[0], args[1:])
        return False


    def missing_tags_error(self, file, tags):
        dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.ERROR,
                                   Gtk.ButtonsType.OK,
                                   _('Missing tags'))
        message = _('The file {0} has the following missing tags:').format(file)
        for tag in tags:
            message += '\n   <b><i>' + tag + '</i></b>'
        message += '\n'
        message += _('Please fix it. The program will terminate.')
        dialog.format_secondary_markup(message)
        dialog.connect('response', self.cancel_and_quit)
        dialog.show_all()
        return False


    def album_error(self, year, month, subject):
        dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.Error,
                                   Gtk.ButtonsType.OK,
                                   _('Album error'))
        message = _('There was a problem creating the album\n'
                    '{0}/{1}/{2}\nThe program will terminate.'
                    ).format(year, month, subject)
        dialog.format_secondary_text(message)
        dialog.connect('response', self.cancel_and_quit)
        dialog.show_all()
        return False


    def duplicated_album(self, album, palbum=None):
        dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.ERROR,
                                   Gtk.ButtonsType.OK,
                                   _('Duplicated album'))
        if palbum == None:
            message = (_('The top album "{0}" is duplicated.\n'
                         'The program will terminate.')).format(album)
        else:
            message = _('The album "{0}" under\n"{1}" is duplicated.\n'
                        'The program will terminate.') % (album, palbum)

        dialog.format_secondary_text(message)
        dialog.connect('response', self.cancel_and_quit)
        dialog.show_all()
        return False


    def done_uploading(self):
        self.progressbar.set_text('')
        self.progressbar.set_fraction(1.0)
        dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
                                   Gtk.ButtonsType.OK,
                                   _('Upload successful'))
        dialog.format_secondary_text(_('A total of {0} pictures were uploaded.'
                                       ).format(len(self.files)))
        dialog.connect('response', self.cancel_and_quit)
        dialog.show_all()
        return False


    def show_uploading(self, file, message, fraction):
        md = GExiv2.Metadata(file)
        pixbuf = GdkPixbuf.Pixbuf.new_from_file(file);
        if md.get_tag_long("Exif.Image.Orientation") == 3:
            pixbuf = pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.UPSIDEDOWN)
        if md.get_tag_long("Exif.Image.Orientation") == 6:
            pixbuf = pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
        if md.get_tag_long("Exif.Image.Orientation") == 8:
            pixbuf = pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
        sc = 200.0 / max(pixbuf.get_width(), pixbuf.get_height())
        pixbuf = pixbuf.scale_simple(int(pixbuf.get_width()*sc),
                                     int(pixbuf.get_height()*sc),
                                     GdkPixbuf.InterpType.HYPER)
        self.image.set_from_pixbuf(pixbuf)
        self.progressbar.set_text(message)
        self.progressbar.set_fraction(fraction)


    def cancel_and_quit(self, window=None, data=None):
        self.uploader.cancel()
        Gtk.main_quit()