2017-07-01 10:11:44 +00:00
|
|
|
--[[--
|
|
|
|
This module contains miscellaneous helper functions for FileManager
|
|
|
|
]]
|
|
|
|
|
|
|
|
local Device = require("device")
|
2017-09-22 16:24:38 +00:00
|
|
|
local DocSettings = require("docsettings")
|
|
|
|
local util = require("ffi/util")
|
2019-10-21 12:24:29 +00:00
|
|
|
local _ = require("gettext")
|
2017-07-01 10:11:44 +00:00
|
|
|
|
|
|
|
local filemanagerutil = {}
|
|
|
|
|
|
|
|
function filemanagerutil.getDefaultDir()
|
2020-06-19 07:41:50 +00:00
|
|
|
return Device.home_dir or "."
|
2017-07-01 10:11:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function filemanagerutil.abbreviate(path)
|
2018-07-28 18:44:55 +00:00
|
|
|
if not path then return "" end
|
2019-10-21 12:24:29 +00:00
|
|
|
if G_reader_settings:nilOrTrue("shorten_home_dir") then
|
2017-07-01 10:11:44 +00:00
|
|
|
local home_dir = G_reader_settings:readSetting("home_dir") or filemanagerutil.getDefaultDir()
|
2019-10-21 12:24:29 +00:00
|
|
|
if path == home_dir then
|
|
|
|
return _("Home")
|
|
|
|
end
|
2017-07-01 10:11:44 +00:00
|
|
|
local len = home_dir:len()
|
|
|
|
local start = path:sub(1, len)
|
2019-11-19 20:06:03 +00:00
|
|
|
if start == home_dir and path:sub(len+1, len+1) == "/" then
|
2019-10-21 12:24:29 +00:00
|
|
|
return path:sub(len+2)
|
2017-07-01 10:11:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
2017-09-22 16:24:38 +00:00
|
|
|
-- Purge doc settings in sidecar directory,
|
|
|
|
function filemanagerutil.purgeSettings(file)
|
|
|
|
local file_abs_path = util.realpath(file)
|
|
|
|
if file_abs_path then
|
|
|
|
os.remove(DocSettings:getSidecarFile(file_abs_path))
|
|
|
|
-- Also remove backup, otherwise it will be used if we re-open this document
|
|
|
|
-- (it also allows for the sidecar folder to be empty and removed)
|
|
|
|
os.remove(DocSettings:getSidecarFile(file_abs_path)..".old")
|
|
|
|
-- If the sidecar folder is empty, os.remove() can delete it.
|
|
|
|
-- Otherwise, the following statement has no effect.
|
|
|
|
os.remove(DocSettings:getSidecarDir(file_abs_path))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-01 10:11:44 +00:00
|
|
|
return filemanagerutil
|