jinglefiletransfer.cpp
/*
Copyright (c) 2013-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 "jinglefiletransfer.h"
#include "tag.h"
#include "gloox.h"
#include "util.h"
#include <cstdlib>
namespace gloox
{
namespace Jingle
{
static const char* typeValues [] = {
"offer",
"request",
"checksum",
"abort",
"received"
};
FileTransfer::FileTransfer( Type type, const FileList& files )
: Plugin( PluginFileTransfer ), m_type( type ), m_files( files )
{
}
FileTransfer::FileTransfer( const Tag* tag )
: Plugin( PluginFileTransfer ), m_type( Invalid )
{
if( !tag || tag->xmlns() != XMLNS_JINGLE_FILE_TRANSFER )
return;
std::string name = tag->name();
if( name == "description" )
{
const Tag* c = tag->findTag( "description/offer|description/request" );
if( c )
{
parseFileList( c->findChildren( "file" ) );
name = c->name();
}
}
else if( name == "checksum" || name == "abort" || name == "received" )
{
parseFileList( tag->findChildren( "file" ) );
}
m_type = static_cast<Type>( util::lookup( name, typeValues ) );
}
void FileTransfer::parseFileList( const TagList& files )
{
TagList::const_iterator it = files.begin();
for( ; it != files.end(); ++it )
{
File f;
Tag *t = (*it)->findChild( "name" );
f.name = t ? t->cdata() : EmptyString;
t = (*it)->findChild( "desc" );
f.desc = t ? t->cdata() : EmptyString;
t = (*it)->findChild( "date" );
f.date = t ? t->cdata() : EmptyString;
t = (*it)->findChild( "size" );
f.size = t ? atoi( t->cdata().c_str() ) : -1;
t = (*it)->findChild( "range" );
if( t )
{
f.range = true;
f.offset = t->hasAttribute( "offset" ) ? atoi( t->findAttribute( "offset" ).c_str() ) : -1;
}
t = (*it)->findChild( "hash", XMLNS, XMLNS_HASHES );
if( t )
{
f.hash_algo = t->findAttribute( "algo" );
f.hash = t->cdata();
}
m_files.push_back( f );
}
}
const StringList FileTransfer::features() const
{
StringList sl;
sl.push_back( XMLNS_JINGLE_FILE_TRANSFER );
return sl;
}
const std::string& FileTransfer::filterString() const
{
static const std::string filter = "content/description[@xmlns='" + XMLNS_JINGLE_FILE_TRANSFER + "']"
"|jingle/abort[@xmlns='" + XMLNS_JINGLE_FILE_TRANSFER + "']"
"|jingle/received[@xmlns='" + XMLNS_JINGLE_FILE_TRANSFER + "']"
"|jingle/checksum[@xmlns='" + XMLNS_JINGLE_FILE_TRANSFER + "']";
return filter;
}
Plugin* FileTransfer::newInstance( const Tag* tag ) const
{
return new FileTransfer( tag );
}
Tag* FileTransfer::tag() const
{
if( m_type == Invalid )
return 0;
Tag* r = 0;
switch( m_type )
{
case Offer:
case Request:
{
r = new Tag( "description", XMLNS, XMLNS_JINGLE_FILE_TRANSFER );
Tag* o = new Tag( r, util::lookup( m_type, typeValues ) );
FileList::const_iterator it = m_files.begin();
for( ; it != m_files.end(); ++it )
{
Tag* f = new Tag( o, "file" );
new Tag( f, "date", (*it).date );
new Tag( f, "name", (*it).name );
new Tag( f, "desc", (*it).desc );
new Tag( f, "size", util::long2string( (*it).size ) );
Tag* h = new Tag( f, "hash", XMLNS, XMLNS_HASHES );
h->addAttribute( "algo", (*it).hash_algo );
h->setCData( (*it).hash );
if( (*it).range )
new Tag( f, "range", "offset", (*it).offset ? util::long2string( (*it).offset ) : EmptyString );
}
break;
}
case Abort:
case Checksum:
case Received:
{
r = new Tag( util::lookup( m_type, typeValues ), XMLNS, XMLNS_JINGLE_FILE_TRANSFER );
FileList::const_iterator it = m_files.begin();
Tag* f = new Tag( r, "file" );
new Tag( f, "date", (*it).date );
new Tag( f, "name", (*it).name );
new Tag( f, "desc", (*it).desc );
new Tag( f, "size", util::long2string( (*it).size ) );
Tag* h = new Tag( f, "hash", XMLNS, XMLNS_HASHES );
h->addAttribute( "algo", (*it).hash_algo );
h->setCData( (*it).hash );
if( (*it).range )
new Tag( f, "range" );
break;
}
default:
break;
}
return r;
}
}
}
gloox::Jingle::FileTransfer::Type
Type
Definition jinglefiletransfer.h:45
gloox::Jingle::FileTransfer::Received
@ Received
Definition jinglefiletransfer.h:50
gloox::Jingle::FileTransfer::Abort
@ Abort
Definition jinglefiletransfer.h:49
gloox::Jingle::FileTransfer::Offer
@ Offer
Definition jinglefiletransfer.h:46
gloox::Jingle::FileTransfer::Checksum
@ Checksum
Definition jinglefiletransfer.h:48
gloox::Jingle::FileTransfer::Invalid
@ Invalid
Definition jinglefiletransfer.h:51
gloox::Jingle::FileTransfer::Request
@ Request
Definition jinglefiletransfer.h:47
gloox::Jingle::FileTransfer::FileList
std::list< File > FileList
Definition jinglefiletransfer.h:70
gloox::Jingle::FileTransfer::FileTransfer
FileTransfer(Type type, const FileList &files)
Definition jinglefiletransfer.cpp:36
gloox::Jingle::FileTransfer::newInstance
virtual Plugin * newInstance(const Tag *tag) const
Definition jinglefiletransfer.cpp:111
gloox::Jingle::FileTransfer::features
virtual const StringList features() const
Definition jinglefiletransfer.cpp:95
gloox::Jingle::FileTransfer::filterString
virtual const std::string & filterString() const
Definition jinglefiletransfer.cpp:102
gloox::Jingle::FileTransfer::files
const FileList & files() const
Definition jinglefiletransfer.h:101
gloox::Jingle::FileTransfer::type
Type type() const
Definition jinglefiletransfer.h:95
gloox::Jingle::FileTransfer::tag
virtual Tag * tag() const
Definition jinglefiletransfer.cpp:116
gloox::Jingle::Plugin::Plugin
Plugin(JinglePluginType type)
Definition jingleplugin.h:76
gloox::Tag
This is an abstraction of an XML element.
Definition tag.h:47
gloox::Tag::name
const std::string & name() const
Definition tag.h:394
gloox::Tag::setCData
bool setCData(const std::string &cdata)
Definition tag.cpp:447
gloox::Tag::addAttribute
bool addAttribute(Attribute *attr)
Definition tag.cpp:354
gloox::Jingle
The namespace containing Jingle-related (XEP-0166 et. al.) classes.
Definition jinglecontent.cpp:22
gloox::Jingle::PluginFileTransfer
@ PluginFileTransfer
Definition jingleplugin.h:41
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::TagList
std::list< Tag * > TagList
Definition tag.h:31
gloox::StringList
std::list< std::string > StringList
Definition gloox.h:1251
gloox::EmptyString
const std::string EmptyString
Definition gloox.cpp:124
gloox::XMLNS
const std::string XMLNS
Definition gloox.cpp:122
gloox::XMLNS_HASHES
const std::string XMLNS_HASHES
Definition gloox.cpp:113
gloox::XMLNS_JINGLE_FILE_TRANSFER
const std::string XMLNS_JINGLE_FILE_TRANSFER
Definition gloox.cpp:104