diff --git a/src/commandmodel.cpp b/src/commandmodel.cpp new file mode 100644 index 000000000..f647783c1 --- /dev/null +++ b/src/commandmodel.cpp @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: 2021 Srevin Saju +// SPDX-License-Identifier: GPL-3.0-or-later + +#include + +#include "commandmodel.h" + + +QVariantList CommandModel::filterModel(const QString &filter) +{ + QVariantList result; + + for (const QVariant &e : matrix) { + auto command = qvariant_cast(e); + if (command.command.startsWith(filter)) { + result.append(e); + if (result.length() > 10) { + return result; + } + } + } + + return result; +} + + +// the help messages are taken from Element (web matrix client, app.element.io) +const QVariantList CommandModel::matrix = { + QVariant::fromValue(Command{"/join", "Join a given room with address"}), + QVariant::fromValue(Command{"/me", "Displays action"}), + +}; diff --git a/src/commandmodel.h b/src/commandmodel.h new file mode 100644 index 000000000..d70e0017a --- /dev/null +++ b/src/commandmodel.h @@ -0,0 +1,58 @@ +// SPDX-FileCopyrightText: 2021 Srevin Saju +// SPDX-License-Identifier: GPL-3.0-only + +#pragma once + +#include +#include +#include +#include +#include + +struct Command { + Command(QString u, QString s) + : command(std::move(std::move(u))) + , help(std::move(std::move(s))) + { + } + Command() = default; + + friend QDataStream &operator<<(QDataStream &arch, const Command &object) + { + arch << object.command; + arch << object.help; + return arch; + } + + friend QDataStream &operator>>(QDataStream &arch, Command &object) + { + arch >> object.command; + arch >> object.help; + return arch; + } + + QString command; + QString help; + +Q_GADGET + Q_PROPERTY(QString command MEMBER command) + Q_PROPERTY(QString help MEMBER help) +}; + +Q_DECLARE_METATYPE(Command) + +class CommandModel : public QObject +{ +Q_OBJECT + +public: + explicit CommandModel(QObject *parent = nullptr) + : QObject(parent) + { + } + + Q_INVOKABLE static QVariantList filterModel(const QString &filter); + +private: + static const QVariantList matrix; +};