Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-photo

Source of release 0.1.0

0.1.0 / src / photo_library.luc

## The library the window shows: the bundle on disk, its catalog, the index of
## photos the panes read, the develop presets, and the import that may be running. The panes never
## touch the catalog themselves; they ask the library.
from luce_prism import prism
from luce_ui.ui import Notifier
from luce_std import files
from luce_std import paths
import library
from photo_index import PhotoIndex
import photo_index
from importer import Importer
import importer
from presets import PresetList
import presets
import catalog
from folders import FolderList
from luce_ptp.ptp import Watcher
import folders

## An import was asked for while another runs.
pub let busy = ErrorCode.package(20)

## The bundle luced-photo opens unless told otherwise.
pub func default_root() -> str!:
    return paths.join(paths.join(files.home_directory(), "Pictures"), "Luced Photo.library")

pub class PhotoLibrary:
    pub var root: str
    pub var store: prism.Store
    pub var index: PhotoIndex
    ## The develop presets, by name; read again after every change.
    pub var presets: PresetList
    ## The library's folders; read again after every change.
    pub var folders: FolderList
    var running: Importer? = none
    ## Told of an import's progress, so the window takes it (none: poll it).
    pub var import_notifier: Notifier? = none
    ## The folder shown (0: every photo), and it with its subfolders' ids.
    pub var folder: int = 0
    var shown_folders: list[int] = []
    ## The filter the index applies (PhotoIndex.filter).
    var minimum_rating: int = 0
    var flags: int = 0
    var label: int = 0
    ## What the search field holds: photos whose name, camera, lens, date or
    ## preset's name hold it (ignoring case).
    var search: str = ""

    pub func init(self, root: str) -> unit!:
        self.root = root
        library.ensure(root)
        self.store = prism.Store.open(library.catalog_path(root))
        self.index = photo_index.read_index(self.store)
        self.presets = presets.read_presets(self.store)
        self.folders = folders.read_folders(self.store)
        self.index.filter(self.minimum_rating, self.flags, self.label, self.search, [], [])

    ## Close this library and open the bundle at `root` in its place, made
    ## when it is missing; the filter stays. Not while an import runs.
    pub func switch_to(self, root: str) -> unit!:
        if self.importing():
            error(busy, "an import is running; wait for it or open the library after")
        if let current = self.running:
            current.close()
        self.running = none
        library.ensure(root)
        let store = prism.Store.open(library.catalog_path(root))
        self.store.close()
        self.store = store
        self.root = root
        self.folders = folders.read_folders(self.store)
        self.folder = 0
        self.shown_folders = []
        self.reload()
        self.presets = presets.read_presets(self.store)

    ## Read the catalog again, keeping the filter.
    pub func reload(self) -> unit!:
        self.index = photo_index.read_index(self.store)
        self.index.filter(self.minimum_rating, self.flags, self.label, self.search, self.searched_presets(), self.shown_folders)

    ## Show only photos with at least `minimum_rating` stars, flags `flags` (0
    ## all, 1 picked, -1 rejected, 2 all but rejected) and label `label` (0 any).
    pub func set_filter(self, minimum_rating: int, flags: int, label: int) -> unit!:
        self.minimum_rating = minimum_rating
        self.flags = flags
        self.label = label
        self.index.filter(minimum_rating, flags, label, self.search, self.searched_presets(), self.shown_folders)

    ## Show only photos matching `text` (search_field), as well as the filter.
    pub func set_search(self, text: str) -> unit!:
        self.search = text.trim()
        self.index.filter(self.minimum_rating, self.flags, self.label, self.search, self.searched_presets(), self.shown_folders)

    ## The ids of the presets whose names hold the search.
    func searched_presets(self) -> list[int]:
        var out: list[int] = []
        if self.search == "":
            return out
        let wanted = self.search.lower()
        for at in 0..<self.presets.count():
            if wanted in self.presets.name(at).lower():
                out.append(self.presets.id(at))
        return out

    # mark: importing ===========================================================================

    ## Import the files and folders at `sources` on a thread of their own, into
    ## the folder shown (none when every photo shows).
    pub func start_import(self, sources: list[str]) -> unit!:
        if let current = self.running:
            if not current.finished():
                error(busy, "an import is already running")
            current.close()
        self.running = importer.start(self.store, self.root, sources, self.import_folder(), self.import_notifier)

    ## Import the photos on camera `at` of `cameras` that the library lacks, as
    ## start_import imports files.
    pub func start_camera_import(self, cameras: Watcher, at: int) -> unit!:
        if let current = self.running:
            if not current.finished():
                error(busy, "an import is already running")
            current.close()
        self.running = importer.start_camera(self.store, self.root, cameras, at, self.import_folder(), self.import_notifier)

    ## The folder an import goes into: the one shown, never the Trash.
    func import_folder(self) -> int:
        return self.folder if self.folder > 0 else 0

    ## Whether an import is running.
    pub func importing(self) -> bool:
        if let current = self.running:
            return not current.finished()
        return false

    ## The import's events since the last poll (importer.lucb lists them).
    pub func poll_import(self) -> list[str]!:
        let current = self.running else return []
        let text = current.poll()
        if text == "":
            if current.finished():
                current.close()
                self.running = none
            return []
        return text.lines()

    # mark: files ===============================================================================

    ## The grid thumbnail and the viewer preview of photo `id`.
    pub func thumb_path(self, id: int) -> str!:
        return library.cached(self.root, "thumbs", id)

    ## Photo `id`'s developed picture (3072 px), and whether it was made with
    ## `settings` (its stamp says).
    pub func developed_path(self, id: int) -> str!:
        return library.cached(self.root, "developed", id)

    pub func developed_ready(self, id: int, settings: str) -> bool:
        let stamp = library.stamp(self.root, id) catch _:
            return false
        let picture = self.developed_path(id) catch _:
            return false
        if not library.exists(stamp) or not library.exists(picture):
            return false
        let made = files.read_text(stamp) catch _:
            return false
        return made == settings

    pub func preview_path(self, id: int) -> str!:
        return library.cached(self.root, "previews", id)

    ## The original file of the photo at `at` in the index.
    pub func original_path(self, at: int) -> str!:
        return paths.join(self.root, self.index.path(at))

    # mark: marks ===============================================================================

    ## Set `name` ("rating", "flag", "label" or "folder")
    ## to `value` on the photos at `positions` of the index, in the catalog and
    ## in the index.
    pub func set_marks(self, positions: list[int], name: str, value: int) -> unit!:
        var ids: list[int] = []
        for at in positions:
            ids.append(self.index.id(at))
        photo_index.set_marks(self.store, ids, name, value)
        for at in positions:
            match name:
                "rating":
                    self.index.set_rating(at, value)
                "flag":
                    self.index.set_flag(at, value)
                "folder":
                    self.index.set_folder(at, value)
                _:
                    self.index.set_label(at, value)

    ## Give the photos at `positions` the preset mark `mark` (preset_marks.luc).
    pub func set_preset_mark(self, positions: list[int], mark: str) -> unit!:
        var ids: list[int] = []
        for at in positions:
            ids.append(self.index.id(at))
        photo_index.set_preset_marks(self.store, ids, mark)
        for at in positions:
            self.index.set_preset(at, mark)

    ## Store photo `id`'s develop settings (text form) in the catalog and the index.
    pub func save_settings(self, id: int, text: str) -> unit!:
        photo_index.save_settings(self.store, id, text)
        let at = self.index.position_of(id)
        if at >= 0:
            self.index.set_settings(at, text)

    # mark: presets =============================================================================

    ## Keep settings `text` as the preset `name` (replacing one of that name),
    ## showing `exposed` ("|" between) in the Library mode; its id.
    pub func save_preset(self, name: str, text: str, exposed: str, folder: int = 0) -> int!:
        let id = presets.save_preset(self.store, self.presets, name, text, exposed, folder)
        self.presets = presets.read_presets(self.store)
        return id

    pub func set_exposed(self, id: int, exposed: str) -> unit!:
        presets.set_exposed(self.store, id, exposed)
        self.presets = presets.read_presets(self.store)

    pub func rename_preset(self, id: int, name: str) -> unit!:
        presets.rename_preset(self.store, id, name)
        self.presets = presets.read_presets(self.store)

    pub func delete_preset(self, id: int) -> unit!:
        presets.delete_preset(self.store, id)
        self.presets = presets.read_presets(self.store)

    ## Put preset `id` into preset folder `folder` (0: none).
    pub func move_preset(self, id: int, folder: int) -> unit!:
        presets.move_preset(self.store, id, folder)
        self.presets = presets.read_presets(self.store)

    ## A new preset folder named `name`; its id. Renaming and deleting one (its
    ## presets left in no folder).
    pub func make_preset_folder(self, name: str) -> int!:
        let id = presets.make_folder(self.store, self.presets, name)
        self.presets = presets.read_presets(self.store)
        return id
    pub func rename_preset_folder(self, id: int, name: str) -> unit!:
        presets.rename_folder(self.store, id, name)
        self.presets = presets.read_presets(self.store)
    pub func delete_preset_folder(self, id: int) -> unit!:
        presets.delete_folder(self.store, self.presets, id)
        self.presets = presets.read_presets(self.store)

    # mark: folders =============================================================================

    ## Show folder `id`'s photos, its subfolders' with them (0: every photo).
    pub func show_folder(self, id: int) -> unit!:
        self.folder = id
        self.shown_folders = self.tree_of(id) if id > 0 else ([catalog.trash_folder] if id == catalog.trash_folder else [])
        self.index.filter(self.minimum_rating, self.flags, self.label, self.search, self.searched_presets(), self.shown_folders)

    ## Folder `id` and every folder inside it.
    pub func tree_of(self, id: int) -> list[int]:
        var out = [id]
        for at in 0..<self.folders.count():
            let other = self.folders.id(at)
            if other != id and self.folders.within(other, id):
                out.append(other)
        return out

    ## How many photos folder `id` holds, its subfolders' with them (0: the library).
    pub func count_in(self, id: int) -> int:
        if id == catalog.trash_folder:
            return self.index.count_in([catalog.trash_folder])
        if id <= 0:
            return self.index.total()
        return self.index.count_in(self.tree_of(id))

    ## A new folder named `name` in folder `parent` (0: at the top); its id.
    pub func make_folder(self, name: str, parent: int) -> int!:
        let id = folders.make_folder(self.store, self.folders, name, parent)
        self.folders = folders.read_folders(self.store)
        return id

    pub func rename_folder(self, id: int, name: str) -> unit!:
        folders.rename_folder(self.store, id, name)
        self.folders = folders.read_folders(self.store)

    ## Put folder `id` into `parent` (0: at the top), unless that is inside it.
    pub func move_folder(self, id: int, parent: int) -> bool!:
        if parent == id or (parent > 0 and self.folders.within(parent, id)):
            return false
        folders.move_folder(self.store, id, parent)
        self.folders = folders.read_folders(self.store)
        return true

    ## Delete folder `id`: its photos and folders go to the folder it was in.
    pub func delete_folder(self, id: int) -> unit!:
        let at = self.folders.position_of(id)
        if at < 0:
            return
        let parent = self.folders.parent(at)
        var children: list[int] = []
        for other in 0..<self.folders.count():
            if self.folders.parent(other) == id:
                children.append(self.folders.id(other))
        for child in children:
            folders.move_folder(self.store, child, parent)
        # Its photos, every one in the catalog whatever the filter shows.
        self.index.move_folder_photos(self.store, id, parent)
        folders.delete_folder(self.store, id)
        self.folders = folders.read_folders(self.store)
        if self.folder == id:
            self.folder = parent
        self.reload()
        self.show_folder(self.folder)

    ## Move the photos at `positions` of the index into folder `id` (0: none).
    pub func move_photos(self, positions: list[int], id: int) -> unit!:
        self.set_marks(positions, "folder", id)
        # Out of the folder shown: they leave the view.
        self.show_folder(self.folder)

    # mark: the trash =========================================================================

    ## Put the photos at `positions` of the index in the Trash, or back out of
    ## it (into no folder); they leave the view.
    pub func trash_photos(self, positions: list[int]) -> unit!:
        self.move_photos(positions, catalog.trash_folder)
    pub func put_back(self, positions: list[int]) -> unit!:
        self.move_photos(positions, 0)

    ## Delete every photo in the Trash, its files with it; how many.
    pub func empty_trash(self) -> int!:
        let count = self.index.empty_trash(self.store, self.root)
        self.reload()
        return count

    pub func close(self):
        if let current = self.running:
            current.close()
        self.running = none
        self.store.close()