7#include "accountconfigorderdialog.h"
8#include "mailcommon_debug.h"
9#include "mailcommonsettings_base.h"
10#include "util/mailutil.h"
11#include <KLocalizedString>
12#include <PimCommon/PimUtil>
17#include <Akonadi/AgentInstance>
18#include <Akonadi/AgentManager>
20#include <KConfigGroup>
21#include <KWindowConfig>
23#include <QDialogButtonBox>
31static const char myAccountConfigOrderDialogName[] =
"AccountConfigOrderDialog";
33struct InstanceStruct {
36 [[nodiscard]]
bool isValid()
const
38 return !name.isEmpty();
42class MailCommon::AccountConfigOrderDialogPrivate
45 QListWidget *mListAccount =
nullptr;
46 QPushButton *mUpButton =
nullptr;
47 QPushButton *mDownButton =
nullptr;
48 QCheckBox *mEnableAccountOrder =
nullptr;
49 MailCommon::MailCommonSettings *mSettings =
nullptr;
52AccountConfigOrderDialog::AccountConfigOrderDialog(MailCommon::MailCommonSettings *settings,
QWidget *parent)
54 , d(new
MailCommon::AccountConfigOrderDialogPrivate)
56 d->mSettings = settings;
57 setWindowTitle(
i18nc(
"@title:window",
"Edit Accounts Order"));
67 mainLayout->addWidget(page);
68 mainLayout->addWidget(buttonBox);
72 page->setLayout(vbox);
74 d->mEnableAccountOrder =
new QCheckBox(
i18nc(
"@option:check",
"Use custom order"),
this);
76 vbox->addWidget(d->mEnableAccountOrder);
83 vlay->addWidget(d->mListAccount);
85 auto upDownBox =
new QWidget(page);
86 auto upDownBoxVBoxLayout =
new QVBoxLayout(upDownBox);
87 upDownBoxVBoxLayout->setContentsMargins({});
89 upDownBoxVBoxLayout->addWidget(d->mUpButton);
91 d->mUpButton->setToolTip(
i18nc(
"Move selected account up.",
"Up"));
92 d->mUpButton->setEnabled(
false);
94 d->mUpButton->setAutoRepeat(
true);
97 upDownBoxVBoxLayout->addWidget(d->mDownButton);
99 d->mDownButton->setToolTip(
i18nc(
"Move selected account down.",
"Down"));
100 d->mDownButton->setEnabled(
false);
102 d->mDownButton->setAutoRepeat(
true);
104 auto spacer =
new QWidget(upDownBox);
105 upDownBoxVBoxLayout->addWidget(spacer);
106 upDownBoxVBoxLayout->setStretchFactor(spacer, 100);
107 vlay->addWidget(upDownBox);
119AccountConfigOrderDialog::~AccountConfigOrderDialog()
124void AccountConfigOrderDialog::slotEnableAccountOrder(
bool state)
126 d->mListAccount->setEnabled(state);
127 d->mUpButton->setEnabled(state);
128 d->mDownButton->setEnabled(state);
130 slotEnableControls();
134void AccountConfigOrderDialog::slotMoveUp()
136 if (!d->mListAccount->currentItem()) {
139 const int pos = d->mListAccount->row(d->mListAccount->currentItem());
140 d->mListAccount->blockSignals(
true);
141 QListWidgetItem *item = d->mListAccount->takeItem(
pos);
144 d->mListAccount->insertItem(
pos - 1, item);
145 d->mListAccount->blockSignals(
false);
146 d->mListAccount->setCurrentRow(
pos - 1);
149void AccountConfigOrderDialog::slotMoveDown()
151 if (!d->mListAccount->currentItem()) {
154 const int pos = d->mListAccount->row(d->mListAccount->currentItem());
155 d->mListAccount->blockSignals(
true);
156 QListWidgetItem *item = d->mListAccount->takeItem(
pos);
159 d->mListAccount->insertItem(
pos + 1, item);
160 d->mListAccount->blockSignals(
false);
161 d->mListAccount->setCurrentRow(
pos + 1);
164void AccountConfigOrderDialog::slotEnableControls()
166 QListWidgetItem *item = d->mListAccount->currentItem();
168 d->mUpButton->setEnabled(item && d->mListAccount->currentRow() != 0);
169 d->mDownButton->setEnabled(item && d->mListAccount->currentRow() != d->mListAccount->count() - 1);
172void AccountConfigOrderDialog::init()
174 const QStringList listOrderAccount = d->mSettings->order();
175 QStringList instanceList;
177 QMap<QString, InstanceStruct> mapInstance;
179 for (
const Akonadi::AgentInstance &instance : lstInstances) {
180 const QString identifier = instance.identifier();
181 if (!MailCommon::Util::isMailAgent(instance) || identifier.
contains(POP3_RESOURCE_IDENTIFIER)) {
184 instanceList << instance.identifier();
185 InstanceStruct instanceStruct;
186 instanceStruct.name = instance.name();
187 if (PimCommon::Util::isImapResource(identifier)) {
188 instanceStruct.name += QLatin1StringView(
" (IMAP)");
189 }
else if (identifier.
startsWith(QLatin1StringView(
"akonadi_maildir_resource"))) {
190 instanceStruct.name += QLatin1StringView(
" (Maildir)");
191 }
else if (identifier.
startsWith(QLatin1StringView(
"akonadi_mailbox_resource"))) {
192 instanceStruct.name += QLatin1StringView(
" (Mailbox)");
193 }
else if (identifier.
startsWith(QLatin1StringView(
"akonadi_mixedmaildir_resource"))) {
194 instanceStruct.name += QLatin1StringView(
" (Mixedmaildir)");
196 qCDebug(MAILCOMMON_LOG) <<
" Unknown resource type " << identifier;
198 instanceStruct.icon = instance.type().icon();
199 mapInstance.
insert(instance.identifier(), instanceStruct);
202 const int numberOfList(listOrderAccount.
count());
203 for (
int i = 0; i < numberOfList; ++i) {
207 QStringList finalList(listOrderAccount);
208 finalList += instanceList;
210 const int numberOfElement(finalList.count());
211 for (
int i = 0; i < numberOfElement; ++i) {
212 const QString identifier(finalList.at(i));
213 const InstanceStruct tmp = mapInstance.
value(identifier);
215 auto item =
new QListWidgetItem(tmp.icon, tmp.name, d->mListAccount);
216 item->
setData(AccountConfigOrderDialog::IdentifierAccount, identifier);
217 d->mListAccount->addItem(item);
220 const bool enabled = d->mSettings->enableAccountOrder();
221 d->mEnableAccountOrder->setChecked(
enabled);
222 slotEnableAccountOrder(
enabled);
225void AccountConfigOrderDialog::slotOk()
228 const int numberOfItems(d->mListAccount->count());
230 for (
int i = 0; i < numberOfItems; ++i) {
231 order << d->mListAccount->item(i)->
data(AccountConfigOrderDialog::IdentifierAccount).toString();
234 d->mSettings->setOrder(order);
235 d->mSettings->setEnableAccountOrder(d->mEnableAccountOrder->isChecked());
236 d->mSettings->save();
240void AccountConfigOrderDialog::readConfig()
249void AccountConfigOrderDialog::writeConfig()
256#include "moc_accountconfigorderdialog.cpp"
QList< AgentInstance > List
static AgentManager * self()
AgentInstance::List instances() const
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
void addLayout(QLayout *layout, int stretch)
QIcon fromTheme(const QString &name)
void setContentsMargins(const QMargins &margins)
const_reference at(qsizetype i) const const
qsizetype count() const const
bool removeOne(const AT &t)
void reserve(qsizetype size)
iterator insert(const Key &key, const T &value)
T value(const Key &key, const T &defaultValue) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
void sort(Qt::CaseSensitivity cs)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void resize(const QSize &newSize)