mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
Collections: add books from a folder (#12892)
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user