TizenRefApp-7556 Implement sound functionality in Consumer and MemoProvider 46/96346/11
authorIryna Ferenchak <i.ferenchak@samsung.com>
Tue, 15 Nov 2016 11:46:40 +0000 (13:46 +0200)
committerGerrit Code Review <gerrit@review.vlan103.tizen.org>
Tue, 15 Nov 2016 13:33:40 +0000 (05:33 -0800)
Change-Id: I2e8a505ff8982dc1b9695cc82d0d1802e6962de1
Signed-off-by: Iryna Ferenchak <i.ferenchak@samsung.com>
lib-common/inc/Common/Model/DataConsumer.h
lib-common/inc/Common/Model/Memo.h
lib-common/inc/Common/Model/SoundRecord.h
lib-common/src/Common/Model/DataConsumer.cpp
lib-common/src/Common/Model/Memo.cpp
lib-common/src/Common/Model/SoundRecord.cpp
memo-svc/inc/Service/DataProvider.h
memo-svc/src/Service/DataProvider.cpp

index 7b7ec39c7d7aab520fe0d9d724cdfed659577730..f764c5bdb62ada4cf3ee4ba1729ff7c6fd83d5b3 100644 (file)
@@ -134,6 +134,7 @@ namespace Common
                        void setMemoImages(Memo *memo, char *image);
                        bundle *prepareBundle(Memo *memo);
                        void setImageToBundle(bundle *bd, Memo *memo);
+                       void setSoundToBundle(bundle *bd, Memo *memo);
 
                        void onDataChanged(data_control_h provider, data_control_data_change_type_e type, bundle *data);
                        void onResult(data_control_h provider, data_control_error_e result, int callback_id);
index 59abc4d44329243c9896016ab1266b1414acf794..e8c89d35c0081912d647a4435affe4cb5d0e9918 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "Common/Model/Colors.h"
 #include "Common/Model/Image.h"
+#include "Common/Model/SoundRecord.h"
 #include "Model/DataItem.h"
 
 #include <list>
