diff --git a/autotests/alternativesmodeltest.h b/autotests/alternativesmodeltest.h --- a/autotests/alternativesmodeltest.h +++ b/autotests/alternativesmodeltest.h @@ -26,6 +26,7 @@ private Q_SLOTS: void runJobTest(); void bigBufferTest(); + void platformFilteringTest(); }; #endif diff --git a/autotests/alternativesmodeltest.cpp b/autotests/alternativesmodeltest.cpp --- a/autotests/alternativesmodeltest.cpp +++ b/autotests/alternativesmodeltest.cpp @@ -50,7 +50,8 @@ QFile::remove(tempfile); QJsonObject input = QJsonObject { {QStringLiteral("urls"), QJsonArray {QStringLiteral("http://kde.org")} }, - {QStringLiteral("mimeType"), QStringLiteral("dummy/thing") } + {QStringLiteral("mimeType"), QStringLiteral("dummy/thing") }, + {QStringLiteral("platform"), QStringLiteral("mobile")} }; model.setInputData(input); @@ -87,7 +88,8 @@ const QJsonObject input = { {QStringLiteral("urls"), QJsonArray {uri} }, {QStringLiteral("mimeType"), QStringLiteral("dummy/thing") }, - {QStringLiteral("destinationPath"), QUrl::fromLocalFile(tempfile).url()} + {QStringLiteral("destinationPath"), QUrl::fromLocalFile(tempfile).url()}, + {QStringLiteral("platform"), QStringLiteral("mobile")} }; model.setInputData(input); model.setPluginType(QStringLiteral("Export")); @@ -111,3 +113,55 @@ QCOMPARE(fi.size(), payload.size()); QVERIFY(QFile::remove(tempfile)); } + +void AlternativesModelTest::platformFilteringTest() +{ + const auto listPlugins = [](const QString &platform = QString()) { + QStringList plugins; + Purpose::AlternativesModel model; + QJsonObject input = QJsonObject { + {QStringLiteral("urls"), QJsonArray {QStringLiteral("http://kde.org")} }, + {QStringLiteral("mimeType"), QStringLiteral("dummy/thing") } + }; + if (!platform.isEmpty()) { + input.insert(QStringLiteral("platform"), platform); + } + model.setInputData(input); + model.setPluginType(QStringLiteral("Export")); + + for (int i = 0; i < model.rowCount(); ++i) { + plugins << model.index(i).data(Purpose::AlternativesModel::PluginIdRole).toString(); + } + return plugins; + }; + + const auto possiblePlatforms = QStringList{QStringLiteral("desktop"), QStringLiteral("mobile")}; + const auto defaultPlatform = Purpose::AlternativesModel::getDefaultPlatform(); + QVERIFY(possiblePlatforms.contains(defaultPlatform)); + + Purpose::AlternativesModel::setDefaultPlatform(QStringLiteral("desktop")); + auto plugins = listPlugins(); + QVERIFY(!plugins.contains(QStringLiteral("saveasplugin"))); + QVERIFY(plugins.contains(QStringLiteral("kdeconnectplugin"))); + + plugins = listPlugins(QStringLiteral("desktop")); + QVERIFY(!plugins.contains(QStringLiteral("saveasplugin"))); + QVERIFY(plugins.contains(QStringLiteral("kdeconnectplugin"))); + + plugins = listPlugins(QStringLiteral("mobile")); + QVERIFY(plugins.contains(QStringLiteral("saveasplugin"))); + QVERIFY(plugins.contains(QStringLiteral("kdeconnectplugin"))); + + Purpose::AlternativesModel::setDefaultPlatform(QStringLiteral("mobile")); + plugins = listPlugins(); + QVERIFY(plugins.contains(QStringLiteral("saveasplugin"))); + QVERIFY(plugins.contains(QStringLiteral("kdeconnectplugin"))); + + plugins = listPlugins(QStringLiteral("mobile")); + QVERIFY(plugins.contains(QStringLiteral("saveasplugin"))); + QVERIFY(plugins.contains(QStringLiteral("kdeconnectplugin"))); + + plugins = listPlugins(QStringLiteral("desktop")); + QVERIFY(!plugins.contains(QStringLiteral("saveasplugin"))); + QVERIFY(plugins.contains(QStringLiteral("kdeconnectplugin"))); +} diff --git a/src/alternativesmodel.h b/src/alternativesmodel.h --- a/src/alternativesmodel.h +++ b/src/alternativesmodel.h @@ -45,6 +45,9 @@ IconNameRole }; + static QString getDefaultPlatform(); + static void setDefaultPlatform(const QString &platform); + AlternativesModel(QObject* parent = Q_NULLPTR); virtual ~AlternativesModel(); diff --git a/src/alternativesmodel.cpp b/src/alternativesmodel.cpp --- a/src/alternativesmodel.cpp +++ b/src/alternativesmodel.cpp @@ -36,6 +36,13 @@ using namespace Purpose; +static QString s_defaultPlatform = +#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || (defined(WINAPI_FAMILY_PHONE_APP) && WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP) + QStringLiteral("mobile"); +#else + QStringLiteral("desktop"); +#endif + typedef bool (*matchFunction)(const QString& constraint, const QJsonValue& value); static bool defaultMatch(const QString& constraint, const QJsonValue& value) @@ -85,6 +92,11 @@ return false; } + QJsonObject inputData = m_inputData; + if (!inputData.contains(QStringLiteral("platform"))) { + inputData.insert(QStringLiteral("platform"), s_defaultPlatform); + } + const QJsonArray constraints = obj.value(QStringLiteral("X-Purpose-Constraints")).toArray(); const QRegularExpression constraintRx(QStringLiteral("(\\w+):(.*)")); for(const QJsonValue& constraint: constraints) { @@ -96,7 +108,7 @@ } QString propertyName = match.captured(1); QString constrainedValue = match.captured(2); - bool acceptable = s_matchFunctions.value(propertyName, defaultMatch)(constrainedValue, m_inputData.value(propertyName)); + bool acceptable = s_matchFunctions.value(propertyName, defaultMatch)(constrainedValue, inputData.value(propertyName)); if (!acceptable) { // qDebug() << "not accepted" << meta.name() << propertyName << constrainedValue << m_inputData[propertyName]; return false; @@ -106,6 +118,16 @@ } }; +QString AlternativesModel::getDefaultPlatform() +{ + return s_defaultPlatform; +} + +void AlternativesModel::setDefaultPlatform(const QString &platform) +{ + s_defaultPlatform = platform; +} + AlternativesModel::AlternativesModel(QObject* parent) : QAbstractListModel(parent) , d_ptr(new AlternativesModelPrivate) diff --git a/src/plugins/saveas/saveasplugin.json b/src/plugins/saveas/saveasplugin.json --- a/src/plugins/saveas/saveasplugin.json +++ b/src/plugins/saveas/saveasplugin.json @@ -60,7 +60,9 @@ "X-Purpose-Configuration": [ "destinationPath" ], - "X-Purpose-Constraints": [], + "X-Purpose-Constraints": [ + "platform:mobile" + ], "X-Purpose-PluginTypes": [ "Export" ]