|
| 1 | +#include "RemoteSessionState.h" |
| 2 | + |
| 3 | +#include <QDebug> |
| 4 | + |
| 5 | +namespace { |
| 6 | +QString normalizedFingerprint(const QString& value) |
| 7 | +{ |
| 8 | + QString normalized; |
| 9 | + for (const QChar character : value) { |
| 10 | + const QChar upper = character.toUpper(); |
| 11 | + if (character.isDigit() || (upper >= QLatin1Char('A') && upper <= QLatin1Char('F'))) |
| 12 | + normalized.append(upper); |
| 13 | + } |
| 14 | + return normalized.size() == 64 ? normalized : QString(); |
| 15 | +} |
| 16 | +} |
| 17 | + |
| 18 | +RemoteSessionState::RemoteSessionState(QObject* parent) : QObject(parent) {} |
| 19 | + |
| 20 | +QString RemoteSessionState::hostIdentity() const { return m_hostIdentity; } |
| 21 | +quint64 RemoteSessionState::sessionGeneration() const { return m_sessionGeneration; } |
| 22 | +quint64 RemoteSessionState::workspaceGeneration() const { return m_workspaceGeneration; } |
| 23 | +int RemoteSessionState::workspaceId() const { return m_workspaceId; } |
| 24 | +bool RemoteSessionState::isAuthenticated() const { return m_authenticated; } |
| 25 | +RemoteSessionState::DataState RemoteSessionState::dataState() const { return m_dataState; } |
| 26 | + |
| 27 | +RemoteSessionState::Snapshot RemoteSessionState::snapshot() const |
| 28 | +{ |
| 29 | + return {m_hostIdentity, m_sessionGeneration, m_workspaceGeneration, m_workspaceId}; |
| 30 | +} |
| 31 | + |
| 32 | +bool RemoteSessionState::accepts(const Snapshot& value) const |
| 33 | +{ |
| 34 | + return m_authenticated && value.hostIdentity == m_hostIdentity |
| 35 | + && value.sessionGeneration == m_sessionGeneration |
| 36 | + && value.workspaceGeneration == m_workspaceGeneration |
| 37 | + && value.workspaceId == m_workspaceId; |
| 38 | +} |
| 39 | + |
| 40 | +void RemoteSessionState::authenticated(const QString& verifiedFingerprint) |
| 41 | +{ |
| 42 | + const QString identity = normalizedFingerprint(verifiedFingerprint); |
| 43 | + if (identity.isEmpty()) return; |
| 44 | + |
| 45 | + const bool hadSession = m_hadAuthenticatedSession; |
| 46 | + const bool sameHost = !m_hostIdentity.isEmpty() && identity == m_hostIdentity; |
| 47 | + const bool changedHost = !m_hostIdentity.isEmpty() && !sameHost; |
| 48 | + ++m_sessionGeneration; |
| 49 | + m_authenticated = true; |
| 50 | + m_hadAuthenticatedSession = true; |
| 51 | + |
| 52 | + if (changedHost) { |
| 53 | + m_hostIdentity = identity; |
| 54 | + advanceWorkspaceGeneration(0); |
| 55 | + setDataState(DataState::NeverLoaded); |
| 56 | + emit hostIdentityChanged(); |
| 57 | + emit operationalStateMustClear(); |
| 58 | + } else { |
| 59 | + m_hostIdentity = identity; |
| 60 | + } |
| 61 | + |
| 62 | + qDebug().noquote() << "Remote session generation" << m_sessionGeneration |
| 63 | + << (sameHost ? "restored for Host" : "established for Host") |
| 64 | + << m_hostIdentity.left(12); |
| 65 | + |
| 66 | + emit authenticatedSessionEstablished(sameHost); |
| 67 | + if (hadSession && sameHost) { |
| 68 | + emit sameHostSessionRestored(); |
| 69 | + emit operationalStateShouldRefresh(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void RemoteSessionState::disconnected() |
| 74 | +{ |
| 75 | + if (!m_authenticated) return; |
| 76 | + ++m_sessionGeneration; |
| 77 | + m_authenticated = false; |
| 78 | + setDataState(m_hostIdentity.isEmpty() ? DataState::NeverLoaded |
| 79 | + : DataState::StaleDisconnected); |
| 80 | + qDebug() << "Remote session lost; generation" << m_sessionGeneration; |
| 81 | + emit authenticatedSessionLost(); |
| 82 | +} |
| 83 | + |
| 84 | +void RemoteSessionState::setWorkspaceId(int workspaceId) |
| 85 | +{ |
| 86 | + workspaceId = qMax(0, workspaceId); |
| 87 | + if (workspaceId == m_workspaceId) return; |
| 88 | + advanceWorkspaceGeneration(workspaceId); |
| 89 | +} |
| 90 | + |
| 91 | +void RemoteSessionState::markCurrent() |
| 92 | +{ |
| 93 | + if (m_authenticated) setDataState(DataState::Current); |
| 94 | +} |
| 95 | + |
| 96 | +void RemoteSessionState::setDataState(DataState state) |
| 97 | +{ |
| 98 | + if (m_dataState == state) return; |
| 99 | + m_dataState = state; |
| 100 | + emit dataStateChanged(state); |
| 101 | +} |
| 102 | + |
| 103 | +void RemoteSessionState::advanceWorkspaceGeneration(int workspaceId) |
| 104 | +{ |
| 105 | + m_workspaceId = workspaceId; |
| 106 | + ++m_workspaceGeneration; |
| 107 | + qDebug() << "Remote Workspace generation" << m_workspaceGeneration |
| 108 | + << "Workspace" << m_workspaceId; |
| 109 | + emit workspaceGenerationChanged(m_workspaceGeneration, m_workspaceId); |
| 110 | +} |
0 commit comments