File indexing completed on 2024-04-21 04:04:41

0001  /*
0002     jabbercapabilitiesmanager.h - Manage entity capabilities(JEP-0115) pool.
0003 
0004     Copyright (c) 2006      by Michaël Larouche     <larouche@kde.org>
0005     Copyright     2006      by Tommi Rantala <tommi.rantala@cs.helsinki.fi>
0006 
0007     Kopete    (c) 2001-2006 by the Kopete developers <kopete-devel@kde.org>
0008 
0009     Imported from caps.cpp from Psi:
0010     Copyright (C) 2005  Remko Troncon
0011 
0012     *************************************************************************
0013     *                                                                       *
0014     * This program is free software; you can redistribute it and/or modify  *
0015     * it under the terms of the GNU General Public License as published by  *
0016     * the Free Software Foundation; either either version 2
0017    of the License, or (at your option) any later version.of the License, or     *
0018     * (at your option) any later version.                                   *
0019     *                                                                       *
0020     *************************************************************************
0021 */
0022 #ifndef JABBERCAPABILITIESMANAGER_H
0023 #define JABBERCAPABILITIESMANAGER_H
0024 
0025 #include <QPair>
0026 #include <QList>
0027 #include <QDate>
0028 
0029 #include <QStringList>
0030 #include <QDomElement>
0031 
0032 #include <im.h>
0033 #include <xmpp.h>
0034 
0035 using namespace XMPP;
0036 
0037 class JabberAccount;
0038 
0039 /**
0040  * @brief Manage Jabber entity capabilities (JEP-0115)
0041  * @author Michaël Larouche <larouche@kde.org>
0042  * @author Remko Troncon
0043  */
0044 class JabberCapabilitiesManager : public QObject
0045 {
0046     Q_OBJECT
0047 public:
0048     /**
0049      * Construct
0050      */
0051     JabberCapabilitiesManager();
0052     ~JabberCapabilitiesManager();
0053 
0054     /**
0055      * Load cached information from local file.
0056      */
0057     void loadCachedInformation();
0058 
0059     /**
0060      * Check if the jid support Entity capabitilies.
0061      * @param jid JID to check.
0062      * @return true if the jid support entity capabitilies.
0063      */
0064     bool capabilitiesEnabled(const Jid& jid) const;
0065 
0066     /**
0067      * Remove account from manager.
0068      */
0069     void removeAccount(JabberAccount *account);
0070 
0071     /**
0072      * Return the features supported for the JID.
0073      */
0074     XMPP::Features features(const Jid& jid) const;
0075     /**
0076      * Return the client name for the current JID.
0077      */
0078     QString clientName(const Jid& jid) const;
0079     /**
0080      * Return the client version for the current JID.
0081      */
0082     QString clientVersion(const Jid& jid) const;
0083 
0084 signals:
0085     void capabilitiesChanged(const XMPP::Jid &jid);
0086 
0087 public slots:
0088     /**
0089      * Update if necessary the capabities for the JID passed in args.
0090      * Caps are received in Presence messages so that's why we are
0091      * passing a XMPP::Status object.
0092      *
0093      * @param jid JID that capabilities was updated.
0094      * @param status The XMPP::Status that contain the caps.
0095      */
0096     void updateCapabilities(JabberAccount *account, const XMPP::Jid &jid, const XMPP::Status &status);
0097 
0098 private slots:
0099     /**
0100      * @brief Called when a reply to disco#info request was received.
0101      * If the result was successful, the resulting features are recorded in the
0102      * features database for the requested node, and all the affected jids are
0103      * put in the queue for update notification.
0104      * If the result was unsuccessful, another jid with the same capabilities is
0105      * selected and sent a disco#info query.
0106      */
0107     void discoRequestFinished();
0108 
0109 private:
0110     /**
0111      * @brief Sends a disco#info request to a given node of a jid through an account.
0112      * When the request is finished, the discoRequestFinished() slot is called.
0113      *
0114      * @param account The account through which to send the disco request.
0115      * @param jid The target entity's JID 
0116      * @param node The target disco#info node
0117      */
0118     void requestDiscoInfo(JabberAccount *account, const Jid& jid, const QString& node);
0119 
0120     /**
0121      * Save capabilities information to disk.
0122      */
0123     void saveInformation();
0124 
0125     class Capabilities;
0126     typedef QList<Capabilities> CapabilitiesList;
0127     /**
0128      * @brief A class representing an entity capability specification.
0129      * An entity capability is a combination of a node, a version, and a set of
0130      * extensions.
0131      */
0132     class Capabilities
0133     {
0134         public:
0135             /**
0136              * Default constructor.
0137              */
0138             Capabilities();
0139             /**
0140              * Define capabilities.
0141              * @param node the node
0142              * @param version the version
0143              * @param extensions the list of extensions (separated by spaces)
0144              */
0145             Capabilities(const QString &node, const QString &version, const QString &extensions);
0146             /**
0147              * Returns the node of the capabilities specification.
0148              */
0149             const QString& node() const;
0150             /**
0151              * @brief Returns the version of the capabilities specification.
0152              */
0153             const QString& version() const;
0154             /**
0155              * @brief Returns the extensions of the capabilities specification.
0156              */
0157             const QString& extensions() const; 
0158             /**
0159              * \brief Flattens the caps specification into the set of 'simple' specifications.
0160              * A 'simple' specification is a specification with exactly one extension,
0161              * or with the version number as the extension.
0162              *
0163              * Example: A caps specification with node=http://psi-im.org, version=0.10,
0164              * and ext='achat vchat' would be expanded into the following list of specs:
0165              *  node=http://psi-im.org, ver=0.10, ext=0.10
0166              *  node=http://psi-im.org, ver=0.10, ext=achat
0167              *  node=http://psi-im.org, ver=0.10, ext=vchat
0168              */
0169             CapabilitiesList flatten() const;
0170     
0171             bool operator==(const Capabilities&) const;
0172             bool operator!=(const Capabilities&) const;
0173             bool operator<(const Capabilities&) const;
0174                 
0175         private:
0176             QString m_node, m_version, m_extensions;
0177     };
0178 
0179     class CapabilitiesInformation
0180     {
0181         public:
0182             CapabilitiesInformation();
0183             const QStringList& features() const;
0184             const DiscoItem::Identities& identities() const;
0185             QStringList jids() const;
0186             bool discovered() const;
0187             int pendingRequests() const;
0188 
0189             void reset();
0190             void removeAccount(JabberAccount* acc);
0191             void removeJid(const Jid&);
0192             void addJid(const Jid&, JabberAccount*);
0193             QPair<Jid,JabberAccount*> nextJid(const Jid&, const Task*);
0194             
0195             void setDiscovered(bool);
0196             void setPendingRequests(int);
0197             void setIdentities(const DiscoItem::Identities&);
0198             void setFeatures(const QStringList&);
0199             
0200             QDomElement toXml(QDomDocument *) const;
0201             void fromXml(const QDomElement&);
0202 
0203         protected:
0204             void updateLastSeen();
0205             
0206         private:
0207             bool m_discovered;
0208             int m_pendingRequests;
0209             QStringList m_features;
0210             DiscoItem::Identities m_identities;
0211 
0212             typedef QList<QPair<QString, JabberAccount*> > JidList;
0213             JidList m_jids;
0214 
0215             QDate m_lastSeen;
0216     };
0217 
0218     class Private;
0219     Private *d;
0220 };
0221 
0222 #endif