diff --git a/autotests/testkldap.cpp b/autotests/testkldap.cpp index ec5a216..8e5e7b3 100644 --- a/autotests/testkldap.cpp +++ b/autotests/testkldap.cpp @@ -1,444 +1,429 @@ /* This file is part of libkdepim. Copyright (c) 2004 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "testkldap.h" #include "ldif.h" #include "ldapdn.h" #include "ldapurl.h" #include "ldapserver.h" #include "ldapconnection.h" -#include "ldapmodel.h" #include "ldapoperation.h" #include "ldapsearch.h" #include "ber.h" #include #include #include QTEST_MAIN(KLdapTest) KLdapTest::KLdapTest(QObject *parent) : QObject(parent) { } void KLdapTest::initTestCase() { /* Read in the connection details of an LDAP server to use for testing. You should copy the file testurl.txt.tmpl to testurl.txt and specify a url in this file. The specified server should not be a production server in case we break anything here. You have been warned! */ QString filename(QStringLiteral("testurl.txt")); QFile file(filename); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream stream(&file); stream >> m_url; file.close(); } m_search = new LdapSearch; - - /* Let's also create an LdapModel object */ - m_model = new LdapModel(this); } void KLdapTest::testBer() { Ber ber1, ber2, ber3, ber4, ber5, ber6, ber7; Ber bber; int ainteger; QByteArray aoctetString1, aoctetString2, aoctetString3; QList alist1, alist2; int binteger; QByteArray boctetString1, boctetString2, boctetString3; QList blist1, blist2; aoctetString1 = "KDE"; aoctetString2 = "the"; aoctetString3 = "next generation"; alist1.append(aoctetString1); alist1.append(aoctetString2); alist2.append(aoctetString2); alist2.append(aoctetString3); alist2.append(aoctetString1); ainteger = 23543; ber1.printf(QStringLiteral("i"), ainteger); ber2.printf(QStringLiteral("o"), &aoctetString1); ber3.printf(QStringLiteral("O"), &aoctetString2); ber4.printf(QStringLiteral("s"), &aoctetString3); ber5.printf(QStringLiteral("{v}"), &alist1); ber6.printf(QStringLiteral("{V}"), &alist2); ber7.printf(QStringLiteral("oi{v}O"), &aoctetString1, ainteger, &alist2, &aoctetString2); //test integer: bber = ber1; bber.scanf(QStringLiteral("i"), &binteger); QCOMPARE(ainteger, binteger); //test octet strings: bber = ber2; bber.scanf(QStringLiteral("o"), &boctetString1); QCOMPARE(aoctetString1, boctetString1); bber = ber3; bber.scanf(QStringLiteral("o"), &boctetString2); QCOMPARE(aoctetString2, boctetString2); bber = ber4; bber.scanf(QStringLiteral("o"), &boctetString3); QCOMPARE(aoctetString3, boctetString3); //test sequence of octet strings: bber = ber5; bber.scanf(QStringLiteral("v"), &blist1); QCOMPARE(alist1, blist1); bber = ber6; bber.scanf(QStringLiteral("v"), &blist2); QCOMPARE(alist2, blist2); //complex tests boctetString1 = boctetString2 = boctetString3 = QByteArray(); binteger = 0; blist1.clear(); blist2.clear(); bber = ber7; bber.scanf(QStringLiteral("oivO"), &boctetString1, &binteger, &blist2, &boctetString2); QCOMPARE(aoctetString1, boctetString1); QCOMPARE(aoctetString2, boctetString2); QCOMPARE(alist2, blist2); QCOMPARE(ainteger, binteger); } void KLdapTest::cleanupTestCase() { delete m_search; - delete m_model; } void KLdapTest::testLdapUrl() { // Test LdapUrl using some hardwired values so that we know what to compare to LdapUrl url; bool critical; url.setUrl(QStringLiteral("ldap://cn=manager,dc=kde,dc=org:password@localhost:3999/" "dc=kde,dc=org?cn,mail?sub?(objectClass=*)?x-dir=base")); url.parseQuery(); QCOMPARE(url.userName(), QStringLiteral("cn=manager,dc=kde,dc=org")); QCOMPARE(url.password(), QStringLiteral("password")); QCOMPARE(url.dn(), LdapDN(QStringLiteral("dc=kde,dc=org"))); QCOMPARE(url.scope(), LdapUrl::Sub); QCOMPARE(url.attributes().at(0), QStringLiteral("cn")); QCOMPARE(url.attributes().at(1), QStringLiteral("mail")); QCOMPARE(url.filter(), QStringLiteral("(objectClass=*)")); QCOMPARE(url.extension(QStringLiteral("x-dir"), critical), QStringLiteral("base")); QCOMPARE(url.query(), QStringLiteral("?cn,mail?sub??x-dir=base")); // For some reason the code removes the filter if it's (objectClass=*)... QCOMPARE(url.toString(), QStringLiteral("ldap://cn=manager,dc=kde,dc=org:password@localhost:3999/" "dc=kde,dc=org??cn,mail?sub??x-dir=base")); // Now set a different filter url.setFilter(QStringLiteral("(objectclass=person)")); QCOMPARE(url.toDisplayString(), QStringLiteral("ldap://cn=manager,dc=kde,dc=org@localhost:3999/" "dc=kde,dc=org??cn,mail?sub?%28objectclass%3Dperson%29?x-dir=base")); QCOMPARE(url.filter(), QStringLiteral("(objectclass=person)")); // And now a filter with non-ascii letters url.setFilter(QStringLiteral("(givenName=Valérie *)")); QCOMPARE(url.toDisplayString(), QString::fromUtf8("ldap://cn=manager,dc=kde,dc=org@localhost:3999/" "dc=kde,dc=org??cn,mail?sub?%28givenName%3DValérie %2A%29?x-dir=base")); QCOMPARE(url.filter(), QStringLiteral("(givenName=Valérie *)")); // Test roundtrip via QUrl, as happens when sending it to kio_ldap const QUrl qurl(url); QCOMPARE(qurl.toDisplayString(), url.toDisplayString()); const LdapUrl kiourl(qurl); QCOMPARE(kiourl.toString(), url.toString()); QCOMPARE(kiourl.toDisplayString(), url.toDisplayString()); QCOMPARE(kiourl.filter(), QStringLiteral("(givenName=Valérie *)")); } void KLdapTest::testLdapConnection() { // Try to connect using an LdapUrl (read in from testurl.txt). LdapUrl url; url.setUrl(m_url); LdapConnection conn; conn.setUrl(url); int ret; if ((ret = conn.connect())) { qDebug() << "Could not connect to LDAP server. Error was:" << conn.connectionError(); } QCOMPARE(ret, 0); LdapOperation op(conn); // Now attempt to bind if ((ret = op.bind_s())) { qDebug() << "Could not bind to server. Error was:" << conn.ldapErrorString(); } QEXPECT_FAIL("", "Will fail since no server is available for testing", Abort); QCOMPARE(ret, 0); } void KLdapTest::testLdapSearch() { // Lets try a search using the specified url LdapUrl url; url.setUrl(m_url); url.parseQuery(); connect(m_search, &LdapSearch::result, this, &KLdapTest::searchResult); connect(m_search, &LdapSearch::data, this, &KLdapTest::searchData); bool success = m_search->search(url); QCoreApplication::processEvents(); QEXPECT_FAIL("", "Will fail since no server is available for testing", Abort); QCOMPARE(success, true); qDebug() << "Search found" << m_objects.size() << "matching entries"; } void KLdapTest::searchResult(KLDAP::LdapSearch *search) { qDebug(); int err = search->error(); if (err) { qDebug() << "Search returned the following error:" << search->errorString(); } QCOMPARE(err, 0); } void KLdapTest::searchData(KLDAP::LdapSearch *search, const KLDAP::LdapObject &obj) { Q_UNUSED(search); //qDebug(); //qDebug() << "Object:"; //qDebug() << obj.toString(); m_objects.append(obj); } void KLdapTest::testLdapDN() { QString strDN(QStringLiteral("uid=Test\\+Person+ou=accounts\\,outgoing,dc=kde,dc=org")); LdapDN dn(strDN); QCOMPARE(dn.isValid(), true); QCOMPARE(dn.rdnString(), QStringLiteral("uid=Test\\+Person+ou=accounts\\,outgoing")); } void KLdapTest::testLdapModel() { // Use the user-supplied testing url LdapUrl url; url.setUrl(m_url); // Create a connection to use and bind with it LdapConnection conn; conn.setUrl(url); int ret; if ((ret = conn.connect())) { qDebug() << "Could not connect to LDAP server. Error was:" << conn.connectionError(); } QCOMPARE(ret, 0); LdapOperation op(conn); if ((ret = op.bind_s())) { qDebug() << "Could not bind to server. Error was:" << conn.ldapErrorString(); } QEXPECT_FAIL("", "Will fail since no server is available for testing", Abort); QCOMPARE(ret, 0); - // Let's use this connection with the model - m_model->setConnection(conn); - QCoreApplication::processEvents(); - - QModelIndex rootIndex = QModelIndex(); - QVariant data = m_model->data(rootIndex, Qt::DisplayRole); - qDebug() << "Root Item Distinguished Name =" << data.toString(); - - QVERIFY(m_model->hasChildren(rootIndex) == true); - QVERIFY(m_model->canFetchMore(rootIndex) == false); } /* void KLdapTest::testKLdap() { LdapUrl url; bool critical; url.setUrl("ldap://cn=manager,dc=kde,dc=org:password@localhost:3999" "/dc=kde,dc=org?cn,mail?sub?(objectClass=*)?x-dir=base"); url.parseQuery(); QCOMPARE( url.user(), QStringLiteral("cn=manager,dc=kde,dc=org") ); QCOMPARE( url.password(), QStringLiteral("password") ); QCOMPARE( url.dn(), QStringLiteral("dc=kde,dc=org") ); QCOMPARE( url.scope(), LdapUrl::Sub ); QCOMPARE( url.attributes().at(0), QStringLiteral("cn") ); QCOMPARE( url.attributes().at(1), QStringLiteral("mail") ); QCOMPARE( url.filter(), QStringLiteral("(objectClass=*)") ); QCOMPARE( url.extension(QStringLiteral("x-dir"), critical), QStringLiteral("base") ); url.setDn("ou=People,dc=kde,dc=org"); QCOMPARE( url.dn(), QStringLiteral("ou=People,dc=kde,dc=org") ); url.setDn("/ou=People,dc=kde,dc=org"); QCOMPARE( url.dn(), QStringLiteral("ou=People,dc=kde,dc=org") ); LdapServer server; // url.setUrl("ldaps://cn=manager,dc=kde,dc=org:passwor@localhost:3999/" "dc=kde,dc=org????x-timelimt=5,x-sizelimit=6,x=pagesize=7,binddn=cn=apple,ou=berry"); url.setUrl("ldaps://cn=manager,dc=kde,dc=org:password@localhost:3999/" "dc=kde,dc=org??base??x-timelimit=5"); url.parseQuery(); server.setUrl( url ); QCOMPARE( url.query(), QStringLiteral("??base??x-timelimit=5") ); QCOMPARE( url.url(), server.url().url() ); LdapControl c1; c1.setControl( QStringLiteral("1.2.3.4.5.6"), QByteArray("abcdefg"), true ); //test copy constructor LdapControl c2(c1); QCOMPARE( c2.oid(), QStringLiteral("1.2.3.4.5.6") ); QCOMPARE( c2.value(), QByteArray("abcdefg") ); QCOMPARE( c2.critical(), true ); //test assignment operator LdapControl c3; c3 = c1; QCOMPARE( c3.oid(), QStringLiteral("1.2.3.4.5.6") ); QCOMPARE( c3.value(), QByteArray("abcdefg") ); QCOMPARE( c3.critical(), true ); */ //test Ber functions /* QByteArray left1("bertest"), right1; int left2 = 0, right2; int left3 = 1, right3; int left4 = 2, right4; int left5 = 3, right5; int left6 = 1, right6; QList left7, right7; left7.append( "abcdefghij" ); left7.append( "123456789" ); left7.append( "1234\0\0\056789" ); Ber ber; ber.printf("{seeiib}", &left1, left2, left3, left4, left5, left6 ); // ber.printf("{ioOi{i}}", left3, &left1, &left2, left4, left4 ); Ber ber2 = ber; unsigned int a; int b; a = ber2.skipTag( b ); qDebug() << "next tag:" << a << "size:" << b; a = ber2.skipTag( b ); qDebug() << "next tag:" << a << "size:" << b; a = ber2.skipTag( b ); qDebug() << "next tag:" << a << "size:" << b; a = ber2.skipTag( b ); qDebug() << "next tag:" << a << "size:" << b; a = ber2.skipTag( b ); qDebug() << "next tag:" << a << "size:" << b; a = ber2.skipTag( b ); qDebug() << "next tag:" << a << "size:" << b; a = ber2.skipTag( b ); qDebug() << "next tag:" << a << "size:" << b; a = ber2.skipTag( b ); qDebug() << "next tag:" << a << "size:" << b; BerElement *_ber, *_ber2; _ber = ber_alloc_t( LBER_USE_DER ); ber_len_t bl; ber_printf( _ber, "{i}", 5 ); qDebug() << "native"; _ber2 = ber_dup( _ber ); a = ber_skip_tag( _ber2, &bl ); qDebug() << "next tag:" << a << "size:" << bl; // ber_dump( _ber, 0 ); // ber2.scanf("{v}", &right5 ); // ber2.scanf("{inoOi{v}}", &right3, &right1, &right2, &right4, &right5 ); // QCOMPARE( left1, right1 ); // QCOMPARE( left2, right2 ); // QCOMPARE( left3, right3 ); // QCOMPARE( left4, right4 ); // QCOMPARE( left5, right5 ); */ /* url.setUrl("ldap://localhost/dc=gyurco,dc=localdomain"); url.parseQuery(); server.setUrl( url ); LdapConnection conn( server ); int result = conn.connect(); qDebug() << "connect result" << result << conn.errorString(); LdapOperation op( conn ); int msgid = op.search( "ou=People,dc=gyurco,dc=localdomain", LdapUrl::One, "", QStringList() ); qDebug() << "search msgid" << msgid; result = op.result( msgid ); qDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString(); while ( result == LdapOperation::RES_SEARCH_ENTRY ) { qDebug() << op.object().toString(); result = op.result( msgid ); } qDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString(); msgid = op.del( "ou=People,dc=gyurco,dc=localdomain" ); qDebug() << "search msgid" << msgid; result = op.result( msgid ); qDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString(); msgid = op.compare( "ou=People,dc=gyurco,dc=localdomain", "objectClass", QByteArray("top") ); qDebug() << "search msgid" << msgid; result = op.result( msgid ); qDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString(); msgid = op.compare( "ou=People,dc=gyurco,dc=localdomain", "objectClass", QByteArray("inetOrgPerson") ); qDebug() << "search msgid" << msgid; result = op.result( msgid ); qDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString(); msgid = op.exop( "1.2.3.4.5.6.7.8", QByteArray("inetOrgPerson") ); qDebug() << "search msgid" << msgid; result = op.result( msgid ); qDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString(); */ /* } */ diff --git a/autotests/testkldap.h b/autotests/testkldap.h index 5467789..94343ce 100644 --- a/autotests/testkldap.h +++ b/autotests/testkldap.h @@ -1,59 +1,57 @@ /* Copyright (c) 2006 Volker Krause This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef TESTKLDAP_H #define TESTKLDAP_H #include -#include #include #include using namespace KLDAP; class KLdapTest : public QObject { Q_OBJECT public: explicit KLdapTest(QObject *parent = nullptr); ~KLdapTest() = default; private Q_SLOTS: //void testKLdap(); void initTestCase(); void cleanupTestCase(); void testLdapUrl(); void testBer(); void testLdapConnection(); void testLdapSearch(); void testLdapDN(); void testLdapModel(); private: void searchResult(KLDAP::LdapSearch *search); void searchData(KLDAP::LdapSearch *search, const KLDAP::LdapObject &obj); QString m_url; LdapSearch *m_search = nullptr; LdapObjects m_objects; - LdapModel *m_model = nullptr; }; #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 823a0d9..0cb42c5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,116 +1,108 @@ include(CheckFunctionExists) include(CheckIncludeFiles) include(CheckSymbolExists) check_include_files(sys/time.h HAVE_SYS_TIME_H) set(kldap_EXTRA_LIBS) if(Ldap_FOUND) set(kldap_EXTRA_LIBS Ldap::Ldap) if(WIN32) set(kldap_EXTRA_LIBS ${kldap_EXTRA_LIBS} ws2_32) endif() set(HAVE_LDAP_H) set(CMAKE_REQUIRED_INCLUDES lber.h ldap.h) set(CMAKE_REQUIRED_LIBRARIES Ldap::Ldap) check_function_exists(ldap_start_tls_s HAVE_LDAP_START_TLS_S) check_function_exists(ldap_initialize HAVE_LDAP_INITIALIZE) check_function_exists(ber_memfree HAVE_BER_MEMFREE) check_function_exists(ldap_unbind_ext HAVE_LDAP_UNBIND_EXT) check_function_exists(ldap_extended_operation HAVE_LDAP_EXTENDED_OPERATION) check_function_exists(ldap_extended_operation_s HAVE_LDAP_EXTENDED_OPERATION_S) check_symbol_exists(ldap_extended_operation ldap.h HAVE_LDAP_EXTENDED_OPERATION_PROTOTYPE) check_symbol_exists(ldap_extended_operation_s ldap.h HAVE_LDAP_EXTENDED_OPERATION_S_PROTOTYPE) endif() set(kldap_EXTRA_LIBS ${kldap_EXTRA_LIBS} Sasl2::Sasl2) configure_file(kldap_config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/kldap_config.h) ########### next target ############### set(kldap_LIB_SRCS ber.cpp ldif.cpp ldapurl.cpp ldapserver.cpp ldapobject.cpp ldapconnection.cpp ldapoperation.cpp ldapcontrol.cpp ldapsearch.cpp ldapconfigwidget.cpp ldapdn.cpp - ldapmodelnode_p.cpp - ldapmodel.cpp - ldapmodel_p.cpp - ldapstructureproxymodel.cpp - ldapattributeproxymodel.cpp ) ecm_qt_declare_logging_category(kldap_LIB_SRCS HEADER ldap_debug.h IDENTIFIER LDAP_LOG CATEGORY_NAME org.kde.pim.ldap) add_library(KF5Ldap ${kldap_LIB_SRCS}) generate_export_header(KF5Ldap BASE_NAME kldap) add_library(KF5::Ldap ALIAS KF5Ldap) target_link_libraries(KF5Ldap PRIVATE Qt5::Widgets KF5::I18n KF5::WidgetsAddons ${kldap_EXTRA_LIBS} ) target_include_directories(KF5Ldap INTERFACE "$") target_include_directories(KF5Ldap PUBLIC "$") set_target_properties(KF5Ldap PROPERTIES VERSION ${KLDAP_VERSION_STRING} SOVERSION ${KLDAP_SOVERSION} EXPORT_NAME Ldap ) install(TARGETS KF5Ldap EXPORT KF5LdapTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS}) ########### install files ############### ecm_generate_headers(KLdap_CamelCase_HEADERS HEADER_NAMES Ber - LdapAttributeProxyModel LdapConfigWidget LdapConnection LdapControl LdapDN - LdapModel LdapObject LdapOperation LdapSearch LdapServer LdapDefs - LdapStructureProxyModel LdapUrl Ldif PREFIX KLDAP REQUIRED_HEADERS KLdap_HEADERS ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kldap_export.h ${KLdap_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KLDAP/kldap COMPONENT Devel ) install(FILES ${KLdap_CamelCase_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KLDAP/KLDAP/ COMPONENT Devel ) ecm_generate_pri_file(BASE_NAME Ldap LIB_NAME KF5Ldap FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/KLDAP/) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) diff --git a/src/ldapattributeproxymodel.cpp b/src/ldapattributeproxymodel.cpp deleted file mode 100644 index 50cd09d..0000000 --- a/src/ldapattributeproxymodel.cpp +++ /dev/null @@ -1,147 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "ldapattributeproxymodel.h" -#include "ldapmodel.h" -#include "ldapmodelnode_p.h" - -#include "ldap_debug.h" -#include - -using namespace KLDAP; - -LdapAttributeProxyModel::LdapAttributeProxyModel(QObject *parent) - : QSortFilterProxyModel(parent) -{ -} - -LdapAttributeProxyModel::~LdapAttributeProxyModel() -{ -} - -QVariant LdapAttributeProxyModel::data(const QModelIndex &index, int role) const -{ - // Included just in case we decide to do any special presentation of the data - // at some other point throughout the 4.x series. - return sourceModel()->data(mapToSource(index), role); -} - -bool LdapAttributeProxyModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - Q_UNUSED(index); - Q_UNUSED(value); - Q_UNUSED(role); - return false; -} - -bool LdapAttributeProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const -{ - QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent); - LdapModelNode::NodeType nodeType - = static_cast( - sourceModel()->data(idx, LdapModel::NodeTypeRole).toUInt()); - return nodeType == LdapModelNode::Attr; -} - -QVariant LdapAttributeProxyModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - if (section == 0) { - return QVariant(i18n("Attribute")); - } else if (section == 1) { - return QVariant(i18n("Value")); - } - } - - return QVariant(); -} - -int LdapAttributeProxyModel::columnCount(const QModelIndex & /*parent*/) const -{ - return 2; -} - -Qt::ItemFlags LdapAttributeProxyModel::flags(const QModelIndex &index) const -{ - // Included so as not to break BC in case we wish to use this later in 4.x - return sourceModel()->flags(mapToSource(index)); -} - -bool LdapAttributeProxyModel::hasChildren(const QModelIndex &parent) const -{ - // We need to handle this carefully bacause of the filtering out of attributes - // and the lazy population approach. - LdapModel *model = static_cast(sourceModel()); - return model->hasChildrenOfType(mapToSource(parent), LdapModel::Attribute); -} - -QModelIndex LdapAttributeProxyModel::mapFromSource(const QModelIndex &sourceIndex) const -{ - return QSortFilterProxyModel::mapFromSource(sourceIndex); -} - -QModelIndex LdapAttributeProxyModel::mapToSource(const QModelIndex &proxyIndex) const -{ - return QSortFilterProxyModel::mapToSource(proxyIndex); -} - -bool LdapAttributeProxyModel::insertRows(int row, int count, const QModelIndex &parent) -{ - Q_UNUSED(row); - Q_UNUSED(count); - Q_UNUSED(parent); - return false; -} - -bool LdapAttributeProxyModel::removeRows(int row, int count, const QModelIndex &parent) -{ - Q_UNUSED(row); - Q_UNUSED(count); - Q_UNUSED(parent); - return false; -} - -void LdapAttributeProxyModel::sort(int column, Qt::SortOrder order) -{ - Q_UNUSED(column); - Q_UNUSED(order); -} - -Qt::DropActions LdapAttributeProxyModel::supportedDropActions() const -{ - return Qt::MoveAction; -} - -QMimeData *LdapAttributeProxyModel::mimeData(const QModelIndexList &indexes) const -{ - Q_UNUSED(indexes); - return nullptr; -} - -bool LdapAttributeProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) -{ - /** \todo Implement drag and drop for LdapModel */ - Q_UNUSED(data); - Q_UNUSED(action); - Q_UNUSED(row); - Q_UNUSED(column); - Q_UNUSED(parent); - return false; -} diff --git a/src/ldapattributeproxymodel.h b/src/ldapattributeproxymodel.h deleted file mode 100644 index 754b90b..0000000 --- a/src/ldapattributeproxymodel.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef KLDAP_LDAPATTRIBUTEPROXYMODEL_H -#define KLDAP_LDAPATTRIBUTEPROXYMODEL_H - -#include - -#include "kldap_export.h" - -namespace KLDAP { -class KLDAP_EXPORT LdapAttributeProxyModel : public QSortFilterProxyModel -{ - Q_OBJECT -public: - explicit LdapAttributeProxyModel(QObject *parent = nullptr); - ~LdapAttributeProxyModel() override; - - Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role) const override; - /** - * Reimplemented from QAbstractItemModel::setData(). This is a placeholder for when - * LdapAttributeProxyModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - Q_REQUIRED_RESULT bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; - Q_REQUIRED_RESULT QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - Q_REQUIRED_RESULT int columnCount(const QModelIndex &parent) const override; - Q_REQUIRED_RESULT Qt::ItemFlags flags(const QModelIndex &index) const override; - Q_REQUIRED_RESULT bool hasChildren(const QModelIndex &parent) const override; - - Q_REQUIRED_RESULT QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override; - Q_REQUIRED_RESULT QModelIndex mapToSource(const QModelIndex &proxyIndex) const override; - - /** - * Reimplemented from QAbstractItemModel::insertRows(). This is a placeholder for when - * LdapAttributeProxyModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; - /** - * Reimplemented from QAbstractItemModel::removeRows(). This is a placeholder for when - * LdapAttributeProxyModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; - /** - * Reimplemented from QAbstractItemModel::removeRows(). The default implementation - * does nothing. - */ - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - - // - // Drag and drop support - // - /** - * Reimplemented from QAbstractItemModel::supportedDropActions(). The default - * implementation returns Qt::MoveAction. - */ - Q_REQUIRED_RESULT Qt::DropActions supportedDropActions() const override; - /** - * Reimplemented from QAbstractItemModel::mimedata(). This is a placeholder for when - * LdapAttributeProxyModel beomes writeable and always returns 0. - */ - QMimeData *mimeData(const QModelIndexList &indexes) const override; - /** - * Reimplemented from QAbstractItemModel::dropMimedata(). This is a placeholder for when - * LdapAttributeProxyModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; -}; -} -#endif diff --git a/src/ldapmodel.cpp b/src/ldapmodel.cpp deleted file mode 100644 index 59051e6..0000000 --- a/src/ldapmodel.cpp +++ /dev/null @@ -1,313 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "ldapmodel.h" -#include "ldapmodel_p.h" -#include "ldapmodelnode_p.h" -#include "ldapsearch.h" - -#include "ldap_debug.h" -#include - -using namespace KLDAP; - -LdapModel::LdapModel(QObject *parent) - : QAbstractItemModel(parent) - , m_d(new LdapModelPrivate(this)) -{ - m_d->createConnections(); -} - -LdapModel::LdapModel(LdapConnection &connection, QObject *parent) - : QAbstractItemModel(parent) - , m_d(new LdapModelPrivate(this, connection)) -{ - m_d->createConnections(); - - // Populate items from the root object to that representing the baseDN - m_d->populateRootToBaseDN(); -} - -LdapModel::~LdapModel() -{ - delete m_d; -} - -void LdapModel::setConnection(LdapConnection &connection) -{ - m_d->setConnection(connection); - - // Refresh the model - m_d->recreateRootItem(); - - // Populate the root object by searching the baseDN - m_d->populateRootToBaseDN(); -} - -QModelIndex LdapModel::parent(const QModelIndex &child) const -{ - if (!child.isValid()) { - return QModelIndex(); - } - - LdapModelNode *childItem = static_cast(child.internalPointer()); - LdapModelDNNode *parentItem = childItem->parent(); - - if (parentItem == m_d->rootNode()) { - return QModelIndex(); - } - - return createIndex(parentItem->row(), 0, parentItem); -} - -QModelIndex LdapModel::index(int row, int col, const QModelIndex &parent) const -{ - // Retrieve a pointer to the parent item - LdapModelDNNode *parentItem = nullptr; - if (!parent.isValid()) { - parentItem = m_d->rootNode(); - } else { - parentItem = static_cast(parent.internalPointer()); - } - - LdapModelNode *childItem = parentItem->child(row); - if (childItem) { - return createIndex(row, col, childItem); - } - qCDebug(LDAP_LOG) << "Could not create valid index for row =" << row << ", col =" << col; - return QModelIndex(); -} - -QVariant LdapModel::data(const QModelIndex &index, int role) const -{ - if (!index.isValid()) { - return QVariant(); - } - - if (role == Qt::DisplayRole) { - // This is what gets displayed by the view delegates. - LdapModelNode *node = static_cast(index.internalPointer()); - if (node->nodeType() == LdapModelNode::DN) { - LdapModelDNNode *dn = static_cast(node); - if (index.column() == 0) { - return dn->dn().rdnString(); - } else { - return QVariant(); - } - } else { - LdapModelAttrNode *attr = static_cast(node); - if (index.column() == 0) { - return QVariant(attr->attributeName()); - } else { - return QVariant(QLatin1String(attr->attributeData().constData())); - } - } - } else if (role == NodeTypeRole) { - LdapModelNode *node = static_cast(index.internalPointer()); - return QVariant(int(node->nodeType())); - } - - /** \todo Include support for nice decorative icons dependent upon - the objectClass + other role data. */ - /** \todo Include support for other roles as needed */ - - return QVariant(); -} - -bool LdapModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - Q_UNUSED(index); - Q_UNUSED(value); - Q_UNUSED(role); - return false; -} - -QVariant LdapModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - if (section == 0) { - return i18n("Attribute"); - } else { - return i18n("Value"); - } - } - - return QVariant(); -} - -Qt::ItemFlags LdapModel::flags(const QModelIndex &index) const -{ - /** \TODO Read-only for now, make read-write upon request */ - if (!index.isValid()) { - return Qt::ItemIsEnabled; - } - - return Qt::ItemFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); -} - -int LdapModel::columnCount(const QModelIndex &parent) const -{ - LdapModelDNNode *parentNode - = parent.isValid() ? static_cast(parent.internalPointer()) : m_d->rootNode(); - return parentNode->columnCount(); -} - -int LdapModel::rowCount(const QModelIndex &parent) const -{ - if (parent.column() > 0) { - return 0; - } - - const LdapModelDNNode *parentNode - = parent.isValid() ? static_cast(parent.internalPointer()) : m_d->rootNode(); - return parentNode->childCount(); -} - -bool LdapModel::hasChildren(const QModelIndex &parent) const -{ - // We return true unless the item has been populated and we are able to do a definitive test - const LdapModelNode *node = parent.isValid() - ? static_cast(parent.internalPointer()) - : m_d->rootNode(); - - if (node->nodeType() != LdapModelNode::DN) { - return false; - } - - const LdapModelDNNode *parentNode = static_cast(node); - if (!parent.isValid() || parentNode->isPopulated()) { - return parentNode->childCount() > 0; - } - return true; -} - -bool LdapModel::canFetchMore(const QModelIndex &parent) const -{ - const LdapModelDNNode *parentNode - = parent.isValid() ? static_cast(parent.internalPointer()) : m_d->rootNode(); - return !parentNode->isPopulated(); -} - -void LdapModel::fetchMore(const QModelIndex &parent) -{ - LdapModelDNNode *parentNode - = parent.isValid() ? static_cast(parent.internalPointer()) : m_d->rootNode(); - - // Search for the immediate children of parentItem. - m_d->searchResults().clear(); - m_d->setSearchType(LdapModelPrivate::ChildObjects, parentNode); - m_d->search(parentNode->dn(), // DN to search from - LdapUrl::One, // What to search - QString()); // Attributes to retrieve - parentNode->setPopulated(true); -} - -bool LdapModel::insertRows(int row, int count, const QModelIndex &parent) -{ - Q_UNUSED(row); - Q_UNUSED(count); - Q_UNUSED(parent); - return false; -} - -bool LdapModel::removeRows(int row, int count, const QModelIndex &parent) -{ - Q_UNUSED(row); - Q_UNUSED(count); - Q_UNUSED(parent); - return false; -} - -void LdapModel::sort(int column, Qt::SortOrder order) -{ - Q_UNUSED(column); - Q_UNUSED(order); -} - -Qt::DropActions LdapModel::supportedDropActions() const -{ - return Qt::MoveAction; -} - -QMimeData *LdapModel::mimeData(const QModelIndexList &indexes) const -{ - Q_UNUSED(indexes); - return nullptr; -} - -bool LdapModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) -{ - /** \todo Implement drag and drop for LdapModel */ - Q_UNUSED(data); - Q_UNUSED(action); - Q_UNUSED(row); - Q_UNUSED(column); - Q_UNUSED(parent); - return false; -} - -bool LdapModel::hasChildrenOfType(const QModelIndex &parent, LdapDataType type) const -{ - // Map from LdapDataType to our internal NodeType - LdapModelNode::NodeType nodeType; - switch (type) { - case Attribute: - nodeType = LdapModelNode::Attr; - break; - - case DistinguishedName: - default: - nodeType = LdapModelNode::DN; - break; - } - - const LdapModelNode *node = parent.isValid() - ? static_cast(parent.internalPointer()) - : m_d->rootNode(); - - const LdapModelDNNode *parentNode = static_cast(node); - if (!parent.isValid() || parentNode->isPopulated()) { - // Check to see if the parent has any children of the specified type - const QList &children = parentNode->children(); - for (LdapModelNode *child : children) { - if (child->nodeType() == nodeType) { - return true; - } - } - - // Either there are no children or only children of a different type - return false; - } - - // If the node is not populated or is the root node (invalid), then return - // true to be on the safe side. - return true; -} - -void LdapModel::revert() -{ -} - -bool LdapModel::submit() -{ - return false; -} - -#include "moc_ldapmodel.cpp" diff --git a/src/ldapmodel.h b/src/ldapmodel.h deleted file mode 100644 index c9e659f..0000000 --- a/src/ldapmodel.h +++ /dev/null @@ -1,206 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef KLDAP_LDAPMODEL_H -#define KLDAP_LDAPMODEL_H - -#include - -#include "ldapconnection.h" -#include "ldapobject.h" -#include "kldap_export.h" - -namespace KLDAP { -/** - * A ModelView interface to an LDAP tree. At present the model is read only. Editing is - * planned for a future release. - * - * This class is best used in conjunction with an LdapStructureProxyModel object for - * displaying the structure of an LDAP tree, and with LdapAttributeProxyModel for - * displaying the attributes of particular objects within the tree. - * - * \author Sean Harmer - */ -class KLDAP_EXPORT LdapModel : public QAbstractItemModel -{ - Q_OBJECT -public: - enum Roles { - NodeTypeRole = Qt::UserRole + 1 - }; - - enum LdapDataType { - DistinguishedName = 0, - Attribute - }; - - /** - * Constructs an LdapModel. You should set a connection for the model to use with - * setConnection(). Clients of this class should connect a slot to the ready() signal - * before setting this model onto a view. - * @param parent the parent QObject - * \see setConnection() - * \see ready() - */ - explicit LdapModel(QObject *parent = nullptr); - /** - * Constructs an LdapModel. Clients of this class should connect a slot to the ready() - * signal before setting this model onto a view. - * @param connection the Ldap connection to use in model construction - * @param parent the parent QObject - * \see setConnection() - * \see ready() - */ - explicit LdapModel(LdapConnection &connection, QObject *parent = nullptr); - ~LdapModel() override; - - /** - * Set the connection that the model should use. - * @param connection the model connection to set - * \see LdapConnection - * \see LdapUrl - */ - void setConnection(LdapConnection &connection); - - // - // Implement the usual QAbstractItemModel interface - // - /** - * Reimplemented from QAbstractItemModel::index(). - */ - Q_REQUIRED_RESULT QModelIndex index(int row, int col, const QModelIndex &parent) const override; - /** - * Reimplemented from QAbstractItemModel::parent(). - */ - Q_REQUIRED_RESULT QModelIndex parent(const QModelIndex &child) const override; - /** - * Reimplemented from QAbstractItemModel::data(). - */ - Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role) const override; - /** - * Reimplemented from QAbstractItemModel::setData(). This is a placeholder for when - * LdapModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - /** - * Reimplemented from QAbstractItemModel::headerData(). - */ - Q_REQUIRED_RESULT QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - /** - * Reimplemented from QAbstractItemModel::flags(). - */ - Q_REQUIRED_RESULT Qt::ItemFlags flags(const QModelIndex &index) const override; - /** - * Reimplemented from QAbstractItemModel::columnCount(). - */ - Q_REQUIRED_RESULT int columnCount(const QModelIndex &parent) const override; - /** - * Reimplemented from QAbstractItemModel::rowCount(). - */ - Q_REQUIRED_RESULT int rowCount(const QModelIndex &parent) const override; - /** - * Reimplemented from QAbstractItemModel::hasChildren(). - */ - Q_REQUIRED_RESULT bool hasChildren(const QModelIndex &parent) const override; - /** - * Reimplemented from QAbstractItemModel::canFetchMore(). - */ - Q_REQUIRED_RESULT bool canFetchMore(const QModelIndex &parent) const override; - /** - * Reimplemented from QAbstractItemModel::fetchMore(). - */ - void fetchMore(const QModelIndex &parent) override; - /** - * Reimplemented from QAbstractItemModel::insertRows(). This is a placeholder for when - * LdapModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; - /** - * Reimplemented from QAbstractItemModel::removeRows(). This is a placeholder for when - * LdapModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; - /** - * Reimplemented from QAbstractItemModel::removeRows(). The default implementation - * does nothing. - */ - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - - // - // Drag and drop support - // - /** - * Reimplemented from QAbstractItemModel::supportedDropActions(). The default - * implementation returns Qt::MoveAction. - */ - Q_REQUIRED_RESULT Qt::DropActions supportedDropActions() const override; - /** - * Reimplemented from QAbstractItemModel::mimedata(). This is a placeholder for when - * LdapModel beomes writeable and always returns 0. - */ - QMimeData *mimeData(const QModelIndexList &indexes) const override; - /** - * Reimplemented from QAbstractItemModel::dropMimedata(). This is a placeholder for when - * LdapModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; - - // - // Other public utility functions - // - /** - * Checks to see if the item referenced by \p parent has any children of - * the type \p type. If the item has not been populated by fetchMore() yet, - * then this function returns true. - * - * \see fetchMore() - * \param parent Index to the item to query. - * \param type The type of child item to search for. - */ - bool hasChildrenOfType(const QModelIndex &parent, LdapDataType type) const; - -public Q_SLOTS: - /** - * Reimplemented from QAbstractItemModel::revert(). This is a placeholder for when - * LdapModel beomes writeable. This implementation does nothing. - */ - void revert() override; - /** - * Reimplemented from QAbstractItemModel::revert(). This is a placeholder for when - * LdapModel beomes writeable. This implementation does nothing and returns false. - */ - bool submit() override; - -Q_SIGNALS: - /** - * The ready() signal is emitted when the model is ready for use by other components. - * When the model is first created and a connection is set, the model queries the - * LDAP server for its base DN and automatically creates items down to that level. - * This requires the event loop to be running. This signal indicates that this process - * has completed and the model can now be set onto views or queried directly from code. - */ - void ready(); - -private: - class LdapModelPrivate; - LdapModelPrivate *const m_d; -}; -} -#endif diff --git a/src/ldapmodel_p.cpp b/src/ldapmodel_p.cpp deleted file mode 100644 index 1aa6783..0000000 --- a/src/ldapmodel_p.cpp +++ /dev/null @@ -1,210 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "ldapmodel_p.h" -#include "ldapmodelnode_p.h" -#include "ldapsearch.h" - -#include "ldap_debug.h" - -using namespace KLDAP; - -LdapModel::LdapModelPrivate::LdapModelPrivate(LdapModel *parent) - : m_parent(parent) - , m_root(new LdapModelDNNode) - , m_search(new LdapSearch) - , m_searchResultObjects() - , m_baseDN() - , m_searchType(NotSearching) - , m_searchItem(nullptr) -{ -} - -LdapModel::LdapModelPrivate::LdapModelPrivate(LdapModel *parent, LdapConnection &connection) - : m_parent(parent) - , m_root(new LdapModelDNNode) - , m_search(new LdapSearch(connection)) - , m_searchResultObjects() - , m_baseDN() - , m_searchType(NotSearching) - , m_searchItem(nullptr) -{ -} - -LdapModel::LdapModelPrivate::~LdapModelPrivate() -{ - delete m_root; - - delete m_search; -} - -void LdapModel::LdapModelPrivate::setConnection(LdapConnection &connection) -{ - m_search->setConnection(connection); -} - -bool LdapModel::LdapModelPrivate::search(const LdapDN &searchBase, LdapUrl::Scope scope, const QString &filter, const QStringList &attributes, int pagesize) -{ - return m_search->search(searchBase, scope, filter, attributes, pagesize); -} - -void LdapModel::LdapModelPrivate::setSearchType(SearchType t, LdapModelDNNode *item) -{ - //qCDebug(LDAP_LOG) << "item =" << item; - m_searchType = t; - m_searchItem = item; -} - -void LdapModel::LdapModelPrivate::recreateRootItem() -{ - //qCDebug(LDAP_LOG); - delete m_root; - m_root = new LdapModelDNNode; - //qCDebug(LDAP_LOG) << "&m_root =" << &m_root; -} - -void LdapModel::LdapModelPrivate::createConnections() -{ - connect(search(), &LdapSearch::data, m_parent, [this](KLDAP::LdapSearch *s, const KLDAP::LdapObject &obj) { - gotSearchData(s, obj); - }); - connect(search(), &LdapSearch::result, m_parent, [this](KLDAP::LdapSearch *s) { - gotSearchResult(s); - }); -} - -void LdapModel::LdapModelPrivate::populateRootToBaseDN() -{ - //qCDebug(LDAP_LOG); - - if (baseDN().isEmpty()) { - // Query the server for the base DN - //qCDebug(LDAP_LOG) << "Searching for the baseDN"; - setSearchType(LdapModelPrivate::NamingContexts, rootNode()); - search(LdapDN(), LdapUrl::Base, QString(), QStringList() << QStringLiteral("namingContexts")); - return; - } - - // Start a search for the details of the baseDN object - //qCDebug(LDAP_LOG) << "Searching for attributes of the baseDN"; - searchResults().clear(); - setSearchType(LdapModelPrivate::BaseDN, rootNode()); - search(baseDN(), LdapUrl::Base, QString(), QStringList() << QStringLiteral("dn") << QStringLiteral("objectClass")); -} - -void LdapModel::LdapModelPrivate::gotSearchResult(KLDAP::LdapSearch *search) -{ - Q_UNUSED(search); - qCDebug(LDAP_LOG); - - switch (searchType()) { - case LdapModelPrivate::NamingContexts: - { - // Set the baseDN - QString baseDN; - if (!searchResults().isEmpty() - && searchResults().at(0).hasAttribute(QStringLiteral("namingContexts"))) { - baseDN = QString::fromLatin1(searchResults().at(0).value(QStringLiteral("namingContexts"))); - //qCDebug(LDAP_LOG) << "Found baseDN =" << baseDN; - } - setBaseDN(LdapDN(baseDN)); - - // Flag that we are no longer searching for the baseDN - setSearchType(LdapModelPrivate::NotSearching); - - // Populate the root item - populateRootToBaseDN(); - - break; - } - case LdapModelPrivate::BaseDN: - { - //qCDebug(LDAP_LOG) << "Found details of the baseDN object." - // << "Creating objects down to this level."; - - // Get the baseDN LdapObject - LdapObject baseDNObj = searchResults().at(0); - - // How many levels of items do we need to create? - int depth = baseDNObj.dn().depth(); - - // Create items that represent objects down to the baseDN - LdapModelDNNode *parent = rootNode(); - LdapModelDNNode *item = nullptr; - for (int i = 0; i < depth; ++i) { - QString dn = baseDN().toString(i); - qCDebug(LDAP_LOG) << "Creating item for DN :" << dn; - - //LdapObject obj( dn ); - item = new LdapModelDNNode(parent, LdapDN(dn)); - parent = item; - } - - // Store the search result - if (item) { - item->setLdapObject(baseDNObj); - } - - // Flag that we are no longer searching - setSearchType(LdapModelPrivate::NotSearching); - //emit( layoutChanged() ); - - // Let the world know we are ready for action - Q_EMIT m_parent->ready(); - - break; - } - case LdapModelPrivate::ChildObjects: - //qCDebug(LDAP_LOG) << "Found" << searchResults().size() << "child objects"; - - if (searchResults().size() != 0) { - // Create an index for the soon-to-be-a-parent item - LdapModelDNNode *parentNode = searchItem(); - int r = parentNode->row(); - QModelIndex parentIndex = m_parent->createIndex(r, 0, parentNode); - - m_parent->beginInsertRows(parentIndex, 0, searchResults().size()); - for (int i = 0; i < searchResults().size(); i++) { - LdapObject object = searchResults().at(i); - LdapModelDNNode *item = new LdapModelDNNode(parentNode, object.dn()); - item->setLdapObject(object); - } - - m_parent->endInsertRows(); - Q_EMIT m_parent->layoutChanged(); - } - - // Flag that we are no longer searching - setSearchType(LdapModelPrivate::NotSearching); - - break; - default: - break; - } -} - -void LdapModel::LdapModelPrivate::gotSearchData(KLDAP::LdapSearch *search, const KLDAP::LdapObject &obj) -{ - Q_UNUSED(search); - //qCDebug(LDAP_LOG); - //qCDebug(LDAP_LOG) << "Object:"; - //qCDebug(LDAP_LOG) << obj.toString(); - searchResults().append(obj); -} diff --git a/src/ldapmodel_p.h b/src/ldapmodel_p.h deleted file mode 100644 index ae1d966..0000000 --- a/src/ldapmodel_p.h +++ /dev/null @@ -1,119 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef KLDAP_LDAPMODELPRIVATE_H -#define KLDAP_LDAPMODELPRIVATE_H - -#include "ldapconnection.h" -#include "ldapdn.h" -#include "ldapmodel.h" -#include "ldapobject.h" - -namespace KLDAP { -class LdapModelDNNode; -class LdapSearch; - -/** - * @internal - */ -class Q_DECL_HIDDEN LdapModel::LdapModelPrivate -{ -public: - enum SearchType { - NotSearching = 0, - NamingContexts, - BaseDN, - ChildObjects - }; - - explicit LdapModelPrivate(LdapModel *parent); - explicit LdapModelPrivate(LdapModel *parent, LdapConnection &connection); - - ~LdapModelPrivate(); - - void setConnection(LdapConnection &connection); - - bool search(const LdapDN &searchBase, LdapUrl::Scope scope = LdapUrl::Sub, const QString &filter = QString(), const QStringList &attributes = QStringList(), int pagesize = 0); - - LdapModelDNNode *rootNode() - { - return m_root; - } - - LdapSearch *search() - { - return m_search; - } - - LdapObjects &searchResults() - { - return m_searchResultObjects; - } - - const LdapObjects &searchResults() const - { - return m_searchResultObjects; - } - - void recreateRootItem(); - - void setBaseDN(const LdapDN &baseDN) - { - m_baseDN = baseDN; - } - - LdapDN &baseDN() - { - return m_baseDN; - } - - const LdapDN &baseDN() const - { - return m_baseDN; - } - - void setSearchType(SearchType t, LdapModelDNNode *item = nullptr); - - Q_REQUIRED_RESULT SearchType searchType() - { - return m_searchType; - } - - LdapModelDNNode *searchItem() - { - return m_searchItem; - } - - void createConnections(); - void populateRootToBaseDN(); - void gotSearchResult(KLDAP::LdapSearch *search); - void gotSearchData(KLDAP::LdapSearch *search, const KLDAP::LdapObject &obj); - -private: - LdapModel *m_parent = nullptr; - LdapModelDNNode *m_root = nullptr; - LdapSearch *m_search = nullptr; - LdapObjects m_searchResultObjects; - LdapDN m_baseDN; - SearchType m_searchType; - LdapModelDNNode *m_searchItem = nullptr; -}; -} -#endif diff --git a/src/ldapmodelnode_p.cpp b/src/ldapmodelnode_p.cpp deleted file mode 100644 index 5b37993..0000000 --- a/src/ldapmodelnode_p.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "ldapmodelnode_p.h" - -#include "ldap_debug.h" - -using namespace KLDAP; - -LdapModelNode::LdapModelNode(LdapModelDNNode *parent) - : m_parent(parent) - , m_isPopulated(false) -{ - if (m_parent) { - m_parent->appendChild(this); - } -} - -LdapModelNode::~LdapModelNode() -{ -} - -LdapModelDNNode *LdapModelNode::parent() -{ - return m_parent; -} - -int LdapModelNode::row() const -{ - if (m_parent) { - return m_parent->children().indexOf(const_cast(this)); - } - return 0; -} - -// -// LdapModelDNNode imlpementation -// - -LdapModelDNNode::LdapModelDNNode(LdapModelDNNode *parent, const LdapDN &dn) - : LdapModelNode(parent) - , m_childItems() - , m_dn(dn) -{ - qCDebug(LDAP_LOG) << "Creating DN =" << m_dn.toString(); -} - -LdapModelDNNode::~LdapModelDNNode() -{ - qDeleteAll(m_childItems); -} - -void LdapModelDNNode::appendChild(LdapModelNode *pItem) -{ - m_childItems.append(pItem); - setPopulated(true); -} - -LdapModelNode *LdapModelDNNode::child(int row) -{ - return m_childItems.value(row); -} - -void LdapModelDNNode::setLdapObject(const LdapObject &object) -{ - // Remember whether this item is populated or not - bool populated = isPopulated(); - - const LdapAttrMap &attrs = object.attributes(); - /* - int attributeCount = 0; - for ( LdapAttrMap::ConstIterator it = attrs.begin(); it != attrs.end(); ++it ) { - attributeCount += (*it).size(); - } - - for ( int i = 0; i < attributeCount; i++ ) - { - LdapModelNode* node = new LdapModelAttrNode( this, QString::number( i ) ); - Q_UNUSED( node ); - } - */ - LdapAttrMap::ConstIterator end(attrs.constEnd()); - for (LdapAttrMap::ConstIterator it = attrs.constBegin(); it != end; ++it) { - const QString attr = it.key(); - LdapAttrValue::ConstIterator end2((*it).constEnd()); - for (LdapAttrValue::ConstIterator it2 = (*it).constBegin(); it2 != end2; ++it2) { - LdapModelNode *node = new LdapModelAttrNode(this, attr, *it2); - Q_UNUSED(node); - } - } - - // Reset the populated flag so that we don't stop the model querying for children - setPopulated(populated); -} - -// -// LdapModelAttrNode imlpementation -// - -LdapModelAttrNode::LdapModelAttrNode(LdapModelDNNode *parent, const QString &attrName, const QByteArray &attrData) - : LdapModelNode(parent) - , m_attrName(attrName) - , m_attrData(attrData) -{ - qCDebug(LDAP_LOG) << "Creating Name =" << m_attrName << " Data =" << m_attrData; -} - -LdapModelAttrNode::~LdapModelAttrNode() -{ -} diff --git a/src/ldapmodelnode_p.h b/src/ldapmodelnode_p.h deleted file mode 100644 index ce4b326..0000000 --- a/src/ldapmodelnode_p.h +++ /dev/null @@ -1,150 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef KLDAP_LDAPMODELNODE_P_H -#define KLDAP_LDAPMODELNODE_P_H - -#include -#include -#include - -#include "ldapdn.h" -#include "ldapobject.h" -#include "kldap_export.h" - -// clazy:excludeall=copyable-polymorphic - -namespace KLDAP { -class LdapModelDNNode; - -/** - * @internal - */ -class LdapModelNode -{ -public: - explicit LdapModelNode(LdapModelDNNode *parent = nullptr); - virtual ~LdapModelNode(); - - enum NodeType { - DN, - Attr - }; - - virtual NodeType nodeType() const = 0; - - LdapModelDNNode *parent(); - int columnCount() const - { - return 2; - } - - int row() const; - - void setPopulated(bool b) - { - m_isPopulated = b; - } - - bool isPopulated() const - { - return m_isPopulated; - } - -private: - LdapModelDNNode *m_parent = nullptr; - bool m_isPopulated = false; -}; - -/** - * @internal - */ -class LdapModelDNNode : public LdapModelNode -{ -public: - explicit LdapModelDNNode(LdapModelDNNode *parent = nullptr, const LdapDN &dn = LdapDN()); - ~LdapModelDNNode() override; - - LdapModelNode::NodeType nodeType() const override - { - return LdapModelNode::DN; - } - - void appendChild(LdapModelNode *pItem); - LdapModelNode *child(int row); - int childCount() const - { - return m_childItems.size(); - } - - const QList &children() const - { - return m_childItems; - } - - const LdapDN &dn() const - { - return m_dn; - } - - /** - * Creates child LdapModelAttrNode object to store \p object's attributes - * and adds them as children of this node. - * - * \param The LdapObject to store in this node. - */ - void setLdapObject(const LdapObject &object); - -private: - QList m_childItems; - LdapDN m_dn; -}; - -/** - * @internal - */ -class LdapModelAttrNode : public LdapModelNode -{ -public: - explicit LdapModelAttrNode(LdapModelDNNode *parent = nullptr, const QString &attrName = QString(), const QByteArray &attrData = QByteArray()); - ~LdapModelAttrNode() override; - - LdapModelNode::NodeType nodeType() const override - { - return LdapModelNode::Attr; - } - - const QString &attributeName() - { - return m_attrName; - } - - const QByteArray &attributeData() - { - return m_attrData; - } - -private: - QString m_attrName; - QByteArray m_attrData; -}; -} - -#endif diff --git a/src/ldapstructureproxymodel.cpp b/src/ldapstructureproxymodel.cpp deleted file mode 100644 index f43f0a0..0000000 --- a/src/ldapstructureproxymodel.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "ldapstructureproxymodel.h" -#include "ldapmodel.h" -#include "ldapmodelnode_p.h" - -#include "ldap_debug.h" -#include - -using namespace KLDAP; - -LdapStructureProxyModel::LdapStructureProxyModel(QObject *parent) - : QSortFilterProxyModel(parent) -{ -} - -LdapStructureProxyModel::~LdapStructureProxyModel() -{ -} - -QVariant LdapStructureProxyModel::data(const QModelIndex &index, int role) const -{ - // Included just in case we decide to do any special presentation of the data - // at some other point throughout the 4.x series. - return sourceModel()->data(mapToSource(index), role); -} - -bool LdapStructureProxyModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - Q_UNUSED(index); - Q_UNUSED(value); - Q_UNUSED(role); - return false; -} - -bool LdapStructureProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const -{ - QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent); - LdapModelNode::NodeType nodeType - = static_cast( - sourceModel()->data(idx, LdapModel::NodeTypeRole).toUInt()); - return nodeType == LdapModelNode::DN; -} - -QVariant LdapStructureProxyModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - Q_UNUSED(section); - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - return i18n("Distinguished Name"); - } - - return QVariant(); -} - -int LdapStructureProxyModel::columnCount(const QModelIndex & /*parent*/) const -{ - // No need for more than one column just to show the structure - return 1; -} - -Qt::ItemFlags LdapStructureProxyModel::flags(const QModelIndex &index) const -{ - // Included so as not to break BC in case we wish to use this later in 4.x - return sourceModel()->flags(mapToSource(index)); -} - -bool LdapStructureProxyModel::hasChildren(const QModelIndex &parent) const -{ - // We need to handle this carefully bacause of the filtering out of attributes - // and the lazy population approach. - LdapModel *model = static_cast(sourceModel()); - return model->hasChildrenOfType(mapToSource(parent), LdapModel::DistinguishedName); -} - -QModelIndex LdapStructureProxyModel::mapFromSource(const QModelIndex &sourceIndex) const -{ - return QSortFilterProxyModel::mapFromSource(sourceIndex); -} - -QModelIndex LdapStructureProxyModel::mapToSource(const QModelIndex &proxyIndex) const -{ - return QSortFilterProxyModel::mapToSource(proxyIndex); -} - -bool LdapStructureProxyModel::insertRows(int row, int count, const QModelIndex &parent) -{ - Q_UNUSED(row); - Q_UNUSED(count); - Q_UNUSED(parent); - return false; -} - -bool LdapStructureProxyModel::removeRows(int row, int count, const QModelIndex &parent) -{ - Q_UNUSED(row); - Q_UNUSED(count); - Q_UNUSED(parent); - return false; -} - -void LdapStructureProxyModel::sort(int column, Qt::SortOrder order) -{ - Q_UNUSED(column); - Q_UNUSED(order); -} - -Qt::DropActions LdapStructureProxyModel::supportedDropActions() const -{ - return Qt::MoveAction; -} - -QMimeData *LdapStructureProxyModel::mimeData(const QModelIndexList &indexes) const -{ - Q_UNUSED(indexes); - return nullptr; -} - -bool LdapStructureProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) -{ - /** \todo Implement drag and drop for LdapModel */ - Q_UNUSED(data); - Q_UNUSED(action); - Q_UNUSED(row); - Q_UNUSED(column); - Q_UNUSED(parent); - return false; -} diff --git a/src/ldapstructureproxymodel.h b/src/ldapstructureproxymodel.h deleted file mode 100644 index e6bdf9b..0000000 --- a/src/ldapstructureproxymodel.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - This file is part of libkldap. - Copyright (c) 2006 Sean Harmer - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef KLDAP_LDAPSTRUCTUREPROXYMODEL_H -#define KLDAP_LDAPSTRUCTUREPROXYMODEL_H - -#include - -#include "kldap_export.h" - -namespace KLDAP { -class KLDAP_EXPORT LdapStructureProxyModel : public QSortFilterProxyModel -{ - Q_OBJECT -public: - explicit LdapStructureProxyModel(QObject *parent = nullptr); - ~LdapStructureProxyModel() override; - - Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role) const override; - /** - * Reimplemented from QAbstractItemModel::setData(). This is a placeholder for when - * LdapStructureProxyModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - Q_REQUIRED_RESULT bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; - Q_REQUIRED_RESULT QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - Q_REQUIRED_RESULT int columnCount(const QModelIndex &parent) const override; - Q_REQUIRED_RESULT Qt::ItemFlags flags(const QModelIndex &index) const override; - Q_REQUIRED_RESULT bool hasChildren(const QModelIndex &parent) const override; - - Q_REQUIRED_RESULT QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override; - Q_REQUIRED_RESULT QModelIndex mapToSource(const QModelIndex &proxyIndex) const override; - - /** - * Reimplemented from QAbstractItemModel::insertRows(). This is a placeholder for when - * LdapStructureProxyModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; - /** - * Reimplemented from QAbstractItemModel::removeRows(). This is a placeholder for when - * LdapStructureProxyModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; - /** - * Reimplemented from QAbstractItemModel::removeRows(). The default implementation - * does nothing. - */ - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - // - // Drag and drop support - // - /** - * Reimplemented from QAbstractItemModel::supportedDropActions(). The default - * implementation returns Qt::MoveAction. - */ - Q_REQUIRED_RESULT Qt::DropActions supportedDropActions() const override; - /** - * Reimplemented from QAbstractItemModel::mimedata(). This is a placeholder for when - * LdapStructureProxyModel beomes writeable and always returns 0. - */ - Q_REQUIRED_RESULT QMimeData *mimeData(const QModelIndexList &indexes) const override; - /** - * Reimplemented from QAbstractItemModel::dropMimedata(). This is a placeholder for when - * LdapStructureProxyModel beomes writeable and always returns false. - */ - Q_REQUIRED_RESULT bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; -}; -} -#endif