newmailnotifieragent.cpp 19.3 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
    Copyright (c) 2013-2015 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    Copyright (c) 2010 Volker Krause <vkrause@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library 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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "newmailnotifieragent.h"

24
#include "newmailnotifierattribute.h"
25
#include "specialnotifierjob.h"
26
#include "newmailnotifieradaptor.h"
Laurent Montel's avatar
Laurent Montel committed
27
#include "newmailnotifieragentsettings.h"
Laurent Montel's avatar
Laurent Montel committed
28
#include "newmailnotifiersettingsdialog.h"
29

30 31
#include <KPIMIdentities/IdentityManager>

32 33
#include <akonadi/dbusconnectionpool.h>

34 35
#include <akonadi/agentfactory.h>
#include <akonadi/changerecorder.h>
Tobias Koenig's avatar
Tobias Koenig committed
36
#include <akonadi/entitydisplayattribute.h>
37
#include <akonadi/entityhiddenattribute.h>
38
#include <akonadi/itemfetchscope.h>
39
#include <akonadi/session.h>
40
#include <Akonadi/AttributeFactory>
41
#include <Akonadi/CollectionFetchScope>
42
#include <akonadi/kmime/specialmailcollections.h>
43
#include <akonadi/kmime/messagestatus.h>
44
#include <Akonadi/AgentManager>
Tobias Koenig's avatar
Tobias Koenig committed
45 46 47
#include <KLocalizedString>
#include <KMime/Message>
#include <KNotification>
48
#include <KNotifyConfigWidget>
49 50
#include <KIconLoader>
#include <KIcon>
51
#include <KConfigGroup>
Laurent Montel's avatar
Laurent Montel committed
52
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
53
#include <KWindowSystem>
54 55
#include <KToolInvocation>

56

57 58
using namespace Akonadi;

Tobias Koenig's avatar
Tobias Koenig committed
59
NewMailNotifierAgent::NewMailNotifierAgent( const QString &id )
Laurent Montel's avatar
Laurent Montel committed
60
    : AgentBase( id )
61
{
62 63 64
    Akonadi::AttributeFactory::registerAttribute<NewMailNotifierAttribute>();
    new NewMailNotifierAdaptor( this );

65 66 67
    mIdentityManager = new KPIMIdentities::IdentityManager( false, this );
    connect(mIdentityManager, SIGNAL(changed()), SLOT(slotIdentitiesChanged()));
    slotIdentitiesChanged();
68
    mDefaultPixmap = KIcon( QLatin1String("kmail") ).pixmap( KIconLoader::SizeMedium, KIconLoader::SizeMedium );
69

70 71 72 73
    DBusConnectionPool::threadConnection().registerObject( QLatin1String( "/NewMailNotifierAgent" ),
                                                           this, QDBusConnection::ExportAdaptors );
    DBusConnectionPool::threadConnection().registerService( QLatin1String( "org.freedesktop.Akonadi.NewMailNotifierAgent" ) );

74 75 76 77
    connect( Akonadi::AgentManager::self(), SIGNAL(instanceStatusChanged(Akonadi::AgentInstance)),
             this, SLOT(slotInstanceStatusChanged(Akonadi::AgentInstance)) );
    connect( Akonadi::AgentManager::self(), SIGNAL(instanceRemoved(Akonadi::AgentInstance)),
             this, SLOT(slotInstanceRemoved(Akonadi::AgentInstance)) );
Laurent Montel's avatar
Laurent Montel committed
78 79
    connect( Akonadi::AgentManager::self(), SIGNAL(instanceAdded(Akonadi::AgentInstance)),
             this, SLOT(slotInstanceAdded(Akonadi::AgentInstance)) );
80 81 82
    connect( Akonadi::AgentManager::self(), SIGNAL(instanceNameChanged(Akonadi::AgentInstance)),
             this, SLOT(slotInstanceNameChanged(Akonadi::AgentInstance)) );

83

Laurent Montel's avatar
Laurent Montel committed
84 85 86 87 88 89 90
    changeRecorder()->setMimeTypeMonitored( KMime::Message::mimeType() );
    changeRecorder()->itemFetchScope().setCacheOnly( true );
    changeRecorder()->itemFetchScope().setFetchModificationTime( false );
    changeRecorder()->fetchCollection( true );
    changeRecorder()->setChangeRecordingEnabled( false );
    changeRecorder()->ignoreSession( Akonadi::Session::defaultSession() );
    changeRecorder()->collectionFetchScope().setAncestorRetrieval( Akonadi::CollectionFetchScope::All );
91
    changeRecorder()->setCollectionMonitored(Collection::root(), true);
Laurent Montel's avatar
Laurent Montel committed
92
    mTimer.setInterval( 5 * 1000 );
Laurent Montel's avatar
Laurent Montel committed
93
    connect( &mTimer, SIGNAL(timeout()), SLOT(slotShowNotifications()) );
94

95 96 97 98 99 100 101 102 103 104 105 106 107
    if (NewMailNotifierAgentSettings::textToSpeakEnabled()) {
        if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.kttsd"))) {
            QString error;
            if (KToolInvocation::startServiceByDesktopName(QLatin1String("kttsd"), QStringList(), &error)) {
                KNotification::event( QLatin1String("text-to-speak-not-found"),
                                      i18n("Starting Jovie Text-to-Speech Service Failed %1", error),
                                      mDefaultPixmap,
                                      0,
                                      KNotification::CloseOnTimeout,
                                      KGlobal::mainComponent());
            }
        }
    }
