Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

jabberoox-agent.cpp

00001 /* jabberoox-agent.cc
00002  * Jabber agent support
00003  *
00004  * Original Code Copyright (C) 1999-2001 Dave Smith (dave@jabber.org)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  * Contributor(s): Julian Missig
00021  *
00022  * This Original Code has been modified by IBM Corporation. Modifications 
00023  * made by IBM described herein are Copyright (c) International Business 
00024  * Machines Corporation, 2002.
00025  *
00026  * Date             Modified by     Description of modification
00027  * 01/20/2002       IBM Corp.       Updated to libjudo 1.1.1
00028  * 2002-03-05       IBM Corp.       Updated to libjudo 1.1.5
00029  */
00030 
00031 #include "jabberoox.hh"
00032 #include <judo.hpp>
00033 #include <session.hh>
00034 #include <sigc++/object_slot.h>
00035 #include <sigc++/slot.h>
00036 
00037 namespace jabberoo {
00038 
00039 // ---------------------------------------------------------
00040 //
00041 // Agent methods
00042 //
00043 // ---------------------------------------------------------
00044 Agent::Agent(const Agent& a)
00045      : _session(a._session), _jid(a._jid), _name(a._name),
00046        _desc(a._desc), _service(a._service), _transport(a._transport),
00047        _registerable(a._registerable), _searchable(a._searchable),
00048        _subagents(a._subagents)
00049 {
00050      if (_name.empty())
00051           _name = "No name available (" + a._jid + ")";
00052 }
00053 
00054 Agent::Agent(const std::string& jid, const judo::Element& baseElement, Session& s)
00055      : _jid(jid), _session(s)
00056 {
00057      _name      = baseElement.getChildCData("name");
00058      _desc      = baseElement.getChildCData("description");
00059      _service   = baseElement.getChildCData("service");
00060      _transport = baseElement.getChildCData("transport");
00061      _registerable   = (baseElement.findElement("register") != NULL);
00062      _searchable     = (baseElement.findElement("search") != NULL);
00063      _subagents      = (baseElement.findElement("agents") != NULL);
00064      _gc_capable     = (baseElement.findElement("groupchat") != NULL);
00065      _fetchrequested = false;
00066 }
00067 
00068 Agent::~Agent()
00069 {}
00070 
00071 void Agent::requestRegister(ElementCallbackFunc f)
00072 {
00073      _session.queryNamespace("jabber:iq:register", f, _jid);
00074 }
00075 
00076 void Agent::requestSearch(ElementCallbackFunc f)
00077 {
00078      _session.queryNamespace("jabber:iq:search", f, _jid);
00079 }
00080 
00081 void Agent::fetch()
00082 {
00083      if ( _subagents && empty() && !_fetchrequested )
00084      {
00085           // Release existing agents
00086           clear();
00087           // Set the fetch request flag
00088           _fetchrequested = true;
00089           // Request agents
00090           _session.queryNamespace("jabber:iq:agents", SigC::slot(*this, &Agent::on_fetch), _jid);
00091      }
00092 }         
00093 
00094 void Agent::on_fetch(const judo::Element& iq)
00095 {
00096      // Reset fetch request flag
00097      _fetchrequested = false;
00098 
00099      // IQ should contain an <iq> tag with one child tag <query>; within <query>,
00100      // there should be 1 or more <agent> tags -- these tags must be parsed into
00101      // agent objects and stored internally to "this" agent
00102      const judo::Element* query = iq.findElement("query");
00103      if (query == NULL)
00104           return;
00105 
00106      if (iq.cmpAttrib("type", "result"))
00107      {
00108           // Walk the <query> tag's children
00109           judo::Element::const_iterator it = query->begin();
00110           for (; it != query->end(); it++)
00111           {
00112                if ((*it)->getType() != judo::Node::ntElement)
00113                     continue;
00114                // Cast the child judo::element into a tag
00115                judo::Element& t = *static_cast<judo::Element*>(*it);
00116                
00117                // Ensure this is an agent tag
00118                if (t.getName() == "agent")
00119                            push_back(agentPtr(new Agent(t.getAttrib("jid"), t, _session)) );
00120           }
00121 
00122           // Notify any one-time observers that this agent has been updated successfully
00123           evtFetchComplete(true);
00124      }
00125      else
00126      {
00127           // Notify any observer that this agent has not been update successfully
00128           evtFetchComplete(false);
00129      }
00130 
00131      // Release all one-time observers
00132      evtFetchComplete.clear();
00133 }
00134 
00135 AgentList::AgentList(const judo::Element& query, Session& s)
00136 {
00137      load(query, s);
00138 }
00139 
00140 void AgentList::load(const judo::Element& query, Session& s)
00141 {
00142      // Ensure list is clear
00143      clear();
00144      // MAke sure this is the proper namespace
00145      if (!query.cmpAttrib("xmlns", "jabber:iq:agents"))
00146           return;
00147 
00148      // Walk each of the child <agent> tags and build agent objects
00149      judo::Element::const_iterator it = query.begin();
00150      for (; it != query.end(); it++)
00151      {
00152           if ((*it)->getType() != judo::Node::ntElement)
00153                continue;
00154           // Cast the child judo::element to a tag
00155           judo::Element& t = *static_cast<judo::Element*>(*it);
00156 
00157           // Ensure this is an <agent> tag
00158           if (t.getName() == "agent")
00159                // Construct and insert an agent
00160                push_back(Agent(t.getAttrib("jid"), t, s));
00161      }
00162 }
00163 
00164 } // namespace jabberoo

Generated on Thu Jul 24 13:31:50 2003 for jabberoo by doxygen1.3-rc3