Mailcommon

selectthunderbirdfilterfileswidget.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "selectthunderbirdfilterfileswidget.h"
8#include "ui_selectthunderbirdfilterfileswidget.h"
9#include <MailImporter/FilterThunderbird>
10#include <QAbstractButton>
11#include <QListWidgetItem>
12
13#include <KLocalizedString>
14using namespace MailCommon;
15
16SelectThunderbirdFilterFilesWidget::SelectThunderbirdFilterFilesWidget(const QString &defaultSettingPath, QWidget *parent)
17 : QWidget(parent)
18 , ui(new Ui::SelectThunderbirdFilterFilesWidget)
19{
20 ui->setupUi(this);
21 connect(ui->buttonGroup, &QButtonGroup::buttonClicked, this, &SelectThunderbirdFilterFilesWidget::slotButtonClicked);
22 connect(ui->profiles, &KComboBox::currentIndexChanged, this, &SelectThunderbirdFilterFilesWidget::slotProfileChanged);
23
24 ui->listFiles->setSelectionMode(QAbstractItemView::MultiSelection);
25 QString defaultProfile;
26 QMap<QString, QString> listProfile = MailImporter::FilterThunderbird::listProfile(defaultProfile, defaultSettingPath);
28 while (i != listProfile.constEnd()) {
29 QString name = i.key();
30 if (i.value() == defaultProfile) {
31 name += i18n(" (default)");
32 }
33 ui->profiles->addItem(name, i.value());
34 ++i;
35 }
36 ui->fileUrl->setEnabled(true);
37 ui->profiles->setEnabled(false);
38 ui->listFiles->setEnabled(false);
39 slotProfileChanged(0);
40 connect(ui->fileUrl, &KUrlRequester::textChanged, this, &SelectThunderbirdFilterFilesWidget::slotUrlChanged);
41 connect(ui->listFiles, &QListWidget::itemSelectionChanged, this, &SelectThunderbirdFilterFilesWidget::slotItemSelectionChanged);
42}
43
44SelectThunderbirdFilterFilesWidget::~SelectThunderbirdFilterFilesWidget()
45{
46 delete ui;
47}
48
49void SelectThunderbirdFilterFilesWidget::slotItemSelectionChanged()
50{
51 Q_EMIT enableOkButton(!ui->listFiles->selectedItems().isEmpty());
52}
53
54void SelectThunderbirdFilterFilesWidget::slotUrlChanged(const QString &path)
55{
56 Q_EMIT enableOkButton(!path.isEmpty());
57}
58
59void SelectThunderbirdFilterFilesWidget::slotButtonClicked(QAbstractButton *button)
60{
61 if (button == ui->selectFile) {
62 ui->fileUrl->setEnabled(true);
63 ui->profiles->setEnabled(false);
64 ui->listFiles->setEnabled(false);
65 Q_EMIT enableOkButton(!ui->fileUrl->text().isEmpty());
66 } else {
67 ui->fileUrl->setEnabled(false);
68 ui->profiles->setEnabled(true);
69 ui->listFiles->setEnabled(true);
70 slotItemSelectionChanged();
71 }
72}
73
74void SelectThunderbirdFilterFilesWidget::slotProfileChanged(int index)
75{
76 if (index >= ui->profiles->count()) {
77 return;
78 }
79
80 QStringList listFilterFiles;
81 const QString path(MailImporter::FilterThunderbird::defaultSettingsPath() + ui->profiles->itemData(index).toString());
82 QDir dir(path);
83 const QStringList subDir = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
84 for (const QString &mailPath : subDir) {
85 const QString subMailPath(path + QLatin1Char('/') + mailPath);
86 QDir dirMail(subMailPath);
87 const QStringList subDirMail = dirMail.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
88 for (const QString &file : subDirMail) {
89 const QString filterFile(subMailPath + QLatin1Char('/') + file + QLatin1StringView("/msgFilterRules.dat"));
90 if (QFile(filterFile).exists()) {
91 listFilterFiles << filterFile;
92 }
93 }
94 }
95 ui->listFiles->clear();
96 ui->listFiles->addItems(listFilterFiles);
97}
98
99QStringList SelectThunderbirdFilterFilesWidget::selectedFiles() const
100{
101 QStringList listFiles;
102 if (ui->selectFile->isChecked()) {
103 listFiles << ui->fileUrl->url().path();
104 } else {
105 const QList<QListWidgetItem *> list = ui->listFiles->selectedItems();
106 listFiles.reserve(list.count());
107 for (QListWidgetItem *item : list) {
108 listFiles << item->text();
109 }
110 }
111 return listFiles;
112}
113
114void SelectThunderbirdFilterFilesWidget::setStartDir(const QUrl &url)
115{
116 ui->fileUrl->setNameFilter(i18n("Thunderbird File (*.dat)"));
117 ui->fileUrl->setStartDir(url);
118}
119
120#include "moc_selectthunderbirdfilterfileswidget.cpp"
void textChanged(const QString &)
QString i18n(const char *text, const TYPE &arg...)
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString name(StandardAction id)
The filter dialog.
void buttonClicked(QAbstractButton *button)
void currentIndexChanged(int index)
qsizetype count() const const
void reserve(qsizetype size)
void itemSelectionChanged()
const_iterator constBegin() const const
const_iterator constEnd() const const
Q_EMITQ_EMIT
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:02:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.