• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
  • contact
contactcompletionmodel.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "contactcompletionmodel_p.h"
23 
24 #include <akonadi/changerecorder.h>
25 #include <akonadi/entitymimetypefiltermodel.h>
26 #include <akonadi/itemfetchscope.h>
27 #include <akonadi/session.h>
28 
29 #include <kabc/addressee.h>
30 
31 using namespace Akonadi;
32 
33 QAbstractItemModel* ContactCompletionModel::mSelf = 0;
34 
35 QAbstractItemModel* ContactCompletionModel::self()
36 {
37  if ( mSelf ) {
38  return mSelf;
39  }
40 
41  ChangeRecorder *monitor = new ChangeRecorder;
42  monitor->fetchCollection( true );
43  monitor->itemFetchScope().fetchFullPayload();
44  monitor->setCollectionMonitored( Akonadi::Collection::root() );
45  monitor->setMimeTypeMonitored( KABC::Addressee::mimeType() );
46 
47  ContactCompletionModel *model = new ContactCompletionModel( monitor );
48 
49  EntityMimeTypeFilterModel *filter = new Akonadi::EntityMimeTypeFilterModel( model );
50  filter->setSourceModel( model );
51  filter->addMimeTypeExclusionFilter( Akonadi::Collection::mimeType() );
52  filter->addMimeTypeExclusionFilter( Akonadi::Collection::virtualMimeType() );
53  filter->setHeaderGroup( Akonadi::EntityTreeModel::ItemListHeaders );
54 
55  mSelf = filter;
56 
57  return mSelf;
58 }
59 
60 ContactCompletionModel::ContactCompletionModel( ChangeRecorder *monitor, QObject *parent )
61  : EntityTreeModel( monitor, parent )
62 {
63  setCollectionFetchStrategy( InvisibleCollectionFetch );
64 }
65 
66 ContactCompletionModel::~ContactCompletionModel()
67 {
68 }
69 
70 QVariant ContactCompletionModel::entityData( const Item &item, int column, int role ) const
71 {
72  if ( !item.hasPayload<KABC::Addressee>() ) {
73  // Pass modeltest
74  if ( role == Qt::DisplayRole ) {
75  return item.remoteId();
76  }
77 
78  return QVariant();
79  }
80 
81  if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
82  const KABC::Addressee contact = item.payload<KABC::Addressee>();
83 
84  switch ( column ) {
85  case NameColumn:
86  if ( !contact.formattedName().isEmpty() ) {
87  return contact.formattedName();
88  } else {
89  return contact.assembledName();
90  }
91  break;
92  case NameAndEmailColumn:
93  {
94  QString name = QString::fromLatin1( "%1 %2" ).arg( contact.givenName() )
95  .arg( contact.familyName() ).simplified();
96  if ( name.isEmpty() ) {
97  name = contact.organization().simplified();
98  }
99  if ( name.isEmpty() ) {
100  return QString();
101  }
102 
103  const QString email = contact.preferredEmail().simplified();
104  if ( email.isEmpty() ) {
105  return QString();
106  }
107 
108  return QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email );
109  }
110  break;
111  case EmailColumn:
112  return contact.preferredEmail();
113  break;
114  }
115  }
116 
117  return EntityTreeModel::entityData( item, column, role );
118 }
119 
120 QVariant ContactCompletionModel::entityData( const Collection &collection, int column, int role ) const
121 {
122  return EntityTreeModel::entityData( collection, column, role );
123 }
124 
125 int ContactCompletionModel::columnCount( const QModelIndex &parent ) const
126 {
127  if ( !parent.isValid() ) {
128  return 3;
129  } else {
130  return 0;
131  }
132 }
133 
134 int ContactCompletionModel::entityColumnCount( HeaderGroup ) const
135 {
136  return 3;
137 }
138 
139 #include "moc_contactcompletionmodel_p.cpp"
Akonadi::EntityMimeTypeFilterModel::addMimeTypeExclusionFilter
void addMimeTypeExclusionFilter(const QString &mimeType)
Add mime type to be excluded by the filter.
Definition: entitymimetypefiltermodel.cpp:92
Akonadi::EntityTreeModel::entityData
virtual QVariant entityData(const Item &item, int column, int role=Qt::DisplayRole) const
Provided for convenience of subclasses.
Definition: entitytreemodel.cpp:136
Akonadi::Monitor::setMimeTypeMonitored
void setMimeTypeMonitored(const QString &mimetype, bool monitored=true)
Sets whether items of the specified mime type shall be monitored for changes.
Definition: monitor.cpp:126
Akonadi::Monitor::setCollectionMonitored
void setCollectionMonitored(const Collection &collection, bool monitored=true)
Sets whether the specified collection shall be monitored for changes.
Definition: monitor.cpp:66
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::Collection::virtualMimeType
static QString virtualMimeType()
Returns the mimetype used for virtual collections.
Definition: collection.cpp:202
Akonadi::Collection::mimeType
static QString mimeType()
Returns the mimetype used for collections.
Definition: collection.cpp:197
Akonadi::EntityMimeTypeFilterModel
A proxy model that filters entities by mime type.
Definition: entitymimetypefiltermodel.h:61
Akonadi::ItemFetchScope::fetchFullPayload
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Definition: itemfetchscope.cpp:68
Akonadi::Monitor::itemFetchScope
ItemFetchScope & itemFetchScope()
Returns the item fetch scope.
Definition: monitor.cpp:197
Akonadi::Collection::root
static Collection root()
Returns the root collection.
Definition: collection.cpp:192
Akonadi::Monitor::fetchCollection
void fetchCollection(bool enable)
Enables automatic fetching of changed collections from the Akonadi storage.
Definition: monitor.cpp:179
Akonadi::EntityTreeModel::ItemListHeaders
Header information for a list of items.
Definition: entitytreemodel.h:384
Akonadi::EntityMimeTypeFilterModel::setHeaderGroup
void setHeaderGroup(EntityTreeModel::HeaderGroup headerGroup)
Sets the header set of the filter model.
Definition: entitymimetypefiltermodel.cpp:152
Akonadi::EntityTreeModel
A model for collections and items together.
Definition: entitytreemodel.h:317
Akonadi::ChangeRecorder
Records and replays change notification.
Definition: changerecorder.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal