2020-01-04 00:18:51 +00:00
|
|
|
local BD = require("ui/bidi")
|
2018-08-22 20:34:20 +00:00
|
|
|
local ButtonDialog = require("ui/widget/buttondialog")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2019-03-09 12:55:03 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2018-08-22 20:34:20 +00:00
|
|
|
local InputDialog = require("ui/widget/inputdialog")
|
|
|
|
local Menu = require("ui/widget/menu")
|
|
|
|
local Screen = require("device").screen
|
|
|
|
local UIManager = require("ui/uimanager")
|
2020-09-25 13:54:14 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2018-08-22 20:34:20 +00:00
|
|
|
local util = require("ffi/util")
|
|
|
|
local _ = require("gettext")
|
2020-09-15 18:39:32 +00:00
|
|
|
local T = util.template
|
2018-08-22 20:34:20 +00:00
|
|
|
|
2019-03-09 12:55:03 +00:00
|
|
|
local FileManagerShortcuts = InputContainer:extend{}
|
2018-08-22 20:34:20 +00:00
|
|
|
|
2019-03-09 12:55:03 +00:00
|
|
|
function FileManagerShortcuts:updateItemTable()
|
2018-08-22 20:34:20 +00:00
|
|
|
local item_table = {}
|
|
|
|
local folder_shortcuts = G_reader_settings:readSetting("folder_shortcuts") or {}
|
|
|
|
for _, item in ipairs(folder_shortcuts) do
|
|
|
|
table.insert(item_table, {
|
|
|
|
text = string.format("%s (%s)", item.text, item.folder),
|
|
|
|
folder = item.folder,
|
|
|
|
friendly_name = item.text,
|
|
|
|
deletable = true,
|
|
|
|
editable = true,
|
|
|
|
callback = function()
|
2019-03-09 12:55:03 +00:00
|
|
|
UIManager:close(self.fm_bookmark)
|
|
|
|
|
|
|
|
local folder = item.folder
|
|
|
|
if folder ~= nil and lfs.attributes(folder, "mode") == "directory" then
|
|
|
|
if self.ui.file_chooser then
|
|
|
|
self.ui.file_chooser:changeToPath(folder)
|
|
|
|
else -- called from Reader
|
|
|
|
local FileManager = require("apps/filemanager/filemanager")
|
|
|
|
|
|
|
|
self.ui:onClose()
|
|
|
|
if FileManager.instance then
|
|
|
|
FileManager.instance:reinit(folder)
|
|
|
|
else
|
|
|
|
FileManager:showFiles(folder)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-22 20:34:20 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
2019-03-09 12:55:03 +00:00
|
|
|
|
2020-09-25 13:54:14 +00:00
|
|
|
table.sort(item_table, function(l, r)
|
|
|
|
return l.text < r.text
|
|
|
|
end)
|
|
|
|
table.insert(item_table, 1, {
|
|
|
|
text = _("Add new folder shortcut"),
|
|
|
|
callback = function()
|
|
|
|
self:addNewFolder()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2019-03-09 12:55:03 +00:00
|
|
|
-- try to stay on current page
|
2020-09-25 13:54:14 +00:00
|
|
|
local select_number
|
2019-03-09 12:55:03 +00:00
|
|
|
|
|
|
|
if self.fm_bookmark.page and self.fm_bookmark.perpage then
|
|
|
|
select_number = (self.fm_bookmark.page - 1) * self.fm_bookmark.perpage + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
self.fm_bookmark:switchItemTable(nil,
|
|
|
|
item_table, select_number)
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:addNewFolder()
|
|
|
|
local PathChooser = require("ui/widget/pathchooser")
|
|
|
|
local path_chooser = PathChooser:new{
|
|
|
|
select_directory = true,
|
|
|
|
select_file = false,
|
2019-03-09 12:55:03 +00:00
|
|
|
path = self.fm_bookmark.curr_path,
|
2018-08-22 20:34:20 +00:00
|
|
|
onConfirm = function(path)
|
|
|
|
local add_folder_input
|
|
|
|
local friendly_name = util.basename(path) or _("my folder")
|
|
|
|
add_folder_input = InputDialog:new{
|
|
|
|
title = self.title,
|
|
|
|
input = friendly_name,
|
|
|
|
input_type = "text",
|
2020-01-04 00:18:51 +00:00
|
|
|
description = T(_("Title for selected folder:\n%1"), BD.dirpath(path)),
|
2018-08-22 20:34:20 +00:00
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(add_folder_input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Add"),
|
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
2019-03-09 12:55:03 +00:00
|
|
|
self:addFolderFromInput(add_folder_input:getInputValue(), path)
|
2018-08-22 20:34:20 +00:00
|
|
|
UIManager:close(add_folder_input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
UIManager:show(add_folder_input)
|
|
|
|
add_folder_input:onShowKeyboard()
|
|
|
|
end
|
|
|
|
}
|
|
|
|
UIManager:show(path_chooser)
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:addFolderFromInput(friendly_name, folder)
|
|
|
|
for __, item in ipairs(G_reader_settings:readSetting("folder_shortcuts") or {}) do
|
|
|
|
if item.text == friendly_name and item.folder == folder then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("A shortcut to this folder already exists."),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local folder_shortcuts = G_reader_settings:readSetting("folder_shortcuts") or {}
|
|
|
|
table.insert(folder_shortcuts, {
|
|
|
|
text = friendly_name,
|
|
|
|
folder = folder,
|
|
|
|
})
|
|
|
|
G_reader_settings:saveSetting("folder_shortcuts", folder_shortcuts)
|
2019-03-09 12:55:03 +00:00
|
|
|
self:updateItemTable()
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:onMenuHold(item)
|
|
|
|
if item.deletable or item.editable then
|
|
|
|
local folder_shortcuts_dialog
|
|
|
|
folder_shortcuts_dialog = ButtonDialog:new{
|
|
|
|
buttons = {
|
|
|
|
{
|
2019-05-28 13:16:55 +00:00
|
|
|
{
|
|
|
|
text = _("Paste file"),
|
|
|
|
enabled = (self._manager.ui.file_chooser and self._manager.ui.clipboard) and true or false,
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(folder_shortcuts_dialog)
|
|
|
|
self._manager.ui:pasteHere(item.folder)
|
|
|
|
end
|
|
|
|
},
|
2018-08-22 20:34:20 +00:00
|
|
|
{
|
|
|
|
text = _("Edit"),
|
|
|
|
enabled = item.editable,
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(folder_shortcuts_dialog)
|
2019-03-09 12:55:03 +00:00
|
|
|
self._manager:editFolderShortcut(item)
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Delete"),
|
|
|
|
enabled = item.deletable,
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(folder_shortcuts_dialog)
|
2019-03-09 12:55:03 +00:00
|
|
|
self._manager:deleteFolderShortcut(item)
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UIManager:show(folder_shortcuts_dialog)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:editFolderShortcut(item)
|
|
|
|
local edit_folder_input
|
|
|
|
edit_folder_input = InputDialog:new {
|
|
|
|
title = _("Edit friendly name"),
|
|
|
|
input = item.friendly_name,
|
|
|
|
input_type = "text",
|
2020-01-04 00:18:51 +00:00
|
|
|
description = T(_("Rename title for selected folder:\n%1"), BD.dirpath(item.folder)),
|
2018-08-22 20:34:20 +00:00
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(edit_folder_input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Apply"),
|
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
|
|
|
self:renameFolderShortcut(item, edit_folder_input:getInputText())
|
|
|
|
UIManager:close(edit_folder_input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
UIManager:show(edit_folder_input)
|
|
|
|
edit_folder_input:onShowKeyboard()
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:renameFolderShortcut(item, new_name)
|
|
|
|
local folder_shortcuts = {}
|
|
|
|
for _, element in ipairs(G_reader_settings:readSetting("folder_shortcuts") or {}) do
|
|
|
|
if element.text == item.friendly_name and element.folder == item.folder then
|
|
|
|
element.text = new_name
|
|
|
|
end
|
|
|
|
table.insert(folder_shortcuts, element)
|
|
|
|
end
|
|
|
|
G_reader_settings:saveSetting("folder_shortcuts", folder_shortcuts)
|
2019-03-09 12:55:03 +00:00
|
|
|
self:updateItemTable()
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:deleteFolderShortcut(item)
|
|
|
|
local folder_shortcuts = {}
|
|
|
|
for _, element in ipairs(G_reader_settings:readSetting("folder_shortcuts") or {}) do
|
|
|
|
if element.text ~= item.friendly_name or element.folder ~= item.folder then
|
|
|
|
table.insert(folder_shortcuts, element)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
G_reader_settings:saveSetting("folder_shortcuts", folder_shortcuts)
|
2019-03-09 12:55:03 +00:00
|
|
|
self:updateItemTable()
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:onSetDimensions(dimen)
|
|
|
|
self.dimen = dimen
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
2019-03-04 21:45:55 +00:00
|
|
|
function FileManagerShortcuts:onShowFolderShortcutsDialog()
|
2019-03-09 12:55:03 +00:00
|
|
|
self.fm_bookmark = Menu:new{
|
2019-03-04 21:45:55 +00:00
|
|
|
title = _("Folder shortcuts"),
|
|
|
|
show_parent = self.ui,
|
2019-03-09 12:55:03 +00:00
|
|
|
width = Screen:getWidth(),
|
|
|
|
height = Screen:getHeight(),
|
|
|
|
no_title = false,
|
|
|
|
parent = nil,
|
|
|
|
has_close_button = true,
|
|
|
|
is_popout = false,
|
|
|
|
is_borderless = true,
|
2019-03-08 20:02:25 +00:00
|
|
|
curr_path = self.ui.file_chooser and self.ui.file_chooser.path or self.ui:getLastDirFile(),
|
2019-03-09 12:55:03 +00:00
|
|
|
onMenuHold = self.onMenuHold,
|
|
|
|
_manager = self,
|
2019-03-04 21:45:55 +00:00
|
|
|
}
|
2019-03-09 12:55:03 +00:00
|
|
|
self:updateItemTable()
|
|
|
|
UIManager:show(self.fm_bookmark)
|
2019-03-04 21:45:55 +00:00
|
|
|
end
|
|
|
|
|
2018-08-22 20:34:20 +00:00
|
|
|
return FileManagerShortcuts
|