2017-12-17 17:27:24 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
|
|
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
|
|
|
|
local BookStatusWidget = require("ui/widget/bookstatuswidget")
|
2016-04-18 05:30:46 +00:00
|
|
|
local Device = require("device")
|
2014-09-17 10:24:33 +00:00
|
|
|
local DocSettings = require("docsettings")
|
2017-09-05 07:01:46 +00:00
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2017-12-17 17:27:24 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local ImageWidget = require("ui/widget/imagewidget")
|
|
|
|
local ScreenSaverWidget = require("ui/widget/screensaverwidget")
|
2017-09-05 07:01:46 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2017-12-17 17:27:24 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2017-12-17 17:27:24 +00:00
|
|
|
local _ = require("gettext")
|
2017-09-05 07:01:46 +00:00
|
|
|
local Screen = Device.screen
|
2017-12-17 17:27:24 +00:00
|
|
|
local T = require("ffi/util").template
|
2014-08-22 07:05:00 +00:00
|
|
|
|
2017-12-17 17:27:24 +00:00
|
|
|
local default_screensaver_message = _("Sleeping")
|
|
|
|
local Screensaver = {}
|
2016-04-15 00:42:54 +00:00
|
|
|
|
|
|
|
local function getRandomImage(dir)
|
|
|
|
if string.sub(dir, string.len(dir)) ~= "/" then
|
|
|
|
dir = dir .. "/"
|
|
|
|
end
|
2014-08-22 07:05:00 +00:00
|
|
|
local pics = {}
|
|
|
|
local i = 0
|
2014-08-26 10:10:26 +00:00
|
|
|
math.randomseed(os.time())
|
2014-08-22 07:05:00 +00:00
|
|
|
for entry in lfs.dir(dir) do
|
|
|
|
if lfs.attributes(dir .. entry, "mode") == "file" then
|
2016-04-15 20:21:39 +00:00
|
|
|
local extension =
|
|
|
|
string.lower(string.match(entry, ".+%.([^.]+)") or "")
|
|
|
|
if extension == "jpg"
|
|
|
|
or extension == "jpeg"
|
|
|
|
or extension == "png" then
|
2014-08-22 07:05:00 +00:00
|
|
|
i = i + 1
|
|
|
|
pics[i] = entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-17 13:12:31 +00:00
|
|
|
if i == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
2017-12-17 17:27:24 +00:00
|
|
|
return dir .. pics[math.random(i)]
|
2016-04-15 20:21:39 +00:00
|
|
|
end
|
|
|
|
|
2017-12-17 17:27:24 +00:00
|
|
|
function Screensaver:chooseFolder()
|
|
|
|
local buttons = {}
|
|
|
|
table.insert(buttons, {
|
|
|
|
{
|
|
|
|
text = _("Choose screensaver directory by long-pressing"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.choose_dialog)
|
|
|
|
require("ui/downloadmgr"):new{
|
|
|
|
title = _("Choose screensaver directory"),
|
|
|
|
onConfirm = function(path)
|
|
|
|
logger.dbg("set screensaver directory to", path)
|
|
|
|
G_reader_settings:saveSetting("screensaver_dir", path)
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Screensaver directory set to:\n%1"), path),
|
|
|
|
timeout = 3,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
}:chooseDir()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
table.insert(buttons, {
|
|
|
|
{
|
|
|
|
text = _("Close"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.choose_dialog)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
local screensaver_dir = G_reader_settings:readSetting("screensaver_dir")
|
|
|
|
if screensaver_dir == nil then
|
|
|
|
local DataStorage = require("datastorage")
|
|
|
|
screensaver_dir = DataStorage:getDataDir() .. "/screenshots/"
|
|
|
|
end
|
|
|
|
self.choose_dialog = ButtonDialogTitle:new{
|
|
|
|
title = T(_("Current screensaver image directory:\n %1"), screensaver_dir),
|
|
|
|
buttons = buttons
|
|
|
|
}
|
|
|
|
UIManager:show(self.choose_dialog)
|
2016-08-01 01:26:26 +00:00
|
|
|
end
|
|
|
|
|
2017-12-17 17:27:24 +00:00
|
|
|
function Screensaver:stretchCover()
|
|
|
|
local lastfile = G_reader_settings:readSetting("lastfile")
|
|
|
|
if DocSettings:hasSidecarFile(lastfile) then
|
|
|
|
local doc_settings = DocSettings:open(lastfile)
|
|
|
|
local stretch_cover_ss = doc_settings:readSetting("stretch_cover")
|
|
|
|
doc_settings:close()
|
|
|
|
if stretch_cover_ss ~= nil then
|
|
|
|
return stretch_cover_ss
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return G_reader_settings:readSetting("stretch_cover_default") or false
|
|
|
|
end
|
2016-08-01 01:26:26 +00:00
|
|
|
|
2017-12-17 17:27:24 +00:00
|
|
|
function Screensaver:excluded()
|
2016-08-01 01:26:26 +00:00
|
|
|
local lastfile = G_reader_settings:readSetting("lastfile")
|
2017-12-17 17:27:24 +00:00
|
|
|
local exclude_ss = false -- consider it not excluded if there's no docsetting
|
|
|
|
if DocSettings:hasSidecarFile(lastfile) then
|
|
|
|
local doc_settings = DocSettings:open(lastfile)
|
|
|
|
exclude_ss = doc_settings:readSetting("exclude_screensaver")
|
|
|
|
doc_settings:close()
|
|
|
|
end
|
|
|
|
return exclude_ss or false
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screensaver:setMessage()
|
|
|
|
local InputDialog = require("ui/widget/inputdialog")
|
|
|
|
local screensaver_message = G_reader_settings:readSetting("screensaver_message")
|
|
|
|
if screensaver_message == nil then
|
|
|
|
screensaver_message = default_screensaver_message
|
2014-08-27 03:07:25 +00:00
|
|
|
end
|
2017-12-17 17:27:24 +00:00
|
|
|
self.input_dialog = InputDialog:new{
|
|
|
|
title = "Screensaver message",
|
|
|
|
input = screensaver_message,
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.input_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Set message"),
|
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
|
|
|
G_reader_settings:saveSetting("screensaver_message", self.input_dialog:getInputText())
|
|
|
|
UIManager:close(self.input_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
self.input_dialog:onShowKeyboard()
|
|
|
|
UIManager:show(self.input_dialog)
|
2014-08-22 07:05:00 +00:00
|
|
|
end
|
|
|
|
|
Screensaver: change default & show messages on reboot and poweroff (#3572)
Use "message" as the default for screensaver (instead of "disable") (https://github.com/koreader/koreader/pull/3535#issuecomment-353337143)
On Kobo
Fix: display message on poweroff or reboot (https://github.com/koreader/koreader/pull/3535#issuecomment-352355865)
Allows for overwritting these default messages with the classic screensaver settings, by just prepending "poweroff_" or "reboot_" to them. (https://github.com/koreader/koreader/pull/3535#issuecomment-352498497)
So one can manually add to its settings.reader.lua:
```
"poweroff_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"poweroff_screensaver_dir" = "path to same as screensaver_dir or alternate dir for poweroff image(s)",
"poweroff_screensaver_message" = "message to use instead of Powered off",
```
```
"reboot_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"reboot_screensaver_dir" = "path to same as screensaver_dir or alternate dir for reboot image(s)",
"reboot_screensaver_message" = "message to use instead of Rebooting...",
```
2018-01-06 12:21:26 +00:00
|
|
|
function Screensaver:show(event, fallback_message)
|
|
|
|
-- These 2 (optional) parameters are to support poweroff and reboot actions
|
|
|
|
-- on Kobo (see uimanager.lua)
|
2017-12-17 17:27:24 +00:00
|
|
|
if self.left_msg then
|
|
|
|
UIManager:close(self.left_msg)
|
|
|
|
self.left_msg = nil
|
|
|
|
end
|
Screensaver: change default & show messages on reboot and poweroff (#3572)
Use "message" as the default for screensaver (instead of "disable") (https://github.com/koreader/koreader/pull/3535#issuecomment-353337143)
On Kobo
Fix: display message on poweroff or reboot (https://github.com/koreader/koreader/pull/3535#issuecomment-352355865)
Allows for overwritting these default messages with the classic screensaver settings, by just prepending "poweroff_" or "reboot_" to them. (https://github.com/koreader/koreader/pull/3535#issuecomment-352498497)
So one can manually add to its settings.reader.lua:
```
"poweroff_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"poweroff_screensaver_dir" = "path to same as screensaver_dir or alternate dir for poweroff image(s)",
"poweroff_screensaver_message" = "message to use instead of Powered off",
```
```
"reboot_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"reboot_screensaver_dir" = "path to same as screensaver_dir or alternate dir for reboot image(s)",
"reboot_screensaver_message" = "message to use instead of Rebooting...",
```
2018-01-06 12:21:26 +00:00
|
|
|
local prefix = event and event.."_" or "" -- "", "poweroff_" or "reboot_"
|
|
|
|
local screensaver_type = G_reader_settings:readSetting(prefix.."screensaver_type")
|
|
|
|
if screensaver_type == nil then
|
|
|
|
screensaver_type = "message"
|
|
|
|
end
|
|
|
|
if screensaver_type == "disable" then
|
2017-12-17 17:27:24 +00:00
|
|
|
return
|
|
|
|
end
|
Screensaver: change default & show messages on reboot and poweroff (#3572)
Use "message" as the default for screensaver (instead of "disable") (https://github.com/koreader/koreader/pull/3535#issuecomment-353337143)
On Kobo
Fix: display message on poweroff or reboot (https://github.com/koreader/koreader/pull/3535#issuecomment-352355865)
Allows for overwritting these default messages with the classic screensaver settings, by just prepending "poweroff_" or "reboot_" to them. (https://github.com/koreader/koreader/pull/3535#issuecomment-352498497)
So one can manually add to its settings.reader.lua:
```
"poweroff_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"poweroff_screensaver_dir" = "path to same as screensaver_dir or alternate dir for poweroff image(s)",
"poweroff_screensaver_message" = "message to use instead of Powered off",
```
```
"reboot_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"reboot_screensaver_dir" = "path to same as screensaver_dir or alternate dir for reboot image(s)",
"reboot_screensaver_message" = "message to use instead of Rebooting...",
```
2018-01-06 12:21:26 +00:00
|
|
|
local widget = nil
|
|
|
|
local background = Blitbuffer.COLOR_WHITE
|
2017-12-17 17:27:24 +00:00
|
|
|
if screensaver_type == "cover" then
|
2014-08-27 03:07:25 +00:00
|
|
|
local lastfile = G_reader_settings:readSetting("lastfile")
|
2017-12-17 17:27:24 +00:00
|
|
|
local exclude = false -- consider it not excluded if there's no docsetting
|
|
|
|
if DocSettings:hasSidecarFile(lastfile) then
|
2016-08-01 01:26:26 +00:00
|
|
|
local doc_settings = DocSettings:open(lastfile)
|
2017-12-17 17:27:24 +00:00
|
|
|
exclude = doc_settings:readSetting("exclude_screensaver")
|
|
|
|
doc_settings:close()
|
2014-09-17 10:24:33 +00:00
|
|
|
end
|
2017-12-17 17:27:24 +00:00
|
|
|
if exclude ~= true then
|
|
|
|
background = Blitbuffer.COLOR_BLACK
|
|
|
|
if lfs.attributes(lastfile, "mode") == "file" then
|
|
|
|
local doc = DocumentRegistry:openDocument(lastfile)
|
|
|
|
local image = doc:getCoverPageImage()
|
|
|
|
doc:close()
|
|
|
|
if image ~= nil then
|
|
|
|
widget = ImageWidget:new{
|
|
|
|
image = image,
|
|
|
|
image_disposable = true,
|
|
|
|
alpha = true,
|
|
|
|
height = Screen:getHeight(),
|
|
|
|
width = Screen:getWidth(),
|
|
|
|
scale_factor = not self:stretchCover() and 0 or nil,
|
|
|
|
}
|
2016-11-16 10:49:34 +00:00
|
|
|
else
|
2017-12-17 17:27:24 +00:00
|
|
|
screensaver_type = "random_image"
|
2016-11-16 10:49:34 +00:00
|
|
|
end
|
2017-12-17 17:27:24 +00:00
|
|
|
else
|
|
|
|
screensaver_type = "random_image"
|
2014-08-26 10:10:26 +00:00
|
|
|
end
|
2017-12-17 17:27:24 +00:00
|
|
|
else --fallback to random images if this book cover is excluded
|
|
|
|
screensaver_type = "random_image"
|
2014-08-26 10:10:26 +00:00
|
|
|
end
|
|
|
|
end
|
2017-12-17 17:27:24 +00:00
|
|
|
if screensaver_type == "bookstatus" then
|
|
|
|
local lastfile = G_reader_settings:readSetting("lastfile")
|
|
|
|
if lfs.attributes(lastfile, "mode") == "file" then
|
|
|
|
local doc = DocumentRegistry:openDocument(lastfile)
|
|
|
|
local doc_settings = DocSettings:open(lastfile)
|
|
|
|
local instance = require("apps/reader/readerui"):_getRunningInstance()
|
|
|
|
if instance ~= nil then
|
|
|
|
widget = BookStatusWidget:new {
|
|
|
|
thumbnail = doc:getCoverPageImage(),
|
|
|
|
props = doc:getProps(),
|
|
|
|
document = doc,
|
|
|
|
settings = doc_settings,
|
|
|
|
view = instance.view,
|
|
|
|
readonly = true,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
screensaver_type = "message"
|
|
|
|
end
|
|
|
|
doc:close()
|
|
|
|
doc_settings:close()
|
|
|
|
else
|
|
|
|
screensaver_type = "message"
|
2017-04-08 21:21:03 +00:00
|
|
|
end
|
2017-12-17 17:27:24 +00:00
|
|
|
end
|
|
|
|
if screensaver_type == "random_image" then
|
Screensaver: change default & show messages on reboot and poweroff (#3572)
Use "message" as the default for screensaver (instead of "disable") (https://github.com/koreader/koreader/pull/3535#issuecomment-353337143)
On Kobo
Fix: display message on poweroff or reboot (https://github.com/koreader/koreader/pull/3535#issuecomment-352355865)
Allows for overwritting these default messages with the classic screensaver settings, by just prepending "poweroff_" or "reboot_" to them. (https://github.com/koreader/koreader/pull/3535#issuecomment-352498497)
So one can manually add to its settings.reader.lua:
```
"poweroff_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"poweroff_screensaver_dir" = "path to same as screensaver_dir or alternate dir for poweroff image(s)",
"poweroff_screensaver_message" = "message to use instead of Powered off",
```
```
"reboot_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"reboot_screensaver_dir" = "path to same as screensaver_dir or alternate dir for reboot image(s)",
"reboot_screensaver_message" = "message to use instead of Rebooting...",
```
2018-01-06 12:21:26 +00:00
|
|
|
local screensaver_dir = G_reader_settings:readSetting(prefix.."screensaver_dir")
|
2017-12-17 17:27:24 +00:00
|
|
|
if screensaver_dir == nil then
|
|
|
|
local DataStorage = require("datastorage")
|
|
|
|
screensaver_dir = DataStorage:getDataDir() .. "/screenshots/"
|
|
|
|
end
|
|
|
|
local image_file = getRandomImage(screensaver_dir)
|
|
|
|
if image_file == nil then
|
|
|
|
screensaver_type = "message"
|
|
|
|
else
|
|
|
|
widget = ImageWidget:new{
|
|
|
|
file = image_file,
|
|
|
|
alpha = true,
|
|
|
|
height = Screen:getHeight(),
|
|
|
|
width = Screen:getWidth(),
|
|
|
|
scale_factor = 0,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if screensaver_type == "readingprogress" then
|
|
|
|
if Screensaver.getReaderProgress ~= nil then
|
|
|
|
widget = Screensaver.getReaderProgress()
|
|
|
|
else
|
|
|
|
screensaver_type = "message"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if screensaver_type == "message" then
|
Screensaver: change default & show messages on reboot and poweroff (#3572)
Use "message" as the default for screensaver (instead of "disable") (https://github.com/koreader/koreader/pull/3535#issuecomment-353337143)
On Kobo
Fix: display message on poweroff or reboot (https://github.com/koreader/koreader/pull/3535#issuecomment-352355865)
Allows for overwritting these default messages with the classic screensaver settings, by just prepending "poweroff_" or "reboot_" to them. (https://github.com/koreader/koreader/pull/3535#issuecomment-352498497)
So one can manually add to its settings.reader.lua:
```
"poweroff_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"poweroff_screensaver_dir" = "path to same as screensaver_dir or alternate dir for poweroff image(s)",
"poweroff_screensaver_message" = "message to use instead of Powered off",
```
```
"reboot_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"reboot_screensaver_dir" = "path to same as screensaver_dir or alternate dir for reboot image(s)",
"reboot_screensaver_message" = "message to use instead of Rebooting...",
```
2018-01-06 12:21:26 +00:00
|
|
|
local screensaver_message = G_reader_settings:readSetting(prefix.."screensaver_message")
|
2017-12-17 17:27:24 +00:00
|
|
|
if G_reader_settings:nilOrFalse("message_background") then
|
|
|
|
background = nil
|
|
|
|
end
|
|
|
|
if screensaver_message == nil then
|
Screensaver: change default & show messages on reboot and poweroff (#3572)
Use "message" as the default for screensaver (instead of "disable") (https://github.com/koreader/koreader/pull/3535#issuecomment-353337143)
On Kobo
Fix: display message on poweroff or reboot (https://github.com/koreader/koreader/pull/3535#issuecomment-352355865)
Allows for overwritting these default messages with the classic screensaver settings, by just prepending "poweroff_" or "reboot_" to them. (https://github.com/koreader/koreader/pull/3535#issuecomment-352498497)
So one can manually add to its settings.reader.lua:
```
"poweroff_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"poweroff_screensaver_dir" = "path to same as screensaver_dir or alternate dir for poweroff image(s)",
"poweroff_screensaver_message" = "message to use instead of Powered off",
```
```
"reboot_screensaver_type" = "random_image" or "message", "cover", "bookstatus", "readingprogress", "disable",
"reboot_screensaver_dir" = "path to same as screensaver_dir or alternate dir for reboot image(s)",
"reboot_screensaver_message" = "message to use instead of Rebooting...",
```
2018-01-06 12:21:26 +00:00
|
|
|
screensaver_message = fallback_message or default_screensaver_message
|
2017-12-17 17:27:24 +00:00
|
|
|
end
|
|
|
|
widget = InfoMessage:new{
|
|
|
|
text = screensaver_message,
|
|
|
|
readonly = true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if widget then
|
|
|
|
self.left_msg = ScreenSaverWidget:new{
|
|
|
|
widget = widget,
|
|
|
|
background = background,
|
|
|
|
}
|
2017-09-05 07:01:46 +00:00
|
|
|
self.left_msg.modal = true
|
2014-12-01 14:39:41 +00:00
|
|
|
-- refresh whole screen for other types
|
2016-11-16 10:49:34 +00:00
|
|
|
UIManager:show(self.left_msg, "full")
|
2014-08-26 10:10:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screensaver:close()
|
2017-12-17 17:27:24 +00:00
|
|
|
if self.left_msg == nil then return end
|
|
|
|
local screensaver_delay = G_reader_settings:readSetting("screensaver_delay")
|
|
|
|
local screensaver_delay_number = tonumber(screensaver_delay)
|
|
|
|
if screensaver_delay_number then
|
|
|
|
UIManager:scheduleIn(screensaver_delay_number, function()
|
|
|
|
logger.dbg("close screensaver")
|
|
|
|
if self.left_msg then
|
|
|
|
UIManager:close(self.left_msg)
|
|
|
|
UIManager:setDirty("all", "full")
|
|
|
|
self.left_msg = nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
elseif screensaver_delay == "disable" or screensaver_delay == nil then
|
|
|
|
logger.dbg("close screensaver")
|
|
|
|
if self.left_msg then
|
|
|
|
UIManager:close(self.left_msg)
|
|
|
|
self.left_msg = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
logger.dbg("tap to exit from screensaver")
|
2014-08-26 10:10:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Screensaver
|