24 #include "mailcommon/tag/addtagdialog.h"
27 #include <KActionCollection>
28 #include <KToggleAction>
29 #include <KXMLGUIClient>
30 #include <KActionMenu>
32 #include <KLocalizedString>
34 #include <Akonadi/Monitor>
36 #include <QSignalMapper>
39 #include <Akonadi/TagFetchJob>
40 #include <Akonadi/TagFetchScope>
41 #include <Akonadi/TagAttribute>
43 using namespace KMail;
50 mActionCollection( actionCollection ),
51 mMessageActions( messageActions ),
52 mMessageTagToggleMapper( 0 ),
53 mGUIClient( guiClient ),
54 mSeparatorMoreAction( 0 ),
55 mSeparatorNewTagAction( 0 ),
59 mTagFetchInProgress( false ),
60 mMonitor(new Akonadi::Monitor(this))
64 mMonitor->setTypeMonitored(Akonadi::Monitor::Tags);
65 mMonitor->tagFetchScope().fetchAttribute<Akonadi::TagAttribute>();
66 connect(mMonitor, SIGNAL(tagAdded(Akonadi::Tag)),
this, SLOT(onTagAdded(Akonadi::Tag)));
67 connect(mMonitor, SIGNAL(tagRemoved(Akonadi::Tag)),
this, SLOT(onTagRemoved(Akonadi::Tag)));
68 connect(mMonitor, SIGNAL(tagChanged(Akonadi::Tag)),
this, SLOT(onTagChanged(Akonadi::Tag)));
78 if ( !mToolbarActions.
isEmpty() ) {
79 if ( mGUIClient->factory() ) {
80 mGUIClient->unplugActionList(
QLatin1String(
"toolbar_messagetag_actions") );
82 mToolbarActions.
clear();
87 foreach( KAction *action, mTagActions ) {
91 mActionCollection->removeAction( action );
94 if ( mSeparatorMoreAction ) {
98 if ( mSeparatorNewTagAction ) {
102 if ( mNewTagAction ) {
112 delete mMessageTagToggleMapper;
113 mMessageTagToggleMapper = 0;
116 void TagActionManager::createTagAction(
const MailCommon::Tag::Ptr &tag,
bool addToMenu )
118 QString cleanName( i18n(
"Message Tag: %1", tag->tagName ) );
120 KToggleAction *
const tagAction =
new KToggleAction( KIcon( tag->iconName ),
122 tagAction->setShortcut( tag->shortcut );
123 tagAction->setIconText( tag->name() );
124 tagAction->setChecked( tag->id() == mNewTagId );
126 mActionCollection->addAction( tag->name(), tagAction );
127 connect( tagAction, SIGNAL(triggered(
bool)),
128 mMessageTagToggleMapper, SLOT(map()) );
133 tagAction->setShortcutConfigurable(
false );
136 mTagActions.
insert( tag->id(), tagAction );
140 if ( tag->inToolbar ) {
141 mToolbarActions.
append( tagAction );
147 if ( mTagFetchInProgress )
151 mTagFetchInProgress =
true;
152 Akonadi::TagFetchJob *fetchJob =
new Akonadi::TagFetchJob(
this);
153 fetchJob->fetchScope().fetchAttribute<Akonadi::TagAttribute>();
154 connect(fetchJob, SIGNAL(result(
KJob*)),
this, SLOT(finishedTagListing(
KJob*)));
156 mTagFetchInProgress =
false;
157 createTagActions(mTags);
161 void TagActionManager::finishedTagListing(
KJob *job)
164 kWarning() << job->errorString();
166 Akonadi::TagFetchJob *fetchJob =
static_cast<Akonadi::TagFetchJob*
>(job);
167 foreach (
const Akonadi::Tag &result, fetchJob->tags()) {
168 mTags.
append( MailCommon::Tag::fromAkonadi( result ) );
170 mTagFetchInProgress =
false;
171 qSort( mTags.
begin(), mTags.
end(), MailCommon::Tag::compare );
172 createTagActions(mTags);
175 void TagActionManager::onSignalMapped(
const QString& tag)
186 this, SLOT(onSignalMapped(
QString)) );
190 bool needToAddMoreAction =
false;
191 const int numberOfTag(tags.
size());
193 foreach(
const MailCommon::Tag::Ptr &tag, tags ) {
195 createTagAction( tag,
true );
198 if ( tag->inToolbar || !tag->shortcut.isEmpty() ) {
199 createTagAction( tag,
false );
204 needToAddMoreAction =
true;
211 if(!mSeparatorNewTagAction) {
212 mSeparatorNewTagAction =
new QAction(
this );
215 mMessageActions->
messageStatusMenu()->menu()->addAction( mSeparatorNewTagAction );
217 if (!mNewTagAction) {
218 mNewTagAction =
new KAction( i18n(
"Add new tag..." ),
this );
219 connect( mNewTagAction, SIGNAL(triggered(
bool)),
220 this, SLOT(newTagActionClicked()) );
224 if (needToAddMoreAction) {
225 if(!mSeparatorMoreAction) {
226 mSeparatorMoreAction =
new QAction(
this );
232 mMoreAction =
new KAction( i18n(
"More..." ),
this );
233 connect( mMoreAction, SIGNAL(triggered(
bool)),
239 if ( !mToolbarActions.
isEmpty() && mGUIClient->factory() ) {
240 mGUIClient->plugActionList(
QLatin1String(
"toolbar_messagetag_actions"), mToolbarActions );
245 const Akonadi::Item &selectedItem )
250 if ( numberOfSelectedMessages >= 1 ) {
251 Q_ASSERT( selectedItem.isValid() );
252 for ( ; it != end; ++it ) {
255 foreach (
const MailCommon::Tag::Ptr &tag, mTags) {
256 if (tag->id() == it.key()) {
262 it.value()->setEnabled(
true );
263 if (numberOfSelectedMessages == 1) {
264 const bool hasTag = selectedItem.hasTag(Akonadi::Tag(it.key()));
265 it.value()->setChecked( hasTag );
266 it.value()->setText( i18n(
"Message Tag: %1", label ) );
268 it.value()->setChecked(
false );
269 it.value()->setText( i18n(
"Toggle Message Tag: %1", label ) );
273 for ( ; it != end; ++it ) {
274 it.value()->setEnabled(
false );
279 void TagActionManager::onTagAdded(
const Akonadi::Tag &akonadiTag)
284 mTags.
append( MailCommon::Tag::fromAkonadi( akonadiTag ) );
285 qSort( mTags.
begin(), mTags.
end(), MailCommon::Tag::compare );
286 createTagActions(mTags);
288 checkTags( checked );
291 void TagActionManager::onTagRemoved(
const Akonadi::Tag &akonadiTag)
293 foreach(
const MailCommon::Tag::Ptr &tag, mTags ) {
294 if(tag->id() == akonadiTag.id()) {
303 void TagActionManager::onTagChanged(
const Akonadi::Tag& akonadiTag)
305 foreach(
const MailCommon::Tag::Ptr &tag, mTags ) {
306 if(tag->id() == akonadiTag.id()) {
311 mTags.
append( MailCommon::Tag::fromAkonadi( akonadiTag ) );
315 void TagActionManager::fillTagList()
320 qSort( mTags.
begin(), mTags.
end(), MailCommon::Tag::compare );
321 createTagActions( mTags );
323 checkTags( checked );
326 void TagActionManager::newTagActionClicked()
329 dialog->setTags(mTags);
330 if ( dialog->exec() == QDialog::Accepted ) {
331 mNewTagId = dialog->tag().id();
340 foreach(
const qint64 &
id, tags ) {
342 mTagActions[id]->setChecked(
true );
352 for ( ; it != end; ++it ) {
353 if ( it.value()->isChecked() ) {
bool contains(const Key &key) const
void setSeparator(bool b)
const_iterator constBegin() const
void tagActionTriggered(const Akonadi::Tag &tag)
Emitted when one of the tagging actions was triggered.
Manages common actions that can be performed on one or more messages.
void tagMoreActionClicked()
Emitted when we want to select more action.
void setMapping(QObject *sender, int id)
static int s_numberMaxTag
QString number(int n, int base)
void append(const T &value)
void clearActions()
Removes all actions from the GUI again.
int removeAll(const T &value)
const_iterator constEnd() const
void updateActionStates(int numberOfSelectedMessages, const Akonadi::Item &selectedItem)
Updates the state of the toggle actions of all tags.
iterator insert(const Key &key, const T &value)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void createActions()
Creates and plugs all tag actions.
qlonglong toLongLong(bool *ok, int base) const
QFuture< T > mapped(const Sequence &sequence, MapFunction function)
TagActionManager(QObject *parent, KActionCollection *actionCollection, MessageActions *messageActions, KXMLGUIClient *guiClient)
Does not take ownership of the action collection, the GUI client or the message actions.
KActionMenu * messageStatusMenu() const