示例#1
0
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");
	}
}
void AbstractAPIService::post(const QByteArray &operation, const APIParameters &parameters, const QByteArray &data, APIServiceResponse& response)
{
	Q_UNUSED(operation);
	Q_UNUSED(parameters);
	Q_UNUSED(data);

	response.setStatus(405,"Method Not allowed");
	QString str(QStringLiteral("Method POST not allowed for service %2"));
	response.setData(str.arg(getPath()).toLatin1());
}
示例#4
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");
	}
}
示例#5
0
void ScriptService::postImpl(const QByteArray& operation, const APIParameters &parameters, const QByteArray &data, APIServiceResponse &response)
{
	Q_UNUSED(data);

	if(operation=="run")
	{
		//retrieve detail about a single script
		QString scriptId = QString::fromUtf8(parameters.value("id"));

		if(scriptMgr->scriptIsRunning())
		{
			response.setData("error: a script is already running");
			return;
		}

		QString script;
		//Prepare the script. Through a refactor, this is separate from actually running the script,
		//and can be done in the HTTP thread. It can also notify the caller of an invalid script ID.
		bool ret = scriptMgr->prepareScript(script,scriptId);

		if(!ret)
		{
			response.setData("error: could not prepare script, wrong id?");
			return;
		}

		//ret = scriptMgr->runScript(scriptId);
		//we may be in another thread! for the actual execution of the script, we need to invoke the event queue
		//we can not use blocking connection here because runPreprocessedScript is blocking!
		//there is also no way to check if the script was actually started except for polling some time later
		QMetaObject::invokeMethod(scriptMgr,"runPreprocessedScript",
					  Qt::QueuedConnection,Q_ARG(QString,script),Q_ARG(QString,scriptId));
		response.setData("ok");
	}
	else if(operation=="direct")
	{
		//directly runs the given script code
		QString code = QString::fromUtf8(parameters.value("code"));
		QString useIncludes = QString::fromUtf8(parameters.value("useIncludes"));

		if(code.isEmpty())
		{
			response.writeRequestError("need parameter: code");
		}
		else
		{
			if(scriptMgr->scriptIsRunning())
			{
				response.setData("error: a script is already running");
				return;
			}

			//use QVariant logic to convert to bool
			bool bUseIncludes = QVariant(useIncludes).toBool();

			//also requires queued connection
			QMetaObject::invokeMethod(scriptMgr,"runScriptDirect", Qt::QueuedConnection,
						  Q_ARG(QString,code),
						  Q_ARG(QString,bUseIncludes ? QStringLiteral("") : QString() ));
			response.setData("ok");
		}
	}
	else if(operation=="stop")
	{
		//scriptMgr->stopScript();
		QMetaObject::invokeMethod(scriptMgr,"stopScript",SERVICE_DEFAULT_INVOKETYPE);

		response.setData("ok");
	}
	else
	{
		//TODO some sort of service description?
		response.writeRequestError("unsupported operation. GET: list,info,status POST: run,direct,stop");
	}
}