' Gambas class file
PRIVATE hUpdateInfo AS File ' handler to read update-watch-info file
PRIVATE iExpHeightMin AS Integer ' hold minimum height of expander
PRIVATE iExpHeight AS Integer = 200 ' hold (previous) height of expander
PRIVATE cPackages AS NEW Collection ' store package information (multi dimensional)
PRIVATE iHeight AS Integer = Desktop.Scale * 5
PRIVATE idStat AS Integer = 0 ' cPagkages: Status (New/Upgrade/Remove/Duplicate)
PRIVATE idSumm AS Integer = 1 ' cPackages: Summary
PRIVATE idVer AS Integer = 2 ' cPackages: Version
PRIVATE idSize AS Integer = 3 ' cPackages: Size (Download)
PUBLIC SUB Form_Open()
DIM sLine AS String
DIM sList AS String
DIM sOutput AS String
DIM sPkgName AS String
DIM aLines AS String[]
DIM iPosi AS Integer
DIM iTotalSize AS Integer
' position window
ME.Center()
' adjust widget size according to it's font size
hbxTop.Height = tlbTitle.Font.Height(tlbTitle.Text) + 4
btnHelp.Width = btnHelp.Font.Width(btnHelp.Text) + 36
btnClose.Width = btnClose.Font.Width(btnClose.Text) + 36
btnRun.Width = btnRun.Font.Width(btnRun.Text) + 36
hbxBottom.Height = btnClose.Font.Height(btnClose.Text) + 16
cbxLater.Width = cbxLater.Font.Width(cbxLater.Text) + 18
hbxMiddle.Height = cbxLater.Font.Height(cbxLater.Text) + 4
tlbSize.Height = tlbSize.Font.Height(tlbSize.Text)
' resize list form
iExpHeightMin = expDetail.Font.Height(expDetail.Text) + 4
IF expDetail.Hidden = TRUE THEN
FList.Height = FList.Height - (expDetail.Height - iExpHeightMin)
ENDIF
' crete list contents
IF Stat(FMain.sPathInfo).Size > 0 THEN
' read update-watch-info file to create list of updates
hUpdateInfo = OPEN FMain.sPathInfo FOR READ
WHILE NOT Eof(hUpdateInfo)
LINE INPUT #hUpdateInfo, sLine
cPackages.Add([Split(sLine, ":")[0], "", "", ""], Split(sLine, ":")[1])
WEND
FOR EACH cPackages
sList = sList & cPackages.Key & " "
NEXT
SHELL "apt-cache show " & sList TO sOutput
aLines = Split(sOutput, "\n")
FOR EACH sLine IN aLines
iPosi = InStr(sLine, ": ")
IF iPosi > 0 THEN
SELECT CASE Split(sLine, ":")[0]
CASE "Package"
sPkgName = Right(sLine, Len(sLine) - (iPosi + 1))
CASE "Version"
cPackages[sPkgName][idVer] = Right(sLine, Len(sLine) - (iPosi + 1))
CASE "Size"
cPackages[sPkgName][idSize] = Right(sLine, Len(sLine) - (iPosi + 1))
iTotalSize = iTotalSize + Val(cPackages[sPkgName][idSize])
CASE "Summary"
cPackages[sPkgName][idSumm] = Right(sLine, Len(sLine) - (iPosi + 1))
END SELECT
ENDIF
NEXT
AddPkgs()
tlbSize.Text = "" & ("Total download size: ") & ConvUnit(Str(iTotalSize)) & ""
ENDIF
' workaround to set focus on close button
btnClose.SetFocus
END
PUBLIC SUB btnClose_Click()
ME.Close()
END
PUBLIC SUB btnHelp_Click()
FMain.hMenuHelp_Click()
END
PRIVATE FUNCTION TryDConv$(sOrig AS String) AS String
' If charset is not UTF-8, then convert it to UTF-8
TRY Conv$(sOrig, "EUC-JP", "UTF-8")
IF ERROR THEN
RETURN sOrig
ELSE
RETURN Conv$(sOrig, "EUC-JP", "UTF-8")
ENDIF
END
PUBLIC SUB expDetail_Hide()
iExpHeight = expDetail.Height
FList.Height = FList.Height - (expDetail.Height - iExpHeightMin)
END
PUBLIC SUB expDetail_Show()
FList.Height = FList.Height + (iExpHeight - iExpHeightMin)
END
PUBLIC SUB cbxLater_Click()
IF cbxLater.Value = TRUE THEN
btnClose.Text = ("&Close")
btnClose.Picture = Stock["24/close"]
ELSE
btnClose.Text = ("&Upgrade")
btnClose.Picture = Stock["24/apply"]
ENDIF
btnClose.Width = btnClose.Font.Width(btnClose.Text) + 36
END
PUBLIC SUB AddPkgs()
DIM hBox AS HBox
DIM hCheckbox AS CheckBox
DIM hTextLabel AS TextLabel
DIM hPanel AS Panel
DIM aElement AS String[]
DIM iToggle AS Integer
FOR EACH aElement IN cPackages
' create HBox
hBox = NEW HBox(lctPkgs)
hBox.Padding = 1
hBox.Spacing = 4
hBox.Height = iHeight
hBox.Name = cPackages.Key
' toggle background color
INC iToggle
IF iToggle MOD 2 = 0 THEN
hBox.Background = Color.Background
ELSE
hBox.Background = Color.Default
ENDIF
'create panel (for space adjustment)
hPanel = NEW Panel(hBox)
HPanel.Width = 4
HPanel.Height = iHeight
' create checkbox
hCheckbox = NEW CheckBox(hBox) AS "CheckBoxes"
hCheckbox.Width = 20
hCheckbox.Height = 24
hCheckbox.Value = TRUE
' create TextLabel for application name, version and summary
hTextLabel = NEW TextLabel(hBox)
hTextLabel.Expand = TRUE
hTextLabel.Alignment = Align.Normal
hTextLabel.Text = "" & cPackages.Key & ""
hTextLabel.Text &= " - " & aElement[idVer] & "
"
hTextLabel.Text &= aElement[idSumm] & " "
' create TextLabel for size
hTextLabel = NEW TextLabel(hBox)
hTextLabel.Alignment = Align.BottomRight
hTextLabel.Text = "" & ("Size: ") & "
" & ConvUnit(aElement[idSize]) & ""
hTextLabel.Width = hTextLabel.Font.Width(("Size: "))
NEXT
END
FUNCTION ConvUnit(sSize AS String) AS String
DIM iConv AS Integer = 0
DIM fConv AS Float = 0
IF Val(sSize) < 1024 THEN
RETURN sSize & "Byte"
ELSE IF Val(sSize) < 1048576 THEN
iConv = Int(Val(sSize) / 1024)
RETURN Str(iConv) & "KB"
ELSE
fConv = Val(sSize) / 1024 / 1024
RETURN Str(Format(fConv, "####.#")) & "MB"
ENDIF
END