@@ -31,13 +32,16 @@ namespace Common
 {
        namespace Model
        {
-               class SoundRecord;
-
                /**
                * @brief Image list
                */
                typedef std::list<Image> ImageList;
 
+               /**
+               * @brief SoundRecord list
+               */
+               typedef std::list<SoundRecord> SoundRecordList;
+
                /**
                 * @brief Memo model
                 */
@@ -52,18 +56,20 @@ namespace Common
                                FieldPinned,
                                FieldColor,
                                FieldImage,
+                               FieldSound,
                                FieldThumbnail,
                                FieldMax
                        };
 
                        enum ChangeType
                        {
-                               ChangeNone      = 0,
-                               ChangeTitle     = 1 << FieldTitle,
-                               ChangeContent   = 1 << FieldContent,
-                               ChangePinned    = 1 << FieldPinned,
-                               ChangeColor     = 1 << FieldColor,
-                               ChangeImage     = 1 << FieldImage,
+                               ChangeNone       = 0,
+                               ChangeTitle      = 1 << FieldTitle,
+                               ChangeContent    = 1 << FieldContent,
+                               ChangePinned     = 1 << FieldPinned,
+                               ChangeColor      = 1 << FieldColor,
+                               ChangeImage      = 1 << FieldImage,
+                               ChangeSound      = 1 << FieldSound,
                                ChangedThumbnail = 1 << FieldThumbnail
                        };
 
@@ -143,6 +149,11 @@ namespace Common
                         */
                        const SoundRecord *getSoundRecord() const;
 
+                       /**
+                        * @return Attached sound record to remove.
+                        */
+                       const SoundRecord *getSoundRecordToRemove() const;
+
                        /**
                         * @brief Set Memo id.
                         * @param[in]   id   Memo id.
@@ -203,6 +214,12 @@ namespace Common
                         */
                        void addSoundRecord();
 
+                       /**
+                        * @brief Add sound record to Memo.
+                        * @param[in] sound     Sound path
+                        */
+                       void addSoundRecord(std::string sound);
+
                        /**
                         * @brief Remove image from Memo.
                         * @param[in]   index   Image index to remove.
@@ -232,6 +249,7 @@ namespace Common
                        virtual int onUpdate(void *data) override;
 
                private:
+                       bool compareSounds(const SoundRecordList &list) const;
                        bool compareImages(const ImageList &list) const;
                        int generateImageIndex();
                        const Image *getImage(int index) const;
@@ -244,7 +262,8 @@ namespace Common
                        ImageList m_ImagesToRemove;
                        ImageList m_Images;
                        std::string m_Thumbnail;
-                       SoundRecord *m_SoundRecord;
+                       SoundRecordList m_SoundRecords;
+                       SoundRecordList m_SoundRecordsToRemove;
                        bool m_IsPinned;
                        bool m_IsNew;
                };
index 7eb8ca4f21895bfff2bf8746ac98bf47b0c30892..0174883b567b436e4b5496e7731b0eee35621962 100644 (file)
@@ -31,11 +31,12 @@ namespace Common
                         * @brief Create sound record.
                         */
                        SoundRecord();
+                       SoundRecord(std::string path);
 
-                       SoundRecord(SoundRecord &&other) = delete;
-                       SoundRecord &operator=(SoundRecord &&other) = delete;
-                       SoundRecord(const SoundRecord &other) = delete;
-                       SoundRecord &operator=(const SoundRecord &other) = delete;
+                       SoundRecord(SoundRecord &&other) = default;
+                       SoundRecord &operator=(SoundRecord &&other) = default;
+                       SoundRecord(const SoundRecord &other) = default;
+                       SoundRecord &operator=(const SoundRecord &other) = default;
 
                        /**
                         * @return Record path.
index eae78e40de7c4804fa943d91021d0cbc8a78ef28..1a62c1cdafdab670976d25138d04a833266406dc 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "Common/Model/DataConsumer.h"
 #include "Common/Model/DataBasePath.h"
+#include "Common/Model/SoundRecord.h"
 
 #include "Utils/Callback.h"
 #include "Utils/Logger.h"
@@ -223,6 +224,8 @@ void DataConsumer::setMemoContent(Memo *memo, result_set_cursor cursor, int colu
                        setMemoImages(memo, value);
                } else if (strcmp(columnName, COLUMN_THUMBNAIL_PATH) == 0) {
                        memo->setThumbnail(value);
+               } else if (strcmp(columnName, COLUMN_SOUND_PATH) == 0) {
+                       memo->addSoundRecord(value);
                } else {
                        ERR("Column name %s is not found", columnName);
                }
@@ -291,6 +294,7 @@ bundle *DataConsumer::prepareBundle(Memo *memo)
        bundle_add_byte(bd, COLUMN_THUMBNAIL_ID, &index, sizeof(index));
 
        setImageToBundle(bd, memo);
+       setSoundToBundle(bd, memo);
 
        return bd;
 }
@@ -318,6 +322,16 @@ void DataConsumer::setImageToBundle(bundle *bd, Memo *memo)
        bundle_add_str_array(bd, COLUMN_IMAGE_PATH, (const char **) images.data(), images.size());
 }
 
+void DataConsumer::setSoundToBundle(bundle *bd, Memo *memo)
+{
+       if (const SoundRecord *soundToRemove = memo->getSoundRecordToRemove()) {
+               bundle_add_str(bd, SOUND_TO_REMOVE, soundToRemove->getPath().c_str());
+       }
+       if (const SoundRecord *soundToSave = memo->getSoundRecord()) {
+               bundle_add_str(bd, SOUND_TO_SAVE, soundToSave->getPath().c_str());
+       }
+}
+
 void DataConsumer::onDataChanged(data_control_h provider, data_control_data_change_type_e type, bundle *data)
 {
        long long *id = nullptr;
index ba38e3edbf3fabbf167f80e769e0b79ca12429e0..8a22ac0fc5b754f739533f7b37f83f1e21b4b096 100644 (file)
@@ -16,7 +16,6 @@
 
 #include <algorithm>
 #include "Common/Model/Memo.h"
-#include "Common/Model/SoundRecord.h"
 #include "Common/Model/TextTool.h"
 #include "Utils/Logger.h"
 
@@ -28,7 +27,6 @@ Memo::Memo()
        : m_Id(0)
        , m_Time(0)
        , m_Color(Color_1)
-       , m_SoundRecord(nullptr)
        , m_IsPinned(false)
        , m_IsNew(true)
 {
@@ -36,7 +34,6 @@ Memo::Memo()
 
 Memo::~Memo()
 {
-       delete m_SoundRecord;
 }
 
 bool Memo::operator==(const Memo &other) const
@@ -44,7 +41,7 @@ bool Memo::operator==(const Memo &other) const
        return (m_Id == other.m_Id && m_Time == other.m_Time && m_Color == other.m_Color
                        && m_IsPinned == other.m_IsPinned && m_Title == other.m_Title
                        && m_Content == other.m_Content && compareImages(other.getImages())
-                       && m_SoundRecord == other.m_SoundRecord);
+                       && compareSounds(other.m_SoundRecords));
 }
 
 bool Memo::operator!=(const Memo &other) const
@@ -119,7 +116,18 @@ const Image *Memo::getFirstImage() const
 
 const SoundRecord *Memo::getSoundRecord() const
 {
-       return m_SoundRecord;
+       if (m_SoundRecords.empty()) {
+               return nullptr;
+       }
+       return &m_SoundRecords.front();
+}
+
+const SoundRecord *Memo::getSoundRecordToRemove() const
+{
+       if (m_SoundRecordsToRemove.empty()) {
+               return nullptr;
+       }
+       return &m_SoundRecordsToRemove.front();
 }
 
 void Memo::setId(long long id)
@@ -170,10 +178,18 @@ void Memo::setThumbnail(std::string image)
 
 void Memo::addSoundRecord()
 {
-       if (m_SoundRecord) {
+       if (!m_SoundRecords.empty()) {
+               removeSoundRecord();
+       }
+       m_SoundRecords.push_back(SoundRecord());
+}
+
+void Memo::addSoundRecord(std::string sound)
+{
+       if (!m_SoundRecords.empty()) {
                removeSoundRecord();
        }
-       m_SoundRecord = new SoundRecord();
+       m_SoundRecords.push_back(SoundRecord(sound));
 }
 
 void Memo::removeImage(int index)
@@ -194,9 +210,9 @@ void Memo::removeImage(int index)
 
 void Memo::removeSoundRecord()
 {
-       if (m_SoundRecord) {
-               delete m_SoundRecord;
-               m_SoundRecord = nullptr;
+       if (!m_SoundRecords.empty()) {
+               m_SoundRecordsToRemove.push_back(std::move(m_SoundRecords.front()));
+               m_SoundRecords.clear();
        }
 }
 
@@ -212,8 +228,8 @@ bool Memo::isNew() const
 
 bool Memo::isEmpty() const
 {
-       return (m_Title.empty() && m_Content.empty()
-                       && m_Images.empty() && (!m_SoundRecord || !m_SoundRecord->isExist()));
+       return (m_Title.empty() && m_Content.empty() && m_Images.empty()
+                       && (m_SoundRecords.empty() || !m_SoundRecords.front().isExist()));
 }
 
 int Memo::onUpdate(void *data)
@@ -245,6 +261,10 @@ int Memo::onUpdate(void *data)
                m_Images = memo->getImages();
                changes |= ChangeImage;
        }
+       if (!compareSounds(memo->m_SoundRecords)) {
+               m_SoundRecords = std::move(memo->m_SoundRecords);
+               changes |= ChangeSound;
+       }
        if (strcmp(getThumbnail(), memo->getThumbnail()) != 0) {
                setThumbnail(memo->getThumbnail());
                changes |= ChangedThumbnail;
@@ -270,6 +290,13 @@ bool Memo::compareImages(const ImageList &list) const
        return true;
 }
 
+bool Memo::compareSounds(const SoundRecordList &list) const
+{
+       return ((m_SoundRecords.empty() && list.empty())
+                       || (!m_SoundRecords.empty() && !list.empty() &&
+                               m_SoundRecords.front().getPath() == list.front().getPath()));
+}
+
 int Memo::generateImageIndex()
 {
        for (int i = 0; i < MAX_IMAGE_COUNT; i++) {
index 5060407bfd261a2259919b593245dff65676f160..83af161b59034c705793cd85e02b124627c87367 100644 (file)
@@ -34,6 +34,11 @@ SoundRecord::SoundRecord()
 {
 }
 
+SoundRecord::SoundRecord(std::string path)
+       : m_Path(std::move(path))
+{
+}
+
 const std::string &SoundRecord::getPath() const
 {
        return m_Path;
index ddf38c4a136728d346b04bbc74d5984402341b64..375ea3774cf12b53833cd32ddea43f70bdb0e94d 100644 (file)
@@ -58,7 +58,7 @@ namespace Service
                void setMemoStmt(sqlite3_stmt *sqlStmt, bundle *data, long long memoId);
                void setRemoveMemoStmt(sqlite3_stmt *sqlStmt, long long memoId);
                void setRemoveMediaStmt(sqlite3_stmt *sqlStmt, int index, const char *path, long long memoId);
-               void setMediaStmt(sqlite3_stmt *sqlStmt, int index, const char *path, long long memoId);
+               void setMediaStmt(sqlite3_stmt *sqlStmt, const char *dir, int index, const char *path, long long memoId);
 
                void setStmtText(sqlite3_stmt *sqlStmt, bundle *data, const char *key, int position);
                void setStmtInt(sqlite3_stmt *sqlStmt, bundle *data, const char *key, int position);
@@ -69,7 +69,7 @@ namespace Service
                void setRemoveFiles(int memoId, const char *stmt);
                void clearSyncFiles();
 
-               std::string getFilePath(const char *imagePath, const char *prefix, int imageIndex, int memoId);
+               std::string getFilePath(const char *imagePath, const char *dir, int imageIndex, int memoId);
 
                bool execute(Stmts commands, int requestId, long long &memoId);
 
index d31b8f2b4b2624d117c1fca84a20d65ad50b2736..edd82b4342449028ce6a97c18f810d1df6ac8005 100644 (file)
@@ -43,7 +43,8 @@ using namespace std::placeholders;
 
 namespace
 {
-       const std::string thumbnailPrefix = "thumbnail";
+       const std::string thumbnailDir = std::string(getDataDir()).append("thumbnails/");
+       const std::string soundDir = std::string(getDataDir()).append("sounds/");
        const std::string imageDir = std::string(getDataDir()).append("images/");
        const std::string imageTempDir = std::string(getDataDir()).append("images_temp/");
 
@@ -272,7 +273,7 @@ void DataProvider::syncThumbnail(bundle *data, long long memoId, SetThumbnail ca
                callback();
                return;
        }
-       std::string destPath = getFilePath(tnPath.c_str(), thumbnailPrefix.c_str(), 0, memoId);
+       std::string destPath = getFilePath(tnPath.c_str(), thumbnailDir.c_str(), 0, memoId);
 
        isSuccess = Common::cropImage(tnPath.c_str(), CROP_WIDTH, CROP_HEIGHT, destPath.c_str(),
                [destPath, memoId, callback, this](int error) {
@@ -372,6 +373,8 @@ void DataProvider::initialize()
 
        data_control_provider_sql_register_cb(&cbs, this);
 
+       createFolder(thumbnailDir.c_str());
+       createFolder(soundDir.c_str());
        createFolder(imageDir.c_str());
        createFolder(imageTempDir.c_str());
 }
@@ -442,10 +445,10 @@ void DataProvider::setRemoveMediaStmt(sqlite3_stmt *sqlStmt, int index, const ch
        m_FilesToRemove.push_back(path);
 }
 
-void DataProvider::setMediaStmt(sqlite3_stmt *sqlStmt, int index, const char *path, long long memoId)
+void DataProvider::setMediaStmt(sqlite3_stmt *sqlStmt, const char *dir, int index, const char *path, long long memoId)
 {
        int position = 0;
-       std::string destPath = getFilePath(path, "", index, memoId);
+       std::string destPath = getFilePath(path, dir, index, memoId);
 
        int err = 0;
        if (index != ABSENT_INDEX) {
@@ -495,7 +498,7 @@ void DataProvider::syncImages(Stmts &commands, bundle *data)
 
        for (int i = 0; i < imageCount; ++i) {
                if (imageStatus[i] == Image::StatusNew) {
-                       commands.push_back({ insertImage, std::bind(&DataProvider::setMediaStmt, this, _1, imageIndex[i], imagePath[i], _2) });
+                       commands.push_back({ insertImage, std::bind(&DataProvider::setMediaStmt, this, _1, imageDir.c_str(), imageIndex[i], imagePath[i], _2) });
                }
        }
 }
@@ -511,7 +514,7 @@ void DataProvider::syncSounds(Stmts &commands, bundle *data)
                commands.push_back({ deleteSound, std::bind(&DataProvider::setRemoveMediaStmt, this, _1, ABSENT_INDEX, soundToRemove, _2) });
        }
        if (soundToSave) {
-               commands.push_back({ insertSound, std::bind(&DataProvider::setMediaStmt, this, _1, ABSENT_INDEX, soundToSave, _2) });
+               commands.push_back({ insertSound, std::bind(&DataProvider::setMediaStmt, this, _1, soundDir.c_str(), ABSENT_INDEX, soundToSave, _2) });
        }
  }
 
@@ -548,12 +551,12 @@ void DataProvider::clearSyncFiles()
        m_FilesToRename.clear();
 }
 
-std::string DataProvider::getFilePath(const char *imagePath, const char *prefix, int imageIndex, int memoId)
+std::string DataProvider::getFilePath(const char *imagePath, const char *dir, int imageIndex, int memoId)
 {
        const char *imageExt = strrchr(imagePath, '.');
 
        char path[PATH_MAX] = { 0, };
-       snprintf(path, sizeof(path), "%s%s%d_%d%s", imageDir.c_str(), prefix,
+       snprintf(path, sizeof(path), "%s%d_%d%s", dir,
                        memoId, imageIndex, imageExt ? imageExt : "");
 
        return path;