2016-01-03 08:47:01 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2017-04-12 18:42:28 +00:00
|
|
|
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
|
|
|
local Device = require("device")
|
2016-06-04 05:05:14 +00:00
|
|
|
local DocSettings = require("docsettings")
|
2017-04-12 18:42:28 +00:00
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Event = require("ui/event")
|
2017-04-12 18:42:28 +00:00
|
|
|
local FileChooser = require("ui/widget/filechooser")
|
2017-07-01 10:11:44 +00:00
|
|
|
local FileManagerBookInfo = require("apps/filemanager/filemanagerbookinfo")
|
2017-04-12 18:42:28 +00:00
|
|
|
local FileManagerConverter = require("apps/filemanager/filemanagerconverter")
|
|
|
|
local FileManagerHistory = require("apps/filemanager/filemanagerhistory")
|
|
|
|
local FileManagerMenu = require("apps/filemanager/filemanagermenu")
|
2016-01-03 08:47:01 +00:00
|
|
|
local Font = require("ui/font")
|
2017-04-12 18:42:28 +00:00
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
|
|
|
local Geom = require("ui/geometry")
|
2016-10-30 22:24:50 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2017-04-12 18:42:28 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local InputDialog = require("ui/widget/inputdialog")
|
2016-12-20 07:19:54 +00:00
|
|
|
local PluginLoader = require("pluginloader")
|
2017-01-02 18:12:34 +00:00
|
|
|
local ReaderDictionary = require("apps/reader/modules/readerdictionary")
|
2017-04-12 18:42:28 +00:00
|
|
|
local ReaderUI = require("apps/reader/readerui")
|
2017-05-12 16:28:42 +00:00
|
|
|
local ReaderWikipedia = require("apps/reader/modules/readerwikipedia")
|
2017-04-12 18:42:28 +00:00
|
|
|
local Screenshoter = require("ui/widget/screenshoter")
|
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
|
|
|
local VerticalSpan = require("ui/widget/verticalspan")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2017-07-01 10:11:44 +00:00
|
|
|
local filemanagerutil = require("apps/filemanager/filemanagerutil")
|
2017-04-12 18:42:28 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
|
|
|
local logger = require("logger")
|
|
|
|
local util = require("ffi/util")
|
|
|
|
local _ = require("gettext")
|
|
|
|
local Screen = Device.screen
|
2016-02-16 06:34:28 +00:00
|
|
|
|
|
|
|
local function restoreScreenMode()
|
2016-02-02 02:25:23 +00:00
|
|
|
local screen_mode = G_reader_settings:readSetting("fm_screen_mode")
|
|
|
|
if Screen:getScreenMode() ~= screen_mode then
|
|
|
|
Screen:setScreenMode(screen_mode or "portrait")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local FileManager = InputContainer:extend{
|
2017-02-23 19:27:03 +00:00
|
|
|
title = _("KOReader File Browser"),
|
2014-03-13 13:52:43 +00:00
|
|
|
root_path = lfs.currentdir(),
|
|
|
|
-- our own size
|
|
|
|
dimen = Geom:new{ w = 400, h = 600 },
|
|
|
|
onExit = function() end,
|
2015-04-22 06:15:04 +00:00
|
|
|
|
|
|
|
mv_bin = Device:isAndroid() and "/system/bin/mv" or "/bin/mv",
|
|
|
|
cp_bin = Device:isAndroid() and "/system/bin/cp" or "/bin/cp",
|
|
|
|
rm_bin = Device:isAndroid() and "/system/bin/rm" or "/bin/rm",
|
2013-08-14 09:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function FileManager:init()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.show_parent = self.show_parent or self
|
|
|
|
|
2016-07-05 06:14:53 +00:00
|
|
|
self.path_text = TextWidget:new{
|
2017-04-29 08:38:09 +00:00
|
|
|
face = Font:getFace("xx_smallinfofont"),
|
2017-07-01 10:11:44 +00:00
|
|
|
text = filemanagerutil.abbreviate(self.root_path),
|
2016-07-05 06:14:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 08:52:33 +00:00
|
|
|
self.banner = FrameContainer:new{
|
|
|
|
padding = 0,
|
|
|
|
bordersize = 0,
|
|
|
|
VerticalGroup:new{
|
|
|
|
TextWidget:new{
|
2017-04-29 08:38:09 +00:00
|
|
|
face = Font:getFace("smalltfont"),
|
2016-07-09 08:52:33 +00:00
|
|
|
text = self.title,
|
|
|
|
},
|
|
|
|
CenterContainer:new{
|
|
|
|
dimen = { w = Screen:getWidth(), h = nil },
|
|
|
|
self.path_text,
|
|
|
|
},
|
|
|
|
VerticalSpan:new{ width = Screen:scaleBySize(10) }
|
|
|
|
}
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2014-06-04 09:22:45 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
local g_show_hidden = G_reader_settings:readSetting("show_hidden")
|
|
|
|
local show_hidden = g_show_hidden == nil and DSHOWHIDDENFILES or g_show_hidden
|
|
|
|
local file_chooser = FileChooser:new{
|
|
|
|
-- remeber to adjust the height when new item is added to the group
|
|
|
|
path = self.root_path,
|
2014-12-02 01:51:31 +00:00
|
|
|
collate = G_reader_settings:readSetting("collate") or "strcoll",
|
2014-03-13 13:52:43 +00:00
|
|
|
show_parent = self.show_parent,
|
|
|
|
show_hidden = show_hidden,
|
2014-11-10 12:46:45 +00:00
|
|
|
width = Screen:getWidth(),
|
2014-03-13 13:52:43 +00:00
|
|
|
height = Screen:getHeight() - self.banner:getSize().h,
|
|
|
|
is_popout = false,
|
|
|
|
is_borderless = true,
|
|
|
|
has_close_button = true,
|
|
|
|
file_filter = function(filename)
|
|
|
|
if DocumentRegistry:getProvider(filename) then
|
|
|
|
return true
|
|
|
|
end
|
2014-06-10 07:57:10 +00:00
|
|
|
end,
|
|
|
|
close_callback = function() return self:onClose() end,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
self.file_chooser = file_chooser
|
|
|
|
|
2016-07-05 00:38:04 +00:00
|
|
|
function file_chooser:onPathChanged(path) -- luacheck: ignore
|
2017-07-01 10:11:44 +00:00
|
|
|
FileManager.instance.path_text:setText(filemanagerutil.abbreviate(path))
|
2016-07-09 08:52:33 +00:00
|
|
|
UIManager:setDirty(FileManager.instance, function()
|
|
|
|
return "ui", FileManager.instance.banner.dimen
|
|
|
|
end)
|
2016-07-05 00:38:04 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-01-03 09:18:54 +00:00
|
|
|
function file_chooser:onFileSelect(file) -- luacheck: ignore
|
2016-05-30 02:36:51 +00:00
|
|
|
FileManager.instance:onClose()
|
2014-09-03 04:09:25 +00:00
|
|
|
ReaderUI:showReader(file)
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
2014-06-04 09:22:45 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
local copyFile = function(file) self:copyFile(file) end
|
|
|
|
local pasteHere = function(file) self:pasteHere(file) end
|
|
|
|
local cutFile = function(file) self:cutFile(file) end
|
|
|
|
local deleteFile = function(file) self:deleteFile(file) end
|
2016-02-18 17:03:27 +00:00
|
|
|
local renameFile = function(file) self:renameFile(file) end
|
2014-03-13 13:52:43 +00:00
|
|
|
local fileManager = self
|
2014-06-04 09:22:45 +00:00
|
|
|
|
2016-01-03 09:18:54 +00:00
|
|
|
function file_chooser:onFileHold(file) -- luacheck: ignore
|
2015-03-12 08:29:15 +00:00
|
|
|
local buttons = {
|
|
|
|
{
|
2014-03-13 13:52:43 +00:00
|
|
|
{
|
2015-03-12 08:29:15 +00:00
|
|
|
text = _("Copy"),
|
|
|
|
callback = function()
|
|
|
|
copyFile(file)
|
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
|
|
|
{
|
2015-03-12 08:29:15 +00:00
|
|
|
text = _("Paste"),
|
|
|
|
enabled = fileManager.clipboard and true or false,
|
|
|
|
callback = function()
|
|
|
|
pasteHere(file)
|
|
|
|
self:refreshPath()
|
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2016-12-03 13:27:32 +00:00
|
|
|
{
|
|
|
|
text = _("Purge .sdr"),
|
2017-02-08 18:37:36 +00:00
|
|
|
enabled = DocSettings:hasSidecarFile(util.realpath(file)),
|
2016-12-03 13:27:32 +00:00
|
|
|
callback = function()
|
2017-02-25 21:16:19 +00:00
|
|
|
local file_abs_path = util.realpath(file)
|
|
|
|
if file_abs_path then
|
|
|
|
local autoremove_deleted_items_from_history = G_reader_settings:readSetting("autoremove_deleted_items_from_history") or false
|
|
|
|
os.remove(DocSettings:getSidecarFile(file_abs_path))
|
|
|
|
-- 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))
|
|
|
|
self:refreshPath()
|
|
|
|
-- also delete from history if autoremove_deleted_items_from_history is enabled
|
|
|
|
if autoremove_deleted_items_from_history then
|
|
|
|
require("readhistory"):removeItemByPath(file_abs_path)
|
2016-12-03 13:27:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
end,
|
|
|
|
},
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2015-03-12 08:29:15 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cut"),
|
|
|
|
callback = function()
|
|
|
|
cutFile(file)
|
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Delete"),
|
|
|
|
callback = function()
|
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
text = _("Are you sure that you want to delete this file?\n") .. file .. ("\n") .. _("If you delete a file, it is permanently lost."),
|
2017-04-04 13:31:13 +00:00
|
|
|
ok_text = _("Delete"),
|
2015-03-12 08:29:15 +00:00
|
|
|
ok_callback = function()
|
2017-02-25 21:16:19 +00:00
|
|
|
local autoremove_deleted_items_from_history = G_reader_settings:readSetting("autoremove_deleted_items_from_history") or false
|
|
|
|
local file_abs_path = util.realpath(file)
|
2015-03-12 08:29:15 +00:00
|
|
|
deleteFile(file)
|
2017-02-25 21:16:19 +00:00
|
|
|
-- also delete from history if autoremove_deleted_items_from_history is enabled
|
|
|
|
if autoremove_deleted_items_from_history then
|
|
|
|
if file_abs_path then
|
|
|
|
require("readhistory"):removeItemByPath(file_abs_path)
|
|
|
|
end
|
|
|
|
end
|
2015-03-12 08:29:15 +00:00
|
|
|
self:refreshPath()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
2016-02-18 17:03:27 +00:00
|
|
|
{
|
|
|
|
text = _("Rename"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
fileManager.rename_dialog = InputDialog:new{
|
|
|
|
title = _("Rename file"),
|
|
|
|
input = util.basename(file),
|
|
|
|
buttons = {{
|
|
|
|
{
|
2017-04-04 13:31:13 +00:00
|
|
|
text = _("Cancel"),
|
2016-02-18 17:03:27 +00:00
|
|
|
enabled = true,
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(fileManager.rename_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
2017-04-04 13:31:13 +00:00
|
|
|
text = _("Rename"),
|
2016-02-18 17:03:27 +00:00
|
|
|
enabled = true,
|
|
|
|
callback = function()
|
2017-04-04 13:31:13 +00:00
|
|
|
renameFile(file)
|
|
|
|
self:refreshPath()
|
2016-02-18 17:03:27 +00:00
|
|
|
UIManager:close(fileManager.rename_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
fileManager.rename_dialog:onShowKeyboard()
|
|
|
|
UIManager:show(fileManager.rename_dialog)
|
|
|
|
end,
|
|
|
|
}
|
2015-03-12 08:29:15 +00:00
|
|
|
},
|
2017-04-12 18:42:28 +00:00
|
|
|
-- a little hack to get visual functionality grouping
|
|
|
|
{},
|
2016-10-30 22:24:50 +00:00
|
|
|
{
|
2017-04-12 18:42:28 +00:00
|
|
|
{
|
|
|
|
text = _("Convert"),
|
|
|
|
enabled = lfs.attributes(file, "mode") == "file"
|
|
|
|
and FileManagerConverter:isSupported(file),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
FileManagerConverter:showConvertButtons(file, self)
|
|
|
|
end,
|
|
|
|
},
|
2016-10-30 22:24:50 +00:00
|
|
|
{
|
|
|
|
text = _("Book information"),
|
2017-07-01 10:11:44 +00:00
|
|
|
enabled = FileManagerBookInfo:isSupported(file),
|
2016-10-30 22:24:50 +00:00
|
|
|
callback = function()
|
2017-07-01 10:11:44 +00:00
|
|
|
FileManagerBookInfo:show(file)
|
2016-10-30 22:24:50 +00:00
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
2015-03-12 08:29:15 +00:00
|
|
|
}
|
|
|
|
if lfs.attributes(file, "mode") == "directory" then
|
|
|
|
local realpath = util.realpath(file)
|
|
|
|
table.insert(buttons, {
|
|
|
|
{
|
|
|
|
text = _("Set as HOME directory"),
|
|
|
|
callback = function()
|
|
|
|
G_reader_settings:saveSetting("home_dir", realpath)
|
|
|
|
UIManager:close(self.file_dialog)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
2016-12-11 00:06:10 +00:00
|
|
|
|
|
|
|
self.file_dialog = ButtonDialogTitle:new{
|
|
|
|
title = file:match("([^/]+)$"),
|
|
|
|
title_align = "center",
|
2015-03-12 08:29:15 +00:00
|
|
|
buttons = buttons,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
UIManager:show(self.file_dialog)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
self.layout = VerticalGroup:new{
|
|
|
|
self.banner,
|
|
|
|
file_chooser,
|
|
|
|
}
|
|
|
|
|
|
|
|
local fm_ui = FrameContainer:new{
|
|
|
|
padding = 0,
|
|
|
|
bordersize = 0,
|
2014-10-22 13:34:11 +00:00
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
2014-03-13 13:52:43 +00:00
|
|
|
self.layout,
|
|
|
|
}
|
|
|
|
|
|
|
|
self[1] = fm_ui
|
|
|
|
|
|
|
|
self.menu = FileManagerMenu:new{
|
|
|
|
ui = self
|
|
|
|
}
|
2016-01-09 07:30:02 +00:00
|
|
|
self.active_widgets = { Screenshoter:new{ prefix = 'FileManager' } }
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(self, self.menu)
|
|
|
|
table.insert(self, FileManagerHistory:new{
|
|
|
|
ui = self,
|
|
|
|
})
|
2017-01-02 18:12:34 +00:00
|
|
|
table.insert(self, ReaderDictionary:new{ ui = self })
|
2017-05-12 16:28:42 +00:00
|
|
|
table.insert(self, ReaderWikipedia:new{ ui = self })
|
2014-03-13 13:52:43 +00:00
|
|
|
|
2016-12-20 07:19:54 +00:00
|
|
|
-- koreader plugins
|
|
|
|
for _,plugin_module in ipairs(PluginLoader:loadPlugins()) do
|
|
|
|
if not plugin_module.is_doc_only then
|
2017-04-21 06:37:04 +00:00
|
|
|
local ok, plugin_or_err = PluginLoader:createPluginInstance(
|
|
|
|
plugin_module, { ui = self, })
|
2016-12-20 07:19:54 +00:00
|
|
|
-- Keep references to the modules which do not register into menu.
|
2017-04-21 06:37:04 +00:00
|
|
|
if ok then
|
2017-05-16 09:11:11 +00:00
|
|
|
table.insert(self, plugin_or_err)
|
2017-04-21 06:37:04 +00:00
|
|
|
logger.info("FM loaded plugin", plugin_module.name,
|
|
|
|
"at", plugin_module.path)
|
|
|
|
end
|
2016-12-20 07:19:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-06 05:39:38 +00:00
|
|
|
if Device:hasKeys() then
|
2016-11-12 19:37:19 +00:00
|
|
|
self.key_events.Close = { {"Home"}, doc = "Close file manager" }
|
2014-11-06 05:39:38 +00:00
|
|
|
end
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
self:handleEvent(Event:new("SetDimensions", self.dimen))
|
2013-08-14 09:29:05 +00:00
|
|
|
end
|
|
|
|
|
2016-04-23 14:56:56 +00:00
|
|
|
function FileManager:reinit(path)
|
|
|
|
self.dimen = Screen:getSize()
|
2015-02-01 17:36:49 +00:00
|
|
|
-- backup the root path and path items
|
2016-04-23 14:56:56 +00:00
|
|
|
self.root_path = path or self.file_chooser.path
|
2015-02-01 17:36:49 +00:00
|
|
|
local path_items_backup = {}
|
|
|
|
for k, v in pairs(self.file_chooser.path_items) do
|
|
|
|
path_items_backup[k] = v
|
|
|
|
end
|
|
|
|
-- reinit filemanager
|
|
|
|
self:init()
|
|
|
|
self.file_chooser.path_items = path_items_backup
|
|
|
|
self:onRefresh()
|
|
|
|
end
|
|
|
|
|
2013-08-25 04:00:06 +00:00
|
|
|
function FileManager:toggleHiddenFiles()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.file_chooser:toggleHiddenFiles()
|
|
|
|
G_reader_settings:saveSetting("show_hidden", self.file_chooser.show_hidden)
|
2013-08-25 04:00:06 +00:00
|
|
|
end
|
2013-08-14 09:29:05 +00:00
|
|
|
|
2014-10-30 14:41:49 +00:00
|
|
|
function FileManager:setCollate(collate)
|
|
|
|
self.file_chooser:setCollate(collate)
|
|
|
|
G_reader_settings:saveSetting("collate", self.file_chooser.collate)
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManager:toggleReverseCollate()
|
|
|
|
self.file_chooser:toggleReverseCollate()
|
|
|
|
G_reader_settings:saveSetting("reverse_collate", self.file_chooser.reverse_collate)
|
|
|
|
end
|
|
|
|
|
2013-08-14 09:29:05 +00:00
|
|
|
function FileManager:onClose()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("close filemanager")
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:close(self)
|
|
|
|
if self.onExit then
|
|
|
|
self:onExit()
|
|
|
|
end
|
|
|
|
return true
|
2013-08-14 09:29:05 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2014-09-10 04:21:07 +00:00
|
|
|
function FileManager:onRefresh()
|
|
|
|
self.file_chooser:refreshPath()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2014-01-18 15:15:44 +00:00
|
|
|
function FileManager:copyFile(file)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.cutfile = false
|
|
|
|
self.clipboard = file
|
2014-01-18 15:15:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileManager:cutFile(file)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.cutfile = true
|
|
|
|
self.clipboard = file
|
2014-01-18 15:15:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileManager:pasteHere(file)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.clipboard then
|
|
|
|
file = util.realpath(file)
|
|
|
|
local orig = util.realpath(self.clipboard)
|
|
|
|
local dest = lfs.attributes(file, "mode") == "directory" and
|
|
|
|
file or file:match("(.*/)")
|
|
|
|
if self.cutfile then
|
2016-12-03 13:27:32 +00:00
|
|
|
-- if we move a file, also move its sidecar directory
|
2017-02-19 11:24:37 +00:00
|
|
|
if DocSettings:hasSidecarFile(orig) then
|
2016-12-03 13:27:32 +00:00
|
|
|
self:moveFile(DocSettings:getSidecarDir(orig), dest) -- dest is always a directory
|
|
|
|
end
|
2016-02-18 17:03:27 +00:00
|
|
|
self:moveFile(orig, dest)
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
2015-04-22 06:15:04 +00:00
|
|
|
util.execute(self.cp_bin, "-r", orig, dest)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
2014-01-18 15:15:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileManager:deleteFile(file)
|
2016-01-07 06:44:07 +00:00
|
|
|
local ok, err
|
|
|
|
local file_abs_path = util.realpath(file)
|
|
|
|
if file_abs_path == nil then
|
2014-11-13 11:28:57 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
2016-01-07 06:44:07 +00:00
|
|
|
text = util.template(_("File %1 not found"), file),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local is_doc = DocumentRegistry:getProvider(file_abs_path)
|
|
|
|
ok, err = os.remove(file_abs_path)
|
2016-02-16 06:34:28 +00:00
|
|
|
if ok and err == nil then
|
2016-01-07 06:44:07 +00:00
|
|
|
if is_doc ~= nil then
|
2016-06-04 05:05:14 +00:00
|
|
|
DocSettings:open(file):purge()
|
2016-01-07 06:44:07 +00:00
|
|
|
end
|
|
|
|
UIManager:show(InfoMessage:new{
|
2016-04-16 10:21:49 +00:00
|
|
|
text = util.template(_("Deleted %1"), file),
|
2014-11-14 20:57:06 +00:00
|
|
|
timeout = 2,
|
2014-11-13 11:28:57 +00:00
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2016-01-07 06:44:07 +00:00
|
|
|
text = util.template(_("An error occurred while trying to delete %1"), file),
|
2014-11-13 11:28:57 +00:00
|
|
|
})
|
|
|
|
end
|
2014-01-18 15:15:44 +00:00
|
|
|
end
|
|
|
|
|
2016-02-18 17:03:27 +00:00
|
|
|
function FileManager:renameFile(file)
|
|
|
|
if util.basename(file) ~= self.rename_dialog:getInputText() then
|
|
|
|
local dest = util.joinPath(util.dirname(file), self.rename_dialog:getInputText())
|
|
|
|
if self:moveFile(file, dest) then
|
|
|
|
if lfs.attributes(dest, "mode") == "file" then
|
|
|
|
local doc = require("docsettings")
|
|
|
|
local move_history = true;
|
|
|
|
if lfs.attributes(doc:getHistoryPath(file), "mode") == "file" and
|
|
|
|
not self:moveFile(doc:getHistoryPath(file), doc:getHistoryPath(dest)) then
|
|
|
|
move_history = false
|
|
|
|
end
|
|
|
|
if lfs.attributes(doc:getSidecarDir(file), "mode") == "directory" and
|
|
|
|
not self:moveFile(doc:getSidecarDir(file), doc:getSidecarDir(dest)) then
|
|
|
|
move_history = false
|
|
|
|
end
|
|
|
|
if move_history then
|
|
|
|
UIManager:show(InfoMessage:new{
|
2016-04-16 10:21:49 +00:00
|
|
|
text = util.template(_("Renamed from %1 to %2"), file, dest),
|
2016-02-18 17:03:27 +00:00
|
|
|
timeout = 2,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2017-05-08 07:26:01 +00:00
|
|
|
text = util.template(
|
|
|
|
_("Failed to move history data of %1 to %2.\nThe reading history may be lost."),
|
|
|
|
file, dest),
|
2016-02-18 17:03:27 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = util.template(
|
2017-05-08 07:26:01 +00:00
|
|
|
_("Failed to rename from %1 to %2"), file, dest),
|
2016-02-18 17:03:27 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-30 14:41:49 +00:00
|
|
|
function FileManager:getSortingMenuTable()
|
|
|
|
local fm = self
|
2014-11-28 11:48:15 +00:00
|
|
|
local collates = {
|
2017-02-12 02:55:31 +00:00
|
|
|
strcoll = {_("title"), _("Sort by title")},
|
2017-02-16 03:47:45 +00:00
|
|
|
access = {_("date read"), _("Sort by last read date")},
|
|
|
|
change = {_("date added"), _("Sort by date added")},
|
|
|
|
modification = {_("date modified"), _("Sort by date modified")},
|
2017-02-12 02:55:31 +00:00
|
|
|
size = {_("size"), _("Sort by size")},
|
|
|
|
type = {_("type"), _("Sort by type")},
|
2014-11-28 11:48:15 +00:00
|
|
|
}
|
2014-10-30 14:41:49 +00:00
|
|
|
local set_collate_table = function(collate)
|
|
|
|
return {
|
2014-11-28 11:48:15 +00:00
|
|
|
text = collates[collate][2],
|
2014-10-30 14:41:49 +00:00
|
|
|
checked_func = function()
|
|
|
|
return fm.file_chooser.collate == collate
|
|
|
|
end,
|
|
|
|
callback = function() fm:setCollate(collate) end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
return {
|
|
|
|
text_func = function()
|
2014-11-28 11:48:15 +00:00
|
|
|
return util.template(
|
2017-08-14 11:15:12 +00:00
|
|
|
_("Sort by: %1"),
|
2014-11-28 11:48:15 +00:00
|
|
|
collates[fm.file_chooser.collate][1]
|
|
|
|
)
|
2014-10-30 14:41:49 +00:00
|
|
|
end,
|
|
|
|
sub_item_table = {
|
|
|
|
set_collate_table("strcoll"),
|
|
|
|
set_collate_table("access"),
|
2017-02-12 02:55:31 +00:00
|
|
|
set_collate_table("change"),
|
|
|
|
set_collate_table("modification"),
|
|
|
|
set_collate_table("size"),
|
|
|
|
set_collate_table("type"),
|
2014-10-30 14:41:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-08-14 11:15:12 +00:00
|
|
|
function FileManager:getStartWithMenuTable()
|
|
|
|
local start_with_setting = G_reader_settings:readSetting("start_with") or "filemanager"
|
|
|
|
local start_withs = {
|
|
|
|
filemanager = {_("file browser"), _("Start with file browser")},
|
|
|
|
history = {_("history"), _("Start with history")},
|
|
|
|
last = {_("last file"), _("Start with last file")},
|
|
|
|
}
|
|
|
|
local set_sw_table = function(start_with)
|
|
|
|
return {
|
|
|
|
text = start_withs[start_with][2],
|
|
|
|
checked_func = function()
|
|
|
|
return start_with_setting == start_with
|
|
|
|
end,
|
|
|
|
callback = function()
|
|
|
|
start_with_setting = start_with
|
|
|
|
G_reader_settings:saveSetting("start_with", start_with)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
return {
|
|
|
|
text_func = function()
|
|
|
|
return util.template(
|
|
|
|
_("Start with: %1"),
|
|
|
|
start_withs[start_with_setting][1]
|
|
|
|
)
|
|
|
|
end,
|
|
|
|
sub_item_table = {
|
|
|
|
set_sw_table("filemanager"),
|
|
|
|
set_sw_table("history"),
|
|
|
|
set_sw_table("last"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-02-02 02:25:23 +00:00
|
|
|
function FileManager:showFiles(path)
|
2017-07-01 10:11:44 +00:00
|
|
|
path = path or G_reader_settings:readSetting("lastdir") or filemanagerutil.getDefaultDir()
|
2016-02-02 02:25:23 +00:00
|
|
|
G_reader_settings:saveSetting("lastdir", path)
|
|
|
|
restoreScreenMode()
|
|
|
|
local file_manager = FileManager:new{
|
|
|
|
dimen = Screen:getSize(),
|
|
|
|
root_path = path,
|
|
|
|
onExit = function()
|
|
|
|
self.instance = nil
|
|
|
|
end
|
|
|
|
}
|
|
|
|
UIManager:show(file_manager)
|
|
|
|
self.instance = file_manager
|
|
|
|
end
|
|
|
|
|
2016-02-18 17:03:27 +00:00
|
|
|
--[[
|
|
|
|
A shortcut to execute mv command (self.mv_bin) with from and to as parameters.
|
|
|
|
Returns a boolean value to indicate the result of mv command.
|
|
|
|
--]]
|
|
|
|
function FileManager:moveFile(from, to)
|
|
|
|
return util.execute(self.mv_bin, from, to) == 0
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return FileManager
|