This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Launcher_Win32.cpp
99 lines (69 loc) · 2.53 KB
/
Launcher_Win32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (C) 2012-2015, Ali Baharev
// All rights reserved.
// This code is published under the GNU Lesser General Public License.
#ifdef _WIN32
#include <QDebug>
#include <QDir>
#include <QString>
#include <windows.h>
#include <ShellApi.h>
#include <Shlwapi.h>
#include "Launcher.hpp"
bool openWithDefaultApp(const QString& file) {
int ret = (int) ShellExecute(GetDesktopWindow(), L"open", (wchar_t*)file.utf16(), NULL, NULL, SW_SHOWNORMAL);
return ret > 32;
}
void openDirectoryWithFileManager(const QString& directory) {
ShellExecute(GetDesktopWindow(), L"explore", (wchar_t*)directory.utf16(), NULL, NULL, SW_SHOWMAXIMIZED);
}
QString getAssociatedApp(const wchar_t* extension, const wchar_t* word = L"open") {
DWORD dwSize = 512;
wchar_t appPath[512] = { 0 };
HRESULT hr = AssocQueryStringW(ASSOCF_VERIFY,
ASSOCSTR_EXECUTABLE,
extension,
word,
appPath,
&dwSize );
QString app;
switch (hr) {
case S_OK:
app = QString::fromWCharArray(appPath);
app = QDir::toNativeSeparators(app);
qDebug() << "Extension" << QString::fromWCharArray(extension);
qDebug() << "App" << app;
break;
case E_POINTER:
qDebug() << "AssocQueryStringW" << "E_POINTER";
break;
case S_FALSE:
qDebug() << "AssocQueryStringW" << "S_FALSE";
break;
default:
qDebug() << "unknown: " << hr << endl;
}
return app;
}
bool hasAssociatedApp(const wchar_t* ext) {
QString extension = QString::fromWCharArray(ext);
qDebug() << "Looking for the associated app for extension " << extension;
QString app = getAssociatedApp(ext);
return !app.isEmpty();
}
bool openWithShortAppname(const QString& nativeFilename, const QString& shortAppName, const QString& ) {
QString file("\""+nativeFilename+"\"");
qDebug() << "Attempting to execute command" << shortAppName << file;
int ret = (int) ShellExecute(GetDesktopWindow(), L"open", (wchar_t*)shortAppName.utf16(), (wchar_t*)file.utf16(), NULL, SW_SHOWNORMAL);
return ret > 32;
}
bool openWithAppAssoc(const QString& file, const QString& extension) {
QString app = getAssociatedApp((const wchar_t*)extension.utf16());
if (app.isEmpty()) {
return false;
}
else {
QString dummy; // TODO Default arg instead?
return openWithShortAppname(file, app, dummy);
}
}
#endif