Skip to content

Commit

Permalink
Add utils_wifi helper
Browse files Browse the repository at this point in the history
  • Loading branch information
emericg committed Mar 22, 2024
1 parent 2ef339f commit 01ea468
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 16 deletions.
39 changes: 37 additions & 2 deletions qml/ScreenHostInfos.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Loader {

// change screen
appContent.state = "HostInfos"

// get current wifi info (on mobile)
utilsWiFi.refreshWiFi()
}

function backAction() {
Expand Down Expand Up @@ -48,9 +51,10 @@ Loader {
anchors.left: parent.left
anchors.right: parent.right

property int maxheight: topPadding + bottomPadding + 4*spacing +
property int maxheight: topPadding + bottomPadding + 5*spacing +
itemAppInfo.height + itemQtInfo.height +
itemOsInfo.height + itemHwInfo.height + itemScreenInfo.height
itemOsInfo.height + itemHwInfo.height +
itemScreenInfo.height + itemNwInfo.height

height: singleColumn ? maxheight : screenHostInfos.height
spacing: Theme.componentMargin
Expand Down Expand Up @@ -540,6 +544,37 @@ Loader {
}
}

////////////////////////////////

FrameThemed {
id: itemNwInfo
width: www

visible: utilsWiFi.currentSSID

ColumnLayout {
anchors.fill: parent
spacing: 12

Column {
Text {
text: qsTr("Network:")
textFormat: Text.PlainText
color: Theme.colorSubText
font.bold: true
font.pixelSize: Theme.fontSizeContentVerySmall
font.capitalization: Font.AllUppercase
}
Text {
text: utilsWiFi.currentSSID
textFormat: Text.PlainText
font.pixelSize: Theme.fontSizeContentBig
color: Theme.colorHighContrast
}
}
}
}

////////////////////////////////////////////////////////////////
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <utils_screen.h>
#include <utils_sysinfo.h>
#include <utils_language.h>
#include <utils_wifi.h>
#include <utils_os_macos_dock.h>

#include <MobileUI>
Expand Down Expand Up @@ -84,6 +85,7 @@ int main(int argc, char *argv[])
UtilsApp *utilsApp = UtilsApp::getInstance();
UtilsScreen *utilsScreen = UtilsScreen::getInstance();
UtilsSysInfo *utilsSysInfo = UtilsSysInfo::getInstance();
UtilsWiFi *utilsWiFi = UtilsWiFi::getInstance();
UtilsLanguage *utilsLanguage = UtilsLanguage::getInstance();
if (!utilsApp || !utilsScreen || !utilsSysInfo || !utilsLanguage)
{
Expand Down Expand Up @@ -112,15 +114,15 @@ int main(int argc, char *argv[])
engine_context->setContextProperty("utilsLanguage", utilsLanguage);
engine_context->setContextProperty("utilsScreen", utilsScreen);
engine_context->setContextProperty("utilsSysInfo", utilsSysInfo);
engine_context->setContextProperty("utilsWiFi", utilsWiFi);

// Load the main view
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(FORCE_MOBILE_UI)
//ShareUtils *mShareUtils = new ShareUtils();
//engine_context->setContextProperty("utilsShare", mShareUtils);
engine.load(QUrl(QStringLiteral("qrc:/qml/MobileApplication.qml")));
#else
engine.load(QUrl(QStringLiteral("qrc:/qml/DesktopApplication.qml")));
#endif

if (engine.rootObjects().isEmpty())
{
qWarning() << "Cannot init QmlApplicationEngine!";
Expand Down
12 changes: 7 additions & 5 deletions src/thirdparty/AppUtils/AppUtils.pri
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ SOURCES += $${PWD}/utils_app.cpp \
$${PWD}/utils_log.cpp \
$${PWD}/utils_maths.cpp \
$${PWD}/utils_screen.cpp \
$${PWD}/utils_sysinfo.cpp
$${PWD}/utils_sysinfo.cpp \
$${PWD}/utils_wifi.cpp

HEADERS += $${PWD}/utils_app.h \
$${PWD}/utils_bits.h \
Expand All @@ -17,7 +18,8 @@ HEADERS += $${PWD}/utils_app.h \
$${PWD}/utils_maths.h \
$${PWD}/utils_screen.h \
$${PWD}/utils_sysinfo.h \
$${PWD}/utils_versionchecker.h
$${PWD}/utils_versionchecker.h \
$${PWD}/utils_wifi.h

INCLUDEPATH += $${PWD}

Expand Down Expand Up @@ -75,9 +77,9 @@ ios {
HEADERS += $${PWD}/utils_os_ios.h

# iOS notifications (optional)
LIBS += -framework UserNotifications
SOURCES += $${PWD}/utils_os_ios_notif.mm
HEADERS += $${PWD}/utils_os_ios_notif.h
#LIBS += -framework UserNotifications
#SOURCES += $${PWD}/utils_os_ios_notif.mm
#HEADERS += $${PWD}/utils_os_ios_notif.h

# iOS WiFi SSID (optional)
#LIBS += -framework SystemConfiguration
Expand Down
22 changes: 15 additions & 7 deletions src/thirdparty/AppUtils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ set(CORE_SOURCES
utils_screen.h
utils_sysinfo.cpp
utils_sysinfo.h
utils_wifi.cpp
utils_wifi.h
utils_versionchecker.h)

# OS specific sources & dependencies
Expand Down Expand Up @@ -90,18 +92,24 @@ if(APPLE AND NOT IOS)
endif()

if(IOS)
set(PLATFORM_SOURCES
utils_os_ios.mm
utils_os_ios.h
utils_os_ios_notif.mm
utils_os_ios_notif.h)

find_package(Qt${QT_DEFAULT_MAJOR_VERSION} REQUIRED COMPONENTS Quick)
set(PLATFORM_LIBRARIES Qt::Quick)
set(PLATFORM_LIBRARIES Qt::GuiPrivate)
set(PLATFORM_LIBRARIES "-framework UIKit" m)

set(PLATFORM_SOURCES
utils_os_ios.mm
utils_os_ios.h)

set(PLATFORM_SOURCES
utils_os_ios_notif.mm
utils_os_ios_notif.h)
set(PLATFORM_LIBRARIES "-framework UserNotifications" m) # show notifications
#set(PLATFORM_LIBRARIES "-framework SystemConfiguration" m) # get WiFi SSID

set(PLATFORM_SOURCES
utils_os_ios_wifi.mm
utils_os_ios_wifi.h)
set(PLATFORM_LIBRARIES "-framework SystemConfiguration" m) # get WiFi SSID
endif()

################################################################################
Expand Down
43 changes: 43 additions & 0 deletions src/thirdparty/AppUtils/utils_os_ios_wifi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*!
* Copyright (c) 2024 Emeric Grange
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef UTILS_OS_IOS_WIFI_H
#define UTILS_OS_IOS_WIFI_H

#include <QtGlobal>
#include <QString>

#if defined(Q_OS_IOS)
/* ************************************************************************** */

/*!
* \brief iOS WiFi info
*/
class UtilsIOSWiFi
{
public:
static QString UtilsIOS::getWifiSSID()
};

/* ************************************************************************** */
#endif // Q_OS_IOS
#endif // UTILS_OS_IOS_WIFI_H
50 changes: 50 additions & 0 deletions src/thirdparty/AppUtils/utils_os_ios_wifi.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*!
* Copyright (c) 2024 Emeric Grange
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "utils_os_ios_wifi.h"

#if defined(Q_OS_IOS)

#import <SystemConfiguration/CaptiveNetwork.h>

/* ************************************************************************** */

QString UtilsIOSWiFi::getWifiSSID()
{
NSString *ssid = nil;
NSArray *interfaces = (__bridge_transfer id)CNCopySupportedInterfaces();

for (NSString *interfaceName in interfaces)
{
NSDictionary *networkInfo = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName);
if (networkInfo[@"SSID"])
{
ssid = networkInfo[@"SSID"];
break;
}
}

return QString::fromNSString(ssid);
}

/* ************************************************************************** */
#endif // Q_OS_IOS
125 changes: 125 additions & 0 deletions src/thirdparty/AppUtils/utils_wifi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*!
* Copyright (c) 2024 Emeric Grange
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "utils_wifi.h"

#if defined(Q_OS_ANDROID)
#include "utils_os_android.h"
#elif defined(Q_OS_IOS)
#include "utils_os_ios_wifi.h"
#endif

#include <QCoreApplication>
#include <QPermission>
#include <QDebug>

/* ************************************************************************** */

UtilsWiFi *UtilsWiFi::instance = nullptr;

UtilsWiFi *UtilsWiFi::getInstance()
{
if (instance == nullptr)
{
instance = new UtilsWiFi();
}

return instance;
}

UtilsWiFi::UtilsWiFi()
{
//
}

UtilsWiFi::~UtilsWiFi()
{
//
}

/* ************************************************************************** */

bool UtilsWiFi::checkLocationPermissions()
{
//qDebug() << "UtilsWiFi::checkLocationPermissions()";
bool permOS_was = m_permOS;

m_permOS = (qApp->checkPermission(QLocationPermission{}) == Qt::PermissionStatus::Granted);

if (permOS_was != m_permOS)
{
Q_EMIT permissionsChanged();
}

return m_permOS;
}

void UtilsWiFi::requestLocationPermissions()
{
//qDebug() << "UtilsWiFi::requestLocationPermissions()";

qApp->requestPermission(QBluetoothPermission{},
this, &UtilsWiFi::requestLocationPermissions_results);
}

void UtilsWiFi::requestLocationPermissions_results()
{
// evaluate the results
if (checkLocationPermissions())
{
refreshWiFi_internal();
}
else
{
// try again?
//requestLocationPermissions();
}
}

/* ************************************************************************** */

void UtilsWiFi::refreshWiFi()
{
if (checkLocationPermissions())
{
refreshWiFi_internal();
}
else
{
requestLocationPermissions();
}
}

void UtilsWiFi::refreshWiFi_internal()
{
#if defined(Q_OS_ANDROID)
m_currentSSID = UtilsAndroid::getWifiSSID();
Q_EMIT wifiChanged();
#elif defined(Q_OS_IOS)
m_currentSSID = UtilsIOS::getWifiSSID();
Q_EMIT wifiChanged();
#else
m_currentSSID = "";
#endif
}

/* ************************************************************************** */
Loading

0 comments on commit 01ea468

Please sign in to comment.