'Set the metadata in the files in the selected book\'s record to
match the current metadata in the database.'

def update_metadata(self):

    from calibre.ebooks.metadata.meta import set_metadata
    from calibre.gui2 import error_dialog, info_dialog
    rows = self.gui.library_view.selectionModel().selectedRows()
    if ((not rows) or (len(rows) == 0)):
        return error_dialog(self.gui, u'Cannot    update    metadata', u'No    books    selected', show=True)
    ids = list(map(self.gui.library_view.model().id, rows))
    db = self.db.new_api
    for book_id in ids:
        mi = db.get_metadata(book_id, get_cover=True, cover_as_data=True)
        fmts = db.formats(book_id)
        if (not fmts):
            continue
        for fmt in fmts:
            fmt = fmt.lower()
            ffile = db.format(book_id, fmt, as_file=True)
            ffile.seek(0)
            set_metadata(ffile, mi, fmt)
            ffile.seek(0)
            db.add_format(book_id, fmt, ffile, run_hooks=False)
    info_dialog(self, u'Updated    files', (u'Updated    the    metadata    in    the    files    of    %d    book(s)' % len(ids)), show=True)
