diff --git a/core/smb4khardwareinterface.cpp b/core/smb4khardwareinterface.cpp index 49e00f3..858f140 100644 --- a/core/smb4khardwareinterface.cpp +++ b/core/smb4khardwareinterface.cpp @@ -1,178 +1,215 @@ /*************************************************************************** Provides an interface to the computer's hardware ------------------- begin : Die Jul 14 2015 copyright : (C) 2015-2019 by Alexander Reinholdt email : alexander.reinholdt@kdemail.net ***************************************************************************/ /*************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* * MA 02110-1335, USA * ***************************************************************************/ // application specific includes #include "smb4khardwareinterface.h" #include "smb4khardwareinterface_p.h" // Qt includes #include #include // KDE includes #include #include #include #if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) #include #endif Q_GLOBAL_STATIC(Smb4KHardwareInterfaceStatic, p); Smb4KHardwareInterface::Smb4KHardwareInterface(QObject *parent) : QObject(parent), d(new Smb4KHardwareInterfacePrivate) { - d->networkConfigUpdated = false; + d->networkSession = nullptr; connect(&d->networkConfigManager, SIGNAL(updateCompleted()), this, SLOT(slotNetworkConfigUpdated())); - connect(&d->networkConfigManager, SIGNAL(onlineStateChanged(bool)), this, SIGNAL(onlineStateChanged(bool))); connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(QString)), this, SLOT(slotDeviceAdded(QString))); connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(QString)), this, SLOT(slotDeviceRemoved(QString))); #if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) startTimer(2000); #endif } Smb4KHardwareInterface::~Smb4KHardwareInterface() { } Smb4KHardwareInterface* Smb4KHardwareInterface::self() { return &p->instance; } void Smb4KHardwareInterface::updateNetworkConfig() { d->networkConfigManager.updateConfigurations(); } -bool Smb4KHardwareInterface::networkConfigIsUpdated() const -{ - return d->networkConfigUpdated; -} - - bool Smb4KHardwareInterface::isOnline() const { - return d->networkConfigManager.isOnline(); + if (d->networkSession) + { + return (d->networkSession->state() == QNetworkSession::Connected); + } + + return false; } #if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) // Using FreeBSD 11 with KF 5.27, Solid is not able to detect mounted shares. // Thus, we check here whether shares have been mounted or unmounted. // This is a hack and should be removed as soon as possible. void Smb4KHardwareInterface::timerEvent(QTimerEvent */*e*/) { KMountPoint::List mountPoints = KMountPoint::currentMountPoints(KMountPoint::BasicInfoNeeded|KMountPoint::NeedMountOptions); QStringList mountPointList, alreadyMounted; for (const QExplicitlySharedDataPointer &mountPoint : mountPoints) { if (QString::compare(mountPoint->mountType(), "smbfs") == 0 || QString::compare(mountPoint->mountType(), "cifs") == 0) { mountPointList.append(mountPoint->mountPoint()); } } QMutableStringListIterator it(mountPointList); while (it.hasNext()) { QString mp = it.next(); int index = -1; if ((index = d->mountPoints.indexOf(mp)) != -1) { d->mountPoints.removeAt(index); alreadyMounted.append(mp); it.remove(); } } if (!d->mountPoints.isEmpty()) { emit networkShareRemoved(); } if (!mountPointList.isEmpty()) { emit networkShareAdded(); } d->mountPoints.clear(); d->mountPoints.append(alreadyMounted); d->mountPoints.append(mountPointList); } #endif void Smb4KHardwareInterface::slotNetworkConfigUpdated() { - d->networkConfigUpdated = true; + // + // Create a network session object if necessary and connect it to the stateChanged() + // signal to monitor changes of the network connection. + // + if (!d->networkSession) + { + d->networkSession = new QNetworkSession(d->networkConfigManager.defaultConfiguration(), this); + connect(d->networkSession, SIGNAL(stateChanged(QNetworkSession::State)), this, SLOT(slotConnectionStateChanged(QNetworkSession::State))); + } + + // + // Tell the application that the network configuration was updated + // emit networkConfigUpdated(); + + // + // Check the state of the network session and emit the onlineStateChanged() + // signal accordingly. + // + if (d->networkSession->state() == QNetworkSession::Connected) + { + emit onlineStateChanged(true); + } + else + { + emit onlineStateChanged(false); + } } +void Smb4KHardwareInterface::slotConnectionStateChanged(QNetworkSession::State state) +{ + if (state == QNetworkSession::Connected) + { + emit onlineStateChanged(true); + } + else + { + emit onlineStateChanged(false); + } +} + + + void Smb4KHardwareInterface::slotDeviceAdded(const QString& udi) { Solid::Device device(udi); if (device.isDeviceInterface(Solid::DeviceInterface::NetworkShare)) { d->udis.append(udi); emit networkShareAdded(); } } void Smb4KHardwareInterface::slotDeviceRemoved(const QString& udi) { Solid::Device device(udi); // For some reason, the device has no valid type at the moment (Frameworks 5.9, // July 2015). Thus, we need the code in the else block for now. if (device.isDeviceInterface(Solid::DeviceInterface::NetworkShare)) { emit networkShareRemoved(); } else { if (d->udis.contains(udi)) { emit networkShareRemoved(); d->udis.removeOne(udi); } } } diff --git a/core/smb4khardwareinterface.h b/core/smb4khardwareinterface.h index 2a27bc2..a0d45ec 100644 --- a/core/smb4khardwareinterface.h +++ b/core/smb4khardwareinterface.h @@ -1,142 +1,140 @@ /*************************************************************************** Provides an interface to the computer's hardware ------------------- begin : Die Jul 14 2015 copyright : (C) 2015-2019 by Alexander Reinholdt email : alexander.reinholdt@kdemail.net ***************************************************************************/ /*************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* * MA 02110-1335, USA * ***************************************************************************/ #ifndef SMB4KHARDWAREINTERFACE_H #define SMB4KHARDWAREINTERFACE_H // application specific includes #include "smb4kglobal.h" // Qt includes #include #include #include #include +#include class Smb4KHardwareInterfacePrivate; /** * This class provides an interface to the computer's hardware. * @author Alexander Reinholdt * @since 2.0.0 */ class Q_DECL_EXPORT Smb4KHardwareInterface : public QObject { Q_OBJECT friend class Smb4KHardwareInterfacePrivate; public: /** * The constructor */ explicit Smb4KHardwareInterface(QObject *parent = 0); /** * The destructor */ ~Smb4KHardwareInterface(); /** * The static pointer to this class. * @returns a static pointer to this class */ static Smb4KHardwareInterface *self(); /** * This function checks the current network configuration. - * You should run this if @see isOnline() and @see networkConfigUpdated() return - * both FALSE. */ void updateNetworkConfig(); - /** - * This function returns TRUE if the network configuration was already updated - * and FALSE otherwise. - * @returns TRUE if the network configuration was updated and FALSE otherwise. - */ - bool networkConfigIsUpdated() const; - /** * This function returns TRUE if the system is online and FALSE otherwise. * @returns TRUE if the system is online. */ bool isOnline() const; #if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) protected: /** * Reimplemented from QObject to check for mounts and unmounts on operating * systems that are not fully supported by Solid, yet. */ void timerEvent(QTimerEvent *e); #endif Q_SIGNALS: /** * This signal is emitted when a network share is added to the system */ void networkShareAdded(); /** * This signal is emitted when a network share is removed from the system */ void networkShareRemoved(); /** * This signal is emitted when the network configuration was updated. */ void networkConfigUpdated(); /** * This signal is emitted when the online state changed. */ void onlineStateChanged(bool online); protected Q_SLOTS: /** * This slot is called by the QNetworkConfigurationManager::updateCompleted() * signal and sets d->networkConfigUpdated to TRUE. */ void slotNetworkConfigUpdated(); + /** + * This slot is called when the state of the network connection changed. + * It is connected to the QNetworkSession::stateChanged() signal. + */ + void slotConnectionStateChanged(QNetworkSession::State state); + /** * This slot is called when a device was added to the system. * @param udi the device UDI */ void slotDeviceAdded(const QString &udi); /** * This slot is called when a device was removed from the system. * @param udi the device UDI */ void slotDeviceRemoved(const QString &udi); private: QScopedPointer d; }; #endif diff --git a/core/smb4khardwareinterface_p.h b/core/smb4khardwareinterface_p.h index 930a11e..368e5d9 100644 --- a/core/smb4khardwareinterface_p.h +++ b/core/smb4khardwareinterface_p.h @@ -1,55 +1,56 @@ /*************************************************************************** Private helper class(es) for the hardware interface ------------------- begin : Die Jul 14 2015 copyright : (C) 2015-2017 by Alexander Reinholdt email : alexander.reinholdt@kdemail.net ***************************************************************************/ /*************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* * MA 02110-1335, USA * ***************************************************************************/ #ifndef SMB4KHARDWAREINTERFACE_P_H #define SMB4KHARDWAREINTERFACE_P_H // application specific includes #include "smb4khardwareinterface.h" // Qt includes #include #include +#include class Smb4KHardwareInterfacePrivate { public: QNetworkConfigurationManager networkConfigManager; - bool networkConfigUpdated; + QNetworkSession *networkSession; QStringList udis; #if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) QStringList mountPoints; #endif }; class Smb4KHardwareInterfaceStatic { public: Smb4KHardwareInterface instance; }; #endif