2014-01-18 15:15:44 +00:00
|
|
|
local FileManagerHistory = require("apps/filemanager/filemanagerhistory")
|
2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
2014-01-18 15:15:44 +00:00
|
|
|
local FileManagerMenu = require("apps/filemanager/filemanagermenu")
|
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2013-10-18 20:38:07 +00:00
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
|
|
|
local FileChooser = require("ui/widget/filechooser")
|
2013-12-19 15:21:41 +00:00
|
|
|
local VerticalSpan = require("ui/widget/verticalspan")
|
2013-10-18 20:38:07 +00:00
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
2014-01-22 18:03:44 +00:00
|
|
|
local ButtonDialog = require("ui/widget/buttondialog")
|
2014-08-14 11:49:42 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2014-01-18 15:15:44 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Font = require("ui/font")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Screen = require("device").screen
|
2013-10-18 20:38:07 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Event = require("ui/event")
|
2014-01-18 15:15:44 +00:00
|
|
|
local DEBUG = require("dbg")
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
2014-06-04 09:22:45 +00:00
|
|
|
local util = require("ffi/util")
|
2014-10-22 13:34:11 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-08-14 09:29:05 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local FileManager = InputContainer:extend{
|
2014-03-13 13:52:43 +00:00
|
|
|
title = _("FileManager"),
|
|
|
|
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
|
|
|
|
|
|
|
|
self.banner = VerticalGroup:new{
|
|
|
|
TextWidget:new{
|
|
|
|
face = Font:getFace("tfont", 24),
|
|
|
|
text = self.title,
|
|
|
|
},
|
2014-11-20 22:07:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
function file_chooser:onFileSelect(file)
|
2014-09-03 04:09:25 +00:00
|
|
|
local ReaderUI = require("apps/reader/readerui")
|
|
|
|
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
|
|
|
|
local fileManager = self
|
2014-06-04 09:22:45 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
function file_chooser:onFileHold(file)
|
|
|
|
--DEBUG("hold file", file)
|
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
|
|
|
},
|
|
|
|
},
|
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."),
|
|
|
|
ok_callback = function()
|
|
|
|
deleteFile(file)
|
|
|
|
self:refreshPath()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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
|
|
|
|
self.file_dialog = ButtonDialog:new{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
table.insert(self, self.menu)
|
|
|
|
table.insert(self, FileManagerHistory:new{
|
|
|
|
ui = self,
|
|
|
|
menu = self.menu
|
|
|
|
})
|
|
|
|
|
2014-11-06 05:39:38 +00:00
|
|
|
if Device:hasKeys() then
|
|
|
|
self.key_events.Close = { {"Home"}, doc = "close filemanager" }
|
|
|
|
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
|
|
|
|
|
2015-02-01 17:36:49 +00:00
|
|
|
function FileManager:resetDimen(dimen)
|
|
|
|
self.dimen = dimen
|
|
|
|
-- backup the root path and path items
|
|
|
|
self.root_path = self.file_chooser.path
|
|
|
|
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()
|
2014-06-10 07:57:10 +00:00
|
|
|
DEBUG("close filemanager")
|
2015-02-01 17:36:49 +00:00
|
|
|
G_reader_settings:saveSetting("fm_screen_mode", Screen:getScreenMode())
|
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-09-07 09:19:51 +00:00
|
|
|
function FileManager:getDefaultDir()
|
|
|
|
if Device:isKindle() then
|
|
|
|
return "/mnt/us/documents"
|
|
|
|
elseif Device:isKobo() then
|
|
|
|
return "/mnt/onboard"
|
2014-10-30 18:42:18 +00:00
|
|
|
elseif Device.isAndroid() then
|
2014-09-07 09:19:51 +00:00
|
|
|
return "/sdcard"
|
|
|
|
else
|
|
|
|
return "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-12 09:25:26 +00:00
|
|
|
function FileManager:restoreScreenMode()
|
|
|
|
local screen_mode = G_reader_settings:readSetting("fm_screen_mode")
|
2015-02-01 18:33:12 +00:00
|
|
|
if Screen:getScreenMode() ~= screen_mode then
|
|
|
|
Screen:setScreenMode(screen_mode or "portrait")
|
|
|
|
end
|
2014-12-03 15:59:48 +00:00
|
|
|
UIManager:setDirty(self, "full")
|
2014-10-12 09:25:26 +00:00
|
|
|
end
|
|
|
|
|
2014-09-03 04:09:25 +00:00
|
|
|
function FileManager:showFiles(path)
|
|
|
|
DEBUG("show home page")
|
2014-09-07 09:19:51 +00:00
|
|
|
path = path or G_reader_settings:readSetting("lastdir") or self:getDefaultDir()
|
2014-09-03 04:09:25 +00:00
|
|
|
G_reader_settings:saveSetting("lastdir", path)
|
2015-02-01 17:36:49 +00:00
|
|
|
self:restoreScreenMode()
|
|
|
|
local file_manager = FileManager:new{
|
2014-09-03 04:09:25 +00:00
|
|
|
dimen = Screen:getSize(),
|
|
|
|
root_path = path,
|
|
|
|
onExit = function()
|
2015-02-01 17:36:49 +00:00
|
|
|
self.instance = nil
|
2014-09-03 04:09:25 +00:00
|
|
|
end
|
2015-02-01 17:36:49 +00:00
|
|
|
}
|
|
|
|
UIManager:show(file_manager)
|
|
|
|
self.instance = file_manager
|
2014-09-03 04:09:25 +00:00
|
|
|
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
|
2015-04-22 06:15:04 +00:00
|
|
|
util.execute(self.mv_bin, 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)
|
2014-11-13 11:28:57 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
DEBUG("File to remove", util.realpath(file))
|
2015-04-22 06:15:04 +00:00
|
|
|
local rm = util.execute(self.rm_bin, "-rf", util.realpath(file))
|
2014-11-13 11:28:57 +00:00
|
|
|
DEBUG("rm status", rm)
|
|
|
|
if rm == 0 then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Successfully deleted\n") .. file,
|
2014-11-14 20:57:06 +00:00
|
|
|
timeout = 2,
|
2014-11-13 11:28:57 +00:00
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("An error occurred while trying to delete\n") .. file,
|
|
|
|
})
|
|
|
|
end
|
2014-01-18 15:15:44 +00:00
|
|
|
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 = {
|
|
|
|
strcoll = {_("by title"), _("Sort by title")},
|
|
|
|
access = {_("by date"), _("Sort by date")},
|
|
|
|
}
|
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(
|
|
|
|
_("Sort order: %1"),
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return FileManager
|