From 3340bdb187c71452b61efb6054467551c2874f01 Mon Sep 17 00:00:00 2001 From: Thomas McGuire Date: Mon, 2 Mar 2009 22:08:20 +0000 Subject: [PATCH] Support the metadata-over-DBus API, as used by e.g. Tracker. Patch by Philip Van Hoof, thanks! http://reviewboard.kde.org/r/168/ BUG: 185331 svn path=/trunk/KDE/kdepim/; revision=934376 --- CMakeLists.txt | 5 ++ kmkernel.cpp | 9 +- kmkernel.h | 5 ++ mailmanagerimpl.cpp | 196 ++++++++++++++++++++++++++++++++++++++++++++ mailmanagerimpl.h | 59 +++++++++++++ 5 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 mailmanagerimpl.cpp create mode 100644 mailmanagerimpl.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d076c4897..cfdb201bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,6 +171,7 @@ set(kmailprivate_LIB_SRCS kmailicalifaceimpl.cpp aboutdata.cpp mailserviceimpl.cpp + mailmanagerimpl.cpp attachmentlistview.cpp kmcomposereditor.cpp kmlineeditspell.cpp @@ -281,6 +282,10 @@ qt4_add_dbus_adaptor(kmailprivate_LIB_SRCS ${CMAKE_SOURCE_DIR}/libkdepim/interfaces/org.kde.mailtransport.service.xml mailserviceimpl.h KMail::MailServiceImpl ) +qt4_add_dbus_adaptor(kmailprivate_LIB_SRCS + ${CMAKE_SOURCE_DIR}/libkdepim/interfaces/org.freedesktop.email.metadata.Manager.xml mailmanagerimpl.h + KMail::MailManagerImpl +) qt4_add_dbus_interfaces(kmailprivate_LIB_SRCS ${CMAKE_BINARY_DIR}/kmail/org.kde.kmail.kmail.xml diff --git a/kmkernel.cpp b/kmkernel.cpp index bfbd5e7bb..23cb654cb 100644 --- a/kmkernel.cpp +++ b/kmkernel.cpp @@ -46,6 +46,8 @@ using KPIM::RecentAddresses; #include "kmailicalifaceimpl.h" #include "mailserviceimpl.h" using KMail::MailServiceImpl; +#include "mailmanagerimpl.h" +using KMail::MailManagerImpl; #include "jobscheduler.h" #include "templateparser.h" using KMail::TemplateParser; @@ -72,6 +74,7 @@ using KWallet::Wallet; #include #include #include +#include #include #include @@ -97,7 +100,7 @@ static KMKernel * mySelf = 0; KMKernel::KMKernel (QObject *parent, const char *name) : QObject(parent), mIdentityManager(0), mConfigureDialog(0), mICalIface(0), mMailService(0), - mContextMenuShown( false ), mWallet( 0 ) + mMailManager( 0 ), mContextMenuShown( false ), mWallet( 0 ) { kDebug(5006); setObjectName( name ); @@ -185,6 +188,8 @@ KMKernel::~KMKernel () mICalIface = 0; delete mMailService; mMailService = 0; + delete mMailManager; + mMailManager = 0; GlobalSettings::self()->writeConfig(); delete mWallet; @@ -196,10 +201,12 @@ KMKernel::~KMKernel () void KMKernel::setupDBus() { (void) new KmailAdaptor( this ); + qDBusRegisterMetaType >(); QDBusConnection::sessionBus().registerObject( "/KMail", this ); mICalIface->registerWithDBus(); mICalIface->readConfig(); mMailService = new MailServiceImpl(); + mMailManager = new MailManagerImpl(); } bool KMKernel::handleCommandLine( bool noArgsOpensReader ) diff --git a/kmkernel.h b/kmkernel.h index 1c48d791f..3941414bd 100644 --- a/kmkernel.h +++ b/kmkernel.h @@ -23,6 +23,8 @@ #define kmkernel KMKernel::self() #define kmconfig KMKernel::config() +Q_DECLARE_METATYPE(QVector) + namespace KIO { class Job; } @@ -37,6 +39,7 @@ class KJob; */ namespace KMail { class MailServiceImpl; + class MailManagerImpl; class UndoStack; class JobScheduler; class MessageSender; @@ -45,6 +48,7 @@ namespace KMail { } namespace KPIM { class ProgressDialog; } using KMail::MailServiceImpl; +using KMail::MailManagerImpl; using KMail::AccountManager; using KMail::UndoStack; using KMail::JobScheduler; @@ -496,6 +500,7 @@ private: // temporary mainwin KMMainWin *mWin; MailServiceImpl *mMailService; + MailManagerImpl *mMailManager; // the time of the last change of the unread or total count of a folder; // this can be queried via D-Bus in order to determine whether the counts diff --git a/mailmanagerimpl.cpp b/mailmanagerimpl.cpp new file mode 100644 index 000000000..e3d9f66aa --- /dev/null +++ b/mailmanagerimpl.cpp @@ -0,0 +1,196 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + * + * This file is part of KMail, the KDE mail client. + * Copyright (c) 2009 Philip Van Hoof + * + * KMail 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) any later version. + * + * KMail 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 + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the Qt library by Trolltech AS, Norway (or with modified versions + * of Qt that use the same license as Qt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * Qt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ +#include "mailmanagerimpl.h" +#include + +#include "kmmessage.h" +#include "kmfolder.h" + +#include +#include + +#include +#include + +static const uint max_per_dbus_call = 1000; + +namespace KMail { + + +MailManagerImpl::MailManagerImpl() +{ + new ManagerAdaptor( this ); + QDBusConnection::sessionBus().registerObject( "/org/freedesktop/email/metadata/Manager", this ); +} + +void MailManagerImpl::processMsgBase( const KMMsgBase *msg, QStringList &subjects, QVector &predicatesArray, QVector &valuesArray ) +{ + QStringList values; + QStringList predicates; + + predicates.append( "EMailMeta:MessageSubject" ); + values.append( msg->subject() ); + + predicates.append( "EMailMeta:MessageFrom" ); + values.append( msg->fromStrip () ); + + predicates.append( "EMailMeta:MessageTo" ); + values.append( msg->toStrip () ); + + predicates.append( "KMail:MessageIdMD5" ); + values.append( msg->msgIdMD5() ); + + predicates.append( "KMail:MessageSerNum" ); + values.append( QString::number( msg->getMsgSerNum() ) ); + + predicates.append( "EMailMeta:MessageSent" ); + values.append( msg->dateStr () ); + + predicates.append( "EMailMeta:MessageSent" ); + values.append( msg->dateStr () ); + + predicates.append( "EMailMeta:MessageSize" ); + values.append( QString::number( static_cast ( msg->msgSizeServer() ) ) ); + + predicates.append( "KMail:MessageUID" ); + values.append( QString::number(static_cast ( msg->UID() ) ) ); + + for ( KMMessageTagList::const_iterator it = msg->tagList()->constBegin(); it != msg->tagList()->constEnd(); ++it ) { + QString tag = (*it); + predicates.append( "KMail:MessageTag" ); + values.append( tag ); + } + + const MessageStatus &stat = msg->messageStatus(); + + predicates.append( "EMailMeta:MessageSeen" ); + if (stat.isRead()) + values.append( "True" ); + else + values.append( "False" ); + + predicates.append( "KMail:MessageSpam" ); + if ( stat.isSpam() ) + values.append( "True" ); + else + values.append( "False" ); + + predicates.append( "KMail:MessageHam" ); + if ( stat.isHam() ) + values.append( "True" ); + else + values.append( "False" ); + + predicates.append( "EMailMeta:MessageAnswered" ); + if ( stat.isReplied() ) + values.append( "True" ); + else + values.append( "False" ); + + predicates.append( "EMailMeta:MessageForwarded" ); + if ( stat.isForwarded() ) + values.append( "True" ); + else + values.append( "False" ); + + subjects.append( "kmail://" + QString::number( msg->getMsgSerNum() ) ); + predicatesArray.append( predicates ); + valuesArray.append( values ); +} + +void MailManagerImpl::Register( const QDBusObjectPath ®istrarPath, uint lastModseq ) +{ + Q_UNUSED( lastModseq ); + registrars.append( registrarPath ); + + QDBusMessage m = QDBusMessage::createMethodCall( message().service(), + registrarPath.path(), + "org.freedesktop.email.metadata.Registrar", "Cleanup" ); + QList args; + args.append ( static_cast ( time( 0 ) ) ); + m.setArguments( args ); + QDBusConnection::sessionBus().send( m ); + + QList< QPointer< KMFolder > > folders = kmkernel->allFolders(); + QList< QPointer< KMFolder > >::Iterator it = folders.begin(); + + while ( it != folders.end() ) { + uint i = 0; + + KMFolder *folder = (*it); + ++it; + + folder->open("DBus"); + + uint fcount = folder->count(); + + while ( i < fcount ) { + + QVector predicatesArray; + QVector valuesArray; + QStringList subjects; + + uint processed = 0; + + for ( ; i < fcount; ++i ) { + const KMMsgBase *msg = folder->getMsgBase( i ); + + processMsgBase( msg, subjects, predicatesArray, valuesArray ); + processed++; + + if ( processed >= max_per_dbus_call ) + break; + } + + if (!subjects.isEmpty()) { + QDBusMessage m = QDBusMessage::createMethodCall( message().service(), + registrarPath.path(), + "org.freedesktop.email.metadata.Registrar", "SetMany" ); + QList args; + + args.append( subjects ); + args.append( qVariantFromValue( predicatesArray ) ); + args.append( qVariantFromValue( valuesArray ) ); + args.append( static_cast ( time( 0 ) ) ); + + m.setArguments( args ); + + QDBusConnection::sessionBus().send( m ); + } + } + + folder->close( "DBus", false ); + } +} + + +}//end namespace KMail + diff --git a/mailmanagerimpl.h b/mailmanagerimpl.h new file mode 100644 index 000000000..6a11caf85 --- /dev/null +++ b/mailmanagerimpl.h @@ -0,0 +1,59 @@ +/* -*- mode: C++; c-file-style: "gnu" -*- + * + * This file is part of KMail, the KDE mail client. + * Copyright (c) 2009 Philip Van Hoof + * + * KMail 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) any later version. + * + * KMail 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 + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the Qt library by Trolltech AS, Norway (or with modified versions + * of Qt that use the same license as Qt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * Qt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ +#ifndef MAILMANAGERIMPL_H +#define MAILMANAGERIMPL_H + +class KMMsgBase; +class QString; +#include +#include + +namespace KMail { + + // This class implements the D-Bus interface + // libkdepim/interfaces/org.freedesktop.email.metadata.Manager.xml + class MailManagerImpl : public QObject, + protected QDBusContext + { + Q_OBJECT + private: + QList< QDBusObjectPath > registrars; + void processMsgBase ( const KMMsgBase *msg, QStringList &subjects, + QVector &predicatesArray, + QVector &valuesArray ); + + public: + MailManagerImpl(); + void Register( const QDBusObjectPath ®istrarPath, uint lastModseq ); + }; +} + +#endif