Verified Commit ce69fb39 authored by Linus Jahn's avatar Linus Jahn 🔌 Committed by Linus Jahn
Browse files

Implement XEP-0264: Jingle Content Thumbnails as StanzaExtension

Will probably only be used for SIMS.
parent ce78e8a5
......@@ -37,6 +37,7 @@ SOURCES += \
src/gloox-extensions/bitsofbinarymemorycache.cpp \
src/gloox-extensions/reference.cpp \
src/gloox-extensions/processinghints.cpp \
src/gloox-extensions/thumb.cpp \
src/gloox-extensions/hash.cpp
HEADERS += \
......@@ -76,6 +77,7 @@ HEADERS += \
src/gloox-extensions/bitsofbinarymemorycache.h \
src/gloox-extensions/reference.h \
src/gloox-extensions/processinghints.h \
src/gloox-extensions/thumb.h \
src/gloox-extensions/hash.h
......
......@@ -41,4 +41,5 @@ set(KAIDAN_SOURCES
${CURDIR}/gloox-extensions/reference.cpp
${CURDIR}/gloox-extensions/processinghints.cpp
${CURDIR}/gloox-extensions/hash.cpp
${CURDIR}/gloox-extensions/thumb.cpp
)
......@@ -106,6 +106,13 @@ namespace gloox {
static const std::string XMLNS_HASH_BLAKE2B_256 = "urn:xmpp:hash-function-text-names:id-blake2b256";
static const std::string XMLNS_HASH_BLAKE2B_384 = "urn:xmpp:hash-function-text-names:id-blake2b384";
static const std::string XMLNS_HASH_BLAKE2B_512 = "urn:xmpp:hash-function-text-names:id-blake2b512";
//
// XEP-0264: Jingle Content Thumbnails
//
static const std::string XMLNS_THUMBS = "urn:xmpp:thumbs:1";
static const int EXT_THUMBS = 4277;
}
#endif // GLOOXEXTS_H__
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2018 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
#include "thumb.h"
#include "gloox-extensions.h"
using namespace gloox;
using namespace gloox::Jingle;
Thumb::Thumb(std::string uri, int width, int height, std::string mediaType)
: StanzaExtension(EXT_THUMBS), m_uri(uri), m_width(width), m_height(height),
m_mediaType(mediaType), m_valid(!uri.empty() && width && height)
{
}
Thumb::Thumb(const Tag* tag)
: StanzaExtension(EXT_THUMBS), m_valid(false)
{
if (!tag || tag->findAttribute("xmlns") != XMLNS_THUMBS ||
!tag->hasAttribute("uri") || !tag->hasAttribute("width") ||
!tag->hasAttribute("height"))
return;
m_uri = tag->findAttribute("uri");
if (tag->hasAttribute("media-type"))
m_mediaType = tag->findAttribute("media-type");
try {
// conversion can cause invalid_argument / out_of_range exception
m_width = std::stoul(tag->findAttribute("width"));
m_height = std::stoul(tag->findAttribute("height"));
} catch (std::invalid_argument &e) {
// Couldn't parse size: input probably doesn't contain valid number
} catch (std::out_of_range &e) {
// Couldn't parse size: is out of range of an unsigned long
}
m_valid = !m_uri.empty() && m_width && m_height;
}
Tag* Thumb::tag() const
{
if (!m_valid)
return new Tag("thumb");
Tag* tag = new Tag("thumb", XMLNS, XMLNS_THUMBS);
tag->addAttribute("uri", m_uri);
if (!m_mediaType.empty())
tag->addAttribute("media-type", m_mediaType);
tag->addAttribute("width", m_width);
tag->addAttribute("height", m_height);
return tag;
}
const std::string& Thumb::filterString() const
{
static const std::string filter = "thumb[@xmlns='" + XMLNS_THUMBS + "']";
return filter;
}
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2018 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef THUMB_H__
#define THUMB_H__
#include <gloox/gloox.h>
#include <gloox/stanzaextension.h>
#include <gloox/tag.h>
#include <string>
namespace gloox {
namespace Jingle {
/**
* @class Thumb An implementation of Jingle Content Thumbnails
* (@xep{0264}) as a StanzaExtension.
*
* XEP Version: 0.4
*
* @author Linus Jahn <lnj@kaidan.im>
* @since 1.0.21
*/
class GLOOX_API Thumb : public gloox::StanzaExtension
{
public:
/**
* Default constructor
*
* @param uri The @c BitsOfBinaryData identifier (but starting
* with 'cid:')
* @param width Width of the thumbnail image
* @param height Height of the thumbnail image
* @param mediaType MIME-type of the image
*/
Thumb(std::string uri, int width, int height,
std::string mediaType = "");
/**
* Construct from XML Tag
*/
Thumb(const Tag *tag);
/**
* Clone the StanzaExtension
*/
virtual StanzaExtension* clone() const
{
return new Thumb(*this);
}
/**
* Get the XML tag
*/
virtual Tag* tag() const;
/**
* Create a new instance
*/
virtual StanzaExtension* newInstance(const Tag* tag) const
{
return new Thumb(tag);
}
/**
* Returns the filter string for filtering by the client
*/
virtual const std::string& filterString() const;
/**
* Check if is valid
*/
bool valid() const
{
return m_valid;
}
std::string uri() const
{
return m_uri;
}
int width() const
{
return m_width;
}
int height() const
{
return m_height;
}
std::string mediaType() const
{
return m_mediaType;
}
private:
bool m_valid;
std::string m_uri;
std::string m_mediaType;
int m_width;
int m_height;
};
}
}
#endif // THUMB_H__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment