Advertisement
Guest User

Main.lua

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