mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Add "Open random document" to Plus menu (#4103)
This commit is contained in:
@@ -19,6 +19,7 @@ local IconButton = require("ui/widget/iconbutton")
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local InputDialog = require("ui/widget/inputdialog")
|
||||
local MultiConfirmBox = require("ui/widget/multiconfirmbox")
|
||||
local PluginLoader = require("pluginloader")
|
||||
local ReaderDictionary = require("apps/reader/modules/readerdictionary")
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
@@ -452,6 +453,15 @@ function FileManager:tapPlus()
|
||||
UIManager:close(self.file_dialog)
|
||||
end
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
text = _("Open random document"),
|
||||
callback = function()
|
||||
self:openRandomFile(self.file_chooser.path)
|
||||
UIManager:close(self.file_dialog)
|
||||
end
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,6 +543,30 @@ function FileManager:setHome(path)
|
||||
return true
|
||||
end
|
||||
|
||||
function FileManager:openRandomFile(dir)
|
||||
local random_file = DocumentRegistry:getRandomFile(dir, false)
|
||||
if random_file then
|
||||
UIManager:show(MultiConfirmBox:new {
|
||||
text = T(_("Do you want to open %1?"), util.basename(random_file)),
|
||||
choice1_text = _("Open"),
|
||||
choice1_callback = function()
|
||||
FileManager.instance:onClose()
|
||||
ReaderUI:showReader(random_file)
|
||||
|
||||
end,
|
||||
choice2_text = _("Another"),
|
||||
choice2_callback = function()
|
||||
self:openRandomFile(dir)
|
||||
end,
|
||||
})
|
||||
UIManager:close(self.file_dialog)
|
||||
else
|
||||
UIManager:show(InfoMessage:new {
|
||||
text = _("File not found"),
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
function FileManager:copyFile(file)
|
||||
self.cutfile = false
|
||||
self.clipboard = file
|
||||
|
||||
@@ -7,6 +7,7 @@ local OpenWithDialog = require("ui/widget/openwithdialog")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local gettext = require("gettext")
|
||||
local logger = require("logger")
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local util = require("util")
|
||||
local T = require("ffi/util").template
|
||||
|
||||
@@ -26,6 +27,33 @@ function DocumentRegistry:addProvider(extension, mimetype, provider, weight)
|
||||
self.filetype_provider[extension] = true
|
||||
end
|
||||
|
||||
function DocumentRegistry:getRandomFile(dir, opened, extension)
|
||||
local DocSettings = require("docsettings")
|
||||
if string.sub(dir, string.len(dir)) ~= "/" then
|
||||
dir = dir .. "/"
|
||||
end
|
||||
local files = {}
|
||||
local i = 0
|
||||
local ok, iter, dir_obj = pcall(lfs.dir, dir)
|
||||
if ok then
|
||||
for entry in iter, dir_obj do
|
||||
if lfs.attributes(dir .. entry, "mode") == "file" and self:hasProvider(dir .. entry)
|
||||
and (opened == nil or DocSettings:hasSidecarFile(dir .. entry) == opened)
|
||||
and (extension == nil or extension[util.getFileNameSuffix(entry)]) then
|
||||
i = i + 1
|
||||
files[i] = entry
|
||||
end
|
||||
end
|
||||
if i == 0 then
|
||||
return nil
|
||||
end
|
||||
else
|
||||
return nil
|
||||
end
|
||||
math.randomseed(os.time())
|
||||
return dir .. files[math.random(i)]
|
||||
end
|
||||
|
||||
--- Returns true if file has provider.
|
||||
-- @string file
|
||||
-- @treturn boolean
|
||||
|
||||
Reference in New Issue
Block a user