Display the last connection time in dashboard/wivrnctl

This commit is contained in:
Guillaume Meunier
2025-03-25 21:15:01 +01:00
parent e0a1598ac5
commit 57a0cf1d4b
10 changed files with 252 additions and 57 deletions
+27 -4
View File
@@ -1,5 +1,5 @@
pragma ComponentBehavior: Bound
import QtQml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as Controls
@@ -13,6 +13,28 @@ Kirigami.ScrollablePage {
// flickable.interactive: false
property date now: new Date()
Timer {
interval: 1000
running: true
repeat: true
onTriggered: headsets.now = new Date();
}
function fuzzy_relative_date(then) {
var time_difference = (headsets.now.getTime() - then.getTime()) / 1000;
if (time_difference < 60)
return i18np("Last connection: %1 second ago", "Last connection: %1 seconds ago", Math.round(time_difference));
if (time_difference < 3600)
return i18np("Last connection: %1 minute ago", "Last connection: %1 minutes ago", Math.round(time_difference / 60));
if (time_difference < 86400)
return i18np("Last connection: %1 hour ago", "Last connection: %1 hours ago", Math.round(time_difference / 3600));
return i18np("Last connection: %1 day ago", "Last connection: %1 days ago", Math.round(time_difference / 86400));
}
Kirigami.PromptDialog {
id: rename_headset
title: i18n("Rename headset")
@@ -32,7 +54,7 @@ Kirigami.ScrollablePage {
Kirigami.Action {
text: i18n("Cancel")
icon.name: "dialog-cancel"
onTriggered: rename_headset.close();
onTriggered: rename_headset.close()
}
]
ColumnLayout {
@@ -62,6 +84,8 @@ Kirigami.ScrollablePage {
id: headset
required property string name
required property string publicKey
required property bool hasLastConnection
required property date lastConnection
contentItem: GridLayout {
id: delegate_layout
@@ -83,8 +107,7 @@ Kirigami.ScrollablePage {
Layout.column: 0
Layout.row: 1
wrapMode: Text.WordWrap
// text: "Last connection: unknown"
text: ""
text: headset.hasLastConnection ? headsets.fuzzy_relative_date(headset.lastConnection) : ""
}
Controls.Button {
+7 -2
View File
@@ -348,10 +348,15 @@ void wivrn_server::on_server_properties_changed(const QString & interface_name,
keys.beginStructure();
QString public_key;
QString name;
keys >> name >> public_key;
qlonglong last_connection_timestamp;
keys >> name >> public_key >> last_connection_timestamp;
keys.endStructure();
m_knownKeys.push_back(headset{name, public_key});
headset h{name, public_key};
if (last_connection_timestamp)
h.setLastConnection(QDateTime::fromSecsSinceEpoch(last_connection_timestamp));
m_knownKeys.push_back(h);
}
keys.endArray();
+22
View File
@@ -20,6 +20,7 @@
#include <QDBusPendingCallWatcher>
#include <QDBusServiceWatcher>
#include <QDateTime>
#include <QObject>
#include <QProcess>
#include <QSize>
@@ -39,9 +40,12 @@ class headset
QML_ELEMENT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QString publicKey READ publicKey WRITE setPublicKey)
Q_PROPERTY(bool hasLastConnection READ hasLastConnection)
Q_PROPERTY(QDateTime lastConnection READ lastConnection WRITE setLastConnection)
QString m_name;
QString m_publicKey;
QDateTime m_lastConnection;
public:
headset() = default;
@@ -49,6 +53,9 @@ public:
headset(headset &&) = default;
headset & operator=(const headset &) = default;
headset & operator=(headset &&) = default;
headset(QString name, QString publicKey, QDateTime lastConnection) :
m_name(name), m_publicKey(publicKey), m_lastConnection(lastConnection) {}
headset(QString name, QString publicKey) :
m_name(name), m_publicKey(publicKey) {}
@@ -62,6 +69,16 @@ public:
return m_publicKey;
}
QDateTime lastConnection() const
{
return m_lastConnection;
}
bool hasLastConnection() const
{
return !m_lastConnection.isNull();
}
void setName(QString value)
{
m_name = value;
@@ -71,6 +88,11 @@ public:
{
m_publicKey = value;
}
void setLastConnection(QDateTime value)
{
m_lastConnection = value;
}
};
class wivrn_server : public QObject