diff --git a/abstractattachment.cpp b/abstractattachment.cpp index d14fc82..57606db 100644 --- a/abstractattachment.cpp +++ b/abstractattachment.cpp @@ -52,6 +52,16 @@ StreamDataBlock::StreamDataBlock(const std::function &stream, } } +/*! + * \brief Buffers the data block. Buffered data can be accessed via buffer(). + */ +void StreamDataBlock::makeBuffer() const +{ + m_buffer = make_unique(size()); + stream().seekg(startOffset()); + stream().read(m_buffer.get(), size()); +} + /*! * \class Media::FileDataBlock * \brief The FileDataBlock class is a reference to a certain data block of a file stream. @@ -136,6 +146,7 @@ void AbstractAttachment::setFile(const std::string &path) m_mimeType = mimeType; } m_data = move(file); + m_isDataFromFile = true; } } // namespace Media diff --git a/abstractattachment.h b/abstractattachment.h index 372ef95..7e9dcf2 100644 --- a/abstractattachment.h +++ b/abstractattachment.h @@ -24,6 +24,9 @@ public: std::istream::pos_type startOffset() const; std::istream::pos_type endOffset() const; std::istream::pos_type size() const; + const std::unique_ptr &buffer() const; + void makeBuffer() const; + void releaseBuffer(); protected: StreamDataBlock(); @@ -31,6 +34,7 @@ protected: std::function m_stream; std::istream::pos_type m_startOffset; std::istream::pos_type m_endOffset; + mutable std::unique_ptr m_buffer; }; /*! @@ -67,6 +71,22 @@ inline std::istream::pos_type StreamDataBlock::size() const return m_endOffset - m_startOffset; } +/*! + * \brief Returns the data buffered via makeBuffer(). + */ +inline const std::unique_ptr &StreamDataBlock::buffer() const +{ + return m_buffer; +} + +/*! + * \brief Releases buffered data. + */ +inline void StreamDataBlock::releaseBuffer() +{ + m_buffer.release(); +} + class LIB_EXPORT FileDataBlock : public StreamDataBlock { public: @@ -96,6 +116,7 @@ public: const StreamDataBlock *data() const; void setData(std::unique_ptr &&data); void setFile(const std::string &path); + bool isDataFromFile() const; std::string label() const; void clear(); bool isIgnored() const; @@ -111,6 +132,7 @@ private: std::string m_mimeType; uint64 m_id; std::unique_ptr m_data; + bool m_isDataFromFile; bool m_ignored; }; @@ -119,6 +141,7 @@ private: */ inline AbstractAttachment::AbstractAttachment() : m_id(0), + m_isDataFromFile(false), m_ignored(false) {} @@ -211,6 +234,15 @@ inline const StreamDataBlock *AbstractAttachment::data() const inline void AbstractAttachment::setData(std::unique_ptr &&data) { m_data = std::move(data); + m_isDataFromFile = false; +} + +/*! + * \brief Returns whether the assigned data has been assigned using the setFile() method. + */ +inline bool AbstractAttachment::isDataFromFile() const +{ + return m_isDataFromFile; } /*!