• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi/contact

contactcompletionmodel.cpp

00001 /*
00002     This file is part of Akonadi Contact.
00003 
00004     Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
00020 */
00021 
00022 #include "contactcompletionmodel_p.h"
00023 
00024 #include "../kdescendantsproxymodel_p.h"
00025 
00026 #include <akonadi/changerecorder.h>
00027 #include <akonadi/entitymimetypefiltermodel.h>
00028 #include <akonadi/itemfetchscope.h>
00029 #include <akonadi/session.h>
00030 
00031 #include <kabc/addressee.h>
00032 
00033 using namespace Akonadi;
00034 
00035 QAbstractItemModel* ContactCompletionModel::mSelf = 0;
00036 
00037 QAbstractItemModel* ContactCompletionModel::self()
00038 {
00039   if ( mSelf )
00040     return mSelf;
00041 
00042   ChangeRecorder *monitor = new ChangeRecorder;
00043   monitor->fetchCollection( true );
00044   monitor->itemFetchScope().fetchFullPayload();
00045   monitor->setCollectionMonitored( Akonadi::Collection::root() );
00046   monitor->setMimeTypeMonitored( KABC::Addressee::mimeType() );
00047 
00048   ContactCompletionModel *model = new ContactCompletionModel( monitor );
00049 
00050   KDescendantsProxyModel *descModel = new KDescendantsProxyModel( model );
00051   descModel->setSourceModel( model );
00052 
00053   EntityMimeTypeFilterModel *filter = new Akonadi::EntityMimeTypeFilterModel( model );
00054   filter->setSourceModel( descModel );
00055   filter->addMimeTypeExclusionFilter( Akonadi::Collection::mimeType() );
00056   filter->setHeaderGroup( Akonadi::EntityTreeModel::ItemListHeaders );
00057 
00058   mSelf = filter;
00059 
00060   return mSelf;
00061 }
00062 
00063 ContactCompletionModel::ContactCompletionModel( ChangeRecorder *monitor, QObject *parent )
00064   : EntityTreeModel( monitor, parent )
00065 {
00066 }
00067 
00068 ContactCompletionModel::~ContactCompletionModel()
00069 {
00070 }
00071 
00072 QVariant ContactCompletionModel::entityData( const Item &item, int column, int role ) const
00073 {
00074   if ( !item.hasPayload<KABC::Addressee>() ) {
00075     // Pass modeltest
00076     if ( role == Qt::DisplayRole )
00077       return item.remoteId();
00078 
00079     return QVariant();
00080   }
00081 
00082   if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
00083     const KABC::Addressee contact = item.payload<KABC::Addressee>();
00084 
00085     switch ( column ) {
00086       case NameColumn:
00087         if ( !contact.formattedName().isEmpty() )
00088           return contact.formattedName();
00089         else
00090           return contact.assembledName();
00091         break;
00092       case NameAndEmailColumn:
00093         {
00094           QString name = QString::fromLatin1( "%1 %2" ).arg( contact.givenName() )
00095                                                        .arg( contact.familyName() ).simplified();
00096           if ( name.isEmpty() )
00097             name = contact.organization().simplified();
00098           if ( name.isEmpty() )
00099             return QString();
00100 
00101           const QString email = contact.preferredEmail().simplified();
00102           if ( email.isEmpty() )
00103             return QString();
00104 
00105           return QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email );
00106         }
00107         break;
00108       case EmailColumn:
00109         return contact.preferredEmail();
00110         break;
00111     }
00112   }
00113 
00114   return EntityTreeModel::entityData( item, column, role );
00115 }
00116 
00117 QVariant ContactCompletionModel::entityData( const Collection &collection, int column, int role ) const
00118 {
00119   return EntityTreeModel::entityData( collection, column, role );
00120 }
00121 
00122 int ContactCompletionModel::columnCount( const QModelIndex &parent ) const
00123 {
00124   if ( !parent.isValid() )
00125     return 3;
00126   else
00127     return 0;
00128 }
00129 
00130 int ContactCompletionModel::entityColumnCount( HeaderGroup ) const
00131 {
00132   return 3;
00133 }
00134 
00135 #include "contactcompletionmodel_p.moc"

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal