More sorting orders in file manager (#2535)

pull/2541/head
Hzj_jie 7 years ago committed by Qingping Hou
parent faaca028e7
commit 726b15ad01

@ -475,8 +475,12 @@ end
function FileManager:getSortingMenuTable() function FileManager:getSortingMenuTable()
local fm = self local fm = self
local collates = { local collates = {
strcoll = {_("by title"), _("Sort by title")}, strcoll = {_("title"), _("Sort by title")},
access = {_("by date"), _("Sort by date")}, access = {_("date read"), _("Sort by date of last read")},
change = {_("date added"), _("Sort by date of adding")},
modification = {_("date modified"), _("Sort by date of modification")},
size = {_("size"), _("Sort by size")},
type = {_("type"), _("Sort by type")},
} }
local set_collate_table = function(collate) local set_collate_table = function(collate)
return { return {
@ -490,13 +494,17 @@ function FileManager:getSortingMenuTable()
return { return {
text_func = function() text_func = function()
return util.template( return util.template(
_("Sort order: %1"), _("Sort by %1"),
collates[fm.file_chooser.collate][1] collates[fm.file_chooser.collate][1]
) )
end, end,
sub_item_table = { sub_item_table = {
set_collate_table("strcoll"), set_collate_table("strcoll"),
set_collate_table("access"), set_collate_table("access"),
set_collate_table("change"),
set_collate_table("modification"),
set_collate_table("size"),
set_collate_table("type"),
} }
} }
end end

@ -7,6 +7,7 @@ local DocSettings = require("docsettings")
local util = require("ffi/util") local util = require("ffi/util")
local _ = require("gettext") local _ = require("gettext")
local ffi = require("ffi") local ffi = require("ffi")
local getFileNameSuffix = require("util").getFileNameSuffix
ffi.cdef[[ ffi.cdef[[
int strcoll (const char *str1, const char *str2); int strcoll (const char *str1, const char *str2);
]] ]]
@ -16,13 +17,16 @@ local function strcoll(str1, str2)
return ffi.C.strcoll(str1, str2) < 0 return ffi.C.strcoll(str1, str2) < 0
end end
local function kobostrcoll(str1, str2)
return str1 < str2
end
local FileChooser = Menu:extend{ local FileChooser = Menu:extend{
no_title = true, no_title = true,
path = lfs.currentdir(), path = lfs.currentdir(),
parent = nil, parent = nil,
show_hidden = nil, show_hidden = nil,
exclude_dirs = {"%.sdr$"}, exclude_dirs = {"%.sdr$"},
strcoll = strcoll,
collate = "strcoll", -- or collate = "access", collate = "strcoll", -- or collate = "access",
reverse_collate = false, reverse_collate = false,
path_items = {}, -- store last browsed location(item index) for each path path_items = {}, -- store last browsed location(item index) for each path
@ -48,11 +52,17 @@ function FileChooser:init()
if attributes ~= nil then if attributes ~= nil then
if attributes.mode == "directory" and f ~= "." and f~=".." then if attributes.mode == "directory" and f ~= "." and f~=".." then
if self.dir_filter(filename) then if self.dir_filter(filename) then
table.insert(dirs, {name = f, attr = attributes}) table.insert(dirs, {name = f,
suffix = getFileNameSuffix(f),
fullpath = filename,
attr = attributes})
end end
elseif attributes.mode == "file" then elseif attributes.mode == "file" then
if self.file_filter == nil or self.file_filter(filename) then if self.file_filter == nil or self.file_filter(filename) then
table.insert(files, {name = f, attr = attributes}) table.insert(files, {name = f,
suffix = getFileNameSuffix(f),
fullpath = filename,
attr = attributes})
end end
end end
end end
@ -61,9 +71,23 @@ function FileChooser:init()
end end
end end
local strcoll_func = strcoll
-- circumvent string collating in Kobo devices. See issue koreader/koreader#686 -- circumvent string collating in Kobo devices. See issue koreader/koreader#686
if Device:isKobo() then if Device:isKobo() then
self.strcoll = function(a, b) return a < b end strcoll_func = kobostrcoll
end
self.strcoll = function(a, b)
if a == nil and b == nil then
return false
elseif a == nil then
return true
elseif b == nil then
return false
elseif DALPHA_SORT_CASE_INSENSITIVE then
return strcoll_func(string.lower(a), string.lower(b))
else
return strcoll_func(a, b)
end
end end
self.item_table = self:genItemTableFromPath(self.path) self.item_table = self:genItemTableFromPath(self.path)
Menu.init(self) -- call parent's init() Menu.init(self) -- call parent's init()
@ -75,26 +99,56 @@ function FileChooser:genItemTableFromPath(path)
self.list(path, dirs, files) self.list(path, dirs, files)
local sorting = nil local sorting
local reverse = self.reverse_collate
if self.collate == "strcoll" then if self.collate == "strcoll" then
if DALPHA_SORT_CASE_INSENSITIVE then sorting = function(a, b)
sorting = function(a, b) return self.strcoll(a.name, b.name)
return self.strcoll(string.lower(a.name), string.lower(b.name)) == not reverse end
elseif self.collate == "access" then
sorting = function(a, b)
if DocSettings:hasSidecarFile(a.fullpath) and not DocSettings:hasSidecarFile(b.fullpath) then
return true
end end
else if not DocSettings:hasSidecarFile(a.fullpath) and DocSettings:hasSidecarFile(b.fullpath) then
sorting = function(a, b) return false
return self.strcoll(a.name, b.name) == not reverse
end end
return a.attr.access > b.attr.access
end end
elseif self.collate == "access" then elseif self.collate == "modification" then
sorting = function(a, b)
return a.attr.modification > b.attr.modification
end
elseif self.collate == "change" then
sorting = function(a, b)
if DocSettings:hasSidecarFile(a.fullpath) and not DocSettings:hasSidecarFile(b.fullpath) then
return false
end
if not DocSettings:hasSidecarFile(a.fullpath) and DocSettings:hasSidecarFile(b.fullpath) then
return true
end
return a.attr.change > b.attr.change
end
elseif self.collate == "size" then
sorting = function(a, b)
return a.attr.size < b.attr.size
end
elseif self.collate == "type" then
sorting = function(a, b) sorting = function(a, b)
if reverse then if a.suffix == nil and b.suffix == nil then
return a.attr.access < b.attr.access return self.strcoll(a.name, b.name)
else else
return a.attr.access > b.attr.access return self.strcoll(a.suffix, b.suffix)
end end
end end
else
sorting = function(a, b)
return a.name < b.name
end
end
if self.reverse_collate then
local sorting_unreversed = sorting
sorting = function(a, b) return sorting_unreversed(b, a) end
end end
table.sort(dirs, sorting) table.sort(dirs, sorting)

@ -266,6 +266,11 @@ function util.splitFileNameSuffix(file)
return string.gsub(file, "(.*)%.(.*)", "%1"), string.gsub(file, ".*%.", "") return string.gsub(file, "(.*)%.(.*)", "%1"), string.gsub(file, ".*%.", "")
end end
function util.getFileNameSuffix(file)
local _, suffix = util.splitFileNameSuffix(file)
return suffix
end
function util.getMenuText(item) function util.getMenuText(item)
local text local text
if item.text_func then if item.text_func then

Loading…
Cancel
Save