Beispiel #1
0
void ScriptService::getImpl(const QByteArray& operation, const APIParameters &parameters, APIServiceResponse &response)
{
	if(operation=="list")
	{
		//list all scripts, this should be thread safe
		QStringList allScripts = scriptMgr->getScriptList();

		response.writeJSON(QJsonDocument(QJsonArray::fromStringList(allScripts)));
	}
	else if (operation == "info")
	{
		if(parameters.contains("id"))
		{
			//retrieve detail about a single script
			QString scriptId = QString::fromUtf8(parameters.value("id"));

			if(parameters.contains("html"))
			{
				QString html = scriptMgr->getHtmlDescription(scriptId, false);
				response.setHeader("Content-Type","text/html; charset=UTF-8");
				response.setData(wrapHtml(html, scriptId).toUtf8());
				return;
			}

			QJsonObject obj;
			//if the script name is wrong, this will return empty strings
			obj.insert("id",scriptId);
			QString d = scriptMgr->getName(scriptId).trimmed();
			obj.insert("name",d);
			obj.insert("name_localized", StelTranslator::globalTranslator->qtranslate(d));
			d = scriptMgr->getDescription(scriptId).trimmed();
			obj.insert("description",d);
			obj.insert("description_localized", StelTranslator::globalTranslator->qtranslate(d));
			obj.insert("author",scriptMgr->getAuthor(scriptId).trimmed());
			obj.insert("license",scriptMgr->getLicense(scriptId).trimmed());
			//shortcut often causes a large delay because the whole file gets searched, and it is usually missing, so we ignore it
			//obj.insert("shortcut",scriptMgr->getShortcut(scriptId));

			response.writeJSON(QJsonDocument(obj));
		}
		else
		{
			response.writeRequestError("need parameter: id");
		}
	}
	else if(operation == "status")
	{
		//generic script status
		QJsonObject obj;
		obj.insert("scriptIsRunning",scriptMgr->scriptIsRunning());
		obj.insert("runningScriptId",scriptMgr->runningScriptId());

		response.writeJSON(QJsonDocument(obj));
	}
	else
	{
		//TODO some sort of service description?
		response.writeRequestError("unsupported operation. GET: list,info,status POST: run,stop");
	}
}
void LocationService::getImpl(const QByteArray& operation, const APIParameters &parameters, APIServiceResponse &response)
{
	if(operation=="list")
	{
		//for now, this does not return location objects (which would require a much larger data transfer), but only the location strings
		//same as in the LocationDialog list

		//TODO not fully thread safe
		QJsonArray list = QJsonArray::fromStringList(QStringList(locMgr->getAllMap().keys()));

		response.writeJSON(QJsonDocument(list));
	}
	else if(operation == "countrylist")
	{
		const StelTranslator& trans = *StelTranslator::globalTranslator;

		QStringList allCountries = StelApp::getInstance().getLocaleMgr().getAllCountryNames();
		QJsonArray list;
		foreach(QString str, allCountries)
		{
			QJsonObject obj;
			obj.insert("name",str);
			obj.insert("name_i18n",trans.qtranslate(str));
			list.append(obj);
		}

		response.writeJSON(QJsonDocument(list));
	}
void LocationSearchService::getImpl(const QByteArray& operation, const APIParameters &parameters, APIServiceResponse &response)
{
	if(operation=="search")
	{
		//parameter must be named "term" to be compatible with jQuery UI autocomplete without further JS code
		QString term = QString::fromUtf8(parameters.value("term"));

		if(term.isEmpty())
		{
			response.writeRequestError("needs non-empty 'term' parameter");
			return;
		}

		//the filtering in the app is provided by QSortFilterProxyModel in the view
		//we dont have that luxury, but we make sure the filtering happens in the separate HTTP thread
		locMgrMutex.lock();
		LocationMap allItems = locMgr.getAllMap();
		locMgrMutex.unlock();

		QJsonArray results;
		const QList<QString>& list = allItems.keys();

		//use a regexp in wildcard mode, the app does the same
		QRegExp exp(term,Qt::CaseInsensitive, QRegExp::Wildcard);

		for(QList<QString>::const_iterator it = list.begin();it!=list.end();++it)
		{
			if(it->contains(exp))
				results.append(*it);
		}

		response.writeJSON(QJsonDocument(results));
	}
	else if(operation=="nearby")
	{
		QString sPlanet = QString::fromUtf8(parameters.value("planet"));
		QString sLatitude = QString::fromUtf8(parameters.value("latitude"));
		QString sLongitude = QString::fromUtf8(parameters.value("longitude"));
		QString sRadius = QString::fromUtf8(parameters.value("radius"));

		float latitude = sLatitude.toFloat();
		float longitude = sLongitude.toFloat();
		float radius = sRadius.toFloat();

		locMgrMutex.lock();
		LocationMap results = locMgr.pickLocationsNearby(sPlanet,longitude,latitude,radius);
		locMgrMutex.unlock();

		response.writeJSON(QJsonDocument(QJsonArray::fromStringList(results.keys())));
	}
	else
	{
		//TODO some sort of service description?
		response.writeRequestError("unsupported operation. GET: search,nearby");
	}
}