2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/widget/filechooser.lua

228 lines
6.9 KiB
Lua
Raw Normal View History

2014-08-14 11:49:42 +00:00
local lfs = require("libs/libkoreader-lfs")
local UIManager = require("ui/uimanager")
2013-10-18 20:38:07 +00:00
local Menu = require("ui/widget/menu")
local Screen = require("device").screen
local Device = require("device")
local DocSettings = require("docsettings")
2014-06-04 09:22:45 +00:00
local util = require("ffi/util")
local _ = require("gettext")
2014-05-15 12:44:48 +00:00
local ffi = require("ffi")
ffi.cdef[[
int strcoll (const char *str1, const char *str2);
2014-05-15 12:44:48 +00:00
]]
-- string sort function respecting LC_COLLATE
local function strcoll(str1, str2)
return ffi.C.strcoll(str1, str2) < 0
2014-05-15 12:44:48 +00:00
end
2012-05-27 21:43:00 +00:00
2013-10-18 20:38:07 +00:00
local FileChooser = Menu:extend{
2014-03-13 13:52:43 +00:00
no_title = true,
path = lfs.currentdir(),
parent = nil,
show_hidden = nil,
exclude_dirs = {"%.sdr$"},
strcoll = strcoll,
collate = "strcoll", -- or collate = "access",
reverse_collate = false,
path_items = {}, -- store last browsed location(item index) for each path
2012-05-27 21:43:00 +00:00
}
function FileChooser:init()
self.width = Screen:getWidth()
-- common dir filter
self.dir_filter = function(dirname)
for _, pattern in ipairs(self.exclude_dirs) do
2016-07-05 00:38:04 +00:00
if dirname:match(pattern) then return false end
end
return true
end
self.list = function(path, dirs, files)
-- lfs.dir directory without permission will give error
local ok, iter, dir_obj = pcall(lfs.dir, path)
if ok then
for f in iter, dir_obj do
if self.show_hidden or not string.match(f, "^%.[^.]") then
local filename = path.."/"..f
local attributes = lfs.attributes(filename)
if attributes ~= nil then
if attributes.mode == "directory" and f ~= "." and f~=".." then
if self.dir_filter(filename) then
table.insert(dirs, {name = f, attr = attributes})
end
elseif attributes.mode == "file" then
if self.file_filter == nil or self.file_filter(filename) then
table.insert(files, {name = f, attr = attributes})
end
end
end
end
end
end
end
-- circumvent string collating in Kobo devices. See issue koreader/koreader#686
if Device:isKobo() then
self.strcoll = function(a, b) return a < b end
end
2014-03-13 13:52:43 +00:00
self.item_table = self:genItemTableFromPath(self.path)
Menu.init(self) -- call parent's init()
2012-05-27 21:43:00 +00:00
end
function FileChooser:genItemTableFromPath(path)
2014-03-13 13:52:43 +00:00
local dirs = {}
local files = {}
self.list(path, dirs, files)
local sorting = nil
local reverse = self.reverse_collate
if self.collate == "strcoll" then
if DALPHA_SORT_CASE_INSENSITIVE then
sorting = function(a, b)
return self.strcoll(string.lower(a.name), string.lower(b.name)) == not reverse
end
else
sorting = function(a, b)
return self.strcoll(a.name, b.name) == not reverse
end
end
elseif self.collate == "access" then
sorting = function(a, b)
if reverse then
return a.attr.access < b.attr.access
else
return a.attr.access > b.attr.access
end
end
end
table.sort(dirs, sorting)
if path ~= "/" then table.insert(dirs, 1, {name = ".."}) end
table.sort(files, sorting)
2012-05-27 21:43:00 +00:00
2014-03-13 13:52:43 +00:00
local item_table = {}
for i, dir in ipairs(dirs) do
2016-04-19 07:59:02 +00:00
-- count sume of directories and files inside dir
local sub_dirs = {}
local dir_files = {}
2016-02-16 06:34:28 +00:00
local subdir_path = self.path.."/"..dir.name
2016-04-19 07:59:02 +00:00
self.list(subdir_path, sub_dirs, dir_files)
2016-12-25 07:43:58 +00:00
local num_items = #sub_dirs + #dir_files
local istr
if num_items == 1 then
istr = _("1 item")
else
istr = util.template(_("%1 items"), num_items)
end
table.insert(item_table, {
text = dir.name.."/",
mandatory = istr,
2016-02-16 06:34:28 +00:00
path = subdir_path
})
2014-03-13 13:52:43 +00:00
end
-- set to false to show all files in regular font
-- set to "opened" to show opened files in bold
-- otherwise, show new files in bold
local show_file_in_bold = G_reader_settings:readSetting("show_file_in_bold")
2014-03-13 13:52:43 +00:00
for _, file in ipairs(files) do
local full_path = self.path.."/"..file.name
local file_size = lfs.attributes(full_path, "size") or 0
2016-02-16 06:34:28 +00:00
local sstr
if file_size > 1024*1024 then
sstr = string.format("%4.1f MB", file_size/1024/1024)
elseif file_size > 1024 then
sstr = string.format("%4.1f KB", file_size/1024)
2014-03-13 13:52:43 +00:00
else
sstr = string.format("%d B", file_size)
2014-03-13 13:52:43 +00:00
end
local file_item = {
text = file.name,
mandatory = sstr,
path = full_path
}
if show_file_in_bold ~= false then
file_item.bold = DocSettings:hasSidecarDir(full_path)
if show_file_in_bold ~= "opened" then
file_item.bold = not file_item.bold
end
end
table.insert(item_table, file_item)
end
2016-12-25 07:43:58 +00:00
-- lfs.dir iterated node string may be encoded with some weird codepage on
-- Windows we need to encode them to utf-8
if ffi.os == "Windows" then
for k, v in pairs(item_table) do
if v.text then
v.text = util.multiByteToUTF8(v.text) or ""
end
end
2014-03-13 13:52:43 +00:00
end
2014-03-13 13:52:43 +00:00
return item_table
end
2012-05-27 21:43:00 +00:00
2014-11-23 10:06:20 +00:00
function FileChooser:updateItems(select_number)
Menu.updateItems(self, select_number) -- call parent's updateItems()
self.path_items[self.path] = (self.page - 1) * self.perpage + (select_number or 1)
2014-11-23 10:06:20 +00:00
end
function FileChooser:refreshPath()
2017-02-01 14:24:21 +00:00
self:switchItemTable(nil, self:genItemTableFromPath(self.path), self.path_items[self.path])
end
function FileChooser:changeToPath(path)
2014-03-13 13:52:43 +00:00
path = util.realpath(path)
self.path = path
self:refreshPath()
2016-07-05 00:38:04 +00:00
self:onPathChanged(path)
end
2013-08-25 04:00:06 +00:00
function FileChooser:toggleHiddenFiles()
2014-03-13 13:52:43 +00:00
self.show_hidden = not self.show_hidden
self:refreshPath()
2013-08-25 04:00:06 +00:00
end
function FileChooser:setCollate(collate)
self.collate = collate
self:refreshPath()
end
function FileChooser:toggleReverseCollate()
self.reverse_collate = not self.reverse_collate
self:refreshPath()
end
function FileChooser:onMenuSelect(item)
-- parent directory of dir without permission get nil mode
-- we need to change to parent path in this case
if lfs.attributes(item.path, "mode") == "file" then
2014-03-13 13:52:43 +00:00
self:onFileSelect(item.path)
else
self:changeToPath(item.path)
2014-03-13 13:52:43 +00:00
end
return true
2012-05-27 22:14:08 +00:00
end
function FileChooser:onMenuHold(item)
2014-03-13 13:52:43 +00:00
self:onFileHold(item.path)
return true
end
function FileChooser:onFileSelect(file)
2014-03-13 13:52:43 +00:00
UIManager:close(self)
return true
end
2013-10-18 20:38:07 +00:00
function FileChooser:onFileHold(file)
2014-03-13 13:52:43 +00:00
return true
end
2016-07-05 00:38:04 +00:00
function FileChooser:onPathChanged(path)
return true
end
2013-10-18 20:38:07 +00:00
return FileChooser