Exemplo n.º 1
0
//--------------------------------------------------------------------------------------------------------------
// Monitors download thread, starts new downloads, and updates progress bar
bool DownloadManager::Update()
{
	PruneCompletedRequests();
	CheckActiveDownload();
	StartNewDownload();

	return m_activeRequest != NULL;
}
void B9UpdateManager::PromptDoUpdates(bool showCheckingBar, bool promptLocalLocation)
{

    silentUpdateChecking = !showCheckingBar;
    //only even think about Doing Updates if the state machine isnt doing anything
    if(downloadState != "Idle" && !silentUpdateChecking)
    {
        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText("B9Creator is already checking for updates");
        msgBox.exec();
        return;
    }


    if(netManager->networkAccessible() == QNetworkAccessManager::NotAccessible && !silentUpdateChecking)
    {
        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText("B9Creator does not have accesss to the network");
        msgBox.exec();
        return;

    }
    //maybe do some other early checks if possible.



    //create waiting bar now
    waitingbar = new LoadingBar(0,0,false);
    if(silentUpdateChecking) waitingbar->hide();
    connect(waitingbar,SIGNAL(rejected()),this,SLOT(OnCancelUpdate()));


    //get the network access manager looking for the file versions.txt on the manifest;
    QNetworkRequest request;

    if(promptLocalLocation)//prompt the user for new manifest location.
    {
        QString path = QFileDialog::getExistingDirectory(NULL,
                       "Locate the B9Creator Update Pack Folder",
                       CROSS_OS_GetDirectoryFromLocationTag("DOCUMENTS_DIR"));
        qDebug() << "Opening Update Pack: " << path;
        if(path.isEmpty())
            return;

        QString manifestURL = QUrl().fromLocalFile(path + "/" + RemoteManifestFileName).toString();
        RemoteManifestPath = QUrl().fromLocalFile(path).toString();
        request.setUrl(QUrl(manifestURL));
    }
    else//user internet manifest location.
    {
        request.setUrl(QUrl(RemoteManifestPath + RemoteManifestFileName));
    }


    //and set the state
    downloadState = "DownloadingFileVersions";

    qDebug() << "UpdateManager: User Started Update Check and Download";

    StartNewDownload(request);
}
void B9UpdateManager::OnRecievedReply(QNetworkReply* reply)
{

    timeoutTimer.stop();
    if(ignoreReplies)
    {
        currentReply->deleteLater();
        currentReply = NULL;
        return;
    }


    if(reply->bytesAvailable() == 0)//nothing downloaded.
    {
        qDebug() << "B9Update Manager, zero bytes downloaded.";
        if(!silentUpdateChecking)
        {
            QMessageBox msgBox;
            msgBox.setText("Online Update Attempt Failed.");
            if(downloadState == "DownloadingFileVersions")
            {
                waitingbar->hide();
                msgBox.setInformativeText("You may use a B9Creator update pack if available. \nBrowse to a local update pack now?");
                msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
                msgBox.setDefaultButton(QMessageBox::Cancel);
                int ret = msgBox.exec();

                if(ret == QMessageBox::Ok)
                {
                    ResetEverything();
                    PromptDoUpdates(true,true);
                    return;
                }//if no - continue to reset below.
            }
            else
            {
                msgBox.setInformativeText("please check your internet connection.");
                msgBox.exec();
            }

        }
        ResetEverything();
        return;
    }

    if(downloadState == "DownloadingFileVersions")
    {
        //remoteEntries - localEntries = updateEntries
        CopyInRemoteEntries();
        CopyInLocalEntries();
        CalculateUpdateEntries();

        //so now we have a big list of local things that need updated...
        if(updateEntries.size() > 0)
        {
            waitingbar->hide();
            QMessageBox msgBox;
            msgBox.setIcon(QMessageBox::Question);
            if(updateEntries.size() == 1)
            {
                msgBox.setText("There is " + QString::number(updateEntries.size()) + " file update available.");
                msgBox.setInformativeText("Do you want to update it? <a href=\"http://b9creator.com/softwaredelta\">View change log</a>");
            }
            else
            {
                msgBox.setText("There are " + QString::number(updateEntries.size()) + " file updates available.");
                msgBox.setInformativeText("Do you want to update them? <a href=\"http://b9creator.com/softwaredelta\">View change log</a>");
            }
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
            msgBox.setDefaultButton(QMessageBox::Yes);

            int doUpdates = msgBox.exec();

            currentUpdateIndx = 0;//at this point we know we can iterate through
            //needed updates

            if(doUpdates == QMessageBox::Yes)
            {
                waitingbar->show();
                downloadState = "DownloadingFiles";//next state
                //send a new request
                currentReply->deleteLater();
                currentReply = NULL;

                //request the file in the right directory on the server
                QNetworkRequest request(QUrl(RemoteManifestPath + updateEntries[currentUpdateIndx].OSDir + updateEntries[currentUpdateIndx].fileName));


                StartNewDownload(request);

            }
            else//no
            {
                ResetEverything();
            }
        }
        else
        {
            ResetEverything();
            emit NotifyUpdateFinished();//emit update finished signal
            if(!silentUpdateChecking)
            {
                QMessageBox msgBox;
                msgBox.setText("All files are up to date");
                msgBox.setIcon(QMessageBox::Information);
                msgBox.exec();
            }
        }

    }
    else if(downloadState == "DownloadingFiles")
    {
        //lets put the file in the temp directory
        if(!OnDownloadDone())
        {
            //bail
            ResetEverything();
            return;
        }
        if(currentUpdateIndx < updateEntries.size() - 1)
        {
            currentUpdateIndx++;
            currentReply->deleteLater();
            currentReply = NULL;


            QNetworkRequest request(QUrl(RemoteManifestPath + updateEntries[currentUpdateIndx].OSDir + updateEntries[currentUpdateIndx].fileName));

            StartNewDownload(request);
        }
        else
        {
            if(!CopyFromTemp())
            {
                //bail
                qDebug() << "UpdateManager: Unable To Copy Files From Temp Directory";
                QMessageBox msgBox;
                msgBox.setText("Unable to update,\nUnable To Copy Files From Temp Directory.");
                msgBox.setIcon(QMessageBox::Warning);
                msgBox.exec();
                ResetEverything();
                return;
            }

            if(!UpdateLocalFileVersionList())
            {
                qDebug() << "UpdateManager: Unable To overwrite local FileVersions.txt";
                QMessageBox msgBox;
                msgBox.setText("Unable to update,\nUnable To overwrite local FileVersions.txt.");
                msgBox.setIcon(QMessageBox::Warning);
                msgBox.exec();
                ResetEverything();
                return;
            }

            waitingbar->hide();
            QMessageBox msgBox;
            msgBox.setIcon(QMessageBox::Information);
            msgBox.setText("All files up to date");
            msgBox.setInformativeText("Please Re-Launch B9Creator.");
            msgBox.exec();

            ResetEverything();

            //Safely Exit the Program.....
            QCoreApplication::exit();
        }
    }
}