Skip to content

Commit

Permalink
Basic network checking added
Browse files Browse the repository at this point in the history
 * Use the infra for doing _shallow_ checking of network states
 * Listen for network state change callbacks and adjust/try accordingly
  • Loading branch information
SneWs committed Jul 16, 2024
1 parent 3b92f92 commit 8861436
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
14 changes: 4 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.5)

project(tail-tray VERSION 0.1 LANGUAGES CXX)
project(tail-tray VERSION 0.0.3 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
Expand All @@ -9,8 +9,8 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Network)

set(PROJECT_SOURCES
main.cpp
Expand All @@ -33,14 +33,8 @@ qt_add_executable(tail-tray
TailRunner.h TailRunner.cpp
)

target_link_libraries(tail-tray PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(tail-tray PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Network)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.tail-tray)
endif()
set_target_properties(tail-tray PROPERTIES
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
Expand Down
48 changes: 47 additions & 1 deletion MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <QDir>
#include <QFile>
#include <QNetworkInformation>

#include "MainWindow.h"

Expand All @@ -22,7 +23,7 @@ MainWindow::MainWindow(QWidget* parent)
accountsTabUi = std::make_unique<AccountsTabUiManager>(ui.get(), pCurrentExecution.get(), this);
pTrayManager = std::make_unique<TrayMenuManager>(settings, pCurrentExecution.get(), this);

changeToState(TailState::NotLoggedIn);
changeToState(TailState::NotConnected);
pCurrentExecution->getAccounts();

connect(ui->btnSettingsClose, &QPushButton::clicked,
Expand All @@ -32,6 +33,8 @@ this, &MainWindow::settingsClosed);

// Make sure the settings tab is selected by default
ui->tabWidget->setCurrentIndex(1);

setupNetworkCallbacks();
}

void MainWindow::showSettingsTab() {
Expand Down Expand Up @@ -68,6 +71,22 @@ void MainWindow::loginFlowCompleted() const {
pCurrentExecution->start();
}

void MainWindow::onNetworkRechabilityChanged(QNetworkInformation::Reachability newReachability) {
qDebug() << "onNetworkRechabilityChanged -> " << newReachability;

if (newReachability == QNetworkInformation::Reachability::Online) {
// Fetch accounts and then get the status info after that
pCurrentExecution->getAccounts();
QTimer::singleShot(500, this, [this]() {
pCurrentExecution->checkStatus();
});
return;
}

if (eCurrentState != TailState::NotConnected)
changeToState(TailState::NotConnected);
}

TailState MainWindow::changeToState(TailState newState)
{
auto retVal = eCurrentState;
Expand Down Expand Up @@ -106,6 +125,33 @@ void MainWindow::onTailStatusChanged(TailStatus* pNewStatus)
accountsTabUi->onTailStatusChanged(pTailStatus.get());
}

bool MainWindow::shallowCheckForNetworkAvailable() {
auto* inst = QNetworkInformation::instance();
if (inst->reachability() == QNetworkInformation::Reachability::Online)
return true;

return false;
}

void MainWindow::setupNetworkCallbacks() const {
auto* inst = QNetworkInformation::instance();
if (inst == nullptr) {
if (!QNetworkInformation::loadDefaultBackend()) {
qDebug() << "Unable to load Network information stack";
return;
}

inst = QNetworkInformation::instance();
if (inst == nullptr) {
qDebug() << "No noetwork information stack available";
return;
}
}

connect(inst, &QNetworkInformation::reachabilityChanged,
this, &MainWindow::onNetworkRechabilityChanged);
}

void MainWindow::syncSettingsToUi() const {
ui->chkAllowIncomingCnx->setChecked(settings.allowIncomingConnections());
ui->chkUseTailscaleDns->setChecked(settings.useTailscaleDns());
Expand Down
5 changes: 5 additions & 0 deletions MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <QMainWindow>
#include <QTimer>
#include <QNetworkInformation>

#include "TailRunner.h"
#include "TailSettings.h"
Expand Down Expand Up @@ -45,11 +46,15 @@ private slots:
void onAccountsListed(const QList<TailAccountInfo>& foundAccounts);
void settingsClosed();
void loginFlowCompleted() const;
void onNetworkRechabilityChanged(QNetworkInformation::Reachability newReachability);

private:
// Switch to the new state and return the prev (old) state back to caller
TailState changeToState(TailState newState);
void onTailStatusChanged(TailStatus* pNewStatus);

static bool shallowCheckForNetworkAvailable();
void setupNetworkCallbacks() const;
};

#endif // MAINWINDOW_H
2 changes: 1 addition & 1 deletion TailRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void TailRunner::onProcessCanReadStdOut() {
}
QJsonObject obj = doc.object();
if (obj.contains("BackendState")) {
auto newState = obj["BackendState"].toString();
//auto newState = obj["BackendState"].toString();
}
break;
}
Expand Down
3 changes: 2 additions & 1 deletion TrayMenuManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ void TrayMenuManager::buildNotConnectedMenu(TailStatus const* pTailStatus) const
pTrayMenu->clear();
pTrayMenu->addAction(pConnect);
pTrayMenu->addSeparator();
pThisDevice->setText(pTailStatus->user->loginName);
if (pTailStatus != nullptr && pTailStatus->user != nullptr)
pThisDevice->setText(pTailStatus->user->loginName);
pTrayMenu->addAction(pThisDevice);
pTrayMenu->addSeparator();
pTrayMenu->addAction(pPreferences);
Expand Down

0 comments on commit 8861436

Please sign in to comment.