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

jabberoo-browse.cpp

00001 /*
00002  * jabberoo-browse.cpp
00003  * Jabber Client Library Browse Support
00004  *
00005  * Original Code Copyright (C) 1999-2001 Dave Smith (dave@jabber.org)
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * Contributor(s): Julian Missig (IBM)
00022  *
00023  */
00024 
00025 
00026 #include <sigc++/object_slot.h>
00027 
00028 namespace jabberoo {
00029 
00030 BrowseDB::Item::Item(const Element& e)
00031 {
00032     _type = e.getAttrib("type");
00033     _category = e.getAttrib("category");
00034     _jid = e.getAttrib("jid");
00035     _name = e.getAttrib("name");
00036     _version = e.getAttrib("version");
00037 
00038     for (Element::const_iterator it = e.begin(); it != e.end(); ++it)
00039     {
00040         if ((*it)->getType() == Node::ntElement)
00041         {
00042             Element* elem = static_cast<Element*>((*it));
00043             std::string name = elem->getName();
00044             if (name == "ns")
00045             {
00046                 _namespaces.push_back(elem->getCDATA());
00047             }
00048             else if (!elem->getAttrib("jid").empty())
00049             {
00050                 _children.push_back(new BrowseDB::Item(*elem));
00051             }
00052         }
00053     }
00054 }
00055 
00056 BrowseDB::Item::~Item()
00057 {
00058     while(!_children.empty())
00059     {
00060         BrowseDB::Item* child = _children.front();
00061         delete child;
00062         _children.pop_front();
00063     }
00064 }
00065 
00066 BrowseDB::BrowseDB(Session& sess) : _session(sess), _query(NULL)
00067 {
00068 }
00069 
00070 BrowseDB::~BrowseDB()
00071 {
00072     clear();
00073 }
00074 
00075 BrowseDB::Item& BrowseDB::operator[](const std::string& jid)
00076 {
00077     BrowseDB::iterator it = _children.find(jid);
00078     if (it == _children.end())
00079     {
00080         throw XCP_NotCached();
00081     }
00082 
00083     return *(it->second);
00084 }
00085 
00086 void BrowseDB::cache(const std::string& jid, BrowseCallbackFunc f)
00087 {
00088     if (!_query)
00089     {
00090         _query = _session.registerXPath(
00091             "/iq[@type='result']/*[@xmlns='jabber:iq:browse']",
00092             SigC::slot(*this, &BrowseDB::browseXPathCB) );
00093     }
00094     // Hook up the callback
00095     _callbacks[jid] = f;
00096     
00097     judo::Element iq("iq");
00098     iq.putAttrib("type", "get");
00099     iq.putAttrib("id", _session.getNextID());
00100     iq.putAttrib("to", jid);
00101     judo::Element* query = iq.addElement("query");
00102     query->putAttrib("xmlns", "jabber:iq:browse");
00103 
00104     // Send it out
00105     _session << iq.toString().c_str();
00106 
00107     // Dupe checking and what not is handled by the xpath that picks up results
00108 }
00109 
00110 void BrowseDB::clear()
00111 {
00112     for (BrowseDB::iterator it = _children.begin(); it != _children.end(); ++it)
00113     {
00114         delete it->second;
00115     }
00116 
00117     _children.clear();
00118 }
00119 
00120 void BrowseDB::browseXPathCB(const Element& e)
00121 {
00122     std::cout << "Received Browse CB: " << e.toString() << std::endl;
00123     Element* child = NULL;
00124     std::string jid;
00125 
00126     // Find the first child element and ensure it has a jid
00127     for (Element::const_iterator it = e.begin(); it != e.end(); ++it)
00128     {
00129         if ( (*it)->getType() == Node::ntElement )
00130         {
00131            child = static_cast<Element*>(*it);
00132            jid = child->getAttrib("jid");
00133            if (!jid.empty())
00134                break;
00135         }
00136     }
00137 
00138     if (child == NULL)
00139     {
00140         // XXX MAJOR ERROR HERE
00141         return;
00142     }
00143 
00144     // We need to make sure we don't already have a copy of this, if we do
00145     // clean it up and let the new one take precedence
00146     BrowseDB::iterator it = _children.find(jid);
00147     if (it != _children.end())
00148     {
00149         _children.erase(it);
00150     }
00151 
00152     // Cache it up
00153     BrowseDB::Item* item = new BrowseDB::Item(*child);
00154     _children[jid] = item;
00155 
00156     // See if we have a callback for this lookup
00157     std::map<std::string, BrowseCallbackFunc>::iterator i = _callbacks.find(jid);
00158     if (i == _callbacks.end())
00159         return;
00160 
00161     i->second(*item);
00162 
00163     _callbacks.erase(i);
00164 }
00165 
00166 } // namespace jabberoo

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