nickel_conf: handle missing file/key/section

pull/1772/head
Dimitrios Semitsoglou-Tsiapos 8 years ago
parent eb47c5a39f
commit 699781de68

@ -13,24 +13,39 @@ local re_PowerOptionsSection = "^%[PowerOptions%]%s*"
local re_AnySection = "^%[.*%]%s*"
function NickelConf.frontLightLevel.get()
local new_intensity
local correct_section = false
local kobo_conf = assert(io.open(kobo_conf_path, "r"))
for line in kobo_conf:lines() do
if string.match(line, re_AnySection) then
correct_section = false
if string.match(line, re_PowerOptionsSection) then
correct_section = true
local kobo_conf = io.open(kobo_conf_path, "r")
if kobo_conf then
for line in kobo_conf:lines() do
if string.match(line, re_AnySection) then
correct_section = false
if string.match(line, re_PowerOptionsSection) then
correct_section = true
end
end
end
if correct_section then
new_intensity = string.match(line, re_FrontLightLevel)
if new_intensity then
new_intensity = tonumber(new_intensity)
break
if correct_section then
new_intensity = string.match(line, re_FrontLightLevel)
if new_intensity then
new_intensity = tonumber(new_intensity)
break
end
end
end
kobo_conf:close()
end
kobo_conf:close()
if not new_intensity then
local Device = require("device")
local powerd = Device:getPowerDevice()
local fallback_FrontLightLevel = powerd.flIntensity or 1
assert(NickelConf.frontLightLevel.set(fallback_FrontLightLevel))
return fallback_FrontLightLevel
end
return new_intensity
end
@ -38,33 +53,59 @@ function NickelConf.frontLightLevel.set(new_intensity)
assert(new_intensity >= 0 and new_intensity <= 100,
"Wrong brightness value given!")
local kobo_conf
local old_intensity
local remaining_file = ""
local lines = {}
local current_position
local correct_section = false
local kobo_conf = assert(io.open(kobo_conf_path, "r"))
for line in kobo_conf:lines() do
if string.match(line, re_AnySection) then
correct_section = false
if string.match(line, re_PowerOptionsSection) then
correct_section = true
local modified_brightness = false
kobo_conf = io.open(kobo_conf_path, "r")
if kobo_conf then
for line in kobo_conf:lines() do
if string.match(line, re_AnySection) then
if correct_section then
-- found a new section after having found the correct one,
-- therefore the key was missing: let the code below add it
kobo_conf:seek("set", current_position)
break
end
if string.match(line, re_PowerOptionsSection) then
correct_section = true
end
end
old_intensity = string.match(line, re_FrontLightLevel)
if correct_section and old_intensity then
lines[#lines + 1] = string.gsub(line, re_BrightnessValue, new_intensity, 1)
modified_brightness = true
break
else
lines[#lines + 1] = line
end
current_position = kobo_conf:seek()
end
old_intensity = string.match(line, re_FrontLightLevel)
if correct_section and old_intensity then
lines[#lines + 1] = string.gsub(line, re_BrightnessValue, new_intensity, 1)
remaining_file = kobo_conf:read("*a")
break
else
lines[#lines + 1] = line
end
if not modified_brightness then
if not correct_section then
lines[#lines + 1] = '[PowerOptions]'
end
lines[#lines + 1] = 'FrontLightLevel=' .. new_intensity
end
if kobo_conf then
remaining_file = kobo_conf:read("*a")
kobo_conf:close()
end
kobo_conf:close()
kobo_conf = assert(io.open(kobo_conf_path, "w"))
kobo_conf_w = assert(io.open(kobo_conf_path, "w"))
for i, line in ipairs(lines) do
kobo_conf:write(line, "\n")
kobo_conf_w:write(line, "\n")
end
kobo_conf:write(remaining_file)
kobo_conf:close()
kobo_conf_w:write(remaining_file)
kobo_conf_w:close()
return true
end
return NickelConf

@ -6,7 +6,6 @@ local Geom = require("ui/geometry")
local util = require("ffi/util")
local DEBUG = require("dbg")
local _ = require("gettext")
local NickelConf = require("device/kobo/nickel_conf")
local MILLION = 1000000
@ -63,9 +62,11 @@ function UIManager:init()
end
local kobo_light_on_start = tonumber(KOBO_LIGHT_ON_START)
if kobo_light_on_start then
local new_intensity
if kobo_light_on_start >= 0 then
new_intensity = math.min(kobo_light_on_start, 100)
elseif kobo_light_on_start == -2 then
local NickelConf = require("device/kobo/nickel_conf")
new_intensity = NickelConf.frontLightLevel:get()
end
if new_intensity then

Loading…
Cancel
Save