diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,7 @@ personpluginmanager.cpp personmanager.cpp personssortfilterproxymodel.cpp + datasourcemodel.cpp resources.qrc ) @@ -63,6 +64,7 @@ PersonData PersonPluginManager PersonsModel + DataSourceModel Global REQUIRED_HEADERS KPeople_HEADERS PREFIX KPeople diff --git a/src/datasourcemodel.h b/src/datasourcemodel.h new file mode 100644 --- /dev/null +++ b/src/datasourcemodel.h @@ -0,0 +1,56 @@ +/* + Copyright (C) 2019 Jonah BrĂ¼chert + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef DATASOURCEMODEL_H +#define DATASOURCEMODEL_H + +#include + +#include +#include + +namespace KPeople +{ + +/** + * @brief Model that lists the different data sources available to KPeople + * + * Provides the plugin ids of the different data sources, which can for example + * be used to add a contact to a specific backend + * + * @since 5.65 + */ +class KPEOPLE_EXPORT DataSourceModel : public QAbstractListModel +{ + Q_OBJECT +public: + enum RoleNames { + PluginIdRole = Qt::UserRole + 1, + }; + Q_ENUM(RoleNames) + + explicit DataSourceModel(QObject *parent = nullptr); + + QHash roleNames() const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = DataSourceModel::PluginIdRole) const override; +}; + +} + +#endif // DATASOURCEMODEL_H diff --git a/src/datasourcemodel.cpp b/src/datasourcemodel.cpp new file mode 100644 --- /dev/null +++ b/src/datasourcemodel.cpp @@ -0,0 +1,58 @@ +/* + Copyright (C) 2019 Jonah BrĂ¼chert + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + + +#include "datasourcemodel.h" + +#include "personpluginmanager.h" +#include "basepersonsdatasource.h" + +using namespace KPeople; + +DataSourceModel::DataSourceModel(QObject *parent) : QAbstractListModel(parent) +{ +} + +QHash DataSourceModel::roleNames() const +{ + QHash roleNames; + + roleNames.insert(DataSourceModel::PluginIdRole, "pluginId"); + + return roleNames; +} + +int DataSourceModel::rowCount(const QModelIndex &parent) const +{ + return parent.isValid() ? 0 : KPeople::PersonPluginManager::dataSourcePlugins().count(); + +} + +QVariant DataSourceModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() < 0 || index.row() >= KPeople::PersonPluginManager::dataSourcePlugins().count()) { + return {}; + } + + switch (role) { + case DataSourceModel::PluginIdRole: + return KPeople::PersonPluginManager::dataSourcePlugins().at(index.row())->sourcePluginId(); + } + + return {}; +} diff --git a/src/declarative/peopleqmlplugin.cpp b/src/declarative/peopleqmlplugin.cpp --- a/src/declarative/peopleqmlplugin.cpp +++ b/src/declarative/peopleqmlplugin.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "declarativepersondata.h" #include @@ -51,6 +52,9 @@ Q_SCRIPTABLE bool addContact(const QVariantMap &properties) { return KPeople::PersonPluginManager::addContact(properties); } + Q_SCRIPTABLE bool addContact(const QString &pluginId, const QVariantMap &properties) { + return KPeople::PersonPluginManager::addContact(pluginId, properties); + } Q_SCRIPTABLE bool deleteContact(const QString &uri) { return KPeople::PersonPluginManager::deleteContact(uri); } @@ -61,6 +65,7 @@ qmlRegisterType(uri, 1, 0, "PersonsModel"); qmlRegisterType(uri, 1, 0, "PersonsSortFilterProxyModel"); qmlRegisterType(uri, 1, 0, "PersonActions"); + qmlRegisterType(uri, 1, 0, "DataSourceModel"); qmlRegisterType(uri, 1, 0, "PersonData"); qmlRegisterType(); qmlRegisterUncreatableType(uri, 1, 0, "ActionType", QStringLiteral("You cannot create ActionType")); diff --git a/src/personpluginmanager.h b/src/personpluginmanager.h --- a/src/personpluginmanager.h +++ b/src/personpluginmanager.h @@ -85,6 +85,15 @@ * @since 5.62 */ static bool deleteContact(const QString &uri); + + /** + * Creates a contact with the specified @p properties + * in the requested backend + * @returns if it could be done successfully + * + * @since 5.65 + */ + static bool addContact(const QString &pluginId, const QVariantMap &properties); }; } diff --git a/src/personpluginmanager.cpp b/src/personpluginmanager.cpp --- a/src/personpluginmanager.cpp +++ b/src/personpluginmanager.cpp @@ -161,3 +161,20 @@ } return ret; } + +bool PersonPluginManager::addContact(const QString &pluginId, const QVariantMap &properties) +{ + bool ret = false; + for (auto p : qAsConst(s_instance->dataSourcePlugins)) { + auto v2 = dynamic_cast(p); + if (!v2) + continue; + + if (v2->sourcePluginId() != pluginId) + continue; + + const bool added = v2->addContact(properties); + ret |= added; + } + return ret; +}