adhoc.cpp

00001 /*
00002   Copyright (c) 2004-2006 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #include "adhoc.h"
00015 #include "disco.h"
00016 #include "discohandler.h"
00017 #include "client.h"
00018 
00019 
00020 namespace gloox
00021 {
00022 
00023   Adhoc::Adhoc( ClientBase *parent, Disco *disco )
00024     : m_parent( parent ), m_disco( disco )
00025   {
00026     if( m_parent && m_disco )
00027     {
00028       m_parent->registerIqHandler( this, XMLNS_ADHOC_COMMANDS );
00029       m_disco->addFeature( XMLNS_ADHOC_COMMANDS );
00030       m_disco->registerNodeHandler( this, XMLNS_ADHOC_COMMANDS );
00031     }
00032   }
00033 
00034   Adhoc::~Adhoc()
00035   {
00036     if( m_parent )
00037     {
00038       m_parent->removeIqHandler( XMLNS_ADHOC_COMMANDS );
00039     }
00040     if( m_disco )
00041     {
00042       m_disco->removeNodeHandler( XMLNS_ADHOC_COMMANDS );
00043     }
00044   }
00045 
00046   StringList Adhoc::handleDiscoNodeFeatures( const std::string& /*node*/ )
00047   {
00048     StringList features;
00049     features.push_back( XMLNS_ADHOC_COMMANDS );
00050     return features;
00051   }
00052 
00053   StringMap Adhoc::handleDiscoNodeItems( const std::string& node )
00054   {
00055     if( node.empty() )
00056     {
00057       StringMap item;
00058       item[XMLNS_ADHOC_COMMANDS] = "Ad-Hoc Commands";
00059       return item;
00060     }
00061     else if( node == XMLNS_ADHOC_COMMANDS )
00062     {
00063       return m_items;
00064     }
00065     else
00066     {
00067       StringMap item;
00068       return item;
00069     }
00070   }
00071 
00072   StringMap Adhoc::handleDiscoNodeIdentities( const std::string& node, std::string& name )
00073   {
00074     StringMap::const_iterator it = m_items.find( node );
00075     if( it != m_items.end() )
00076       name = (*it).second;
00077     else
00078       name = "Ad-Hoc Commands";
00079 
00080     StringMap ident;
00081     if( node == XMLNS_ADHOC_COMMANDS )
00082       ident["automation"] = "command-list";
00083     else
00084       ident["automation"] = "command-node";
00085     return ident;
00086   }
00087 
00088   bool Adhoc::handleIq( Stanza *stanza )
00089   {
00090     if( stanza->hasChild( "command" ) )
00091     {
00092       Tag *c = stanza->findChild( "command" );
00093       const std::string node = c->findAttribute( "node" );
00094       AdhocCommandProviderMap::const_iterator it = m_adhocCommandProviders.find( node );
00095       if( !node.empty() && ( it != m_adhocCommandProviders.end() ) )
00096       {
00097         (*it).second->handleAdhocCommand( node, c, stanza->from(), stanza->id() );
00098         return true;
00099       }
00100     }
00101     return false;
00102   }
00103 
00104   bool Adhoc::handleIqID( Stanza * /*stanza*/, int /*context*/ )
00105   {
00106     return false;
00107   }
00108 
00109   void Adhoc::registerAdhocCommandProvider( AdhocCommandProvider *acp, const std::string& command,
00110                                             const std::string& name )
00111   {
00112     m_disco->registerNodeHandler( this, command );
00113     m_adhocCommandProviders[command] = acp;
00114     m_items[command] = name;
00115   }
00116 
00117   void Adhoc::removeAdhocCommandProvider( const std::string& command )
00118   {
00119     m_disco->removeNodeHandler( command );
00120     m_adhocCommandProviders.erase( command );
00121     m_items.erase( command );
00122   }
00123 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1