Advertisement
Guest User

Main.lua

a guest
May 29th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.19 KB | None | 0 0
  1.  
  2. local GUI = require("GUI")
  3. local buffer = require("doubleBuffering")
  4. local computer = require("computer")
  5. local fs = require("filesystem")
  6. local event = require("event")
  7. local MineOSPaths = require("MineOSPaths")
  8. local MineOSCore = require("MineOSCore")
  9. local MineOSNetwork = require("MineOSNetwork")
  10. local MineOSInterface = require("MineOSInterface")
  11.  
  12. local args, options = require("shell").parse(...)
  13.  
  14. ------------------------------------------------------------------------------------------------------
  15.  
  16. local favourites = {
  17.     {name = "Root", path = "/"},
  18.     {name = "Desktop", path = MineOSPaths.desktop},
  19.     {name = "Applications", path = MineOSPaths.applications},
  20.     {name = "Pictures", path = MineOSPaths.pictures},
  21.     {name = "System", path = MineOSPaths.system},
  22.     {name = "Libraries", path = "/lib/"},
  23.     {name = "Trash", path = MineOSPaths.trash},
  24. }
  25. local resourcesPath = MineOSCore.getCurrentScriptDirectory()
  26. local favouritesPath = MineOSPaths.applicationData .. "Finder/Favourites3.cfg"
  27.  
  28. local sidebarFromY = 1
  29. local iconFieldYOffset = 2
  30. local scrollTimerID
  31.  
  32. local workpathHistory = {}
  33. local workpathHistoryCurrent = 0
  34.  
  35. ------------------------------------------------------------------------------------------------------
  36.  
  37. local mainContainer, window = MineOSInterface.addWindow(GUI.filledWindow(1, 1, 88, 26, 0xF0F0F0))
  38.  
  39. local titlePanel = window:addChild(GUI.panel(1, 1, 1, 3, 0xE1E1E1))
  40.  
  41. local prevButton = window:addChild(GUI.adaptiveRoundedButton(9, 2, 1, 0, 0xFFFFFF, 0x4B4B4B, 0x3C3C3C, 0xFFFFFF, "<"))
  42. prevButton.colors.disabled.background = prevButton.colors.default.background
  43. prevButton.colors.disabled.text = 0xC3C3C3
  44.  
  45. local nextButton = window:addChild(GUI.adaptiveRoundedButton(14, 2, 1, 0, 0xFFFFFF, 0x4B4B4B, 0x3C3C3C, 0xFFFFFF, ">"))
  46. nextButton.colors.disabled = prevButton.colors.disabled
  47.  
  48. local FTPButton = window:addChild(GUI.adaptiveRoundedButton(20, 2, 1, 0, 0xFFFFFF, 0x4B4B4B, 0x3C3C3C, 0xFFFFFF, MineOSCore.localization.networkFTPNewConnection))
  49. FTPButton.colors.disabled = prevButton.colors.disabled
  50. FTPButton.disabled = not MineOSNetwork.internetProxy
  51.  
  52. local sidebarContainer = window:addChild(GUI.container(1, 4, 20, 1))
  53. sidebarContainer.panel = sidebarContainer:addChild(GUI.panel(1, 1, sidebarContainer.width, 1, 0xFFFFFF, MineOSCore.properties.transparencyEnabled and 0.3))
  54. sidebarContainer.itemsContainer = sidebarContainer:addChild(GUI.container(1, 1, sidebarContainer.width, 1))
  55.  
  56. local searchInput = window:addChild(GUI.input(1, 2, 36, 1, 0xFFFFFF, 0x4B4B4B, 0xA5A5A5, 0xFFFFFF, 0x2D2D2D, nil, MineOSCore.localization.search, true))
  57.  
  58. local iconField = window:addChild(MineOSInterface.iconField(1, 4, 1, 1, 2, 2, 0x3C3C3C, 0x3C3C3C, MineOSPaths.desktop))
  59.  
  60. local scrollBar = window:addChild(GUI.scrollBar(1, 4, 1, 1, 0xC3C3C3, 0x4B4B4B, iconFieldYOffset, 1, 1, 1, 1, true))
  61. scrollBar.eventHandler = nil
  62.  
  63. local statusBar = window:addChild(GUI.object(1, 1, 1, 1))
  64.  
  65. statusBar.draw = function(object)
  66.     buffer.drawRectangle(object.x, object.y, object.width, object.height, 0xFFFFFF, 0x3C3C3C, " ")
  67.     buffer.drawText(object.x + 1, object.y, 0x3C3C3C, string.limit(("root/" .. iconField.workpath):gsub("/+$", ""):gsub("%/+", " ► "), object.width - 2, "left"))
  68. end
  69.  
  70. local sidebarResizer = window:addChild(GUI.resizer(1, 4, 3, 5, 0xFFFFFF, 0x0))
  71.  
  72. ------------------------------------------------------------------------------------------------------
  73.  
  74. local function saveFavourites()
  75.     table.toFile(favouritesPath, favourites)
  76. end
  77.  
  78. local function updateFileListAndDraw()
  79.     iconField:updateFileList()
  80.     MineOSInterface.mainContainer:drawOnScreen()
  81. end
  82.  
  83. local function workpathHistoryButtonsUpdate()
  84.     prevButton.disabled = workpathHistoryCurrent <= 1
  85.     nextButton.disabled = workpathHistoryCurrent >= #workpathHistory
  86. end
  87.  
  88. local function prevOrNextWorkpath(next)
  89.     if next then
  90.         if workpathHistoryCurrent < #workpathHistory then
  91.             workpathHistoryCurrent = workpathHistoryCurrent + 1
  92.         end
  93.     else
  94.         if workpathHistoryCurrent > 1 then
  95.             workpathHistoryCurrent = workpathHistoryCurrent - 1
  96.         end
  97.     end
  98.  
  99.     workpathHistoryButtonsUpdate()
  100.     iconField.yOffset = iconFieldYOffset
  101.     iconField:setWorkpath(workpathHistory[workpathHistoryCurrent])
  102.    
  103.     updateFileListAndDraw()
  104. end
  105.  
  106. local function addWorkpath(path)
  107.     workpathHistoryCurrent = workpathHistoryCurrent + 1
  108.     table.insert(workpathHistory, workpathHistoryCurrent, path)
  109.     for i = workpathHistoryCurrent + 1, #workpathHistory do
  110.         workpathHistory[i] = nil
  111.     end
  112.  
  113.     workpathHistoryButtonsUpdate()
  114.     searchInput.text = ""
  115.     iconField.yOffset = iconFieldYOffset
  116.     iconField:setWorkpath(path)
  117. end
  118.  
  119. local function newSidebarItem(y, textColor, text, path)
  120.     local object = sidebarContainer.itemsContainer:addChild(GUI.object(1, y, 1, 1))
  121.    
  122.     if text then
  123.         object.draw = function(object)
  124.             object.width = sidebarContainer.itemsContainer.width
  125.  
  126.             local currentTextColor = textColor
  127.             if path == iconField.workpath then
  128.                 buffer.drawRectangle(object.x, object.y, object.width, 1, 0x3366CC, 0xFFFFFF, " ")
  129.                 currentTextColor = 0xFFFFFF
  130.             end
  131.            
  132.             buffer.drawText(object.x + 1, object.y, currentTextColor, string.limit(text, object.width - 2, "center"))
  133.         end
  134.  
  135.         object.eventHandler = function(mainContainer, object, e1, ...)
  136.             if e1 == "touch" and object.onTouch then
  137.                 object.onTouch(e1, ...)
  138.             end
  139.         end
  140.     end
  141.  
  142.     return object
  143. end
  144.  
  145. local function onFavouriteTouch(path)
  146.     if fs.exists(path) then
  147.         addWorkpath(path)
  148.         updateFileListAndDraw()
  149.     else
  150.         GUI.alert("Path doesn't exists: " .. path)
  151.     end
  152. end
  153.  
  154. local openFTP, updateSidebar
  155.  
  156. openFTP = function(...)
  157.     local mountPath = MineOSNetwork.mountPaths.FTP .. MineOSNetwork.getFTPProxyName(...) .. "/"
  158.     local proxy, reason = MineOSNetwork.connectToFTP(...)
  159.     if proxy then
  160.         MineOSNetwork.umountFTPs()
  161.         fs.mount(proxy, mountPath)
  162.         addWorkpath(mountPath)
  163.         updateSidebar()
  164.         updateFileListAndDraw()
  165.     else
  166.         GUI.alert(reason)
  167.     end
  168. end
  169.  
  170. updateSidebar = function()
  171.     local y = sidebarFromY
  172.     sidebarContainer.itemsContainer:removeChildren()
  173.  
  174.     newSidebarItem(y, 0x3C3C3C, MineOSCore.localization.favourite)
  175.     y = y + 1
  176.     for i = 1, #favourites do
  177.         local object = newSidebarItem(y, 0x555555, " " .. fs.name(favourites[i].name), favourites[i].path)
  178.        
  179.         object.onTouch = function(e1, e2, e3, e4, e5)
  180.             if e5 == 1 then
  181.                 local menu = GUI.addContextMenu(mainContainer, e3, e4)
  182.                
  183.                 menu:addItem(MineOSCore.localization.removeFromFavourites).onTouch = function()
  184.                     table.remove(favourites, i)
  185.                     saveFavourites()
  186.                     updateSidebar()
  187.                     MineOSInterface.mainContainer:drawOnScreen()
  188.                 end
  189.  
  190.                 mainContainer:drawOnScreen()
  191.             else
  192.                 onFavouriteTouch(favourites[i].path)
  193.             end
  194.         end
  195.  
  196.         y = y + 1
  197.     end
  198.  
  199.     local added = false
  200.     for proxy, path in fs.mounts() do
  201.         if proxy.MineOSNetworkModem then
  202.             if not added then
  203.                 y = y + 1
  204.                 newSidebarItem(y, 0x3C3C3C, MineOSCore.localization.network)
  205.                 y, added = y + 1, true
  206.             end
  207.  
  208.             newSidebarItem(y, 0x555555, " " .. MineOSNetwork.getModemProxyName(proxy), path .. "/").onTouch = function()
  209.                 addWorkpath(path .. "/")
  210.                 updateFileListAndDraw()
  211.             end
  212.  
  213.             y = y + 1
  214.         end
  215.     end
  216.  
  217.     if MineOSNetwork.internetProxy and #MineOSCore.properties.FTPConnections > 0 then
  218.         y = y + 1
  219.         newSidebarItem(y, 0x3C3C3C, MineOSCore.localization.networkFTPConnections)
  220.         y = y + 1
  221.        
  222.         for i = 1, #MineOSCore.properties.FTPConnections do
  223.             local connection = MineOSCore.properties.FTPConnections[i]
  224.             local name = MineOSNetwork.getFTPProxyName(connection.address, connection.port, connection.user)
  225.             local mountPath = MineOSNetwork.mountPaths.FTP .. name .. "/"
  226.  
  227.             newSidebarItem(y, 0x555555, " " .. name, mountPath).onTouch = function(e1, e2, e3, e4, e5)
  228.                 if e5 == 1 then
  229.                     local menu = GUI.addContextMenu(mainContainer, e3, e4)
  230.                    
  231.                     menu:addItem(MineOSCore.localization.delete).onTouch = function()
  232.                         table.remove(MineOSCore.properties.FTPConnections, i)
  233.                         MineOSCore.saveProperties()
  234.                         updateSidebar()
  235.                         MineOSInterface.mainContainer:drawOnScreen()
  236.                     end
  237.  
  238.                     mainContainer:drawOnScreen()
  239.                 else
  240.                     openFTP(connection.address, connection.port, connection.user, connection.password)
  241.                 end
  242.             end
  243.  
  244.             y = y + 1
  245.         end
  246.     end
  247.  
  248.     y = y + 1
  249.     newSidebarItem(y, 0x3C3C3C, MineOSCore.localization.mounts)
  250.     y = y + 1
  251.     for proxy, path in fs.mounts() do
  252.         if path ~= "/" and not proxy.MineOSNetworkModem and not proxy.MineOSNetworkFTP then
  253.             newSidebarItem(y, 0x555555, " " .. (proxy.getLabel() or fs.name(path)), path .. "/").onTouch = function()
  254.                 onFavouriteTouch(path .. "/")
  255.             end
  256.  
  257.             y = y + 1
  258.         end
  259.     end
  260. end
  261.  
  262. sidebarContainer.itemsContainer.eventHandler = function(mainContainer, object, e1, e2, e3, e4, e5)
  263.     if e1 == "scroll" then
  264.         if (e5 > 0 and sidebarFromY < 1) or (e5 < 0 and sidebarContainer.itemsContainer.children[#sidebarContainer.itemsContainer.children].localY > 1) then
  265.             sidebarFromY = sidebarFromY + e5
  266.             updateSidebar()
  267.             MineOSInterface.mainContainer:drawOnScreen()
  268.         end
  269.     end
  270. end
  271.  
  272. local function updateScrollBar()
  273.     local shownFilesCount = #iconField.fileList - iconField.fromFile + 1
  274.    
  275.     local horizontalLines = math.ceil(shownFilesCount / iconField.iconCount.horizontal)
  276.     local minimumOffset = 3 - (horizontalLines - 1) * (MineOSCore.properties.iconHeight + MineOSCore.properties.iconVerticalSpaceBetween) - MineOSCore.properties.iconVerticalSpaceBetween
  277.    
  278.     if iconField.yOffset > iconFieldYOffset then
  279.         iconField.yOffset = iconFieldYOffset
  280.     elseif iconField.yOffset < minimumOffset then
  281.         iconField.yOffset = minimumOffset
  282.     end
  283.  
  284.     if shownFilesCount > iconField.iconCount.total then
  285.         scrollBar.hidden = false
  286.         scrollBar.maximumValue = math.abs(minimumOffset)
  287.         scrollBar.value = math.abs(iconField.yOffset - iconFieldYOffset)
  288.     else
  289.         scrollBar.hidden = true
  290.     end
  291. end
  292.  
  293. searchInput.onInputFinished = function()
  294.     iconField.filenameMatcher = searchInput.text
  295.     iconField.fromFile = 1
  296.     iconField.yOffset = iconFieldYOffset
  297.  
  298.     updateFileListAndDraw()
  299. end
  300.  
  301. nextButton.onTouch = function()
  302.     prevOrNextWorkpath(true)
  303. end
  304.  
  305. prevButton.onTouch = function()
  306.     prevOrNextWorkpath(false)
  307. end
  308.  
  309. FTPButton.onTouch = function()
  310.     local container = MineOSInterface.addBackgroundContainer(MineOSInterface.mainContainer, MineOSCore.localization.networkFTPNewConnection)
  311.  
  312.     local ad, po, us, pa, la = "ftp.example.com", "21", "root", "1234"
  313.     if #MineOSCore.properties.FTPConnections > 0 then
  314.         local la = MineOSCore.properties.FTPConnections[#MineOSCore.properties.FTPConnections]
  315.         ad, po, us, pa = la.address, tostring(la.port), la.user, la.password
  316.     end
  317.  
  318.     local addressInput = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xE1E1E1, 0x696969, 0x696969, 0xE1E1E1, 0x2D2D2D, ad, MineOSCore.localization.networkFTPAddress, true))
  319.     local portInput = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xE1E1E1, 0x696969, 0x696969, 0xE1E1E1, 0x2D2D2D, po, MineOSCore.localization.networkFTPPort, true))
  320.     local userInput = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xE1E1E1, 0x696969, 0x696969, 0xE1E1E1, 0x2D2D2D, us, MineOSCore.localization.networkFTPUser, true))
  321.     local passwordInput = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xE1E1E1, 0x696969, 0x696969, 0xE1E1E1, 0x2D2D2D, pa, MineOSCore.localization.networkFTPPassword, true, "*"))
  322.     container.layout:addChild(GUI.button(1, 1, 36, 3, 0xA5A5A5, 0xFFFFFF, 0x2D2D2D, 0xE1E1E1, "OK")).onTouch = function()
  323.         container:remove()
  324.  
  325.         local port = tonumber(portInput.text)
  326.         if port then
  327.             local found = false
  328.             for i = 1, #MineOSCore.properties.FTPConnections do
  329.                 if
  330.                     MineOSCore.properties.FTPConnections[i].address == addressInput.text and
  331.                     MineOSCore.properties.FTPConnections[i].port == port and
  332.                     MineOSCore.properties.FTPConnections[i].user == userInput.text and
  333.                     MineOSCore.properties.FTPConnections[i].password == passwordInput.text
  334.                 then
  335.                     found = true
  336.                     break
  337.                 end
  338.             end
  339.  
  340.             if not found then
  341.                 table.insert(MineOSCore.properties.FTPConnections, {
  342.                     address = addressInput.text,
  343.                     port = port,
  344.                     user = userInput.text,
  345.                     password = passwordInput.text
  346.                 })
  347.                 MineOSCore.saveProperties()
  348.  
  349.                 updateSidebar()
  350.                 MineOSInterface.mainContainer:drawOnScreen()
  351.  
  352.                 openFTP(addressInput.text, port, userInput.text, passwordInput.text)
  353.             end
  354.         end
  355.     end
  356.  
  357.     MineOSInterface.mainContainer:drawOnScreen()
  358. end
  359.  
  360. statusBar.eventHandler = function(mainContainer, object, e1, e2)
  361.     if e1 == "component_added" or e1 == "component_removed" then
  362.         FTPButton.disabled = not MineOSNetwork.internetProxy
  363.         updateSidebar()
  364.         MineOSInterface.mainContainer:drawOnScreen()
  365.     elseif e1 == "MineOSNetwork" then
  366.         if e2 == "updateProxyList" or e2 == "timeout" then
  367.             updateSidebar()
  368.             MineOSInterface.mainContainer:drawOnScreen()
  369.         end
  370.     end
  371. end
  372.  
  373. iconField.eventHandler = function(mainContainer, object, e1, e2, e3, e4, e5)
  374.     if e1 == "scroll" then
  375.         iconField.yOffset = iconField.yOffset + e5 * 2
  376.  
  377.         updateScrollBar()
  378.  
  379.         local delta = iconField.yOffset - iconField.iconsContainer.children[1].localY
  380.         for i = 1, #iconField.iconsContainer.children do
  381.             iconField.iconsContainer.children[i].localY = iconField.iconsContainer.children[i].localY + delta
  382.         end
  383.  
  384.         MineOSInterface.mainContainer:drawOnScreen()
  385.  
  386.         if scrollTimerID then
  387.             event.cancel(scrollTimerID)
  388.             scrollTimerID = nil
  389.         end
  390.  
  391.         scrollTimerID = event.timer(0.3, function()
  392.             computer.pushSignal("Finder", "updateFileList")
  393.         end, 1)
  394.     elseif e1 == "MineOSCore" or e1 == "Finder" then
  395.         if e2 == "updateFileList" then
  396.             if e1 == "MineOSCore" then
  397.                 iconField.yOffset = iconFieldYOffset
  398.             end
  399.             updateFileListAndDraw()
  400.         elseif e2 == "updateFavourites" then
  401.             if e3 then
  402.                 table.insert(favourites, e3)
  403.             end
  404.             saveFavourites()
  405.             updateSidebar()
  406.             MineOSInterface.mainContainer:drawOnScreen()
  407.         end
  408.     end
  409. end
  410.  
  411. iconField.launchers.directory = function(icon)
  412.     addWorkpath(icon.path)
  413.     updateFileListAndDraw()
  414. end
  415.  
  416. iconField.launchers.showPackageContent = function(icon)
  417.     addWorkpath(icon.path)
  418.     updateFileListAndDraw()
  419. end
  420.  
  421. iconField.launchers.showContainingFolder = function(icon)
  422.     addWorkpath(fs.path(MineOSCore.readShortcut(icon.path)))
  423.     updateFileListAndDraw()
  424. end
  425.  
  426. local overrideUpdateFileList = iconField.updateFileList
  427. iconField.updateFileList = function(...)
  428.     mainContainer:drawOnScreen()
  429.     overrideUpdateFileList(...)
  430.     updateScrollBar()
  431. end
  432.  
  433. local function calculateSizes(width, height)
  434.     sidebarContainer.height = height - 3
  435.    
  436.     sidebarContainer.panel.width = sidebarContainer.width
  437.     sidebarContainer.panel.height = sidebarContainer.height
  438.    
  439.     sidebarContainer.itemsContainer.width = sidebarContainer.width
  440.     sidebarContainer.itemsContainer.height = sidebarContainer.height
  441.  
  442.     sidebarResizer.localX = sidebarContainer.width - 1
  443.     sidebarResizer.localY = math.floor(sidebarContainer.localY + sidebarContainer.height / 2 - sidebarResizer.height / 2 - 1)
  444.  
  445.     window.backgroundPanel.width = width - sidebarContainer.width
  446.     window.backgroundPanel.height = height - 4
  447.     window.backgroundPanel.localX = sidebarContainer.width + 1
  448.     window.backgroundPanel.localY = 4
  449.  
  450.     statusBar.localX = sidebarContainer.width + 1
  451.     statusBar.localY = height
  452.     statusBar.width = window.backgroundPanel.width
  453.  
  454.     titlePanel.width = width
  455.     searchInput.width = math.floor(width * 0.25)
  456.     searchInput.localX = width - searchInput.width - 1
  457.  
  458.     iconField.width = window.backgroundPanel.width
  459.     iconField.height = height + 4
  460.     iconField.localX = window.backgroundPanel.localX
  461.  
  462.     scrollBar.localX = window.width
  463.     scrollBar.height = window.backgroundPanel.height
  464.     scrollBar.shownValueCount = scrollBar.height - 1
  465.    
  466.     window.actionButtons:moveToFront()
  467. end
  468.  
  469. window.onResize = function(width, height)
  470.     calculateSizes(width, height)
  471.     MineOSInterface.mainContainer:drawOnScreen()
  472.     updateFileListAndDraw()
  473. end
  474.  
  475. sidebarResizer.onResize = function(dragWidth, dragHeight)
  476.     sidebarContainer.width = sidebarContainer.width + dragWidth
  477.     sidebarContainer.width = sidebarContainer.width >= 5 and sidebarContainer.width or 5
  478.     calculateSizes(window.width, window.height)
  479. end
  480.  
  481. sidebarResizer.onResizeFinished = function()
  482.     updateFileListAndDraw()
  483. end
  484.  
  485. local overrideMaximize = window.actionButtons.maximize.onTouch
  486. window.actionButtons.maximize.onTouch = function()
  487.     iconField.yOffset = iconFieldYOffset
  488.     overrideMaximize()
  489. end
  490.  
  491. window.actionButtons.close.onTouch = function()
  492.     window:close()
  493. end
  494.  
  495. ------------------------------------------------------------------------------------------------------
  496.  
  497. if fs.exists(favouritesPath) then
  498.     favourites = table.fromFile(favouritesPath)
  499. else
  500.     saveFavourites()
  501. end
  502.  
  503. if (options.o or options.open) and args[1] and fs.isDirectory(args[1]) then
  504.     addWorkpath(args[1])
  505. else
  506.     addWorkpath("/")
  507. end
  508.  
  509. updateSidebar()
  510. window:resize(window.width, window.height)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement