main.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. --[[--
  2. This is a debug plugin to test Plugin functionality.
  3. @module koplugin.HelloWorld
  4. --]]--
  5. -- This is a debug plugin, remove the following if block to enable it
  6. local Dispatcher = require("dispatcher") -- luacheck:ignore
  7. local InfoMessage = require("ui/widget/infomessage")
  8. local UIManager = require("ui/uimanager")
  9. local WidgetContainer = require("ui/widget/container/widgetcontainer")
  10. local socket = require("socket")
  11. local BackgroundTaskPlugin = require("ui/plugin/background_task_plugin")
  12. local PluginShare = require("pluginshare")
  13. local _ = require("gettext")
  14. local Hello = WidgetContainer:new{
  15. name = "firefoxreader",
  16. is_doc_only = false,
  17. }
  18. function Hello:onDispatcherRegisterActions()
  19. Dispatcher:registerAction("helloworld_action", {category="none", event="HelloWorld", title=_("Hello World"), general=true,})
  20. end
  21. function Hello:checkForConnection()
  22. print("FFR: Background job is run!", self)
  23. if self.socket == nil then
  24. print("FFR: no socket")
  25. --self:bindSocket()
  26. end
  27. print("FFR: Socket is now", self.socket)
  28. local client = self.socket:accept()
  29. if client == nil then
  30. print("No client")
  31. return
  32. end
  33. local line = ""
  34. local headers = ""
  35. repeat
  36. line = client:receive('*l')
  37. headers = headers .. line .. "\n"
  38. until line == ""
  39. local _, _, content_length = string.find(headers, "\n[Cc]ontent.[Ll]ength: (%d+)\n")
  40. print("Content length is " .. tostring(content_length))
  41. local data = client:receive(tonumber(content_length))
  42. client:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n")
  43. client:close()
  44. if data == nil then
  45. return
  46. end
  47. print("Received data")
  48. local f = io.open(self.tmppath, "w")
  49. print("File opened")
  50. f:write(data)
  51. print("Data written")
  52. f:close()
  53. print("File closed")
  54. local ReaderUI = require("apps/reader/readerui")
  55. ReaderUI:showReader(self.tmppath)
  56. print("FFR: Received data: " .. data)
  57. end
  58. function Hello:bindSocket()
  59. print("FFR: Initializing plugin")
  60. self.socket = socket.bind("192.168.15.244", "17144")
  61. self.socket:settimeout(0.02) -- 20ms
  62. print("FFR: Socket was bound")
  63. end
  64. function Hello:_schedule()
  65. print("Scheduling")
  66. self:checkForConnection()
  67. UIManager:scheduleIn(4, self.task)
  68. end
  69. function Hello:init()
  70. print("Initializing")
  71. self.tmppath = "/mnt/us/documents/test.html"
  72. self.task = function()
  73. self:_schedule()
  74. end
  75. --table.insert(PluginShare.backgroundJobs, {
  76. -- when = 2,
  77. -- repeated = true,
  78. -- executable = function()
  79. -- print("hello")
  80. -- end,
  81. -- callback = function()
  82. -- print("Job finished!")
  83. -- end,
  84. --})
  85. local Event = require("ui/event")
  86. --UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated"))
  87. --print("scheduled")
  88. self:onDispatcherRegisterActions()
  89. self.ui.menu:registerToMainMenu(self)
  90. self:bindSocket()
  91. self:_schedule()
  92. end
  93. function Hello:addToMainMenu(menu_items)
  94. menu_items.hello_world = {
  95. text = _("Hello World"),
  96. -- in which menu this should be appended
  97. sorting_hint = "more_tools",
  98. -- a callback when tapping
  99. callback = function()
  100. UIManager:show(InfoMessage:new{
  101. text = _("Hello, plugin world"),
  102. })
  103. end,
  104. }
  105. end
  106. function Hello:onHelloWorld()
  107. local popup = InfoMessage:new{
  108. text = _("Hello World"),
  109. }
  110. UIManager:show(popup)
  111. end
  112. return Hello