Esempio n. 1
0
 def add_book(self, path, collection=None):
     """Add the archive at <path> to the library. If <collection> is
     not None, it is the collection that the books should be put in.
     Return True if the book was successfully added (or was already
     added).
     """
     path = os.path.abspath(path)
     name = os.path.basename(path)
     info = archive.get_archive_info(path)
     if info is None:
         return False
     format_, pages, size = info
     thumbnail.get_thumbnail(path, create=True, dst_dir=_cover_dir)
     old = self._con.execute('''SELECT id FROM Book
         WHERE path = ?''', (path,)).fetchone()
     try:
         if old is not None:
             self._con.execute('''UPDATE Book SET
                 name = ?, pages = ?, format = ?, size = ?
                 WHERE path = ?''', (name, pages, format_, size, path))
         else:
             self._con.execute('''INSERT INTO Book
                 (name, path, pages, format, size)
                 VALUES (?, ?, ?, ?, ?)''',
                               (name, path, pages, format_, size))
     except dbapi2.Error:
         print('! Could not add book {} to the library'.format(path))
         return False
     if collection is not None:
         book = self._con.execute('''SELECT id FROM Book
             WHERE path = ?''', (path,)).fetchone()
         self.add_book_to_collection(book, collection)
     return True
Esempio n. 2
0
    def get_thumbnail(self, page=None, width=128, height=128, create=False):
        """Return a thumbnail pixbuf of <page> that fit in a box with
        dimensions <width>x<height>. Return a thumbnail for the current
        page if <page> is None.

        If <create> is True, and <width>x<height> <= 128x128, the
        thumbnail is also stored on disk.
        """
        self._wait_on_page(page)
        path = self.get_path_to_page(page)
        if width <= 128 and height <= 128:
            thumb = thumbnail.get_thumbnail(path, create)
        else:
            try:
                if "gif" not in path[-3:].lower():
                    thumb = gtk.gdk.pixbuf_new_from_file_at_size(path, width, height)
                else:
                    thumb = gtk.gdk.PixbufAnimation(path).get_static_image()
                    src_width = thumb.get_width()
                    src_height = thumb.get_height()
                    if float(src_width) / width > float(src_height) / height:
                        thumb = thumb.scale_simple(width,
                                                   int(max(src_height * width / src_width, 1)), gtk.gdk.INTERP_TILES)
                    else:
                        thumb = thumb.scale_simple(int(max(src_width * height / src_height, 1)),
                                                   height, gtk.gdk.INTERP_TILES)
            except Exception:
                thumb = None
        if thumb is None:
            thumb = self._get_missing_image()
        thumb = image.fit_in_rectangle(thumb, width, height)
        return thumb
Esempio n. 3
0
    def get_thumbnail(self, page=None, width=128, height=128, create=False):
        """Return a thumbnail pixbuf of <page> that fit in a box with
        dimensions <width>x<height>. Return a thumbnail for the current
        page if <page> is None.

        If <create> is True, and <width>x<height> <= 128x128, the
        thumbnail is also stored on disk.
        """
        self._wait_on_page(page)
        path = self.get_path_to_page(page)
        if width <= 128 and height <= 128:
            thumb = thumbnail.get_thumbnail(path, create)
        else:
            try:
                if "gif" not in path[-3:].lower():
                    thumb = gtk.gdk.pixbuf_new_from_file_at_size(path, width, height)
                else:
                    thumb = gtk.gdk.PixbufAnimation(path).get_static_image()
                    src_width = thumb.get_width()
                    src_height = thumb.get_height()
                    if float(src_width) / width > float(src_height) / height:
                        thumb = thumb.scale_simple(width,
                                                   int(max(src_height * width / src_width, 1)), gtk.gdk.INTERP_TILES)
                    else:
                        thumb = thumb.scale_simple(int(max(src_width * height / src_height, 1)),
                                                   height, gtk.gdk.INTERP_TILES)
            except Exception:
                thumb = None
        if thumb is None:
            thumb = self._get_missing_image()
        thumb = image.fit_in_rectangle(thumb, width, height)
        return thumb
Esempio n. 4
0
 def add_extra_image(self, path):
     """Add an imported image (at <path>) to the end of the image list."""
     thumb = thumbnail.get_thumbnail(path, create=False)
     if thumb is None:
         thumb = self.render_icon(gtk.STOCK_MISSING_IMAGE,
                                  gtk.ICON_SIZE_DIALOG)
     thumb = image.fit_in_rectangle(thumb, 67, 100)
     thumb = image.add_border(thumb, 1, 0x555555FF)
     self._liststore.append([thumb, os.path.basename(path), path])
