2017-12-17 12:02:08 +00:00
local ConfirmBox = require ( " ui/widget/confirmbox " )
local DataStorage = require ( " datastorage " )
2022-10-08 21:10:58 +00:00
local DictQuickLookup = require ( " ui/widget/dictquicklookup " )
2017-08-22 15:24:31 +00:00
local InfoMessage = require ( " ui/widget/infomessage " )
2017-04-07 13:20:57 +00:00
local InputDialog = require ( " ui/widget/inputdialog " )
2017-12-17 12:02:08 +00:00
local KeyValuePage = require ( " ui/widget/keyvaluepage " )
local LuaData = require ( " luadata " )
2017-04-07 13:20:57 +00:00
local NetworkMgr = require ( " ui/network/manager " )
2014-08-20 06:41:45 +00:00
local ReaderDictionary = require ( " apps/reader/modules/readerdictionary " )
2018-01-16 11:36:52 +00:00
local Trapper = require ( " ui/trapper " )
2014-08-20 06:41:45 +00:00
local Translator = require ( " ui/translator " )
2017-04-07 13:20:57 +00:00
local UIManager = require ( " ui/uimanager " )
2017-05-12 16:28:42 +00:00
local Wikipedia = require ( " ui/wikipedia " )
2023-12-16 08:36:57 +00:00
local filemanagerutil = require ( " apps/filemanager/filemanagerutil " )
2018-03-16 19:47:51 +00:00
local lfs = require ( " libs/libkoreader-lfs " )
2016-12-29 08:10:38 +00:00
local logger = require ( " logger " )
2017-08-22 15:24:31 +00:00
local util = require ( " util " )
2014-08-20 06:41:45 +00:00
local _ = require ( " gettext " )
2016-12-06 21:15:52 +00:00
local T = require ( " ffi/util " ) . template
2014-08-20 06:41:45 +00:00
2017-12-17 12:02:08 +00:00
local wikipedia_history = nil
2014-08-20 06:41:45 +00:00
-- Wikipedia as a special dictionary
2014-10-15 10:01:58 +00:00
local ReaderWikipedia = ReaderDictionary : extend {
2014-08-20 06:41:45 +00:00
-- identify itself
2016-12-06 21:15:52 +00:00
is_wiki = true ,
2017-12-17 12:02:08 +00:00
disable_history = G_reader_settings : isTrue ( " wikipedia_disable_history " ) ,
2014-08-20 06:41:45 +00:00
}
2014-10-15 10:01:58 +00:00
function ReaderWikipedia : init ( )
2022-01-16 16:48:13 +00:00
self.wiki_languages = { }
2016-12-06 21:15:52 +00:00
self.ui . menu : registerToMainMenu ( self )
2017-12-17 12:02:08 +00:00
if not wikipedia_history then
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
wikipedia_history = LuaData : open ( DataStorage : getSettingsDir ( ) .. " /wikipedia_history.lua " , " WikipediaHistory " )
2017-12-17 12:02:08 +00:00
end
2014-10-15 10:01:58 +00:00
end
2017-01-14 17:38:28 +00:00
function ReaderWikipedia : lookupInput ( )
self.input_dialog = InputDialog : new {
2021-04-02 15:59:29 +00:00
title = _ ( " Enter a word or phrase to look up " ) ,
2017-01-14 17:38:28 +00:00
input = " " ,
input_type = " text " ,
buttons = {
{
{
text = _ ( " Cancel " ) ,
2022-03-04 20:20:00 +00:00
id = " close " ,
2017-01-14 17:38:28 +00:00
callback = function ( )
UIManager : close ( self.input_dialog )
end ,
} ,
{
2017-04-07 13:20:57 +00:00
text = _ ( " Search Wikipedia " ) ,
2017-01-14 17:38:28 +00:00
is_enter_default = true ,
callback = function ( )
2021-04-04 15:27:17 +00:00
if self.input_dialog : getInputText ( ) == " " then return end
2017-01-14 17:38:28 +00:00
UIManager : close ( self.input_dialog )
2021-01-01 13:34:51 +00:00
-- Trust that input text does not need any cleaning (allows querying for "-suffix")
self : onLookupWikipedia ( self.input_dialog : getInputText ( ) , true )
2017-01-14 17:38:28 +00:00
end ,
} ,
}
} ,
}
UIManager : show ( self.input_dialog )
2018-03-30 10:46:36 +00:00
self.input_dialog : onShowKeyboard ( )
2017-01-14 17:38:28 +00:00
end
2017-03-04 13:46:38 +00:00
function ReaderWikipedia : addToMainMenu ( menu_items )
menu_items.wikipedia_lookup = {
2016-12-06 21:15:52 +00:00
text = _ ( " Wikipedia lookup " ) ,
2019-03-02 12:29:10 +00:00
callback = function ( ) self : onShowWikipediaLookup ( ) end ,
2017-02-28 21:46:32 +00:00
}
2017-12-17 12:02:08 +00:00
menu_items.wikipedia_history = {
text = _ ( " Wikipedia history " ) ,
enabled_func = function ( )
return wikipedia_history : has ( " wikipedia_history " )
end ,
callback = function ( )
local wikipedia_history_table = wikipedia_history : readSetting ( " wikipedia_history " )
local kv_pairs = { }
local previous_title
self : initLanguages ( ) -- so current lang is set
for i = # wikipedia_history_table , 1 , - 1 do
local value = wikipedia_history_table [ i ]
if value.book_title ~= previous_title then
table.insert ( kv_pairs , { value.book_title .. " : " , " " } )
end
previous_title = value.book_title
local type_s = " ▱ " -- lookup: small white parallelogram
if value.page then
type_s = " ▤ " -- full page: large square with lines
end
local lang_s = " "
if value.lang ~= self.wiki_languages [ 1 ] : lower ( ) then
-- We show item's lang only when different from current lang
lang_s = " [ " .. value.lang : upper ( ) .. " ] "
end
local text = type_s .. value.word .. lang_s
table.insert ( kv_pairs , {
os.date ( " %Y-%m-%d %H:%M:%S " , value.time ) ,
text ,
callback = function ( )
2021-01-01 13:34:51 +00:00
-- Word had been cleaned before being added to history
self : onLookupWikipedia ( value.word , true , nil , value.page , value.lang )
2017-12-17 12:02:08 +00:00
end
} )
end
UIManager : show ( KeyValuePage : new {
title = _ ( " Wikipedia history " ) ,
2021-02-20 19:15:43 +00:00
value_overflow_align = " right " ,
2017-12-17 12:02:08 +00:00
kv_pairs = kv_pairs ,
} )
end ,
}
2024-03-13 23:16:52 +00:00
local function genChoiceMenuEntry ( title , setting , value , default )
return {
text = title ,
checked_func = function ( )
return G_reader_settings : readSetting ( setting , default ) == value
end ,
callback = function ( )
G_reader_settings : saveSetting ( setting , value )
end ,
}
end
2017-08-22 15:24:31 +00:00
menu_items.wikipedia_settings = {
text = _ ( " Wikipedia settings " ) ,
sub_item_table = {
{
text = _ ( " Set Wikipedia languages " ) ,
2018-09-04 21:55:58 +00:00
keep_menu_open = true ,
2017-08-22 15:24:31 +00:00
callback = function ( )
local wikilang_input
local function save_wikilang ( )
local wiki_languages = { }
local langs = wikilang_input : getInputText ( )
for lang in langs : gmatch ( " %S+ " ) do
if not lang : match ( " ^[%a-]+$ " ) then
UIManager : show ( InfoMessage : new {
text = T ( _ ( " %1 does not look like a valid Wikipedia language. " ) , lang )
} )
return
end
lang = lang : lower ( )
table.insert ( wiki_languages , lang )
end
G_reader_settings : saveSetting ( " wikipedia_languages " , wiki_languages )
-- re-init languages
self.wiki_languages = { }
self : initLanguages ( )
UIManager : close ( wikilang_input )
end
-- Use the list built by initLanguages (even if made from UI
-- and document languages) as the initial value
self : initLanguages ( )
local curr_languages = table.concat ( self.wiki_languages , " " )
wikilang_input = InputDialog : new {
title = _ ( " Wikipedia languages " ) ,
input = curr_languages ,
input_hint = " en fr zh " ,
input_type = " text " ,
2018-03-16 19:47:51 +00:00
description = _ ( " Enter one or more Wikipedia language codes (the 2 or 3 letters before .wikipedia.org), in the order you wish to see them available, separated by a space. For example: \n en fr zh \n \n Full list at https://en.wikipedia.org/wiki/List_of_Wikipedias " ) ,
2017-08-22 15:24:31 +00:00
buttons = {
{
{
text = _ ( " Cancel " ) ,
2022-03-04 20:20:00 +00:00
id = " close " ,
2017-08-22 15:24:31 +00:00
callback = function ( )
UIManager : close ( wikilang_input )
end ,
} ,
{
text = _ ( " Save " ) ,
is_enter_default = true ,
callback = save_wikilang ,
} ,
}
} ,
}
UIManager : show ( wikilang_input )
2018-03-30 10:46:36 +00:00
wikilang_input : onShowKeyboard ( )
2017-08-22 15:24:31 +00:00
end ,
2024-03-13 23:16:52 +00:00
separator = true ,
2017-08-22 15:24:31 +00:00
} ,
{ -- setting used by dictquicklookup
2021-02-22 17:44:16 +00:00
text = _ ( " Set Wikipedia 'Save as EPUB' folder " ) ,
2018-09-04 21:55:58 +00:00
keep_menu_open = true ,
2023-12-16 08:36:57 +00:00
help_text = _ ( [ [
2019-08-22 15:11:47 +00:00
Wikipedia articles can be saved as an EPUB for more comfortable reading .
2018-03-16 19:47:51 +00:00
2023-12-16 08:36:57 +00:00
You can choose an existing folder , or use a default folder named " Wikipedia " in your reader ' s home folder.]]),
callback = function ( )
local title_header = _ ( " Current Wikipedia 'Save as EPUB' folder: " )
local current_path = G_reader_settings : readSetting ( " wikipedia_save_dir " )
local default_path = DictQuickLookup.getWikiSaveEpubDefaultDir ( )
local caller_callback = function ( path )
G_reader_settings : saveSetting ( " wikipedia_save_dir " , path )
if not util.pathExists ( path ) then
lfs.mkdir ( path )
2017-08-22 15:24:31 +00:00
end
end
2023-12-16 08:36:57 +00:00
filemanagerutil.showChooseDialog ( title_header , caller_callback , current_path , default_path )
2018-03-16 19:47:51 +00:00
end ,
} ,
{ -- setting used by dictquicklookup
2021-02-22 17:44:16 +00:00
text = _ ( " Save Wikipedia EPUB in current book folder " ) ,
2018-03-16 19:47:51 +00:00
checked_func = function ( )
return G_reader_settings : isTrue ( " wikipedia_save_in_book_dir " )
end ,
callback = function ( )
G_reader_settings : flipNilOrFalse ( " wikipedia_save_in_book_dir " )
2017-08-22 15:24:31 +00:00
end ,
2024-03-13 23:16:52 +00:00
} ,
{ -- setting used in wikipedia.lua
text_func = function ( )
local include_images = _ ( " ask " )
if G_reader_settings : readSetting ( " wikipedia_epub_include_images " ) == true then
include_images = _ ( " always " )
elseif G_reader_settings : readSetting ( " wikipedia_epub_include_images " ) == false then
include_images = _ ( " never " )
end
return T ( _ ( " Include images in EPUB: %1 " ) , include_images )
end ,
sub_item_table = {
genChoiceMenuEntry ( _ ( " Ask " ) , " wikipedia_epub_include_images " , nil ) ,
genChoiceMenuEntry ( _ ( " Include images " ) , " wikipedia_epub_include_images " , true ) ,
genChoiceMenuEntry ( _ ( " Don't include images " ) , " wikipedia_epub_include_images " , false ) ,
} ,
} ,
{ -- setting used in wikipedia.lua
text_func = function ( )
local images_quality = _ ( " ask " )
if G_reader_settings : readSetting ( " wikipedia_epub_highres_images " ) == true then
images_quality = _ ( " higher " )
elseif G_reader_settings : readSetting ( " wikipedia_epub_highres_images " ) == false then
images_quality = _ ( " standard " )
end
return T ( _ ( " Images quality in EPUB: %1 " ) , images_quality )
end ,
enabled_func = function ( )
return G_reader_settings : readSetting ( " wikipedia_epub_include_images " ) ~= false
end ,
sub_item_table = {
genChoiceMenuEntry ( _ ( " Ask " ) , " wikipedia_epub_highres_images " , nil ) ,
genChoiceMenuEntry ( _ ( " Standard quality " ) , " wikipedia_epub_highres_images " , false ) ,
genChoiceMenuEntry ( _ ( " Higher quality " ) , " wikipedia_epub_highres_images " , true ) ,
} ,
2017-12-17 12:02:08 +00:00
separator = true ,
} ,
{
text = _ ( " Enable Wikipedia history " ) ,
checked_func = function ( )
return not self.disable_history
end ,
callback = function ( )
self.disable_history = not self.disable_history
G_reader_settings : saveSetting ( " wikipedia_disable_history " , self.disable_history )
end ,
} ,
{
text = _ ( " Clean Wikipedia history " ) ,
2021-05-06 15:27:39 +00:00
enabled_func = function ( )
return wikipedia_history : has ( " wikipedia_history " )
end ,
2018-09-04 21:55:58 +00:00
keep_menu_open = true ,
2021-05-06 15:27:39 +00:00
callback = function ( touchmenu_instance )
2017-12-17 12:02:08 +00:00
UIManager : show ( ConfirmBox : new {
text = _ ( " Clean Wikipedia history? " ) ,
ok_text = _ ( " Clean " ) ,
ok_callback = function ( )
-- empty data table to replace current one
wikipedia_history : reset { }
2021-05-06 15:27:39 +00:00
touchmenu_instance : updateItems ( )
2017-12-17 12:02:08 +00:00
end ,
} )
end ,
2018-01-16 11:36:52 +00:00
separator = true ,
} ,
{ -- setting used in wikipedia.lua
text = _ ( " Show image in search results " ) ,
checked_func = function ( )
return G_reader_settings : nilOrTrue ( " wikipedia_show_image " )
end ,
callback = function ( )
G_reader_settings : flipNilOrTrue ( " wikipedia_show_image " )
end ,
} ,
{ -- setting used in wikipedia.lua
text = _ ( " Show more images in full article " ) ,
enabled_func = function ( )
return G_reader_settings : nilOrTrue ( " wikipedia_show_image " )
end ,
checked_func = function ( )
return G_reader_settings : nilOrTrue ( " wikipedia_show_more_images " ) and G_reader_settings : nilOrTrue ( " wikipedia_show_image " )
end ,
callback = function ( )
G_reader_settings : flipNilOrTrue ( " wikipedia_show_more_images " )
end ,
} ,
2017-08-22 15:24:31 +00:00
}
}
2016-12-06 21:15:52 +00:00
end
function ReaderWikipedia : initLanguages ( word )
if # self.wiki_languages > 0 then -- already done
return
end
-- Fill self.wiki_languages with languages to propose
local wikipedia_languages = G_reader_settings : readSetting ( " wikipedia_languages " )
if type ( wikipedia_languages ) == " table " and # wikipedia_languages > 0 then
2022-01-16 16:48:13 +00:00
-- use this setting, no need to guess: we reference the setting table, so
-- any update to it will have it saved in settings
2016-12-06 21:15:52 +00:00
self.wiki_languages = wikipedia_languages
else
-- guess some languages
self.seen_lang = { }
local addLanguage = function ( lang )
if lang and lang ~= " " then
-- convert "zh-CN" and "zh-TW" to "zh"
lang = lang : match ( " (.*)- " ) or lang
if lang == " C " then lang = " en " end
lang = lang : lower ( )
if not self.seen_lang [ lang ] then
table.insert ( self.wiki_languages , lang )
self.seen_lang [ lang ] = true
end
end
end
-- use book and UI languages
2017-05-12 16:28:42 +00:00
if self.view then
2023-08-30 04:53:59 +00:00
addLanguage ( self.ui . doc_props.language )
2017-05-12 16:28:42 +00:00
end
2016-12-06 21:15:52 +00:00
addLanguage ( G_reader_settings : readSetting ( " language " ) )
if # self.wiki_languages == 0 and word then
-- if no language at all, do a translation of selected word
local ok_translator , lang
2016-10-26 17:20:12 +00:00
ok_translator , lang = pcall ( Translator.detect , Translator , word )
2016-12-06 21:15:52 +00:00
if ok_translator then
addLanguage ( lang )
end
2016-10-26 17:20:12 +00:00
end
2016-12-06 21:15:52 +00:00
-- add english anyway, so we have at least one language
addLanguage ( " en " )
end
end
2021-01-01 13:34:51 +00:00
function ReaderWikipedia : onLookupWikipedia ( word , is_sane , box , get_fullpage , forced_lang )
2018-01-16 11:36:52 +00:00
-- Wrapped through Trapper, as we may be using Trapper:dismissableRunInSubprocess() in it
Trapper : wrap ( function ( )
2021-01-01 13:34:51 +00:00
self : lookupWikipedia ( word , is_sane , box , get_fullpage , forced_lang )
2018-01-16 11:36:52 +00:00
end )
return true
end
2021-01-01 13:34:51 +00:00
function ReaderWikipedia : lookupWikipedia ( word , is_sane , box , get_fullpage , forced_lang )
if NetworkMgr : willRerunWhenOnline ( function ( ) self : lookupWikipedia ( word , is_sane , box , get_fullpage , forced_lang ) end ) then
Various Wi-Fi QoL improvements (#6424)
* Revamped most actions that require an internet connection to a new/fixed backend that allows forwarding the initial action and running it automatically once connected. (i.e., it'll allow you to set "Action when Wi-Fi is off" to "turn_on", and whatch stuff connect and do what you wanted automatically without having to re-click anywhere instead of showing you a Wi-Fi prompt and then not doing anything without any other feedback).
* Speaking of, fixed the "turn_on" beforeWifi action to, well, actually work. It's no longer marked as experimental.
* Consistently use "Wi-Fi" everywhere.
* On Kobo/Cervantes/Sony, implemented a "Kill Wi-Fi connection when inactive" system that will automatically disconnect from Wi-Fi after sustained *network* inactivity (i.e., you can keep reading, it'll eventually turn off on its own). This should be smart and flexible enough not to murder Wi-Fi while you need it, while still not keeping it uselessly on and murdering your battery.
(i.e., enable that + turn Wi-Fi on when off and enjoy never having to bother about Wi-Fi ever again).
* Made sending `NetworkConnected` / `NetworkDisconnected` events consistent (they were only being sent... sometimes, which made relying on 'em somewhat problematic).
* restoreWifiAsync is now only run when really needed (i.e., we no longer stomp on an existing working connection just for the hell of it).
* We no longer attempt to kill a bogus non-existent Wi-Fi connection when going to suspend, we only do it when it's actually needed.
* Every method of enabling Wi-Fi will now properly tear down Wi-Fi on failure, instead of leaving it in an undefined state.
* Fixed an issue in the fancy crash screen on Kobo/reMarkable that could sometime lead to the log excerpt being missing.
* Worked-around a number of sneaky issues related to low-level Wi-Fi/DHCP/DNS handling on Kobo (see the lengthy comments [below](https://github.com/koreader/koreader/pull/6424#issuecomment-663881059) for details). Fix #6421
Incidentally, this should also fix the inconsistencies experienced re: Wi-Fi behavior in Nickel when toggling between KOReader and Nickel (use NM/KFMon, and run a current FW for best results).
* For developers, this involves various cleanups around NetworkMgr and NetworkListener. Documentation is in-line, above the concerned functions.
2020-07-27 01:39:06 +00:00
-- Not online yet, nothing more to do here, NetworkMgr will forward the callback and run it once connected!
2017-01-14 17:38:28 +00:00
return
end
Various Wi-Fi QoL improvements (#6424)
* Revamped most actions that require an internet connection to a new/fixed backend that allows forwarding the initial action and running it automatically once connected. (i.e., it'll allow you to set "Action when Wi-Fi is off" to "turn_on", and whatch stuff connect and do what you wanted automatically without having to re-click anywhere instead of showing you a Wi-Fi prompt and then not doing anything without any other feedback).
* Speaking of, fixed the "turn_on" beforeWifi action to, well, actually work. It's no longer marked as experimental.
* Consistently use "Wi-Fi" everywhere.
* On Kobo/Cervantes/Sony, implemented a "Kill Wi-Fi connection when inactive" system that will automatically disconnect from Wi-Fi after sustained *network* inactivity (i.e., you can keep reading, it'll eventually turn off on its own). This should be smart and flexible enough not to murder Wi-Fi while you need it, while still not keeping it uselessly on and murdering your battery.
(i.e., enable that + turn Wi-Fi on when off and enjoy never having to bother about Wi-Fi ever again).
* Made sending `NetworkConnected` / `NetworkDisconnected` events consistent (they were only being sent... sometimes, which made relying on 'em somewhat problematic).
* restoreWifiAsync is now only run when really needed (i.e., we no longer stomp on an existing working connection just for the hell of it).
* We no longer attempt to kill a bogus non-existent Wi-Fi connection when going to suspend, we only do it when it's actually needed.
* Every method of enabling Wi-Fi will now properly tear down Wi-Fi on failure, instead of leaving it in an undefined state.
* Fixed an issue in the fancy crash screen on Kobo/reMarkable that could sometime lead to the log excerpt being missing.
* Worked-around a number of sneaky issues related to low-level Wi-Fi/DHCP/DNS handling on Kobo (see the lengthy comments [below](https://github.com/koreader/koreader/pull/6424#issuecomment-663881059) for details). Fix #6421
Incidentally, this should also fix the inconsistencies experienced re: Wi-Fi behavior in Nickel when toggling between KOReader and Nickel (use NM/KFMon, and run a current FW for best results).
* For developers, this involves various cleanups around NetworkMgr and NetworkListener. Documentation is in-line, above the concerned functions.
2020-07-27 01:39:06 +00:00
2016-12-06 21:15:52 +00:00
-- word is the text to query. If get_fullpage is true, it is the
-- exact wikipedia page title we want the full page of.
self : initLanguages ( word )
2017-01-21 18:23:13 +00:00
local lang
if forced_lang then
2022-01-16 16:48:13 +00:00
-- use provided lang (from readerlink when noticing that an external link is a wikipedia url,
-- of from Wikipedia lookup history, or when switching to next language in DictQuickLookup)
2017-01-21 18:23:13 +00:00
lang = forced_lang
else
2022-01-16 16:48:13 +00:00
-- use first lang from self.wiki_languages
2017-01-21 18:23:13 +00:00
lang = self.wiki_languages [ 1 ]
end
2016-12-29 08:10:38 +00:00
logger.dbg ( " lookup word: " , word , box , get_fullpage )
2016-12-06 21:15:52 +00:00
-- no need to clean word if get_fullpage, as it is the exact wikipetia page title
if word and not get_fullpage then
-- escape quotes and other funny characters in word
2021-01-01 13:34:51 +00:00
word = self : cleanSelection ( word , is_sane )
2016-12-06 21:15:52 +00:00
-- no need to lower() word with wikipedia search
end
2016-12-29 08:10:38 +00:00
logger.dbg ( " stripped word: " , word )
2016-12-06 21:15:52 +00:00
if word == " " then
return
2016-10-26 17:20:12 +00:00
end
2017-12-17 12:02:08 +00:00
local display_word = word : gsub ( " _ " , " " )
if not self.disable_history then
2023-08-30 04:53:59 +00:00
local book_title = self.ui . doc_props and self.ui . doc_props.display_title or _ ( " Wikipedia lookup " )
2017-12-17 12:02:08 +00:00
wikipedia_history : addTableItem ( " wikipedia_history " , {
book_title = book_title ,
time = os.time ( ) ,
word = display_word ,
lang = lang : lower ( ) ,
page = get_fullpage ,
} )
end
2017-01-01 09:34:21 +00:00
2018-01-16 11:36:52 +00:00
-- Fix lookup message to include lang and set appropriate error texts
local no_result_text , req_failure_text
2017-01-01 09:34:21 +00:00
if get_fullpage then
2018-01-16 11:36:52 +00:00
self.lookup_msg = T ( _ ( " Retrieving Wikipedia %2 article: \n %1 " ) , " %1 " , lang : upper ( ) )
req_failure_text = _ ( " Failed to retrieve Wikipedia article. " )
no_result_text = _ ( " Wikipedia article not found. " )
2017-01-01 09:34:21 +00:00
else
self.lookup_msg = T ( _ ( " Searching Wikipedia %2 for: \n %1 " ) , " %1 " , lang : upper ( ) )
2018-01-16 11:36:52 +00:00
req_failure_text = _ ( " Failed searching Wikipedia. " )
2021-04-02 15:59:29 +00:00
no_result_text = _ ( " No results. " )
2017-01-01 09:34:21 +00:00
end
2017-12-17 12:02:08 +00:00
self : showLookupInfo ( display_word )
2018-01-16 11:36:52 +00:00
2014-08-20 06:41:45 +00:00
local results = { }
2016-12-06 21:15:52 +00:00
local ok , pages
2018-01-16 11:36:52 +00:00
local lookup_cancelled = false
Wikipedia : setTrapWidget ( self.lookup_progress_msg )
2016-12-06 21:15:52 +00:00
if get_fullpage then
2018-01-16 11:36:52 +00:00
ok , pages = pcall ( Wikipedia.getFullPage , Wikipedia , word , lang )
2016-12-06 21:15:52 +00:00
else
2018-01-16 11:36:52 +00:00
ok , pages = pcall ( Wikipedia.searchAndGetIntros , Wikipedia , word , lang )
end
Wikipedia : resetTrapWidget ( )
if not ok and pages and string.find ( pages , Wikipedia.dismissed_error_code ) then
-- So we can display an alternate dummy result
lookup_cancelled = true
-- Or we could just not show anything with:
-- self:dismissLookupInfo()
-- return
2016-12-06 21:15:52 +00:00
end
2014-08-20 06:41:45 +00:00
if ok and pages then
2016-12-06 21:15:52 +00:00
-- sort pages according to 'index' attribute if present (not present
-- in fullpage results)
local sorted_pages = { }
local has_indexes = false
for pageid , page in pairs ( pages ) do
if page.index ~= nil then
sorted_pages [ page.index + 1 ] = page
has_indexes = true
end
end
if has_indexes then
pages = sorted_pages
end
2014-08-20 06:41:45 +00:00
for pageid , page in pairs ( pages ) do
2023-05-11 18:23:47 +00:00
local definition = page.extract or ( page.length and _ ( " No introduction. " ) ) or no_result_text
2016-12-06 21:15:52 +00:00
if page.length then
-- we get 'length' only for intro results
-- let's append it to definition so we know
-- how big/valuable the full page is
local fullkb = math.ceil ( page.length / 1024 )
local more_factor = math.ceil ( page.length / ( 1 + definition : len ( ) ) ) -- +1 just in case len()=0
2018-01-16 11:36:52 +00:00
definition = definition .. " \n " .. T ( _ ( " (full article : %1 kB, = %2 x this intro length) " ) , fullkb , more_factor )
2016-12-06 21:15:52 +00:00
end
2014-08-20 06:41:45 +00:00
local result = {
2016-12-06 21:15:52 +00:00
dict = T ( _ ( " Wikipedia %1 " ) , lang : upper ( ) ) ,
2014-08-20 06:41:45 +00:00
word = page.title ,
2016-12-06 21:15:52 +00:00
definition = definition ,
2020-12-22 17:45:34 +00:00
is_wiki_fullpage = get_fullpage ,
2017-01-21 18:23:13 +00:00
lang = lang ,
2019-12-06 21:55:35 +00:00
rtl_lang = Wikipedia : isWikipediaLanguageRTL ( lang ) ,
2018-01-16 11:36:52 +00:00
images = page.images ,
2014-08-20 06:41:45 +00:00
}
table.insert ( results , result )
end
2017-12-17 12:02:08 +00:00
-- logger.dbg of results will be done by ReaderDictionary:showDict()
2014-08-20 06:41:45 +00:00
else
-- dummy results
2018-01-16 11:36:52 +00:00
local definition
if lookup_cancelled then
2021-01-09 20:59:27 +00:00
definition = _ ( " Wikipedia request interrupted. " )
2018-01-16 11:36:52 +00:00
elseif ok then
definition = no_result_text
else
definition = req_failure_text
logger.dbg ( " error: " , pages )
end
2014-08-20 06:41:45 +00:00
results = {
{
2016-12-06 21:15:52 +00:00
dict = T ( _ ( " Wikipedia %1 " ) , lang : upper ( ) ) ,
2014-08-20 06:41:45 +00:00
word = word ,
2018-01-16 11:36:52 +00:00
definition = definition ,
2020-12-22 17:45:34 +00:00
is_wiki_fullpage = get_fullpage ,
2017-01-21 18:23:13 +00:00
lang = lang ,
2014-08-20 06:41:45 +00:00
}
}
2022-01-16 16:48:13 +00:00
-- Also put this as a k/v into the results array: if we end up with this
-- after lang rotation, DictQuickLookup will not update this lang rotation.
results.no_result = true
2016-12-29 08:10:38 +00:00
logger.dbg ( " dummy result table: " , word , results )
2014-08-20 06:41:45 +00:00
end
2016-12-06 21:15:52 +00:00
self : showDict ( word , results , box )
2014-08-20 06:41:45 +00:00
end
2022-01-16 16:48:13 +00:00
function ReaderWikipedia : getWikiLanguages ( first_lang )
-- Always return a copy of ours
local wiki_languages = { unpack ( self.wiki_languages ) }
local is_first_lang = first_lang == wiki_languages [ 1 ]
if not is_first_lang then
-- return a wiki_languages with requested lang at first
if util.arrayContains ( wiki_languages , first_lang ) then
-- first_lang in the list: rotate until it is first
while wiki_languages [ 1 ] ~= first_lang do
table.insert ( wiki_languages , table.remove ( wiki_languages , 1 ) )
end
else
-- first_lang not in the list: add it first
table.insert ( wiki_languages , 1 , first_lang )
end
end
local update_wiki_languages_on_close = false
2022-10-08 21:10:58 +00:00
if DictQuickLookup.rotated_update_wiki_languages_on_close ~= nil then
2022-01-16 16:48:13 +00:00
-- Flag set by DictQuickLookup when rotating, forwarding the flag
-- of the rotated out DictQuickLookup instance: trust it
2022-10-08 21:10:58 +00:00
update_wiki_languages_on_close = DictQuickLookup.rotated_update_wiki_languages_on_close
DictQuickLookup.rotated_update_wiki_languages_on_close = nil
2022-01-16 16:48:13 +00:00
else
-- Not a rotation. Only if it's the first request with the current
-- first language, we will have it (and any lang rotation from it)
-- update the main ReaderWikipedia.wiki_languages. That is, queries
-- from Wikipedia url links for another language, or from Wikipedia
-- lookup history with other languages (and any lang rotation made
-- from them) won't update it.
if is_first_lang then
update_wiki_languages_on_close = true
2022-10-08 21:10:58 +00:00
for i = # DictQuickLookup.window_list - 1 , 1 , - 1 do -- (ignore the last one, which is the one calling this)
if DictQuickLookup.window_list [ i ] . is_wiki then
2022-01-16 16:48:13 +00:00
-- Another upper Wikipedia result: only this one may update it
update_wiki_languages_on_close = false
break
end
end
end
end
return wiki_languages , update_wiki_languages_on_close
end
function ReaderWikipedia : onUpdateWikiLanguages ( wiki_languages )
-- Update our self.wiki_languages in-place
while table.remove ( self.wiki_languages ) do end
for _ , lang in ipairs ( wiki_languages ) do
table.insert ( self.wiki_languages , lang )
end
end
2014-10-28 07:58:04 +00:00
-- override onSaveSettings in ReaderDictionary
function ReaderWikipedia : onSaveSettings ( )
end
2019-03-02 12:29:10 +00:00
function ReaderWikipedia : onShowWikipediaLookup ( )
Various Wi-Fi QoL improvements (#6424)
* Revamped most actions that require an internet connection to a new/fixed backend that allows forwarding the initial action and running it automatically once connected. (i.e., it'll allow you to set "Action when Wi-Fi is off" to "turn_on", and whatch stuff connect and do what you wanted automatically without having to re-click anywhere instead of showing you a Wi-Fi prompt and then not doing anything without any other feedback).
* Speaking of, fixed the "turn_on" beforeWifi action to, well, actually work. It's no longer marked as experimental.
* Consistently use "Wi-Fi" everywhere.
* On Kobo/Cervantes/Sony, implemented a "Kill Wi-Fi connection when inactive" system that will automatically disconnect from Wi-Fi after sustained *network* inactivity (i.e., you can keep reading, it'll eventually turn off on its own). This should be smart and flexible enough not to murder Wi-Fi while you need it, while still not keeping it uselessly on and murdering your battery.
(i.e., enable that + turn Wi-Fi on when off and enjoy never having to bother about Wi-Fi ever again).
* Made sending `NetworkConnected` / `NetworkDisconnected` events consistent (they were only being sent... sometimes, which made relying on 'em somewhat problematic).
* restoreWifiAsync is now only run when really needed (i.e., we no longer stomp on an existing working connection just for the hell of it).
* We no longer attempt to kill a bogus non-existent Wi-Fi connection when going to suspend, we only do it when it's actually needed.
* Every method of enabling Wi-Fi will now properly tear down Wi-Fi on failure, instead of leaving it in an undefined state.
* Fixed an issue in the fancy crash screen on Kobo/reMarkable that could sometime lead to the log excerpt being missing.
* Worked-around a number of sneaky issues related to low-level Wi-Fi/DHCP/DNS handling on Kobo (see the lengthy comments [below](https://github.com/koreader/koreader/pull/6424#issuecomment-663881059) for details). Fix #6421
Incidentally, this should also fix the inconsistencies experienced re: Wi-Fi behavior in Nickel when toggling between KOReader and Nickel (use NM/KFMon, and run a current FW for best results).
* For developers, this involves various cleanups around NetworkMgr and NetworkListener. Documentation is in-line, above the concerned functions.
2020-07-27 01:39:06 +00:00
local connect_callback = function ( )
2019-03-02 12:29:10 +00:00
self : lookupInput ( )
end
Various Wi-Fi QoL improvements (#6424)
* Revamped most actions that require an internet connection to a new/fixed backend that allows forwarding the initial action and running it automatically once connected. (i.e., it'll allow you to set "Action when Wi-Fi is off" to "turn_on", and whatch stuff connect and do what you wanted automatically without having to re-click anywhere instead of showing you a Wi-Fi prompt and then not doing anything without any other feedback).
* Speaking of, fixed the "turn_on" beforeWifi action to, well, actually work. It's no longer marked as experimental.
* Consistently use "Wi-Fi" everywhere.
* On Kobo/Cervantes/Sony, implemented a "Kill Wi-Fi connection when inactive" system that will automatically disconnect from Wi-Fi after sustained *network* inactivity (i.e., you can keep reading, it'll eventually turn off on its own). This should be smart and flexible enough not to murder Wi-Fi while you need it, while still not keeping it uselessly on and murdering your battery.
(i.e., enable that + turn Wi-Fi on when off and enjoy never having to bother about Wi-Fi ever again).
* Made sending `NetworkConnected` / `NetworkDisconnected` events consistent (they were only being sent... sometimes, which made relying on 'em somewhat problematic).
* restoreWifiAsync is now only run when really needed (i.e., we no longer stomp on an existing working connection just for the hell of it).
* We no longer attempt to kill a bogus non-existent Wi-Fi connection when going to suspend, we only do it when it's actually needed.
* Every method of enabling Wi-Fi will now properly tear down Wi-Fi on failure, instead of leaving it in an undefined state.
* Fixed an issue in the fancy crash screen on Kobo/reMarkable that could sometime lead to the log excerpt being missing.
* Worked-around a number of sneaky issues related to low-level Wi-Fi/DHCP/DNS handling on Kobo (see the lengthy comments [below](https://github.com/koreader/koreader/pull/6424#issuecomment-663881059) for details). Fix #6421
Incidentally, this should also fix the inconsistencies experienced re: Wi-Fi behavior in Nickel when toggling between KOReader and Nickel (use NM/KFMon, and run a current FW for best results).
* For developers, this involves various cleanups around NetworkMgr and NetworkListener. Documentation is in-line, above the concerned functions.
2020-07-27 01:39:06 +00:00
NetworkMgr : runWhenOnline ( connect_callback )
2019-03-02 12:29:10 +00:00
return true
end
2014-08-20 06:41:45 +00:00
return ReaderWikipedia