File indexing completed on 2024-12-01 04:46:43
0001 /* 0002 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "mergecontactwidgetlist.h" 0008 0009 #include <KLocalizedString> 0010 #include <QMenu> 0011 0012 using namespace KABMergeContacts; 0013 MergeContactWidgetList::MergeContactWidgetList(QWidget *parent) 0014 : QListWidget(parent) 0015 { 0016 setContextMenuPolicy(Qt::CustomContextMenu); 0017 connect(this, &MergeContactWidgetList::customContextMenuRequested, this, &MergeContactWidgetList::slotCustomContextMenuRequested); 0018 } 0019 0020 MergeContactWidgetList::~MergeContactWidgetList() = default; 0021 0022 QString MergeContactWidgetList::itemName(const KContacts::Addressee &address) const 0023 { 0024 const QString realName = address.realName(); 0025 if (!realName.isEmpty()) { 0026 return realName; 0027 } 0028 const QString name = address.name(); 0029 if (!name.isEmpty()) { 0030 return name; 0031 } 0032 return address.fullEmail(); 0033 } 0034 0035 void MergeContactWidgetList::slotCustomContextMenuRequested(const QPoint &) 0036 { 0037 QMenu menu(this); 0038 menu.addAction(i18n("Select All"), this, &MergeContactWidgetList::slotSelectAll); 0039 menu.addAction(i18n("Deselect All"), this, &MergeContactWidgetList::slotDeselectAll); 0040 menu.exec(QCursor::pos()); 0041 } 0042 0043 void MergeContactWidgetList::slotSelectAll() 0044 { 0045 for (int i = 0; i < count(); ++i) { 0046 QListWidgetItem *itemWidget = item(i); 0047 itemWidget->setCheckState(Qt::Checked); 0048 } 0049 } 0050 0051 void MergeContactWidgetList::slotDeselectAll() 0052 { 0053 for (int i = 0; i < count(); ++i) { 0054 QListWidgetItem *itemWidget = item(i); 0055 itemWidget->setCheckState(Qt::Unchecked); 0056 } 0057 } 0058 0059 void MergeContactWidgetList::fillListContact(const Akonadi::Item::List &items) 0060 { 0061 clear(); 0062 for (const Akonadi::Item &item : items) { 0063 if (item.hasPayload<KContacts::Addressee>()) { 0064 auto widgetItem = new MergeContactWidgetListItem(item, this); 0065 auto address = item.payload<KContacts::Addressee>(); 0066 0067 widgetItem->setText(itemName(address)); 0068 } 0069 } 0070 } 0071 0072 Akonadi::Item::List MergeContactWidgetList::listSelectedContacts() const 0073 { 0074 Akonadi::Item::List lstItems; 0075 for (int i = 0; i < count(); ++i) { 0076 QListWidgetItem *itemWidget = item(i); 0077 if (itemWidget->checkState() == Qt::Checked) { 0078 lstItems.append((static_cast<MergeContactWidgetListItem *>(itemWidget))->item()); 0079 } 0080 } 0081 return lstItems; 0082 } 0083 0084 Akonadi::Item MergeContactWidgetList::currentAkonadiItem() const 0085 { 0086 QListWidgetItem *curr = currentItem(); 0087 if (curr) { 0088 return (static_cast<MergeContactWidgetListItem *>(curr))->item(); 0089 } 0090 return {}; 0091 } 0092 0093 MergeContactWidgetListItem::MergeContactWidgetListItem(const Akonadi::Item &item, QListWidget *parent) 0094 : QListWidgetItem(parent) 0095 , mItem(item) 0096 { 0097 setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); 0098 setCheckState(Qt::Unchecked); 0099 } 0100 0101 Akonadi::Item MergeContactWidgetListItem::item() const 0102 { 0103 return mItem; 0104 } 0105 0106 #include "moc_mergecontactwidgetlist.cpp"