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

akonadi

  • 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 <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 
77  mContactGroupFormatter->setContactGroup( group );
78 
79  QList<QVariantMap> additionalFields;
80 
81  if ( !mCurrentAddressBookName.isEmpty() ) {
82  QVariantMap addressBookName;
83  addressBookName.insert( QLatin1String( "title" ), i18n( "Address Book" ) );
84  addressBookName.insert( QLatin1String( "value" ), mCurrentAddressBookName );
85 
86  additionalFields << addressBookName;
87  }
88 
89  mContactGroupFormatter->setAdditionalFields( additionalFields );
90 
91  mBrowser->setHtml( mContactGroupFormatter->toHtml() );
92  }
93 
94  void slotMailClicked( const QString&, const QString &email )
95  {
96  QString name, address;
97 
98  // remove the 'mailto:' and split into name and email address
99  KABC::Addressee::parseEmailAddress( email.mid( 7 ), name, address );
100 
101  emit mParent->emailClicked( name, address );
102  }
103 
104  void _k_expandResult( KJob *job )
105  {
106  mExpandJob = 0;
107 
108  if ( !job->error() ) {
109  ContactGroupExpandJob *expandJob = qobject_cast<ContactGroupExpandJob*>( job );
110  mCurrentContacts = expandJob->contacts();
111  }
112 
113  // stop any running fetch job
114  if ( mParentCollectionFetchJob ) {
115  mParent->disconnect( mParentCollectionFetchJob, SIGNAL(result(KJob*)), mParent, SLOT(slotParentCollectionFetched(KJob*)) );
116  delete mParentCollectionFetchJob;
117  mParentCollectionFetchJob = 0;
118  }
119 
120  mParentCollectionFetchJob = new CollectionFetchJob( mCurrentItem.parentCollection(), CollectionFetchJob::Base, mParent );
121  mParent->connect( mParentCollectionFetchJob, SIGNAL(result(KJob*)), SLOT(slotParentCollectionFetched(KJob*)) );
122  }
123 
124  void slotParentCollectionFetched( KJob *job )
125  {
126  mParentCollectionFetchJob = 0;
127  mCurrentAddressBookName.clear();
128 
129  if ( !job->error() ) {
130  CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
131  if ( !fetchJob->collections().isEmpty() ) {
132  const Collection collection = fetchJob->collections().first();
133  if ( collection.hasAttribute<EntityDisplayAttribute>() ) {
134  mCurrentAddressBookName = collection.attribute<EntityDisplayAttribute>()->displayName();
135  } else {
136  mCurrentAddressBookName = collection.name();
137  }
138  }
139  }
140 
141  updateView();
142  }
143 
144  ContactGroupViewer *mParent;
145  TextBrowser *mBrowser;
146  QString mCurrentGroupName;
147  KABC::Addressee::List mCurrentContacts;
148  QString mCurrentAddressBookName;
149  Item mCurrentItem;
150  ContactGroupExpandJob *mExpandJob;
151  CollectionFetchJob *mParentCollectionFetchJob;
152  AbstractContactGroupFormatter *mStandardContactGroupFormatter;
153  AbstractContactGroupFormatter *mContactGroupFormatter;
154 };
155 
156 ContactGroupViewer::ContactGroupViewer( QWidget *parent )
157  : QWidget( parent ), d( new Private( this ) )
158 {
159  QVBoxLayout *layout = new QVBoxLayout( this );
160  layout->setMargin( 0 );
161 
162  d->mBrowser->setNotifyClick( true );
163 
164  connect( d->mBrowser, SIGNAL(mailClick(QString,QString)),
165  this, SLOT(slotMailClicked(QString,QString)) );
166 
167  layout->addWidget( d->mBrowser );
168 
169  // always fetch full payload for contact groups
170  fetchScope().fetchFullPayload();
171  fetchScope().setAncestorRetrieval( ItemFetchScope::Parent );
172 }
173 
174 ContactGroupViewer::~ContactGroupViewer()
175 {
176  delete d;
177 }
178 
179 Akonadi::Item ContactGroupViewer::contactGroup() const
180 {
181  return ItemMonitor::item();
182 }
183 
184 void ContactGroupViewer::setContactGroup( const Akonadi::Item &group )
185 {
186  ItemMonitor::setItem( group );
187 }
188 
189 void ContactGroupViewer::setContactGroupFormatter( AbstractContactGroupFormatter *formatter )
190 {
191  if ( formatter == 0 ) {
192  d->mContactGroupFormatter = d->mStandardContactGroupFormatter;
193  } else {
194  d->mContactGroupFormatter = formatter;
195  }
196 }
197 
198 void ContactGroupViewer::itemChanged( const Item &item )
199 {
200  if ( !item.hasPayload<KABC::ContactGroup>() ) {
201  return;
202  }
203 
204  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
205  d->mCurrentGroupName = group.name();
206  d->mCurrentItem = item;
207 
208  if ( d->mExpandJob ) {
209  disconnect( d->mExpandJob, SIGNAL(result(KJob*)), this, SLOT(_k_expandResult(KJob*)) );
210  d->mExpandJob->kill();
211  }
212 
213  d->mExpandJob = new ContactGroupExpandJob( group );
214  connect( d->mExpandJob, SIGNAL(result(KJob*)), SLOT(_k_expandResult(KJob*)) );
215  d->mExpandJob->start();
216 }
217 
218 void ContactGroupViewer::itemRemoved()
219 {
220  d->mBrowser->clear();
221 }
222 
223 #include "moc_contactgroupviewer.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