Commit 17474975 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Now newmailnotifier works better than kmail notifier. I added a new dbus...

Now newmailnotifier works better than kmail notifier. I added a new dbus interface and config dialog (not new i18n).

I can't replace kmail notifier by it in 4.11 but will continue to improve it in 4.11 and 4.12
And as it's an agent we can have notify even if kmail is closed
parent 89283593
......@@ -10,8 +10,11 @@ if (BUILD_NEW_MAIL_NOTIFIER_AGENT)
set(newmailnotifieragent_SRCS
newmailnotifieragent.cpp
newmailnotifierattribute.cpp
)
qt4_add_dbus_adaptor(newmailnotifieragent_SRCS org.freedesktop.Akonadi.NewMailNotifier.xml newmailnotifieragent.h NewMailNotifierAgent)
kde4_add_executable( akonadi_newmailnotifier_agent ${newmailnotifieragent_SRCS})
......@@ -22,6 +25,7 @@ target_link_libraries( akonadi_newmailnotifier_agent
${KDEPIMLIBS_AKONADI_KMIME_LIBS}
${QT_QTCORE_LIBRARY}
${QT_QTDBUS_LIBRARY}
${KDE4_KNOTIFYCONFIG_LIBS}
)
......
......@@ -21,26 +21,42 @@
#include "newmailnotifieragent.h"
#include "newmailnotifierattribute.h"
#include "newmailnotifieradaptor.h"
#include <akonadi/dbusconnectionpool.h>
#include <akonadi/agentfactory.h>
#include <akonadi/changerecorder.h>
#include <akonadi/entitydisplayattribute.h>
#include <akonadi/entityhiddenattribute.h>
#include <akonadi/itemfetchscope.h>
#include <akonadi/session.h>
#include <Akonadi/AttributeFactory>
#include <Akonadi/CollectionFetchScope>
#include <akonadi/kmime/specialmailcollections.h>
#include <akonadi/kmime/messagestatus.h>
#include <KLocalizedString>
#include <KMime/Message>
#include <KNotification>
#include <KNotifyConfigWidget>
#include <KIconLoader>
#include <KIcon>
#include <KConfigGroup>
using namespace Akonadi;
NewMailNotifierAgent::NewMailNotifierAgent( const QString &id )
: AgentBase( id )
: AgentBase( id ), mNotifierEnabled(true)
{
Akonadi::AttributeFactory::registerAttribute<NewMailNotifierAttribute>();
new NewMailNotifierAdaptor( this );
DBusConnectionPool::threadConnection().registerObject( QLatin1String( "/NewMailNotifierAgent" ),
this, QDBusConnection::ExportAdaptors );
DBusConnectionPool::threadConnection().registerService( QLatin1String( "org.freedesktop.Akonadi.NewMailNotifierAgent" ) );
changeRecorder()->setMimeTypeMonitored( KMime::Message::mimeType() );
changeRecorder()->itemFetchScope().setCacheOnly( true );
changeRecorder()->itemFetchScope().setFetchModificationTime( false );
......@@ -49,14 +65,53 @@ NewMailNotifierAgent::NewMailNotifierAgent( const QString &id )
changeRecorder()->setAllMonitored(true);
changeRecorder()->ignoreSession( Akonadi::Session::defaultSession() );
changeRecorder()->collectionFetchScope().setAncestorRetrieval( Akonadi::CollectionFetchScope::All );
changeRecorder()->setCollectionMonitored(Collection::root(), true);
m_timer.setInterval( 5 * 1000 );
connect( &m_timer, SIGNAL(timeout()), SLOT(showNotifications()) );
m_timer.setSingleShot( true );
KConfigGroup group( KGlobal::config(), "General" );
mNotifierEnabled = group.readEntry( "enabled", true);
if (mNotifierEnabled) {
m_timer.setSingleShot( true );
}
}
void NewMailNotifierAgent::setEnableNotifier(bool b)
{
if (mNotifierEnabled != b) {
mNotifierEnabled = b;
KConfigGroup group( KGlobal::config(), "General" );
group.writeEntry( "enabled", mNotifierEnabled);
if (!mNotifierEnabled) {
mNewMails.clear();
}
}
}
bool NewMailNotifierAgent::enabledNotifier() const
{
return mNotifierEnabled;
}
void NewMailNotifierAgent::configure( WId /*windowId*/ )
{
KNotifyConfigWidget::configure( 0 );
}
bool NewMailNotifierAgent::excludeSpecialCollection(const Akonadi::Collection &collection) const
{
if ( collection.hasAttribute<Akonadi::EntityHiddenAttribute>() )
return true;
if ( collection.hasAttribute<NewMailNotifierAttribute>() ) {
if (collection.attribute<NewMailNotifierAttribute>()->ignoreNewMail()) {
return true;
}
}
SpecialMailCollections::Type type = SpecialMailCollections::self()->specialCollectionType(collection);
switch(type) {
case SpecialMailCollections::Invalid: //Not a special collection
......@@ -69,16 +124,18 @@ bool NewMailNotifierAgent::excludeSpecialCollection(const Akonadi::Collection &c
void NewMailNotifierAgent::itemMoved( const Akonadi::Item &item, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination )
{
if (!mNotifierEnabled)
return;
Akonadi::MessageStatus status;
status.setStatusFromFlags( item.flags() );
if ( status.isRead() || status.isSpam() || status.isIgnored() )
return;
if ( excludeSpecialCollection(collectionSource) ) {
return; // outbox, sent-mail, trash, drafts or templates.
}
if ( excludeSpecialCollection(collectionDestination) ) {
return; // outbox, sent-mail, trash, drafts or templates.
}
if ( mNewMails.contains( collectionSource ) ) {
QList<Akonadi::Item::Id> idListFrom = mNewMails[ collectionSource ];
if ( idListFrom.contains( item.id() ) ) {
......@@ -90,7 +147,7 @@ void NewMailNotifierAgent::itemMoved( const Akonadi::Item &item, const Akonadi::
if ( !excludeSpecialCollection(collectionDestination) ) {
QList<Akonadi::Item::Id> idListTo = mNewMails[ collectionDestination ];
idListTo.append( item.id() );
mNewMails[ collectionDestination ]=idListTo;
mNewMails[ collectionDestination ] = idListTo;
}
}
......@@ -98,8 +155,9 @@ void NewMailNotifierAgent::itemMoved( const Akonadi::Item &item, const Akonadi::
void NewMailNotifierAgent::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
{
if ( collection.hasAttribute<Akonadi::EntityHiddenAttribute>() )
if (!mNotifierEnabled)
return;
if ( excludeSpecialCollection(collection) ) {
return; // outbox, sent-mail, trash, drafts or templates.
}
......@@ -118,6 +176,9 @@ void NewMailNotifierAgent::itemAdded( const Akonadi::Item &item, const Akonadi::
void NewMailNotifierAgent::showNotifications()
{
if (!mNotifierEnabled)
return;
QStringList texts;
QHash< Akonadi::Collection, QList<Akonadi::Item::Id> >::const_iterator end(mNewMails.constEnd());
for ( QHash< Akonadi::Collection, QList<Akonadi::Item::Id> >::const_iterator it = mNewMails.constBegin(); it != end; ++it ) {
......@@ -132,7 +193,7 @@ void NewMailNotifierAgent::showNotifications()
kDebug() << texts;
const QPixmap pixmap = KIcon( QLatin1String("kmail") ).pixmap( KIconLoader::SizeSmall, KIconLoader::SizeSmall );
const QPixmap pixmap = KIcon( QLatin1String("kmail") ).pixmap( KIconLoader::SizeMedium, KIconLoader::SizeMedium );
KNotification::event( QLatin1String("new-email"),
texts.join( QLatin1String("<br>") ),
pixmap,
......
......@@ -33,16 +33,24 @@ class NewMailNotifierAgent : public Akonadi::AgentBase, public Akonadi::AgentBas
public:
explicit NewMailNotifierAgent( const QString &id );
void setEnableNotifier(bool b);
bool enabledNotifier() const;
protected:
void itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection );
void itemMoved( const Akonadi::Item &item, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination );
private slots:
void showNotifications();
void configure(WId windowId);
private:
bool excludeSpecialCollection(const Akonadi::Collection &collection) const;
QHash<Akonadi::Collection, QList<Akonadi::Item::Id> > mNewMails;
QTimer m_timer;
bool mNotifierEnabled;
};
#endif
/*
Copyright (c) 2013 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "newmailnotifierattribute.h"
#include <QByteArray>
#include <QIODevice>
#include <QDataStream>
class NewMailNotifierAttributePrivate
{
public:
NewMailNotifierAttributePrivate()
: ignoreNewMail(false)
{
}
bool ignoreNewMail;
};
NewMailNotifierAttribute::NewMailNotifierAttribute()
: d(new NewMailNotifierAttributePrivate)
{
}
NewMailNotifierAttribute::~NewMailNotifierAttribute()
{
delete d;
}
NewMailNotifierAttribute *NewMailNotifierAttribute::clone() const
{
NewMailNotifierAttribute *attr = new NewMailNotifierAttribute();
attr->setIgnoreNewMail(ignoreNewMail());
return attr;
}
QByteArray NewMailNotifierAttribute::type() const
{
static const QByteArray sType( "newmailnotifierattribute" );
return sType;
}
QByteArray NewMailNotifierAttribute::serialized() const
{
QByteArray result;
QDataStream s( &result, QIODevice::WriteOnly );
s << ignoreNewMail();
return result;
}
void NewMailNotifierAttribute::deserialize( const QByteArray &data )
{
QDataStream s( data );
s >> d->ignoreNewMail;
}
bool NewMailNotifierAttribute::ignoreNewMail() const
{
return d->ignoreNewMail;
}
void NewMailNotifierAttribute::setIgnoreNewMail(bool b)
{
d->ignoreNewMail = b;
}
/*
Copyright (c) 2013 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef NEWMAILNOTIFIERATTRIBUTE_H
#define NEWMAILNOTIFIERATTRIBUTE_H
#include <akonadi/attribute.h>
class NewMailNotifierAttributePrivate;
class NewMailNotifierAttribute : public Akonadi::Attribute
{
public:
NewMailNotifierAttribute();
~NewMailNotifierAttribute();
/* reimpl */
NewMailNotifierAttribute *clone() const;
QByteArray type() const;
QByteArray serialized() const;
void deserialize( const QByteArray &data );
bool ignoreNewMail() const;
void setIgnoreNewMail(bool b);
private:
friend class NewMailNotifierAttributePrivate;
NewMailNotifierAttributePrivate * const d;
};
#endif // NEWMAILNOTIFIERATTRIBUTE_H
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.Akonadi.NewMailNotifier">
<method name="setEnableNotifier" >
<arg type="b" direction="in"/>
</method>
<method name="enabledNotifier" >
<arg type="b" direction="out"/>
</method>
</interface>
</node>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment