| 655 | } |
| 656 | |
| 657 | void MainWindow::checkUpdate(bool forceInfoBox) { |
| 658 | if (!CEMU_RELEASE) { |
| 659 | if (forceInfoBox) { |
| 660 | QMessageBox::warning(this, MSG_WARNING, tr("Checking updates is disabled for development builds")); |
| 661 | } |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | static const QString latestReleaseURL = QStringLiteral("https://github.com/CE-Programming/CEmu/releases/latest"); |
| 666 | QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
| 667 | connect(manager, &QNetworkAccessManager::finished, this, [this, manager, forceInfoBox](QNetworkReply* reply) { |
| 668 | const QByteArray respData = reply->readAll(); |
| 669 | if (!respData.isEmpty()) { |
| 670 | QJsonParseError parseError; |
| 671 | const auto json = QJsonDocument::fromJson(respData, &parseError); |
| 672 | if (parseError.error != QJsonParseError::NoError || json.isNull()) { |
| 673 | goto updateCheckErr; |
| 674 | } |
| 675 | const auto tag_name = json.object().value(QStringLiteral("tag_name")).toString(); |
| 676 | const auto name = json.object().value(QStringLiteral("name")).toString(); |
| 677 | if (QStringLiteral(CEMU_VERSION).compare(tag_name) == 0) { |
| 678 | if (forceInfoBox) { |
| 679 | QMessageBox::information(this, tr("No update available"), tr("You already have the latest CEmu version") + QStringLiteral(" (" CEMU_VERSION ")")); |
| 680 | } |
| 681 | } else if (!tag_name.isEmpty()) { |
| 682 | QMessageBox updateInfoBox(this); |
| 683 | updateInfoBox.addButton(QMessageBox::Ok); |
| 684 | updateInfoBox.setIconPixmap(QPixmap(QStringLiteral(":/icons/resources/icons/icon.png"))); |
| 685 | updateInfoBox.setWindowTitle(tr("CEmu update")); |
| 686 | updateInfoBox.setText(tr("<b>A new version of CEmu is available!</b>" |
| 687 | "<br/><br/>%1<br/><br/>" |
| 688 | "You can <a href='%2'>download it here</a>.") |
| 689 | .arg(name.isEmpty() ? tag_name : name) |
| 690 | .arg(latestReleaseURL)); |
| 691 | updateInfoBox.setTextFormat(Qt::RichText); |
| 692 | updateInfoBox.show(); |
| 693 | updateInfoBox.exec(); |
| 694 | } else { |
| 695 | goto updateCheckErr; |
| 696 | } |
| 697 | } else { |
| 698 | updateCheckErr: |
| 699 | if (forceInfoBox) { |
| 700 | QMessageBox updateInfoBox(this); |
| 701 | updateInfoBox.addButton(QMessageBox::Ok); |
| 702 | updateInfoBox.setIcon(QMessageBox::Warning); |
| 703 | updateInfoBox.setWindowTitle(tr("Update check failed")); |
| 704 | updateInfoBox.setText(tr("<b>An error occurred while checking for CEmu updates.</b>" |
| 705 | "<br/>" |
| 706 | "You can however <a href='%1'>go here</a> to check yourself.") |
| 707 | .arg(latestReleaseURL)); |
| 708 | updateInfoBox.setTextFormat(Qt::RichText); |
| 709 | updateInfoBox.show(); |
| 710 | updateInfoBox.exec(); |
| 711 | } |
| 712 | } |
| 713 | manager->deleteLater(); |
| 714 | }); |