Skip to content

Commit afa88bc

Browse files
committed
#3632 webapp: show connected status in status bar
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent 2f56f86 commit afa88bc

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# QOwnNotes Changelog
22

3+
## 26.6.3
4+
5+
- Added a compact web application connection status indicator to the main window
6+
status bar when web application support is enabled; right-clicking it now shows
7+
a context menu with a **Connected systems** submenu
8+
(for [#3632](https://github.com/pbek/QOwnNotes/issues/3632))
9+
310
## 26.6.2
411

512
- Fixed custom shortcuts for actions that appear in multiple menus, such as

src/mainwindow.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include <QKeyEvent>
7575
#include <QKeySequence>
7676
#include <QListWidgetItem>
77+
#include <QMenu>
7778
#include <QMessageBox>
7879
#include <QMimeData>
7980
#include <QPageSetupDialog>
@@ -287,8 +288,10 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
287288
_noteSubFolderDockWidgetVisible = true;
288289
_noteExternallyRemovedCheckEnabled = true;
289290
_readOnlyButton = new QPushButton(this);
291+
_webAppStatusButton = new QPushButton(this);
290292
_lastNoteSelectionWasMultiple = false;
291293
_webSocketServerService = nullptr;
294+
_webAppClientService = nullptr;
292295
_mcpService = nullptr;
293296
_closeEventWasFired = false;
294297
_useNoteFolderButtons = settings.value("useNoteFolderButtons").toBool();
@@ -911,6 +914,8 @@ void MainWindow::initWebSocketServerService() {
911914
void MainWindow::initWebAppClientService() {
912915
_webAppClientServiceSettingsKey = webAppClientServiceSettingsKey();
913916
_webAppClientService = new WebAppClientService();
917+
connectWebAppClientServiceSignals();
918+
updateWebAppStatusButton();
914919
}
915920

916921
/**
@@ -928,6 +933,7 @@ void MainWindow::reinitWebAppClientService() {
928933

929934
delete _webAppClientService;
930935
_webAppClientService = nullptr;
936+
_webAppConnectedDevices.clear();
931937
initWebAppClientService();
932938
}
933939

@@ -2971,6 +2977,9 @@ void MainWindow::setupStatusBarWidgets() {
29712977

29722978
ui->statusBar->addPermanentWidget(_noteEditLineNumberLabel);
29732979

2980+
setupWebAppStatusButton();
2981+
ui->statusBar->addPermanentWidget(_webAppStatusButton);
2982+
29742983
/*
29752984
* setup of update available button
29762985
*/
@@ -2988,6 +2997,77 @@ void MainWindow::setupStatusBarWidgets() {
29882997
ui->statusBar->addPermanentWidget(_updateAvailableButton);
29892998
}
29902999

3000+
void MainWindow::setupWebAppStatusButton() {
3001+
_webAppStatusButton->setFlat(true);
3002+
_webAppStatusButton->setText(tr("Web"));
3003+
_webAppStatusButton->setStyleSheet(QStringLiteral("QPushButton {padding: 0 5px}"));
3004+
_webAppStatusButton->setContextMenuPolicy(Qt::CustomContextMenu);
3005+
3006+
connect(_webAppStatusButton, &QWidget::customContextMenuRequested, this,
3007+
&MainWindow::showWebAppStatusContextMenu);
3008+
3009+
updateWebAppStatusButton();
3010+
}
3011+
3012+
void MainWindow::connectWebAppClientServiceSignals() {
3013+
if (_webAppClientService == nullptr) {
3014+
return;
3015+
}
3016+
3017+
connect(_webAppClientService, &WebAppClientService::connectionStateChanged, this,
3018+
[this](bool) { updateWebAppStatusButton(); });
3019+
connect(_webAppClientService, &WebAppClientService::connectedDevicesUpdated, this,
3020+
&MainWindow::updateWebAppConnectedDevices);
3021+
}
3022+
3023+
void MainWindow::updateWebAppStatusButton() {
3024+
if (_webAppStatusButton == nullptr) {
3025+
return;
3026+
}
3027+
3028+
const bool enabled = Utils::Misc::isWebAppSupportEnabled();
3029+
const bool connected =
3030+
_webAppClientService != nullptr && _webAppClientService->checkIsConnected();
3031+
_webAppStatusButton->setVisible(enabled);
3032+
_webAppStatusButton->setToolTip(connected ? tr("Web app is connected")
3033+
: tr("Web app is not connected"));
3034+
_webAppStatusButton->setStyleSheet(
3035+
connected ? QStringLiteral("QPushButton {padding: 0 5px; color: #2e7d32}")
3036+
: QStringLiteral("QPushButton {padding: 0 5px; color: #b71c1c}"));
3037+
}
3038+
3039+
void MainWindow::updateWebAppConnectedDevices(const QStringList &deviceNames) {
3040+
_webAppConnectedDevices = deviceNames;
3041+
updateWebAppStatusButton();
3042+
}
3043+
3044+
void MainWindow::showWebAppStatusContextMenu(const QPoint &point) {
3045+
if (_webAppStatusButton == nullptr || !Utils::Misc::isWebAppSupportEnabled()) {
3046+
return;
3047+
}
3048+
3049+
const bool connected =
3050+
_webAppClientService != nullptr && _webAppClientService->checkIsConnected();
3051+
if (connected) {
3052+
_webAppClientService->sendRequestConnectedDevices();
3053+
}
3054+
3055+
QMenu menu(this);
3056+
menu.addAction(connected ? tr("Web app connected") : tr("Web app disconnected"))
3057+
->setEnabled(false);
3058+
3059+
QMenu *connectedSystemsMenu = menu.addMenu(tr("Connected systems"));
3060+
if (_webAppConnectedDevices.isEmpty()) {
3061+
connectedSystemsMenu->addAction(tr("No connected systems"))->setEnabled(false);
3062+
} else {
3063+
for (const QString &deviceName : qAsConst(_webAppConnectedDevices)) {
3064+
connectedSystemsMenu->addAction(deviceName)->setEnabled(false);
3065+
}
3066+
}
3067+
3068+
menu.exec(_webAppStatusButton->mapToGlobal(point));
3069+
}
3070+
29913071
void MainWindow::initializeOpenAiActivitySpinner() {
29923072
_aiToolbarManager->initializeOpenAiActivitySpinner();
29933073
}

src/mainwindow.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class QPlainTextEdit;
5050
class QTextEdit;
5151
class QTextDocument;
5252
class QLabel;
53+
class QMenu;
5354
class QPushButton;
5455
class QFile;
5556
class QToolBar;
@@ -958,6 +959,7 @@ class MainWindow : public QMainWindow {
958959
NoteHistory noteHistory;
959960
QHash<int, NoteHistoryItem> noteBookmarks;
960961
QPushButton *_updateAvailableButton;
962+
QPushButton *_webAppStatusButton;
961963
NoteFilePathLabel *_noteFilePathLabel;
962964
QLabel *_noteEditLineNumberLabel;
963965
QPushButton *_readOnlyButton;
@@ -1027,6 +1029,7 @@ class MainWindow : public QMainWindow {
10271029
WebSocketServerService *_webSocketServerService;
10281030
WebAppClientService *_webAppClientService;
10291031
QString _webAppClientServiceSettingsKey;
1032+
QStringList _webAppConnectedDevices;
10301033
McpService *_mcpService;
10311034
bool _brokenTagNoteLinksRemoved = false;
10321035

@@ -1123,6 +1126,12 @@ class MainWindow : public QMainWindow {
11231126

11241127
void setupStatusBarWidgets();
11251128

1129+
void setupWebAppStatusButton();
1130+
void connectWebAppClientServiceSignals();
1131+
void updateWebAppStatusButton();
1132+
void updateWebAppConnectedDevices(const QStringList &deviceNames);
1133+
void showWebAppStatusContextMenu(const QPoint &point);
1134+
11261135
void gotoNextNote();
11271136

11281137
void activateContextMenu();

src/services/webappclientservice.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ void WebAppClientService::onConnected() {
291291

292292
// Send registration with connection name to the server
293293
sendRegister();
294+
sendRequestConnectedDevices();
295+
296+
emit connectionStateChanged(true);
294297
}
295298

296299
void WebAppClientService::onDisconnected() {
@@ -300,6 +303,9 @@ void WebAppClientService::onDisconnected() {
300303

301304
Utils::Misc::printInfo(
302305
tr("QOwnNotes is now disconnected from websocket to %1").arg(getServerUrl()));
306+
307+
emit connectionStateChanged(false);
308+
emit connectedDevicesUpdated(QStringList());
303309
}
304310

305311
void WebAppClientService::onTextMessageReceived(const QString &message) {

src/services/webappclientservice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class WebAppClientService : public QObject {
4747
bool sendClipboardAsText() const;
4848

4949
signals:
50+
void connectionStateChanged(bool connected);
5051
void connectedDevicesUpdated(const QStringList &deviceNames);
5152

5253
private slots:

0 commit comments

Comments
 (0)