2021-01-12 23:11:00 +11:00
-- Copyright (C) 2018-2021 by KittenOS NEO contributors
--
-- Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
-- THIS SOFTWARE.
2018-03-19 10:10:54 +11:00
-- Used by filedialog to provide a sane relative environment.
-- Essentially, the filedialog is just a 'thin' UI wrapper over this.
-- Returns the root node.
local function dialog ( name , parent )
return {
name = name ,
list = function ( ) return { { " Back " , function ( ) return nil , parent end } } end ,
unknownAvailable = false ,
selectUnknown = function ( text ) end
}
end
local getFsNode , getRoot
local setupCopyNode
2018-12-24 10:51:30 +11:00
function setupCopyNode ( parent , myRoot , op , complete )
2018-03-19 10:10:54 +11:00
local function handleResult ( aRes , res )
if aRes then
2018-04-24 11:24:10 +10:00
return complete ( res , true )
2018-03-19 10:10:54 +11:00
else
2018-12-24 10:51:30 +11:00
return nil , setupCopyNode ( parent , res , op , complete )
2018-03-19 10:10:54 +11:00
end
end
return {
name = " ( " .. op .. " ) " .. myRoot.name ,
list = function ( )
local l = { }
table.insert ( l , { " Cancel Operation: " .. op , function ( )
2018-04-24 11:24:10 +10:00
complete ( nil , false )
2018-03-19 10:10:54 +11:00
return false , parent
end } )
for _ , v in ipairs ( myRoot.list ( ) ) do
table.insert ( l , { v [ 1 ] , function ( )
return handleResult ( v [ 2 ] ( ) )
end } )
end
return l
end ,
unknownAvailable = myRoot.unknownAvailable ,
selectUnknown = function ( tx )
return handleResult ( myRoot.selectUnknown ( tx ) )
end
}
end
2018-04-24 11:24:10 +10:00
local function setupCopyVirtualEnvironment ( fs , parent , fwrap , impliedName )
2018-03-19 10:10:54 +11:00
if not fwrap then
return false , dialog ( " Could not open source " , parent )
end
2018-12-24 10:51:30 +11:00
local myRoot = getRoot ( fs , true , impliedName )
2018-03-19 10:10:54 +11:00
-- Setup wrapping node
2018-04-24 11:24:10 +10:00
return setupCopyNode ( parent , myRoot , " Copy " , function ( fwrap2 , intent )
2018-03-19 10:10:54 +11:00
if not fwrap2 then
2018-04-24 11:24:10 +10:00
fwrap.close ( )
if intent then
return false , dialog ( " Could not open dest. " , parent )
end
return false , parent
2018-03-19 10:10:54 +11:00
end
local data = fwrap.read ( neo.readBufSize )
while data do
fwrap2.write ( data )
data = fwrap.read ( neo.readBufSize )
end
fwrap.close ( )
fwrap2.close ( )
return false , dialog ( " Completed copy. " , parent )
2018-12-24 10:51:30 +11:00
end )
2018-03-19 10:10:54 +11:00
end
2018-12-24 10:51:30 +11:00
function getFsNode ( fs , parent , fsc , path , mode , impliedName )
2018-03-19 10:10:54 +11:00
local va = fsc.address : sub ( 1 , 4 )
2018-04-24 11:24:10 +10:00
local fscrw = not fsc.isReadOnly ( )
2018-04-27 01:13:42 +10:00
local dir = path : sub ( # path , # path ) == " / "
local confirmedDel = false
local t
2018-12-24 10:51:30 +11:00
local function selectUnknown ( text )
-- Relies on text being nil if used in leaf node
local rt , re = require ( " sys-filewrap " ) . create ( fsc , path .. ( text or " " ) , mode )
if not rt then
return false , dialog ( " Open Error: " .. tostring ( re ) , parent )
end
return true , rt
end
2018-04-27 01:13:42 +10:00
t = {
name = ( ( dir and " DIR: " ) or " FILE: " ) .. va .. path ,
list = function ( )
local n = { }
n [ 1 ] = { " .. " , function ( )
return nil , parent
end }
if dir then
2018-03-19 10:10:54 +11:00
for k , v in ipairs ( fsc.list ( path ) ) do
2018-04-26 07:57:25 +10:00
local nm = " F: " .. v
2018-03-19 10:10:54 +11:00
local fp = path .. v
2020-04-01 04:10:55 +11:00
local cDir = fsc.isDirectory ( fp )
if cDir then
2018-04-26 07:57:25 +10:00
nm = " D: " .. v
2018-03-19 10:10:54 +11:00
end
2020-04-01 04:10:55 +11:00
if ( not cDir ) and ( fscrw or mode == false ) and ( mode ~= nil ) then
local vn = v
n [ k + 1 ] = { nm , function ( ) return selectUnknown ( vn ) end }
else
n [ k + 1 ] = { nm , function ( ) return nil , getFsNode ( fs , t , fsc , fp , mode , impliedName ) end }
end
2018-03-19 10:10:54 +11:00
end
2020-04-01 04:10:55 +11:00
else
table.insert ( n , { " Copy " , function ( )
local rt , re = require ( " sys-filewrap " ) . create ( fsc , path , false )
if not rt then
return false , dialog ( " Open Error: " .. tostring ( re ) , parent )
end
return nil , setupCopyVirtualEnvironment ( fs , parent , rt , path : match ( " [^/]*$ " ) or " " )
end } )
2018-04-27 01:13:42 +10:00
end
if fscrw then
if dir then
2018-04-24 11:24:10 +10:00
table.insert ( n , { " Mk. Directory " , function ( )
return nil , {
name = " MKDIR... " ,
2018-04-27 01:13:42 +10:00
list = function ( ) return { {
" Cancel " , function ( )
return false , t
end
} } end ,
2018-04-24 11:24:10 +10:00
unknownAvailable = true ,
selectUnknown = function ( text )
fsc.makeDirectory ( path .. text )
return nil , dialog ( " Done! " , t )
end
}
2018-03-21 01:18:59 +11:00
end } )
2018-04-24 11:24:10 +10:00
end
2018-04-27 01:13:42 +10:00
if path ~= " / " then
local delText = " Delete "
if confirmedDel then
delText = " Delete <ARMED> "
end
table.insert ( n , { delText , function ( )
if not confirmedDel then
confirmedDel = true
return nil , t
end
fsc.remove ( path )
return nil , dialog ( " Done. " , parent )
end } )
else
table.insert ( n , { " Relabel Disk " , function ( )
return nil , {
name = " Disk Relabel... " ,
list = function ( ) return { {
fsc.getLabel ( ) or " Cancel " ,
function ( )
return false , t
end
} } end ,
unknownAvailable = true ,
selectUnknown = function ( tx )
fsc.setLabel ( tx )
return false , t
end
}
end } )
2018-03-28 00:40:05 +11:00
end
2018-04-24 11:24:10 +10:00
end
2018-12-24 10:51:30 +11:00
if not dir then
elseif impliedName then
table.insert ( n , { " Implied: " .. impliedName , function ( )
return selectUnknown ( impliedName )
end } )
2018-12-12 03:09:54 +11:00
end
2018-03-19 10:10:54 +11:00
return n
end ,
2018-04-27 01:13:42 +10:00
unknownAvailable = dir and ( mode ~= nil ) and ( ( mode == false ) or fscrw ) ,
2018-12-24 10:51:30 +11:00
selectUnknown = selectUnknown
2018-03-19 10:10:54 +11:00
}
2018-04-27 01:13:42 +10:00
return t
2018-03-19 10:10:54 +11:00
end
2018-12-24 10:51:30 +11:00
function getRoot ( fs , mode , defName )
2018-03-19 10:10:54 +11:00
local t
t = {
name = " DRVS: " ,
list = function ( )
local l = { }
for fsi in fs.list ( ) do
local id = fsi.getLabel ( )
if fsi == fs.primary then
2018-04-24 11:24:10 +10:00
id = " NEO " .. ( ( id and ( " : " .. id ) ) or " Disk " )
2018-03-19 10:10:54 +11:00
elseif fsi == fs.temporary then
2018-04-24 11:24:10 +10:00
id = " RAM " .. ( ( id and ( " " .. id ) ) or " Disk " )
else
id = id or " Disk "
2018-03-19 10:10:54 +11:00
end
local used , total = fsi.spaceUsed ( ) , fsi.spaceTotal ( )
local amount = string.format ( " %02i " , math.ceil ( ( used / total ) * 100 ) )
local mb = math.floor ( total / ( 1024 * 1024 ) )
if fsi.isReadOnly ( ) then
2018-04-24 11:24:10 +10:00
id = " RO " .. amount .. " % " .. mb .. " M " .. id
2018-03-19 10:10:54 +11:00
else
2018-04-24 11:24:10 +10:00
id = " RW " .. amount .. " % " .. mb .. " M " .. id
2018-03-19 10:10:54 +11:00
end
2018-04-24 11:24:10 +10:00
table.insert ( l , { fsi.address : sub ( 1 , 4 ) .. " " .. id , function ( )
2018-12-24 10:51:30 +11:00
return nil , getFsNode ( fs , t , fsi , " / " , mode , defName )
2018-03-19 10:10:54 +11:00
end } )
end
return l
end ,
unknownAvailable = false ,
2018-04-24 11:24:10 +10:00
selectUnknown = function ( text )
return false , dialog ( " Ow, that hurt... " , t )
end
2018-03-19 10:10:54 +11:00
}
return t
end
return getRoot