ArchiveViewer: view files (#10683)

reviewable/pr10696/r1
hius07 11 months ago committed by GitHub
parent 890dc81081
commit 3e43a21cf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,8 @@
local BD = require("ui/bidi") local BD = require("ui/bidi")
local ButtonDialog = require("ui/widget/buttondialog")
local CenterContainer = require("ui/widget/container/centercontainer") local CenterContainer = require("ui/widget/container/centercontainer")
local ConfirmBox = require("ui/widget/confirmbox") local DocumentRegistry = require("document/documentregistry")
local InfoMessage = require("ui/widget/infomessage")
local Menu = require("ui/widget/menu") local Menu = require("ui/widget/menu")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer") local WidgetContainer = require("ui/widget/container/widgetcontainer")
@ -58,7 +60,7 @@ function ArchiveViewer:openArchiveViewer(file)
local title = item.path == "" and filename or filename.."/"..item.path local title = item.path == "" and filename or filename.."/"..item.path
self_menu:switchItemTable(title, self:getItemTable(item.path)) self_menu:switchItemTable(title, self:getItemTable(item.path))
else else
self:extractFile(file, item.path) self:showFileDialog(file, item.path)
end end
end, end,
close_callback = function() close_callback = function()
@ -96,10 +98,13 @@ function ArchiveViewer:getZipListTable(file)
end end
local std_out = io.popen("unzip ".."-qql \""..file.."\"") local std_out = io.popen("unzip ".."-qql \""..file.."\"")
for line in std_out:lines() do if std_out then
-- entry datetime not used so far for line in std_out:lines() do
local fsize, fname = string.match(line, "%s+(%d+)%s+[-0-9]+%s+[0-9:]+%s+(.+)") -- entry datetime not used so far
parse_path(fname, fsize or 0) local fsize, fname = string.match(line, "%s+(%d+)%s+[-0-9]+%s+[0-9:]+%s+(.+)")
parse_path(fname, fsize or 0)
end
std_out:close()
end end
end end
@ -168,21 +173,82 @@ function ArchiveViewer:getItemDirMandatory(name)
return text return text
end end
function ArchiveViewer:showFileDialog(arcfile, filepath)
local dialog
local buttons = {
{
{
text = _("Extract"),
callback = function()
UIManager:close(dialog)
self:extractFile(arcfile, filepath)
end,
},
{
text = _("View"),
callback = function()
UIManager:close(dialog)
self:viewFile(arcfile, filepath)
end,
},
},
}
dialog = ButtonDialog:new{
title = filepath .. "\n\n" .. _("On extraction, if the file already exists, it will be overwritten."),
width_factor = 0.8,
buttons = buttons,
}
UIManager:show(dialog)
end
function ArchiveViewer:viewFile(arcfile, filepath)
local content
if self.arc_type == "zip" then
local std_out = io.popen("unzip ".."-qp \""..arcfile.."\"".." ".."\""..filepath.."\"")
if std_out then
content = std_out:read("*all")
std_out:close()
else
UIManager:show(InfoMessage:new{
text = _("Cannot extract file content."),
icon = "notice-warning",
})
return
end
else
return
end
if DocumentRegistry:isImageFile(filepath) then
local RenderImage = require("ui/renderimage")
local ImageViewer = require("ui/widget/imageviewer")
UIManager:show(ImageViewer:new{
image = RenderImage:renderImageData(content, #content),
fullscreen = true,
with_title_bar = false,
})
else
local TextViewer = require("ui/widget/textviewer")
UIManager:show(TextViewer:new{
title = filepath,
title_multilines = true,
text = content,
justified = false,
})
end
end
function ArchiveViewer:extractFile(arcfile, filepath) function ArchiveViewer:extractFile(arcfile, filepath)
UIManager:show(ConfirmBox:new{ if self.arc_type == "zip" then
text = _("Extract this file?") .. "\n\n" .. filepath .. "\n\n" .. local std_out = io.popen("unzip ".."-qo \""..arcfile.."\"".." ".."\""..filepath.."\""..
_("If the file already exists, it will be overwritten."), " -d ".."\""..util.splitFilePathName(arcfile).."\"")
ok_text = _("Extract"), if std_out then
ok_callback = function() std_out:close()
if self.arc_type == "zip" then end
io.popen("unzip ".."-qo \""..arcfile.."\"".." ".."\""..filepath.."\"".. else
" -d ".."\""..util.splitFilePathName(arcfile).."\"") return
else -- add other archivers here end
return self.fm_updated = true
end
self.fm_updated = true
end,
})
end end
return ArchiveViewer return ArchiveViewer

Loading…
Cancel
Save