1 /**********************************************************************************************
2     Copyright (C) 2017 Norbert Truchsess <norbert.truchsess@t-online.de>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #ifndef CROUTERBROUTERSETUP_H
20 #define CROUTERBROUTERSETUP_H
21 
22 #include "units/IUnit.h"
23 #include <QtCore>
24 
25 class QJSValue;
26 class QNetworkAccessManager;
27 class QNetworkReply;
28 class QWebEnginePage;
29 
30 class CRouterBRouterSetup : public QObject
31 {
32     Q_OBJECT
33 public:
34     CRouterBRouterSetup(QObject* parent);
35     ~CRouterBRouterSetup();
36 
37     enum mode_e { eModeLocal, eModeOnline, eModeIllegal };
38     struct tile_t { QPoint tile; QDateTime date; qreal size; };
39 
40     void load();
41     void save();
42 
43     void resetAll();
resetInstallMode()44     void resetInstallMode() { installMode = defaultInstallMode; }
resetOnlineConfigUrl()45     void resetOnlineConfigUrl() { expertConfigUrl = defaultConfigUrl; }
resetOnlineServiceUrl()46     void resetOnlineServiceUrl() { onlineServiceUrl = defaultOnlineServiceUrl; }
resetOnlineProfilesUrl()47     void resetOnlineProfilesUrl() { onlineProfilesUrl = defaultOnlineProfilesUrl; }
resetLocalProfileDir()48     void resetLocalProfileDir() { localProfileDir = defaultLocalProfileDir; }
resetLocalCustomProfileDir()49     void resetLocalCustomProfileDir() { localCustomProfileDir = defaultLocalCustomProfileDir; }
resetLocalSegmentsDir()50     void resetLocalSegmentsDir() { localSegmentsDir = defaultLocalSegmentsDir; }
resetLocalHost()51     void resetLocalHost() { localHost = defaultLocalHost; }
resetLocalPort()52     void resetLocalPort() { localPort = defaultLocalPort; }
resetLocalBindLocalonly()53     void resetLocalBindLocalonly() { localBindLocalonly = defaultLocalBindLocalonly; }
resetLocalNumberThreads()54     void resetLocalNumberThreads() { localNumberThreads = defaultLocalNumberThreads; }
resetLocalMaxRunningTime()55     void resetLocalMaxRunningTime() { localMaxRunningTime = defaultLocalMaxRunningTime; }
resetLocalJavaOpts()56     void resetLocalJavaOpts() { localJavaOpts = defaultLocalJavaOpts; }
resetBinariesUrl()57     void resetBinariesUrl() { expertBinariesUrl = defaultBinariesUrl; }
resetSegmentsUrl()58     void resetSegmentsUrl() { expertSegmentsUrl = defaultSegmentsUrl; }
59 
60     QStringList getProfiles() const;
61 
62     void addProfile(const QString& profile);
63     void deleteProfile(const QString& profile);
64     void profileUp(const QString& profile);
65     void profileDown(const QString& profile);
66 
67     void readLocalProfiles();
68 
69     void loadOnlineConfig(bool update) const;
70     void loadOnlineVersion() const;
71     void loadExpertBinariesPage() const;
72     void loadExpertSegmentsPage() const;
73     void loadLocalOnlineProfiles(bool update) const;
74     void displayProfileAsync(const QString& profile);
75     void displayOnlineProfileAsync(const QString& profile) const;
76 
77     QString findJava() const;
78     bool isLocalBRouterInstalled() const;
79     bool isLocalBRouterDefaultDir() const;
80 
81     QUrl getServiceUrl() const;
82     QString getSegmentsUrl() const;
83     QString getBinariesUrl() const;
84     QString getConfigUrl() const;
85 
86     void parseBRouterVersion(const QString& text);
87 
88     void onInvalidSetup();
89 
90 signals:
91     void sigOnlineConfigLoaded();
92     void sigVersionChanged();
93     void sigBinariesPageLoaded();
94     void sigSegmentsPageLoaded();
95     void sigProfilesChanged();
96     void sigDisplayOnlineProfileFinished(const QString profile, const QString content);
97     void sigError(const QString error, const QString details);
98 
99 private slots:
100     void slotOnlineRequestFinished(QNetworkReply* reply);
101     void slotLoadOnlineProfilesRequestFinished(bool ok);
102 
103 private:
104     enum request_e { eTypeConfig, eTypeProfile, eTypeBinariesPage, eTypeSegmentsPage, eTypeVersion };
105     enum profileRequest_e { eProfileInstall, eProfileDisplay };
106 
107     QDir getProfileDir(const mode_e mode) const;
108     void loadOnlineProfileAsync(const QString& profile, const profileRequest_e mode) const;
109     void loadOnlineConfigFinished(QNetworkReply* reply);
110     void loadOnlineVersionFinished(QNetworkReply* reply);
111     void loadOnlineProfileFinished(QNetworkReply* reply);
112     void afterSlotLoadOnlineProfilesRequestFinishedRunJavascript(const QVariant& v);
113     void mergeOnlineProfiles(const QStringList& profilesLoaded);
114     void emitOnlineConfigScriptError(const QJSValue& error);
115     void emitNetworkError(QString error);
116     mode_e modeFromString(const QString& mode) const;
117     QString stringFromMode(const mode_e mode) const;
118 
119     QStringList onlineProfiles;
120     QStringList localProfiles;
121 
122     QNetworkAccessManager* networkAccessManager;
123     QWebEnginePage* profilesWebPage;
124 
125     bool expertMode;
126     mode_e installMode;
127     QString expertConfigUrl;
128     QString onlineServiceUrl;
129     QString onlineProfilesUrl;
130     QStringList onlineProfilesAvailable;
131     QString localDir;
132     QString localJavaExecutable;
133     QString localProfileDir;
134     QString localCustomProfileDir;
135     QString localSegmentsDir;
136     QString localHost;
137     QString localPort;
138     bool localBindLocalonly;
139     QString localNumberThreads;
140     QString localMaxRunningTime;
141     QString localJavaOpts;
142     QString expertBinariesUrl;
143     QString expertSegmentsUrl;
144 
145     int versionMajor { NOINT };
146     int versionMinor { NOINT };
147     int versionPatch { NOINT };
148 
149     const bool defaultExpertMode = false;
150     const mode_e defaultInstallMode = eModeOnline;
151     const QString defaultConfigUrl = "https://brouter.de/brouter-web/config.js";
152     const QString defaultOnlineServiceUrl = "https://brouter.de";
153     const QString defaultOnlineProfilesUrl = "https://brouter.de/brouter/profiles2/";
154     const QString defaultLocalDir = ".";
155     const QString defaultLocalProfileDir = "profiles2";
156     const QString defaultLocalCustomProfileDir = "customprofiles";
157     const QString defaultLocalSegmentsDir = "segments4";
158     const QString defaultLocalHost = "127.0.0.1";
159     const QString defaultLocalPort = "17777";
160     const bool defaultLocalBindLocalonly = true;
161     const QString defaultLocalNumberThreads = "1";
162     const QString defaultLocalMaxRunningTime = "300";
163     const QString defaultLocalJavaOpts = "-Xmx128M -Xms128M -Xmn8M";
164     const QString defaultBinariesUrl = "https://brouter.de/brouter_bin/";
165     const QString defaultSegmentsUrl = "https://brouter.de/brouter/segments4/";
166 
167     const QString onlineCacheDir = "BRouter";
168 
169     friend class CRouterBRouter;
170     friend class CRouterBRouterLocal;
171     friend class CRouterBRouterSetupPage;
172     friend class CRouterBRouterSetupWizard;
173     friend class CRouterBRouterTilesSelect;
174 };
175 
176 #endif
177