34 #include <kdescendantsproxymodel.h>
39 #include <akonadi/changerecorder.h>
40 #include <akonadi/collectionfilterproxymodel.h>
41 #include <akonadi/entitytreemodel.h>
42 #include <akonadi/monitor.h>
44 #include <kabc/addressee.h>
45 #include <kabc/contactgroup.h>
46 #include <kldap/ldapserver.h>
48 #include <KDE/KConfigGroup>
51 #include <KDE/KLocale>
52 #include <KDE/KPushButton>
55 #include <QtDBus/QDBusConnection>
56 #include <QToolButton>
57 #include <QTreeWidget>
64 setAutoRelaySignals(
true );
71 : mLdapClient( ldapClient ), mWeight(0)
75 virtual QString label()
const
77 return i18n(
"LDAP server %1", mLdapClient->server().host() );
80 virtual QIcon icon()
const
82 return KIcon( QLatin1String(
"view-ldap-resource") );
85 virtual int completionWeight()
const
87 return mLdapClient->completionWeight();
92 KConfig *config = KLDAP::LdapClientSearchConfig::config();
93 KConfigGroup group( config,
"LDAP" );
94 group.writeEntry( QString::fromLatin1(
"SelectedCompletionWeight%1" ).arg( mLdapClient->clientNumber() ),
100 virtual void setCompletionWeight(
int weight )
113 SimpleCompletionItem(
CompletionOrderEditor* editor,
const QString& label,
const QString& identifier,
int weight )
114 : mLabel( label ), mIdentifier( identifier )
116 KConfigGroup group( editor->
configFile(),
"CompletionWeights" );
117 mWeight = group.readEntry( mIdentifier, weight );
120 void setIcon(
const QIcon &icon )
125 virtual QString label()
const
130 virtual QIcon icon()
const
135 virtual int completionWeight()
const
142 KConfigGroup group( editor->
configFile(),
"CompletionWeights" );
143 group.writeEntry( mIdentifier, mWeight );
147 virtual void setCompletionWeight(
int weight )
161 class CompletionViewItem :
public QTreeWidgetItem
164 CompletionViewItem( QTreeWidget *parent,
CompletionItem* item, QTreeWidgetItem *preceding )
165 : QTreeWidgetItem( parent, preceding )
173 setText( 0, mItem->label() );
174 setIcon( 0, mItem->icon() );
182 bool operator<(
const QTreeWidgetItem &other )
const
184 const QTreeWidgetItem *otherItem = &other;
185 const CompletionViewItem *completionItem =
static_cast<const CompletionViewItem*
>( otherItem );
187 return (mItem->completionWeight() > completionItem->item()->completionWeight());
196 :
KDialog( parent ), mConfig( QLatin1String(
"kpimcompletionorder") ), mLdapSearch( ldapSearch ), mDirty( false )
198 setCaption( i18n(
"Edit Completion Order" ) );
199 setButtons( Ok|Cancel );
200 setDefaultButton( Ok );
202 showButtonSeparator(
true );
204 QDBusConnection::sessionBus().registerObject( QLatin1String(
"/"),
this, QDBusConnection::ExportAdaptors );
207 setMainWidget( page );
208 mListView =
new QTreeWidget( page );
209 mListView->setColumnCount( 1 );
210 mListView->setAlternatingRowColors(
true );
211 mListView->setIndentation( 0 );
212 mListView->setAllColumnsShowFocus(
true );
213 mListView->setHeaderHidden (
true );
214 mListView->setSortingEnabled(
true );
217 mUpButton =
new KPushButton( upDownBox );
218 mUpButton->setAutoRepeat(
true);
219 mUpButton->setObjectName( QLatin1String(
"mUpButton") );
220 mUpButton->setIcon( KIcon(QLatin1String(
"go-up")) );
221 mUpButton->setEnabled(
false );
222 mUpButton->setFocusPolicy( Qt::StrongFocus );
224 mDownButton =
new KPushButton( upDownBox );
225 mDownButton->setAutoRepeat(
true);
226 mDownButton->setObjectName( QLatin1String(
"mDownButton") );
227 mDownButton->setIcon( KIcon(QLatin1String(
"go-down")) );
228 mDownButton->setEnabled(
false );
229 mDownButton->setFocusPolicy( Qt::StrongFocus );
232 upDownBox->setStretchFactor( spacer, 100 );
234 connect( mListView, SIGNAL(itemSelectionChanged()),
235 SLOT(slotSelectionChanged()) );
236 connect( mListView, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
237 SLOT(slotSelectionChanged()) );
238 connect( mUpButton, SIGNAL(clicked()),
this, SLOT(slotMoveUp()) );
239 connect( mDownButton, SIGNAL(clicked()),
this, SLOT(slotMoveDown()) );
240 connect(
this, SIGNAL(okClicked()),
this, SLOT(slotOk()));
242 loadCompletionItems();
251 void CompletionOrderEditor::readConfig()
253 KConfigGroup group( KGlobal::config(),
"CompletionOrderEditor" );
254 const QSize size = group.readEntry(
"Size", QSize(600, 400) );
255 if ( size.isValid() ) {
260 void CompletionOrderEditor::writeConfig()
262 KConfigGroup group( KGlobal::config(),
"CompletionOrderEditor" );
263 group.writeEntry(
"Size", size() );
268 void CompletionOrderEditor::addCompletionItemForIndex(
const QModelIndex &index )
270 const Akonadi::Collection collection = index.data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
271 if ( !collection.isValid() )
274 SimpleCompletionItem *item =
new SimpleCompletionItem(
this, index.data().toString(), QString::number( collection.id() ), 60 );
275 item->setIcon( index.data( Qt::DecorationRole ).value<QIcon>() );
277 new CompletionViewItem( mListView, item, 0 );
280 void CompletionOrderEditor::loadCompletionItems()
284 new CompletionViewItem( mListView,
new LDAPCompletionItem( client ), 0 );
287 Akonadi::ChangeRecorder *monitor =
new Akonadi::ChangeRecorder(
this );
288 monitor->fetchCollection(
true );
289 monitor->setCollectionMonitored( Akonadi::Collection::root() );
290 monitor->setMimeTypeMonitored( KABC::Addressee::mimeType(),
true );
291 monitor->setMimeTypeMonitored( KABC::ContactGroup::mimeType(),
true );
293 Akonadi::EntityTreeModel *model =
new Akonadi::EntityTreeModel( monitor,
this );
294 model->setItemPopulationStrategy( Akonadi::EntityTreeModel::NoItemPopulation );
296 KDescendantsProxyModel *descendantsProxy =
new KDescendantsProxyModel(
this );
297 descendantsProxy->setDisplayAncestorData(
true );
298 descendantsProxy->setSourceModel( model );
300 Akonadi::CollectionFilterProxyModel *mimeTypeProxy =
new Akonadi::CollectionFilterProxyModel(
this );
301 mimeTypeProxy->addMimeTypeFilters( QStringList() << KABC::Addressee::mimeType()
302 << KABC::ContactGroup::mimeType() );
303 mimeTypeProxy->setSourceModel( descendantsProxy );
304 mimeTypeProxy->setExcludeVirtualCollections(
true );
306 mCollectionModel = mimeTypeProxy;
308 connect( mimeTypeProxy, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
309 this, SLOT(rowsInserted(QModelIndex,
int,
int)) );
311 for (
int row = 0; row < mCollectionModel->rowCount(); ++row )
312 addCompletionItemForIndex( mCollectionModel->index( row, 0 ) );
314 mListView->sortItems( 0, Qt::AscendingOrder );
317 void CompletionOrderEditor::rowsInserted(
const QModelIndex &parent,
int start,
int end )
319 for (
int row = start; row <= end; ++row )
320 addCompletionItemForIndex( mCollectionModel->index( row, 0, parent ) );
322 mListView->sortItems( 0, Qt::AscendingOrder );
325 void CompletionOrderEditor::slotSelectionChanged()
327 QTreeWidgetItem *item = mListView->currentItem();
328 mDownButton->setEnabled( item && mListView->itemBelow( item ) );
329 mUpButton->setEnabled( item && mListView->itemAbove( item ) );
332 static void swapItems( CompletionViewItem *one, CompletionViewItem *other )
341 one->setItem( otherCompletion );
342 other->setItem( oneCompletion );
345 void CompletionOrderEditor::slotMoveUp()
347 CompletionViewItem *item =
static_cast<CompletionViewItem *
>( mListView->currentItem() );
349 CompletionViewItem *above =
static_cast<CompletionViewItem *
>( mListView->itemAbove( item ) );
350 if ( !above )
return;
352 mListView->setCurrentItem( above, 0, QItemSelectionModel::Select | QItemSelectionModel::Current );
353 mListView->sortItems( 0, Qt::AscendingOrder );
357 void CompletionOrderEditor::slotMoveDown()
359 CompletionViewItem *item =
static_cast<CompletionViewItem *
>( mListView->currentItem() );
361 CompletionViewItem *below =
static_cast<CompletionViewItem *
>( mListView->itemBelow( item ) );
362 if ( !below )
return;
364 mListView->setCurrentItem( below );
365 mListView->setCurrentItem( below, 0, QItemSelectionModel::Select | QItemSelectionModel::Current );
366 mListView->sortItems( 0, Qt::AscendingOrder );
370 void CompletionOrderEditor::slotOk()
374 for (
int itemIndex = 0; itemIndex < mListView->topLevelItemCount(); ++itemIndex ) {
375 CompletionViewItem *item =
376 static_cast<CompletionViewItem *
>( mListView->topLevelItem( itemIndex ) );
377 item->item()->setCompletionWeight( w );
378 item->item()->save(
this );
386 #include "completionordereditor_p.moc"
387 #include "completionordereditor.moc"
virtual void setCompletionWeight(int weight)=0
QList< LdapClient * > clients() const
Returns the list of configured LDAP clients.
CompletionOrderEditor(KLDAP::LdapClientSearch *ldapSearch, QWidget *parent)
static void swapItems(CompletionViewItem *one, CompletionViewItem *other)
virtual int completionWeight() const =0
void completionOrderChanged()
An object that represents a configured LDAP server.
CompletionOrderEditorAdaptor(QObject *parent)