File indexing completed on 2024-12-15 04:55:36

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "importimapsettingsearchpage.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QLabel>
0011 #include <QListWidget>
0012 #include <QVBoxLayout>
0013 
0014 ImportImapSettingSearchPage::ImportImapSettingSearchPage(QWidget *parent)
0015     : QWidget(parent)
0016 {
0017     auto mainLayout = new QVBoxLayout(this);
0018     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0019     auto label = new QLabel(i18n("Please select the program from which you like to import IMAP settings:"), this);
0020     label->setObjectName(QLatin1StringView("label"));
0021     mainLayout->addWidget(label);
0022 
0023     mFoundProgramList = new QListWidget(this);
0024     mFoundProgramList->setObjectName(QLatin1StringView("foundprogramlist"));
0025     mainLayout->addWidget(mFoundProgramList);
0026     connect(mFoundProgramList, &QListWidget::itemChanged, this, &ImportImapSettingSearchPage::slotItemChanged);
0027 }
0028 
0029 ImportImapSettingSearchPage::~ImportImapSettingSearchPage() = default;
0030 
0031 void ImportImapSettingSearchPage::setProgramList(const QStringList &lst)
0032 {
0033     for (const QString &prog : lst) {
0034         auto item = new QListWidgetItem(prog, mFoundProgramList);
0035         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
0036         item->setCheckState(Qt::Unchecked);
0037     }
0038 }
0039 
0040 QStringList ImportImapSettingSearchPage::selectedPrograms() const
0041 {
0042     QStringList checkedItems;
0043     const int nbProgram = {mFoundProgramList->count()};
0044     for (int i = 0; i < nbProgram; ++i) {
0045         QListWidgetItem *item = mFoundProgramList->item(i);
0046         if (item->checkState() == Qt::Checked) {
0047             checkedItems << item->text();
0048         }
0049     }
0050     return checkedItems;
0051 }
0052 
0053 void ImportImapSettingSearchPage::slotItemChanged()
0054 {
0055     bool hasSelectedItem = false;
0056     const int nbProgram = {mFoundProgramList->count()};
0057     for (int i = 0; i < nbProgram; ++i) {
0058         QListWidgetItem *item = mFoundProgramList->item(i);
0059         if (item->checkState() == Qt::Checked) {
0060             hasSelectedItem = true;
0061             break;
0062         }
0063     }
0064     Q_EMIT needToImportSettings(hasSelectedItem);
0065 }
0066 
0067 #include "moc_importimapsettingsearchpage.cpp"