Skip to content

Commit

Permalink
Version log and (limited) notifications log (#10178)
Browse files Browse the repository at this point in the history
This PR will close #1257 and #3418.
  • Loading branch information
zwim committed Mar 5, 2023
1 parent 9f37863 commit efd2df6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
23 changes: 23 additions & 0 deletions frontend/ui/elements/screen_notification_menu_table.lua
@@ -1,4 +1,6 @@
local Notification = require("ui/widget/notification")
local TextViewer = require("ui/widget/textviewer")
local UIManager = require("ui/uimanager")
local _ = require("gettext")

local band = bit.band
Expand Down Expand Up @@ -76,5 +78,26 @@ This allows selecting which to show or hide.]]),
end,
separator = true,
},
{
text = _("Show past notifications"),
callback = function()
local content = require("ui/widget/notification"):getPastMessages()

if not content or #content == 0 then
content = _("No notifications available.")
else
content = table.concat(content, "\n")
end

local textviewer
textviewer = TextViewer:new{
title = _("Past notifications"),
text = content,
justified = false,
}
UIManager:show(textviewer)
end,
keep_menu_open = true,
},
}
}
15 changes: 14 additions & 1 deletion frontend/ui/widget/notification.lua
Expand Up @@ -48,6 +48,9 @@ local SOURCE_ALL = SOURCE_BOTTOM_MENU +
SOURCE_DISPATCHER +
SOURCE_OTHER
-- Maximum number of saved message text
local MAX_NB_PAST_MESSAGES = 20
local Notification = InputContainer:extend{
face = Font:getFace("x_smallinfofont"),
text = _("N/A"),
Expand All @@ -73,6 +76,7 @@ local Notification = InputContainer:extend{
SOURCE_SOME = SOURCE_SOME,
SOURCE_DEFAULT = SOURCE_DEFAULT,
SOURCE_ALL = SOURCE_ALL,
_past_messages = {}, -- a static class member to store the N last messages text
}
function Notification:init()
Expand Down Expand Up @@ -164,6 +168,10 @@ function Notification:notify(arg, source, refresh_after)
return false
end
function Notification:getPastMessages()
return self._past_messages
end
function Notification:_cleanShownStack()
-- Clean stack of shown notifications
if self._shown_idx then
Expand Down Expand Up @@ -204,10 +212,15 @@ function Notification:onShow()
if self.timeout then
UIManager:scheduleIn(self.timeout, function() UIManager:close(self) end)
end
if #self._past_messages >= MAX_NB_PAST_MESSAGES then
table.remove(self._past_messages)
end
table.insert(self._past_messages, 1, os.date("%X: ") .. self.text)
return true
end
function Notification:onTapClose()
if self.toast then return end -- should not happen
UIManager:close(self)
Expand Down
45 changes: 45 additions & 0 deletions frontend/version.lua
Expand Up @@ -2,6 +2,8 @@
This module helps with retrieving version information.
]]

local VERSION_LOG_FILE = "version.log"

local Version = {}

--- Returns current KOReader git-rev.
Expand Down Expand Up @@ -86,4 +88,47 @@ function Version:getBuildDate()
return self.date
end

--- Get last line in `VERSION_LOG_FILE`.
-- @treturn last line in `VERSION_LOG_FILE` or an empty string
function Version:getLastLogLine()
local log_file = io.open(VERSION_LOG_FILE, "r")
local last_log_line
if log_file then
for line in log_file:lines() do
last_log_line = line
end
log_file:close()
end

return last_log_line or ""
end

--- Append text to a `VERSION_LOG_FILE`.
-- @string text text to be appended
function Version:appendToLogFile(text)
local log_file = io.open(VERSION_LOG_FILE, "a")
if not log_file then
return
end
log_file:write(text, "\n")
log_file:close()
return true
end

--- Updates `VERSION_LOG_FILE` and keep the file small
-- @string model device model (may contain spaces)
function Version:updateVersionLog(current_model)
local last_line = Version:getLastLogLine()

local dummy, dummy, last_version, last_model = last_line:match("(.-), (.-), (.-), (.-)$")
self.last_version = last_version or "last version not found"
self.last_model = last_model or "last model not found"

if self.rev ~= last_version or current_model ~= last_model then
-- Appends KOReader git-rev, model and current date to the `VERSION_LOG_FILE`
-- in the format 'YYYY-mm-dd, HH:MM:SS, git-rev, model'
self:appendToLogFile(os.date("%Y-%m-%d, %X, ") .. self.rev .. ", " .. current_model)
end
end

return Version
6 changes: 5 additions & 1 deletion reader.lua
Expand Up @@ -26,7 +26,8 @@ local userpatch = require("userpatch")
userpatch.applyPatches(userpatch.early_once)
userpatch.applyPatches(userpatch.early)

io.write(" [*] Version: ", require("version"):getCurrentRevision(), "\n\n")
local Version = require("version")
io.write(" [*] Version: ", Version:getCurrentRevision(), "\n\n")

-- Load default settings
G_defaults = require("luadefaults"):open()
Expand Down Expand Up @@ -185,6 +186,9 @@ end
local CanvasContext = require("document/canvascontext")
CanvasContext:init(Device)

-- Update the version log file if there was an update or the device has changed
Version:updateVersionLog(Device.model)

-- Handle one time migration stuff (settings, deprecation, ...) in case of an upgrade...
require("ui/data/onetime_migration")

Expand Down

0 comments on commit efd2df6

Please sign in to comment.