Mailcommon

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

KDE's Doxygen guidelines are available online.