<commit_before>/*******************************************************************************

Licensed to the OpenCOR team under one or more contributor license agreements.
See the NOTICE.txt file distributed with this work for additional information
regarding copyright ownership. The OpenCOR team licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

*******************************************************************************/

//==============================================================================
// Check for updates window
//==============================================================================

#include "checkforupdateswindow.h"
#include "cliutils.h"
#include "mainwindow.h"

//==============================================================================

#include "ui_checkforupdateswindow.h"

//==============================================================================

#include <QCoreApplication>
#include <QCheckBox>
#include <QDesktopServices>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
#include <QSettings>
#include <QTextEdit>
#include <QVariantMap>

//==============================================================================

namespace OpenCOR {

//==============================================================================

CheckForUpdatesEngine::CheckForUpdatesEngine(const QString &pApplicationDate) :
    mApplicationDate(pApplicationDate),
    mStatus(QString()),
    mNewerVersions(QStringList())
{
}

//==============================================================================

void CheckForUpdatesEngine::check()
{
    // Retrieve some information about the different versions of OpenCOR that
    // are available

    QByteArray fileVersionsContents = QByteArray();
    QString errorMessage = QString();

    mNewerVersions.clear();

    if (OpenCOR::readFileContentsFromUrl("http://www.opencor.ws/downloads/index.js", fileVersionsContents, &errorMessage)) {
        QJsonParseError jsonParseError;
        QJsonDocument versions = QJsonDocument::fromJson(fileVersionsContents.mid(15, fileVersionsContents.length()-17), &jsonParseError);

        if (jsonParseError.error == QJsonParseError::NoError) {
            QVariantMap versionMap;
            int versionMajor, versionMinor, versionPatch;
            int versionDay, versionMonth, versionYear;
            QString versionVersion;
            QString versionDate;

            foreach (const QVariant &version, versions.object().toVariantMap()["versions"].toList()) {
                // Retrieve the version and date of the current version

                versionMap = version.toMap();

                versionMajor = versionMap["major"].toInt();
                versionMinor = versionMap["minor"].toInt();
                versionPatch = versionMap["patch"].toInt();

                versionDay   = versionMap["day"].toInt();
                versionMonth = versionMap["month"].toInt();
                versionYear  = versionMap["year"].toInt();

                versionDate = QString("%1-%2-%3").arg(versionYear)
                                                 .arg(versionMonth, 2, 10, QChar('0'))
                                                 .arg(versionDay, 2, 10, QChar('0'));

                if (!versionMajor && !versionMinor && !versionPatch) {
                    versionVersion = versionDate;
                } else {
                    versionVersion = QString("%1.%2").arg(versionMajor).arg(versionMinor);

                    if (versionPatch)
                        versionVersion = QString("%1.%2").arg(versionVersion).arg(versionPatch);
                }

                // Check whether the version is newer and, if so, add it to our list
<commit_msg>Some minor cleaning up [ci skip].<commit_after>/*******************************************************************************

Licensed to the OpenCOR team under one or more contributor license agreements.
See the NOTICE.txt file distributed with this work for additional information
regarding copyright ownership. The OpenCOR team licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

*******************************************************************************/

//==============================================================================
// Check for updates window
//==============================================================================

#include "checkforupdateswindow.h"
#include "cliutils.h"
#include "mainwindow.h"

//==============================================================================

#include "ui_checkforupdateswindow.h"

//==============================================================================

#include <QCoreApplication>
#include <QCheckBox>
#include <QDesktopServices>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
#include <QSettings>
#include <QTextEdit>
#include <QVariantMap>

//==============================================================================

namespace OpenCOR {

//==============================================================================

CheckForUpdatesEngine::CheckForUpdatesEngine(const QString &pApplicationDate) :
    mApplicationDate(pApplicationDate),
    mStatus(QString()),
    mNewerVersions(QStringList())
{
}

//==============================================================================

void CheckForUpdatesEngine::check()
{
    // Retrieve some information about the different versions of OpenCOR that
    // are available

    QByteArray fileVersionsContents = QByteArray();
    QString errorMessage = QString();

    mNewerVersions.clear();

    if (OpenCOR::readFileContentsFromUrl("http://www.opencor.ws/downloads/index.js", fileVersionsContents, &errorMessage)) {
        QJsonParseError jsonParseError;
        QJsonDocument versions = QJsonDocument::fromJson(fileVersionsContents.mid(15, fileVersionsContents.length()-17), &jsonParseError);

        if (jsonParseError.error == QJsonParseError::NoError) {
            QVariantMap versionMap;
            int versionMajor, versionMinor, versionPatch;
            int versionDay, versionMonth, versionYear;
            QString versionVersion;
            QString versionDate;

            foreach (const QVariant &version, versions.object().toVariantMap()["versions"].toList()) {
                // Retrieve the version and date of the current version

                versionMap = version.toMap();

                versionMajor = versionMap["major"].toInt();
                versionMinor = versionMap["minor"].toInt();
                versionPatch = versionMap["patch"].toInt();

                versionDay   = versionMap["day"].toInt();
                versionMonth = versionMap["month"].toInt();
                versionYear  = versionMap["year"].toInt();

                versionDate = QString("%1-%2-%3").arg(versionYear)
                                                 .arg(versionMonth, 2, 10, QChar('0'))
                                                 .arg(versionDay, 2, 10, QChar('0'));

                if (!versionMajor && !versionMinor && !versionPatch) {
                    versionVersion = versionDate;
                } else {
                    versionVersion = QString("%1.%2").arg(versionMajor, versionMinor);

                    if (versionPatch)
                        versionVersion = QString("%1.%2").arg(versionVersion, versionPatch);
                }

                // Check whether the version is newer and, if so, add it to our list

{'fraction_non_alphanumeric': 0.24958579881656806, 'fraction_numerical': 0.00544378698224852, 'mean_word_length': 3.4737956590788777, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1658944', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1799, 'n_words': 535}