Commit 4d85643a authored by Ronan's avatar Ronan
Browse files

feat(core): add better content class

parent 44152b9f
......@@ -69,7 +69,6 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES
conference/session/port-config.h
content/content-type.h
content/content.h
content/content.h
core/core.h
db/abstract/abstract-db-p.h
db/abstract/abstract-db.h
......@@ -127,7 +126,6 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES
conference/session/media-session.cpp
content/content-type.cpp
content/content.cpp
content/content.cpp
core/core.cpp
db/abstract/abstract-db.cpp
db/events-db.cpp
......
......@@ -16,11 +16,8 @@
* 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-type.h"
#include "object/clonable-object-p.h"
#include "content.h"
......@@ -30,148 +27,82 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
class ContentPrivate : public ObjectPrivate {
class ContentPrivate : public ClonableObjectPrivate {
public:
struct Cache {
string type;
string subType;
string customHeaderValue;
string encoding;
};
SalBodyHandler *bodyHandler = nullptr;
void *cryptoContext = nullptr;
string name;
string key;
mutable Cache cache;
vector<char> body;
ContentType contentType;
};
// -----------------------------------------------------------------------------
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;
}
Content::Content () : ClonableObject(*new ContentPrivate) {}
void Content::setType (const string &type) {
Content::Content (const Content &src) : ClonableObject(*new ContentPrivate) {
L_D(Content);
sal_body_handler_set_type(d->bodyHandler, L_STRING_TO_C(type));
d->body = src.getBody();
d->contentType = src.getContentType();
}
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) {
Content::Content (Content &&src) {
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);
d->body = move(src.getPrivate()->body);
d->contentType = move(src.getPrivate()->contentType);
}
void Content::setBuffer (const void *buffer, size_t size) {
Content &Content::operator= (const Content &src) {
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));
}
if (this != &src) {
d->body = src.getBody();
d->contentType = src.getContentType();
}
size_t Content::getSize () const {
L_D(const Content);
return sal_body_handler_get_size(d->bodyHandler);
return *this;
}
void Content::setSize (size_t size) {
Content &Content::operator= (Content &&src) {
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;
}
if (this != &src) {
d->body = move(src.getPrivate()->body);
d->contentType = move(src.getPrivate()->contentType);
}
void Content::setEncoding (const string &encoding) {
L_D(Content);
sal_body_handler_set_encoding(d->bodyHandler, L_STRING_TO_C(encoding));
return *this;
}
const string &Content::getName () const {
const ContentType &Content::getContentType () const {
L_D(const Content);
return d->name;
return d->contentType;
}
void Content::setName (const string &name) {
void Content::setContentType (const ContentType &contentType) {
L_D(Content);
d->name = name;
d->contentType = contentType;
}
bool Content::isMultipart () const {
const std::vector<char> &Content::getBody () const {
L_D(const Content);
return sal_body_handler_is_multipart(d->bodyHandler);
return d->body;
}
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);
void Content::setBody (const std::vector<char> &body) {
L_D(Content);
d->body = body;
}
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);
void Content::setBody (const std::string &body) {
L_D(Content);
d->body = vector<char>(body.cbegin(), body.cend());
}
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;
void Content::setBody (const void *buffer, size_t size) {
L_D(Content);
const char *start = static_cast<const char *>(buffer);
d->body = vector<char>(start, start + size);
}
const string &Content::getKey () const {
size_t Content::getSize () const {
L_D(const Content);
return d->key;
}
void Content::setKey (const string &key) {
L_D(Content);
d->key = key;
return d->body.size();
}
LINPHONE_END_NAMESPACE
......@@ -19,54 +19,38 @@
#ifndef _CONTENT_H_
#define _CONTENT_H_
#include <memory>
#include <string>
#include <vector>
#include "object/object.h"
#include "object/clonable-object.h"
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
class ContentPrivate;
class ContentType;
class Content : public Object {
friend class Core;
class LINPHONE_PUBLIC Content : public ClonableObject {
public:
const std::string &getType () const;
void setType (const std::string &type);
Content ();
Content (const Content &src);
Content (Content &&src);
const std::string &getSubType () const;
void setSubType (const std::string &subType);
Content &operator= (const Content &src);
Content &operator= (Content &&src);
const void *getBuffer () const;
void setBuffer (const void *buffer, size_t size);
const ContentType &getContentType () const;
void setContentType (const ContentType &contentType);
const std::vector<char> &getBody () const;
void setBody (const std::vector<char> &body);
void setBody (const std::string &body);
void setBody (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 ();
L_DECLARE_PRIVATE(Content);
L_DISABLE_COPY(Content);
};
LINPHONE_END_NAMESPACE
......
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