00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00105 _session << iq.toString().c_str();
00106
00107
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
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
00141 return;
00142 }
00143
00144
00145
00146 BrowseDB::iterator it = _children.find(jid);
00147 if (it != _children.end())
00148 {
00149 _children.erase(it);
00150 }
00151
00152
00153 BrowseDB::Item* item = new BrowseDB::Item(*child);
00154 _children[jid] = item;
00155
00156
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 }