Esempio n. 5
0
 def add_extra_image(self, path):
     """Add an imported image (at <path>) to the end of the image list."""
     thumb = thumbnail.get_thumbnail(path, create=False)
     if thumb is None:
         thumb = self.render_icon(gtk.STOCK_MISSING_IMAGE,
                                  gtk.ICON_SIZE_DIALOG)
     thumb = image.fit_in_rectangle(thumb, 67, 100)
     thumb = image.add_border(thumb, 1, 0x555555FF)
     self._liststore.append([thumb, os.path.basename(path), path])
Esempio n. 6
0
 def get_book_cover(self, book):
     """Return a pixbuf with a thumbnail of the cover of <book>, or
     None if the cover can not be fetched.
     """
     try:
         path = self._con.execute('''SELECT path FROM Book
             WHERE id = ?''', (book,)).fetchone()
     except Exception:
         print('! Non-existant book #{:d}'.format(book))
         return None
     thumb = thumbnail.get_thumbnail(path, create=True, dst_dir=_cover_dir)
     if thumb is None:
         print('! Could not get cover for {}'.format(path))
     return thumb
Esempio n. 7
0
 def add_book(self, path, collection=None):
     """Add the archive at <path> to the library. If <collection> is
     not None, it is the collection that the books should be put in.
     Return True if the book was successfully added (or was already
     added).
     """
     path = os.path.abspath(path)
     name = os.path.basename(path)
     info = archive.get_archive_info(path)
     if info is None:
         return False
     format_, pages, size = info
     thumbnail.get_thumbnail(path, create=True, dst_dir=_cover_dir)
     old = self._con.execute(
         '''SELECT id FROM Book
         WHERE path = ?''', (path, )).fetchone()
     try:
         if old is not None:
             self._con.execute(
                 '''UPDATE Book SET
                 name = ?, pages = ?, format = ?, size = ?
                 WHERE path = ?''', (name, pages, format_, size, path))
         else:
             self._con.execute(
                 '''INSERT INTO Book
                 (name, path, pages, format, size)
                 VALUES (?, ?, ?, ?, ?)''',
                 (name, path, pages, format_, size))
     except dbapi2.Error:
         print('! Could not add book {} to the library'.format(path))
         return False
     if collection is not None:
         book = self._con.execute(
             '''SELECT id FROM Book
             WHERE path = ?''', (path, )).fetchone()
         self.add_book_to_collection(book, collection)
     return True
Esempio n. 8
0
 def get_book_cover(self, book):
     """Return a pixbuf with a thumbnail of the cover of <book>, or
     None if the cover can not be fetched.
     """
     try:
         path = self._con.execute(
             '''SELECT path FROM Book
             WHERE id = ?''', (book, )).fetchone()
     except Exception:
         print('! Non-existant book #{:d}'.format(book))
         return None
     thumb = thumbnail.get_thumbnail(path, create=True, dst_dir=_cover_dir)
     if thumb is None:
         print('! Could not get cover for {}'.format(path))
     return thumb
Esempio n. 9
0
 def _update_preview(self, *args):
     path = self.filechooser.get_preview_filename()
     if path and os.path.isfile(path):
         pixbuf = thumbnail.get_thumbnail(path, prefs['create thumbnails'])
         if pixbuf is None:
             self._preview_image.clear()
             self._namelabel.set_text('')
             self._sizelabel.set_text('')
         else:
             pixbuf = image.add_border(pixbuf, 1)
             self._preview_image.set_from_pixbuf(pixbuf)
             self._namelabel.set_text(encoding.to_unicode(
                     os.path.basename(path)))
             self._sizelabel.set_text(
                     '{:.1f} KiB'.format(os.stat(path).st_size / 1024.0))
     else:
         self._preview_image.clear()
         self._namelabel.set_text('')
         self._sizelabel.set_text('')
Esempio n. 10
0
 def _update_preview(self, *args):
     path = self.filechooser.get_preview_filename()
     if path and os.path.isfile(path):
         pixbuf = thumbnail.get_thumbnail(path, prefs['create thumbnails'])
         if pixbuf is None:
             self._preview_image.clear()
             self._namelabel.set_text('')
             self._sizelabel.set_text('')
         else:
             pixbuf = image.add_border(pixbuf, 1)
             self._preview_image.set_from_pixbuf(pixbuf)
             self._namelabel.set_text(
                 encoding.to_unicode(os.path.basename(path)))
             self._sizelabel.set_text('%.1f KiB' %
                                      (os.stat(path).st_size / 1024.0))
     else:
         self._preview_image.clear()
         self._namelabel.set_text('')
         self._sizelabel.set_text('')