Mailcommon

filteractionmissingfolderdialog.cpp
1 /*
2 
3  SPDX-FileCopyrightText: 2011-2022 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "filteractionmissingfolderdialog.h"
9 #include "filter/kmfilteraccountlist.h"
10 #include "folder/folderrequester.h"
11 #include "kernel/mailkernel.h"
12 #include "tag/addtagdialog.h"
13 #include "util/mailutil.h"
14 
15 #include <Akonadi/EntityMimeTypeFilterModel>
16 
17 #include <KIdentityManagement/IdentityCombo>
18 
19 #include <KLocalizedString>
20 
21 #include <KConfigGroup>
22 #include <QDialogButtonBox>
23 #include <QLabel>
24 #include <QListWidget>
25 #include <QPushButton>
26 #include <QVBoxLayout>
27 
28 FilterActionMissingFolderDialog::FilterActionMissingFolderDialog(const Akonadi::Collection::List &list,
29  const QString &filtername,
30  const QString &argStr,
31  QWidget *parent)
32  : QDialog(parent)
33  , mFolderRequester(new MailCommon::FolderRequester(this))
34 {
35  setModal(true);
36  setWindowTitle(i18nc("@title:window", "Select Folder"));
37  auto mainLayout = new QVBoxLayout(this);
38 
39  auto lab = new QLabel(i18n("Folder path was \"%1\".", argStr));
40  lab->setObjectName(QStringLiteral("argumentlabel"));
41  lab->setWordWrap(true);
42  mainLayout->addWidget(lab);
43  if (!list.isEmpty()) {
44  lab = new QLabel(i18n("The following folders can be used for this filter:"));
45  lab->setObjectName(QStringLiteral("label"));
46  lab->setWordWrap(true);
47  mainLayout->addWidget(lab);
48  mListwidget = new QListWidget(this);
49  mainLayout->addWidget(mListwidget);
50  const int numberOfItems(list.count());
51  for (int i = 0; i < numberOfItems; ++i) {
52  const Akonadi::Collection col = list.at(i);
53  auto item = new QListWidgetItem(MailCommon::Util::fullCollectionPath(col));
54  item->setData(FilterActionMissingFolderDialog::IdentifyCollection, col.id());
55  mListwidget->addItem(item);
56  }
57  connect(mListwidget, &QListWidget::currentItemChanged, this, &FilterActionMissingFolderDialog::slotCurrentItemChanged);
58  connect(mListwidget, &QListWidget::itemDoubleClicked, this, &FilterActionMissingFolderDialog::slotDoubleItemClicked);
59  }
60 
61  auto label = new QLabel(this);
62  label->setObjectName(QStringLiteral("folderlabel"));
63  label->setWordWrap(true);
64  if (filtername.isEmpty()) {
65  label->setText(i18n("Please select a folder:"));
66  } else {
67  label->setText(
68  i18n("Filter folder is missing. "
69  "Please select a folder to use with filter \"%1\":",
70  filtername));
71  }
72  mainLayout->addWidget(label);
73  mFolderRequester->setObjectName(QStringLiteral("folderrequester"));
74  connect(mFolderRequester, &MailCommon::FolderRequester::folderChanged, this, &FilterActionMissingFolderDialog::slotFolderChanged);
75  mainLayout->addWidget(mFolderRequester);
76  mainLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding));
77 
79  buttonBox->setObjectName(QStringLiteral("buttonbox"));
80  mOkButton = buttonBox->button(QDialogButtonBox::Ok);
81  mOkButton->setDefault(true);
82  mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
83  mOkButton->setEnabled(false);
84  connect(buttonBox, &QDialogButtonBox::accepted, this, &FilterActionMissingFolderDialog::accept);
85  connect(buttonBox, &QDialogButtonBox::rejected, this, &FilterActionMissingFolderDialog::reject);
86  mainLayout->addWidget(buttonBox);
87  readConfig();
88 }
89 
90 FilterActionMissingFolderDialog::~FilterActionMissingFolderDialog()
91 {
92  writeConfig();
93 }
94 
95 void FilterActionMissingFolderDialog::readConfig()
96 {
97  KConfigGroup group(KSharedConfig::openStateConfig(), "FilterActionMissingCollectionDialog");
98 
99  const QSize size = group.readEntry("Size", QSize(500, 300));
100  if (size.isValid()) {
101  resize(size);
102  }
103 }
104 
105 void FilterActionMissingFolderDialog::writeConfig()
106 {
107  KConfigGroup group(KSharedConfig::openStateConfig(), "FilterActionMissingCollectionDialog");
108  group.writeEntry("Size", size());
109 }
110 
111 void FilterActionMissingFolderDialog::slotFolderChanged(const Akonadi::Collection &col)
112 {
113  mOkButton->setEnabled(col.isValid());
114 }
115 
116 void FilterActionMissingFolderDialog::slotDoubleItemClicked(QListWidgetItem *item)
117 {
118  if (!item) {
119  return;
120  }
121 
122  const Akonadi::Collection::Id id = item->data(FilterActionMissingFolderDialog::IdentifyCollection).toLongLong();
123 
124  mFolderRequester->setCollection(Akonadi::Collection(id));
125  accept();
126 }
127 
128 void FilterActionMissingFolderDialog::slotCurrentItemChanged()
129 {
130  QListWidgetItem *currentItem = mListwidget->currentItem();
131  if (currentItem) {
132  const Akonadi::Collection::Id id = currentItem->data(FilterActionMissingFolderDialog::IdentifyCollection).toLongLong();
133  mFolderRequester->setCollection(Akonadi::Collection(id));
134  }
135 }
136 
137 Akonadi::Collection FilterActionMissingFolderDialog::selectedCollection() const
138 {
139  return mFolderRequester->collection();
140 }
141 
142 void FilterActionMissingFolderDialog::getPotentialFolders(const QAbstractItemModel *model,
143  const QModelIndex &parentIndex,
144  const QString &lastElement,
146 {
147  const int rowCount = model->rowCount(parentIndex);
148  for (int row = 0; row < rowCount; ++row) {
149  const QModelIndex index = model->index(row, 0, parentIndex);
150  if (model->rowCount(index) > 0) {
151  getPotentialFolders(model, index, lastElement, list);
152  }
153  if (model->data(index).toString() == lastElement) {
155  }
156  }
157 }
158 
159 Akonadi::Collection::List FilterActionMissingFolderDialog::potentialCorrectFolders(const QString &path, bool &exactPath)
160 {
162  const QString realPath = MailCommon::Util::realFolderPath(path);
163  if (realPath.isEmpty()) {
164  return lst;
165  }
166 
167  if (KernelIf->collectionModel()) {
168  const int lastSlash = realPath.lastIndexOf(QLatin1Char('/'));
169  QString lastElement;
170  if (lastSlash == -1) {
171  lastElement = realPath;
172  } else {
173  lastElement = realPath.right(realPath.length() - lastSlash - 1);
174  }
175 
176  FilterActionMissingFolderDialog::getPotentialFolders(KernelIf->collectionModel(), QModelIndex(), lastElement, lst);
177 
178  const int numberOfItems(lst.count());
179  for (int i = 0; i < numberOfItems; ++i) {
180  if (MailCommon::Util::fullCollectionPath(lst.at(i)) == realPath) {
181  exactPath = true;
182  return Akonadi::Collection::List() << lst.at(i);
183  }
184  }
185  }
186  return lst;
187 }
bool isValid() const const
virtual int rowCount(const QModelIndex &parent) const const=0
virtual QVariant data(const QModelIndex &index, int role) const const=0
T value() const const
void readConfig()
QVector< Collection > List
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
qlonglong toLongLong(bool *ok) const const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
virtual QVariant data(int role) const const
QString i18n(const char *text, const TYPE &arg...)
const T & at(int i) const const
bool isEmpty() const const
int length() const const
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
Key_Return
QString label(StandardShortcut id)
void itemDoubleClicked(QListWidgetItem *item)
QString right(int n) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
QString i18nc(const char *context, const char *text, const TYPE &arg...)
int count(const T &value) const const
bool isValid() const
const MAILCOMMON_EXPORT QVector< FilterActionDesc * > & list() const
Provides read-only access to a list of all known filter actions.
The filter dialog.
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.