Commit c2e6729f authored by Andreas Traczyk's avatar Andreas Traczyk

connectivity: monitor for network changes

Change-Id: Id54979a7837459d8165a3cfc05bf2498c46cbc88
parent bf35b0ca
......@@ -24,26 +24,13 @@
#include "callwidget.h"
#include "ui_callwidget.h"
#include <QComboBox>
#include <QDesktopServices>
#include <QScrollBar>
#include <QWebEngineScript>
#include <QMimeData>
#include <algorithm>
#include <memory>
#include <qrencode.h>
//ERROR is defined in windows.h
// client
#include "utils.h"
#ifdef Q_OS_WIN
//ERROR is defined in windows.h
#undef ERROR
#undef interface
// lrc
#include "globalinstances.h"
// client
#endif
#include "animationhelpers.h"
#include "conversationitemdelegate.h"
#include "globalsystemtray.h"
......@@ -53,6 +40,20 @@
#include "ringthemeutils.h"
#include "settingskey.h"
// lrc
#include "globalinstances.h"
#include <qrencode.h>
#include <QComboBox>
#include <QDesktopServices>
#include <QScrollBar>
#include <QWebEngineScript>
#include <QMimeData>
#include <algorithm>
#include <memory>
CallWidget::CallWidget(QWidget* parent) :
NavWidget(parent),
ui(new Ui::CallWidget),
......
/***************************************************************************
* Copyright (C) 2019 by Savoir-faire Linux *
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> *
* *
* 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 3 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, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
#include "connectivitymonitor.h"
#include "lrcinstance.h"
#include <atlbase.h>
#include <netlistmgr.h>
class NetworkEventHandler : public INetworkListManagerEvents
{
public:
NetworkEventHandler() : m_lRefCnt(1) {};
virtual ~NetworkEventHandler() {};
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) {
HRESULT hr = S_OK;
if (IsEqualIID(riid, IID_IUnknown)) {
*ppvObject = (IUnknown *) this;
} else if (IsEqualIID(riid, IID_INetworkListManagerEvents)) {
*ppvObject = (INetworkListManagerEvents *) this;
} else {
hr = E_NOINTERFACE;
}
return hr;
};
ULONG STDMETHODCALLTYPE AddRef() {
return (ULONG) InterlockedIncrement(&m_lRefCnt);
};
ULONG STDMETHODCALLTYPE Release() {
LONG res = InterlockedDecrement(&m_lRefCnt);
if (res == 0) {
delete this;
}
return (ULONG) res;
};
virtual HRESULT STDMETHODCALLTYPE
ConnectivityChanged(NLM_CONNECTIVITY newConnectivity) {
qDebug() << "connectivity changed: " << newConnectivity;
if (connectivityChangedCb_) {
connectivityChangedCb_();
}
return S_OK;
};
void setOnConnectivityChangedCallBack(std::function<void()>&& cb) {
connectivityChangedCb_ = cb;
};
private:
LONG m_lRefCnt;
std::function<void()> connectivityChangedCb_;
};
ConnectivityMonitor::ConnectivityMonitor(QObject* parent)
: QObject(parent)
{
CoInitialize(NULL);
IUnknown *pUnknown = NULL;
HRESULT hr = CoCreateInstance(CLSID_NetworkListManager,
NULL, CLSCTX_ALL, IID_IUnknown, (void **) &pUnknown);
if (FAILED(hr)) {
return;
}
pNetworkListManager_ = NULL;
hr = pUnknown->QueryInterface(IID_INetworkListManager,
(void **) &pNetworkListManager_);
if (FAILED(hr)) {
destroy();
pUnknown->Release();
return;
}
pCPContainer_ = NULL;
hr = pNetworkListManager_->QueryInterface(IID_IConnectionPointContainer,
(void **) &pCPContainer_);
if (FAILED(hr)) {
destroy();
pUnknown->Release();
return;
}
pConnectPoint_ = NULL;
hr = pCPContainer_->FindConnectionPoint(IID_INetworkListManagerEvents,
&pConnectPoint_);
if (SUCCEEDED(hr)) {
cookie_ = NULL;
netEventHandler_ = new NetworkEventHandler;
netEventHandler_->setOnConnectivityChangedCallBack(
[this] {
emit connectivityChanged();
});
hr = pConnectPoint_->Advise((IUnknown *) netEventHandler_, &cookie_);
} else {
destroy();
}
pUnknown->Release();
}
void
ConnectivityMonitor::destroy()
{
if (pConnectPoint_) {
pConnectPoint_->Unadvise(cookie_);
pConnectPoint_->Release();
}
if (pCPContainer_) {
pCPContainer_->Release();
}
if (pNetworkListManager_) {
pNetworkListManager_->Release();
pNetworkListManager_ = NULL;
}
}
ConnectivityMonitor::~ConnectivityMonitor()
{
destroy();
CoUninitialize();
}
bool
ConnectivityMonitor::isOnline()
{
if (!pNetworkListManager_) {
return false;
}
VARIANT_BOOL IsConnect = VARIANT_FALSE;
HRESULT hr = pNetworkListManager_->get_IsConnectedToInternet(&IsConnect);
if (SUCCEEDED(hr)) {
return IsConnect == VARIANT_TRUE;
}
return false;
}
\ No newline at end of file
/***************************************************************************
* Copyright (C) 2019 by Savoir-faire Linux *
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> *
* *
* 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 3 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, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
#pragma once
#include <QObject>
class ConnectivityMonitor : public QObject
{
Q_OBJECT
public:
explicit ConnectivityMonitor(QObject* parent = 0);
~ConnectivityMonitor();
bool isOnline();
signals:
void connectivityChanged();
private:
void destroy();
struct INetworkListManager* pNetworkListManager_;
struct IConnectionPointContainer* pCPContainer_;
struct IConnectionPoint* pConnectPoint_;
class NetworkEventHandler* netEventHandler_;
unsigned long cookie_;
};
\ No newline at end of file
......@@ -81,11 +81,9 @@ MainWindow::MainWindow(QWidget* parent)
connect(&GlobalSystemTray::instance(), SIGNAL(messageClicked()), this, SLOT(notificationClicked()));
connect(&netManager_, &QNetworkConfigurationManager::onlineStateChanged,
[=](bool online) {
Q_UNUSED(online);
LRCInstance::connectivityChanged();
});
connectivityMonitor_ = std::make_unique<ConnectivityMonitor>(this);
connect(connectivityMonitor_.get(), &ConnectivityMonitor::connectivityChanged,
[this] { LRCInstance::connectivityChanged(); });
auto flags_ = windowFlags();
......@@ -209,6 +207,7 @@ MainWindow::MainWindow(QWidget* parent)
MainWindow::~MainWindow()
{
connectivityMonitor_.reset();
updateTimer_->stop();
delete ui;
}
......@@ -321,6 +320,7 @@ void MainWindow::closeEvent(QCloseEvent* event)
settings.setValue(SettingsKey::windowState, saveState());
this->disconnect(screenChangedConnection_);
QMainWindow::closeEvent(event);
connectivityMonitor_.reset();
}
}
......
......@@ -18,15 +18,14 @@
#pragma once
#include "navwidget.h"
#include "settingswidget.h"
#include "utils.h"
#include "connectivitymonitor.h"
#include "globalsystemtray.h"
#include <QMainWindow>
#include <QMouseEvent>
#include <QNetworkConfigurationManager>
#include "navwidget.h"
#include "settingswidget.h"
#include "utils.h"
static constexpr char IDM_ABOUTBOX = 0x0010;
......@@ -80,6 +79,8 @@ private:
explicit MainWindow(QWidget* parent = 0);
~MainWindow();
Ui::MainWindow* ui;
void setWindowSize(ScreenEnum scr, bool firstUse = false);
ScreenEnum lastScr_;
int lastAccountCount_;
......@@ -92,8 +93,8 @@ private:
QAction* settingsAction_;
QAction* exitAction_;
Ui::MainWindow* ui;
QNetworkConfigurationManager netManager_;
std::unique_ptr<ConnectivityMonitor> connectivityMonitor_;
QMetaObject::Connection screenChangedConnection_;
QTimer *updateTimer_;
......
......@@ -238,6 +238,7 @@ del /s /q $(OutDir)\Jami.exp</Command>
<ClCompile Include="bannedlistmodel.cpp" />
<ClCompile Include="animationhelpers.cpp" />
<ClCompile Include="callaudioonlyavataroverlay.cpp" />
<ClCompile Include="connectivitymonitor.cpp" />
<ClCompile Include="contactpicker.cpp" />
<ClCompile Include="contactpickeritemdelegate.cpp" />
<ClCompile Include="currentaccountcombobox.cpp" />
......@@ -468,6 +469,10 @@ del /s /q $(OutDir)\Jami.exp</Command>
<IncludePath Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\GeneratedFiles\$(ConfigurationName);.\GeneratedFiles;.;$(ProjectDir)..\ring-daemon\contrib\msvc\include;$(ProjectDir)..\daemon\contrib\msvc\include;$(ProjectDir)..\ring-lrc\src;$(ProjectDir)..\lrc\src;$(ProjectDir)qrencode-win32\qrencode-win32;$(QTDIR)\include;$(QTDIR)\include\QtSvg;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtWinExtras;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtXml;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtWebEngineWidgets;$(QTDIR)\include\QtWebChannel;$(QTDIR)\include\QtCore;$(QTDIR)\mkspecs\win32-msvc;.\release</IncludePath>
<IncludePath Condition="'$(Configuration)|$(Platform)'=='ReleaseCompile|x64'">.\GeneratedFiles\$(ConfigurationName);.\GeneratedFiles;.;$(ProjectDir)..\ring-daemon\contrib\msvc\include;$(ProjectDir)..\daemon\contrib\msvc\include;$(ProjectDir)..\ring-lrc\src;$(ProjectDir)..\lrc\src;$(ProjectDir)winsparkle\include;$(ProjectDir)qrencode-win32\qrencode-win32;$(QTDIR)\include;$(QTDIR)\include\QtSvg;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtWinExtras;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtXml;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtWebEngineWidgets;$(QTDIR)\include\QtWebChannel;$(QTDIR)\include\QtCore;$(QTDIR)\mkspecs\win32-msvc;.\release</IncludePath>
</QtMoc>
<QtMoc Include="connectivitymonitor.h">
<IncludePath Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\GeneratedFiles\$(ConfigurationName);.\GeneratedFiles;.;$(ProjectDir)..\ring-daemon\contrib\msvc\include;$(ProjectDir)..\daemon\contrib\msvc\include;$(ProjectDir)..\ring-lrc\src;$(ProjectDir)..\lrc\src;$(ProjectDir)qrencode-win32\qrencode-win32;$(QTDIR)\include;$(QTDIR)\include\QtSvg;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtWinExtras;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtXml;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtWebEngineWidgets;$(QTDIR)\include\QtWebChannel;$(QTDIR)\include\QtCore;$(QTDIR)\mkspecs\win32-msvc;.\release</IncludePath>
<IncludePath Condition="'$(Configuration)|$(Platform)'=='ReleaseCompile|x64'">.\GeneratedFiles\$(ConfigurationName);.\GeneratedFiles;.;$(ProjectDir)..\ring-daemon\contrib\msvc\include;$(ProjectDir)..\daemon\contrib\msvc\include;$(ProjectDir)..\ring-lrc\src;$(ProjectDir)..\lrc\src;$(ProjectDir)winsparkle\include;$(ProjectDir)qrencode-win32\qrencode-win32;$(QTDIR)\include;$(QTDIR)\include\QtSvg;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtWinExtras;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtXml;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtWebEngineWidgets;$(QTDIR)\include\QtWebChannel;$(QTDIR)\include\QtCore;$(QTDIR)\mkspecs\win32-msvc;.\release</IncludePath>
</QtMoc>
<ClInclude Include="pixbufmanipulator.h" />
<QtMoc Include="ringbutton.h">
</QtMoc>
......
......@@ -231,6 +231,9 @@
<ClCompile Include="accountmigrationdialog.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="connectivitymonitor.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<QtMoc Include="aboutdialog.h">
......@@ -404,6 +407,9 @@
<QtMoc Include="accountmigrationdialog.h">
<Filter>Header Files</Filter>
</QtMoc>
<QtMoc Include="connectivitymonitor.h">
<Filter>Header Files</Filter>
</QtMoc>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="debug\moc_predefs.h.cbt">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment