1/*
2 This file is part of libkldap.
3 Copyright (c) 2006 Sean Harmer <sh@theharmers.co.uk>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#ifndef KLDAP_LDAPMODEL_H
22#define KLDAP_LDAPMODEL_H
23
24#include <QtCore/QAbstractItemModel>
25
26#include "ldapconnection.h"
27#include "ldapobject.h"
28#include "kldap_export.h"
29
30namespace KLDAP {
31
32/**
33 * A ModelView interface to an LDAP tree. At present the model is read only. Editing is
34 * planned for a future release.
35 *
36 * This class is best used in conjunction with an LdapStructureProxyModel object for
37 * displaying the structure of an LDAP tree, and with LdapAttributeProxyModel for
38 * displaying the attributes of particular objects within the tree.
39 *
40 * \author Sean Harmer <sh@theharmers.co.uk>
41 */
42class KLDAP_EXPORT LdapModel : public QAbstractItemModel
43{
44 Q_OBJECT
45 public:
46 enum Roles {
47 NodeTypeRole = Qt::UserRole + 1
48 };
49
50 enum LdapDataType {
51 DistinguishedName = 0,
52 Attribute
53 };
54
55 /**
56 * Constructs an LdapModel. You should set a connection for the model to use with
57 * setConnection(). Clients of this class should connect a slot to the ready() signal
58 * before setting this model onto a view.
59 * @param parent the parent QObject
60 * \see setConnection()
61 * \see ready()
62 */
63 explicit LdapModel( QObject *parent = 0 );
64 /**
65 * Constructs an LdapModel. Clients of this class should connect a slot to the ready()
66 * signal before setting this model onto a view.
67 * @param connection the Ldap connection to use in model construction
68 * @param parent the parent QObject
69 * \see setConnection()
70 * \see ready()
71 */
72 explicit LdapModel( LdapConnection &connection, QObject *parent = 0 );
73 virtual ~LdapModel();
74
75 /**
76 * Set the connection that the model should use.
77 * @param connection the model connection to set
78 * \see LdapConnection
79 * \see LdapUrl
80 */
81 void setConnection( LdapConnection &connection );
82
83 //
84 // Implement the usual QAbstractItemModel interface
85 //
86 /**
87 * Reimplemented from QAbstractItemModel::index().
88 */
89 virtual QModelIndex index( int row, int col, const QModelIndex &parent ) const;
90 /**
91 * Reimplemented from QAbstractItemModel::parent().
92 */
93 virtual QModelIndex parent( const QModelIndex &child ) const;
94 /**
95 * Reimplemented from QAbstractItemModel::data().
96 */
97 virtual QVariant data( const QModelIndex &index, int role ) const;
98 /**
99 * Reimplemented from QAbstractItemModel::setData(). This is a placeholder for when
100 * LdapModel beomes writeable and always returns false.
101 */
102 virtual bool setData( const QModelIndex &index,
103 const QVariant &value,
104 int role = Qt::EditRole );
105 /**
106 * Reimplemented from QAbstractItemModel::headerData().
107 */
108 virtual QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
109 /**
110 * Reimplemented from QAbstractItemModel::flags().
111 */
112 virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
113 /**
114 * Reimplemented from QAbstractItemModel::columnCount().
115 */
116 virtual int columnCount( const QModelIndex &parent ) const;
117 /**
118 * Reimplemented from QAbstractItemModel::rowCount().
119 */
120 virtual int rowCount( const QModelIndex &parent ) const;
121 /**
122 * Reimplemented from QAbstractItemModel::hasChildren().
123 */
124 virtual bool hasChildren( const QModelIndex &parent ) const;
125 /**
126 * Reimplemented from QAbstractItemModel::canFetchMore().
127 */
128 virtual bool canFetchMore( const QModelIndex &parent ) const;
129 /**
130 * Reimplemented from QAbstractItemModel::fetchMore().
131 */
132 virtual void fetchMore( const QModelIndex &parent );
133 /**
134 * Reimplemented from QAbstractItemModel::insertRows(). This is a placeholder for when
135 * LdapModel beomes writeable and always returns false.
136 */
137 virtual bool insertRows( int row, int count,
138 const QModelIndex &parent = QModelIndex() );
139 /**
140 * Reimplemented from QAbstractItemModel::removeRows(). This is a placeholder for when
141 * LdapModel beomes writeable and always returns false.
142 */
143 virtual bool removeRows( int row, int count,
144 const QModelIndex &parent = QModelIndex() );
145 /**
146 * Reimplemented from QAbstractItemModel::removeRows(). The default implementation
147 * does nothing.
148 */
149 virtual void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );
150
151 //
152 // Drag and drop support
153 //
154 /**
155 * Reimplemented from QAbstractItemModel::supportedDropActions(). The default
156 * implementation returns Qt::MoveAction.
157 */
158 virtual Qt::DropActions supportedDropActions() const;
159 /**
160 * Reimplemented from QAbstractItemModel::mimedata(). This is a placeholder for when
161 * LdapModel beomes writeable and always returns 0.
162 */
163 virtual QMimeData *mimeData( const QModelIndexList &indexes ) const;
164 /**
165 * Reimplemented from QAbstractItemModel::dropMimedata(). This is a placeholder for when
166 * LdapModel beomes writeable and always returns false.
167 */
168 virtual bool dropMimeData( const QMimeData *data, Qt::DropAction action,
169 int row, int column, const QModelIndex &parent );
170
171 //
172 // Other public utility functions
173 //
174 /**
175 * Checks to see if the item referenced by \p parent has any children of
176 * the type \p type. If the item has not been populated by fetchMore() yet,
177 * then this function returns true.
178 *
179 * \see fetchMore()
180 * \param parent Index to the item to query.
181 * \param type The type of child item to search for.
182 */
183 bool hasChildrenOfType( const QModelIndex &parent, LdapDataType type ) const;
184
185 public Q_SLOTS:
186 /**
187 * Reimplemented from QAbstractItemModel::revert(). This is a placeholder for when
188 * LdapModel beomes writeable. This implementation does nothing.
189 */
190 virtual void revert();
191 /**
192 * Reimplemented from QAbstractItemModel::revert(). This is a placeholder for when
193 * LdapModel beomes writeable. This implementation does nothing and returns false.
194 */
195 virtual bool submit();
196
197 Q_SIGNALS:
198 /**
199 * The ready() signal is emitted when the model is ready for use by other components.
200 * When the model is first created and a connection is set, the model queries the
201 * LDAP server for its base DN and automatically creates items down to that level.
202 * This requires the event loop to be running. This signal indicates that this process
203 * has completed and the model can now be set onto views or queried directly from code.
204 */
205 void ready();
206
207 private:
208 class LdapModelPrivate;
209 LdapModelPrivate *const m_d;
210
211 Q_PRIVATE_SLOT( m_d, void gotSearchResult( KLDAP::LdapSearch* ) )
212 Q_PRIVATE_SLOT( m_d, void gotSearchData( KLDAP::LdapSearch*, const KLDAP::LdapObject& ) )
213};
214
215}
216#endif
217