void AsyncLoader::uploadFileFinished(QString id)
{
	if (id == "asyncloader")
	{
		OnlineDataNode *dataNode = NULL;

		try
		{
			string path = fq(_currentEvent->path());

			if (_currentEvent->IsOnlineNode())
			{
				dataNode = _odm->getActionDataNode(id);

				if (dataNode->error())
					throw runtime_error(fq(dataNode->errorMessage()));

				path = fq(_odm->getLocalPath(_currentEvent->path()));

				_currentEvent->setPath(dataNode->path());
			}

			_currentPackage->initalMD5 = fq(fileChecksum(tq(path), QCryptographicHash::Md5));

			if (dataNode != NULL)
				_currentPackage->id = fq(dataNode->nodeId());
			else
				_currentPackage->id = path;

			_currentEvent->setComplete();

			if (dataNode != NULL)
				_odm->deleteActionDataNode(id);
		}
		catch (runtime_error e)
		{
			if (dataNode != NULL)
				_odm->deleteActionDataNode(id);
			_currentEvent->setComplete(false, e.what());
		}
		catch (exception e)
		{
			if (dataNode != NULL)
				_odm->deleteActionDataNode(id);
			_currentEvent->setComplete(false, e.what());
		}
	}
}
void AsyncLoader::loadPackage(QString id)
{
	if (id == "asyncloader")
	{
		OnlineDataNode *dataNode = NULL;

		try
		{
			string path = fq(_currentEvent->path());

			if (_currentEvent->IsOnlineNode())
			{
				//Find file extension in the OSF
				string extension=".jasp"; //default
				QString qpath(path.c_str());
				int slashPos = qpath.lastIndexOf("/");
				int dotPos = qpath.lastIndexOf('.');
				if (dotPos != -1 && dotPos > slashPos)
				{
					QString ext = qpath.mid(dotPos);
					extension=ext.toStdString();
				}

				dataNode = _odm->getActionDataNode(id);

				if (dataNode != NULL && dataNode->error())
					throw runtime_error(fq(dataNode->errorMessage()));

				//Generated local path has no extension
				path = fq(_odm->getLocalPath(_currentEvent->path()));

				//loadPackage argument extension determines type
				_loader.loadPackage(_currentPackage, path, extension, boost::bind(&AsyncLoader::progressHandler, this, _1, _2));
			}
			else
				_loader.loadPackage(_currentPackage, path, "", boost::bind(&AsyncLoader::progressHandler, this, _1, _2));

			QString calcMD5 = fileChecksum(tq(path), QCryptographicHash::Md5);

			if (dataNode != NULL)
			{
				if (calcMD5 != dataNode->md5().toLower())
					throw runtime_error("The securtiy check of the downloaded file has failed.\n\nLoading has been cancelled due to an MD5 mismatch.");
			}

			_currentPackage->initalMD5 = fq(calcMD5);

			if (dataNode != NULL)
			{
				_currentPackage->id = fq(dataNode->nodeId());
				_currentEvent->setPath(dataNode->path());
			}
			else
				_currentPackage->id = path;

			_currentEvent->setComplete();

			if (dataNode != NULL)
				_odm->deleteActionDataNode(id);
		}
		catch (runtime_error e)
		{
			if (dataNode != NULL)
				_odm->deleteActionDataNode(id);
			_currentEvent->setComplete(false, e.what());
		}
		catch (exception e)
		{
			if (dataNode != NULL)
				_odm->deleteActionDataNode(id);
			_currentEvent->setComplete(false, e.what());
		}
	}
}