108
    if (isActive()) {
Laurent Montel's avatar
Laurent Montel committed
109
        mTimer.setSingleShot( true );
110 111 112
    }
}

113 114 115 116 117
void NewMailNotifierAgent::slotIdentitiesChanged()
{
    mListEmails = mIdentityManager->allEmails();
}

118 119 120 121 122 123 124
void NewMailNotifierAgent::doSetOnline(bool online)
{
    if (!online) {
        clearAll();
    }
}

125 126 127 128 129 130 131 132 133 134 135
void NewMailNotifierAgent::setExcludeMyselfFromNotification(bool b)
{
    NewMailNotifierAgentSettings::setExcludeEmailsFromMe(b);
    NewMailNotifierAgentSettings::self()->writeConfig();
}

bool NewMailNotifierAgent::excludeMyselfFromNotification() const
{
    return NewMailNotifierAgentSettings::excludeEmailsFromMe();
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
void NewMailNotifierAgent::setShowPhoto(bool show)
{
    NewMailNotifierAgentSettings::setShowPhoto(show);
    NewMailNotifierAgentSettings::self()->writeConfig();
}

bool NewMailNotifierAgent::showPhoto() const
{
    return NewMailNotifierAgentSettings::showPhoto();
}

void NewMailNotifierAgent::setShowFrom(bool show)
{
    NewMailNotifierAgentSettings::setShowFrom(show);
    NewMailNotifierAgentSettings::self()->writeConfig();
}

bool NewMailNotifierAgent::showFrom() const
{
    return NewMailNotifierAgentSettings::showFrom();
}

void NewMailNotifierAgent::setShowSubject(bool show)
{
    NewMailNotifierAgentSettings::setShowSubject(show);
    NewMailNotifierAgentSettings::self()->writeConfig();
}

bool NewMailNotifierAgent::showSubject() const
{
    return NewMailNotifierAgentSettings::showSubject();
}

void NewMailNotifierAgent::setShowFolderName(bool show)
{
    NewMailNotifierAgentSettings::setShowFolder(show);
    NewMailNotifierAgentSettings::self()->writeConfig();
}

bool NewMailNotifierAgent::showFolderName() const
{
    return NewMailNotifierAgentSettings::showFolder();
}
179

180
void NewMailNotifierAgent::setEnableAgent(bool enabled)
181
{
182
    NewMailNotifierAgentSettings::setEnabled(enabled);
Laurent Montel's avatar
Laurent Montel committed
183
    NewMailNotifierAgentSettings::self()->writeConfig();
184
    if (!enabled) {
Laurent Montel's avatar
Laurent Montel committed
185
        clearAll();
186 187 188
    }
}

189
void NewMailNotifierAgent::setVerboseMailNotification(bool verbose)
190
{
191
    NewMailNotifierAgentSettings::setVerboseNotification(verbose);
Laurent Montel's avatar
Laurent Montel committed
192
    NewMailNotifierAgentSettings::self()->writeConfig();
193 194 195 196
}

bool NewMailNotifierAgent::verboseMailNotification() const
{
Laurent Montel's avatar
Laurent Montel committed
197
    return NewMailNotifierAgentSettings::verboseNotification();
198 199
}

200
void NewMailNotifierAgent::setBeepOnNewMails(bool beep)
201
{
202
    NewMailNotifierAgentSettings::setBeepOnNewMails(beep);
Laurent Montel's avatar
Laurent Montel committed
203
    NewMailNotifierAgentSettings::self()->writeConfig();
204 205 206 207
}

bool NewMailNotifierAgent::beepOnNewMails() const
{
Laurent Montel's avatar
Laurent Montel committed
208
    return NewMailNotifierAgentSettings::beepOnNewMails();
209 210
}

Laurent Montel's avatar
Laurent Montel committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
void NewMailNotifierAgent::setTextToSpeakEnabled(bool enabled)
{
    NewMailNotifierAgentSettings::setTextToSpeakEnabled(enabled);
    NewMailNotifierAgentSettings::self()->writeConfig();
}

bool NewMailNotifierAgent::textToSpeakEnabled() const
{
    return NewMailNotifierAgentSettings::textToSpeakEnabled();
}

void NewMailNotifierAgent::setTextToSpeak(const QString &msg)
{
    NewMailNotifierAgentSettings::setTextToSpeak(msg);
    NewMailNotifierAgentSettings::self()->writeConfig();
}

QString NewMailNotifierAgent::textToSpeak() const
{
    return NewMailNotifierAgentSettings::textToSpeak();
}

233 234 235 236 237
void NewMailNotifierAgent::clearAll()
{
    mNewMails.clear();
    mInstanceNameInProgress.clear();
}
238

239
bool NewMailNotifierAgent::enabledAgent() const
240
{
Laurent Montel's avatar
Laurent Montel committed
241
    return NewMailNotifierAgentSettings::enabled();
242 243
}

244 245 246 247 248 249 250 251 252 253 254
bool NewMailNotifierAgent::showButtonToDisplayMail() const
{
    return NewMailNotifierAgentSettings::showButtonToDisplayMail();
}

void NewMailNotifierAgent::setShowButtonToDisplayMail(bool b)
{
    NewMailNotifierAgentSettings::setShowButtonToDisplayMail(b);
    NewMailNotifierAgentSettings::self()->writeConfig();
}

255 256 257 258 259 260

void NewMailNotifierAgent::showConfigureDialog(qlonglong windowId)
{
    configure( windowId );
}

Laurent Montel's avatar
Laurent Montel committed
261
void NewMailNotifierAgent::configure( WId windowId )
262
{
Laurent Montel's avatar
Laurent Montel committed
263 264 265 266 267 268 269 270 271 272
    QPointer<NewMailNotifierSettingsDialog> dialog = new NewMailNotifierSettingsDialog;
    if (windowId) {
#ifndef Q_WS_WIN
        KWindowSystem::setMainWindow( dialog, windowId );
#else
        KWindowSystem::setMainWindow( dialog, (HWND)windowId );
#endif
    }
    dialog->exec();
    delete dialog;
273 274
}

275 276
bool NewMailNotifierAgent::excludeSpecialCollection(const Akonadi::Collection &collection) const
{
277 278 279 280 281 282 283 284 285
    if ( collection.hasAttribute<Akonadi::EntityHiddenAttribute>() )
        return true;

    if ( collection.hasAttribute<NewMailNotifierAttribute>() ) {
        if (collection.attribute<NewMailNotifierAttribute>()->ignoreNewMail()) {
            return true;
        }
    }

286 287 288 289
    if (!collection.contentMimeTypes().contains( KMime::Message::mimeType()) ) {
        return true;
    }

290 291 292 293 294 295 296 297
    SpecialMailCollections::Type type = SpecialMailCollections::self()->specialCollectionType(collection);
    switch(type) {
    case SpecialMailCollections::Invalid: //Not a special collection
    case SpecialMailCollections::Inbox:
        return false;
    default:
        return true;
    }
298

299 300
}

301
void NewMailNotifierAgent::itemsRemoved(const Item::List &items )
Laurent Montel's avatar
Laurent Montel committed
302
{
303
    if (!isActive())
Laurent Montel's avatar
Laurent Montel committed
304
        return;
305

Laurent Montel's avatar
Laurent Montel committed
306 307 308
    QHash< Akonadi::Collection, QList<Akonadi::Item::Id> >::iterator end(mNewMails.end());
    for ( QHash< Akonadi::Collection, QList<Akonadi::Item::Id> >::iterator it = mNewMails.begin(); it != end; ++it ) {
        QList<Akonadi::Item::Id> idList = it.value();
309
        bool itemFound = false;
Laurent Montel's avatar
Laurent Montel committed
310
        Q_FOREACH( const Item &item, items ) {
311 312 313 314
            if (idList.contains(item.id())) {
                idList.removeAll( item.id() );
                itemFound = true;
            }
315 316 317 318 319
        }
        if (itemFound) {
            if (mNewMails[it.key()].isEmpty()) {
                mNewMails.remove( it.key() );
            } else {
320
                mNewMails[it.key()] = idList;
Laurent Montel's avatar
Laurent Montel committed
321 322 323 324 325
            }
        }
    }
}

326
void NewMailNotifierAgent::itemsFlagsChanged( const Akonadi::Item::List &items, const QSet<QByteArray> &addedFlags, const QSet<QByteArray> &removedFlags )
Laurent Montel's avatar
Laurent Montel committed
327
{
328
    if (!isActive())
Laurent Montel's avatar
Laurent Montel committed
329
        return;
Laurent Montel's avatar
Laurent Montel committed
330
    Q_FOREACH (const Akonadi::Item &item, items) {
331 332 333 334 335 336 337 338 339 340 341 342 343 344
        QHash< Akonadi::Collection, QList<Akonadi::Item::Id> >::iterator end(mNewMails.end());
        for ( QHash< Akonadi::Collection, QList<Akonadi::Item::Id> >::iterator it = mNewMails.begin(); it != end; ++it ) {
            QList<Akonadi::Item::Id> idList= it.value();
            if (idList.contains(item.id()) && addedFlags.contains("\\SEEN")) {
                idList.removeAll( item.id() );
                if ( idList.isEmpty() ) {
                    mNewMails.remove( it.key() );
                    break;
                } else {
                    (*it) = idList;
                }
            }
        }
    }
Laurent Montel's avatar
Laurent Montel committed
345 346
}

347
void NewMailNotifierAgent::itemsMoved( const Akonadi::Item::List &items, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination )
348
{
349
    if (!isActive())
350 351
        return;

Laurent Montel's avatar
Laurent Montel committed
352
    Q_FOREACH (const Akonadi::Item &item, items) {
Laurent Montel's avatar
Laurent Montel committed
353
        if (ignoreStatusMail(item)) {
354
            continue;
Laurent Montel's avatar
Laurent Montel committed
355
        }
356

357 358 359
        if ( excludeSpecialCollection(collectionSource) ) {
            continue; // outbox, sent-mail, trash, drafts or templates.
        }
360

361 362 363 364
        if ( mNewMails.contains( collectionSource ) ) {
            QList<Akonadi::Item::Id> idListFrom = mNewMails[ collectionSource ];
            if ( idListFrom.contains( item.id() ) ) {
                idListFrom.removeAll( item.id() );
365 366

                if ( idListFrom.isEmpty() ) {
367
                    mNewMails.remove( collectionSource );
368 369 370
                } else {
                    mNewMails[ collectionSource ] = idListFrom;
                }
371 372 373 374 375
                if ( !excludeSpecialCollection(collectionDestination) ) {
                    QList<Akonadi::Item::Id> idListTo = mNewMails[ collectionDestination ];
                    idListTo.append( item.id() );
                    mNewMails[ collectionDestination ] = idListTo;
                }
Laurent Montel's avatar
Laurent Montel committed
376
            }
Laurent Montel's avatar
Laurent Montel committed
377 378
        }
    }
379
}
Tobias Koenig's avatar
Tobias Koenig committed
380

Laurent Montel's avatar
Laurent Montel committed
381 382 383 384 385 386 387 388 389
bool NewMailNotifierAgent::ignoreStatusMail(const Akonadi::Item &item)
{
    Akonadi::MessageStatus status;
    status.setStatusFromFlags( item.flags() );
    if ( status.isRead() || status.isSpam() || status.isIgnored() )
        return true;
    return false;
}

Laurent Montel's avatar
Laurent Montel committed
390 391
void NewMailNotifierAgent::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
{
392
    if (!isActive())
Laurent Montel's avatar
Laurent Montel committed
393
        return;
394

Laurent Montel's avatar
Laurent Montel committed
395 396 397 398
    if ( excludeSpecialCollection(collection) ) {
        return; // outbox, sent-mail, trash, drafts or templates.
    }

Laurent Montel's avatar
Laurent Montel committed
399
    if (ignoreStatusMail(item)) {
Laurent Montel's avatar
Laurent Montel committed
400
        return;
Laurent Montel's avatar
Laurent Montel committed
401
    }
Laurent Montel's avatar
Laurent Montel committed
402

Laurent Montel's avatar
Laurent Montel committed
403 404
    if ( !mTimer.isActive() ) {
        mTimer.start();
Laurent Montel's avatar
Laurent Montel committed
405
    }
Laurent Montel's avatar
Laurent Montel committed
406
    mNewMails[ collection ].append( item.id() );
407 408
}

Laurent Montel's avatar
Laurent Montel committed
409
void NewMailNotifierAgent::slotShowNotifications()
410
{
Laurent Montel's avatar
Be safe  
Laurent Montel committed
411 412 413
    if (mNewMails.isEmpty())
        return;

414
    if (!isActive())
415 416
        return;

Laurent Montel's avatar
Laurent Montel committed
417
    if (!mInstanceNameInProgress.isEmpty()) {
418 419 420 421 422
        //Restart timer until all is done.
        mTimer.start();
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
423
    QString message;
Laurent Montel's avatar
Laurent Montel committed
424
    if (NewMailNotifierAgentSettings::verboseNotification()) {
425
        bool hasUniqMessage = true;
426
        Akonadi::Item::Id item = -1;
427
        QString currentPath;
Laurent Montel's avatar
Laurent Montel committed
428 429
        QStringList texts;
        QHash< Akonadi::Collection, QList<Akonadi::Item::Id> >::const_iterator end(mNewMails.constEnd());
430 431 432 433
        const int numberOfCollection(mNewMails.count());
        if (numberOfCollection > 1)
            hasUniqMessage = false;

Laurent Montel's avatar
Laurent Montel committed
434 435 436 437 438 439 440
        for ( QHash< Akonadi::Collection, QList<Akonadi::Item::Id> >::const_iterator it = mNewMails.constBegin(); it != end; ++it ) {
            Akonadi::EntityDisplayAttribute *attr = it.key().attribute<Akonadi::EntityDisplayAttribute>();
            QString displayName;
            if ( attr && !attr->displayName().isEmpty() )
                displayName = attr->displayName();
            else
                displayName = it.key().name();
441 442

            if (hasUniqMessage) {
Laurent Montel's avatar
Laurent Montel committed
443 444 445 446
                if (it.value().count() == 0) {
                    //You can have an unique folder with 0 message
                    return;
                } else if (it.value().count() == 1 ) {
447 448 449 450 451 452
                    item = it.value().first();
                    currentPath = displayName;
                    break;
                } else {
                    hasUniqMessage = false;
                }
453
            }
454 455 456 457 458 459 460 461 462 463 464 465
            QString resourceName;
            if (!mCacheResourceName.contains(it.key().resource())) {
                Q_FOREACH ( const Akonadi::AgentInstance &instance, Akonadi::AgentManager::self()->instances() ) {
                    if (instance.identifier() == it.key().resource()) {
                        mCacheResourceName.insert(instance.identifier(), instance.name());
                        resourceName = instance.name();
                        break;
                    }
                }
            } else {
                resourceName = mCacheResourceName.value(it.key().resource());
            }
Laurent Montel's avatar
Laurent Montel committed
466 467
            const int numberOfEmails(it.value().count());
            if (numberOfEmails>0) {
468
                texts.append( i18np( "One new email in %2 from \"%3\"", "%1 new emails in %2 from \"%3\"", numberOfEmails, displayName,
469
                                 resourceName ) );
Laurent Montel's avatar
Laurent Montel committed
470
            }
471
        }
472
        if (hasUniqMessage) {
Laurent Montel's avatar
Laurent Montel committed
473
            SpecialNotifierJob *job = new SpecialNotifierJob(mListEmails, currentPath, item, this);
474
            job->setDefaultPixmap(mDefaultPixmap);
Laurent Montel's avatar
Laurent Montel committed
475
            connect(job, SIGNAL(displayNotification(QPixmap,QString)), SLOT(slotDisplayNotification(QPixmap,QString)));
476 477
            mNewMails.clear();
            return;
478 479
        } else {
            message = texts.join( QLatin1String("<br>") );
Laurent Montel's avatar
Laurent Montel committed
480
        }
Laurent Montel's avatar
Laurent Montel committed
481
    } else {
Laurent Montel's avatar
Laurent Montel committed
482
        message = i18n( "New mail arrived" );
Laurent Montel's avatar
Laurent Montel committed
483
    }
Laurent Montel's avatar
Laurent Montel committed
484

Laurent Montel's avatar
Laurent Montel committed
485
    kDebug() << message;
Laurent Montel's avatar
Laurent Montel committed
486

487
    slotDisplayNotification(mDefaultPixmap, message);
488

Laurent Montel's avatar
Laurent Montel committed
489
    mNewMails.clear();
490 491
}

Laurent Montel's avatar
Laurent Montel committed
492

Laurent Montel's avatar
Laurent Montel committed
493 494
void NewMailNotifierAgent::slotDisplayNotification(const QPixmap &pixmap, const QString &message)
{
495 496 497 498 499 500
    KNotification::event( QLatin1String("new-email"),
                          message,
                          pixmap,
                          0,
                          KNotification::CloseOnTimeout,
                          KGlobal::mainComponent());
Laurent Montel's avatar
Laurent Montel committed
501

Laurent Montel's avatar
Laurent Montel committed
502
    if ( NewMailNotifierAgentSettings::beepOnNewMails() ) {
Laurent Montel's avatar
Laurent Montel committed
503 504 505
        KNotification::beep();
    }
}
Laurent Montel's avatar
Laurent Montel committed
506

507 508 509 510 511 512 513 514 515 516 517 518
void NewMailNotifierAgent::slotInstanceNameChanged(const Akonadi::AgentInstance &instance)
{
    if (!isActive())
        return;

    const QString identifier(instance.identifier());
    if (mCacheResourceName.contains(identifier)) {
        mCacheResourceName.remove(identifier);
        mCacheResourceName.insert(identifier, instance.name());
    }
}

519 520
void NewMailNotifierAgent::slotInstanceStatusChanged(const Akonadi::AgentInstance &instance)
{
521
    if (!isActive())
522 523
        return;

524 525
    const QString identifier(instance.identifier());
    switch(instance.status()) {
526
    case Akonadi::AgentInstance::Broken:
527
    case Akonadi::AgentInstance::Idle:
528 529 530 531 532 533
    {
        if (mInstanceNameInProgress.contains(identifier)) {
            mInstanceNameInProgress.removeAll(identifier);
        }
        break;
    }
534
    case Akonadi::AgentInstance::Running:
535
    {
536
        if (!excludeAgentType(instance)) {
537 538 539
            if (!mInstanceNameInProgress.contains(identifier)) {
                mInstanceNameInProgress.append(identifier);
            }
540 541 542
        }
        break;
    }
543
    case Akonadi::AgentInstance::NotConfigured:
Laurent Montel's avatar
Debug--  
Laurent Montel committed
544
        //Nothing
545 546 547 548
        break;
    }
}

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
bool NewMailNotifierAgent::excludeAgentType(const Akonadi::AgentInstance &instance)
{
    if ( instance.type().mimeTypes().contains( KMime::Message::mimeType() ) ) {
        const QStringList capabilities( instance.type().capabilities() );
        if ( capabilities.contains( QLatin1String("Resource") ) &&
             !capabilities.contains( QLatin1String("Virtual") ) &&
             !capabilities.contains( QLatin1String("MailTransport") ) ) {
            return false;
        } else {
            return true;
        }
    }
    return true;
}

564 565
void NewMailNotifierAgent::slotInstanceRemoved(const Akonadi::AgentInstance &instance)
{
566
    if (!isActive())
567 568 569 570 571 572
        return;

    const QString identifier(instance.identifier());
    if (mInstanceNameInProgress.contains(identifier)) {
        mInstanceNameInProgress.removeAll(identifier);
    }
573 574
}

Laurent Montel's avatar
Laurent Montel committed
575 576 577 578 579
void NewMailNotifierAgent::slotInstanceAdded(const Akonadi::AgentInstance &instance)
{
    mCacheResourceName.insert(instance.identifier(), instance.name());
}

Laurent Montel's avatar
Laurent Montel committed
580 581
void NewMailNotifierAgent::printDebug()
{
Laurent Montel's avatar
Laurent Montel committed
582
    kDebug()<<"instance in progress: "<<mInstanceNameInProgress
Laurent Montel's avatar
Laurent Montel committed
583
            <<"\n notifier enabled : "<<NewMailNotifierAgentSettings::enabled()
Laurent Montel's avatar
Laurent Montel committed
584
            <<"\n check in progress : "<<!mInstanceNameInProgress.isEmpty()
Laurent Montel's avatar
Laurent Montel committed
585
            <<"\n beep on new mails: "<<NewMailNotifierAgentSettings::beepOnNewMails();
Laurent Montel's avatar
Laurent Montel committed
586 587
}

588 589 590 591 592
bool NewMailNotifierAgent::isActive() const
{
    return isOnline() && NewMailNotifierAgentSettings::enabled();
}

593 594
AKONADI_AGENT_MAIN( NewMailNotifierAgent )

595