summaryrefslogtreecommitdiff
path: root/dalos-isoviewer.lua
blob: e7f4b1e1fba5ecde6e9fbf569e39b9722175c626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
loadmodule "luacd"

local controlTree
local controlList
local controlListComplete
local currentDir
local ISOElements

dalosp.isoviewer = {
    completeSpace = function (self, text, size, alignment)
        local newtext = ""

        if alignment == "right" then
            for j = size, string.len(text) + 1, -1 do
                newtext = newtext .. " "
            end
            newtext = newtext .. text
        else
            newtext = text
            for j = size, string.len(text) + 1, -1 do
                newtext = newtext .. " "
            end
        end

        return newtext
    end,

    fillList = function (self, List, arg_dir)
        local arg_root
        local index = 1
        local currentDir = {}
        local cdutil = self.cdutil
        local FList = {}
        local root = cdutil:findpath(arg_dir)
        
        List.RemoveItem = nil

        for name, dir in root:iterate(cdutil) do
            if name ~= "." and name ~= ".." then
                local elem
                if dir:isdir() then
                    elem = {
                        Name = name,
                        Size = dir.Size,
                        Sector = dir.Sector,
                        TypeFile = "File Folder",
                        DateFile = string.format('%02d', dir.Day) .. "/" .. string.format('%02d', dir.Month) .. "/" .. dir.Year .. " " .. string.format('%02d', dir.Hour) .. ":" .. string.format('%02d', dir.Minute) .. ":" .. string.format('%02d', dir.Second),
                        Parent = arg_dir,
                    }
                else
                    elem = {
                        Name = name,
                        Size = dir.Size,
                        Sector = dir.Sector,
                        TypeFile = "File",
                        DateFile = string.format('%02d', dir.Day) .. "/" .. string.format('%02d', dir.Month) .. "/" .. dir.Year .. " " .. string.format('%02d', dir.Hour) .. ":" .. string.format('%02d', dir.Minute) .. ":" .. string.format('%02d', dir.Second),
                        Parent = arg_dir,
                    }
                end
                table.insert(currentDir, elem)
            end
        end

        table.sort(currentDir, function(a, b) if a.TypeFile == b.TypeFile then return a.Name < b.Name else return a.TypeFile > b.TypeFile end end)

        for i, entry in ipairs(currentDir) do
            List[i] = self:completeSpace(entry.Name, 20) .. "   " .. self:completeSpace(entry.Size, 9, "right") .. "   " .. self:completeSpace(entry.Sector, 6, "right") .. "   " .. self:completeSpace(entry.TypeFile, 15) .. "   " .. self:completeSpace(entry.DateFile, 19) .. "   "
            FList[i] = { full = entry.Parent .. entry.Name, type = entry.TypeFile }
        end
        
        List.FList = FList
    end,

    fillListComplete = function (self, List, ISOElements)
        local FList = {}

        List.RemoveItem = nil

        for i, entry in ipairs(ISOElements) do
            List[i] = self:completeSpace(entry.Name, 20) .. "   " .. self:completeSpace(entry.Sector, 6, "right") .. "   " .. self:completeSpace(entry.Size, 9, "right") .. "   " .. self:completeSpace(math.ceil(entry.Size / 2048), 9, "right") .. "   " .. self:completeSpace(entry.TypeFile, 15)
            FList[i] = { full = entry.Parent .. entry.Name, type = entry.TypeFile }
        end
        
        List.FList = FList
    end,

    getISOElements = function (self)
        local ISOElements = {}
        local cdutil = self.cdutil

        local function list_tree(arg_root)
            local root = cdutil:findpath(arg_root)
            local tree = {}

            for name, dir in root:iterate(cdutil) do
                if name ~= "." and name ~= ".." then
                    local elem = {
                        Name = name,
                        Size = dir.Size,
                        Sector = dir.Sector,
                        DateFile = string.format('%02d', dir.Day) .. "/" .. string.format('%02d', dir.Month) .. "/" .. dir.Year .. " " .. string.format('%02d', dir.Hour) .. ":" .. string.format('%02d', dir.Minute) .. ":" .. string.format('%02d', dir.Second),
                        Parent = arg_root,
                    }
                    if dir:isdir() then
                        elem.TypeFile = "File Folder"

                        local subtree = list_tree(arg_root .. name .. "/")
                        subtree.branchname = name
                        table.insert(tree, subtree)
                    else
                        elem.TypeFile = "File"
                    end
                    table.insert(ISOElements, elem)
                end
            end
            
            return tree
        end
        
        tree = list_tree "/"

        table.sort(ISOElements, function(a, b) return a.Sector < b.Sector end)

        return tree, ISOElements
    end,
    
    input_change = function (self, ind)
        local h = self:get_linked_input(ind)
        
        iup.TreeSetValue(self.controlTree, {})
        self.controlList.RemoveItem = nil
        self.controlListComplete.RemoveItem = nil
        self:set_houtput(nil)
        
        if h then
            local cdutil = cdutils(h)
            self.cdutil = cdutil
            local pvd = createpvd(cdutil)
            local volid = pvd.volid
            pvd:destroy()

            local treeElements, ISOElements = self:getISOElements()
            iup.TreeSetValue(self.controlTree, treeElements)
            self.controlTree.name = volid
            
            self:fillList(self.controlList, "/")            
            self:fillListComplete(self.controlListComplete, ISOElements)
        end
    end,
    
    create = function (d, tab, settings)
        tab.ninputs = 1
        tab.noutputs = 1
        tab.otype = dalos.objtype.LUA_VIEWER
        tab.activate = function (self) self.dlg:show() end
        tab.configure = dalosp.isoviewer.configure
        tab.input_change = dalosp.isoviewer.input_change
        tab.default_name = "Isoviewer"
        tab.ntype = "Isoviewer"
        tab.get_settings = dalosp.isoviewer.get_settings
        local extra = {}
        
        local obj = dalos.object(d, tab, extra)
        
        local function create_tree(volid)
            local control_tree = iup.tree {
                EXPAND      = "VERTICAL",
                RASTERSIZE  = "200x300",
                EXPANDALL   = "NO",
                ADDEXPANDED = "NO",
            }

            control_tree.name = volid

            return control_tree
        end

        local function create_list(size)
            local List = iup.list {
                VALUE  = 4,
                SIZE   = size,
                EXPAND = "HORIZONTAL",
                FONT   = "Courier New",
            }

            return List
        end

        local function createHeader(name, size)
            return iup.text {
                VALUE     = name,
                SIZE      = size,
                READONLY  = "YES",
                BGCOLOR   = "201 201 201",
                ALIGNMENT = "ACENTER"
            }
        end
        
        local controlTree, controlList, controlListComplete

        controlTree         = create_tree("Root")
        controlList         = create_list("385x270")
        controlListComplete = create_list("100x270")
        controlList.MULTIPLE = "YES"

        function controlList:dblclick_cb(pos, text)
--[[
            if currentDir[pos].TypeFile == "File Folder" then
                obj:fillList(self.controlList, currentDir[pos].Parent .. currentDir[pos].Name .. "/")
            end
]]--
        end

        function controlTree:selection_cb(id, status)
            if status == 0 then return end

            local val = id
            local parent = {}
            local dir = ""
            while tonumber(val) > 0 do
                table.insert(parent, controlTree["TITLE" .. val] .. "/")

                val = controlTree["parent" .. val]
            end
            table.insert(parent, "/")

            for i = table.getn(parent), 1, -1 do
                dir = dir .. parent[i]
            end

            obj:fillList(controlList, dir)

            return iup.DEFAULT
        end
        
        local listAction = function (self, text, pos, state)
            if state ~= 1 then return iup.DEFAULT end
            local file = self.FList[pos]
            if file.type == "File" then
                local cdutil = obj.cdutil
                local dir = cdutil:findpath(file.full)
                obj:set_houtput(cdutil:cdfile(dir))
            else
                obj:set_houtput(nil)
            end
            return iup.DEFAULT
        end

        controlList.action = listAction
        controlListComplete.action = listAction

        local dlg = iup.dialog {
            TITLE  = "Lua-ISOViewer",
            MARGIN = "5x5",
            iup.hbox {
                controlTree,
                iup.fill { SIZE = "5x5" },
                iup.tabs {
                    SIZE = "200x300";
                    iup.vbox {
                        MARGIN = 0,
                        TABTITLE = "Liste";
                        iup.hbox {
                            createHeader("Name"     , 123),
                            createHeader("Size"     , 67),
                            createHeader("Sector"   , 50),
                            createHeader("Type"     , 101),
                            createHeader("Date Time", 123),
                            createHeader("XA flags" , 50),
                        },
                        controlList,
                    },
                    iup.vbox {
                        MARGIN = 0,
                        TABTITLE = "Liste complète";
                        iup.hbox {
                            createHeader("Name"     , 123),
                            createHeader("Sector"   , 50),
                            createHeader("Size"     , 67),
                            createHeader("Nb Sector", 67),
                            createHeader("Type"     , 101),
                        },
                        controlListComplete,
                    },
                },
            },
        }
        
        obj.dlg = dlg
        
        obj.completeSpace = dalosp.isoviewer.completeSpace
        obj.fillList = dalosp.isoviewer.fillList
        obj.fillListComplete = dalosp.isoviewer.fillListComplete
        obj.getISOElements = dalosp.isoviewer.getISOElements
        
        obj.controlTree = controlTree
        obj.controlList = controlList
        obj.controlListComplete = controlListComplete
        
        return obj
    end,
}

dalos.isoviewer = dalosp.isoviewer .create
dalos:register_obj("Isoviewer", dalos.isoviewer , "Basic Viewers")