Commit 665ff35e authored by Ronan's avatar Ronan
Browse files

feat(core): add wrapped content

parent 6c613582
......@@ -16,22 +16,162 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// From coreapi.
#include "private.h"
#include "c-wrapper/c-tools.h"
#include "object/object-p.h"
#include "content.h"
// =============================================================================
using namespace std;
LINPHONE_BEGIN_NAMESPACE
class ContentPrivate : public ObjectPrivate {
private:
public:
struct Cache {
string type;
string subType;
string customHeaderValue;
string encoding;
};
L_DECLARE_PUBLIC(Content);
SalBodyHandler *bodyHandler = nullptr;
void *cryptoContext = nullptr;
string name;
string key;
mutable Cache cache;
};
// -----------------------------------------------------------------------------
Content::Content (ContentPrivate &p) : Object(p) {}
Content::Content () : Object(*new ContentPrivate) {}
const string &Content::getType () const {
L_D(const Content);
d->cache.type = sal_body_handler_get_subtype(d->bodyHandler);
return d->cache.type;
}
void Content::setType (const string &type) {
L_D(Content);
sal_body_handler_set_type(d->bodyHandler, L_STRING_TO_C(type));
}
const string &Content::getSubType () const {
L_D(const Content);
d->cache.subType = sal_body_handler_get_subtype(d->bodyHandler);
return d->cache.subType;
}
void Content::setSubType (const string &subType) {
L_D(Content);
sal_body_handler_set_subtype(d->bodyHandler, L_STRING_TO_C(subType));
}
const void *Content::getBuffer () const {
L_D(const Content);
return sal_body_handler_get_data(d->bodyHandler);
}
void Content::setBuffer (const void *buffer, size_t size) {
L_D(Content);
sal_body_handler_set_size(d->bodyHandler, size);
void *data = belle_sip_malloc(size);
sal_body_handler_set_data(d->bodyHandler, memcpy(data, buffer, size));
}
size_t Content::getSize () const {
L_D(const Content);
return sal_body_handler_get_size(d->bodyHandler);
}
void Content::setSize (size_t size) {
L_D(Content);
sal_body_handler_set_data(d->bodyHandler, nullptr);
sal_body_handler_set_size(d->bodyHandler, size);
}
const string &Content::getEncoding () const {
L_D(const Content);
d->cache.encoding = sal_body_handler_get_encoding(d->bodyHandler);
return d->cache.encoding;
}
void Content::setEncoding (const string &encoding) {
L_D(Content);
sal_body_handler_set_encoding(d->bodyHandler, L_STRING_TO_C(encoding));
}
const string &Content::getName () const {
L_D(const Content);
return d->name;
}
void Content::setName (const string &name) {
L_D(Content);
d->name = name;
}
bool Content::isMultipart () const {
L_D(const Content);
return sal_body_handler_is_multipart(d->bodyHandler);
}
shared_ptr<Content> Content::getPart (int index) const {
L_D(const Content);
if (!isMultipart())
return nullptr;
SalBodyHandler *bodyHandler = sal_body_handler_get_part(d->bodyHandler, index);
if (!bodyHandler)
return nullptr;
Content *content = new Content();
sal_body_handler_ref(bodyHandler);
content->getPrivate()->bodyHandler = bodyHandler;
return shared_ptr<Content>(content);
}
shared_ptr<Content> Content::findPartByHeader (const string &headerName, const string &headerValue) const {
L_D(const Content);
if (!isMultipart())
return nullptr;
SalBodyHandler *bodyHandler = sal_body_handler_find_part_by_header(
d->bodyHandler,
L_STRING_TO_C(headerName),
L_STRING_TO_C(headerValue)
);
if (!bodyHandler)
return nullptr;
Content *content = new Content();
sal_body_handler_ref(bodyHandler);
content->getPrivate()->bodyHandler = bodyHandler;
return shared_ptr<Content>(content);
}
const string &Content::getCustomHeaderValue (const string &headerName) const {
L_D(const Content);
d->cache.customHeaderValue = sal_body_handler_get_header(d->bodyHandler, L_STRING_TO_C(headerName));
return d->cache.customHeaderValue;
}
const string &Content::getKey () const {
L_D(const Content);
return d->key;
}
void Content::setKey (const string &key) {
L_D(Content);
d->key = key;
}
LINPHONE_END_NAMESPACE
......@@ -19,6 +19,9 @@
#ifndef _CONTENT_H_
#define _CONTENT_H_
#include <memory>
#include <string>
#include "object/object.h"
// =============================================================================
......@@ -27,14 +30,40 @@ LINPHONE_BEGIN_NAMESPACE
class ContentPrivate;
class LINPHONE_PUBLIC Content : public Object {
class Content : public Object {
friend class Core;
public:
// Nothing for the moment.
const std::string &getType () const;
void setType (const std::string &type);
const std::string &getSubType () const;
void setSubType (const std::string &subType);
const void *getBuffer () const;
void setBuffer (const void *buffer, size_t size);
size_t getSize () const;
void setSize (size_t size);
const std::string &getEncoding () const;
void setEncoding (const std::string &encoding);
const std::string &getName () const;
void setName (const std::string &name);
bool isMultipart () const;
std::shared_ptr<Content> getPart (int index) const;
std::shared_ptr<Content> findPartByHeader (const std::string &headerName, const std::string &headerValue) const;
const std::string &getCustomHeaderValue (const std::string &headerName) const;
const std::string &getKey () const;
void setKey (const std::string &key);
private:
Content (ContentPrivate &p);
Content ();
L_DECLARE_PRIVATE(Content);
L_DISABLE_COPY(Content);
......
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