diff options
author | Andrew Knight <andrew.knight@digia.com> | 2014-05-19 09:33:46 +0300 |
---|---|---|
committer | Andrew Knight <andrew.knight@digia.com> | 2014-05-19 09:06:27 +0200 |
commit | aa18322de9c8aab79fd3fd4163a3e8795a1565fd (patch) | |
tree | b2c673ff7dbebf91721b41acc0aaad53357c5f30 | |
parent | 835c86e72dd4fe548e98beb8695f5ad8d840d54e (diff) |
Add privacy policy link for WinRT
For apps which use the network, the Windows Store requires that a privacy
policy link appear in the Settings Charm. As there is no Qt API for this,
add some platform-specific code to provide the policy link.
Change-Id: Iac7715ace86cf8b414d102eb724678d0ffaf2452
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@digia.com>
Reviewed-by: Oliver Wolff <oliver.wolff@digia.com>
-rw-r--r-- | src/main.cpp | 17 | ||||
-rw-r--r-- | src/src.pri | 5 | ||||
-rw-r--r-- | src/winrtcharms.cpp | 199 | ||||
-rw-r--r-- | src/winrtcharms.h | 66 |
4 files changed, 286 insertions, 1 deletions
diff --git a/src/main.cpp b/src/main.cpp index 95f18dd..629330a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the examples of the Qt Toolkit. @@ -46,6 +46,11 @@ #include <QtQuick/QQuickWindow> #include <QtQml/QQmlApplicationEngine> +#if defined(Q_OS_WINRT) && !defined(Q_OS_WINPHONE) +# include "winrtcharms.h" +# include <QtGui/QDesktopServices> +#endif // Q_OS_WINRT && !Q_OS_WINPHONE + #include "daymodel.h" #include "citymodel.h" #include "cities.h" @@ -100,6 +105,16 @@ int main(int argc, char *argv[]) qtTranslator.load("QuickForecast_" + QLocale::system().name(), ":/translations/"); app.installTranslator(&qtTranslator); +#if defined(Q_OS_WINRT) && !defined(Q_OS_WINPHONE) + // WinRT requires that we install a privacy policy link to the Charms bar when the network is + // used, so create a Charms menu item and connect a slot to handle it + WinRTSettingsCharm settingsCharm; + settingsCharm.addItem(app.tr("Privacy Policy")); + QObject::connect(&settingsCharm, &WinRTSettingsCharm::itemClicked, [](const QString &) { + QDesktopServices::openUrl(QUrl("http://qt.digia.com/Digia-Legal-Notice--Privacy-Policy/")); + }); +#endif // Q_OS_WINRT && !Q_OS_WINPHONE + #ifndef Q_OS_IOS //QTBUG-34490 QFontDatabase::addApplicationFont(":/weatherapp/fonts/OpenSans-Bold.ttf"); QFontDatabase::addApplicationFont(":/weatherapp/fonts/OpenSans-Semibold.ttf"); diff --git a/src/src.pri b/src/src.pri index 5a3bbdf..af6159b 100644 --- a/src/src.pri +++ b/src/src.pri @@ -14,3 +14,8 @@ HEADERS += \ $$PWD/citieslistmodel.h \ $$PWD/weatherimageprovider.h \ $$PWD/applicationpaths.h + +winrt:!winphone { + SOURCES += $$PWD/winrtcharms.cpp + HEADERS += $$PWD/winrtcharms.h +} diff --git a/src/winrtcharms.cpp b/src/winrtcharms.cpp new file mode 100644 index 0000000..e46ec46 --- /dev/null +++ b/src/winrtcharms.cpp @@ -0,0 +1,199 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "winrtcharms.h" + +#include <QStringList> + +#include <windows.h> +#include <roapi.h> +#include <wrl.h> +#include <windows.ui.popups.h> +#include <windows.ui.applicationsettings.h> +#include <windows.foundation.collections.h> + +using namespace Microsoft::WRL; +using namespace Microsoft::WRL::Wrappers; +using namespace ABI::Windows::Foundation; +using namespace ABI::Windows::Foundation::Collections; +using namespace ABI::Windows::UI::ApplicationSettings; +using namespace ABI::Windows::UI::Popups; + +class WinRTSettingsCharmPrivate +{ +public: + WinRTSettingsCharmPrivate(WinRTSettingsCharm *q) + : q_ptr(q) + { + } + + ComPtr<ISettingsPane> settingsPane; + EventRegistrationToken cookie; + QStringList items; + + HRESULT settingsRequested(ISettingsPane *, ISettingsPaneCommandsRequestedEventArgs *args) + { + ComPtr<ISettingsPaneCommandsRequest> request; + HRESULT hr = args->get_Request(&request); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to get settings command request."); + return S_OK; + } + + ComPtr<IUICommandFactory> factory; + hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_UI_Popups_UICommand).Get(), + IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to get command factory."); + return S_OK; + } + + ComPtr<IVector<SettingsCommand *>> commands; + hr = request->get_ApplicationCommands(&commands); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to get command list."); + return S_OK; + } + + foreach (const QString &item, items) { + ComPtr<IUICommand> command; + HStringReference label(reinterpret_cast<const wchar_t *>(item.utf16()), item.length()); + hr = factory->Create(label.Get(), &command); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to create UI command."); + return S_OK; + } + + hr = command->put_Invoked(Callback<IUICommandInvokedHandler>( + this, &WinRTSettingsCharmPrivate::commandInvoked).Get()); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to set command invoked handler."); + return S_OK; + } + + hr = commands->Append(command.Get()); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to append privacy policy command."); + return S_OK; + } + } + + return S_OK; + } + + HRESULT commandInvoked(IUICommand *command) + { + Q_Q(WinRTSettingsCharm); + + HString label; + HRESULT hr = command->get_Label(label.GetAddressOf()); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to get invoked command label."); + return S_OK; + } + + emit q->itemClicked(QString::fromWCharArray(label.GetRawBuffer(Q_NULLPTR))); + + return S_OK; + } + +private: + WinRTSettingsCharm *q_ptr; + Q_DECLARE_PUBLIC(WinRTSettingsCharm) +}; + +WinRTSettingsCharm::WinRTSettingsCharm() + : d_ptr(new WinRTSettingsCharmPrivate(this)) +{ + Q_D(WinRTSettingsCharm); + + ComPtr<ISettingsPaneStatics> factory; + HRESULT hr = RoGetActivationFactory(Wrappers::HString::MakeReference(RuntimeClass_Windows_UI_ApplicationSettings_SettingsPane).Get(), + IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to get settings pane factory."); + return; + } + + hr = factory->GetForCurrentView(&d->settingsPane); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to get settings pane from factory."); + return; + } + + hr = d->settingsPane->add_CommandsRequested(Callback<ITypedEventHandler<SettingsPane *, SettingsPaneCommandsRequestedEventArgs *>>( + d, &WinRTSettingsCharmPrivate::settingsRequested).Get(), &d->cookie); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to add commands requested callback."); + return; + } +} + +WinRTSettingsCharm::~WinRTSettingsCharm() +{ + Q_D(WinRTSettingsCharm); + + if (!d->settingsPane) + return; + + HRESULT hr = d->settingsPane->remove_CommandsRequested(d->cookie); + if (FAILED(hr)) { + qErrnoWarning(hr, "Failed to remove settings pane callback."); + return; + } +} + +QStringList WinRTSettingsCharm::items() const +{ + Q_D(const WinRTSettingsCharm); + return d->items; +} + +void WinRTSettingsCharm::addItem(const QString &label) +{ + Q_D(WinRTSettingsCharm); + d->items.append(label); +} + +void WinRTSettingsCharm::removeItem(const QString &label) +{ + Q_D(WinRTSettingsCharm); + d->items.removeOne(label); +} diff --git a/src/winrtcharms.h b/src/winrtcharms.h new file mode 100644 index 0000000..feddf7d --- /dev/null +++ b/src/winrtcharms.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINRTCHARMS_H +#define WINRTCHARMS_H + +#include <QtCore/QObject> + +class WinRTSettingsCharmPrivate; +class WinRTSettingsCharm : public QObject +{ + Q_OBJECT +public: + WinRTSettingsCharm(); + ~WinRTSettingsCharm(); + + QStringList items() const; + void addItem(const QString &label); + void removeItem(const QString &label); + +signals: + void itemClicked(const QString &label); + +private: + QScopedPointer<WinRTSettingsCharmPrivate> d_ptr; + Q_DECLARE_PRIVATE(WinRTSettingsCharm) +}; + +#endif // WINRTCHARMS_H |