SVN_SILENT made messages (after extraction)
[kdepim.git] / kmail / foldershortcutactionmanager.cpp
blob777c0bb0d8f358332283668dafe94af1bffc3e0a
1 /* Copyright 2010 Thomas McGuire <mcguire@kde.org>
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License as
5 published by the Free Software Foundation; either version 2 of
6 the License or (at your option) version 3 or any later version
7 accepted by the membership of KDE e.V. (or its successor approved
8 by the membership of KDE e.V.), which shall act as a proxy
9 defined in Section 14 of version 3 of the license.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "foldershortcutactionmanager.h"
21 #include "MailCommon/FolderCollection"
22 #include "mailcommon/mailkernel.h"
24 #include <AkonadiCore/ChangeRecorder>
25 #include <AkonadiCore/EntityDisplayAttribute>
26 #include <AkonadiCore/EntityTreeModel>
27 #include <AkonadiCore/EntityMimeTypeFilterModel>
29 #include <QAction>
30 #include <KActionCollection>
31 #include <KLocalizedString>
32 #include <QIcon>
34 using namespace KMail;
35 using namespace MailCommon;
37 FolderShortcutCommand::FolderShortcutCommand(QWidget *mainwidget,
38 const Akonadi::Collection &col)
39 : QObject(mainwidget),
40 mCollectionFolder(col),
41 mMainWidget(mainwidget),
42 mAction(Q_NULLPTR)
44 connect(this, SIGNAL(selectCollectionFolder(Akonadi::Collection)), mMainWidget, SLOT(slotSelectCollectionFolder(Akonadi::Collection)));
47 FolderShortcutCommand::~FolderShortcutCommand()
49 if (mAction && mAction->parentWidget()) {
50 mAction->parentWidget()->removeAction(mAction);
52 delete mAction;
55 void FolderShortcutCommand::start()
57 Q_EMIT selectCollectionFolder(mCollectionFolder);
60 void FolderShortcutCommand::setAction(QAction *action)
62 mAction = action;
65 FolderShortcutActionManager::FolderShortcutActionManager(QWidget *parent,
66 KActionCollection *actionCollection
68 : QObject(parent),
69 mActionCollection(actionCollection),
70 mParent(parent)
74 void FolderShortcutActionManager::createActions()
76 // When this function is called, the ETM has not finished loading yet. Therefore, when new
77 // rows are inserted in the ETM, see if we have new collections that we can assign shortcuts
78 // to.
79 const QAbstractItemModel *model = KernelIf->collectionModel();
80 connect(model, &QAbstractItemModel::rowsInserted,
81 this, &FolderShortcutActionManager::slotRowsInserted, Qt::UniqueConnection);
82 connect(KernelIf->folderCollectionMonitor(), &Akonadi::Monitor::collectionRemoved,
83 this, &FolderShortcutActionManager::slotCollectionRemoved, Qt::UniqueConnection);
85 if (model->rowCount() > 0) {
86 updateShortcutsForIndex(QModelIndex(), 0, model->rowCount() - 1);
90 void FolderShortcutActionManager::slotRowsInserted(const QModelIndex &parent, int start, int end)
92 updateShortcutsForIndex(parent, start, end);
95 void FolderShortcutActionManager::updateShortcutsForIndex(const QModelIndex &parent,
96 int start, int end)
98 QAbstractItemModel *model = KernelIf->collectionModel();
99 for (int i = start; i <= end; ++i) {
100 if (model->hasIndex(i, 0, parent)) {
101 const QModelIndex child = model->index(i, 0, parent);
102 Akonadi::Collection collection =
103 model->data(child, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
104 if (collection.isValid()) {
105 shortcutChanged(collection);
107 if (model->rowCount(child) > 0) {
108 updateShortcutsForIndex(child, 0, model->rowCount(child) - 1);
114 void FolderShortcutActionManager::slotCollectionRemoved(const Akonadi::Collection &col)
116 delete mFolderShortcutCommands.take(col.id());
119 void FolderShortcutActionManager::shortcutChanged(const Akonadi::Collection &col)
121 // remove the old one, no autodelete in Qt4
122 slotCollectionRemoved(col);
123 const QSharedPointer<FolderCollection> folderCollection(FolderCollection::forCollection(col, false));
124 const QKeySequence shortcut(folderCollection->shortcut());
125 if (shortcut.isEmpty()) {
126 return;
129 FolderShortcutCommand *command = new FolderShortcutCommand(mParent, col);
130 mFolderShortcutCommands.insert(col.id(), command);
132 QIcon icon(QStringLiteral("folder"));
133 if (col.hasAttribute<Akonadi::EntityDisplayAttribute>() &&
134 !col.attribute<Akonadi::EntityDisplayAttribute>()->iconName().isEmpty()) {
135 icon = QIcon(col.attribute<Akonadi::EntityDisplayAttribute>()->iconName());
138 const QString actionLabel = i18n("Folder Shortcut %1", col.name());
139 QString actionName = i18n("Folder Shortcut %1", QString::number(col.id()));
140 actionName.replace(QLatin1Char(' '), QLatin1Char('_'));
141 QAction *action = mActionCollection->addAction(actionName);
142 // The folder shortcut is set in the folder shortcut dialog.
143 // The shortcut set in the shortcut dialog would not be saved back to
144 // the folder settings correctly.
145 mActionCollection->setShortcutsConfigurable(action, false);
146 action->setText(actionLabel);
147 action->setShortcut(shortcut);
148 action->setIcon(icon);
150 connect(action, &QAction::triggered, command, &FolderShortcutCommand::start);
151 command->setAction(action); // will be deleted along with the command