From f025cce05925ca08b5e7134defdf2d0056f00ce2 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Sun, 15 Dec 2024 18:22:05 +0200 Subject: [PATCH] Collections: add books from a folder (#12892) --- .../filemanager/filemanagercollection.lua | 43 +++++++++++++++++++ frontend/readcollection.lua | 25 ++++++----- frontend/util.lua | 5 ++- 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/frontend/apps/filemanager/filemanagercollection.lua b/frontend/apps/filemanager/filemanagercollection.lua index 1edc22670..a79b8d3bc 100644 --- a/frontend/apps/filemanager/filemanagercollection.lua +++ b/frontend/apps/filemanager/filemanagercollection.lua @@ -13,6 +13,7 @@ local UIManager = require("ui/uimanager") local WidgetContainer = require("ui/widget/container/widgetcontainer") local filemanagerutil = require("apps/filemanager/filemanagerutil") local _ = require("gettext") +local N_ = _.ngettext local T = require("ffi/util").template local util = require("util") @@ -239,6 +240,22 @@ function FileManagerCollection:showCollDialog() self:sortCollection() end, }}, + {}, -- separator + {{ + text = _("Add all books from a folder"), + callback = function() + UIManager:close(coll_dialog) + self:addBooksFromFolder(false) + end, + }}, + {{ + text = _("Add all books from a folder and its subfolders"), + callback = function() + UIManager:close(coll_dialog) + self:addBooksFromFolder(true) + end, + }}, + {}, -- separator {{ text = _("Add a book to collection"), callback = function() @@ -297,6 +314,32 @@ function FileManagerCollection:sortCollection() UIManager:show(sort_widget) end +function FileManagerCollection:addBooksFromFolder(include_subfolders) + local PathChooser = require("ui/widget/pathchooser") + local path_chooser = PathChooser:new{ + path = G_reader_settings:readSetting("home_dir"), + select_file = false, + onConfirm = function(folder) + local files_found = {} + local DocumentRegistry = require("document/documentregistry") + util.findFiles(folder, function(file) + files_found[file] = DocumentRegistry:hasProvider(file) or nil + end, include_subfolders) + local count = ReadCollection:addItemsMultiple(files_found, { [self.coll_menu.collection_name] = true }) + local text + if count == 0 then + text = _("No books added to collection") + else + text = T(N_("1 book added to collection", "%1 books added to collection", count), count) + self:updateItemTable() + self.files_updated = true + end + UIManager:show(InfoMessage:new{ text = text }) + end, + } + UIManager:show(path_chooser) +end + function FileManagerCollection:onBookMetadataChanged() if self.coll_menu then self.coll_menu:updateItems() diff --git a/frontend/readcollection.lua b/frontend/readcollection.lua index 5e34258d8..2846be7b2 100644 --- a/frontend/readcollection.lua +++ b/frontend/readcollection.lua @@ -1,6 +1,6 @@ local DataStorage = require("datastorage") -local FFIUtil = require("ffi/util") local LuaSettings = require("luasettings") +local ffiUtil = require("ffi/util") local lfs = require("libs/libkoreader-lfs") local logger = require("logger") local util = require("util") @@ -17,7 +17,7 @@ local ReadCollection = { -- read, write local function buildEntry(file, order, mandatory) - file = FFIUtil.realpath(file) + file = ffiUtil.realpath(file) if not file then return end if not mandatory then -- new item local attr = lfs.attributes(file) @@ -84,12 +84,12 @@ end -- info function ReadCollection:isFileInCollection(file, collection_name) - file = FFIUtil.realpath(file) or file + file = ffiUtil.realpath(file) or file return self.coll[collection_name][file] and true or false end function ReadCollection:isFileInCollections(file) - file = FFIUtil.realpath(file) or file + file = ffiUtil.realpath(file) or file for _, coll in pairs(self.coll) do if coll[file] then return true @@ -99,7 +99,7 @@ function ReadCollection:isFileInCollections(file) end function ReadCollection:getCollectionsWithFile(file) - file = FFIUtil.realpath(file) or file + file = ffiUtil.realpath(file) or file local collections = {} for coll_name, coll in pairs(self.coll) do if coll[file] then @@ -129,7 +129,7 @@ function ReadCollection:addItem(file, collection_name) end function ReadCollection:addRemoveItemMultiple(file, collections_to_add) - file = FFIUtil.realpath(file) or file + file = ffiUtil.realpath(file) or file for coll_name, coll in pairs(self.coll) do if collections_to_add[coll_name] then if not coll[file] then @@ -146,23 +146,26 @@ function ReadCollection:addRemoveItemMultiple(file, collections_to_add) end function ReadCollection:addItemsMultiple(files, collections_to_add, no_write) + local count = 0 for file in pairs(files) do - file = FFIUtil.realpath(file) or file + file = ffiUtil.realpath(file) or file for coll_name in pairs(collections_to_add) do local coll = self.coll[coll_name] if not coll[file] then local max_order = self:getCollectionMaxOrder(coll_name) coll[file] = buildEntry(file, max_order + 1) + count = count + 1 end end end - if not no_write then + if not no_write and count > 0 then self:write() end + return count end function ReadCollection:removeItem(file, collection_name, no_write) -- FM: delete file; FMColl: remove file - file = FFIUtil.realpath(file) or file + file = ffiUtil.realpath(file) or file if collection_name then if self.coll[collection_name][file] then self.coll[collection_name][file] = nil @@ -226,7 +229,7 @@ function ReadCollection:_updateItem(coll_name, file_name, new_filepath, new_path end function ReadCollection:updateItem(file, new_filepath) -- FM: rename file, move file - file = FFIUtil.realpath(file) or file + file = ffiUtil.realpath(file) or file local do_write for coll_name, coll in pairs(self.coll) do if coll[file] then @@ -242,7 +245,7 @@ end function ReadCollection:updateItems(files, new_path) -- FM: move files local do_write for file in pairs(files) do - file = FFIUtil.realpath(file) or file + file = ffiUtil.realpath(file) or file for coll_name, coll in pairs(self.coll) do if coll[file] then self:_updateItem(coll_name, file, nil, new_path) diff --git a/frontend/util.lua b/frontend/util.lua index 8e1a1b686..c2a70bda7 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -783,7 +783,8 @@ end --- Recursively scan directory for files inside -- @string path -- @func callback(fullpath, name, attr) -function util.findFiles(dir, cb) +function util.findFiles(dir, cb, recursive) + recursive = recursive ~= false local function scan(current) local ok, iter, dir_obj = pcall(lfs.dir, current) if not ok then return end @@ -792,7 +793,7 @@ function util.findFiles(dir, cb) -- lfs can return nil here, as it will follow symlinks! local attr = lfs.attributes(path) or {} if attr.mode == "directory" then - if f ~= "." and f ~= ".." then + if recursive and f ~= "." and f ~= ".." then scan(path) end elseif attr.mode == "file" or attr.mode == "link" then