File indexing completed on 2024-05-12 05:20:37
0001 /* SPDX-FileCopyrightText: 2010 Thomas McGuire <mcguire@kde.org> 0002 0003 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 */ 0005 #include "foldershortcutactionmanager.h" 0006 0007 #include <MailCommon/FolderSettings> 0008 #include <MailCommon/MailKernel> 0009 0010 #include <Akonadi/ChangeRecorder> 0011 #include <Akonadi/EntityDisplayAttribute> 0012 #include <Akonadi/EntityMimeTypeFilterModel> 0013 #include <Akonadi/EntityTreeModel> 0014 0015 #include <KActionCollection> 0016 #include <KLocalizedString> 0017 #include <QAction> 0018 #include <QIcon> 0019 0020 using namespace KMail; 0021 using namespace MailCommon; 0022 0023 FolderShortcutCommand::FolderShortcutCommand(QWidget *mainwidget, const Akonadi::Collection &col) 0024 : QObject(mainwidget) 0025 , mCollectionFolder(col) 0026 , mMainWidget(mainwidget) 0027 { 0028 connect(this, SIGNAL(selectCollectionFolder(Akonadi::Collection)), mMainWidget, SLOT(slotSelectCollectionFolder(Akonadi::Collection))); 0029 } 0030 0031 FolderShortcutCommand::~FolderShortcutCommand() 0032 { 0033 if (mAction) { 0034 auto action = qobject_cast<QWidget *>(mAction->parent()); 0035 if (action) { 0036 action->removeAction(mAction); 0037 } 0038 } 0039 delete mAction; 0040 } 0041 0042 void FolderShortcutCommand::start() 0043 { 0044 Q_EMIT selectCollectionFolder(mCollectionFolder); 0045 } 0046 0047 void FolderShortcutCommand::setAction(QAction *action) 0048 { 0049 mAction = action; 0050 } 0051 0052 FolderShortcutActionManager::FolderShortcutActionManager(QWidget *parent, KActionCollection *actionCollection) 0053 : QObject(parent) 0054 , mActionCollection(actionCollection) 0055 , mParent(parent) 0056 { 0057 } 0058 0059 void FolderShortcutActionManager::createActions() 0060 { 0061 // When this function is called, the ETM has not finished loading yet. Therefore, when new 0062 // rows are inserted in the ETM, see if we have new collections that we can assign shortcuts 0063 // to. 0064 const QAbstractItemModel *model = KernelIf->collectionModel(); 0065 connect(model, &QAbstractItemModel::rowsInserted, this, &FolderShortcutActionManager::slotRowsInserted, Qt::UniqueConnection); 0066 connect(KernelIf->folderCollectionMonitor(), 0067 &Akonadi::Monitor::collectionRemoved, 0068 this, 0069 &FolderShortcutActionManager::slotCollectionRemoved, 0070 Qt::UniqueConnection); 0071 0072 const int rowCount(model->rowCount()); 0073 if (rowCount > 0) { 0074 updateShortcutsForIndex(QModelIndex(), 0, rowCount - 1); 0075 } 0076 } 0077 0078 void FolderShortcutActionManager::slotRowsInserted(const QModelIndex &parent, int start, int end) 0079 { 0080 updateShortcutsForIndex(parent, start, end); 0081 } 0082 0083 void FolderShortcutActionManager::updateShortcutsForIndex(const QModelIndex &parent, int start, int end) 0084 { 0085 QAbstractItemModel *model = KernelIf->collectionModel(); 0086 for (int i = start; i <= end; ++i) { 0087 if (model->hasIndex(i, 0, parent)) { 0088 const QModelIndex child = model->index(i, 0, parent); 0089 auto collection = model->data(child, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>(); 0090 if (collection.isValid()) { 0091 shortcutChanged(collection); 0092 } 0093 if (model->hasChildren(child)) { 0094 updateShortcutsForIndex(child, 0, model->rowCount(child) - 1); 0095 } 0096 } 0097 } 0098 } 0099 0100 void FolderShortcutActionManager::slotCollectionRemoved(const Akonadi::Collection &col) 0101 { 0102 delete mFolderShortcutCommands.take(col.id()); 0103 } 0104 0105 void FolderShortcutActionManager::shortcutChanged(const Akonadi::Collection &col) 0106 { 0107 // remove the old one, no autodelete in Qt4 0108 slotCollectionRemoved(col); 0109 const QSharedPointer<FolderSettings> folderCollection(FolderSettings::forCollection(col, false)); 0110 const QKeySequence shortcut(folderCollection->shortcut()); 0111 if (shortcut.isEmpty()) { 0112 return; 0113 } 0114 0115 auto command = new FolderShortcutCommand(mParent, col); 0116 mFolderShortcutCommands.insert(col.id(), command); 0117 0118 QIcon icon(QStringLiteral("folder")); 0119 if (col.hasAttribute<Akonadi::EntityDisplayAttribute>() && !col.attribute<Akonadi::EntityDisplayAttribute>()->iconName().isEmpty()) { 0120 icon = QIcon(col.attribute<Akonadi::EntityDisplayAttribute>()->iconName()); 0121 } 0122 0123 const QString actionLabel = i18n("Folder Shortcut %1", col.name()); 0124 QString actionName = i18n("Folder Shortcut %1", QString::number(col.id())); 0125 actionName.replace(QLatin1Char(' '), QLatin1Char('_')); 0126 QAction *action = mActionCollection->addAction(actionName); 0127 // The folder shortcut is set in the folder shortcut dialog. 0128 // The shortcut set in the shortcut dialog would not be saved back to 0129 // the folder settings correctly. 0130 mActionCollection->setShortcutsConfigurable(action, false); 0131 action->setText(actionLabel); 0132 mActionCollection->setDefaultShortcut(action, shortcut); 0133 action->setIcon(icon); 0134 0135 connect(action, &QAction::triggered, command, &FolderShortcutCommand::start); 0136 command->setAction(action); // will be deleted along with the command 0137 } 0138 0139 #include "moc_foldershortcutactionmanager.cpp"