File indexing completed on 2024-11-24 04:43:05

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "mergecontactselectlistwidget.h"
0008 #include "kaddressbookmergelibprivate_debug.h"
0009 #include <KLocalizedString>
0010 #include <QLabel>
0011 #include <QListWidget>
0012 #include <QLocale>
0013 #include <QVBoxLayout>
0014 using namespace KABMergeContacts;
0015 using namespace KContacts;
0016 
0017 MergeContactSelectListWidget::MergeContactSelectListWidget(QWidget *parent)
0018     : QWidget(parent)
0019     , mTitle(new QLabel(this))
0020     , mSelectListWidget(new QListWidget(this))
0021     , mConflictType(MergeContacts::None)
0022 {
0023     auto vbox = new QVBoxLayout(this);
0024     mTitle->setObjectName(QLatin1StringView("title"));
0025     vbox->addWidget(mTitle);
0026     mSelectListWidget->setObjectName(QLatin1StringView("listwidget"));
0027     vbox->addWidget(mSelectListWidget);
0028 }
0029 
0030 MergeContactSelectListWidget::~MergeContactSelectListWidget() = default;
0031 
0032 void MergeContactSelectListWidget::setContacts(MergeContacts::ConflictInformation conflictType, const KContacts::Addressee::List &lst)
0033 {
0034     mConflictType = conflictType;
0035     if (lst.isEmpty() || (conflictType == MergeContacts::None)) {
0036         return;
0037     }
0038     updateTitle();
0039     fillList(lst);
0040 }
0041 
0042 void MergeContactSelectListWidget::updateTitle()
0043 {
0044     QString title;
0045     switch (mConflictType) {
0046     case MergeContacts::None:
0047         // qCWarning(KADDRESSBOOKMERGELIBPRIVATE_LOG) << " MergeContacts::None used in updateTitle. It's a bug";
0048         // it's not possible.
0049         break;
0050     case MergeContacts::Birthday:
0051         title = Addressee::birthdayLabel();
0052         break;
0053     case MergeContacts::Geo:
0054         title = Addressee::geoLabel();
0055         break;
0056     case MergeContacts::Photo:
0057         title = Addressee::photoLabel();
0058         break;
0059     case MergeContacts::Logo:
0060         title = Addressee::logoLabel();
0061         break;
0062     case MergeContacts::Anniversary:
0063         title = i18nc("The wedding anniversary of a contact", "Anniversary");
0064         break;
0065     case MergeContacts::Name:
0066         title = Addressee::nameLabel();
0067         break;
0068     case MergeContacts::NickName:
0069         title = Addressee::nickNameLabel();
0070         break;
0071     case MergeContacts::Blog:
0072         title = i18n("Blog Feed");
0073         break;
0074     case MergeContacts::HomePage:
0075         title = QStringLiteral("HomePage");
0076         break;
0077     case MergeContacts::Organization:
0078         title = Addressee::organizationLabel();
0079         break;
0080     case MergeContacts::Profession:
0081         title = i18n("Profession");
0082         break;
0083     case MergeContacts::Title:
0084         title = Addressee::titleLabel();
0085         break;
0086     case MergeContacts::Departement:
0087         title = Addressee::departmentLabel();
0088         break;
0089     case MergeContacts::Office:
0090         title = i18n("Office");
0091         break;
0092     case MergeContacts::ManagerName:
0093         title = i18n("Manager");
0094         break;
0095     case MergeContacts::Assistant:
0096         title = i18n("Assistant");
0097         break;
0098     case MergeContacts::FreeBusy:
0099         title = QStringLiteral("FreeBusy");
0100         break;
0101     case MergeContacts::FamilyName:
0102         title = Addressee::familyNameLabel();
0103         break;
0104     case MergeContacts::PartnerName:
0105         title = i18n("Spouse");
0106         break;
0107     case MergeContacts::Keys:
0108         title = QStringLiteral("Keys");
0109         break;
0110     }
0111 
0112     mTitle->setText(title);
0113 }
0114 
0115 void MergeContactSelectListWidget::addItem(const QString &str, const QIcon &icon)
0116 {
0117     if (str.isEmpty()) {
0118         auto item = new QListWidgetItem(mSelectListWidget);
0119         item->setFlags(Qt::NoItemFlags);
0120         item->setText(i18n("(Undefined)"));
0121         mSelectListWidget->addItem(item);
0122     } else {
0123         if (!icon.isNull()) {
0124             auto item = new QListWidgetItem(mSelectListWidget);
0125             item->setText(str);
0126             item->setIcon(icon);
0127             mSelectListWidget->addItem(item);
0128         } else {
0129             mSelectListWidget->addItem(str);
0130         }
0131     }
0132 }
0133 
0134 void MergeContactSelectListWidget::fillList(const KContacts::Addressee::List &lst)
0135 {
0136     mSelectListWidget->clear();
0137     for (const Addressee &addr : lst) {
0138         switch (mConflictType) {
0139         case MergeContacts::None:
0140             break;
0141         case MergeContacts::Birthday: {
0142             const QDate birdthDt = addr.birthday().date();
0143             QString birdth;
0144             if (birdthDt.isValid()) {
0145                 birdth = QLocale().toString(birdthDt);
0146             }
0147             addItem(birdth);
0148             break;
0149         }
0150         case MergeContacts::Geo: {
0151             const Geo geo = addr.geo();
0152             const QString str = QStringLiteral("%1-%2").arg(geo.latitude()).arg(geo.longitude());
0153             addItem(str);
0154             break;
0155         }
0156         case MergeContacts::Photo:
0157             // TODO fix when it's an url
0158             addItem(QString(), QIcon(QPixmap::fromImage(addr.photo().data())));
0159             break;
0160         case MergeContacts::Logo:
0161             // TODO fix when it's an url
0162             addItem(QString(), QIcon(QPixmap::fromImage(addr.logo().data())));
0163             break;
0164         case MergeContacts::Anniversary: {
0165             QString anniversary;
0166             const QDate anniversaryDt = QDate::fromString(addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Anniversary")), Qt::ISODate);
0167             if (anniversaryDt.isValid()) {
0168                 anniversary = QLocale().toString(anniversaryDt);
0169             }
0170             addItem(anniversary);
0171             break;
0172         }
0173         case MergeContacts::Name:
0174             addItem(addr.name());
0175             break;
0176         case MergeContacts::NickName:
0177             addItem(addr.nickName());
0178             break;
0179         case MergeContacts::Blog: {
0180             const QString newBlog = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("BlogFeed"));
0181             addItem(newBlog);
0182             break;
0183         }
0184         case MergeContacts::HomePage:
0185             addItem(addr.url().toString());
0186             break;
0187         case MergeContacts::Organization:
0188             addItem(addr.organization());
0189             break;
0190         case MergeContacts::Profession: {
0191             const QString newBlog = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession"));
0192             addItem(newBlog);
0193             break;
0194         }
0195         case MergeContacts::Title:
0196             addItem(addr.title());
0197             break;
0198         case MergeContacts::Departement:
0199             addItem(addr.department());
0200             break;
0201         case MergeContacts::Office: {
0202             const QString newBlog = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Office"));
0203             addItem(newBlog);
0204             break;
0205         }
0206         case MergeContacts::ManagerName: {
0207             const QString newBlog = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ManagersName"));
0208             addItem(newBlog);
0209             break;
0210         }
0211         case MergeContacts::Assistant: {
0212             const QString newBlog = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-AssistantsName"));
0213             addItem(newBlog);
0214             break;
0215         }
0216         case MergeContacts::FreeBusy:
0217             // FIXME
0218             break;
0219         case MergeContacts::FamilyName:
0220             addItem(addr.familyName());
0221             break;
0222         case MergeContacts::PartnerName: {
0223             const QString newBlog = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName"));
0224             addItem(newBlog);
0225             break;
0226         }
0227         case MergeContacts::Keys:
0228             // TODO
0229             break;
0230         }
0231     }
0232 }
0233 
0234 int MergeContactSelectListWidget::selectedContact() const
0235 {
0236     return mSelectListWidget->currentRow();
0237 }
0238 
0239 MergeContacts::ConflictInformation MergeContactSelectListWidget::conflictType() const
0240 {
0241     return mConflictType;
0242 }
0243 
0244 bool MergeContactSelectListWidget::verifySelectedInfo() const
0245 {
0246     return selectedContact() != -1;
0247 }
0248 
0249 #include "moc_mergecontactselectlistwidget.cpp"