aboutsummaryrefslogtreecommitdiff
path: root/src/book_detail.py
blob: 168a21290db515f415edb01309be9421edc7524b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk
import os
import subprocess

import src.utils as utils
import src.book_files as book_files

class BookDetail(Gtk.Window):

    def __init__(self, parent_window, library, book_id, data):
        Gtk.Window.__init__(self)

        utils.configure_dialog(self, parent_window, data['title'], height=800)

        scrolled_window = Gtk.ScrolledWindow()
        self.set_child(scrolled_window)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20)
        scrolled_window.set_child(box)
        utils.set_margin(box, 20)

        if has_info(data, 'subtitle'):
            box.append(utils.label(data['subtitle']))

        if has_info(data, 'year'):
            box.append(utils.label(str(data['year'])))

        if len(data['authors']):
            box.append(utils.label(', '.join(data['authors'])))

        if len(data['genres']):
            box.append(utils.label(', '.join(data['genres'])))

        picture = Gtk.Picture.new_for_filename(f'{library}/{book_id}/cover.png')
        picture.set_can_shrink(False)
        box.append(picture)

        box.append(utils.label(data['summary']))

        for path in book_files.get(library, book_id):
            box.append(book_line(path))

def has_info(data, key):
    return key in data and data[key]

def book_line(path):
    box = Gtk.Box(spacing=20)
    box.append(utils.label(os.path.basename(path)))
    open_button = Gtk.Button(label='Ouvrir')
    open_button.connect('clicked', lambda _: open_book(path))
    box.append(open_button)
    return box

def open_book(path):
    subprocess.run(['xdg-open', path])