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

akonadi/contact

  • 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  ChangeRecorder *monitor = new ChangeRecorder;
41  monitor->fetchCollection( true );
42  monitor->itemFetchScope().fetchFullPayload();
43  monitor->setCollectionMonitored( Akonadi::Collection::root() );
44  monitor->setMimeTypeMonitored( KABC::Addressee::mimeType() );
45 
46  ContactCompletionModel *model = new ContactCompletionModel( monitor );
47 
48  EntityMimeTypeFilterModel *filter = new Akonadi::EntityMimeTypeFilterModel( model );
49  filter->setSourceModel( model );
50  filter->addMimeTypeExclusionFilter( Akonadi::Collection::mimeType() );
51  filter->setHeaderGroup( Akonadi::EntityTreeModel::ItemListHeaders );
52 
53  mSelf = filter;
54 
55  return mSelf;
56 }
57 
58 ContactCompletionModel::ContactCompletionModel( ChangeRecorder *monitor, QObject *parent )
59  : EntityTreeModel( monitor, parent )
60 {
61  setCollectionFetchStrategy( InvisibleCollectionFetch );
62 }
63 
64 ContactCompletionModel::~ContactCompletionModel()
65 {
66 }
67 
68 QVariant ContactCompletionModel::entityData( const Item &item, int column, int role ) const
69 {
70  if ( !item.hasPayload<KABC::Addressee>() ) {
71  // Pass modeltest
72  if ( role == Qt::DisplayRole )
73  return item.remoteId();
74 
75  return QVariant();
76  }
77 
78  if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
79  const KABC::Addressee contact = item.payload<KABC::Addressee>();
80 
81  switch ( column ) {
82  case NameColumn:
83  if ( !contact.formattedName().isEmpty() )
84  return contact.formattedName();
85  else
86  return contact.assembledName();
87  break;
88  case NameAndEmailColumn:
89  {
90  QString name = QString::fromLatin1( "%1 %2" ).arg( contact.givenName() )
91  .arg( contact.familyName() ).simplified();
92  if ( name.isEmpty() )
93  name = contact.organization().simplified();
94  if ( name.isEmpty() )
95  return QString();
96 
97  const QString email = contact.preferredEmail().simplified();
98  if ( email.isEmpty() )
99  return QString();
100 
101  return QString::fromLatin1( "%1 <%2>" ).arg( name ).arg( email );
102  }
103  break;
104  case EmailColumn:
105  return contact.preferredEmail();
106  break;
107  }
108  }
109 
110  return EntityTreeModel::entityData( item, column, role );
111 }
112 
113 QVariant ContactCompletionModel::entityData( const Collection &collection, int column, int role ) const
114 {
115  return EntityTreeModel::entityData( collection, column, role );
116 }
117 
118 int ContactCompletionModel::columnCount( const QModelIndex &parent ) const
119 {
120  if ( !parent.isValid() )
121  return 3;
122  else
123  return 0;
124 }
125 
126 int ContactCompletionModel::entityColumnCount( HeaderGroup ) const
127 {
128  return 3;
129 }
130 
131 #include "contactcompletionmodel_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 09:08:05 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

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

kdepimlibs-4.9.1 API Reference

Skip menu "kdepimlibs-4.9.1 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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