bool DownloadManagerImpl::removeAndUpdateDownload(int downloadId, DownloadManager::DownloadStatus status)
{
    Download *download = getDownloadByDownloadId(downloadId);
    if (!download) {
        qDebug() << "Could not find downloadId = " << downloadId;

        // check download
        checkDownloadQueue();

        return false;
    }

    // Update status
    download->setDownloadStatus(status);
    if (!m_downloadDAO->updateDownload(download))
        qDebug() << __PRETTY_FUNCTION__ << " Could not update information for download with id = " << downloadId;

    // Remove the download from the downloading list
    removeDownloadById(downloadId);

    qDebug() << "Remove the download with downloadId = " << downloadId;

    // check download
    checkDownloadQueue();

    return true;
}
Exemplo n.º 2
0
void DownloadManager::checkDownloads(UserConnection* aConn) {
    dcassert(aConn->getDownload() == NULL);

    QueueItem::Priority prio = QueueManager::getInstance()->hasDownload(aConn->getUser());
    if(!startDownload(prio)) {
        removeConnection(aConn);
        return;
    }

    Download* d = QueueManager::getInstance()->getDownload(*aConn, aConn->isSet(UserConnection::FLAG_SUPPORTS_TTHL));

    if(!d) {
        Lock l(cs);
        aConn->setState(UserConnection::STATE_IDLE);
        idlers.push_back(aConn);
        return;
    }

    aConn->setState(UserConnection::STATE_SND);

    if(aConn->isSet(UserConnection::FLAG_SUPPORTS_XML_BZLIST) && d->getType() == Transfer::TYPE_FULL_LIST) {
        d->setFlag(Download::FLAG_XML_BZ_LIST);
    }

    {
        Lock l(cs);
        downloads.push_back(d);
    }
    fire(DownloadManagerListener::Requesting(), d);

    dcdebug("Requesting " I64_FMT "/" I64_FMT "\n", static_cast<long long int>(d->getStartPos()), static_cast<long long int>(d->getSize()));

    aConn->send(d->getCommand(aConn->isSet(UserConnection::FLAG_SUPPORTS_ZLIB_GET)));
}
void DownloadManagerImpl::release()
{
    m_mutexLocker.lock();
    // Free downloads in the downloading list
    foreach(Download *download, m_downloadingList) {
        if (download == 0)
            continue;
        download->stop();
        download->deleteLater();
        download = 0;
    }
    m_downloadingList.clear();

    for (int i = 0; i < m_downloadQueue.count(); i++) {
        Download *download = m_downloadQueue.at(i);
        if (download == 0)
            continue;
        download->stop();
        download->deleteLater();
        download = 0;
    }
    m_downloadQueue.clear();
    m_mutexLocker.unlock();

    if (m_dbConnection) {
        m_dbConnection->close();
        delete m_dbConnection;
        m_dbConnection = 0;
    }

    if (m_downloadDAO) {
        delete m_downloadDAO;
        m_downloadDAO = 0;
    }
}
Download *DownloadManagerImpl::getDownloadByContactId(int contactId)
{
    QMutexLocker locker(&m_mutexLocker);
    for (int i = 0; i < m_downloadQueue.count(); i++) {
        Download *download = m_downloadQueue.at(i);
        if (download == 0)
            continue;
        Contact *contact = download->getContact();
        if (contact == 0)
            continue;
        if (contact->getId() == contactId)
            return download;
    }

    foreach(Download *download, m_downloadingList) {
        if (download == 0)
            continue;
        Contact *contact = download->getContact();
        if (contact == 0)
            continue;
        if (contact->getId() == contactId)
            return download;
    }

    return 0;
}
Exemplo n.º 5
0
FileDownloader::Download& FileDownloader::DownloadFile(std::string url) {
    Download* download = new FileDownloader::Download();
    download->container = this;
    download->url = url;
    activeDownloads.push_back(download);
    
    MemoryStruct mem;
    mem.memory = 0;
    mem.size = 0;
    
    CURL* curl = (CURL*)this->curl;
    
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem);
    CURLcode res = curl_easy_perform(curl);
    
    DownloadedFile downloadedFile;
    downloadedFile.url = download->url;
    downloadedFile.data = mem.memory;
    downloadedFile.size = (int)mem.size;
    download->Completed(downloadedFile);
    download->container->activeDownloads.erase(std::find(download->container->activeDownloads.begin(), download->container->activeDownloads.end(), download));
    delete download;
    DownloadComplete(downloadedFile);
    return *download;
}
int DownloadManagerImpl::getCurrentRemainTimeByDownload(int downloadId)
{
    Download *download = getDownloadByDownloadId(downloadId);
    if (download)
        return download->getCurrentRemainTime();

    return -1;
}
int DownloadManagerImpl::getCurrentRemainTimeByContact(int contactId)
{
    Download *download = getDownloadByContactId(contactId);
    if (download)
        return download->getCurrentRemainTime();

    return -1;
}
Exemplo n.º 8
0
void didCreateDestinationCallback(CFURLDownloadRef, CFURLRef, const void* clientInfo)
{
    // The concept of the ".download bundle" is internal to the Download, so we try to hide its
    // existence by reporting the final destination was created, when in reality the bundle was created.

    Download* download = downloadFromClientInfo(clientInfo);
    download->didCreateDestination(download->destination());
}
OSStatus SecureDownloadFinished (SecureDownloadRef downloadRef)
{
	API_BEGIN
	Required (downloadRef);
	Download* d = (Download*) downloadRef;
	d->Finalize ();
	API_END
}
void DownloadManager::cancelDownload(DownloadID downloadID)
{
    Download* download = m_downloads.get(downloadID);
    if (!download)
        return;

    download->cancel();
}
OSStatus SecureDownloadUpdateWithData (SecureDownloadRef downloadRef, CFDataRef data)
{
	API_BEGIN
	Required (downloadRef);
	Required (data);
	Download* d = (Download*) downloadRef;
	d->UpdateWithData (data);
	API_END
}
Exemplo n.º 12
0
int64_t DownloadManager::getRunningAverage() {
    Lock l(cs);
    int64_t avg = 0;
    for(DownloadList::iterator i = downloads.begin(); i != downloads.end(); ++i) {
        Download* d = *i;
        avg += d->getAverageSpeed();
    }
    return avg;
}
OSStatus SecureDownloadCopyCreationDate (SecureDownloadRef downloadRef, CFDateRef* date)
{
	API_BEGIN

	Required (downloadRef);
	Download* d = (Download*) downloadRef;
	Required (date) = d->CopyDate ();

	API_END
}
OSStatus SecureDownloadCopyName (SecureDownloadRef downloadRef, CFStringRef* name)
{
	API_BEGIN

	Required (downloadRef);
	Download* d = (Download*) downloadRef;
	Required (name) = d->CopyName ();

	API_END
}
OSStatus SecureDownloadCopyURLs (SecureDownloadRef downloadRef, CFArrayRef* urls)
{
	API_BEGIN

	Required (downloadRef);
	Download* d = (Download*) downloadRef;
	Required (urls) = d->CopyURLs ();

	API_END
}
OSStatus SecureDownloadGetDownloadSize (SecureDownloadRef downloadRef, SInt64* size)
{
	API_BEGIN

	Required (downloadRef);
	Download* d = (Download*) downloadRef;
	Required (size) = d->GetDownloadSize ();

	API_END
}
Exemplo n.º 17
0
    int Download::progressUpdate(void* inObject, double inTotalToDownload,
        double inNowDownloaded, double inTotalToUpload, double inNowUploaded)
    {
        Download* d = static_cast<Download*>(inObject);

        if (inNowDownloaded >= 0.0 && inTotalToDownload > 0.0)
            d->mProgress = inNowDownloaded / inTotalToDownload;

        return d->stopRequested();
    }
