linux.x86.linphone/Linphone/core/address-books/LdapList.cpp
2024-09-05 08:33:21 +00:00

100 lines
3.1 KiB
C++

/*
* Copyright (c) 2010-2024 Belledonne Communications SARL.
*
* This file is part of linphone-desktop
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LdapList.hpp"
#include "LdapGui.hpp"
#include "core/App.hpp"
#include "model/object/VariantObject.hpp"
#include <QSharedPointer>
#include <linphone++/linphone.hh>
// =============================================================================
DEFINE_ABSTRACT_OBJECT(LdapList)
QSharedPointer<LdapList> LdapList::create() {
auto model = QSharedPointer<LdapList>(new LdapList(), &QObject::deleteLater);
model->moveToThread(App::getInstance()->thread());
model->setSelf(model);
return model;
}
QSharedPointer<LdapCore> LdapList::createLdapCore(const std::shared_ptr<linphone::Ldap> &ldap) {
auto LdapCore = LdapCore::create(ldap);
return LdapCore;
}
LdapList::LdapList(QObject *parent) : ListProxy(parent) {
mustBeInMainThread(getClassName());
}
LdapList::~LdapList() {
mustBeInMainThread("~" + getClassName());
mModelConnection = nullptr;
}
void LdapList::setSelf(QSharedPointer<LdapList> me) {
mModelConnection = QSharedPointer<SafeConnection<LdapList, CoreModel>>(
new SafeConnection<LdapList, CoreModel>(me, CoreModel::getInstance()), &QObject::deleteLater);
mModelConnection->makeConnectToCore(&LdapList::lUpdate, [this]() {
mModelConnection->invokeToModel([this]() {
QList<QSharedPointer<LdapCore>> *ldaps = new QList<QSharedPointer<LdapCore>>();
mustBeInLinphoneThread(getClassName());
for (auto ldap : CoreModel::getInstance()->getCore()->getLdapList()) {
auto model = createLdapCore(ldap);
ldaps->push_back(model);
}
mModelConnection->invokeToCore([this, ldaps]() {
mustBeInMainThread(getClassName());
resetData();
add(*ldaps);
delete ldaps;
});
});
});
emit lUpdate();
}
void LdapList::removeAllEntries() {
beginResetModel();
for (auto it = mList.rbegin(); it != mList.rend(); ++it) {
auto ldap = it->objectCast<LdapCore>();
ldap->remove();
}
mList.clear();
endResetModel();
}
void LdapList::remove(const int &row) {
beginRemoveRows(QModelIndex(), row, row);
auto item = mList[row].objectCast<LdapCore>();
item->remove();
mList.takeAt(row);
endRemoveRows();
}
QVariant LdapList::data(const QModelIndex &index, int role) const {
int row = index.row();
if (!index.isValid() || row < 0 || row >= mList.count()) return QVariant();
if (role == Qt::DisplayRole) {
return QVariant::fromValue(new LdapGui(mList[row].objectCast<LdapCore>()));
}
return QVariant();
}