ArchiveInstaller.qml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import QtQuick 2.0
  2. import FileIO 1.0
  3. import Process 1.0
  4. Item {
  5. id: archiveInstaller
  6. signal finished()
  7. signal error()
  8. property string outputDir
  9. readonly property alias isRunning: process.isRunning
  10. property var progress
  11. readonly property string deleteFiles: outputDir + "/deletefiles.txt"
  12. Process {
  13. id: process
  14. property string archive
  15. property string lastOutput
  16. property int filesListCount: 0
  17. property int filesUnarchivedCount: 0
  18. property int mode: modeIdle
  19. readonly property int modeIdle: 0
  20. readonly property int modeList: 1
  21. readonly property int modeUnarchive: 2
  22. workingDirectory: outputDir
  23. onFinished: {
  24. console.log("unzip exit code: " + code)
  25. if (code === 0) {
  26. onProcessFinished()
  27. } else {
  28. ui.log(lastOutput)
  29. archiveInstaller.error()
  30. }
  31. }
  32. onOutputRead: {
  33. lastOutput = output
  34. let filesCount = 0
  35. for (let line of output.split("\n")) {
  36. if (line.match('/')) {
  37. ++filesCount
  38. }
  39. }
  40. switch (mode) {
  41. case modeList:
  42. filesListCount += filesCount
  43. break
  44. case modeUnarchive:
  45. filesUnarchivedCount += filesCount
  46. if (filesListCount) {
  47. progress = {
  48. status: qsTr("Unpacking"),
  49. unarchived: filesUnarchivedCount,
  50. total: filesListCount,
  51. percent: filesUnarchivedCount * 100.0 / filesListCount
  52. }
  53. }
  54. break
  55. }
  56. }
  57. function onProcessFinished() {
  58. switch (process.mode) {
  59. case process.modeList:
  60. installArchive()
  61. break
  62. case process.modeUnarchive:
  63. removeOldFiles()
  64. break
  65. }
  66. }
  67. function listArchive() {
  68. mode = modeList
  69. filesListCount = 0
  70. process.start("unzip", ["-l", archive])
  71. }
  72. function installArchive() {
  73. FileIO.removeFile(deleteFiles)
  74. mode = modeUnarchive
  75. filesUnarchivedCount = 0
  76. process.start("unzip", ["-o", archive])
  77. }
  78. function removeOldFiles() {
  79. let files = FileIO.readTextFile(deleteFiles).split("\n")
  80. for (let file of files) {
  81. if (!file || file.startsWith("/") || file.indexOf("/../") !== -1) {
  82. continue
  83. }
  84. FileIO.removeFile(outputDir + "/" + file.trim())
  85. }
  86. FileIO.removeFile(deleteFiles)
  87. archiveInstaller.finished()
  88. }
  89. }
  90. function install(archive) {
  91. progress = {
  92. status: qsTr("Checking"),
  93. unarchived: null,
  94. total: null,
  95. percent: 0
  96. }
  97. process.archive = archive
  98. process.listArchive()
  99. }
  100. function cancel() {
  101. process.terminate()
  102. }
  103. }