Add rename function for filemanager

pull/1840/head
zijiehe 8 years ago
parent 4484231d27
commit 8861365264

@ -1 +1 @@
Subproject commit 9528f1122b7c0644e8640925cf8eb8b09c8134ea
Subproject commit 74e57274ab8d21b99e614bcf4c050e32f407feee

@ -6,6 +6,7 @@ local DocumentRegistry = require("document/documentregistry")
local VerticalGroup = require("ui/widget/verticalgroup")
local Screenshoter = require("ui/widget/screenshoter")
local ButtonDialog = require("ui/widget/buttondialog")
local InputDialog = require("ui/widget/inputdialog")
local VerticalSpan = require("ui/widget/verticalspan")
local FileChooser = require("ui/widget/filechooser")
local TextWidget = require("ui/widget/textwidget")
@ -28,7 +29,7 @@ local function getDefaultDir()
return "/mnt/us/documents"
elseif Device:isKobo() then
return "/mnt/onboard"
elseif Device.isAndroid() then
elseif Device:isAndroid() then
return "/sdcard"
else
return "."
@ -97,6 +98,7 @@ function FileManager:init()
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 renameFile = function(file) self:renameFile(file) end
local fileManager = self
function file_chooser:onFileHold(file) -- luacheck: ignore
@ -141,6 +143,40 @@ function FileManager:init()
})
end,
},
{
text = _("Rename"),
callback = function()
UIManager:close(self.file_dialog)
fileManager.rename_dialog = InputDialog:new{
title = _("Rename file"),
input = util.basename(file),
buttons = {{
{
text = _("OK"),
enabled = true,
callback = function()
renameFile(file)
self:refreshPath()
fileManager.rename_dialog:onClose()
UIManager:close(fileManager.rename_dialog)
end,
},
{
text = _("Cancel"),
enabled = true,
callback = function()
fileManager.rename_dialog:onClose()
UIManager:close(fileManager.rename_dialog)
end,
},
}},
width = Screen:getWidth() * 0.8,
height = Screen:getHeight() * 0.2,
}
fileManager.rename_dialog:onShowKeyboard()
UIManager:show(fileManager.rename_dialog)
end,
}
},
}
if lfs.attributes(file, "mode") == "directory" then
@ -254,7 +290,7 @@ function FileManager:pasteHere(file)
local dest = lfs.attributes(file, "mode") == "directory" and
file or file:match("(.*/)")
if self.cutfile then
util.execute(self.mv_bin, orig, dest)
self:moveFile(orig, dest)
else
util.execute(self.cp_bin, "-r", orig, dest)
end
@ -296,6 +332,44 @@ function FileManager:deleteFile(file)
end
end
function FileManager:renameFile(file)
local InfoMessage = require("ui/widget/infomessage")
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{
text = util.template(_("Successfully renamed from %1 to %2"), file, dest),
timeout = 2,
})
else
UIManager:show(InfoMessage:new{
text = util.template(_(
"Failed to move history data of %1 to %2.\n" ..
"The reading history may be lost."), file, dest),
})
end
end
else
UIManager:show(InfoMessage:new{
text = util.template(
_("Failed to rename from %1 to %2"), file, dest),
})
end
end
end
function FileManager:getSortingMenuTable()
local fm = self
local collates = {
@ -340,4 +414,12 @@ function FileManager:showFiles(path)
self.instance = file_manager
end
--[[
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
return FileManager

Loading…
Cancel
Save