9#include "ekosliveclient.h"
10#include "ekos/manager.h"
14#include "ekos_debug.h"
15#include "QProgressIndicator.h"
17#include <config-kstars.h>
20#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
21#include <qt6keychain/keychain.h>
23#include <qt5keychain/keychain.h>
27#include <QJsonDocument>
29#include <QNetworkAccessManager>
30#include <QNetworkReply>
35Client::Client(Ekos::Manager *manager) :
QDialog(manager), m_Manager(manager)
43 leftBanner->setPixmap(im);
46 bottomLayout->insertWidget(1, pi);
50 username->setText(Options::ekosLiveUsername());
53 Options::setEkosLiveUsername(username->text());
58 connect(onlineManager.get(), &NodeManager::authenticationError,
this, [
this](
const QString & message)
60 onlineLabel->setToolTip(message);
65 connect(offlineManager.get(), &NodeManager::authenticationError,
this, [
this](
const QString & message)
67 offlineLabel->setToolTip(message);
70 m_NodeManagers.append(std::move(onlineManager));
71 m_NodeManagers.append(std::move(offlineManager));
79 for (
auto &oneManager : m_NodeManagers)
80 oneManager->disconnectNodes();
84 for (
auto &oneManager : m_NodeManagers)
86 oneManager->setCredentials(username->text(), password->text());
87 oneManager->authenticate();
96 for (
auto &oneManager : m_NodeManagers)
98 oneManager->setCredentials(username->text(), password->text());
99 oneManager->authenticate();
104 rememberCredentialsCheck->setChecked(Options::rememberCredentials());
107 Options::setRememberCredentials(toggled);
109 autoStartCheck->setChecked(Options::autoStartEkosLive());
112 Options::setAutoStartEkosLive(toggled);
116 QKeychain::ReadPasswordJob *job =
new QKeychain::ReadPasswordJob(
QLatin1String(
"kstars"));
117 job->setAutoDelete(
false);
119 connect(job, &QKeychain::Job::finished,
this, [&](QKeychain::Job * job)
121 if (job->error() ==
false)
123 const auto passwordText =
dynamic_cast<QKeychain::ReadPasswordJob*
>(job)->textData().toLatin1();
126 if (passwordText.isEmpty() ==
false && username->text().isEmpty() ==
false)
128 password->setText(passwordText);
129 if (autoStartCheck->isChecked())
131 for (
auto &oneManager : m_NodeManagers)
133 oneManager->setCredentials(username->text(), password->text());
134 oneManager->authenticate();
141 if (autoStartCheck->isChecked())
142 qCWarning(KSTARS_EKOS) <<
"Failed to automatically connect due to missing EkosLive credentials:" << job->errorString();
149 m_Message =
new Message(m_Manager, m_NodeManagers);
150 connect(m_Message, &Message::connected,
this, &Client::onConnected);
151 connect(m_Message, &Message::disconnected,
this, &Client::onDisconnected);
152 connect(m_Message, &Message::expired,
this, [&](
const QUrl & url)
155 for (
auto &oneManager : m_NodeManagers)
157 if (oneManager->property(
"websocketURL").toUrl() == url)
159 oneManager->disconnectNodes();
160 oneManager->setCredentials(username->text(), password->text());
161 oneManager->authenticate();
166 m_Media =
new Media(m_Manager, m_NodeManagers);
167 connect(m_Media, &Media::connected,
this, &Client::onConnected);
168 m_Cloud =
new Cloud(m_Manager, m_NodeManagers);
169 connect(m_Cloud, &Cloud::connected,
this, &Client::onConnected);
174 for (
auto &oneManager : m_NodeManagers)
175 oneManager->disconnectNodes();
178void Client::onConnected()
182 m_isConnected =
true;
184 connectB->setText(
i18n(
"Disconnect"));
187 auto disconnected =
QIcon(
":/icons/AlignFailure.svg").
pixmap(
QSize(32, 32));
190 onlineLabel->setStyleSheet(m_NodeManagers[0]->isConnected() ?
"color:white" :
"color:gray");
191 onlineIcon->setPixmap(m_NodeManagers[0]->isConnected() ? connected : disconnected);
192 if (m_NodeManagers[0]->isConnected())
193 onlineLabel->setToolTip(
QString());
195 selectServersB->setEnabled(
false);
196 offlineLabel->setStyleSheet(m_NodeManagers[1]->isConnected() ?
"color:white" :
"color:gray");
197 offlineIcon->setPixmap(m_NodeManagers[1]->isConnected() ? connected : disconnected);
198 if (m_NodeManagers[1]->isConnected())
199 offlineLabel->setToolTip(
QString());
201 if (rememberCredentialsCheck->isChecked())
204 QKeychain::WritePasswordJob *job =
new QKeychain::WritePasswordJob(
QLatin1String(
"kstars"));
205 job->setAutoDelete(
true);
207 job->setTextData(password->text());
213void Client::onDisconnected()
216 m_isConnected =
false;
217 connectB->setText(
i18n(
"Connect"));
219 auto disconnected =
QIcon(
":/icons/AlignFailure.svg").
pixmap(
QSize(32, 32));
222 onlineLabel->setStyleSheet(m_NodeManagers[0]->isConnected() ?
"color:white" :
"color:gray");
223 onlineIcon->setPixmap(m_NodeManagers[0]->isConnected() ? connected : disconnected);
225 offlineLabel->setStyleSheet(m_NodeManagers[1]->isConnected() ?
"color:white" :
"color:gray");
226 offlineIcon->setPixmap(m_NodeManagers[1]->isConnected() ? connected : disconnected);
228 selectServersB->setEnabled(
true);
231void Client::setConnected(
bool enabled)
234 if (enabled == m_isConnected)
240void Client::setConfig(
bool rememberCredentials,
bool autoConnect)
242 rememberCredentialsCheck->setChecked(rememberCredentials);
243 autoStartCheck->setChecked(autoConnect);
248 username->setText(user);
249 Options::setEkosLiveUsername(user);
251 password->setText(pass);
254void Client::showSelectServersDialog()
257 dialog.setMinimumWidth(300);
258 dialog.setWindowTitle(
i18nc(
"@title:window",
"Select EkosLive Servers"));
269 machineID.setReadOnly(
true);
272 observatoryEdit.setText(Options::ekosLiveObservatory());
273 offlineEdit.setText(Options::ekosLiveOfflineServer());
274 onlineEdit.setText(Options::ekosLiveOnlineServer());
277 layout->
addRow(&observatory, &observatoryEdit);
278 layout->
addRow(&offline, &offlineEdit);
279 layout->
addRow(&online, &onlineEdit);
280 layout->
addRow(&
id, &machineID);
281 dialog.setLayout(layout);
284 Options::setEkosLiveObservatory(observatoryEdit.text());
285 Options::setEkosLiveOfflineServer(offlineEdit.text());
286 Options::setEkosLiveOnlineServer(onlineEdit.text());
291void Client::syncURLs()
293 auto onlineSSL =
QUrl(Options::ekosLiveOnlineServer()).
scheme() ==
"https";
296 m_NodeManagers[Online]->setURLs(
QUrl(
"https:" + onlineURL),
QUrl(
"wss:" + onlineURL));
298 m_NodeManagers[Online]->setURLs(
QUrl(
"http:" + onlineURL),
QUrl(
"ws:" + onlineURL));
300 auto offlineSSL =
QUrl(Options::ekosLiveOfflineServer()).
scheme() ==
"https";
303 m_NodeManagers[Offline]->setURLs(
QUrl(
"https:" + offlineURL),
QUrl(
"wss:" + offlineURL));
305 m_NodeManagers[Offline]->setURLs(
QUrl(
"http:" + offlineURL),
QUrl(
"ws:" + offlineURL));
The QProgressIndicator class lets an application display a progress indicator to show that a long tas...
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Generic record interfaces and implementations.
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QIcon fromTheme(const QString &name)
bool load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
QString fromUtf8(QByteArrayView str)
QByteArray machineUniqueId()
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString scheme() const const
QString url(FormattingOptions options) const const