Exemplo n.º 18
0
void DownloadManager::addDownload(QString url, int type, int page, QString fileName)
{
    Download *newDownload = new Download();
    newDownload->setType(type);
    newDownload->setFileName(fileName);
    newDownload->setUrl(url);
    newDownload->setPage(page);
    queue.enqueue(newDownload);
    updateDownloads();
}
Exemplo n.º 19
0
void DownloadManager::abortDownload(const string& aTarget) {
	Lock l(cs);
	for(Download::Iter i = downloads.begin(); i != downloads.end(); ++i) {
		Download* d = *i;
		if(d->getTarget() == aTarget) {
			dcassert(d->getUserConnection() != NULL);
			d->getUserConnection()->disconnect();
			break;
		}
	}
}
Exemplo n.º 20
0
void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef cfURLDownloadRef, CFStringRef objectName, const void* clientInfo)
{ 
    Download* download = downloadFromClientInfo(clientInfo);
    bool allowOverwrite;
    String destination = download->decideDestinationWithSuggestedFilename(objectName, allowOverwrite);
    if (destination.isNull())
        return;

    RetainPtr<CFStringRef> cfPath(AdoptCF, CFStringCreateWithCharactersNoCopy(0, reinterpret_cast<const UniChar*>(destination.characters()), destination.length(), kCFAllocatorNull));
    RetainPtr<CFURLRef> pathURL(AdoptCF, CFURLCreateWithFileSystemPath(0, cfPath.get(), kCFURLWindowsPathStyle, false));
    CFURLDownloadSetDestination(cfURLDownloadRef, pathURL.get(), allowOverwrite);
}
Exemplo n.º 21
0
void DownloadManager::on(TimerManagerListener::Second, uint64_t aTick) noexcept {
    typedef vector<pair<string, UserPtr> > TargetList;
    TargetList dropTargets;

    {
        Lock l(cs);

        DownloadList tickList;
        // Tick each ongoing download
        for(DownloadList::iterator i = downloads.begin(); i != downloads.end(); ++i) {
            if((*i)->getPos() > 0) {
                tickList.push_back(*i);
                (*i)->tick();
            }
        }

        if(tickList.size() > 0)
            fire(DownloadManagerListener::Tick(), tickList);


        // Automatically remove or disconnect slow sources
        if((uint32_t)(aTick / 1000) % SETTING(AUTODROP_INTERVAL) == 0) {
            for(DownloadList::iterator i = downloads.begin(); i != downloads.end(); ++i) {
                Download* d = *i;
                uint64_t timeElapsed = aTick - d->getStart();
                uint64_t timeInactive = aTick - d->getUserConnection().getLastActivity();
                uint64_t bytesDownloaded = d->getPos();
                bool timeElapsedOk = timeElapsed >= (uint32_t)SETTING(AUTODROP_ELAPSED) * 1000;
                bool timeInactiveOk = timeInactive <= (uint32_t)SETTING(AUTODROP_INACTIVITY) * 1000;
                bool speedTooLow = timeElapsedOk && timeInactiveOk && bytesDownloaded > 0 ?
                    bytesDownloaded / timeElapsed * 1000 < (uint32_t)SETTING(AUTODROP_SPEED) : false;
                bool isUserList = d->getType() == Transfer::TYPE_FULL_LIST;
                bool onlineSourcesOk = isUserList ?
                    true : QueueManager::getInstance()->countOnlineSources(d->getPath()) >= SETTING(AUTODROP_MINSOURCES);
                bool filesizeOk = !isUserList && d->getSize() >= ((int64_t)SETTING(AUTODROP_FILESIZE)) * 1024;
                bool dropIt = (isUserList && BOOLSETTING(AUTODROP_FILELISTS)) ||
                    (filesizeOk && BOOLSETTING(AUTODROP_ALL));
                if(speedTooLow && onlineSourcesOk && dropIt) {
                    if(BOOLSETTING(AUTODROP_DISCONNECT) && isUserList) {
                        d->getUserConnection().disconnect();
                    } else {
                        dropTargets.push_back(make_pair(d->getPath(), d->getUser()));
                    }
                }
            }
        }
    }
    for(TargetList::iterator i = dropTargets.begin(); i != dropTargets.end(); ++i) {
        QueueManager::getInstance()->removeSource(i->first, i->second, QueueItem::Source::FLAG_SLOW_SOURCE);
    }
}
Exemplo n.º 22
0
void DownloadQueue::remove(int position)
{
   this->updateMarkersRemove(position);

   Download* download = (*this)[position];

   if (FileDownload* fileDownload = dynamic_cast<FileDownload*>(download))
      this->downloadsSortedByTime.remove(fileDownload->getLastTimeGetAllUnfinishedChunks(), fileDownload);

   this->downloadsIndexedByName.remove(download->getLocalEntry().name(), download);
   this->downloadsIndexedBySourcePeer.remove(download->getPeerSource(), download);
   this->downloads.removeAt(position);
   this->erroneousDownloads.removeOne(download);
}
Exemplo n.º 23
0
// TimeSlice Interface
void
dmz::JsExtV8HTTPCurl::update_time_slice (const Float64 DeltaTime) {

   v8::Context::Scope cscope (_v8Context);
   v8::HandleScope scope;

   if (_dlList) {

      Download *prev (0);
      Download *current (_dlList);

      while (current) {

         if (current->done ()) {

            if (prev) { prev->next = current->next; }
            else { _dlList = current->next; }

            Download *tmp = current;

            current = current->next;

            delete tmp; tmp = 0;
         }
         else { current = current->next; }
      }
   }

   if (_ulList) {

      Upload *prev (0);
      Upload *current (_ulList);

      while (current) {

         if (current->done ()) {

            if (prev) { prev->next = current->next; }
            else { _ulList = current->next; }

            Upload *tmp = current;

            current = current->next;

            delete tmp; tmp = 0;
         }
         else { current = current->next; }
      }
   }
}
Exemplo n.º 24
0
int Download::downloadProgress(void *clientp, double dltotal, double dlnow,
                     double ultotal, double ulnow)
{
    Download *d = reinterpret_cast<Download*>(clientp);

    if (d->mOptions.cancel)
    {
        return d->mUpdateFunction(d->mPtr, DOWNLOAD_STATUS_CANCELLED, (size_t) dltotal,
                                  (size_t) dlnow);
        return -5;
    }

    return d->mUpdateFunction(d->mPtr, DOWNLOAD_STATUS_IDLE, (size_t) dltotal, (size_t) dlnow);
}
Exemplo n.º 25
0
void DownloadManager::on(Command::SND, UserConnection* aSource, const Command& cmd) throw() {
	int64_t bytes = Util::toInt64(cmd.getParam(3));

	if(cmd.getParam(0) == "tthl") {
		if(aSource->getState() != UserConnection::STATE_TREE) {
			dcdebug("DM::SND Bad state, ignoring\n");
			return;
		}
		Download* d = aSource->getDownload();
		d->setFile(new TreeOutputStream(d->getOldDownload()->getTigerTree()));
		d->setSize(bytes);
		d->setPos(0);
		dcassert(d->isSet(Download::FLAG_TREE_DOWNLOAD));
		aSource->setState(UserConnection::STATE_DONE);

		if(cmd.hasFlag("ZL", 4)) {
			d->setFile(new FilteredOutputStream<UnZFilter, true>(d->getFile()));
		}

		aSource->setDataMode();
	} else if(cmd.getParam(0) == "file") {
		if(aSource->getState() != UserConnection::STATE_FILELENGTH) {
			dcdebug("DM::onFileLength Bad state, ignoring\n");
			return;
		}

		if(prepareFile(aSource, (bytes == -1) ? -1 : aSource->getDownload()->getPos() + bytes)) {
			aSource->setDataMode();
		}
	}
}
Exemplo n.º 26
0
QList<IDownload*> DownloadManager::getDownloads() const
{
   QList<IDownload*> listDownloads;

   // TODO: very heavy!
   for (int i = 0; i < this->downloadQueue.size(); i++)
   {
      Download* download = this->downloadQueue[i];
      if (download->getStatus() != DELETED)
         listDownloads << download;
   }

   return listDownloads;
}
Exemplo n.º 27
0
bool DownloadManagerImpl::removeDownloadById(int downloadId)
{
    QMutexLocker locker(&m_mutexLocker);
    for (int i = 0; i < m_downloadQueue.count(); i++) {
        Download *download = m_downloadQueue.at(i);
        if (download == 0)
            continue;
        if (download->getId() == downloadId) {
            m_downloadQueue.removeAt(i);
            // Free memory
            download->stop();
            download->deleteLater();
            return true;
        }
    }

    for (int i = 0; i < m_downloadingList.count(); i++) {
        Download *download = m_downloadingList.at(i);
        if (download == 0)
            continue;
        if (download->getId() == downloadId) {
            m_downloadingList.removeAt(i);
            // Free memory
            download->stop();
            download->deleteLater();
            return true;
        }
    }

    return false;
}
OSStatus SecureDownloadCreateWithTicket (CFDataRef ticket,
										 SecureDownloadTrustSetupCallback setup,
										 void* setupContext,
										 SecureDownloadTrustEvaluateCallback evaluate,
										 void* evaluateContext,
										 SecureDownloadRef* downloadRef)
{
	API_BEGIN
	
	Download* sd = new Download ();
	sd->Initialize (ticket, setup, setupContext, evaluate, evaluateContext);
	Required (downloadRef) = sd;

	API_END
}
Exemplo n.º 29
0
QList<IDownload*> DownloadManager::getDownloads() const
{
   QList<IDownload*> listDownloads;

   // A bit heavy . . .
   listDownloads.reserve(this->downloadQueue.size());
   for (int i = 0; i < this->downloadQueue.size(); i++)
   {
      Download* download = this->downloadQueue[i];
      if (download->getStatus() != DELETED)
         listDownloads << download;
   }

   return listDownloads;
}
Exemplo n.º 30
0
bool DownloadManagerImpl::isDownloadExistingInQueue(int contactId)
{
    for (int i = 0; i < m_downloadQueue.count(); i++) {
        Download *download = m_downloadQueue.at(i);
        if (download == 0)
            continue;
        Contact *contact = download->getContact();
        if (contact == 0)
            continue;
        if (contact->getId() == contactId)
            return true;
    }

    return false;
}