2 kopeteaccount.h - Kopete Account
4 Copyright (c) 2007 by Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
5 Copyright (c) 2003-2005 by Olivier Goffart <ogoffart@kde.org>
6 Copyright (c) 2003-2004 by Martijn Klingens <klingens@kde.org>
7 Copyright (c) 2004 by Richard Smith <kde@metafoo.co.uk>
9 Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
11 *************************************************************************
13 * This library is free software; you can redistribute it and/or *
14 * modify it under the terms of the GNU Lesser General Public *
15 * License as published by the Free Software Foundation; either *
16 * version 2 of the License, or (at your option) any later version. *
18 *************************************************************************
21 #ifndef KOPETEACCOUNT_H
22 #define KOPETEACCOUNT_H
24 #include "kopeteonlinestatus.h"
25 #include "kopetestatusmessage.h"
26 #include "kopete_export.h"
28 #include <QtCore/QObject>
29 #include <QtGui/QPixmap>
30 #include <QtCore/QHash>
32 #include <kconfiggroup.h>
47 class PropertyContainer
;
50 * The Kopete::Account class handles one account.
51 * Each protocol should subclass this class in its own custom accounts class.
52 * There are few pure virtual method that the protocol must implement. Examples are:
54 * \li \ref disconnect()
55 * \li \ref createContact()
57 * If your account requires a password, derive from @ref PasswordedAccount instead of this class.
59 * The accountId is an @em constant unique id, which represents the login.
60 * The @ref myself() contact is one of the most important contacts, which represents
61 * the user tied to this account. You must create this contact in the contructor of your
62 * account and pass it to @ref setMyself().
64 * All account data is saved to @ref KConfig. This includes the accountId, the autoconnect flag and
65 * the color. You can save more data using @ref configGroup()
67 * When you create a new account, you have to register it with the account manager by calling
68 * @ref AccountManager::registerAccount.
70 * @author Olivier Goffart <ogoffart\@kde.org>
72 class KOPETE_EXPORT Account
: public QObject
77 Q_PROPERTY( QString accountId READ accountId
)
78 Q_PROPERTY( bool excludeConnect READ excludeConnect WRITE setExcludeConnect
)
79 Q_PROPERTY( QColor color READ color WRITE setColor
)
80 Q_PROPERTY( QPixmap accountIcon READ accountIcon
)
81 Q_PROPERTY( bool isConnected READ isConnected
)
82 Q_PROPERTY( bool isAway READ isAway
)
83 Q_PROPERTY( bool suppressStatusNotification READ suppressStatusNotification
)
84 Q_PROPERTY( uint priority READ priority WRITE setPriority
)
88 * \brief Describes how the account was disconnected
90 * Manual means that the disconnection was done by the user and no reconnection
91 * will take place. Any other value will reconnect the account on disconnection.
92 * The case where the password is wrong will be handled differently.
93 * @see @ref disconnected
95 enum DisconnectReason
{
96 OtherClient
= -4, ///< connection went down because another client connected the same account
97 BadPassword
= -3, ///< connection failed because password was incorrect
98 BadUserName
= -2, ///< connection failed because user name was invalid / unknown
99 InvalidHost
= -1, ///< connection failed because host is unreachable
100 Manual
= 0, ///< the user disconnected normally
101 ConnectionReset
= 1, ///< the connection was lost
102 Unknown
= 99 ///< the reason for disconnection is unknown
106 * \brief Describes what should be done when the contact is added to a metacontact
107 * @sa @ref addContact()
110 ChangeKABC
= 0, ///< The KDE Address book may be updated
111 DontChangeKABC
= 1, ///< The KDE Address book will not be changed
112 Temporary
= 2 ///< The contact will not be added on the contact list
116 * \brief Describes what should be done when we set new OnlineStatus
117 * @sa @ref setOnlineStatus()
119 enum OnlineStatusOption
{
120 None
= 0x00, ///< Use the online status
121 KeepSpecialFlags
= 0x01 ///< Use the online status but keep special flags, e.g. Invisible
123 Q_DECLARE_FLAGS(OnlineStatusOptions
, OnlineStatusOption
)
126 * @param parent the protocol for this account. The account is a child object of the
127 * protocol, so it will be automatically deleted when the protocol is.
128 * @param accountID the unique ID of this account.
129 * @param name the name of this QObject.
131 Account(Protocol
*parent
, const QString
&accountID
);
135 * \return the Protocol for this account
137 Protocol
*protocol() const ;
140 * \return the unique ID of this account used as the login
142 QString
accountId() const;
145 * \return The label of this account, for the GUI
147 QString
accountLabel() const;
150 * \brief Get the priority of this account.
152 * Used for sorting and determining the preferred account to message a contact.
154 uint
priority() const;
157 * \brief Set the priority of this account.
159 * @note This method is called by the UI, and should not be called elsewhere.
161 void setPriority( uint priority
);
164 * \brief Set if the account should not log in automatically.
166 * This function can be used by the EditAccountPage. Kopete handles connection automatically.
167 * @sa @ref excludeConnect
169 void setExcludeConnect(bool);
172 * \brief Get if the account should not log in.
174 * @return @c true if the account should not be connected when connectAll at startup, @c false otherwise.
176 bool excludeConnect() const;
179 * \brief Get the color for this account.
181 * The color will be used to visually differentiate this account from other accounts on the
184 * \return the user color for this account
186 const QColor
color() const;
189 * \brief Set the color for this account.
191 * This is called by Kopete's account config page; you don't have to set the color yourself.
195 void setColor( const QColor
&color
);
198 * \brief Get the icon for this account.
200 * Generates an image of size @p size representing this account. The result is not cached.
202 * @param size the size of the icon. If the size is 0, the default size is used.
203 * @return the icon for this account, colored if needed
205 QPixmap
accountIcon( const int size
= 0 ) const;
208 * \brief change the account icon.
209 * by default the icon of an account is the protocol one, but it may be overide it.
210 * Set QString() to go back to the default (the protocol icon)
212 * this call will emit colorChanged()
214 void setCustomIcon( const QString
& );
217 * \brief return the icon base
218 * This is the custom account icon set with setIcon. if this icon is null, then the protocol icon is used
219 * don't use this function to get the icon that need to be displayed, use accountIcon
221 QString
customIcon() const;
224 * \brief Retrieve the identity this account belongs to
226 * \return a pointer to the Identity object this account belongs to.
228 * \see setIdentity().
230 Identity
* identity() const;
233 * \brief Sets the identity this account belongs to
235 * Setting the account to a new identity implies it to be removed from the
236 * identity it was previously associated.
238 * @param ident The identity this account should be associated to
239 * \return @c true if the identity was changed, @c false otherwise
241 * @note You should call the default implementation from your reimplementation
243 virtual bool setIdentity( Kopete::Identity
*ident
);
246 * \brief Retrieve the 'myself' contact.
248 * \return a pointer to the Contact object for this account
252 Contact
* myself() const;
255 * @brief Fill the menu with actions for this account
257 * You have to reimplement this method to add custom actions to the @p actionMenu which will
258 * be shown in the statusbar. It is the caller's responsibility to ensure the menu is deleted.
260 * The default implementation provides a generic menu, with actions generated from the protocol's
261 * registered statuses, and an action to show the account's settings dialog.
263 * You should call the default implementation from your reimplementation, and add more actions
264 * you require to the resulting action menu.
266 * @see OnlineStatusManager::registerOnlineStatus
268 virtual void fillActionMenu( KActionMenu
*actionMenu
);
271 * @brief Return true if account has custom status menu.
273 * You have to reimplement this method and return true if you don't want to have status menu in menu
274 * which will be shown in the statusbar
276 * The default implementation returns false.
278 virtual bool hasCustomStatusMenu() const;
281 * @brief Retrieve the list of contacts for this account
283 * The list is guaranteed to contain only contacts for this account,
284 * so you can safely use static_cast to your own derived contact class
287 const QHash
<QString
,Contact
*>& contacts();
290 * Indicates whether or not we should suppress status notifications
291 * for contacts belonging to this account.
293 * This is used when we just connected or disconnected, and every contact has their initial
296 * @return @c true if notifications should not be used, @c false otherwise
298 bool suppressStatusNotification() const;
301 * \brief Create a contact (creating a new metacontact if necessary)
303 * If a contact for this account with ID @p contactId is not already on the contact list,
304 * a new contact with that ID is created, and added to a new metacontact.
306 * If @p mode is @c ChangeKABC, MetaContact::updateKABC will be called on the resulting metacontact.
307 * If @p mode is @c Temporary, MetaContact::setTemporary will be called on the resulting metacontact,
308 * and the metacontact will not be added to @p group.
309 * If @p mode is @c DontChangeKABC, no additional action is carried out.
311 * @param contactId the @ref Contact::contactId of the contact to create
312 * @param displayName the displayname (alias) of the new metacontact. Leave as QString() if
313 * no alias is known, then by default, the nick will be taken as alias and tracked if changed.
314 * @param group the group to add the created metacontact to, or 0 for the top-level group.
315 * @param mode the mode used to add the contact. Use DontChangeKABC when deserializing.
316 * @return the new created metacontact or 0L if the operation failed
318 MetaContact
* addContact( const QString
&contactId
, const QString
&displayName
= QString(), Group
*group
= 0, AddMode mode
= DontChangeKABC
);
321 * @brief Create a new contact, adding it to an existing metacontact
323 * If a contact for this account with ID @p contactId is not already on the contact list,
324 * a new contact with that ID is created, and added to the metacontact @p parent.
326 * @param contactId the @ref Contact::contactId of the contact to create
327 * @param parent the parent metacontact (must not be 0)
328 * @param mode the mode used to add the contact. See addContact(const QString&,const QString&,Group*,AddMode) for details.
330 * @return @c true if creation of the contact succeeded or the contact was already in the list,
331 * @c false otherwise.
333 bool addContact( const QString
&contactId
, MetaContact
*parent
, AddMode mode
= DontChangeKABC
);
336 * @brief Indicate whether the account is connected at all.
338 * This is a convenience method that calls @ref Contact::isOnline() on @ref myself().
339 * This function is safe to call if @ref setMyself() has not been called yet.
341 * @see @ref isConnectedChanged()
343 bool isConnected() const;
346 * @brief Indicate whether the account is away.
348 * This is a convenience method that queries @ref Contact::onlineStatus() on @ref myself().
349 * This function is safe to call if @ref setMyself() has not been called yet.
354 * Return the @ref KConfigGroup used to write and read special properties
356 * "Protocol", "AccountId" , "Color", "AutoConnect", "Priority", "Enabled" , "Icon" are reserved keyword
357 * already in use in that group.
359 * for compatibility, try to not use key that start with a uppercase
361 KConfigGroup
*configGroup() const;
364 * @brief Remove the account from the server.
366 * Reimplement this if your protocol supports removing the accounts from the server.
367 * This function is called by @ref AccountManager::removeAccount typically when you remove the
368 * account on the account config page.
370 * You should add a confirmation message box before removing the account. The default
371 * implementation does nothing.
373 * @return @c false only if the user requested for the account to be deleted, and deleting the
374 * account failed. Returns @c true in all other cases.
376 virtual bool removeAccount();
379 * \return a pointer to the blacklist of the account
380 * @todo remove or implement correctly (BlackLister)
382 BlackLister
* blackLister();
385 * \return @c true if the contact with ID @p contactId is in the blacklist, @c false otherwise.
386 * @todo remove or implement correctly (BlackLister)
388 virtual bool isBlocked( const QString
&contactId
);
392 * \brief Set the 'myself' contact.
394 * This contact must be defined for every account, because it holds the online status
395 * of the account. You must call this function in the constructor of your account.
397 * The myself contact can't be deleted as long as the account still exists. The myself
398 * contact is used as a member of every ChatSession involving this account. myself's
399 * contactId should be the accountID. The online status of the myself contact represents
400 * the account's status.
402 * The myself should have the @ref ContactList::myself() as parent metacontact
405 void setMyself( Contact
*myself
);
408 * \brief Create a new contact in the specified metacontact
410 * You shouldn't ever call this method yourself. To add contacts, use @ref addContact().
412 * This method is called by @ref addContact(). In this method, you should create the
413 * new custom @ref Contact, using @p parentContact as the parent.
415 * If the metacontact is not temporary and the protocol supports it, you can add the
416 * contact to the server.
418 * @param contactId the ID of the contact to create
419 * @param parentContact the metacontact to add this contact to
420 * @return @c true if creating the contact succeeded, @c false on failure.
422 virtual bool createContact( const QString
&contactId
, MetaContact
*parentContact
) =0;
426 * \brief Sets the account label
428 * @param label The label to set
430 void setAccountLabel( const QString
&label
);
434 * \brief The service has been disconnected
436 * You have to call this method when you are disconnected. Depending on the value of
437 * @p reason, this function may attempt to reconnect to the server.
439 * - BadPassword will ask again for the password
440 * - OtherClient will show a message box
442 * @param reason the reason for the disconnection.
444 virtual void disconnected( Kopete::Account::DisconnectReason reason
);
447 * @brief Sets the online status of all contacts in this account to the same value
449 * Some protocols do not provide status-changed events for all contacts when an account
450 * becomes connected or disconnected. For such protocols, this function may be useful
451 * to set all contacts offline.
453 * Calls @ref Kopete::Contact::setOnlineStatus on all contacts of this account (except the
454 * @ref myself() contact), passing @p status as the status.
456 * @param status the status to set all contacts of this account except @ref myself() to.
458 void setAllContactsStatus( const Kopete::OnlineStatus
&status
);
462 * The color of the account has been changed
464 * also emitted when the icon change
465 * @todo probably rename to accountIconChanged
467 void colorChanged( const QColor
& );
470 * Emitted when the account is deleted.
471 * @warning emitted in the Account destructor. It is not safe to call any functions on @p account.
473 void accountDestroyed( const Kopete::Account
*account
);
476 * Emitted whenever @ref isConnected() changes.
478 void isConnectedChanged();
483 * Reads the configuration information of the account from KConfig.
490 * Register a new Contact with the account. This should be called @em only from the
491 * @ref Contact constructor, not from anywhere else (not even a derived class).
493 void registerContact( Contact
*c
);
497 * @brief Go online for this service.
499 * @param initialStatus is the status to connect with. If it is an invalid status for this
500 * account, the default online for the account should be used.
501 * @todo probably deprecate in favor of setOnlineStatus
503 virtual void connect( const Kopete::OnlineStatus
& initialStatus
= OnlineStatus() ) = 0;
506 * @brief Go offline for this service.
508 * If the service is connecting, you should abort the connection.
510 * You should call the @ref disconnected function from this function.
511 * @todo probably deprecate in favor of setOnlineStatus
513 virtual void disconnect( ) = 0 ;
517 * Reimplement this function to set the online status
518 * @param status is the new status
519 * @param reason is the status message to set.
520 * @param options specify how the status should be set
521 * @note If needed, you need to connect. if the offline status is given, you should disconnect
523 virtual void setOnlineStatus( const Kopete::OnlineStatus
& status
,
524 const Kopete::StatusMessage
&reason
= Kopete::StatusMessage(),
525 const OnlineStatusOptions
& options
= None
) = 0;
527 * Reimplement this function to set the status message(with metadata).
528 * You should use this method to set the status message instead of using setOnlineStatus.
529 * @param statusMessage is the status message to set. (Use Kopete::StatusMessage).
531 virtual void setStatusMessage( const Kopete::StatusMessage
&statusMessage
) = 0;
534 * Display the edit account widget for the account
536 void editAccount( QWidget
* parent
= 0L );
539 * Add a user to the blacklist. The default implementation calls
540 * blackList()->addContact( contactId )
542 * @param contactId the contact to be added to the blacklist
543 * @todo remove or implement correctly (BlackLister)
545 virtual void block( const QString
&contactId
);
548 * Remove a user from the blacklist. The default implementation calls
549 * blackList()->removeContact( contactId )
551 * @param contactId the contact to be removed from the blacklist
552 * @todo remove or implement correctly (BlackLister)
554 virtual void unblock( const QString
&contactId
);
558 * Restore online status and status message on reconnect.
560 virtual void reconnect();
563 * Track the deletion of a Contact and clean up
565 void contactDestroyed( Kopete::Contact
* );
568 * The @ref myself() contact's online status changed.
570 void slotOnlineStatusChanged( Kopete::Contact
*contact
, const Kopete::OnlineStatus
&newStatus
, const Kopete::OnlineStatus
&oldStatus
);
573 * The @ref myself() contact's property changed.
575 void slotContactPropertyChanged( Kopete::PropertyContainer
*, const QString
&, const QVariant
&, const QVariant
& );
578 * Stop the suppression of status notification (connected to a timer)
580 void slotStopSuppression();
586 Q_DECLARE_OPERATORS_FOR_FLAGS(Account::OnlineStatusOptions
)
588 } //END namespace Kopete