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

akonadi/contact

  • akonadi
  • contact
contactgroupviewer.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 "contactgroupviewer.h"
23 
24 #include "contactgroupexpandjob.h"
25 #include "standardcontactgroupformatter.h"
26 #include "textbrowser_p.h"
27 
28 #include <akonadi/collectionfetchjob.h>
29 #include <akonadi/entitydisplayattribute.h>
30 #include <akonadi/item.h>
31 #include <akonadi/itemfetchjob.h>
32 #include <akonadi/itemfetchscope.h>
33 #include <kabc/addressee.h>
34 #include <kabc/contactgroup.h>
35 #include <kcolorscheme.h>
36 #include <kglobal.h>
37 #include <kicon.h>
38 #include <klocale.h>
39 #include <kstringhandler.h>
40 
41 #include <QtGui/QVBoxLayout>
42 
43 using namespace Akonadi;
44 
45 class ContactGroupViewer::Private
46 {
47  public:
48  Private( ContactGroupViewer *parent )
49  : mParent( parent ), mExpandJob( 0 ), mParentCollectionFetchJob( 0 )
50  {
51  mBrowser = new TextBrowser;
52 
53  static QPixmap groupPixmap = KIcon( QLatin1String( "x-mail-distribution-list" ) ).pixmap( QSize( 100, 100 ) );
54  mBrowser->document()->addResource( QTextDocument::ImageResource,
55  QUrl( QLatin1String( "group_photo" ) ),
56  groupPixmap );
57 
58  mStandardContactGroupFormatter = new StandardContactGroupFormatter;
59  mContactGroupFormatter = mStandardContactGroupFormatter;
60  }
61 
62  ~Private()
63  {
64  delete mStandardContactGroupFormatter;
65  }
66 
67  void updateView()
68  {
69  mParent->setWindowTitle( i18n( "Contact Group %1", mCurrentGroupName ) );
70 
71  KABC::ContactGroup group;
72  group.setName( mCurrentGroupName );
73  foreach ( const KABC::Addressee &contact, mCurrentContacts )
74  group.append( KABC::ContactGroup::Data( contact.realName(), contact.preferredEmail() ) );
75 
76  mContactGroupFormatter->setContactGroup( group );
77 
78  QList<QVariantMap> additionalFields;
79 
80  if ( !mCurrentAddressBookName.isEmpty() ) {
81  QVariantMap addressBookName;
82  addressBookName.insert( QLatin1String( "title" ), i18n( "Address Book" ) );
83  addressBookName.insert( QLatin1String( "value" ), mCurrentAddressBookName );
84 
85  additionalFields << addressBookName;
86  }
87 
88  mContactGroupFormatter->setAdditionalFields( additionalFields );
89 
90  mBrowser->setHtml( mContactGroupFormatter->toHtml() );
91  }
92 
93  void slotMailClicked( const QString&, const QString &email )
94  {
95  QString name, address;
96 
97  // remove the 'mailto:' and split into name and email address
98  KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address );
99 
100  emit mParent->emailClicked( name, address );
101  }
102 
103  void _k_expandResult( KJob *job )
104  {
105  mExpandJob = 0;
106 
107  if ( !job->error() ) {
108  ContactGroupExpandJob *expandJob = qobject_cast<ContactGroupExpandJob*>( job );
109  mCurrentContacts = expandJob->contacts();
110  }
111 
112  // stop any running fetch job
113  if ( mParentCollectionFetchJob ) {
114  mParent->disconnect( mParentCollectionFetchJob, SIGNAL(result(KJob*)), mParent, SLOT(slotParentCollectionFetched(KJob*)) );
115  delete mParentCollectionFetchJob;
116  mParentCollectionFetchJob = 0;
117  }
118 
119  mParentCollectionFetchJob = new CollectionFetchJob( mCurrentItem.parentCollection(), CollectionFetchJob::Base, mParent );
120  mParent->connect( mParentCollectionFetchJob, SIGNAL(result(KJob*)), SLOT(slotParentCollectionFetched(KJob*)) );
121  }
122 
123  void slotParentCollectionFetched( KJob *job )
124  {
125  mParentCollectionFetchJob = 0;
126  mCurrentAddressBookName.clear();
127 
128  if ( !job->error() ) {
129  CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
130  if ( !fetchJob->collections().isEmpty() ) {
131  const Collection collection = fetchJob->collections().first();
132  if ( collection.hasAttribute<EntityDisplayAttribute>() )
133  mCurrentAddressBookName = collection.attribute<EntityDisplayAttribute>()->displayName();
134  else
135  mCurrentAddressBookName = collection.name();
136  }
137  }
138 
139  updateView();
140  }
141 
142  ContactGroupViewer *mParent;
143  TextBrowser *mBrowser;
144  QString mCurrentGroupName;
145  KABC::Addressee::List mCurrentContacts;
146  QString mCurrentAddressBookName;
147  Item mCurrentItem;
148  ContactGroupExpandJob *mExpandJob;
149  CollectionFetchJob *mParentCollectionFetchJob;
150  AbstractContactGroupFormatter *mStandardContactGroupFormatter;
151  AbstractContactGroupFormatter *mContactGroupFormatter;
152 };
153 
154 ContactGroupViewer::ContactGroupViewer( QWidget *parent )
155  : QWidget( parent ), d( new Private( this ) )
156 {
157  QVBoxLayout *layout = new QVBoxLayout( this );
158  layout->setMargin( 0 );
159 
160  d->mBrowser->setNotifyClick( true );
161 
162  connect( d->mBrowser, SIGNAL(mailClick(QString,QString)),
163  this, SLOT(slotMailClicked(QString,QString)) );
164 
165  layout->addWidget( d->mBrowser );
166 
167  // always fetch full payload for contact groups
168  fetchScope().fetchFullPayload();
169  fetchScope().setAncestorRetrieval( ItemFetchScope::Parent );
170 }
171 
172 ContactGroupViewer::~ContactGroupViewer()
173 {
174  delete d;
175 }
176 
177 Akonadi::Item ContactGroupViewer::contactGroup() const
178 {
179  return ItemMonitor::item();
180 }
181 
182 void ContactGroupViewer::setContactGroup( const Akonadi::Item &group )
183 {
184  ItemMonitor::setItem( group );
185 }
186 
187 void ContactGroupViewer::setContactGroupFormatter( AbstractContactGroupFormatter *formatter )
188 {
189  if ( formatter == 0 )
190  d->mContactGroupFormatter = d->mStandardContactGroupFormatter;
191  else
192  d->mContactGroupFormatter = formatter;
193 }
194 
195 void ContactGroupViewer::itemChanged( const Item &item )
196 {
197  if ( !item.hasPayload<KABC::ContactGroup>() )
198  return;
199 
200  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
201  d->mCurrentGroupName = group.name();
202  d->mCurrentItem = item;
203 
204  if ( d->mExpandJob ) {
205  disconnect( d->mExpandJob, SIGNAL(result(KJob*)), this, SLOT(_k_expandResult(KJob*)) );
206  d->mExpandJob->kill();
207  }
208 
209  d->mExpandJob = new ContactGroupExpandJob( group );
210  connect( d->mExpandJob, SIGNAL(result(KJob*)), SLOT(_k_expandResult(KJob*)) );
211  d->mExpandJob->start();
212 }
213 
214 void ContactGroupViewer::itemRemoved()
215 {
216  d->mBrowser->clear();
217 }
218 
219 #include "contactgroupviewer.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:47:07 by doxygen 1.8.1.2 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.5 API Reference

Skip menu "kdepimlibs-4.9.5 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