search.cpp
/*
Copyright (c) 2006-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "search.h"
#include "clientbase.h"
#include "dataform.h"
#include "iq.h"
namespace gloox
{
// Search::Query ----
Search::Query::Query( DataForm* form )
: StanzaExtension( ExtSearch ), m_form( form ), m_fields( 0 )
{
}
Search::Query::Query( int fields, const SearchFieldStruct& values )
: StanzaExtension( ExtSearch ), m_form( 0 ), m_fields( fields ), m_values( values )
{
}
Search::Query::Query( const Tag* tag )
: StanzaExtension( ExtSearch ), m_form( 0 ), m_fields( 0 )
{
if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_SEARCH )
return;
const TagList& l = tag->children();
TagList::const_iterator it = l.begin();
for( ; it != l.end(); ++it )
{
if( (*it)->name() == "instructions" )
{
m_instructions = (*it)->cdata();
}
else if( (*it)->name() == "item" )
{
m_srl.push_back( new SearchFieldStruct( (*it) ) );
}
else if( (*it)->name() == "first" )
m_fields |= SearchFieldFirst;
else if( (*it)->name() == "last" )
m_fields |= SearchFieldLast;
else if( (*it)->name() == "email" )
m_fields |= SearchFieldEmail;
else if( (*it)->name() == "nick" )
m_fields |= SearchFieldNick;
else if( !m_form && (*it)->name() == "x" && (*it)->xmlns() == XMLNS_X_DATA )
m_form = new DataForm( (*it) );
}
}
Search::Query::~Query()
{
delete m_form;
SearchResultList::iterator it = m_srl.begin();
for( ; it != m_srl.end(); ++it )
delete (*it);
}
const std::string& Search::Query::filterString() const
{
static const std::string filter = "/iq/query[@xmlns='" + XMLNS_SEARCH + "']";
return filter;
}
Tag* Search::Query::tag() const
{
Tag* t = new Tag( "query" );
t->setXmlns( XMLNS_SEARCH );
if( m_form )
t->addChild( m_form->tag() );
else if( m_fields )
{
if( !m_instructions.empty() )
new Tag( t, "instructions", m_instructions );
if( m_fields & SearchFieldFirst )
new Tag( t, "first", m_values.first() );
if( m_fields & SearchFieldLast )
new Tag( t, "last", m_values.last() );
if( m_fields & SearchFieldNick )
new Tag( t, "nick", m_values.nick() );
if( m_fields & SearchFieldEmail )
new Tag( t, "email", m_values.email() );
}
else if( !m_srl.empty() )
{
SearchResultList::const_iterator it = m_srl.begin();
for( ; it != m_srl.end(); ++it )
{
t->addChild( (*it)->tag() );
}
}
return t;
}
// ---- ~Search::Query ----
// ---- Search ----
Search::Search( ClientBase* parent )
: m_parent( parent )
{
if( m_parent )
m_parent->registerStanzaExtension( new Query() );
}
Search::~Search()
{
if( m_parent )
{
m_parent->removeIDHandler( this );
m_parent->removeStanzaExtension( ExtRoster );
}
}
void Search::fetchSearchFields( const JID& directory, SearchHandler* sh )
{
if( !m_parent || !directory || !sh )
return;
const std::string& id = m_parent->getID();
IQ iq( IQ::Get, directory, id );
iq.addExtension( new Query() );
m_track[id] = sh;
m_parent->send( iq, this, FetchSearchFields );
}
void Search::search( const JID& directory, DataForm* form, SearchHandler* sh )
{
if( !m_parent || !directory || !sh )
return;
const std::string& id = m_parent->getID();
IQ iq( IQ::Set, directory, id );
iq.addExtension( new Query( form ) );
m_track[id] = sh;
m_parent->send( iq, this, DoSearch );
}
void Search::search( const JID& directory, int fields, const SearchFieldStruct& values, SearchHandler* sh )
{
if( !m_parent || !directory || !sh )
return;
const std::string& id = m_parent->getID();
IQ iq( IQ::Set, directory, id );
iq.addExtension( new Query( fields, values ) );
m_track[id] = sh;
m_parent->send( iq, this, DoSearch );
}
void Search::handleIqID( const IQ& iq, int context )
{
TrackMap::iterator it = m_track.find( iq.id() );
if( it != m_track.end() )
{
switch( iq.subtype() )
{
case IQ::Result:
{
const Query* q = iq.findExtension<Query>( ExtSearch );
if( !q )
return;
switch( context )
{
case FetchSearchFields:
{
if( q->form() )
{
(*it).second->handleSearchFields( iq.from(), q->form() );
}
else
{
(*it).second->handleSearchFields( iq.from(), q->fields(), q->instructions() );
}
break;
}
case DoSearch:
{
if( q->form() )
{
(*it).second->handleSearchResult( iq.from(), q->form() );
}
else
{
(*it).second->handleSearchResult( iq.from(), q->result() );
}
break;
}
}
break;
}
case IQ::Error:
(*it).second->handleSearchError( iq.from(), iq.error() );
break;
default:
break;
}
m_track.erase( it );
}
return;
}
}
gloox::ClientBase
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition clientbase.h:79
gloox::DataForm
An abstraction of a XEP-0004 Data Form.
Definition dataform.h:57
gloox::IQ
An abstraction of an IQ stanza.
Definition iq.h:34
gloox::IQ::subtype
IqType subtype() const
Definition iq.h:74
gloox::IQ::Set
@ Set
Definition iq.h:46
gloox::IQ::Error
@ Error
Definition iq.h:49
gloox::IQ::Result
@ Result
Definition iq.h:48
gloox::IQ::Get
@ Get
Definition iq.h:45
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::SearchFieldStruct
Definition searchhandler.h:34
gloox::SearchHandler
A virtual interface that enables objects to receive Jabber Search (XEP-0055) results.
Definition searchhandler.h:145
gloox::Search::fetchSearchFields
void fetchSearchFields(const JID &directory, SearchHandler *sh)
Definition search.cpp:128
gloox::Search::handleIqID
virtual void handleIqID(const IQ &iq, int context)
Definition search.cpp:167
gloox::Search::search
void search(const JID &directory, DataForm *form, SearchHandler *sh)
Definition search.cpp:140
gloox::Search::~Search
~Search()
Definition search.cpp:119
gloox::Search::Search
Search(ClientBase *parent)
Definition search.cpp:112
gloox::StanzaExtension
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
Definition stanzaextension.h:204
gloox::Stanza::addExtension
void addExtension(const StanzaExtension *se)
Definition stanza.cpp:52
gloox::Stanza::id
const std::string & id() const
Definition stanza.h:63
gloox::Stanza::error
const Error * error() const
Definition stanza.cpp:47
gloox::Stanza::from
const JID & from() const
Definition stanza.h:51
gloox::Stanza::findExtension
const StanzaExtension * findExtension(int type) const
Definition stanza.cpp:57
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::ExtRoster
@ ExtRoster
Definition stanzaextension.h:64
gloox::ExtSearch
@ ExtSearch
Definition stanzaextension.h:75
gloox::XMLNS_SEARCH
const std::string XMLNS_SEARCH
Definition gloox.cpp:47