Exemple #1
0
void Updater::httpFinished()
{
    if (httpRequestAborted) {
        reply->deleteLater();
        return;
    }

    QString data = QString(mData);
        
    if (reply->error()) {
        //TODO: add a warning.
        qWarning() << "Failed to connect to server.";
    } else {

        QStringList urls = data.split("::", QString::SkipEmptyParts);
        if(urls.count() == 2) {
            QMessageBox msgbox(this);
            msgbox.setIcon(QMessageBox::Information);
            msgbox.setText(tr("There is a new version of %1.").arg(AppInfo::inst()->appName));
            msgbox.setInformativeText(tr("Would you like to download the new version?"));
            /*QPushButton* downloadNow =*/ msgbox.addButton(tr("Download the new version"), QMessageBox::ActionRole);
            QPushButton* seeNotes    = msgbox.addButton(tr("See what has changed"), QMessageBox::HelpRole);
            QPushButton* remindLater = msgbox.addButton(tr("Remind me later"), QMessageBox::RejectRole);

            msgbox.exec();

            if(msgbox.clickedButton() == remindLater)
                return;
            if(msgbox.clickedButton() == seeNotes) {
                QDesktopServices::openUrl(QUrl(urls.last()));
                return;
            }
                
            downloadInstaller(QUrl(urls.first()));
            
        } else if(!mSilent) {
            QMessageBox::information(this, tr("No updates available"),
                            tr("There are no updates available for %1 at this time.").arg(AppInfo::inst()->appName), QMessageBox::Ok);
        }
    }
    
    reply->deleteLater();
    reply = 0;
}
void HttpUpdater::getXmlFinished(QNetworkReply *reply)
{
    if (reply == requestXml)
    {
        //if xml file could not be downloaded
        if (reply->error())
        {
            QMessageBox::warning(0, "HEXplorer::update", QString(reply->errorString()) +
                                  "\n\n" +
                                  "HEXplorer could not read update.xml file for available updates.\n\n" +
                                  "Please control or configure your internet connection \n" +
                                  "setting properly the proxy parameters if you are behind a proxy\n" +
                                  "or simply disabling the automatic updates into Edit/Settings.\n",
                                  QMessageBox::Ok, QMessageBox::Cancel);

            return; //exit download
        }

        if (!reply->open(QFile::ReadOnly | QFile::Text))
        {
            QMessageBox::warning(0, "HEXplorer::update", "Unable to open update.xml file",
                                  QMessageBox::Ok, QMessageBox::Cancel);

            return;
        }
        else
        {
            QByteArray data = reply->readAll();
            QXmlStreamReader reader(data);
            while (!reader.atEnd())
            {
                reader.readNext();
                if (reader.isStartElement() && reader.attributes().hasAttribute("version"))
                {
                    newVersion = reader.attributes().value("version").toString();
                    QString old_Version = qApp->applicationVersion();
                    QStringList oldList = old_Version.split(".");
                    QStringList newList = newVersion.split(".");
                    if (newList.at(0).toDouble() > oldList.at(0).toDouble())
                    {
                        reader.readNext();
                        if(reader.isCharacters())
                        {
                            updateFilePath = reader.text().toString();
                        }
                    }
                    else if (newList.at(0).toDouble() == oldList.at(0).toDouble())
                    {
                        if (newList.at(1).toDouble() > oldList.at(1).toDouble())
                        {
                            reader.readNext();
                            if(reader.isCharacters())
                            {
                                updateFilePath = reader.text().toString();
                            }
                        }
                        else if (newList.at(1).toDouble() == oldList.at(1).toDouble())
                        {
                            if (newList.at(2).toDouble() > oldList.at(2).toDouble())
                            {
                                reader.readNext();
                                if(reader.isCharacters())
                                {
                                    updateFilePath = reader.text().toString();
                                }
                            }
                        }
                    }
                }
                else if (reader.isStartElement() && reader.qualifiedName() == "details")
                {
                    reader.readNext();
                    if(reader.isCharacters())
                    {
                        updateDetails = reader.text().toString();
                    }
                }
            }

            //display streamreader error
            if (reader.error())
            {
                QMessageBox::critical(0, "HEXplorer::update", reader.errorString(),
                                      QMessageBox::Ok, QMessageBox::Cancel);
                return;
            }

            //if update is or not available, do:
            if(updateFilePath.isEmpty())
            {
                if (displayUptoDate)
                {
                    QMessageBox::information(0, "HEXplorer::update", "You are up to date at version "
                                             + qApp->applicationVersion(),
                                             QMessageBox::Ok, QMessageBox::Cancel);
                }
            }
            else
            {
                QMessageBox msgBox;
                msgBox.setIconPixmap(QPixmap(":/icones/updates.png").scaled(80,80));
                msgBox.setText("A new update is available :\n - current version : " + qApp->applicationVersion() + "\n - new version : " + newVersion);
                msgBox.setInformativeText("would you like to download it?");
                msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                msgBox.setDefaultButton(QMessageBox::Yes);
                msgBox.setDetailedText(updateDetails);
                int ret = msgBox.exec();

                if (ret == QMessageBox::Yes)
                {
                    binUrl.setUrl(updateFilePath);
                    downloadInstaller(binUrl);;
                }
                else
                    return;
            }
        }
    }
}
void HttpUpdater::managerRequestFinished(QNetworkReply *reply)
{
    if (reply == replyXml)
    {
        //if xml file could not be downloaded
        if (reply->error())
        {
            QMessageBox::warning(0, "HEXplorer::update", QString(reply->errorString()) +
                                  "\n\n" +
                                  "HEXplorer could not read update.xml file for available updates.\n\n" +
                                  "Please control or configure your internet connection \n" +
                                  "setting properly the proxy parameters if you are behind a proxy\n" +
                                  "or simply disabling the automatic updates into Edit/Settings.\n",
                                  QMessageBox::Ok, QMessageBox::Cancel);

            return; //exit download
        }

        if (!reply->open(QFile::ReadOnly | QFile::Text))
        {
            QMessageBox::warning(0, "HEXplorer::update", "Unable to open update.xml file",
                                  QMessageBox::Ok, QMessageBox::Cancel);

            return;
        }
        else
        {
            QJsonDocument jsonDocument = QJsonDocument::fromJson(reply->readAll());
            QJsonObject rootObject = jsonDocument.object();
            QByteArray data = QByteArray::fromBase64(rootObject.value("content").toVariant().toByteArray());

            QXmlStreamReader reader(data);

            while (!reader.atEnd())
            {
                reader.readNext();
                if (reader.isStartElement() && reader.attributes().hasAttribute("version"))
                {
                    newVersion = reader.attributes().value("version").toString();
                    QString old_Version = qApp->applicationVersion();
                    QStringList oldList = old_Version.split(".");
                    QStringList newList = newVersion.split(".");
                    if (newList.at(0).toDouble() > oldList.at(0).toDouble())
                    {
                        reader.readNext();
                        if(reader.isCharacters())
                        {
                            updateFilePath = reader.text().toString();
                        }
                    }
                    else if (newList.at(0).toDouble() == oldList.at(0).toDouble())
                    {
                        if (newList.at(1).toDouble() > oldList.at(1).toDouble())
                        {
                            reader.readNext();
                            if(reader.isCharacters())
                            {
                                updateFilePath = reader.text().toString();
                            }
                        }
                        else if (newList.at(1).toDouble() == oldList.at(1).toDouble())
                        {
                            if (newList.at(2).toDouble() > oldList.at(2).toDouble())
                            {
                                reader.readNext();
                                if(reader.isCharacters())
                                {
                                    updateFilePath = reader.text().toString();
                                }
                            }
                        }
                    }
                }
                else if (reader.isStartElement() && reader.qualifiedName() == "details")
                {
                    reader.readNext();
                    if(reader.isCharacters())
                    {
                        updateDetails = reader.text().toString();
                    }
                }
            }

            //display streamreader error
            if (reader.error())
            {
                QMessageBox::critical(0, "HEXplorer::update", reader.errorString(),
                                      QMessageBox::Ok, QMessageBox::Cancel);
                return;
            }

            //if update is or not available, do:
            if(updateFilePath.isEmpty())
            {
                if (displayUptoDate)
                {
                    QMessageBox::information(0, "HEXplorer::update", "You are up to date at version "
                                             + qApp->applicationVersion(),
                                             QMessageBox::Ok, QMessageBox::Cancel);
                }
            }
            else
            {
                QMessageBox msgBox;
                msgBox.setIconPixmap(QPixmap(":/icones/updates.png").scaled(80,80));
                msgBox.setText("A new update is available :\n - current version installed: " + qApp->applicationVersion() + "\n - new version available: " + newVersion);
                //msgBox.setInformativeText("would you like to download it?");
                msgBox.setInformativeText("Download at https://github.com/adhoc2/HEXplorer/releases/");
                //msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                //msgBox.setDefaultButton(QMessageBox::Yes);
                msgBox.setDefaultButton(QMessageBox::Ok);
                msgBox.setDetailedText(updateDetails);
                int ret = msgBox.exec();

                if (ret == QMessageBox::Yes)
                {
                    //binUrl.setUrl(updateFilePath);
                    binUrl.setUrl("https://api.github.com/repos/adhoc2/HEXplorer/releases/assets/2328230");
                    //binUrl.setUrl("https://github.com/adhoc2/HEXplorer/releases/download/v0.7.12/setup_HEXplorer_0712.exe");
                    downloadInstaller(binUrl);
                }
                else
                    return;
            }

        }
    }
    else if (reply == replyDownloader)
    {
        progressBar.hide();
        binFile.close();

        if (reply->error())
        {
            QMessageBox::warning(0, "HEXplorer::update", QString(reply->errorString()) +
                                  "\n\n" +
                                  "Please control your internet connection \n" +
                                  "or set the proxy parameters properly into Edit/Settings",
                                  QMessageBox::Ok, QMessageBox::Cancel);

            return; //exit
        }

        QMessageBox msgBox;
        msgBox.setIconPixmap(QPixmap(":/icones/updates.png").scaled(80,80));
        msgBox.setText("Download completed.");
        msgBox.setInformativeText("Would you like to install ?");
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msgBox.setDefaultButton(QMessageBox::Yes);
        int ret = msgBox.exec();

        //update user_List
        if (ret == QMessageBox::Yes)
        {
            //execute the downloaded file
            launchInstaller();
        }
        else
            return;
    }
}