1 /*
2  * Stellarium Remote Control plugin
3  * Copyright (C) 2015 Florian Schaukowitsch
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
18  */
19 
20 #include "ViewService.hpp"
21 
22 #include "StelApp.hpp"
23 #include "StelCore.hpp"
24 #include "StelFileMgr.hpp"
25 #include "StelModuleMgr.hpp"
26 #include "StelSkyCultureMgr.hpp"
27 #include "StelTranslator.hpp"
28 #include "LandscapeMgr.hpp"
29 
30 #include <QFile>
31 #include <QMimeDatabase>
32 #include <QJsonArray>
33 #include <QJsonDocument>
34 
ViewService(QObject * parent)35 ViewService::ViewService(QObject *parent) : AbstractAPIService(parent)
36 {
37 	core = StelApp::getInstance().getCore();
38 	lsMgr = GETSTELMODULE(LandscapeMgr);
39 	skyCulMgr = &StelApp::getInstance().getSkyCultureMgr();
40 }
41 
get(const QByteArray & operation,const APIParameters & parameters,APIServiceResponse & response)42 void ViewService::get(const QByteArray &operation, const APIParameters &parameters, APIServiceResponse &response)
43 {
44 	Q_UNUSED(parameters);
45 
46 	if(operation=="listlandscape")
47 	{
48 		//list all installed landscapes
49 
50 		QMap<QString,QString> map = lsMgr->getNameToDirMap();
51 		QJsonObject obj;
52 
53 		QMapIterator<QString,QString> it(map);
54 		while(it.hasNext())
55 		{
56 			it.next();
57 			//value is the id here, key is name
58 			obj.insert(it.value(), StelTranslator::globalTranslator->qtranslate(it.key()));
59 		}
60 
61 		response.writeJSON(QJsonDocument(obj));
62 	}
63 	else if (operation.startsWith("landscapedescription/"))
64 	{
65 		int startidx = operation.indexOf('/');
66 		//get the path after the name and map it to the landscapes' directory
67 		QByteArray path = operation.mid(startidx+1);
68 
69 		if(path.isEmpty())
70 		{
71 			//return the HTML description of the current landscape
72 			QString str = lsMgr->getCurrentLandscapeHtmlDescription();
73 			response.writeWrappedHTML(str, lsMgr->getCurrentLandscapeName());
74 		}
75 		else
76 		{
77 			//map path to landscape dir and return files
78 			QString baseFolder = StelFileMgr::findFile("landscapes/" + lsMgr->getCurrentLandscapeID());
79 			QString pathString = baseFolder + '/' + QString::fromUtf8(path);
80 
81 			response.writeFile(pathString);
82 		}
83 	}
84 	else if (operation=="listskyculture")
85 	{
86 		//list installed skycultures
87 		QMap<QString, StelSkyCulture> map = skyCulMgr->getDirToNameMap();
88 
89 		QJsonObject obj;
90 		QMapIterator<QString,StelSkyCulture> it(map);
91 		while(it.hasNext())
92 		{
93 			it.next();
94 			obj.insert(it.key(),StelTranslator::globalTranslator->qtranslate(it.value().englishName));
95 		}
96 
97 		response.writeJSON(QJsonDocument(obj));
98 	}
99 	else if (operation.startsWith("skyculturedescription/"))
100 	{
101 		int startidx = operation.indexOf('/');
102 		//get the path after the name and map it to the sky cultures' directory
103 		QByteArray path = operation.mid(startidx+1);
104 
105 		if(path.isEmpty())
106 		{
107 			//return the HTML description of the current landscape
108 			QString str = skyCulMgr->getCurrentSkyCultureHtmlDescription();
109 			response.writeWrappedHTML(str, skyCulMgr->getCurrentSkyCultureNameI18());
110 		}
111 		else
112 		{
113 			//map path to sky cultures dir and return files
114 			QString baseFolder = StelFileMgr::findFile("skycultures/" + skyCulMgr->getCurrentSkyCultureID());
115 			QString pathString = baseFolder + '/' + QString::fromUtf8(path);
116 
117 			response.writeFile(pathString);
118 		}
119 	}
120 	else if (operation=="listprojection")
121 	{
122 		//list projection types
123 		QStringList keys = core->getAllProjectionTypeKeys();
124 
125 		QJsonObject obj;
126 
127 		for (auto str : keys)
128 		{
129 			QString name = core->projectionTypeKeyToNameI18n(str);
130 			obj.insert(str,name);
131 		}
132 
133 		response.writeJSON(QJsonDocument(obj));
134 	}
135 	else if (operation=="projectiondescription")
136 	{
137 		//returns the description of the current projection
138 		QString str = core->getProjection(StelCore::FrameJ2000)->getHtmlSummary();
139 		response.writeWrappedHTML(str, core->getCurrentProjectionNameI18n());
140 	}
141 	else
142 	{
143 		//TODO some sort of service description?
144 		response.writeRequestError("unsupported operation. GET: listlandscape,landscapedescription/,listskyculture,skyculturedescription/,listprojection,projectiondescription");
145 	}
146 }
147