ArchiveInstaller.qml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if (line.match('error')) {
  40. ui.log(line)
  41. }
  42. }
  43. switch (mode) {
  44. case modeList:
  45. filesListCount += filesCount
  46. break
  47. case modeUnarchive:
  48. filesUnarchivedCount += filesCount
  49. if (filesListCount) {
  50. progress = {
  51. status: qsTr("Unpacking"),
  52. unarchived: filesUnarchivedCount,
  53. total: filesListCount,
  54. percent: filesUnarchivedCount * 100.0 / filesListCount
  55. }
  56. }
  57. break
  58. }
  59. }
  60. function onProcessFinished() {
  61. switch (process.mode) {
  62. case process.modeList:
  63. installArchive()
  64. break
  65. case process.modeUnarchive:
  66. removeOldFiles()
  67. break
  68. }
  69. }
  70. function listArchive() {
  71. mode = modeList
  72. filesListCount = 0
  73. process.start("unzip", ["-l", archive])
  74. }
  75. function installArchive() {
  76. FileIO.removeFile(deleteFiles)
  77. mode = modeUnarchive
  78. filesUnarchivedCount = 0
  79. process.start("unzip", ["-o", archive])
  80. }
  81. function removeOldFiles() {
  82. let files = FileIO.readTextFile(deleteFiles).split("\n")
  83. for (let file of files) {
  84. if (!file || file.startsWith("/") || file.indexOf("/../") !== -1) {
  85. continue
  86. }
  87. FileIO.removeFile(outputDir + "/" + file.trim())
  88. }
  89. FileIO.removeFile(deleteFiles)
  90. archiveInstaller.finished()
  91. }
  92. }
  93. function install(archive) {
  94. progress = {
  95. status: qsTr("Checking"),
  96. unarchived: null,
  97. total: null,
  98. percent: 0
  99. }
  100. process.archive = archive
  101. process.listArchive()
  102. }
  103. function cancel() {
  104. process.terminate()
  105. }
  106. }