From 9b9d4cb25199c34d1789e69d73e23b8793b00973 Mon Sep 17 00:00:00 2001 From: Thomas McGuire Date: Sun, 4 Apr 2010 20:35:13 +0000 Subject: [PATCH] Factor out folder shortcut action handing into its own class. This is similar to the tag action manager. Less stuff in kmmainwidget is always good. svn path=/trunk/KDE/kdepim/kmail/; revision=1111090 --- CMakeLists.txt | 1 + foldercollection.cpp | 6 ++- foldershortcutactionmanager.cpp | 91 +++++++++++++++++++++++++++++++++ foldershortcutactionmanager.h | 71 +++++++++++++++++++++++++ kmail_part.cpp | 3 +- kmmainwidget.cpp | 58 ++------------------- kmmainwidget.h | 22 +++----- 7 files changed, 182 insertions(+), 70 deletions(-) create mode 100644 foldershortcutactionmanager.cpp create mode 100644 foldershortcutactionmanager.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a5233d815..4c2964474 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,6 +148,7 @@ set(kmailprivate_LIB_SRCS searchdescriptionattribute.cpp tag.cpp tagactionmanager.cpp + foldershortcutactionmanager.cpp ) soprano_add_ontology(kmailprivate_LIB_SRCS diff --git a/foldercollection.cpp b/foldercollection.cpp index 97d2897fe..321ed2d82 100644 --- a/foldercollection.cpp +++ b/foldercollection.cpp @@ -31,6 +31,7 @@ #include "kmcommands.h" #include "expirejob.h" #include "compactionjob.h" +#include "foldershortcutactionmanager.h" #include #include @@ -249,8 +250,9 @@ void FolderCollection::setShortcut( const KShortcut &sc, KMMainWidget *main ) { if ( mShortcut != sc ) { mShortcut = sc; - if( main) - main->shortcutChanged( mCollection ); + if ( main) { + main->folderShortcutActionManager()->shortcutChanged( mCollection ); + } } } diff --git a/foldershortcutactionmanager.cpp b/foldershortcutactionmanager.cpp new file mode 100644 index 000000000..c1533499e --- /dev/null +++ b/foldershortcutactionmanager.cpp @@ -0,0 +1,91 @@ +/* Copyright 2010 Thomas McGuire + + 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) version 3 or any later version + accepted by the membership of KDE e.V. (or its successor approved + by the membership of KDE e.V.), which shall act as a proxy + defined in Section 14 of version 3 of the license. + + 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. If not, see . +*/ +#include "foldershortcutactionmanager.h" + +#include "foldercollection.h" +#include "kmcommands.h" +#include "kmkernel.h" +#include "kmmainwidget.h" + +#include +#include + +#include +#include + +using namespace KMail; + +FolderShortcutActionManager::FolderShortcutActionManager( KMMainWidget *parent, + KActionCollection *actionCollection ) + : QObject( parent ), + mActionCollection( actionCollection ), + mParent( parent ) +{ + connect( KMKernel::self()->monitor(), SIGNAL( collectionRemoved( const Akonadi::Collection & ) ), + this, SLOT( slotCollectionRemoved( const Akonadi::Collection& ) ) ); +} + +void FolderShortcutActionManager::createActions() +{ + QList folders = kmkernel->allFoldersCollection(); + for ( int i = 0 ; i < folders.size(); ++i ) { + Akonadi::Collection col = folders.at( i ); + shortcutChanged( col ); + } +} + +void FolderShortcutActionManager::slotCollectionRemoved( const Akonadi::Collection &col ) +{ + delete mFolderShortcutCommands.take( col.id() ); +} + +void FolderShortcutActionManager::shortcutChanged( const Akonadi::Collection &col ) +{ + // remove the old one, no autodelete in Qt4 + slotCollectionRemoved( col ); + QSharedPointer folderCollection( FolderCollection::forCollection( col ) ); + if ( folderCollection->shortcut().isEmpty() ) + return; + + FolderShortcutCommand *command = new FolderShortcutCommand( mParent, col ); + mFolderShortcutCommands.insert( col.id(), command ); + + KIcon icon( "folder" ); + if ( col.hasAttribute() && + !col.attribute()->iconName().isEmpty() ) { + icon = KIcon( col.attribute()->iconName() ); + } + + const QString actionLabel = i18n( "Folder Shortcut %1", col.name() ); + QString actionName = i18n( "Folder Shortcut %1", folderCollection->idString() ); + actionName.replace( ' ', '_' ); + KAction *action = mActionCollection->addAction( actionName ); + // The folder shortcut is set in the folder shortcut dialog. + // The shortcut set in the shortcut dialog would not be saved back to + // the folder settings correctly. + action->setShortcutConfigurable( false ); + action->setText( actionLabel ); + action->setShortcuts( folderCollection->shortcut() ); + action->setIcon( icon ); + + connect( action, SIGNAL( triggered( bool ) ), command, SLOT( start() ) ); + command->setAction( action ); // will be deleted along with the command +} + +#include "foldershortcutactionmanager.moc" diff --git a/foldershortcutactionmanager.h b/foldershortcutactionmanager.h new file mode 100644 index 000000000..03dcfed51 --- /dev/null +++ b/foldershortcutactionmanager.h @@ -0,0 +1,71 @@ +/* Copyright 2010 Thomas McGuire + + 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) version 3 or any later version + accepted by the membership of KDE e.V. (or its successor approved + by the membership of KDE e.V.), which shall act as a proxy + defined in Section 14 of version 3 of the license. + + 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. If not, see . +*/ +#ifndef FOLDERSHORTCUTACTIONMANAGER_H +#define FOLDERSHORTCUTACTIONMANAGER_H + +#include "kmail_export.h" + +#include + +#include +#include + +namespace Akonadi { + class Collection; +} + +class FolderShortcutCommand; +class KActionCollection; +class KMMainWidget; + +namespace KMail { + + class KMAIL_EXPORT FolderShortcutActionManager : public QObject + { + Q_OBJECT + + public: + + FolderShortcutActionManager( KMMainWidget *parent, KActionCollection *actionCollection ); + void createActions(); + + public slots: + + /** + * Updates the shortcut action for this collection. Call this when a shortcut was + * added, removed or changed. + */ + void shortcutChanged( const Akonadi::Collection &collection ); + + private slots: + + /** + * Removes the shortcut actions associated with a folder. + */ + void slotCollectionRemoved( const Akonadi::Collection &collection ); + + private: + + QMap< Akonadi::Entity::Id, FolderShortcutCommand* > mFolderShortcutCommands; + KActionCollection *mActionCollection; + KMMainWidget *mParent; + }; +} + +#endif diff --git a/kmail_part.cpp b/kmail_part.cpp index 06c7b71ce..9935f6cea 100644 --- a/kmail_part.cpp +++ b/kmail_part.cpp @@ -48,6 +48,7 @@ #include "folderselectiontreeview.h" #include "foldertreeview.h" #include "tagactionmanager.h" +#include "foldershortcutactionmanager.h" #include #include @@ -180,7 +181,7 @@ void KMailPart::guiActivateEvent(KParts::GUIActivateEvent *e) KParts::ReadOnlyPart::guiActivateEvent(e); mainWidget->initializeFilterActions(); mainWidget->tagActionManager()->createActions(); - mainWidget->initializeFolderShortcutActions(); + mainWidget->folderShortcutActionManager()->createActions(); mainWidget->updateVactionScriptStatus(); } diff --git a/kmmainwidget.cpp b/kmmainwidget.cpp index 6c987683b..82f6fabe6 100644 --- a/kmmainwidget.cpp +++ b/kmmainwidget.cpp @@ -57,6 +57,7 @@ #include "globalsettings.h" #include "foldertreeview.h" #include "tagactionmanager.h" +#include "foldershortcutactionmanager.h" #if !defined(NDEBUG) #include "sievedebugdialog.h" using KMail::SieveDebugDialog; @@ -848,8 +849,8 @@ void KMMainWidget::createWidgets() opt |= FolderSelectionTreeView::UseLineEditForFiltering; mCollectionFolderView = new FolderSelectionTreeView( this, mGUIClient, opt ); - connect( mCollectionFolderView->folderTreeView(), SIGNAL( currentChanged( const Akonadi::Collection &) ), this, SLOT( slotFolderChanged( const Akonadi::Collection& ) ) ); - connect( KMKernel::self()->monitor(), SIGNAL( collectionRemoved( const Akonadi::Collection &) ), this, SLOT( slotCollectionRemoved( const Akonadi::Collection& ) ) ); + connect( mCollectionFolderView->folderTreeView(), SIGNAL( currentChanged( const Akonadi::Collection & ) ), + this, SLOT( slotFolderChanged( const Akonadi::Collection& ) ) ); mCollectionFolderView->setSelectionMode( QAbstractItemView::ExtendedSelection ); const KConfigGroup cfg( KMKernel::config(), "CollectionFolderView" ); @@ -3488,6 +3489,7 @@ void KMMainWidget::setupActions() updateFolderMenu(); mTagActionManager = new KMail::TagActionManager( this, actionCollection(), mMsgActions, mGUIClient ); + mFolderShortcutActionManager = new KMail::FolderShortcutActionManager( this, actionCollection() ); } //----------------------------------------------------------------------------- @@ -3834,7 +3836,7 @@ void KMMainWidget::slotShowStartupFolder() // the main window or Kontact calls createGUI(). // This function however is called with a single shot timer. initializeFilterActions(); - initializeFolderShortcutActions(); + mFolderShortcutActionManager->createActions(); mTagActionManager->createActions(); messageActions()->setupForwardingActionsList( mGUIClient ); @@ -3964,16 +3966,6 @@ void KMMainWidget::clearFilterActions() mFilterCommands.clear(); } -//----------------------------------------------------------------------------- -void KMMainWidget::initializeFolderShortcutActions() -{ - QList folders = kmkernel->allFoldersCollection(); - for ( int i = 0 ; i < folders.size(); ++i ) { - Akonadi::Collection col = folders.at( i ); - shortcutChanged( col ); - } -} - //----------------------------------------------------------------------------- void KMMainWidget::initializeFilterActions() { @@ -4032,11 +4024,6 @@ void KMMainWidget::initializeFilterActions() updateMessageActions(); } -void KMMainWidget::slotCollectionRemoved( const Akonadi::Collection& col) -{ - delete mFolderShortcutCommands.take( col.id() ); -} - //----------------------------------------------------------------------------- void KMMainWidget::initializeIMAPActions( bool setState /* false the first time, true later on */ ) { @@ -4071,41 +4058,6 @@ QList KMMainWidget::actionList() return actionCollection()->actions(); } -void KMMainWidget::shortcutChanged( const Akonadi::Collection & col ) -{ - // remove the old one, no autodelete in Qt4 - slotCollectionRemoved( col ); - QSharedPointer fd( FolderCollection::forCollection( col ) ); - if ( fd->shortcut().isEmpty() ) - return; - - FolderShortcutCommand *c = new FolderShortcutCommand( this, col ); - mFolderShortcutCommands.insert( col.id(), c ); - - QString actionlabel = i18n( "Folder Shortcut %1", col.name() ); - QString actionname = i18n( "Folder Shortcut %1", fd->idString() ); - QString normalizedName = actionname.replace(' ', '_'); - KAction *action = actionCollection()->addAction( normalizedName ); - // The folder shortcut is set in the folder shortcut dialog. - // The shortcut set in the shortcut dialog would not be saved back to - // the folder settings correctly. - action->setShortcutConfigurable( false ); -#ifdef OLD_FOLDERVIEW - mMainFolderView->addAction( action ); // <-- FIXME: why this is added to the folder view ? -#endif - action->setText( actionlabel ); - connect( action, SIGNAL( triggered(bool) ), c, SLOT( start() ) ); - action->setShortcuts( fd->shortcut() ); - - KIcon icon( "folder" ); - if ( col.hasAttribute() && - !col.attribute()->iconName().isEmpty() ) { - icon = KIcon( col.attribute()->iconName() ); - } - action->setIcon( icon ); - c->setAction( action ); // will be deleted along with the command -} - //----------------------------------------------------------------------------- QString KMMainWidget::findCurrentImapPath() { diff --git a/kmmainwidget.h b/kmmainwidget.h index 87f78bd56..3a3481a50 100644 --- a/kmmainwidget.h +++ b/kmmainwidget.h @@ -61,7 +61,6 @@ class KActionMenu; class KToggleAction; class FolderTreeView; class KMMetaFilterActionCommand; -class FolderShortcutCommand; class KMSystemTray; class CustomTemplatesMenu; @@ -80,6 +79,7 @@ namespace KMail { class FavoriteFolderView; class StatusBarLabel; class TagActionManager; + class FolderShortcutActionManager; } class FolderSelectionTreeView; @@ -184,11 +184,13 @@ class KMAIL_EXPORT KMMainWidget : public QWidget /** Returns the XML GUI client. */ KXMLGUIClient* guiClient() const { return mGUIClient; } - KMail::TagActionManager *tagActionManager() const { return mTagActionManager; } - - /** Add, remove or adjust the folder's shortcut. */ - void shortcutChanged( const Akonadi::Collection & ); + KMail::TagActionManager *tagActionManager() const { + return mTagActionManager; + } + KMail::FolderShortcutActionManager *folderShortcutActionManager() const { + return mFolderShortcutActionManager; + } public slots: // Moving messages around @@ -261,9 +263,6 @@ class KMAIL_EXPORT KMMainWidget : public QWidget /** Create IMAP-account-related actions if applicable */ void initializeIMAPActions() { initializeIMAPActions( true ); } - /** Create actions for the folder shortcuts. */ - void initializeFolderShortcutActions(); - /** Trigger the dialog for editing out-of-office scripts. */ void slotEditVacation(); @@ -441,11 +440,6 @@ class KMAIL_EXPORT KMMainWidget : public QWidget void slotConfigChanged(); - /** - Remove the shortcut actions associated with a folder. - */ - void slotCollectionRemoved( const Akonadi::Collection& ); - /** Show a splash screen for the longer-lasting operation */ void slotShowBusySplash(); @@ -617,9 +611,9 @@ private: QList mFilterMenuActions; QList mFilterTBarActions; QList mFilterCommands; - QHash mFolderShortcutCommands; KMail::TagActionManager *mTagActionManager; + KMail::FolderShortcutActionManager *mFolderShortcutActionManager; KMSystemTray *mSystemTray; KSharedConfig::Ptr mConfig; KXMLGUIClient *mGUIClient;