8#include "completionorderwidget.h"
11#include <KDescendantsProxyModel>
12#include <KLDAPCore/LdapClient>
13#include <KLDAPCore/LdapClientSearch>
15#include <KContacts/Addressee>
16#include <KContacts/ContactGroup>
17#include <KLDAPCore/LdapClientSearchConfig>
19#include <Akonadi/ChangeRecorder>
20#include <Akonadi/CollectionFilterProxyModel>
21#include <Akonadi/EntityTreeModel>
22#include <Akonadi/Monitor>
24#include <kldapcore/ldapserver.h>
26#include <KConfigGroup>
27#include <KLocalizedString>
28#include <QDBusConnection>
32#include <QTreeWidgetItem>
37CompletionOrderEditorAdaptor::CompletionOrderEditorAdaptor(
QObject *parent)
40 setAutoRelaySignals(
true);
43class LDAPCompletionItem :
public CompletionItem
47 : mLdapClient(ldapClient)
52 [[nodiscard]]
QString label()
const override
57 [[nodiscard]]
QIcon icon()
const override
62 [[nodiscard]]
int completionWeight()
const override
67 void save(CompletionOrderWidget *)
override
69 KConfig *config = KLDAPCore::LdapClientSearchConfig::config();
71 group.writeEntry(QStringLiteral(
"SelectedCompletionWeight%1").arg(mLdapClient->
clientNumber()), mWeight);
75 [[nodiscard]]
bool hasEnableSupport()
const override
80 [[nodiscard]]
bool isEnabled()
const override
85 void setIsEnabled(
bool b)
override
91 void setCompletionWeight(
int weight)
override
101class SimpleCompletionItem :
public CompletionItem
104 SimpleCompletionItem(CompletionOrderWidget *editor,
const QString &label,
const QString &identifier,
int weight,
bool enableSupport =
false)
106 , mIdentifier(identifier)
107 , mHasEnableSupport(enableSupport)
110 KConfigGroup groupCompletionWeights(editor->configFile(), QStringLiteral(
"CompletionWeights"));
111 mWeight = groupCompletionWeights.readEntry(mIdentifier, weight);
112 if (mHasEnableSupport) {
113 KConfigGroup groupEnabled(editor->configFile(), QStringLiteral(
"CompletionEnabled"));
114 mEnabled = groupEnabled.readEntry(mIdentifier,
true);
118 ~SimpleCompletionItem()
override =
default;
120 [[nodiscard]]
bool isEnabled()
const override
125 [[nodiscard]]
bool hasEnableSupport()
const override
127 return mHasEnableSupport;
130 void setIcon(
const QIcon &icon)
140 [[nodiscard]]
QIcon icon()
const override
145 [[nodiscard]]
int completionWeight()
const override
150 void setIsEnabled(
bool b)
override
155 void save(CompletionOrderWidget *editor)
override
157 KConfigGroup group(editor->configFile(), QStringLiteral(
"CompletionWeights"));
158 group.writeEntry(mIdentifier, mWeight);
159 if (mHasEnableSupport) {
160 KConfigGroup groupEnabled(editor->configFile(), QStringLiteral(
"CompletionEnabled"));
161 groupEnabled.writeEntry(mIdentifier, isEnabled());
166 void setCompletionWeight(
int weight)
override
176 bool mHasEnableSupport;
191 ~CompletionViewItem()
override
196 void setItem(CompletionItem *item)
201 if (mItem->hasEnableSupport()) {
209 [[nodiscard]] CompletionItem *item()
const
217 const auto completionItem =
static_cast<const CompletionViewItem *
>(otherItem);
219 return mItem->completionWeight() > completionItem->item()->completionWeight();
223 CompletionItem *mItem =
nullptr;
226CompletionOrderWidget::CompletionOrderWidget(
QWidget *parent)
228 , mConfig(QStringLiteral(
"kpimcompletionorder"))
230 new CompletionOrderEditorAdaptor(
this);
237 pageHBoxLayout->setContentsMargins({});
238 hbox->addWidget(page);
240 mListView->setObjectName(
"listview"_L1);
242 pageHBoxLayout->addWidget(mListView);
243 mListView->setColumnCount(1);
244 mListView->setAlternatingRowColors(
true);
245 mListView->setIndentation(0);
246 mListView->setAllColumnsShowFocus(
true);
247 mListView->setHeaderHidden(
true);
248 mListView->setSortingEnabled(
true);
250 auto upDownBox =
new QWidget(page);
251 auto upDownBoxVBoxLayout =
new QVBoxLayout(upDownBox);
252 upDownBoxVBoxLayout->setContentsMargins({});
253 pageHBoxLayout->addWidget(upDownBox);
255 upDownBoxVBoxLayout->addWidget(mUpButton);
256 mUpButton->setAutoRepeat(
true);
257 mUpButton->setObjectName(
"mUpButton"_L1);
259 mUpButton->setEnabled(
false);
260 mUpButton->setToolTip(
i18nc(
"@info:tooltip",
"Move Up"));
264 upDownBoxVBoxLayout->addWidget(mDownButton);
265 mDownButton->setAutoRepeat(
true);
266 mDownButton->setObjectName(
"mDownButton"_L1);
268 mDownButton->setEnabled(
false);
269 mDownButton->setToolTip(
i18nc(
"@info:tooltip",
"Move Down"));
272 auto spacer =
new QWidget(upDownBox);
273 upDownBoxVBoxLayout->addWidget(spacer);
274 upDownBoxVBoxLayout->setStretchFactor(spacer, 100);
283CompletionOrderWidget::~CompletionOrderWidget() =
default;
285void CompletionOrderWidget::save()
290 KConfigGroup group(configFile(), QStringLiteral(
"CompletionWeights"));
293 for (
int itemIndex = 0; itemIndex < mListView->
topLevelItemCount(); ++itemIndex) {
294 auto item =
static_cast<CompletionViewItem *
>(mListView->
topLevelItem(itemIndex));
295 item->item()->setCompletionWeight(w);
296 item->item()->setIsEnabled(item->checkState(0) ==
Qt::Checked);
297 item->item()->save(
this);
300 Q_EMIT completionOrderChanged();
304KConfig *CompletionOrderWidget::configFile()
309void CompletionOrderWidget::addRecentAddressItem()
312 auto item =
new SimpleCompletionItem(
this,
i18n(
"Recent Addresses"), QStringLiteral(
"Recent Addresses"), 10);
314 new CompletionViewItem(mListView, item);
317void CompletionOrderWidget::addCompletionItemForCollection(
const QModelIndex &index)
320 if (!collection.isValid()) {
327 new CompletionViewItem(mListView, item);
330void CompletionOrderWidget::loadCompletionItems()
336 new CompletionViewItem(mListView,
new LDAPCompletionItem(client));
341 monitor->fetchCollection(
true);
350 descendantsProxy->setDisplayAncestorData(
true);
351 descendantsProxy->setSourceModel(model);
355 mimeTypeProxy->setSourceModel(descendantsProxy);
356 mimeTypeProxy->setExcludeVirtualCollections(
true);
358 mCollectionModel = mimeTypeProxy;
361 for (
int row = 0; row < mCollectionModel->
rowCount(); ++row) {
362 addCompletionItemForCollection(mCollectionModel->
index(row, 0));
364 addRecentAddressItem();
373 mLdapSearch = ldapSearch;
376void CompletionOrderWidget::rowsInserted(
const QModelIndex &parent,
int start,
int end)
378 for (
int row =
start; row <=
end; ++row) {
379 addCompletionItemForCollection(mCollectionModel->
index(row, 0,
parent));
385void CompletionOrderWidget::slotItemChanged()
390void CompletionOrderWidget::slotSelectionChanged()
397static void swapItems(CompletionViewItem *one, CompletionViewItem *other)
399 CompletionItem *oneCompletion = one->item();
400 CompletionItem *otherCompletion = other->item();
402 int weight = otherCompletion->completionWeight();
403 otherCompletion->setCompletionWeight(oneCompletion->completionWeight());
404 oneCompletion->setCompletionWeight(weight);
406 one->setItem(oneCompletion);
407 other->setItem(otherCompletion);
410void CompletionOrderWidget::slotMoveUp()
412 auto item =
static_cast<CompletionViewItem *
>(mListView->
currentItem());
416 auto above =
static_cast<CompletionViewItem *
>(mListView->
itemAbove(item));
420 swapItems(item, above);
422 slotSelectionChanged();
426void CompletionOrderWidget::slotMoveDown()
428 auto item =
static_cast<CompletionViewItem *
>(mListView->
currentItem());
432 auto below =
static_cast<CompletionViewItem *
>(mListView->
itemBelow(item));
436 swapItems(item, below);
438 slotSelectionChanged();
442#include "moc_completionorderwidget.cpp"
QList< KLDAPCore::LdapClient * > clients() const
int completionWeight() const
const KLDAPCore::LdapServer server() const
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QAction * end(const QObject *recvr, const char *slot, QObject *parent)
QString label(StandardShortcut id)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
void rowsInserted(const QModelIndex &parent, int first, int last)
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
QIcon fromTheme(const QString &name)
QVariant data(int role) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QString number(double n, char format, int precision)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setText(int column, const QString &text)
QString toString() const const