diff --git a/agents/unifiedmailboxagent/settingsdialog.cpp b/agents/unifiedmailboxagent/settingsdialog.cpp index df03bd192..6b348a6f0 100644 --- a/agents/unifiedmailboxagent/settingsdialog.cpp +++ b/agents/unifiedmailboxagent/settingsdialog.cpp @@ -1,162 +1,161 @@ /* Copyright (C) 2018 Daniel Vrátil This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "settingsdialog.h" #include "unifiedmailboxmanager.h" #include "unifiedmailboxeditor.h" #include "unifiedmailbox.h" #include "mailkernel.h" #include #include #include #include #include #include #include #include #include #include #include namespace { static constexpr const char *DialogGroup = "UnifiedMailboxSettingsDialog"; } SettingsDialog::SettingsDialog(const KSharedConfigPtr &config, UnifiedMailboxManager &boxManager, WId, QWidget *parent) : QDialog(parent) , mBoxManager(boxManager) , mKernel(new MailKernel(config, this)) , mConfig(config) { - auto l = new QVBoxLayout; - setLayout(l); + auto l = new QVBoxLayout(this); auto h = new QHBoxLayout; l->addLayout(h); mBoxModel = new QStandardItemModel(this); auto view = new QListView(this); view->setEditTriggers(QListView::NoEditTriggers); view->setModel(mBoxModel); h->addWidget(view); auto v = new QVBoxLayout; h->addLayout(v); auto addButton = new QPushButton(QIcon::fromTheme(QStringLiteral("list-add-symbolic")), i18n("Add")); v->addWidget(addButton); connect(addButton, &QPushButton::clicked, this, [this]() { auto mailbox = std::make_unique(); auto editor = new UnifiedMailboxEditor(mailbox.get(), mConfig, this); if (editor->exec()) { mailbox->setId(mailbox->name()); // assign ID addBox(mailbox.get()); mBoxManager.insertBox(std::move(mailbox)); } }); auto editButton = new QPushButton(QIcon::fromTheme(QStringLiteral("entry-edit")), i18n("Modify")); editButton->setEnabled(false); v->addWidget(editButton); connect(editButton, &QPushButton::clicked, this, [this, view]() { const auto indexes = view->selectionModel()->selectedIndexes(); if (!indexes.isEmpty()) { auto item = mBoxModel->itemFromIndex(indexes[0]); auto mailbox = item->data().value(); auto editor = new UnifiedMailboxEditor(mailbox, mConfig, this); if (editor->exec()) { item->setText(mailbox->name()); item->setIcon(QIcon::fromTheme(mailbox->icon())); } } }); auto removeButton = new QPushButton(QIcon::fromTheme(QStringLiteral("list-remove-symbolic")), i18n("Remove")); removeButton->setEnabled(false); v->addWidget(removeButton); connect(removeButton, &QPushButton::clicked, this, [this, view]() { const auto indexes = view->selectionModel()->selectedIndexes(); if (!indexes.isEmpty()) { auto item = mBoxModel->itemFromIndex(indexes[0]); const auto mailbox = item->data().value(); if (KMessageBox::warningYesNo( this, i18n("Do you really want to remove unified mailbox %1?", mailbox->name()), i18n("Really Remove?"), KStandardGuiItem::remove(), KStandardGuiItem::cancel()) == KMessageBox::Yes) { mBoxModel->removeRow(item->row()); mBoxManager.removeBox(mailbox->id()); } } }); v->addStretch(1); connect(view->selectionModel(), &QItemSelectionModel::selectionChanged, this, [view, editButton, removeButton]() { const bool hasSelection = view->selectionModel()->hasSelection(); editButton->setEnabled(hasSelection); removeButton->setEnabled(hasSelection); }); auto box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); connect(box, &QDialogButtonBox::accepted, this, &SettingsDialog::accept); connect(box, &QDialogButtonBox::rejected, this, &SettingsDialog::reject); l->addWidget(box); loadBoxes(); const auto dlgGroup = config->group(DialogGroup); if (dlgGroup.hasKey("geometry")) { restoreGeometry(dlgGroup.readEntry("geometry", QByteArray())); } else { resize(500, 500); } } SettingsDialog::~SettingsDialog() { auto dlgGroup = mConfig->group(DialogGroup); dlgGroup.writeEntry("geometry", saveGeometry()); } void SettingsDialog::accept() { mBoxManager.saveBoxes(); QDialog::accept(); } void SettingsDialog::loadBoxes() { mBoxModel->clear(); for (const auto &mailboxIt : mBoxManager) { addBox(mailboxIt.second.get()); } } void SettingsDialog::addBox(UnifiedMailbox *box) { auto item = new QStandardItem(QIcon::fromTheme(box->icon()), box->name()); item->setData(QVariant::fromValue(box)); mBoxModel->appendRow(item); } diff --git a/agents/unifiedmailboxagent/unifiedmailboxeditor.cpp b/agents/unifiedmailboxagent/unifiedmailboxeditor.cpp index 6d99cbeaf..ffe181298 100644 --- a/agents/unifiedmailboxagent/unifiedmailboxeditor.cpp +++ b/agents/unifiedmailboxagent/unifiedmailboxeditor.cpp @@ -1,184 +1,183 @@ /* Copyright (C) 2018 Daniel Vrátil This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "unifiedmailboxeditor.h" #include "unifiedmailbox.h" #include "mailkernel.h" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { static constexpr const char *EditorGroup = "UnifiedMailboxEditorDialog"; class SelfFilterProxyModel : public QSortFilterProxyModel { Q_OBJECT public: explicit SelfFilterProxyModel(QObject *parent = nullptr) : QSortFilterProxyModel(parent) {} QVariant data(const QModelIndex &index, int role) const override { if (role == Qt::CheckStateRole) { // Make top-level collections uncheckable const Akonadi::Collection col = data(index, Akonadi::EntityTreeModel::CollectionRole).value(); if (col.parentCollection() == Akonadi::Collection::root()) { return {}; } } return QSortFilterProxyModel::data(index, role); } Qt::ItemFlags flags(const QModelIndex &index) const override { // Make top-level collections uncheckable const Akonadi::Collection col = data(index, Akonadi::EntityTreeModel::CollectionRole).value(); if (col.parentCollection() == Akonadi::Collection::root()) { return QSortFilterProxyModel::flags(index) & ~Qt::ItemIsUserCheckable; } else { return QSortFilterProxyModel::flags(index); } } bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override { // Hide ourselves const auto sourceIndex = sourceModel()->index(source_row, 0, source_parent); const Akonadi::Collection col = sourceModel()->data(sourceIndex, Akonadi::EntityTreeModel::CollectionRole).value(); return !UnifiedMailboxManager::isUnifiedMailbox(col); } }; } UnifiedMailboxEditor::UnifiedMailboxEditor(const KSharedConfigPtr &config, QWidget* parent) : UnifiedMailboxEditor({}, config, parent) { } UnifiedMailboxEditor::UnifiedMailboxEditor(UnifiedMailbox *mailbox, const KSharedConfigPtr &config, QWidget *parent) : QDialog(parent) , mMailbox(mailbox) , mConfig(config) { - auto l = new QVBoxLayout; - setLayout(l); + auto l = new QVBoxLayout(this); auto f = new QFormLayout; l->addLayout(f); auto nameEdit = new QLineEdit(mMailbox->name()); f->addRow(i18n("Name:"), nameEdit); connect(nameEdit, &QLineEdit::textChanged, this, [this](const QString &name) { - mMailbox->setName(name); + mMailbox->setName(name.trimmed()); }); auto iconButton = new QPushButton(QIcon::fromTheme(mMailbox->icon(), QIcon::fromTheme(QStringLiteral("folder-mail"))), i18n("Pick icon...")); f->addRow(i18n("Icon:"), iconButton); connect(iconButton, &QPushButton::clicked, this, [iconButton, this]() { const auto iconName = KIconDialog::getIcon(); if (!iconName.isEmpty()) { mMailbox->setIcon(iconName); iconButton->setIcon(QIcon::fromTheme(iconName)); } }); mMailbox->setIcon(iconButton->icon().name()); l->addSpacing(10); auto ftw = new MailCommon::FolderTreeWidget(nullptr, nullptr, MailCommon::FolderTreeWidget::TreeViewOptions(MailCommon::FolderTreeWidget::UseDistinctSelectionModel | MailCommon::FolderTreeWidget::HideStatistics)); l->addWidget(ftw); auto ftv = ftw->folderTreeView(); auto sourceModel = ftv->model(); auto selectionModel = ftw->selectionModel(); auto checkable = new KCheckableProxyModel(this); checkable->setSourceModel(sourceModel); checkable->setSelectionModel(selectionModel); const auto sources = mMailbox->sourceCollections(); for (const auto source : sources) { const auto index = Akonadi::EntityTreeModel::modelIndexForCollection(selectionModel->model(), Akonadi::Collection(source)); selectionModel->select(index, QItemSelectionModel::Select); } connect(checkable->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this](const QItemSelection &selected, const QItemSelection &deselected) { auto indexes = selected.indexes(); for (const auto &index : indexes) { mMailbox->addSourceCollection(index.data(Akonadi::EntityTreeModel::CollectionIdRole).toLongLong()); } indexes = deselected.indexes(); for (const auto &index : indexes) { mMailbox->removeSourceCollection(index.data(Akonadi::EntityTreeModel::CollectionIdRole).toLongLong()); } }); auto selfFilter = new SelfFilterProxyModel(this); selfFilter->setSourceModel(checkable); ftv->setModel(selfFilter); ftv->expandAll(); auto box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); connect(box, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(box, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(nameEdit, &QLineEdit::textChanged, box, [box](const QString &name) { - box->button(QDialogButtonBox::Ok)->setEnabled(!name.isEmpty()); + box->button(QDialogButtonBox::Ok)->setEnabled(!name.trimmed().isEmpty()); }); - box->button(QDialogButtonBox::Ok)->setEnabled(!nameEdit->text().isEmpty()); + box->button(QDialogButtonBox::Ok)->setEnabled(!nameEdit->text().trimmed().isEmpty()); l->addWidget(box); const auto editorGroup = config->group(EditorGroup); if (editorGroup.hasKey("geometry")) { restoreGeometry(editorGroup.readEntry("geometry", QByteArray())); } else { resize(500, 900); } } UnifiedMailboxEditor::~UnifiedMailboxEditor() { auto editorGrp = mConfig->group(EditorGroup); editorGrp.writeEntry("geometry", saveGeometry()); } #include "unifiedmailboxeditor.moc"