From 3e35a74950e46882bb9d6eb1f3e21a5bdabe3efb Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Thu, 14 Dec 2017 15:20:42 -0500 Subject: More work on the OSInterface framework (mostly networking-related) --- src-qt5/src-cpp/framework-OSInterface-template.cpp | 49 +++++++++++++++++++--- src-qt5/src-cpp/framework-OSInterface.h | 14 ++++++- src-qt5/src-cpp/framework-OSInterface.pri | 4 ++ src-qt5/src-cpp/framework-OSInterface_private.cpp | 15 +++++++ 4 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 src-qt5/src-cpp/framework-OSInterface.pri (limited to 'src-qt5') diff --git a/src-qt5/src-cpp/framework-OSInterface-template.cpp b/src-qt5/src-cpp/framework-OSInterface-template.cpp index bc995601..625acae4 100644 --- a/src-qt5/src-cpp/framework-OSInterface-template.cpp +++ b/src-qt5/src-cpp/framework-OSInterface-template.cpp @@ -9,6 +9,7 @@ //Start/stop interface watchers/notifications void OSInterface::start(){ setupMediaWatcher(); //will create/connect the filesystem watcher automatically + setupNetworkManager(); } void OSInterface::stop(){ @@ -32,11 +33,27 @@ int OSInterface::volume(){ return -1; } void OSInterface::setVolume(int){} // = Network Information = -bool OSInterface::networkAvailable(){ return false; } -QString OSInterface::networkType(){ return QString(); } //"wifi", "wired", or "cell" +bool OSInterface::networkAvailable(){ + if(INFO.contains()){ return INFO.value("netaccess/available").toBool(); } + return false; +} + +QString OSInterface::networkType(){ + if(INFO.contains("netaccess/type")){ return INFO.value("netaccess/type").toString(); } //"wifi", "wired", or "cell" + return ""; +} + float OSInterface::networkStrength(){ return -1; } //percentage. ("wired" type should always be 100%) -QString OSInterface::networkHostname(){ return QString(); } -QHostAddress OSInterface::networkAddress(){ return QHostAddress(); } + +QString OSInterface::networkHostname(){ + return QHostInfo::localHostName(); +} + +QHostAddress OSInterface::networkAddress(){ + QString addr; + if(INFO.contains("netaccess/address")){ addr = INFO.value("netaccess/address").toString(); } + return QHostAddress(addr); +} // = Network Modification = // = Media Shortcuts = @@ -95,6 +112,28 @@ void OSInterface::iodeviceReadyRead(){} void OSInterface::iodeviceAboutToClose(){} //NetworkAccessManager slots -void OSInterface::netAccessChanged(QNetworkAccessManager::NetworkAccessibility){} +void OSInterface::netAccessChanged(QNetworkAccessManager::NetworkAccessibility stat){ + INFO.setValue("netaccess/available", stat== QNetworkAccessManager::Accessible); + //Update all the other network status info at the same time + QNetworkConfiguration active = netman->activeConfiguration(); + //Type of connection + QString type; + switch(active->bearerTypeFamily()){ + case QNetworkConfiguration::BearerEthernet: type="wired"; break; + case QNetworkConfiguration::BearnerWLAN: type="wifi"; break; + case QNetworkConfiguration::Bearer2G: type="cell-2G"; break; + case QNetworkConfiguration::Bearer3G: type="cell-3G"; break; + case QNetworkConfiguration::Bearer4G: type="cell-4G"; break; + } + INFO.setValue("netaccess/type", type); + qDebug() << "Detected Device Status:" << active->identifier() << type << stat; + QNetworkInterface iface = QNetworkInterface::interfaceFromName(active->identifier()); + QString address = iface.hardwareAddress(); + qDebug() << " - Address:" << address; + INFO.setValue("netaccess/address", address); + + emit networkStatusChanged(); +} + void OSInterface::netRequestFinished(QNetworkReply*){} void OSInterface::netSslErrors(QNetworkReply*, const QList&){} diff --git a/src-qt5/src-cpp/framework-OSInterface.h b/src-qt5/src-cpp/framework-OSInterface.h index cccaddb4..c48fc9f8 100644 --- a/src-qt5/src-cpp/framework-OSInterface.h +++ b/src-qt5/src-cpp/framework-OSInterface.h @@ -23,6 +23,8 @@ #include #include #include +#include +#include class OSInterface : public QObject{ Q_OBJECT @@ -51,6 +53,7 @@ class OSInterface : public QObject{ Q_PROPERTY( bool canSuspend READ canSuspend NOTIFY powerAvailableChanged) //Brightness Q_PROPERTY( int brightness READ brightness WRITE setBrightness NOTIFY brightnessChanged) + public: // ================ // SEMI-VIRTUAL FUNCTIONS - NEED TO BE DEFINED IN THE OS-SPECIFIC FILES @@ -71,7 +74,7 @@ public: Q_INVOKABLE void setVolume(int); // = Network Information = Q_INVOKABLE bool networkAvailable(); - Q_INVOKABLE QString networkType(); //"wifi", "wired", or "cell" + Q_INVOKABLE QString networkType(); //"wifi", "wired", "cell", "cell-2G", "cell-3G", "cell-4G" Q_INVOKABLE float networkStrength(); //percentage. ("wired" type should always be 100%) Q_INVOKABLE QString networkHostname(); Q_INVOKABLE QHostAddress networkAddress(); @@ -131,6 +134,8 @@ private slots: void netAccessChanged(QNetworkAccessManager::NetworkAccessibility); void netRequestFinished(QNetworkReply*); void netSslErrors(QNetworkReply*, const QList&); + //Timer slots + void timerUpdate(); signals: void interfaceChanged(OSInterface::Interface); @@ -157,17 +162,24 @@ private: QIODevice *iodevice; //Network Access Manager (check network connectivity, etc) QNetworkAccessManager *netman; + //Timer for regular probes/updates + QTimer *timer; // Internal implifications for connecting the various watcher objects to their respective slots // (OS-agnostic - defined in the "OSInterface_private.cpp" file) void connectWatcher(); //setup the internal connections *only* void connectIodevice(); //setup the internal connections *only* void connectNetman(); //setup the internal connections *only* + void connectTimer(); //setup the internal connections *only* + // External Media Management (if system uses *.desktop shortcuts only) void setupMediaWatcher(); bool handleMediaDirChange(QString dir); //returns true if directory was handled QStringList autoHandledMediaFiles(); + // Qt-based NetworkAccessManager usage + void setupNetworkManager(); + public: OSInterface(QObject *parent = 0); ~OSInterface(); diff --git a/src-qt5/src-cpp/framework-OSInterface.pri b/src-qt5/src-cpp/framework-OSInterface.pri new file mode 100644 index 00000000..92fb8889 --- /dev/null +++ b/src-qt5/src-cpp/framework-OSInterface.pri @@ -0,0 +1,4 @@ +QT *= core network + +HEADERS *= framework-OSInterface.h +SOURCES *= framework-OSInterface_private.cpp diff --git a/src-qt5/src-cpp/framework-OSInterface_private.cpp b/src-qt5/src-cpp/framework-OSInterface_private.cpp index 23db9a6c..7dfc8606 100644 --- a/src-qt5/src-cpp/framework-OSInterface_private.cpp +++ b/src-qt5/src-cpp/framework-OSInterface_private.cpp @@ -55,6 +55,11 @@ void OSInterface::connectNetman(){ connect(netman, SIGNAL(sslErrors(QNetworkReply*, const QList&)), this, SLOT(netSslErrors(QNetworkReply*, const QList&)) ); } +void OSInterface::connectTimer(){ + if(timer==0){ return; } + connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()) ); +} + // External Media Management (if system uses *.desktop shortcuts) void OSInterface::setupMediaWatcher(){ //Create/connect the watcher if needed @@ -90,3 +95,13 @@ QStringList OSInterface::autoHandledMediaFiles(){ } return files; } + +// Qt-based NetworkAccessManager usage +void OSInterface::setupNetworkManager(){ + if(netman==0){ + netman = new QNetworkAccessManager(this); + connectNetman(); + } + //Load the initial state of the network accessibility + netAccessChanged(netman->networkAccessibility()); +} -- cgit