game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
homeassistant_panel.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// homeassistant_panel.cpp
4// ==============================================================================
5// Embedded Home Assistant web panel implementation
6// ==============================================================================
7
9#include "theme_manager.h"
10#include <QDesktopServices>
11#include <QInputDialog>
12#include <QSettings>
13#include <QWebEngineHistory>
14#include <QWebEngineSettings>
15
16// ==============================================================================
17// HAWebEnginePage - Navigation restriction
18// ==============================================================================
19
20bool HAWebEnginePage::acceptNavigationRequest(const QUrl& url, NavigationType /*type*/,
21 bool isMainFrame)
22{
23 // Allow all non-main-frame requests (iframes, resources, etc.)
24 if (!isMainFrame)
25 return true;
26
27 // Allow initial page load (before any page is loaded, baseUrl is empty/about:blank)
28 QUrl baseUrl(QWebEnginePage::url());
29 if (baseUrl.isEmpty() || baseUrl.scheme() == "about")
30 return true;
31
32 // SECURITY: only http(s) may load into the main frame. file://, data: and
33 // qrc: URLs all have an empty host, so the old "same host OR empty host"
34 // check let a hostile/compromised HA server (or a crafted link reaching the
35 // main frame) pivot the panel to the local filesystem (file:///etc/passwd)
36 // or an attacker-controlled data: document. Hand anything else to the
37 // system browser.
38 if (url.scheme() != "http" && url.scheme() != "https")
39 {
40 QDesktopServices::openUrl(url);
41 return false;
42 }
43
44 // Allow internal HA navigation (same host)
45 if (url.host() == baseUrl.host())
46 return true;
47
48 // Allow common HA auth flows (Google, Nabu Casa)
49 QString host = url.host();
50 if (host.endsWith("home-assistant.io") || host.endsWith("nabucasa.com") ||
51 host.endsWith("accounts.google.com"))
52 return true;
53
54 // External link - open in system browser instead
55 QDesktopServices::openUrl(url);
56 return false;
57}
58
59QWebEnginePage* HAWebEnginePage::createWindow(WebWindowType /*type*/)
60{
61 // Catch window.open() and target="_blank" links
62 // Return a temporary page that captures the URL and opens it externally
63 auto* tempPage = new QWebEnginePage(profile(), this);
64 connect(tempPage, &QWebEnginePage::urlChanged, this,
65 [tempPage](const QUrl& url)
66 {
67 if (url.isValid() && !url.isEmpty() && url.scheme() != "about")
68 {
69 QDesktopServices::openUrl(url);
70 tempPage->deleteLater();
71 }
72 });
73 return tempPage;
74}
75
76// ==============================================================================
77// Constructor / Destructor
78// ==============================================================================
79
81 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
82{
83 loadSettings();
84 setupUI();
85 connect(themeManager_, &ThemeManager::themeChanged, this, &HomeAssistantPanel::applyTheme);
86}
87
89{
90 saveSettings();
91}
92
94{
95 const auto& colors = themeManager_->colors();
96
97 QString btnStyle =
98 QString("QPushButton { background: transparent; border: none; padding: 2px 6px; "
99 "font-size: 12px; color: %1; border-radius: 3px; }"
100 "QPushButton:hover { background: %2; }"
101 "QPushButton:focus { border: 1px solid %4; }"
102 "QPushButton:disabled { color: %3; }")
103 .arg(colors.textPrimary.name(), colors.hoverBackground.name(), colors.textMuted.name(),
104 colors.accent.name());
105
106 for (QPushButton* btn : {backBtn_, forwardBtn_, refreshBtn_, homeBtn_, settingsBtn_})
107 {
108 if (btn)
109 btn->setStyleSheet(btnStyle);
110 }
111
112 if (navWidget_)
113 {
114 navWidget_->setStyleSheet(QString("background: %1; border-bottom: 1px solid %2;")
115 .arg(colors.panelBackground.name(), colors.border.name()));
116 }
117}
118
119// ==============================================================================
120// Public Methods
121// ==============================================================================
122
123void HomeAssistantPanel::ensureWebView()
124{
125 if (webView_)
126 return;
127
128 // Persistent web engine profile (preserves login cookies across sessions)
129 profile_ = new QWebEngineProfile("homeassistant", this);
130
131 // Configure web engine settings
132 auto* settings = profile_->settings();
133 settings->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
134 settings->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
135 settings->setAttribute(QWebEngineSettings::WebGLEnabled, true);
136 settings->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, true);
137 settings->setAttribute(QWebEngineSettings::PluginsEnabled, false);
138
139 // Create web view with custom page (restricts external navigation)
140 auto* page = new HAWebEnginePage(profile_, this);
141 webView_ = new QWebEngineView(this);
142 webView_->setPage(page);
143 webView_->setFocusPolicy(Qt::ClickFocus);
144
145 // Connect signals
146 connect(webView_, &QWebEngineView::urlChanged, this, &HomeAssistantPanel::onUrlChanged);
147 connect(webView_, &QWebEngineView::loadStarted, this, &HomeAssistantPanel::onLoadStarted);
148 connect(webView_, &QWebEngineView::loadFinished, this, &HomeAssistantPanel::onLoadFinished);
149 connect(webView_, &QWebEngineView::titleChanged, this, &HomeAssistantPanel::titleChanged);
150
151 layout_->addWidget(webView_, 1);
152}
153
155{
156 ensureWebView();
157 if (webView_ && !haUrl_.isEmpty() && !dashboardLoaded_)
158 {
159 webView_->load(QUrl(haUrl_));
160 dashboardLoaded_ = true;
161 }
162}
163
164void HomeAssistantPanel::setUrl(const QString& url)
165{
166 haUrl_ = url;
167 dashboardLoaded_ = false; // Force reload on next loadDashboard()
168 saveSettings();
169}
170
171// ==============================================================================
172// Private Slots
173// ==============================================================================
174
175void HomeAssistantPanel::onUrlChanged(const QUrl& /*url*/)
176{
177 if (backBtn_)
178 backBtn_->setEnabled(webView_->history()->canGoBack());
179 if (forwardBtn_)
180 forwardBtn_->setEnabled(webView_->history()->canGoForward());
181}
182
183void HomeAssistantPanel::onLoadStarted()
184{
185 if (refreshBtn_)
186 {
187 refreshBtn_->setText("\u2716"); // X (stop)
188 refreshBtn_->setToolTip("Stop loading");
189 }
190}
191
192void HomeAssistantPanel::onLoadFinished(bool /*ok*/)
193{
194 if (refreshBtn_)
195 {
196 refreshBtn_->setText("\u21BB"); // Refresh arrow
197 refreshBtn_->setToolTip("Reload page");
198 }
199}
200
201void HomeAssistantPanel::onBackClicked()
202{
203 if (webView_)
204 webView_->back();
205}
206
207void HomeAssistantPanel::onForwardClicked()
208{
209 if (webView_)
210 webView_->forward();
211}
212
213void HomeAssistantPanel::onRefreshClicked()
214{
215 if (!webView_)
216 return;
217
218 if (refreshBtn_ && refreshBtn_->text() == "\u2716")
219 webView_->stop();
220 else
221 webView_->reload();
222}
223
224void HomeAssistantPanel::onHomeClicked()
225{
226 if (webView_ && !haUrl_.isEmpty())
227 {
228 webView_->load(QUrl(haUrl_));
229 }
230}
231
232void HomeAssistantPanel::onSettingsClicked()
233{
234 bool ok;
235 QString newUrl =
236 QInputDialog::getText(this, "Home Assistant URL",
237 "Enter your Home Assistant URL:", QLineEdit::Normal, haUrl_, &ok);
238 if (ok && !newUrl.isEmpty())
239 {
240 if (!newUrl.startsWith("http"))
241 newUrl = "https://" + newUrl;
242 // Enforce http/https only — reject file://, javascript://, data:, etc.
243 QUrl parsed(newUrl);
244 if (!parsed.isValid() || (parsed.scheme() != "http" && parsed.scheme() != "https"))
245 return;
246 setUrl(newUrl);
247 if (webView_)
248 webView_->load(QUrl(haUrl_));
249 }
250}
251
252// ==============================================================================
253// Private Methods
254// ==============================================================================
255
256void HomeAssistantPanel::setupUI()
257{
258 layout_ = new QVBoxLayout(this);
259 layout_->setContentsMargins(0, 0, 0, 0);
260 layout_->setSpacing(0);
261
262 // Compact navigation bar - no URL bar, just essential controls
263 auto* navLayout = new QHBoxLayout();
264 navLayout->setContentsMargins(4, 1, 4, 1);
265 navLayout->setSpacing(2);
266
267 const auto& colors = themeManager_->colors();
268 QString btnStyle =
269 QString("QPushButton { background: transparent; border: none; padding: 2px 6px; "
270 "font-size: 12px; color: %1; border-radius: 3px; }"
271 "QPushButton:hover { background: %2; }"
272 "QPushButton:focus { border: 1px solid %4; }"
273 "QPushButton:disabled { color: %3; }")
274 .arg(colors.textPrimary.name(), colors.hoverBackground.name(), colors.textMuted.name(),
275 colors.accent.name());
276
277 backBtn_ = new QPushButton("\u25C0", this);
278 backBtn_->setToolTip("Back");
279 backBtn_->setFixedSize(24, 20);
280 backBtn_->setStyleSheet(btnStyle);
281 backBtn_->setEnabled(false);
282 connect(backBtn_, &QPushButton::clicked, this, &HomeAssistantPanel::onBackClicked);
283
284 forwardBtn_ = new QPushButton("\u25B6", this);
285 forwardBtn_->setToolTip("Forward");
286 forwardBtn_->setFixedSize(24, 20);
287 forwardBtn_->setStyleSheet(btnStyle);
288 forwardBtn_->setEnabled(false);
289 connect(forwardBtn_, &QPushButton::clicked, this, &HomeAssistantPanel::onForwardClicked);
290
291 refreshBtn_ = new QPushButton("\u21BB", this);
292 refreshBtn_->setToolTip("Reload page");
293 refreshBtn_->setFixedSize(24, 20);
294 refreshBtn_->setStyleSheet(btnStyle);
295 connect(refreshBtn_, &QPushButton::clicked, this, &HomeAssistantPanel::onRefreshClicked);
296
297 homeBtn_ = new QPushButton("\u2302", this);
298 homeBtn_->setToolTip("Home Assistant dashboard");
299 homeBtn_->setFixedSize(24, 20);
300 homeBtn_->setStyleSheet(btnStyle);
301 connect(homeBtn_, &QPushButton::clicked, this, &HomeAssistantPanel::onHomeClicked);
302
303 settingsBtn_ = new QPushButton("\u2699", this); // Gear icon
304 settingsBtn_->setToolTip("Change Home Assistant URL");
305 settingsBtn_->setFixedSize(24, 20);
306 settingsBtn_->setStyleSheet(btnStyle);
307 connect(settingsBtn_, &QPushButton::clicked, this, &HomeAssistantPanel::onSettingsClicked);
308
309 navLayout->addWidget(backBtn_);
310 navLayout->addWidget(forwardBtn_);
311 navLayout->addWidget(refreshBtn_);
312 navLayout->addWidget(homeBtn_);
313 navLayout->addStretch();
314 navLayout->addWidget(settingsBtn_);
315
316 navWidget_ = new QWidget(this);
317 navWidget_->setLayout(navLayout);
318 navWidget_->setFixedHeight(24);
319 navWidget_->setStyleSheet(QString("background: %1; border-bottom: 1px solid %2;")
320 .arg(colors.panelBackground.name(), colors.border.name()));
321 layout_->addWidget(navWidget_);
322
323 // Web engine is lazily initialized in ensureWebView() to avoid
324 // QWebEngineView stealing focus from the menu bar on startup.
325
326 setLayout(layout_);
327}
328
329void HomeAssistantPanel::loadSettings()
330{
331 QSettings settings;
332 QString url = settings.value(SETTINGS_KEY_URL, DEFAULT_HA_URL).toString();
333 // Only allow http:// and https:// schemes when loading from persisted settings
334 QUrl parsed(url);
335 haUrl_ = (parsed.isValid() && (parsed.scheme() == "http" || parsed.scheme() == "https"))
336 ? url
337 : DEFAULT_HA_URL;
338}
339
340void HomeAssistantPanel::saveSettings()
341{
342 QSettings settings;
343 settings.setValue(SETTINGS_KEY_URL, haUrl_);
344}
QWebEnginePage * createWindow(WebWindowType type) override
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) override
HomeAssistantPanel(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void titleChanged(const QString &title)
void setUrl(const QString &url)
void themeChanged()
Emitted when theme or accent color changes.