2020-01-04 00:18:51 +00:00
local BD = require ( " ui/bidi " )
2018-02-22 23:02:32 +00:00
local DataStorage = require ( " datastorage " )
local DocSettings = require ( " frontend/docsettings " )
local ReadHistory = require ( " readhistory " )
local FFIUtil = require ( " ffi/util " )
local Ftp = require ( " apps/cloudstorage/ftp " )
local FtpApi = require ( " apps/cloudstorage/ftpapi " )
local InfoMessage = require ( " ui/widget/infomessage " )
local LuaSettings = require ( " frontend/luasettings " )
local UIManager = require ( " ui/uimanager " )
local NetworkMgr = require ( " ui/network/manager " )
local WidgetContainer = require ( " ui/widget/container/widgetcontainer " )
2018-07-11 15:10:21 +00:00
local ftp = require ( " socket.ftp " )
2018-02-22 23:02:32 +00:00
local logger = require ( " logger " )
local util = require ( " util " )
local _ = require ( " gettext " )
local T = FFIUtil.template
2018-08-17 18:54:11 +00:00
local Send2Ebook = WidgetContainer : new {
name = " send2ebook " ,
}
2018-02-22 23:02:32 +00:00
local initialized = false
local send2ebook_config_file = " send2ebook_settings.lua "
local config_key_custom_dl_dir = " custom_dl_dir " ;
local default_download_dir_name = " send2ebook "
local download_dir_path
local send2ebook_settings
function Send2Ebook : downloadFileAndRemove ( connection_url , remote_path , local_download_path )
local url = connection_url .. remote_path
2018-07-11 15:10:21 +00:00
local response = ftp.get ( url .. " ;type=i " )
2018-02-22 23:02:32 +00:00
if response ~= nil then
local_download_path = util.fixUtf8 ( local_download_path , " _ " )
local file = io.open ( local_download_path , " w " )
file : write ( response )
file : close ( )
FtpApi : delete ( url )
return 1
else
logger.err ( " Send2Ebook: Error. Invalid connection data? " )
return 0
end
end
function Send2Ebook : init ( )
self.ui . menu : registerToMainMenu ( self )
end
function Send2Ebook : addToMainMenu ( menu_items )
self : lazyInitialization ( )
menu_items.send2ebook = {
text = _ ( " Send2Ebook (Receiver) " ) ,
sub_item_table = {
{
text = _ ( " Download and remove from server " ) ,
2018-09-04 21:55:58 +00:00
keep_menu_open = true ,
2018-02-22 23:02:32 +00:00
callback = function ( )
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 ( )
self : process ( )
end
NetworkMgr : runWhenOnline ( connect_callback )
2018-02-22 23:02:32 +00:00
end ,
} ,
{
text = _ ( " Go to download folder " ) ,
callback = function ( )
2020-05-05 16:42:04 +00:00
if self.ui . document then
self.ui : onClose ( )
end
2018-02-22 23:02:32 +00:00
local FileManager = require ( " apps/filemanager/filemanager " )
if FileManager.instance then
FileManager.instance : reinit ( download_dir_path )
else
FileManager : showFiles ( download_dir_path )
end
end ,
} ,
{
text = _ ( " Remove read (opened) articles " ) ,
2018-09-04 21:55:58 +00:00
keep_menu_open = true ,
2018-02-22 23:02:32 +00:00
callback = self.removeReadActicles ,
} ,
{
2021-02-22 17:44:16 +00:00
text = _ ( " Set custom download folder " ) ,
2018-09-04 21:55:58 +00:00
keep_menu_open = true ,
2018-02-22 23:02:32 +00:00
callback = self.setCustomDownloadDirectory ,
} ,
{
text = _ ( " Settings " ) ,
2018-09-04 21:55:58 +00:00
keep_menu_open = true ,
2018-02-22 23:02:32 +00:00
callback = self.editFtpConnection ,
} ,
{
text = _ ( " Help " ) ,
2018-09-04 21:55:58 +00:00
keep_menu_open = true ,
2018-02-22 23:02:32 +00:00
callback = function ( )
UIManager : show ( InfoMessage : new {
2020-01-04 00:18:51 +00:00
text = T ( _ ( ' Send2Ebook lets you send articles found on PC/Android phone to your Ebook reader (using ftp server). \n \n More details: https://github.com/mwoz123/send2ebook \n \n Downloads to local folder: %1 ' ) , BD.dirpath ( download_dir_path ) )
2018-02-22 23:02:32 +00:00
} )
end ,
} ,
} ,
}
end
function Send2Ebook : lazyInitialization ( )
if not initialized then
logger.dbg ( " Send2Ebook: obtaining download folder " )
send2ebook_settings = LuaSettings : open ( ( " %s/%s " ) : format ( DataStorage : getSettingsDir ( ) , send2ebook_config_file ) )
if send2ebook_settings : has ( config_key_custom_dl_dir ) then
download_dir_path = send2ebook_settings : readSetting ( config_key_custom_dl_dir )
else
download_dir_path = ( " %s/%s/ " ) : format ( DataStorage : getFullDataDir ( ) , default_download_dir_name )
end
if not lfs.attributes ( download_dir_path , " mode " ) then
logger.dbg ( " Send2Ebook: Creating initial directory " )
lfs.mkdir ( download_dir_path )
end
end
end
function Send2Ebook : process ( )
2018-02-23 08:18:28 +00:00
local info = InfoMessage : new { text = _ ( " Connecting… " ) }
2018-02-22 23:02:32 +00:00
UIManager : show ( info )
logger.dbg ( " Send2Ebook: force repaint due to upcoming blocking calls " )
UIManager : forceRePaint ( )
UIManager : close ( info )
local count = 1
local ftp_config = send2ebook_settings : readSetting ( " ftp_config " ) or { address = " Please setup ftp in settings " , username = " " , password = " " , folder = " " }
2018-05-04 15:06:58 +00:00
local connection_url = FtpApi : generateUrl ( ftp_config.address , util.urlEncode ( ftp_config.username ) , util.urlEncode ( ftp_config.password ) )
2018-02-22 23:02:32 +00:00
local ftp_files_table = FtpApi : listFolder ( connection_url .. ftp_config.folder , ftp_config.folder ) --args looks strange but otherwise resonse with invalid paths
if not ftp_files_table then
2020-01-04 00:18:51 +00:00
info = InfoMessage : new { text = T ( _ ( " Could not get file list for server: %1, user: %2, folder: %3 " ) , BD.ltr ( ftp_config.address ) , ftp_config.username , BD.dirpath ( ftp_config.folder ) ) }
2018-02-22 23:02:32 +00:00
else
local total_entries = table.getn ( ftp_files_table )
logger.dbg ( " Send2Ebook: total_entries " , total_entries )
if total_entries > 1 then total_entries = total_entries - 2 end --remove result "../" (upper folder) and "./" (current folder)
for idx , ftp_file in ipairs ( ftp_files_table ) do
logger.dbg ( " Send2Ebook: processing ftp_file: " , ftp_file )
2019-08-26 13:49:50 +00:00
--- @todo Recursive download folders.
2018-03-01 19:34:27 +00:00
if ftp_file [ " type " ] == " file " then
2018-02-22 23:02:32 +00:00
info = InfoMessage : new { text = T ( _ ( " Processing %1/%2 " ) , count , total_entries ) }
UIManager : show ( info )
UIManager : forceRePaint ( )
UIManager : close ( info )
local remote_file_path = ftp_file [ " url " ]
logger.dbg ( " Send2Ebook: remote_file_path " , remote_file_path )
local local_file_path = download_dir_path .. ftp_file [ " text " ]
count = count + Send2Ebook : downloadFileAndRemove ( connection_url , remote_file_path , local_file_path )
end
2018-03-01 19:34:27 +00:00
info = InfoMessage : new { text = T ( _ ( " Processing finished. Success: %1, failed: %2 " ) , count - 1 , total_entries + 1 - count ) }
2018-02-22 23:02:32 +00:00
end
end
UIManager : show ( info )
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 : afterWifiAction ( )
2018-02-22 23:02:32 +00:00
end
function Send2Ebook : removeReadActicles ( )
logger.dbg ( " Send2Ebook: Removing read articles from : " , download_dir_path )
for entry in lfs.dir ( download_dir_path ) do
if entry ~= " . " and entry ~= " .. " then
local entry_path = download_dir_path .. entry
if DocSettings : hasSidecarFile ( entry_path ) then
local entry_mode = lfs.attributes ( entry_path , " mode " )
if entry_mode == " file " then
os.remove ( entry_path )
local sdr_dir = DocSettings : getSidecarDir ( entry_path )
logger.dbg ( " Send2Ebook: sdr dir to be removed: " , sdr_dir )
FFIUtil.purgeDir ( sdr_dir )
end
end
end
end
UIManager : show ( InfoMessage : new {
text = _ ( " All read articles removed. " )
} )
end
function Send2Ebook : setCustomDownloadDirectory ( )
require ( " ui/downloadmgr " ) : new {
2019-04-19 18:51:41 +00:00
onConfirm = function ( path )
logger.dbg ( " Send2Ebook: set download directory to: " , path )
send2ebook_settings : saveSetting ( config_key_custom_dl_dir , ( " %s/ " ) : format ( path ) )
send2ebook_settings : flush ( )
initialized = false
Send2Ebook : lazyInitialization ( )
end ,
2018-02-22 23:02:32 +00:00
} : chooseDir ( )
end
function Send2Ebook : editFtpConnection ( )
local item = send2ebook_settings : readSetting ( " ftp_config " ) or { text = " ignore this field, it's not used here;) fill rest " , address = " ftp:// " , username = " " , password = " " , folder = " / " }
local callbackEdit = function ( updated_config , fields )
local data = { text = fields [ 1 ] , address = fields [ 2 ] , username = fields [ 3 ] , password = fields [ 4 ] , folder = fields [ 5 ] }
send2ebook_settings : saveSetting ( " ftp_config " , data )
send2ebook_settings : flush ( )
initialized = false
Send2Ebook : lazyInitialization ( )
end
Ftp : config ( item , callbackEdit )
end
function Send2Ebook : onCloseDocument ( )
local document_full_path = self.ui . document.file
2018-03-11 14:05:30 +00:00
if document_full_path and download_dir_path and download_dir_path == string.sub ( document_full_path , 1 , string.len ( download_dir_path ) ) then
2018-02-22 23:02:32 +00:00
logger.dbg ( " Send2Ebook: document_full_path: " , document_full_path )
logger.dbg ( " Send2Ebook: download_dir_path: " , download_dir_path )
logger.dbg ( " Send2Ebook: removing Send2Ebook file from history. " )
ReadHistory : removeItemByPath ( document_full_path )
2020-05-19 16:21:58 +00:00
self.ui : setLastDirForFileBrowser ( download_dir_path )
2018-02-22 23:02:32 +00:00
end
end
return Send2Ebook