int QueueBundleUtils::compareBundles(const BundlePtr& a, const BundlePtr& b, int aPropertyName) noexcept {
		switch (aPropertyName) {
		case PROP_NAME: {
			COMPARE_TYPE(a, b);

			return Util::stricmp(a->getName(), b->getName());
		}
		case PROP_TYPE: {
			COMPARE_TYPE(a, b);
			
			if (!a->isFileBundle() && !b->isFileBundle()) {
				// Directory bundles
				RLock l(QueueManager::getInstance()->getCS());
				auto dirsA = QueueManager::getInstance()->bundleQueue.getDirectoryCount(a);
				auto dirsB = QueueManager::getInstance()->bundleQueue.getDirectoryCount(b);

				if (dirsA != dirsB) {
					return compare(dirsA, dirsB);
				}

				auto filesA = a->getQueueItems().size() + a->getFinishedFiles().size();
				auto filesB = b->getQueueItems().size() + b->getFinishedFiles().size();

				return compare(filesA, filesB);
			}

			return Util::stricmp(Util::getFileExt(a->getTarget()), Util::getFileExt(b->getTarget()));
		}
		case PROP_PRIORITY: {
			COMPARE_FINISHED(a, b);
			if (a->isFinished() != b->isFinished()) {
				return a->isFinished() ? 1 : -1;
			}

			return compare(static_cast<int>(a->getPriority()), static_cast<int>(b->getPriority()));
		}
		case PROP_STATUS: {
			if (a->getStatus() != b->getStatus()) {
				return compare(a->getStatus(),  b->getStatus());
			}

			return compare(
				a->getPercentage(a->getDownloadedBytes()), 
				b->getPercentage(b->getDownloadedBytes())
			);
		}
		case PROP_SOURCES: {
			COMPARE_FINISHED(a, b);

			auto countsA = QueueManager::getInstance()->getSourceCount(a);
			auto countsB = QueueManager::getInstance()->getSourceCount(b);

			return QueueItemBase::SourceCount::compare(countsA, countsB);
		}
		default:
			dcassert(0);
		}

		return 0;
	}
示例#2
0
void BundleQueue::removeBundle(BundlePtr& aBundle) noexcept{
	if (aBundle->getStatus() == Bundle::STATUS_NEW) {
		return;
	}

	{
		auto infoPtr = getPathInfos(aBundle->getTarget());
		if (infoPtr) {
			auto pathInfos = *infoPtr;
			for (const auto& p : pathInfos) {
				removePathInfo(p);
			}
		}
	}

	dcassert(aBundle->getFinishedFiles().empty());
	dcassert(aBundle->getQueueItems().empty());

	removeSearchPrio(aBundle);
	bundles.erase(aBundle->getToken());

	dcassert(bundlePaths.size() == static_cast<size_t>(boost::count_if(bundles | map_values, [](const BundlePtr& b) { return !b->isFileBundle(); })));

	aBundle->deleteXmlFile();
}
示例#3
0
	std::string QueueUtils::formatBundleStatus(const BundlePtr& aBundle) noexcept {
		auto getPercentage = [&] {
			return aBundle->getSize() > 0 ? (double)aBundle->getDownloadedBytes() *100.0 / (double)aBundle->getSize() : 0;
		};

		switch (aBundle->getStatus()) {
		case Bundle::STATUS_NEW:
		case Bundle::STATUS_QUEUED: {
			if (aBundle->isPausedPrio())
				return STRING_F(PAUSED_PCT, getPercentage());

			if (aBundle->getSpeed() > 0) { // Bundle->isRunning() ?
				return STRING_F(RUNNING_PCT, getPercentage());
			}
			else {
				return STRING_F(WAITING_PCT, getPercentage());
			}
		}
		case Bundle::STATUS_RECHECK: return STRING(RECHECKING);
		case Bundle::STATUS_DOWNLOADED: return STRING(MOVING);
		case Bundle::STATUS_MOVED: return STRING(DOWNLOADED);
		case Bundle::STATUS_DOWNLOAD_FAILED:
		case Bundle::STATUS_FAILED_MISSING:
		case Bundle::STATUS_SHARING_FAILED: return aBundle->getLastError();
		case Bundle::STATUS_FINISHED: return STRING(FINISHED);
		case Bundle::STATUS_HASHING: return STRING(HASHING);
		case Bundle::STATUS_HASH_FAILED: return STRING(HASH_FAILED);
		case Bundle::STATUS_HASHED: return STRING(HASHING_FINISHED);
		case Bundle::STATUS_SHARED: return STRING(SHARED);
		default:
			return Util::emptyString;
		}
	}
