1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qwinrtservices.h"
41 #include "qwinrtfileengine.h"
42 #include <QtCore/QUrl>
43 #include <QtCore/QDir>
44 #include <QtCore/QCoreApplication>
45 #include <QtCore/qfunctions_winrt.h>
46 #include <private/qeventdispatcher_winrt_p.h>
47
48 #include <wrl.h>
49 #include <windows.foundation.h>
50 #include <windows.storage.h>
51 #include <windows.system.h>
52 using namespace Microsoft::WRL;
53 using namespace Microsoft::WRL::Wrappers;
54 using namespace ABI::Windows::Foundation;
55 using namespace ABI::Windows::Storage;
56 using namespace ABI::Windows::System;
57
58 QT_BEGIN_NAMESPACE
59
60 class QWinRTServicesPrivate
61 {
62 public:
63 ComPtr<IUriRuntimeClassFactory> uriFactory;
64 ComPtr<IStorageFileStatics> fileFactory;
65 ComPtr<ILauncherStatics> launcher;
66 };
67
QWinRTServices()68 QWinRTServices::QWinRTServices()
69 : d_ptr(new QWinRTServicesPrivate)
70 {
71 Q_D(QWinRTServices);
72
73 HRESULT hr;
74 hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Foundation_Uri).Get(),
75 IID_PPV_ARGS(&d->uriFactory));
76 Q_ASSERT_X(SUCCEEDED(hr), Q_FUNC_INFO, qPrintable(qt_error_string(hr)));
77
78 hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Storage_StorageFile).Get(),
79 IID_PPV_ARGS(&d->fileFactory));
80 Q_ASSERT_X(SUCCEEDED(hr), Q_FUNC_INFO, qPrintable(qt_error_string(hr)));
81
82 hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_System_Launcher).Get(),
83 IID_PPV_ARGS(&d->launcher));
84 Q_ASSERT_X(SUCCEEDED(hr), Q_FUNC_INFO, qPrintable(qt_error_string(hr)));
85 }
86
openUrl(const QUrl & url)87 bool QWinRTServices::openUrl(const QUrl &url)
88 {
89 Q_D(QWinRTServices);
90
91 ComPtr<IUriRuntimeClass> uri;
92 QString urlString = url.toString();
93 HStringReference uriString(reinterpret_cast<LPCWSTR>(urlString.utf16()),
94 uint(urlString.length()));
95 HRESULT hr = d->uriFactory->CreateUri(uriString.Get(), &uri);
96 RETURN_FALSE_IF_FAILED("Failed to create URI from QUrl.");
97
98 boolean result;
99 hr = QEventDispatcherWinRT::runOnXamlThread([this, d, uri, &result]() {
100 ComPtr<IAsyncOperation<bool>> op;
101 HRESULT hr = d->launcher->LaunchUriAsync(uri.Get(), &op);
102 RETURN_HR_IF_FAILED("Failed to start URI launch.");
103
104 hr = QWinRTFunctions::await(op, &result);
105 RETURN_HR_IF_FAILED("Failed to launch URI.");
106 return hr;
107 });
108 RETURN_FALSE_IF_FAILED("Failed to launch URI from Xaml thread.");
109
110 return result;
111 }
112
openDocument(const QUrl & url)113 bool QWinRTServices::openDocument(const QUrl &url)
114 {
115 Q_D(QWinRTServices);
116
117 HRESULT hr;
118 ComPtr<IStorageFile> file;
119 ComPtr<IStorageItem> item = QWinRTFileEngineHandler::registeredFile(url.toLocalFile());
120 if (item) {
121 hr = item.As(&file);
122 if (FAILED(hr))
123 qErrnoWarning(hr, "Failed to cast picked item to a file");
124 }
125 if (!file) {
126 const QString pathString = QDir::toNativeSeparators(url.toLocalFile());
127 HStringReference path(reinterpret_cast<LPCWSTR>(pathString.utf16()),
128 uint(pathString.length()));
129 ComPtr<IAsyncOperation<StorageFile *>> op;
130 hr = d->fileFactory->GetFileFromPathAsync(path.Get(), &op);
131 RETURN_FALSE_IF_FAILED("Failed to initialize file URI.");
132
133 hr = QWinRTFunctions::await(op, file.GetAddressOf());
134 RETURN_FALSE_IF_FAILED("Failed to get file URI.");
135 }
136
137 boolean result;
138 {
139 hr = QEventDispatcherWinRT::runOnXamlThread([this, d, file, &result]() {
140 ComPtr<IAsyncOperation<bool>> op;
141 HRESULT hr = d->launcher->LaunchFileAsync(file.Get(), &op);
142 RETURN_HR_IF_FAILED("Failed to start file launch.");
143
144 hr = QWinRTFunctions::await(op, &result);
145 RETURN_HR_IF_FAILED("Failed to launch file.");
146 return hr;
147 });
148 RETURN_FALSE_IF_FAILED("Failed to launch file from Xaml thread.");
149 }
150
151 return result;
152 }
153
154 QT_END_NAMESPACE
155