/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Updates and installs ////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
void ServerManager::getServerDescription(const int index)
{
    Server *s = &m_Servers[index];
    qWarning() << "getAllDescription" << index << s->nativeUrl();
    for(int j = 0; j < m_WorkingEngines.count(); ++j) {
        DataPack::IServerEngine *engine = m_WorkingEngines.at(j);
        if (engine->managesServer(*s)) {
            ServerEngineQuery query;
            query.server = s;
            query.forceDescriptionFromLocalCache = false;
            query.downloadDescriptionFiles = true;
            query.downloadPackFile = false;
            engine->addToDownloadQueue(query);
            connect(engine, SIGNAL(queueDowloaded()), this, SLOT(engineDescriptionDownloadDone()));
            engine->startDownloadQueue();
        }
    }
}
void ServerManager::getAllDescriptionFile(QProgressBar *bar)
{
    if (m_WorkingEngines.count()==0) {
        LOG_ERROR("No ServerEngine recorded.");
        Q_EMIT allServerDescriptionAvailable();
        return;
    }

    // Clear engine download queue
    m_Packs.clear();
    for(int j = 0; j < m_WorkingEngines.count(); ++j) {
        m_WorkingEngines[j]->stopJobsAndClearQueue();
    }

    // Populate all server engine
    int workingTasks = 0;
    for(int i=0; i < m_Servers.count(); ++i) {
        Server *s = &m_Servers[i];
        qWarning() << "ServerManager::getAllDescription" << i << s->nativeUrl();
        for(int j = 0; j < m_WorkingEngines.count(); ++j) {
            DataPack::IServerEngine *engine = m_WorkingEngines.at(j);
            if (engine->managesServer(*s)) {
                ServerEngineQuery query;
                query.server = s;
                query.forceDescriptionFromLocalCache = false;
                query.downloadDescriptionFiles = true;
                query.downloadPackFile = false;
                ++workingTasks;
                engine->addToDownloadQueue(query);
            }
        }
    }
    // Populate progressBar
    // FIXME: this object should not use QProgressBar but emit a progress signal instead
    if (bar) {
        bar->setRange(0, workingTasks);
        bar->setValue(0);
        m_ProgressBar = bar;
    }
    // Then start all server engine
    for(int j = 0; j < m_WorkingEngines.count(); ++j) {
        DataPack::IServerEngine *engine = m_WorkingEngines.at(j);
        if (engine->downloadQueueCount() > 0) {
            connect(engine, SIGNAL(queueDowloaded()), this, SLOT(engineDescriptionDownloadDone()));
            engine->startDownloadQueue();
        }
    }
}
//FIXME: *bar must not be directly accessed, not thread safe & bad design
//--> use signal/slot mechanism
bool PackManager::downloadPack(const Pack &pack, QProgressBar *bar)
{
    Q_ASSERT(!m_Engines.isEmpty());
    m_Msg.clear();
    m_Errors.clear();

    Server &server = serverManager()->getServerForPack(pack);

    if (server.isNull()) {
        LOG_ERROR(tr("No server found for pack %1 (%2)").arg(pack.uuid()).arg(pack.version()));
        m_Errors << tr("No server found for pack %1 (%2)").arg(pack.uuid()).arg(pack.version());
        if (bar) {
            bar->setRange(0,1);
            bar->setValue(1);
        }
        return false;
    }

    // Pack already downloaded ?
    if (isPackInPersistentCache(pack)) {
//        qWarning() << "IN CACHE" << pack.name() << pack.version();
        if (checkCachedPackFileIntegrity(pack)) {
            LOG("Pack already downloaded. Using the cached pack: " + pack.persistentlyCachedZipFileName()) ;
            if (bar) {
                bar->setRange(0,1);
                bar->setValue(1);
            }
            ServerEngineStatus status;
            status.downloadCorrectlyFinished = true;
            status.hasError = false;
            status.isSuccessful = true;
            Q_EMIT packDownloaded(pack, status);
            return true;
        } else {
            QString error;
            Utils::removeDirRecursively(pack.persistentlyCachedZipFileName(), &error);
        }
    }

    // Download the pack from this server
    for(int j=0; j < m_Engines.count(); ++j) {
        DataPack::IServerEngine *engine = m_Engines.at(j);
        if (engine->managesServer(server)) {
            // Create the label/progress for the pack
            DataPack::ServerEngineQuery query;
            query.downloadPackFile = true;
            query.pack = &pack;
            query.server = &server;
            query.progressBar = bar;
            engine->addToDownloadQueue(query);
            LOG(tr("Adding %1 to %2 download queue").arg(pack.uuid()).arg(server.uuid()));
            m_Msg << tr("Adding %1 to %2 download queue.").arg(pack.uuid()).arg(server.uuid());
        }
    }

    // Start download
    bool downloading = false;
    for(int i = 0; i < m_Engines.count(); ++i) {
        DataPack::IServerEngine *engine = m_Engines.at(i);
        if (engine->downloadQueueCount() > 0) {
            downloading = true;
            connect(engine, SIGNAL(packDownloaded(DataPack::Pack, DataPack::ServerEngineStatus)),
                    this, SLOT(packDownloadDone(DataPack::Pack, DataPack::ServerEngineStatus)));
            engine->startDownloadQueue();
        }
    }
    if (!downloading) {
        m_Errors << tr("Nothing to download");
        return false;
    }
    return true;
}