File indexing completed on 2024-04-14 04:01:46

0001  /*
0002   * jabberresource.cpp
0003   *
0004   * Copyright (c) 2005-2006 by Michaƫl Larouche <larouche@kde.org>
0005   * Copyright (c) 2004 by Till Gerken <till@tantalo.net>
0006   *
0007   * Kopete    (c) 2001-2006 by the Kopete developers  <kopete-devel@kde.org>
0008   *
0009   * *************************************************************************
0010   * *                                                                       *
0011   * * This program is free software; you can redistribute it and/or modify  *
0012   * * it under the terms of the GNU General Public License as published by  *
0013   * * the Free Software Foundation; either either version 2
0014    of the License, or (at your option) any later version.of the License, or     *
0015   * * (at your option) any later version.                                   *
0016   * *                                                                       *
0017   * *************************************************************************
0018   */
0019 
0020 #include "jabberresource.h"
0021 
0022 // Qt includes
0023 #include <QTimer>
0024 
0025 // KDE includes
0026 #include "jabber_protocol_debug.h"
0027 
0028 // libiris includes
0029 #include <im.h>
0030 #include <xmpp_tasks.h>
0031 
0032 // Kopete includes
0033 #include "jabberprotocol.h"
0034 #include "jabberaccount.h"
0035 #include "jabbercapabilitiesmanager.h"
0036 
0037 class JabberResource::Private
0038 {
0039 public:
0040     Private( JabberAccount *t_account, const XMPP::Jid &t_jid, const XMPP::Resource &t_resource )
0041      : account(t_account), jid(t_jid), resource(t_resource), capsEnabled(false)
0042     {
0043         // Make sure the resource is always set.
0044         jid.setResource(resource.name());
0045     }
0046 
0047     JabberAccount *account;
0048     XMPP::Jid jid;
0049     XMPP::Resource resource;
0050     
0051     QString clientName, clientSystem;
0052     XMPP::Features supportedFeatures;
0053     bool capsEnabled;
0054 };
0055 
0056 JabberResource::JabberResource ( JabberAccount *account, const XMPP::Jid &jid, const XMPP::Resource &resource )
0057     : d( new Private(account, jid, resource) )
0058 {
0059     d->capsEnabled = account->protocol()->capabilitiesManager()->capabilitiesEnabled(jid);
0060 
0061     if ( account->isConnected () )
0062     {
0063         QTimer::singleShot ( account->client()->getPenaltyTime () * 1000, this, SLOT (slotGetTimedClientVersion()) );
0064         if(!d->capsEnabled)
0065         {
0066             QTimer::singleShot ( account->client()->getPenaltyTime () * 1000, this, SLOT (slotGetDiscoCapabilties()) );
0067         }
0068     }
0069 }
0070 
0071 JabberResource::~JabberResource ()
0072 {
0073     delete d;
0074 }
0075 
0076 const XMPP::Jid &JabberResource::jid () const
0077 {
0078     return d->jid;
0079 }
0080 
0081 const XMPP::Resource &JabberResource::resource () const
0082 {
0083     return d->resource;
0084 }
0085 
0086 void JabberResource::setResource ( const XMPP::Resource &resource )
0087 {
0088     d->resource = resource;
0089 
0090     // Check if the caps are now available.
0091     d->capsEnabled = d->account->protocol()->capabilitiesManager()->capabilitiesEnabled(d->jid);
0092 
0093     emit updated( this );
0094 }
0095 
0096 const QString &JabberResource::clientName () const
0097 {
0098     return d->clientName;
0099 }
0100 
0101 const QString &JabberResource::clientSystem () const
0102 {
0103     return d->clientSystem;
0104 }
0105 
0106 XMPP::Features JabberResource::features() const
0107 {
0108     if(d->capsEnabled)
0109     {
0110         return d->account->protocol()->capabilitiesManager()->features(d->jid);
0111     }
0112     else
0113     {
0114         return d->supportedFeatures;
0115     }
0116 }
0117 
0118 void JabberResource::slotGetTimedClientVersion ()
0119 {
0120     if ( d->account->isConnected () )
0121     {
0122         qCDebug(JABBER_PROTOCOL_LOG) << "Requesting client version for " << d->jid.full ();
0123 
0124         // request client version
0125         XMPP::JT_ClientVersion *task = new XMPP::JT_ClientVersion ( d->account->client()->rootTask () );
0126         // signal to ourselves when the vCard data arrived
0127         QObject::connect ( task, SIGNAL (finished()), this, SLOT (slotGotClientVersion()) );
0128         task->get ( d->jid );
0129         task->go ( true );
0130     }
0131 }
0132 
0133 void JabberResource::slotGotClientVersion ()
0134 {
0135     XMPP::JT_ClientVersion *clientVersion = (XMPP::JT_ClientVersion *) sender ();
0136 
0137     if ( clientVersion->success () )
0138     {
0139         d->clientName = clientVersion->name () + ' ' + clientVersion->version ();
0140         d->clientSystem = clientVersion->os ();
0141 
0142         emit updated ( this );
0143     }
0144 }
0145 
0146 void JabberResource:: slotGetDiscoCapabilties ()
0147 {
0148     if ( d->account->isConnected () )
0149     {
0150         qCDebug(JABBER_PROTOCOL_LOG) << "Requesting Client Features for " << d->jid.full ();
0151 
0152         XMPP:: JT_DiscoInfo *task = new XMPP::JT_DiscoInfo ( d->account->client()->rootTask () );
0153         // Retrive features when service discovery is done.
0154         QObject::connect ( task, SIGNAL (finished()), this, SLOT (slotGotDiscoCapabilities()) );
0155         task->get ( d->jid);
0156         task->go ( true );
0157     }
0158 }
0159 
0160 void JabberResource::slotGotDiscoCapabilities ()
0161 {
0162     XMPP::JT_DiscoInfo *discoInfo = (XMPP::JT_DiscoInfo *) sender ();
0163 
0164     if ( discoInfo->success () )
0165     {
0166         d->supportedFeatures = discoInfo->item().features();
0167         
0168         emit updated ( this );
0169     }
0170 }
0171 
0172 #include "moc_jabberresource.cpp"