123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- --[[--
- This is a debug plugin to test Plugin functionality.
- @module koplugin.HelloWorld
- --]]--
- -- This is a debug plugin, remove the following if block to enable it
- local Dispatcher = require("dispatcher") -- luacheck:ignore
- local InfoMessage = require("ui/widget/infomessage")
- local UIManager = require("ui/uimanager")
- local WidgetContainer = require("ui/widget/container/widgetcontainer")
- local socket = require("socket")
- local BackgroundTaskPlugin = require("ui/plugin/background_task_plugin")
- local PluginShare = require("pluginshare")
- local _ = require("gettext")
- local Hello = WidgetContainer:new{
- name = "firefoxreader",
- is_doc_only = false,
- }
- function Hello:onDispatcherRegisterActions()
- Dispatcher:registerAction("helloworld_action", {category="none", event="HelloWorld", title=_("Hello World"), general=true,})
- end
- function Hello:checkForConnection()
- print("FFR: Background job is run!", self)
- if self.socket == nil then
- print("FFR: no socket")
- --self:bindSocket()
- end
- print("FFR: Socket is now", self.socket)
- local client = self.socket:accept()
- if client == nil then
- print("No client")
- return
- end
- local line = ""
- local headers = ""
- repeat
- line = client:receive('*l')
- headers = headers .. line .. "\n"
- until line == ""
- local _, _, content_length = string.find(headers, "\n[Cc]ontent.[Ll]ength: (%d+)\n")
- print("Content length is " .. tostring(content_length))
- local data = client:receive(tonumber(content_length))
- client:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n")
- client:close()
- if data == nil then
- return
- end
- print("Received data")
- local f = io.open(self.tmppath, "w")
- print("File opened")
- f:write(data)
- print("Data written")
- f:close()
- print("File closed")
- local ReaderUI = require("apps/reader/readerui")
- ReaderUI:showReader(self.tmppath)
- print("FFR: Received data: " .. data)
- end
- function Hello:bindSocket()
- print("FFR: Initializing plugin")
- self.socket = socket.bind("192.168.15.244", "17144")
- self.socket:settimeout(0.02) -- 20ms
- print("FFR: Socket was bound")
- end
- function Hello:_schedule()
- print("Scheduling")
- self:checkForConnection()
- UIManager:scheduleIn(4, self.task)
- end
- function Hello:init()
- print("Initializing")
- self.tmppath = "/mnt/us/documents/test.html"
- self.task = function()
- self:_schedule()
- end
- --table.insert(PluginShare.backgroundJobs, {
- -- when = 2,
- -- repeated = true,
- -- executable = function()
- -- print("hello")
- -- end,
- -- callback = function()
- -- print("Job finished!")
- -- end,
- --})
- local Event = require("ui/event")
- --UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated"))
- --print("scheduled")
- self:onDispatcherRegisterActions()
- self.ui.menu:registerToMainMenu(self)
- self:bindSocket()
- self:_schedule()
- end
- function Hello:addToMainMenu(menu_items)
- menu_items.hello_world = {
- text = _("Hello World"),
- -- in which menu this should be appended
- sorting_hint = "more_tools",
- -- a callback when tapping
- callback = function()
- UIManager:show(InfoMessage:new{
- text = _("Hello, plugin world"),
- })
- end,
- }
- end
- function Hello:onHelloWorld()
- local popup = InfoMessage:new{
- text = _("Hello World"),
- }
- UIManager:show(popup)
- end
- return Hello
|