Example #1
0
void ImapSession::DownloadPart(const MojObject& folderId, const MojObject& emailId, const MojObject& partId, const MojRefCountedPtr<DownloadListener>& listener, Command::Priority priority)
{
	MojRefCountedPtr<FetchPartCommand> command(new FetchPartCommand(*this, folderId, emailId, partId, priority));

	// Look for an active command
	MojRefCountedPtr<ImapCommand> activeCommand = FindActiveCommand(command);
	if(activeCommand.get()) {
		MojLogDebug(m_log, "attaching download request to existing active command");

		if(listener.get())
			static_cast<FetchPartCommand*>(activeCommand.get())->AddDownloadListener(listener);

		return;
	}

	// Look for a pending command with equal or higher priority
	// TODO: requeue if a higher priority is needed
	MojRefCountedPtr<ImapCommand> pendingCommand = FindPendingCommand(command);
	if(pendingCommand.get() && pendingCommand->GetPriority() >= priority) {
		MojLogDebug(m_log, "attaching download request to existing pending command");

		if(listener.get())
			static_cast<FetchPartCommand*>(pendingCommand.get())->AddDownloadListener(listener);

		// Poke the queue in case it's not already running
		CheckQueue();
		return;
	}

	// Otherwise, use the new command
	if(listener.get())
		command->AddDownloadListener(listener);

	// If this is a high priority request, set m_recentUserInteraction to true
	if (priority >= Command::HighPriority) {
		m_recentUserInteraction = true;
	}

	m_commandManager->QueueCommand(command, false);
	CheckQueue();
}