Skip to content

Commit 035c78b

Browse files
committedSep 25, 2018
Add native platform interface for usb storage events
Allows native interfaces to send a signal when a USB storage device is inserted/removed Refs #14481
1 parent c817e38 commit 035c78b

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,11 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
13211321
QgsGui::instance()->nativePlatformInterface()->initializeMainWindow( windowHandle(),
13221322
QgsApplication::applicationName(),
13231323
QgsApplication::organizationName(),
1324-
Qgis::QGIS_VERSION
1325-
);
1324+
Qgis::QGIS_VERSION );
1325+
connect( QgsGui::instance()->nativePlatformInterface(), &QgsNative::usbStorageNotification, this, [ = ]( const QString & path, bool inserted )
1326+
{
1327+
QgsDebugMsg( QStringLiteral( "USB STORAGE NOTIFICATION! %1 %2" ).arg( path ).arg( inserted ? QStringLiteral( "inserted" ) : QStringLiteral( "removed" ) ) );
1328+
} );
13261329

13271330
// setup application progress reports from task manager
13281331
connect( QgsApplication::taskManager(), &QgsTaskManager::taskAdded, this, []

‎src/native/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ SET(QGIS_NATIVE_SRCS
3636
qgsnative.cpp
3737
)
3838

39+
SET (QGIS_NATIVE_MOC_HDRS
40+
qgsnative.h
41+
)
42+
3943
IF(APPLE)
4044
SET(QGIS_APP_OBJC_SRCS
4145
mac/cocoainitializer.mm
@@ -54,6 +58,10 @@ IF(WIN32)
5458
../../external/wintoast/src/wintoastlib.cpp
5559
win/qgswinnative.cpp
5660
)
61+
SET (QGIS_NATIVE_MOC_HDRS
62+
${QGIS_NATIVE_MOC_HDRS}
63+
win/qgswinnative.h
64+
)
5765
SET(QGIS_NATIVE_SRCS ${QGIS_NATIVE_SRCS}
5866
${QGIS_APP_WIN32_SRCS}
5967
)
@@ -72,6 +80,8 @@ SET(QGIS_NATIVE_HDRS
7280
qgsnative.h
7381
)
7482

83+
QT5_WRAP_CPP(QGIS_NATIVE_MOC_SRCS ${QGIS_NATIVE_MOC_HDRS})
84+
7585
# install headers
7686

7787
IF(APPLE)
@@ -107,7 +117,7 @@ ENDIF(WIN32)
107117
#############################################################
108118
# qgis_native library
109119

110-
ADD_LIBRARY(qgis_native SHARED ${QGIS_NATIVE_SRCS} ${QGIS_NATIVE_HDRS})
120+
ADD_LIBRARY(qgis_native SHARED ${QGIS_NATIVE_SRCS} ${QGIS_NATIVE_MOC_SRCS} ${QGIS_NATIVE_HDRS} ${QGIS_NATIVE_MOC_HDRS})
111121
SET_PROPERTY(TARGET qgis_native PROPERTY POSITION_INDEPENDENT_CODE ON)
112122

113123
GENERATE_EXPORT_HEADER(

‎src/native/qgsnative.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <QImage>
2323
#include <QVariant>
2424
#include <vector>
25+
#include <QObject>
2526

2627
class QString;
2728
class QWindow;
@@ -33,8 +34,10 @@ class QWindow;
3334
* are implemented in subclasses to provide platform abstraction.
3435
* \since QGIS 3.0
3536
*/
36-
class NATIVE_EXPORT QgsNative
37+
class NATIVE_EXPORT QgsNative : public QObject
3738
{
39+
Q_OBJECT
40+
3841
public:
3942

4043
//! Native interface capabilities
@@ -215,6 +218,21 @@ class NATIVE_EXPORT QgsNative
215218
* \since QGIS 3.4
216219
*/
217220
virtual void onRecentProjectsChanged( const std::vector< RecentProjectProperties > &recentProjects );
221+
222+
signals:
223+
224+
/**
225+
* Emitted whenever a USB storage device has been inserted or removed.
226+
*
227+
* The \a path argument gives the file path to the device (if available).
228+
*
229+
* If \a inserted is true then the device was inserted. If \a inserted is false then
230+
* the device was removed.
231+
*
232+
* \since QGIS 3.4
233+
*/
234+
void usbStorageNotification( const QString &path, bool inserted );
235+
218236
};
219237

220238
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsNative::Capabilities )

‎src/native/win/qgswinnative.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
#include <QString>
2222
#include <QDir>
2323
#include <QWindow>
24+
#include <QAbstractEventDispatcher>
2425
#include <QtWinExtras/QWinTaskbarButton>
2526
#include <QtWinExtras/QWinTaskbarProgress>
2627
#include <QtWinExtras/QWinJumpList>
2728
#include <QtWinExtras/QWinJumpListItem>
2829
#include <QtWinExtras/QWinJumpListCategory>
2930
#include "wintoastlib.h"
31+
#include <Dbt.h>
3032

3133
QgsNative::Capabilities QgsWinNative::capabilities() const
3234
{
@@ -57,6 +59,10 @@ void QgsWinNative::initializeMainWindow( QWindow *window,
5759
mWinToastInitialized = true;
5860
mCapabilities = mCapabilities | NativeDesktopNotifications;
5961
}
62+
63+
mNativeEventFilter = new QgsWinNativeEventFilter();
64+
QAbstractEventDispatcher::instance()->installNativeEventFilter( mNativeEventFilter );
65+
connect( mNativeEventFilter, &QgsWinNativeEventFilter::usbStorageNotification, this, &QgsNative::usbStorageNotification );
6066
}
6167

6268
void QgsWinNative::cleanup()
@@ -156,3 +162,27 @@ QgsNative::NotificationResult QgsWinNative::showDesktopNotification( const QStri
156162

157163
return result;
158164
}
165+
166+
bool QgsWinNativeEventFilter::nativeEventFilter( const QByteArray &, void *message, long * )
167+
{
168+
MSG *pWindowsMessage = static_cast<MSG *>( message );
169+
unsigned int wParam = pWindowsMessage->wParam;
170+
if ( wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE )
171+
{
172+
long lParam = pWindowsMessage->lParam;
173+
unsigned long deviceType = reinterpret_cast<DEV_BROADCAST_HDR *>( lParam )->dbch_devicetype;
174+
if ( deviceType == DBT_DEVTYP_VOLUME )
175+
{
176+
unsigned long unitmask = reinterpret_cast<DEV_BROADCAST_VOLUME *>( lParam )->dbcv_unitmask;
177+
for ( int i = 0; i < 32; ++i )
178+
{
179+
if ( ( unitmask & ( 1 << i ) ) != 0 )
180+
{
181+
const QChar drive( 65 + i );
182+
emit usbStorageNotification( QStringLiteral( "%1:/" ).arg( drive ), wParam == DBT_DEVICEARRIVAL );
183+
}
184+
}
185+
}
186+
}
187+
return false;
188+
}

‎src/native/win/qgswinnative.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,30 @@
1919
#define QGSMACNATIVE_H
2020

2121
#include "qgsnative.h"
22-
#include <windows.h>
23-
#include <shlobj.h>
22+
#include <QAbstractNativeEventFilter>
23+
24+
#include <Windows.h>
25+
#include <ShlObj.h>
2426
#pragma comment(lib,"Shell32.lib")
2527

2628
class QWinTaskbarButton;
2729
class QWinTaskbarProgress;
2830
class QWindow;
2931

32+
33+
class QgsWinNativeEventFilter : public QObject, public QAbstractNativeEventFilter
34+
{
35+
Q_OBJECT
36+
public:
37+
38+
bool nativeEventFilter( const QByteArray &eventType, void *message, long * ) override;
39+
40+
signals:
41+
42+
void usbStorageNotification( const QString &path, bool inserted );
43+
};
44+
45+
3046
class NATIVE_EXPORT QgsWinNative : public QgsNative
3147
{
3248
public:
@@ -49,6 +65,8 @@ class NATIVE_EXPORT QgsWinNative : public QgsNative
4965
bool mWinToastInitialized = false;
5066
QWinTaskbarButton *mTaskButton = nullptr;
5167
QWinTaskbarProgress *mTaskProgress = nullptr;
68+
QgsWinNativeEventFilter *mNativeEventFilter = nullptr;
69+
5270
};
5371

5472
#endif // QGSMACNATIVE_H

0 commit comments

Comments
 (0)
Please sign in to comment.