File indexing completed on 2024-11-10 04:50:02

0001 /*
0002   SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "selectthunderbirdfilterfileswidget.h"
0008 #include "ui_selectthunderbirdfilterfileswidget.h"
0009 #include <MailImporter/FilterThunderbird>
0010 #include <QAbstractButton>
0011 #include <QListWidgetItem>
0012 
0013 #include <KLocalizedString>
0014 using namespace MailCommon;
0015 
0016 SelectThunderbirdFilterFilesWidget::SelectThunderbirdFilterFilesWidget(const QString &defaultSettingPath, QWidget *parent)
0017     : QWidget(parent)
0018     , ui(new Ui::SelectThunderbirdFilterFilesWidget)
0019 {
0020     ui->setupUi(this);
0021     connect(ui->buttonGroup, &QButtonGroup::buttonClicked, this, &SelectThunderbirdFilterFilesWidget::slotButtonClicked);
0022     connect(ui->profiles, &KComboBox::currentIndexChanged, this, &SelectThunderbirdFilterFilesWidget::slotProfileChanged);
0023 
0024     ui->listFiles->setSelectionMode(QAbstractItemView::MultiSelection);
0025     QString defaultProfile;
0026     QMap<QString, QString> listProfile = MailImporter::FilterThunderbird::listProfile(defaultProfile, defaultSettingPath);
0027     QMap<QString, QString>::const_iterator i = listProfile.constBegin();
0028     while (i != listProfile.constEnd()) {
0029         QString name = i.key();
0030         if (i.value() == defaultProfile) {
0031             name += i18n(" (default)");
0032         }
0033         ui->profiles->addItem(name, i.value());
0034         ++i;
0035     }
0036     ui->fileUrl->setEnabled(true);
0037     ui->profiles->setEnabled(false);
0038     ui->listFiles->setEnabled(false);
0039     slotProfileChanged(0);
0040     connect(ui->fileUrl, &KUrlRequester::textChanged, this, &SelectThunderbirdFilterFilesWidget::slotUrlChanged);
0041     connect(ui->listFiles, &QListWidget::itemSelectionChanged, this, &SelectThunderbirdFilterFilesWidget::slotItemSelectionChanged);
0042 }
0043 
0044 SelectThunderbirdFilterFilesWidget::~SelectThunderbirdFilterFilesWidget()
0045 {
0046     delete ui;
0047 }
0048 
0049 void SelectThunderbirdFilterFilesWidget::slotItemSelectionChanged()
0050 {
0051     Q_EMIT enableOkButton(!ui->listFiles->selectedItems().isEmpty());
0052 }
0053 
0054 void SelectThunderbirdFilterFilesWidget::slotUrlChanged(const QString &path)
0055 {
0056     Q_EMIT enableOkButton(!path.isEmpty());
0057 }
0058 
0059 void SelectThunderbirdFilterFilesWidget::slotButtonClicked(QAbstractButton *button)
0060 {
0061     if (button == ui->selectFile) {
0062         ui->fileUrl->setEnabled(true);
0063         ui->profiles->setEnabled(false);
0064         ui->listFiles->setEnabled(false);
0065         Q_EMIT enableOkButton(!ui->fileUrl->text().isEmpty());
0066     } else {
0067         ui->fileUrl->setEnabled(false);
0068         ui->profiles->setEnabled(true);
0069         ui->listFiles->setEnabled(true);
0070         slotItemSelectionChanged();
0071     }
0072 }
0073 
0074 void SelectThunderbirdFilterFilesWidget::slotProfileChanged(int index)
0075 {
0076     if (index >= ui->profiles->count()) {
0077         return;
0078     }
0079 
0080     QStringList listFilterFiles;
0081     const QString path(MailImporter::FilterThunderbird::defaultSettingsPath() + ui->profiles->itemData(index).toString());
0082     QDir dir(path);
0083     const QStringList subDir = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
0084     for (const QString &mailPath : subDir) {
0085         const QString subMailPath(path + QLatin1Char('/') + mailPath);
0086         QDir dirMail(subMailPath);
0087         const QStringList subDirMail = dirMail.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
0088         for (const QString &file : subDirMail) {
0089             const QString filterFile(subMailPath + QLatin1Char('/') + file + QLatin1StringView("/msgFilterRules.dat"));
0090             if (QFile(filterFile).exists()) {
0091                 listFilterFiles << filterFile;
0092             }
0093         }
0094     }
0095     ui->listFiles->clear();
0096     ui->listFiles->addItems(listFilterFiles);
0097 }
0098 
0099 QStringList SelectThunderbirdFilterFilesWidget::selectedFiles() const
0100 {
0101     QStringList listFiles;
0102     if (ui->selectFile->isChecked()) {
0103         listFiles << ui->fileUrl->url().path();
0104     } else {
0105         const QList<QListWidgetItem *> list = ui->listFiles->selectedItems();
0106         listFiles.reserve(list.count());
0107         for (QListWidgetItem *item : list) {
0108             listFiles << item->text();
0109         }
0110     }
0111     return listFiles;
0112 }
0113 
0114 void SelectThunderbirdFilterFilesWidget::setStartDir(const QUrl &url)
0115 {
0116     ui->fileUrl->setNameFilter(i18n("Thunderbird File (*.dat)"));
0117     ui->fileUrl->setStartDir(url);
0118 }
0119 
0120 #include "moc_selectthunderbirdfilterfileswidget.cpp"