Mailcommon

selectthunderbirdfilterfileswidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2012-2022 Laurent Montel <[email protected]>
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>
14 using namespace MailCommon;
15 
16 SelectThunderbirdFilterFilesWidget::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 
44 SelectThunderbirdFilterFilesWidget::~SelectThunderbirdFilterFilesWidget()
45 {
46  delete ui;
47 }
48 
49 void SelectThunderbirdFilterFilesWidget::slotItemSelectionChanged()
50 {
51  Q_EMIT enableOkButton(!ui->listFiles->selectedItems().isEmpty());
52 }
53 
54 void SelectThunderbirdFilterFilesWidget::slotUrlChanged(const QString &path)
55 {
56  Q_EMIT enableOkButton(!path.isEmpty());
57 }
58 
59 void 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 
74 void 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 + QLatin1String("/msgFilterRules.dat"));
90  if (QFile(filterFile).exists()) {
91  listFilterFiles << filterFile;
92  }
93  }
94  }
95  ui->listFiles->clear();
96  ui->listFiles->addItems(listFilterFiles);
97 }
98 
99 QStringList 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 
114 void SelectThunderbirdFilterFilesWidget::setStartDir(const QUrl &url)
115 {
116  ui->fileUrl->setFilter(QStringLiteral("*.dat"));
117  ui->fileUrl->setStartDir(url);
118 }
QMap::const_iterator constBegin() const const
const T value(const Key &key, const T &defaultValue) const const
int count(const T &value) const const
void buttonClicked(QAbstractButton *button)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
void textChanged(const QString &)
void reserve(int alloc)
QString i18n(const char *text, const TYPE &arg...)
QMap::const_iterator constEnd() const const
bool isEmpty() const const
void setupUi(QWidget *widget)
const Key key(const T &value, const Key &defaultKey) const const
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
QString name(StandardShortcut id)
void currentIndexChanged(int index)
void clear()
void itemSelectionChanged()
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.