Skip to content

Commit c79a9a1

Browse files
committedNov 12, 2018
[needs-docs] Implement native file properties support for Windows
Allows opening the native file explorer file/directory properties dialog for browser items
1 parent 9f1d00d commit c79a9a1

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed
 

‎src/native/win/qgswinnative.cpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@
2929
#include <QtWinExtras/QWinJumpListCategory>
3030
#include "wintoastlib.h"
3131
#include <Dbt.h>
32+
#include <memory>
33+
34+
35+
struct ITEMIDLISTDeleter
36+
{
37+
void operator()( ITEMIDLIST *pidl )
38+
{
39+
ILFree( pidl );
40+
}
41+
};
42+
43+
using ITEMIDLIST_unique_ptr = std::unique_ptr< ITEMIDLIST, ITEMIDLISTDeleter>;
44+
45+
3246

3347
QgsNative::Capabilities QgsWinNative::capabilities() const
3448
{
@@ -40,6 +54,7 @@ void QgsWinNative::initializeMainWindow( QWindow *window,
4054
const QString &organizationName,
4155
const QString &version )
4256
{
57+
mWindow = window;
4358
if ( mTaskButton )
4459
return; // already initialized!
4560

@@ -69,24 +84,46 @@ void QgsWinNative::cleanup()
6984
{
7085
if ( mWinToastInitialized )
7186
WinToastLib::WinToast::instance()->clear();
87+
mWindow = nullptr;
7288
}
7389

74-
void QgsWinNative::openFileExplorerAndSelectFile( const QString &path )
90+
std::unique_ptr< wchar_t[] > pathToWChar( const QString &path )
7591
{
7692
const QString nativePath = QDir::toNativeSeparators( path );
7793

78-
wchar_t *pathArray = new wchar_t[static_cast< uint>( nativePath.length() + 1 )];
79-
nativePath.toWCharArray( pathArray );
94+
std::unique_ptr< wchar_t[] > pathArray( new wchar_t[static_cast< uint>( nativePath.length() + 1 )] );
95+
nativePath.toWCharArray( pathArray.get() );
8096
pathArray[nativePath.length()] = 0;
97+
return pathArray;
98+
}
8199

82-
ITEMIDLIST *pidl = ILCreateFromPathW( pathArray );
100+
void QgsWinNative::openFileExplorerAndSelectFile( const QString &path )
101+
{
102+
std::unique_ptr< wchar_t[] > pathArray = pathToWChar( path );
103+
ITEMIDLIST_unique_ptr pidl( ILCreateFromPathW( pathArray.get() ) );
83104
if ( pidl )
84105
{
85-
SHOpenFolderAndSelectItems( pidl, 0, nullptr, 0 );
86-
ILFree( pidl );
106+
SHOpenFolderAndSelectItems( pidl.get(), 0, nullptr, 0 );
107+
pidl.reset();
87108
}
109+
}
88110

89-
delete[] pathArray;
111+
void QgsWinNative::showFileProperties( const QString &path )
112+
{
113+
std::unique_ptr< wchar_t[] > pathArray = pathToWChar( path );
114+
ITEMIDLIST_unique_ptr pidl( ILCreateFromPathW( pathArray.get() ) );
115+
if ( pidl )
116+
{
117+
SHELLEXECUTEINFO info{ sizeof( info ) };
118+
if ( mWindow )
119+
info.hwnd = reinterpret_cast<HWND>( mWindow->winId() );
120+
info.nShow = SW_SHOWNORMAL;
121+
info.fMask = SEE_MASK_INVOKEIDLIST;
122+
info.lpIDList = pidl.get();
123+
info.lpVerb = "properties";
124+
125+
ShellExecuteEx( &info );
126+
}
90127
}
91128

92129
void QgsWinNative::showUndefinedApplicationProgress()

‎src/native/win/qgswinnative.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class NATIVE_EXPORT QgsWinNative : public QgsNative
5757
const QString &version ) override;
5858
void cleanup() override;
5959
void openFileExplorerAndSelectFile( const QString &path ) override;
60+
void showFileProperties( const QString &path ) override;
6061
void showUndefinedApplicationProgress() override;
6162
void setApplicationProgress( double progress ) override;
6263
void hideApplicationProgress() override;
@@ -65,7 +66,8 @@ class NATIVE_EXPORT QgsWinNative : public QgsNative
6566

6667
private:
6768

68-
Capabilities mCapabilities = nullptr;
69+
QWindow *mWindow = nullptr;
70+
Capabilities mCapabilities = NativeFilePropertiesDialog;
6971
bool mWinToastInitialized = false;
7072
QWinTaskbarButton *mTaskButton = nullptr;
7173
QWinTaskbarProgress *mTaskProgress = nullptr;

0 commit comments

Comments
 (0)