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

akonadi

  • 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->setHeaderGroup( Akonadi::EntityTreeModel::ItemListHeaders );
53 
54  mSelf = filter;
55 
56  return mSelf;
57 }
58 
59 ContactCompletionModel::ContactCompletionModel( ChangeRecorder *monitor, QObject *parent )
60  : EntityTreeModel( monitor, parent )
61 {
62  setCollectionFetchStrategy( InvisibleCollectionFetch );
63 }
64 
65 ContactCompletionModel::~ContactCompletionModel()
66 {
67 }
68 
69 QVariant ContactCompletionModel::entityData( const Item &item, int column, int role ) const
70 {
71  if ( !item.hasPayload<KABC::Addressee>() ) {
72  // Pass modeltest
73  if ( role == Qt::DisplayRole ) {
74  return item.remoteId();
75  }
76 
77  return QVariant();
78  }
79 
80  if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
81  const KABC::Addressee contact = item.payload<KABC::Addressee>();
82 
83  switch ( column ) {
84  case NameColumn:
85  if ( !contact.formattedName().isEmpty() ) {
86  return contact.formattedName();
87  } else {
88  return contact.assembledName();
89  }
90  break;
91  case NameAndEmailColumn:
92  {
93  QString name = QString::fromLatin1( "%1 %2" ).arg( contact.givenName() )
94  .arg( contact.familyName() ).simplified();
95  if ( name.isEmpty() ) {
96  name = contact.organization().simplified();
97  }
98  if ( name.isEmpty() ) {
99  return QString();
100  }
101 
102  const QString email = contact.preferredEmail().simplified();
103  if ( email.isEmpty() ) {
104  return QString();
105  }
106 
107  return QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email );
108  }
109  break;
110  case EmailColumn:
111  return contact.preferredEmail();
112  break;
113  }
114  }
115 
116  return EntityTreeModel::entityData( item, column, role );
117 }
118 
119 QVariant ContactCompletionModel::entityData( const Collection &collection, int column, int role ) const
120 {
121  return EntityTreeModel::entityData( collection, column, role );
122 }
123 
124 int ContactCompletionModel::columnCount( const QModelIndex &parent ) const
125 {
126  if ( !parent.isValid() ) {
127  return 3;
128  } else {
129  return 0;
130  }
131 }
132 
133 int ContactCompletionModel::entityColumnCount( HeaderGroup ) const
134 {
135  return 3;
136 }
137 
138 #include "moc_contactcompletionmodel_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:34 by doxygen 1.8.3.1 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-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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