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

kopete/kopete

  • sources
  • kde-4.12
  • kdenetwork
  • kopete
  • kopete
  • identity
identitystatuswidget.cpp
Go to the documentation of this file.
1 /*
2  identitystatuswidget.cpp - Kopete identity status configuration widget
3 
4  Copyright (c) 2007-2009 by Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
5  Copyright (c) 2007 by Will Stephenson <wstephenson@kde.org>
6 
7  Kopete (c) 2003-2009 by the Kopete developers <kopete-devel@kde.org>
8 
9  *************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  *************************************************************************
17 */
18 
19 
20 #include "identitystatuswidget.h"
21 #include "ui_identitystatusbase.h"
22 #include "addaccountwizard.h"
23 
24 #include <KIcon>
25 #include <KMenu>
26 #include <KActionMenu>
27 #include <QTimeLine>
28 #include <QToolTip>
29 #include <QCursor>
30 #include <QUrl>
31 #include <QHelpEvent>
32 #include <KColorScheme>
33 #include <kopeteidentity.h>
34 #include <kopeteidentitymanager.h>
35 #include <kopeteaccount.h>
36 #include <kopeteaccountmanager.h>
37 #include <kopetecontact.h>
38 #include <kopeteprotocol.h>
39 #include <kopetestdaction.h>
40 #include <avatardialog.h>
41 #include <KDebug>
42 #include <kopeteuiglobal.h>
43 #include <KCMultiDialog>
44 
45 #include "kopetestatusrootaction.h"
46 
47 class IdentityStatusWidget::Private
48 {
49 public:
50  Kopete::Identity *identity;
51  Ui::IdentityStatusBase ui;
52  QTimeLine *timeline;
53  QString photoPath;
54  QHash<QListWidgetItem *,Kopete::Account *> accountHash;
55  bool dirty;
56 };
57 
58 IdentityStatusWidget::IdentityStatusWidget(Kopete::Identity *identity, QWidget *parent)
59 : QWidget(parent), d(new Private())
60 {
61  d->identity = 0;
62 
63  // animation for showing/hiding
64  d->timeline = new QTimeLine( 150, this );
65  d->timeline->setCurveShape( QTimeLine::EaseInOutCurve );
66  connect( d->timeline, SIGNAL(valueChanged(qreal)),
67  this, SLOT(slotAnimate(qreal)) );
68 
69  d->ui.setupUi(this);
70  d->ui.accounts->setContextMenuPolicy( Qt::CustomContextMenu );
71  QWidget::setVisible( false );
72 
73  setIdentity(identity);
74 
75  // user input signals
76  connect( d->ui.accounts, SIGNAL(customContextMenuRequested(QPoint)),
77  this, SLOT(showAccountContextMenu(QPoint)) );
78  connect( d->ui.accounts, SIGNAL(itemClicked(QListWidgetItem*)),
79  this, SLOT(slotAccountClicked(QListWidgetItem*)) );
80  connect( d->ui.accounts, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
81  this, SLOT(slotAccountDoubleClicked(QListWidgetItem*)) );
82  connect( d->ui.photo, SIGNAL(clicked()),
83  this, SLOT(slotPhotoClicked()));
84 
85  connect( Kopete::AccountManager::self(), SIGNAL(accountRegistered(Kopete::Account*)),
86  this, SLOT(slotAccountRegistered(Kopete::Account*)));
87  connect( Kopete::AccountManager::self(), SIGNAL(accountUnregistered(const Kopete::Account*)),
88  this, SLOT(slotAccountUnregistered(const Kopete::Account*)));
89 
90  connect( Kopete::IdentityManager::self(), SIGNAL(identityUnregistered(const Kopete::Identity*)),
91  this, SLOT(slotIdentityUnregistered(const Kopete::Identity*)));
92 
93  d->ui.accounts->viewport()->installEventFilter( this );
94 }
95 
96 IdentityStatusWidget::~IdentityStatusWidget()
97 {
98  delete d;
99 }
100 
101 void IdentityStatusWidget::setIdentity(Kopete::Identity *identity)
102 {
103  if (d->identity == identity)
104  return;
105 
106  if (d->identity)
107  {
108  // if we were showing an identity before, disconnect the signal to handle updates
109  disconnect( d->identity, SIGNAL(propertyChanged(Kopete::PropertyContainer*,QString,QVariant,QVariant)),
110  this, SLOT(slotIdentityPropertyChanged(Kopete::PropertyContainer*)) );
111  disconnect( d->identity, SIGNAL(identityChanged(Kopete::Identity*)),
112  this, SLOT(slotIdentityChanged(Kopete::Identity*)));
113  }
114  d->identity = identity;
115  load();
116 
117  if (d->identity)
118  {
119  // Handle identity changes
120  connect( d->identity, SIGNAL(propertyChanged(Kopete::PropertyContainer*,QString,QVariant,QVariant)),
121  this, SLOT(slotIdentityPropertyChanged(Kopete::PropertyContainer*)) );
122  connect( d->identity, SIGNAL(identityChanged(Kopete::Identity*)),
123  this, SLOT(slotIdentityChanged(Kopete::Identity*)));
124  }
125 }
126 
127 Kopete::Identity *IdentityStatusWidget::identity() const
128 {
129  return d->identity;
130 }
131 
132 void IdentityStatusWidget::setVisible( bool visible )
133 {
134  if ( visible == isVisible() )
135  return;
136 
137  // animate the widget disappearing
138  d->timeline->setDirection( visible ? QTimeLine::Forward
139  : QTimeLine::Backward );
140  d->timeline->start();
141 }
142 
143 bool IdentityStatusWidget::eventFilter( QObject *watched, QEvent *event )
144 {
145  if( event->type() == QEvent::ToolTip && watched == d->ui.accounts->viewport() )
146  {
147  QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
148  QListWidgetItem* item = d->ui.accounts->itemAt( helpEvent->pos() );
149  if ( item )
150  {
151  const Kopete::Account * account = d->accountHash.value( item, 0 );
152  if ( account )
153  item->setToolTip( account->myself()->toolTip() );
154  }
155  }
156  return QWidget::eventFilter( watched, event );
157 }
158 
159 void IdentityStatusWidget::slotAnimate(qreal amount)
160 {
161  if (amount == 0)
162  {
163  QWidget::setVisible( false );
164  return;
165  }
166 
167  if (amount == 1.0)
168  {
169  layout()->setSizeConstraint( QLayout::SetDefaultConstraint );
170  setFixedHeight( sizeHint().height() );
171  return;
172  }
173 
174  setFixedHeight( sizeHint().height() * amount );
175 
176  if (!isVisible())
177  QWidget::setVisible( true );
178 }
179 
180 void IdentityStatusWidget::load()
181 {
182  // clear
183  d->ui.accounts->clear();
184  d->accountHash.clear();
185 
186  if (!d->identity)
187  return;
188 
189  Kopete::Global::Properties *props = Kopete::Global::Properties::self();
190 
191  // photo
192  if (d->identity->hasProperty(props->photo().key()))
193  {
194  d->photoPath = d->identity->property(props->photo()).value().toString();
195  d->ui.photo->setIcon( QIcon( d->photoPath ) );
196  } else {
197  d->photoPath.clear();
198  d->ui.photo->setIcon( KIcon( "user-identity" ) );
199  }
200 
201  d->ui.identityName->setText(d->identity->label());
202 
203  foreach(Kopete::Account *a, d->identity->accounts()) {
204  addAccountItem( a );
205  }
206  if ( d->identity->accounts().isEmpty() ) {
207  new QListWidgetItem( KIcon("configure" ), i18nc("Button to open account configuration widget", "Click to add an account" ), d->ui.accounts );
208  }
209  resizeAccountListWidget();
210 }
211 
212 void IdentityStatusWidget::slotAccountRegistered( Kopete::Account *account )
213 {
214  if (account && account->identity() == d->identity && d->accountHash.isEmpty())
215  {
216  // Remove "Add account" placeholder
217  d->ui.accounts->clear();
218  }
219 
220  addAccountItem( account );
221  resizeAccountListWidget();
222 }
223 
224 void IdentityStatusWidget::slotAccountUnregistered( const Kopete::Account *account )
225 {
226  QListWidgetItem * item = 0;
227 
228  QHashIterator<QListWidgetItem*, Kopete::Account *> i( d->accountHash );
229  while ( i.hasNext() ) {
230  i.next();
231  Kopete::Account * candidate = i.value();
232  if ( candidate == account ) {
233  item = i.key();
234  }
235  }
236  if( !item )
237  return;
238  d->ui.accounts->takeItem( d->ui.accounts->row( item ) );
239  d->accountHash.remove( item );
240  delete item;
241 
242  if ( d->identity && d->identity->accounts().isEmpty() ) {
243  new QListWidgetItem( KIcon("configure" ), i18nc("Button to open account configuration widget", "Click to add an account" ), d->ui.accounts );
244  }
245  resizeAccountListWidget();
246 }
247 
248 void IdentityStatusWidget::addAccountItem( Kopete::Account *account )
249 {
250  // debug to diagnose if the account was created with the right identity. see comment in
251  // slotAccountRegistered
252  //kDebug() << "Adding Account item for identity: " << ( account->identity() ? account->identity()->label() : "" ) << ", showing identity " << ( d->identity ? d->identity->label() : "" )<< " in widget.";
253  if ( !account || ( account->identity() != d->identity ) ) {
254  return;
255  }
256 
257  connect( account->myself(),
258  SIGNAL(onlineStatusChanged(Kopete::Contact*,Kopete::OnlineStatus,Kopete::OnlineStatus)),
259  this, SLOT(slotAccountStatusIconChanged(Kopete::Contact*)) );
260 
261  QListWidgetItem * item = new QListWidgetItem( account->accountIcon(), account->accountLabel(), d->ui.accounts );
262  d->accountHash.insert( item, account );
263 
264  slotAccountStatusIconChanged( account->myself() );
265 }
266 
267 void IdentityStatusWidget::slotAccountStatusIconChanged( Kopete::Contact *contact )
268 {
270  Kopete::OnlineStatus status = contact->onlineStatus();
271  QListWidgetItem * item = 0;
272  QHashIterator<QListWidgetItem*, Kopete::Account *> i( d->accountHash );
273  while ( i.hasNext() ) {
274  i.next();
275  Kopete::Account * candidate = i.value();
276  if ( candidate == contact->account() ) {
277  item = i.key();
278  }
279  }
280  if( !item )
281  return;
282 
283  item->setIcon ( status.iconFor( contact->account() ) );
284 }
285 
286 void IdentityStatusWidget::showAccountContextMenu( const QPoint & point )
287 {
288  QListWidgetItem * item = d->ui.accounts->itemAt( point );
289  if ( item && !d->accountHash.isEmpty() ) {
290  Kopete::Account * account = d->accountHash[ item ];
291  if ( account ) {
292  KActionMenu *actionMenu = new KActionMenu( account->accountId(), account );
293 
294  if ( !account->hasCustomStatusMenu() )
295  Kopete::StatusRootAction::createAccountStatusActions( account, actionMenu );
296 
297  account->fillActionMenu( actionMenu );
298 
299  actionMenu->menu()->exec( d->ui.accounts->mapToGlobal( point ) );
300  delete actionMenu;
301  }
302  }
303 }
304 
305 void IdentityStatusWidget::slotAccountClicked( QListWidgetItem * item )
306 {
307  Q_UNUSED( item );
308 
309  if ( d->identity && d->identity->accounts().isEmpty() )
310  {
311  Q_ASSERT(d->accountHash.isEmpty());
312  // "Add an account" item
313  AddAccountWizard *addwizard = new AddAccountWizard( this, true );
314  addwizard->setIdentity(identity());
315  addwizard->show();
316  }
317 }
318 
319 void IdentityStatusWidget::slotAccountDoubleClicked( QListWidgetItem * item )
320 {
321  //Account toggles connect/disconnect at double click!
322  if ( item && !d->accountHash.isEmpty() )
323  {
324  Kopete::Account * account = d->accountHash[ item ];
325  if ( account ) {
326  if ( account->myself()->onlineStatus().status() == Kopete::OnlineStatus::Offline )
327  {
328  Kopete::OnlineStatus s(Kopete::OnlineStatus::Online);
329  account->connect( s );
330  } else {
331  account->disconnect();
332  }
333  }
334  }
335 }
336 
337 void IdentityStatusWidget::slotPhotoClicked()
338 {
339  if ( !d->identity )
340  return;
341 
342  bool ok, changed = false;
343  const QString photoPath = Kopete::UI::AvatarDialog::getAvatar( this, d->photoPath, &ok);
344  if ( ok ) {
345  Kopete::Global::Properties *props = Kopete::Global::Properties::self();
346  if ( photoPath.isEmpty() ) {
347  d->identity->removeProperty( props->photo() );
348  d->photoPath.clear();
349  changed = true;
350  }
351  else if ( photoPath != d->photoPath ) {
352  d->identity->setProperty(props->photo(), photoPath);
353  d->photoPath = photoPath;
354  changed = true;
355  }
356 
357  if ( changed ) {
358  load();
359  }
360  }
361 }
362 
363 void IdentityStatusWidget::resizeAccountListWidget()
364 {
365  int frameWidth = d->ui.accounts->frameWidth();
366  int itemHeight = d->ui.accounts->sizeHintForRow( 0 );
367  int itemCount = d->ui.accounts->count();
368  d->ui.accounts->setFixedHeight( 2 * frameWidth
369  + itemHeight * ( itemCount ? itemCount : 1 ) );
370  layout()->invalidate();
371  setFixedHeight( sizeHint().height() );
372  //adjustSize();
373 }
374 
375 void IdentityStatusWidget::slotIdentityUnregistered( const Kopete::Identity* identity )
376 {
377  if ( identity == d->identity )
378  {
379  disconnect( identity );
380  hide();
381  setIdentity( Kopete::IdentityManager::self()->defaultIdentity() );
382  }
383 }
384 
385 void IdentityStatusWidget::slotIdentityPropertyChanged(Kopete::PropertyContainer *container)
386 {
387  Kopete::Identity *identity = dynamic_cast<Kopete::Identity*>(container);
388 
389  slotIdentityChanged(identity);
390 }
391 
392 void IdentityStatusWidget::slotIdentityChanged(Kopete::Identity *identity)
393 {
394  // if it is not the identity currently being shown, there is no need to update
395  if (identity != d->identity)
396  return;
397 
398  load();
399 }
400 
401 #include "identitystatuswidget.moc"
402 // vim: set noet ts=4 sts=4 sw=4:
403 
IdentityStatusWidget::IdentityStatusWidget
IdentityStatusWidget(Kopete::Identity *ident, QWidget *parent=0)
Default constructor for the identity status widget.
Definition: identitystatuswidget.cpp:58
IdentityStatusWidget::~IdentityStatusWidget
~IdentityStatusWidget()
Definition: identitystatuswidget.cpp:96
QWidget
addaccountwizard.h
QObject
kopetestatusrootaction.h
identitystatuswidget.h
Kopete::StatusRootAction::createAccountStatusActions
static void createAccountStatusActions(Account *account, KActionMenu *parent, QAction *before=0)
insert "setStatus" actions from the given account to the specified actionMenu.
Definition: kopetestatusrootaction.cpp:80
QListWidgetItem
KActionMenu
IdentityStatusWidget::eventFilter
bool eventFilter(QObject *watched, QEvent *event)
Definition: identitystatuswidget.cpp:143
IdentityStatusWidget::setVisible
virtual void setVisible(bool visible)
Definition: identitystatuswidget.cpp:132
IdentityStatusWidget::identity
Kopete::Identity * identity() const
This method returns the identity currently being managed by this widget.
Definition: identitystatuswidget.cpp:127
AddAccountWizard
Definition: addaccountwizard.h:43
IdentityStatusWidget::setIdentity
void setIdentity(Kopete::Identity *identity)
Definition: identitystatuswidget.cpp:101
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/kopete

Skip menu "kopete/kopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

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