SelfUpdate.qml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import QtQuick 2.0
  2. import QtQuick.Controls 2.15
  3. Item {
  4. property string updateMode
  5. property string webVersion
  6. property string link
  7. property string description
  8. Script {
  9. id: script
  10. property int mode: modeNone
  11. readonly property int modeNone: 0
  12. readonly property int modeCheckSelfUpdate: 1
  13. readonly property int modePerformSelfUpdate: 2
  14. function checkSelfUpdate() {
  15. ui.log("")
  16. ui.log(qsTr("Checking %APPNAME% for updates...").replace("%APPNAME%", appName))
  17. mode = modeCheckSelfUpdate
  18. execScript('selfupdate', 'check')
  19. }
  20. function performSelfUpdate() {
  21. ui.log("")
  22. ui.log(qsTr("Downloading and updating %APPNAME%...").replace("%APPNAME%", appName))
  23. mode = modePerformSelfUpdate
  24. execScript('selfupdate', ['update', link])
  25. }
  26. onFinished: {
  27. switch (mode) {
  28. case modeCheckSelfUpdate:
  29. onCheckFinished(code, output)
  30. break
  31. case modePerformSelfUpdate:
  32. onPerformSelfUpdateFinished(code, output)
  33. break
  34. }
  35. }
  36. function onCheckFinished(code, output) {
  37. if (code !== 0) {
  38. ui.log(output)
  39. return
  40. }
  41. [, updateMode, webVersion, link] = output.match(/.*PLVERSION\s([^\s]+)\s([^\s]+)\s([^\s]+)\n.*/)
  42. description = (output.match(/.*PLDESCRIPTION\s(.*)\n.*/)[1] || "").trim()
  43. if (!webVersion) {
  44. ui.log(output)
  45. ui.log(qsTr("Failed to get update version"))
  46. return
  47. }
  48. let installedVersion = version.split(/\s/)[0]
  49. if (installedVersion === webVersion) {
  50. ui.log(appName + " " + qsTr("is up to date"))
  51. return
  52. }
  53. ui.log(appName + " " + qsTr("update available") + " v" + webVersion)
  54. dialogConfirmUpdate.open()
  55. }
  56. function onPerformSelfUpdateFinished(code, output) {
  57. if (code === 0) {
  58. // force quit
  59. clientInstaller.installerState = clientInstaller.stateDone
  60. window.close()
  61. return
  62. }
  63. // error?
  64. ui.log("")
  65. ui.log(output)
  66. }
  67. }
  68. Dialog {
  69. id: dialogConfirmUpdate
  70. anchors.centerIn: parent
  71. title: window.title
  72. standardButtons: Dialog.Yes | Dialog.No
  73. modal: true
  74. focus: true
  75. width: block * 8
  76. Text {
  77. anchors.fill: parent
  78. textFormat: Text.RichText
  79. wrapMode: Text.WordWrap
  80. text: appName + " v" + webVersion + " " + qsTr("is available.")
  81. + "<br/>" + qsTr("Do you wish to") + " " + qsTr(updateMode) + qsTr("?")
  82. + "<br/><br/><b>" + qsTr("Release notes:") + "</b>" + description
  83. }
  84. onAccepted: {
  85. switch (updateMode) {
  86. case "update":
  87. script.performSelfUpdate()
  88. break
  89. case "download":
  90. Qt.openUrlExternally(link)
  91. break
  92. }
  93. }
  94. }
  95. function check() {
  96. script.checkSelfUpdate()
  97. }
  98. }