mirror of
https://github.com/koreader/koreader.git
synced 2025-12-18 12:02:09 +01:00
add datastorage module to handle data directory on various platform
On kindle, kobo and pocketbook the data directory is the current running directory but on Android the app is installed in system defined location and users may have no access to that location. The same circumstances should be true for the upcoming Koreader for Ubuntu touch, so the data directory (in which tessdata, dictionaries, global settings, persistant defaults and probably history data are stored) could be stored in another place.
This commit is contained in:
2
Makefile
2
Makefile
@@ -29,7 +29,7 @@ ANDROID_LAUNCHER_DIR:=$(ANDROID_DIR)/luajit-launcher
|
|||||||
WIN32_DIR=$(PLATFORM_DIR)/win32
|
WIN32_DIR=$(PLATFORM_DIR)/win32
|
||||||
|
|
||||||
# files to link from main directory
|
# files to link from main directory
|
||||||
INSTALL_FILES=reader.lua frontend resources defaults.lua l10n \
|
INSTALL_FILES=reader.lua frontend resources defaults.lua datastorage.lua l10n \
|
||||||
git-rev README.md COPYING
|
git-rev README.md COPYING
|
||||||
|
|
||||||
# for gettext
|
# for gettext
|
||||||
|
|||||||
14
datastorage.lua
Normal file
14
datastorage.lua
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
-- need low-level mechnism to detect android to avoid recursive dependency
|
||||||
|
local isAndroid = pcall(require, "android")
|
||||||
|
|
||||||
|
local DataStorage = {}
|
||||||
|
|
||||||
|
function DataStorage:getDataDir()
|
||||||
|
if isAndroid then
|
||||||
|
return "/sdcard/koreader/"
|
||||||
|
else
|
||||||
|
return "./"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return DataStorage
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
local InfoMessage = require("ui/widget/infomessage")
|
local InfoMessage = require("ui/widget/infomessage")
|
||||||
local UIManager = require("ui/uimanager")
|
|
||||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
||||||
local InputDialog = require("ui/widget/inputdialog")
|
local InputDialog = require("ui/widget/inputdialog")
|
||||||
local MultiInputDialog = require("ui/widget/multiinputdialog")
|
local MultiInputDialog = require("ui/widget/multiinputdialog")
|
||||||
local ConfirmBox = require("ui/widget/confirmbox")
|
local ConfirmBox = require("ui/widget/confirmbox")
|
||||||
local CenterContainer = require("ui/widget/container/centercontainer")
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
||||||
|
local DataStorage = require("datastorage")
|
||||||
|
local UIManager = require("ui/uimanager")
|
||||||
local Screen = require("device").screen
|
local Screen = require("device").screen
|
||||||
local Menu = require("ui/widget/menu")
|
local Menu = require("ui/widget/menu")
|
||||||
local Font = require("ui/font")
|
local Font = require("ui/font")
|
||||||
@@ -319,16 +320,16 @@ function SetDefaults:SaveSettings()
|
|||||||
file:close()
|
file:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
local filename = "defaults.persistent.lua"
|
local persistent_filename = DataStorage:getDataDir() .. "/defaults.persistent.lua"
|
||||||
local file
|
local file
|
||||||
if io.open(filename,"r") == nil then
|
if io.open(persistent_filename,"r") == nil then
|
||||||
file = io.open(filename, "w")
|
file = io.open(persistent_filename, "w")
|
||||||
file:write("-- For configuration changes that persists between (nightly) releases\n")
|
file:write("-- For configuration changes that persists between (nightly) releases\n")
|
||||||
file:close()
|
file:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
local dpl = {}
|
local dpl = {}
|
||||||
fileread("defaults.persistent.lua",dpl)
|
fileread(persistent_filename, dpl)
|
||||||
local dl = {}
|
local dl = {}
|
||||||
fileread("defaults.lua",dl)
|
fileread("defaults.lua",dl)
|
||||||
self.results = {}
|
self.results = {}
|
||||||
@@ -355,12 +356,16 @@ function SetDefaults:SaveSettings()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
file = io.open("defaults.persistent.lua", "w")
|
file = io.open(persistent_filename, "w")
|
||||||
for i = 1,#dpl do
|
if file then
|
||||||
file:write(dpl[i] .. "\n")
|
for i = 1,#dpl do
|
||||||
|
file:write(dpl[i] .. "\n")
|
||||||
|
end
|
||||||
|
file:close()
|
||||||
|
UIManager:show(InfoMessage:new{
|
||||||
|
text = _("Default settings were saved successfully!"),
|
||||||
|
})
|
||||||
end
|
end
|
||||||
file:close()
|
|
||||||
UIManager:show(InfoMessage:new{text = _("Default settings were saved successfully!")})
|
|
||||||
settings_changed = false
|
settings_changed = false
|
||||||
end
|
end
|
||||||
return SetDefaults
|
return SetDefaults
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
local lfs = require("libs/libkoreader-lfs")
|
local lfs = require("libs/libkoreader-lfs")
|
||||||
local DocSettings = {}
|
local DocSettings = {}
|
||||||
local dump = require("dump")
|
local dump = require("dump")
|
||||||
|
local DataStorage = require("datastorage")
|
||||||
|
|
||||||
function DocSettings:getHistoryPath(fullpath)
|
function DocSettings:getHistoryPath(fullpath)
|
||||||
return "./history/[" .. fullpath:gsub("(.*/)([^/]+)","%1] %2"):gsub("/","#") .. ".lua"
|
return "./history/[" .. fullpath:gsub("(.*/)([^/]+)","%1] %2"):gsub("/","#") .. ".lua"
|
||||||
@@ -26,7 +27,7 @@ function DocSettings:open(docfile)
|
|||||||
local sidecar_path = nil
|
local sidecar_path = nil
|
||||||
if docfile == ".reader" then
|
if docfile == ".reader" then
|
||||||
-- we handle reader setting as special case
|
-- we handle reader setting as special case
|
||||||
history_path = "settings.reader.lua"
|
history_path = DataStorage:getDataDir() .. "/settings.reader.lua"
|
||||||
else
|
else
|
||||||
if lfs.attributes("./history", "mode") ~= "directory" then
|
if lfs.attributes("./history", "mode") ~= "directory" then
|
||||||
lfs.mkdir("history")
|
lfs.mkdir("history")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
local TileCacheItem = require("document/tilecacheitem")
|
local TileCacheItem = require("document/tilecacheitem")
|
||||||
local KOPTContext = require("ffi/koptcontext")
|
local KOPTContext = require("ffi/koptcontext")
|
||||||
local Document = require("document/document")
|
local Document = require("document/document")
|
||||||
|
local DataStorage = require("datastorage")
|
||||||
local CacheItem = require("cacheitem")
|
local CacheItem = require("cacheitem")
|
||||||
local Screen = require("device").screen
|
local Screen = require("device").screen
|
||||||
local Geom = require("ui/geometry")
|
local Geom = require("ui/geometry")
|
||||||
@@ -11,7 +12,7 @@ local util = require("ffi/util")
|
|||||||
|
|
||||||
local KoptInterface = {
|
local KoptInterface = {
|
||||||
ocrengine = "ocrengine",
|
ocrengine = "ocrengine",
|
||||||
tessocr_data = "data",
|
tessocr_data = DataStorage:getDataDir() .. "/data",
|
||||||
ocr_lang = "eng",
|
ocr_lang = "eng",
|
||||||
ocr_type = 3, -- default 0, for more accuracy use 3
|
ocr_type = 3, -- default 0, for more accuracy use 3
|
||||||
last_context_size = nil,
|
last_context_size = nil,
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
local A = require("android")
|
local A = require("android")
|
||||||
A.dl.library_path = A.dl.library_path .. ":" .. A.dir .. "/libs"
|
A.dl.library_path = A.dl.library_path .. ":" .. A.dir .. "/libs"
|
||||||
|
|
||||||
|
local ffi = require("ffi")
|
||||||
|
ffi.cdef[[
|
||||||
|
char *getenv(const char *name);
|
||||||
|
int putenv(const char *envvar);
|
||||||
|
]]
|
||||||
|
|
||||||
-- check uri of the intent that starts this application
|
-- check uri of the intent that starts this application
|
||||||
local file = A.jni:context(A.app.activity.vm, function(JNI)
|
local file = A.jni:context(A.app.activity.vm, function(JNI)
|
||||||
local uri = JNI:callObjectMethod(
|
local uri = JNI:callObjectMethod(
|
||||||
@@ -29,6 +35,9 @@ pcall(function() dofile("/sdcard/koreader/patch.lua") end)
|
|||||||
-- set proper permission for sdcv
|
-- set proper permission for sdcv
|
||||||
A.execute("chmod", "755", "./sdcv")
|
A.execute("chmod", "755", "./sdcv")
|
||||||
|
|
||||||
|
-- set TESSDATA_PREFIX env var
|
||||||
|
ffi.C.putenv("TESSDATA_PREFIX=/sdcard/koreader/data")
|
||||||
|
|
||||||
-- create fake command-line arguments
|
-- create fake command-line arguments
|
||||||
arg = {"-d", file or "/sdcard"}
|
arg = {"-d", file or "/sdcard"}
|
||||||
dofile(A.dir.."/reader.lua")
|
dofile(A.dir.."/reader.lua")
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
-- load default settings
|
-- load default settings
|
||||||
require "defaults"
|
require "defaults"
|
||||||
pcall(dofile, "defaults.persistent.lua")
|
local DataStorage = require("datastorage")
|
||||||
|
pcall(dofile, DataStorage:getDataDir() .. "/defaults.persistent.lua")
|
||||||
|
|
||||||
-- set search path for 'require()'
|
-- set search path for 'require()'
|
||||||
package.path = "common/?.lua;rocks/share/lua/5.1/?.lua;frontend/?.lua;" .. package.path
|
package.path = "common/?.lua;rocks/share/lua/5.1/?.lua;frontend/?.lua;" .. package.path
|
||||||
|
|||||||
Reference in New Issue
Block a user