Skip to content

Commit

Permalink
filemanager: "PathChanged" event
Browse files Browse the repository at this point in the history
  • Loading branch information
hius07 authored Oct 30, 2024
1 parent 186bc7e commit 33e8b7e
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions frontend/apps/filemanager/filemanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ local ReadHistory = require("readhistory")
local Screenshoter = require("ui/widget/screenshoter")
local TitleBar = require("ui/widget/titlebar")
local UIManager = require("ui/uimanager")
local ffiUtil = require("ffi/util")
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local BaseUtil = require("ffi/util")
local util = require("util")
local _ = require("gettext")
local C_ = _.pgettext
local N_ = _.ngettext
local Screen = Device.screen
local T = BaseUtil.template
local T = ffiUtil.template

local FileManager = InputContainer:extend{
title = _("KOReader"),
Expand Down Expand Up @@ -106,14 +106,15 @@ function FileManager:onSetDimensions(dimen)
end

function FileManager:updateTitleBarPath(path)
path = path or self.file_chooser.path
local text = BD.directory(filemanagerutil.abbreviate(path))
if FileManagerShortcuts:hasFolderShortcut(path) then
if self.folder_shortcuts:hasFolderShortcut(path) then
text = "" .. text
end
self.title_bar:setSubTitle(text)
end

FileManager.onPathChanged = FileManager.updateTitleBarPath

function FileManager:setupLayout()
self.show_parent = self.show_parent or self
self.title_bar = TitleBar:new{
Expand All @@ -135,7 +136,6 @@ function FileManager:setupLayout()
right_icon_tap_callback = function() self:onShowPlusMenu() end,
right_icon_hold_callback = false, -- propagate long-press to dispatcher
}
self:updateTitleBarPath(self.root_path)

local file_chooser = FileChooser:new{
path = self.root_path,
Expand All @@ -158,11 +158,6 @@ function FileManager:setupLayout()

local file_manager = self

function file_chooser:onPathChanged(path)
file_manager:updateTitleBarPath(path)
return true
end

function file_chooser:onFileSelect(item)
if file_manager.selected_files then -- toggle selection
item.dim = not item.dim and true or nil
Expand Down Expand Up @@ -315,7 +310,7 @@ function FileManager:setupLayout()
})
end
else -- folder
local folder = BaseUtil.realpath(file)
local folder = ffiUtil.realpath(file)
table.insert(buttons, {
{
text = _("Set as HOME folder"),
Expand Down Expand Up @@ -431,6 +426,7 @@ function FileManager:init()

self:initGesListener()
self:handleEvent(Event:new("SetDimensions", self.dimen))
self:handleEvent(Event:new("PathChanged", self.file_chooser.path))

if FileManager.instance == nil then
logger.dbg("Spinning up new FileManager instance", tostring(self))
Expand Down Expand Up @@ -726,7 +722,7 @@ function FileManager:reinit(path, focused_file)
UIManager:flushSettings()
self.dimen = Screen:getSize()
-- backup the root path and path items
self.root_path = BaseUtil.realpath(path or self.file_chooser.path)
self.root_path = ffiUtil.realpath(path or self.file_chooser.path)
local path_items_backup = {}
for k, v in pairs(self.file_chooser.path_items) do
path_items_backup[k] = v
Expand Down Expand Up @@ -826,7 +822,7 @@ function FileManager:openRandomFile(dir)
local random_file = filemanagerutil.getRandomFile(dir, match_func)
if random_file then
UIManager:show(MultiConfirmBox:new{
text = T(_("Do you want to open %1?"), BD.filename(BaseUtil.basename(random_file))),
text = T(_("Do you want to open %1?"), BD.filename(ffiUtil.basename(random_file))),
choice1_text = _("Open"),
choice1_callback = function()
local ReaderUI = require("apps/reader/readerui")
Expand Down Expand Up @@ -856,11 +852,11 @@ function FileManager:cutFile(file)
end

function FileManager:pasteFileFromClipboard(file)
local orig_file = BaseUtil.realpath(self.clipboard)
local orig_name = BaseUtil.basename(orig_file)
local dest_path = BaseUtil.realpath(file or self.file_chooser.path)
local orig_file = ffiUtil.realpath(self.clipboard)
local orig_name = ffiUtil.basename(orig_file)
local dest_path = ffiUtil.realpath(file or self.file_chooser.path)
dest_path = isFile(dest_path) and dest_path:match("(.*/)") or dest_path
local dest_file = BaseUtil.joinPath(dest_path, orig_name)
local dest_file = ffiUtil.joinPath(dest_path, orig_name)
if orig_file == dest_file or orig_file == dest_path then -- do not paste to itself
self.clipboard = nil
return
Expand Down Expand Up @@ -951,12 +947,12 @@ function FileManager:showCopyMoveSelectedFilesDialog(close_callback)
end

function FileManager:pasteSelectedFiles(overwrite)
local dest_path = BaseUtil.realpath(self.file_chooser.path)
local dest_path = ffiUtil.realpath(self.file_chooser.path)
local ok_files = {}
for orig_file in pairs(self.selected_files) do
local orig_name = BaseUtil.basename(orig_file)
local dest_file = BaseUtil.joinPath(dest_path, orig_name)
if BaseUtil.realpath(orig_file) == dest_file then -- do not paste to itself
local orig_name = ffiUtil.basename(orig_file)
local dest_file = ffiUtil.joinPath(dest_path, orig_name)
if ffiUtil.realpath(orig_file) == dest_file then -- do not paste to itself
self.selected_files[orig_file] = nil
else
local ok
Expand Down Expand Up @@ -1046,7 +1042,7 @@ function FileManager:createFolder()
end

function FileManager:showDeleteFileDialog(filepath, post_delete_callback, pre_delete_callback)
local file = BaseUtil.realpath(filepath)
local file = ffiUtil.realpath(filepath)
if file == nil then
UIManager:show(InfoMessage:new{
text = T(_("File not found:\n%1"), BD.filepath(filepath)),
Expand Down Expand Up @@ -1083,7 +1079,7 @@ function FileManager:deleteFile(file, is_file)
return true
end
else
local ok = BaseUtil.purgeDir(file)
local ok = ffiUtil.purgeDir(file)
if ok then
ReadHistory:folderDeleted(file) -- will delete sdr
ReadCollection:removeItemsByPath(file)
Expand All @@ -1099,7 +1095,7 @@ end
function FileManager:deleteSelectedFiles()
local ok_files = {}
for orig_file in pairs(self.selected_files) do
local file_abs_path = BaseUtil.realpath(orig_file)
local file_abs_path = ffiUtil.realpath(orig_file)
local ok = file_abs_path and os.remove(file_abs_path)
if ok then
DocSettings.updateLocation(file_abs_path) -- delete sdr
Expand Down Expand Up @@ -1129,7 +1125,7 @@ function FileManager:showRenameFileDialog(file, is_file)
local dialog
dialog = InputDialog:new{
title = is_file and _("Rename file") or _("Rename folder"),
input = BaseUtil.basename(file),
input = ffiUtil.basename(file),
buttons = {{
{
text = _("Cancel"),
Expand All @@ -1155,8 +1151,8 @@ function FileManager:showRenameFileDialog(file, is_file)
end

function FileManager:renameFile(file, basename, is_file)
if BaseUtil.basename(file) == basename then return end
local dest = BaseUtil.joinPath(BaseUtil.dirname(file), basename)
if ffiUtil.basename(file) == basename then return end
local dest = ffiUtil.joinPath(ffiUtil.dirname(file), basename)

local function doRenameFile()
if self:moveFile(file, dest) then
Expand Down Expand Up @@ -1220,7 +1216,7 @@ function FileManager:showFiles(path, focused_file, selected_files)
FileManager.instance:onClose()
end

path = BaseUtil.realpath(path or G_reader_settings:readSetting("lastdir") or filemanagerutil.getDefaultDir())
path = ffiUtil.realpath(path or G_reader_settings:readSetting("lastdir") or filemanagerutil.getDefaultDir())
G_reader_settings:saveSetting("lastdir", path)
self:setRotationMode()
local file_manager = FileManager:new{
Expand All @@ -1236,19 +1232,19 @@ end
--- A shortcut to execute mv.
-- @treturn boolean result of mv command
function FileManager:moveFile(from, to)
return BaseUtil.execute(self.mv_bin, from, to) == 0
return ffiUtil.execute(self.mv_bin, from, to) == 0
end

--- A shortcut to execute cp.
-- @treturn boolean result of cp command
function FileManager:copyFileFromTo(from, to)
return BaseUtil.execute(self.cp_bin, from, to) == 0
return ffiUtil.execute(self.cp_bin, from, to) == 0
end

--- A shortcut to execute cp recursively.
-- @treturn boolean result of cp command
function FileManager:copyRecursive(from, to)
return BaseUtil.execute(self.cp_bin, "-r", from, to ) == 0
return ffiUtil.execute(self.cp_bin, "-r", from, to ) == 0
end

function FileManager:onHome()
Expand Down Expand Up @@ -1341,9 +1337,9 @@ function FileManager:showSelectedFilesList()
local a_path, a_name = util.splitFilePathName(a.text)
local b_path, b_name = util.splitFilePathName(b.text)
if a_path == b_path then
return BaseUtil.strcoll(a_name, b_name)
return ffiUtil.strcoll(a_name, b_name)
end
return BaseUtil.strcoll(a_path, b_path)
return ffiUtil.strcoll(a_path, b_path)
end
table.sort(selected_files, sorting)

Expand Down

0 comments on commit 33e8b7e

Please sign in to comment.