adhoc.cpp

00001 /*
00002   Copyright (c) 2004-2005 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       m_disco->removeNodeHandler( XMLNS_ADHOC_COMMANDS );
00040     }
00041   }
00042 
00043   StringList Adhoc::handleDiscoNodeFeatures( const std::string& /*node*/ )
00044   {
00045     StringList features;
00046     features.push_back( XMLNS_ADHOC_COMMANDS );
00047     return features;
00048   }
00049 
00050   StringMap Adhoc::handleDiscoNodeItems( const std::string& node )
00051   {
00052     if( node.empty() )
00053     {
00054       StringMap item;
00055       item[XMLNS_ADHOC_COMMANDS] = "Ad-Hoc Commands";
00056       return item;
00057     }
00058     else if( node == XMLNS_ADHOC_COMMANDS )
00059     {
00060       return m_items;
00061     }
00062     else
00063     {
00064       StringMap item;
00065       return item;
00066     }
00067   }
00068 
00069   StringMap Adhoc::handleDiscoNodeIdentities( const std::string& node,
00070                                                                   std::string& name )
00071   {
00072     StringMap::const_iterator it = m_items.find( node );
00073     if( it != m_items.end() )
00074       name = (*it).second;
00075     else
00076       name = "Ad-Hoc Commands";
00077 
00078     StringMap ident;
00079     if( node == XMLNS_ADHOC_COMMANDS )
00080       ident["automation"] = "command-list";
00081     else
00082       ident["automation"] = "command-node";
00083     return ident;
00084   }
00085 
00086   bool Adhoc::handleIq( Stanza *stanza )
00087   {
00088     if( stanza->hasChild( "command" ) )
00089     {
00090       Tag *c = stanza->findChild( "command" );
00091       const std::string node = c->findAttribute( "node" );
00092       AdhocCommandProviderMap::const_iterator it = m_adhocCommandProviders.find( node );
00093       if( !node.empty() && ( it != m_adhocCommandProviders.end() ) )
00094       {
00095         (*it).second->handleAdhocCommand( node, c );
00096         return true;
00097       }
00098     }
00099     return false;
00100   }
00101 
00102   void Adhoc::registerAdhocCommandProvider( AdhocCommandProvider *acp, const std::string& command,
00103                                             const std::string& name )
00104   {
00105     m_disco->registerNodeHandler( this, command );
00106     m_adhocCommandProviders[command] = acp;
00107     m_items[command] = name;
00108   }
00109 
00110 }

Generated on Mon Jan 16 16:19:54 2006 for gloox by  doxygen 1.4.6