示例#4
0
	json QueueUtils::serializeBundleProperty(const BundlePtr& aBundle, int aPropertyName) noexcept {
		switch (aPropertyName) {
		case QueueApi::PROP_SOURCES:
		{
			int total = 0, online = 0;
			std::string str;
			getBundleSourceInfo(aBundle, online, total, str);

			return {
				{ "online", online },
				{ "total", total },
				{ "str", str },
			};
		}

		case QueueApi::PROP_STATUS:
		{
			return{
				{ "id", aBundle->getStatus() },
				{ "failed", aBundle->isFailed() },
				{ "str", formatBundleStatus(aBundle) },
			};
		}

		case QueueApi::PROP_TYPE:
		{
			if (aBundle->isFileBundle()) {
				return Serializer::serializeFileType(aBundle->getTarget());
			} else {
				size_t files = 0;
				size_t folders = 0;

				{
					RLock l(QueueManager::getInstance()->getCS());
					files = aBundle->getQueueItems().size() + aBundle->getFinishedFiles().size();
					folders = aBundle->getDirectories().size();
				}

				return Serializer::serializeFolderType(files, folders);
			}
		}
		case QueueApi::PROP_PRIORITY: {
			return serializePriority(*aBundle.get());
		}
		}

		dcassert(0);
		return json();
	}
	string QueueBundleUtils::formatStatusId(const BundlePtr& aBundle) noexcept {
		switch (aBundle->getStatus()) {
			case Bundle::STATUS_NEW: return "new";
			case Bundle::STATUS_QUEUED: return "queued";
			case Bundle::STATUS_RECHECK: return "recheck";
			case Bundle::STATUS_DOWNLOADED: return "downloaded";
			case Bundle::STATUS_MOVED: return "moved";
			case Bundle::STATUS_DOWNLOAD_FAILED: return "download_failed";
			case Bundle::STATUS_FAILED_MISSING: return "scan_failed_files_missing";
			case Bundle::STATUS_SHARING_FAILED: return "scan_failed";
			case Bundle::STATUS_FINISHED: return "finished";
			case Bundle::STATUS_HASHING: return "hashing";
			case Bundle::STATUS_HASH_FAILED: return "hash_failed";
			case Bundle::STATUS_HASHED: return "hashed";
			case Bundle::STATUS_SHARED: return "shared";
		}

		dcassert(0);
		return Util::emptyString;
	}
	json QueueBundleUtils::serializeBundleProperty(const BundlePtr& aBundle, int aPropertyName) noexcept {
		switch (aPropertyName) {
		case PROP_SOURCES:
		{
			auto c = QueueManager::getInstance()->getSourceCount(aBundle);
			return Serializer::serializeSourceCount(c);
		}

		case PROP_STATUS:
		{
			return{
				{ "id", formatStatusId(aBundle) },
				{ "failed", aBundle->isFailed() },
				{ "finished", aBundle->getStatus() >= Bundle::STATUS_MOVED },
				{ "str", formatDisplayStatus(aBundle) },
			};
		}

		case PROP_TYPE:
		{
			if (aBundle->isFileBundle()) {
				return Serializer::serializeFileType(aBundle->getTarget());
			} else {
				size_t files = 0, folders = 0;
				QueueManager::getInstance()->getBundleContent(aBundle, files, folders);

				return Serializer::serializeFolderType(static_cast<int>(files), static_cast<int>(folders));
			}
		}
		case PROP_PRIORITY: {
			return Serializer::serializePriority(*aBundle.get());
		}
		}

		dcassert(0);
		return nullptr;
	}
示例#7
0
	int QueueUtils::compareBundles(const BundlePtr& a, const BundlePtr& b, int aPropertyName) noexcept {
		switch (aPropertyName) {
		case QueueApi::PROP_NAME: {
			if (a->isFileBundle() && !b->isFileBundle()) return 1;
			if (!a->isFileBundle() && b->isFileBundle()) return -1;

			return Util::stricmp(a->getName(), b->getName());
		}
		case QueueApi::PROP_TYPE: {
			if (a->isFileBundle() != b->isFileBundle()) {
				// Directories go first
				return a->isFileBundle() ? 1 : -1;
			} 
			
			if (!a->isFileBundle() && !b->isFileBundle()) {
				// Directory bundles
				RLock l(QueueManager::getInstance()->getCS());
				auto dirsA = a->getDirectories().size();
				auto dirsB = a->getDirectories().size();
				if (dirsA != dirsB) {
					return compare(dirsA, dirsB);
				}

				auto filesA = a->getQueueItems().size() + a->getFinishedFiles().size();
				auto filesB = b->getQueueItems().size() + b->getFinishedFiles().size();

				return compare(filesA, filesB);
			}

			return Util::stricmp(Util::getFileExt(a->getTarget()), Util::getFileExt(b->getTarget()));
		}
		case QueueApi::PROP_PRIORITY: {
			if (a->isFinished() != b->isFinished()) {
				return a->isFinished() ? 1 : -1;
			}

			return compare(static_cast<int>(a->getPriority()), static_cast<int>(b->getPriority()));
		}
		case QueueApi::PROP_STATUS: {
			if (a->getStatus() != b->getStatus()) {
				return compare(a->getStatus(),  b->getStatus());
			}

			return compare(a->getDownloadedBytes(), b->getDownloadedBytes());
		}
		case QueueApi::PROP_SOURCES: {
			if (a->isFinished() != b->isFinished()) {
				return a->isFinished() ? 1 : -1;
			}

			int onlineA = 0, totalA = 0, onlineB = 0, totalB = 0;
			std::string str;
			getBundleSourceInfo(a, onlineA, totalA, str);
			getBundleSourceInfo(b, onlineB, totalB, str);

			if (onlineA != onlineB) {
				return compare(onlineA, onlineB);
			}

			return compare(totalA, totalB);
		}
		default:
			dcassert(0);
		}

		return 0;
	}