Update lib-apps-common 92/118692/2
authorAleksandr Sapozhnik <a.sapozhnik@samsung.com>
Mon, 13 Mar 2017 14:45:17 +0000 (16:45 +0200)
committerGerrit Code Review <gerrit@review.vlan103.tizen.org>
Tue, 14 Mar 2017 11:40:50 +0000 (04:40 -0700)
Change-Id: If21739d86bed8d4bc59773385b702da8bcd715f8
Signed-off-by: Aleksandr Sapozhnik <a.sapozhnik@samsung.com>
14 files changed:
lib-apps-common/inc/Model/DataControlConsumer.h [new file with mode: 0644]
lib-apps-common/inc/Model/DataControlProvider.h [new file with mode: 0644]
lib-apps-common/inc/Model/DataItem.h
lib-apps-common/inc/Model/DataProvider.h
lib-apps-common/inc/Utils/Bundle.h [new file with mode: 0644]
lib-apps-common/inc/Utils/Logger.h
lib-apps-common/src/Model/DataControlConsumer.cpp [new file with mode: 0644]
lib-apps-common/src/Model/DataControlProvider.cpp [new file with mode: 0644]
lib-apps-common/src/Model/DataItem.cpp
lib-apps-common/src/Model/DataProvider.cpp
lib-apps-common/src/Utils/Bundle.cpp [new file with mode: 0644]
lib-common/inc/Common/Model/Alarm.h
lib-common/src/Common/Model/Alarm.cpp
lib-common/src/Common/Model/AlarmBuilder.cpp

diff --git a/lib-apps-common/inc/Model/DataControlConsumer.h b/lib-apps-common/inc/Model/DataControlConsumer.h
new file mode 100644 (file)
index 0000000..3cb0b24
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MODEL_DATA_CONTROL_CONSUMER_H
+#define MODEL_DATA_CONTROL_CONSUMER_H
+
+#include "Model/DataProvider.h"
+#include "Utils/Bundle.h"
+#include "Utils/CallbackManager.h"
+#include "Utils/Range.h"
+
+#include <data_control_sql.h>
+
+namespace Model
+{
+       /**
+        * @brief Represents the data consumer of the data control.
+        */
+       class EXPORT_API DataControlConsumer
+       {
+       public:
+               typedef DataProvider::DataList DataList;
+
+               /**
+                * @brief List of columns for select query.
+                *        The first must be the index column.
+                */
+               typedef Utils::Range<const char **> ColumnList;
+
+               /**
+                * @brief Called after data item changed.
+                * @param[in]   Which item data was changed
+                * @param[in]   Change type
+                */
+               typedef Utils::CallbackManager<int, data_control_data_change_type_e> DataItemChangeCallback;
+
+               /**
+                * @brief Called when requested Data Item(s) is received.
+                * @param[in]   dataList    List of received Data Items
+                */
+               typedef std::function<void(DataList dataList)> GetCallback;
+
+               /**
+                * @brief Called when result of insert, update or delete is received.
+                * @param[in]   isSuccess   Whether operation was successful
+                * @param[in]   id          Data Item's id if it was inserted
+                */
+               typedef std::function<void(bool isSuccess, int id)> ResultCallback;
+
+               /**
+                * @brief Create data consumer.
+                * @param[in]   providerId  The data provider's id
+                * @param[in]   tableId     The table's id
+                * @param[in]   columnList  The list of columns for select query
+                */
+               DataControlConsumer(const char *providerId, const char *tableId, ColumnList columnList);
+               virtual ~DataControlConsumer();
+
+               DataControlConsumer(const DataControlConsumer &that) = delete;
+               DataControlConsumer &operator=(const DataControlConsumer &that) = delete;
+
+               /**
+                * @brief Retrieve item from database by ID.
+                * @param[in]   id          ID
+                * @param[in]   callback    Callback to be called when data item is received
+                */
+               virtual void getDataItem(int id, GetCallback callback);
+
+               /**
+                * @brief Retrieve all data items from database.
+                * @param[in]   callback    Callback to be called when data items are received
+                */
+               virtual void getDataItems(GetCallback callback);
+
+               /**
+                * @brief Convenience wrapper to insert new or update existing data item.
+                * @see insertDataItem()
+                * @see updateDataItem()
+                */
+               void saveDataItem(const DataItem &item, ResultCallback callback);
+
+               /**
+                * @brief Insert data item into database.
+                * @param[in]   item        Data item to insert
+                * @param[in]   callback    Callback to be called when operation is complete
+                */
+               void insertDataItem(const DataItem &item, ResultCallback callback = nullptr);
+
+               /**
+                * @brief Update existing data item in the database.
+                * @param[in]   item        Data item to update
+                * @param[in]   callback    Callback to be called when operation is complete
+                */
+               void updateDataItem(const DataItem &item, ResultCallback callback = nullptr);
+
+               /**
+                * @brief Delete data item from the database.
+                * @param[in]   id          Data item ID
+                * @param[in]   callback    Callback to be called when operation is complete
+                */
+               void deleteDataItem(int id, ResultCallback callback = nullptr);
+
+               /**
+                * @brief Add/remove data item change callback.
+                */
+               DataItemChangeCallback &onDataItemChanged();
+
+       protected:
+               /**
+                * @brief Create data item from the result cursor.
+                * @param[in]   cursor   The result cursor
+                * @return Data item or nullptr.
+                */
+               virtual DataItem *createDataItem(result_set_cursor cursor) = 0;
+
+               /**
+                * @brief Create bundle from data item.
+                * @param[in]   item   Data item
+                * @return Bundle.
+                */
+               virtual Utils::Bundle createBundle(const DataItem &item) = 0;
+
+               /**
+                * @brief Invoke select query to database.
+                * @param[in]   where      The select query condition
+                * @param[in]   callback   Callback to be called when operation is completed
+                */
+               void selectDataItems(const char *where, GetCallback callback);
+
+       private:
+               void onSelectResponse(int requestId, data_control_h provider,
+                               result_set_cursor result, bool isSuccess, const char *error);
+               void onInsertResponse(int requestId, data_control_h provider,
+                               long long id, bool isSuccess, const char *error);
+               void onUpdateResponse(int requestId, data_control_h provider,
+                               bool isSuccess, const char *error);
+               void onDeleteResponse(int requestId, data_control_h provider,
+                               bool isSuccess, const char *error);
+               void onDataChanged(data_control_h provider,
+                               data_control_data_change_type_e type, bundle *data);
+               void onDataCallbackAdded(data_control_h provider,
+                               data_control_error_e result, int callbackId);
+
+               data_control_h m_Provider;
+               ColumnList m_ColumnList;
+               int m_ChangeCallbackId;
+               DataItemChangeCallback m_OnDataItemChanged;
+               std::vector<std::pair<int, ResultCallback>> m_ResultCallbacks;
+               std::vector<std::pair<int, GetCallback>> m_GetCallbacks;
+       };
+}
+
+#endif /* MODEL_DATA_CONTROL_CONSUMER_H */
diff --git a/lib-apps-common/inc/Model/DataControlProvider.h b/lib-apps-common/inc/Model/DataControlProvider.h
new file mode 100644 (file)
index 0000000..387b3dd
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MODEL_DATA_CONTROL_PROVIDER_H
+#define MODEL_DATA_CONTROL_PROVIDER_H
+
+#include "Model/DataProvider.h"
+#include <data_control_noti.h>
+
+namespace Model
+{
+       class DataControlConsumer;
+
+       /**
+        * @brief Represents data provider that works through DataControlConsumer.
+        */
+       class EXPORT_API DataControlProvider : public DataProvider
+       {
+       public:
+               /**
+                * @brief Create data control provider.
+                * @param[in]   consumer   The data control consumer
+                */
+               explicit DataControlProvider(DataControlConsumer &consumer);
+               virtual ~DataControlProvider() override;
+
+       private:
+               struct ChangeInfo
+               {
+                       int id;
+                       data_control_data_change_type_e type;
+               };
+
+               virtual void startInit() override;
+               virtual void startUpdate() override;
+
+               void applyChange(ChangeInfo change, ::Model::DataItem *newItem);
+               void onDataItemChanged(int id, data_control_data_change_type_e type);
+
+               DataControlConsumer &m_Consumer;
+               std::vector<ChangeInfo> m_Changes;
+               int m_ChangesPending;
+       };
+}
+
+#endif /* MODEL_DATA_CONTROL_PROVIDER_H */
index e410d3e6379db573c867b3c66e566d2b565b1bbc..ff70af60680fd5d09465c5f6ab79c8b36dfdc3af 100644 (file)
@@ -38,7 +38,7 @@ namespace Model
        public:
                /**
                 * @brief Called after item was updated.
-                * @param[in] Which item data was updated (depends on the specific item).
+                * @param[in] Which item data was updated (depends on the specific item)
                 */
                typedef Utils::CallbackManager<int> UpdateCallback;
 
@@ -49,11 +49,17 @@ namespace Model
 
                DataItem();
                DataItem(const DataItem &);
+               DataItem &operator=(const DataItem &);
                virtual ~DataItem() { }
 
+               /**
+                * @return ID.
+                */
+               int getId() const;
+
                /**
                 * @brief Update item with new data.
-                * @param[in]   data    New item data.
+                * @param[in]   data    New item data
                 */
                void update(void *data);
 
@@ -79,6 +85,12 @@ namespace Model
                DeleteCallback &onDeleted();
 
        protected:
+               /**
+                * @brief Set ID.
+                * @param[in]   id   Data item id
+                */
+               void setId(int id);
+
                /**
                 * @brief Mark item as changed with specified change type.
                 * @param[in]   changeType  Item change type
@@ -103,6 +115,7 @@ namespace Model
                friend class DataProvider;
                void finishUpdate();
 
+               int m_Id;
                bool m_IsStandalone;
                int m_Changes;
                ChangeType m_ChangeType;
index 6c4422f5d58324e56cd76d9129e6df4b25833469..101023b6d3c7ef283124662c38ff8ae5e48046b1 100644 (file)
@@ -73,6 +73,13 @@ namespace Model
                 */
                void destroy();
 
+               /**
+                * @brief Find data item by id.
+                * @param[in]   id    Data item ID
+                * @return Data item or nullptr.
+                */
+               DataItem *findDataItem(int id);
+
                /**
                 * @brief Set whether to start update immediately when update() is called.
                 * @param[in]   isEnabled   Whether data update is enabled
diff --git a/lib-apps-common/inc/Utils/Bundle.h b/lib-apps-common/inc/Utils/Bundle.h
new file mode 100644 (file)
index 0000000..630f889
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UTILS_BUNDLE_H
+#define UTILS_BUNDLE_H
+
+#include <bundle.h>
+#include <tizen.h>
+
+namespace Utils
+{
+       /**
+        * @brief Bundle wrapper. Provides lifetime management and convenience
+        *        methods for adding and accessing stored values.
+        */
+       class EXPORT_API Bundle
+       {
+       public:
+               Bundle();
+
+               /**
+                * @brief Wrap a pre-existing bundle object.
+                * @param[in]   bundle  A bundle to wrap
+                * @remark The bundle lifetime is not tied to the wrapper in this case.
+                */
+               explicit Bundle(bundle *bundle);
+               Bundle(const Bundle &that) = delete;
+               Bundle(Bundle &&that);
+               virtual ~Bundle();
+
+               Bundle &operator=(const Bundle &that) = delete;
+               Bundle &operator=(Bundle &&that);
+
+               /**
+                * @return Wrapped bundle object.
+                */
+               bundle *getBundle() const;
+
+               /**
+                * @brief Get integer value from a bundle.
+                * @param[in]   key     Value key
+                * @return Integer value.
+                */
+               int getInt(const char *key) const;
+
+               /**
+                * @brief Get string value from a bundle.
+                * @param[in]   key     Value key
+                * @return string value.
+                */
+               const char *getStr(const char *key) const;
+
+               /**
+                * @brief Add integer value to a bundle.
+                * @param[in]   key     Value key
+                * @param[in]   value   Integer value
+                * @return This object for method chaining.
+                */
+               Bundle &addInt(const char *key, int value);
+
+               /**
+                * @brief Add string value to a bundle.
+                * @param[in]   key     Value key
+                * @param[in]   str     String value
+                * @return This object for method chaining.
+                */
+               Bundle &addStr(const char *key, const char *str);
+
+       private:
+               bundle *m_Bundle;
+               bool m_IsOwner;
+       };
+}
+
+#endif /* UTILS_BUNDLE_H */
index 97ebe429667b3b5475bab41c46e62d32fbab65a7..8922e62ff3618d175eab8df242bb2c2b5da889a3 100644 (file)
@@ -51,7 +51,7 @@ if (expr) { \
 }
 
 #define LOG_IF_ERR(code, action, fmt, arg...) \
-LOG_IF(code != TIZEN_ERROR_NONE, action, fmt " %s.", ##arg, get_error_message(code))
+LOG_IF(TIZEN_ERROR_IS_ERROR(code), action, fmt " %s.", ##arg, get_error_message(code))
 
 #define WARN_IF(expr, fmt, arg...) \
 LOG_IF(expr, , fmt, ##arg)
diff --git a/lib-apps-common/src/Model/DataControlConsumer.cpp b/lib-apps-common/src/Model/DataControlConsumer.cpp
new file mode 100644 (file)
index 0000000..c733d88
--- /dev/null
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Model/DataControlConsumer.h"
+
+#include "Utils/Callback.h"
+#include "Utils/Logger.h"
+
+#include <data_control_noti.h>
+
+using namespace Model;
+
+namespace
+{
+       template <typename T, typename... A>
+       void sendResponse(T &callbacks, int requestId, A... args)
+       {
+               for (auto it = callbacks.begin(); it != callbacks.end(); ++it) {
+                       if (it->first == requestId) {
+                               if (it->second) {
+                                       it->second(std::move(args)...);
+                               }
+                               callbacks.erase(it);
+                               break;
+                       }
+               }
+       }
+}
+
+DataControlConsumer::DataControlConsumer(const char *providerId, const char *tableId,
+               ColumnList columnList)
+       : m_Provider(nullptr), m_ColumnList(columnList), m_ChangeCallbackId(0)
+{
+       data_control_sql_create(&m_Provider);
+       data_control_sql_set_provider_id(m_Provider, providerId);
+       data_control_sql_set_data_id(m_Provider, tableId);
+
+       data_control_sql_response_cb cbs;
+       cbs.select_cb = makeCallbackWithLastParam(&DataControlConsumer::onSelectResponse);
+       cbs.insert_cb = makeCallbackWithLastParam(&DataControlConsumer::onInsertResponse);
+       cbs.update_cb = makeCallbackWithLastParam(&DataControlConsumer::onUpdateResponse);
+       cbs.delete_cb = makeCallbackWithLastParam(&DataControlConsumer::onDeleteResponse);
+
+       int err = data_control_sql_register_response_cb(m_Provider, &cbs, this);
+       WARN_IF_ERR(err, "data_control_sql_register_response_cb() failed.");
+
+       err = data_control_add_data_change_cb(m_Provider,
+                       makeCallbackWithLastParam(&DataControlConsumer::onDataChanged), this,
+                       makeCallbackWithLastParam(&DataControlConsumer::onDataCallbackAdded), this,
+                       &m_ChangeCallbackId);
+       WARN_IF_ERR(err, "data_control_add_data_change_cb() failed.");
+}
+
+DataControlConsumer::~DataControlConsumer()
+{
+       data_control_remove_data_change_cb(m_Provider, m_ChangeCallbackId);
+       data_control_sql_destroy(m_Provider);
+}
+
+void DataControlConsumer::getDataItem(int id, GetCallback callback)
+{
+       std::string where;
+
+       if (id) {
+               where.append(m_ColumnList[0])
+                        .append(" = " )
+                        .append(std::to_string(id));
+       }
+
+       selectDataItems(!where.empty() ? where.c_str() : nullptr, std::move(callback));
+}
+
+void DataControlConsumer::getDataItems(GetCallback callback)
+{
+       selectDataItems(nullptr, std::move(callback));
+}
+
+void DataControlConsumer::saveDataItem(const DataItem &item, ResultCallback callback)
+{
+       if (item.getId() == 0) {
+               insertDataItem(item, std::move(callback));
+       } else {
+               updateDataItem(item, std::move(callback));
+       }
+}
+
+void DataControlConsumer::insertDataItem(const DataItem &item, ResultCallback callback)
+{
+       int requestId = 0;
+       int err = data_control_sql_insert(m_Provider, createBundle(item).getBundle(), &requestId);
+
+       WARN_IF_ERR(err, "data_control_sql_insert() failed.");
+       m_ResultCallbacks.push_back({ requestId, std::move(callback) });
+}
+
+void DataControlConsumer::updateDataItem(const DataItem &item, ResultCallback callback)
+{
+       int requestId = 0;
+       int err = data_control_sql_update(m_Provider, createBundle(item).getBundle(),
+                       std::to_string(item.getId()).c_str(), &requestId);
+
+       WARN_IF_ERR(err, "data_control_sql_update() failed.");
+       m_ResultCallbacks.push_back({ requestId, std::move(callback) });
+}
+
+void DataControlConsumer::deleteDataItem(int id, ResultCallback callback)
+{
+       int requestId = 0;
+       int err = data_control_sql_delete(m_Provider, std::to_string(id).c_str(), &requestId);
+
+       WARN_IF_ERR(err, "data_control_sql_delete() failed.");
+       m_ResultCallbacks.push_back({ requestId, std::move(callback) });
+}
+
+DataControlConsumer::DataItemChangeCallback &DataControlConsumer::onDataItemChanged()
+{
+       return m_OnDataItemChanged;
+}
+
+void DataControlConsumer::selectDataItems(const char *where, GetCallback callback)
+{
+       int requestId = 0;
+       int err = data_control_sql_select(m_Provider, (char **)m_ColumnList.begin(), m_ColumnList.count(),
+                               where, nullptr, &requestId);
+
+       WARN_IF_ERR(err, "data_control_sql_select() failed.");
+       m_GetCallbacks.push_back({ requestId, std::move(callback) });
+}
+
+
+void DataControlConsumer::onSelectResponse(int requestId, data_control_h provider,
+               result_set_cursor result, bool isSuccess, const char *error)
+{
+       DataList list;
+       int err = data_control_sql_step_first(result);
+       while (err == DATA_CONTROL_ERROR_NONE) {
+               DataItem *item = createDataItem(result);
+               if (item) {
+                       list.push_back(item);
+               }
+               err = data_control_sql_step_next(result);
+       }
+       sendResponse(m_GetCallbacks, requestId, std::move(list));
+}
+
+void DataControlConsumer::onInsertResponse(int requestId, data_control_h provider,
+               long long id, bool isSuccess, const char *error)
+{
+       sendResponse(m_ResultCallbacks, requestId, isSuccess, id);
+}
+
+void DataControlConsumer::onUpdateResponse(int requestId, data_control_h provider,
+               bool isSuccess, const char *error)
+{
+       sendResponse(m_ResultCallbacks, requestId, isSuccess, -1);
+}
+
+void DataControlConsumer::onDeleteResponse(int requestId, data_control_h provider,
+               bool isSuccess, const char *error)
+{
+       sendResponse(m_ResultCallbacks, requestId, isSuccess, -1);
+}
+
+void DataControlConsumer::onDataChanged(data_control_h provider,
+               data_control_data_change_type_e type, bundle *data)
+{
+       int id = Utils::Bundle(data).getInt(m_ColumnList[0]);
+       if (id) {
+               m_OnDataItemChanged(id, type);
+       }
+}
+void DataControlConsumer::onDataCallbackAdded(data_control_h provider, data_control_error_e result, int callbackId)
+{
+       WARN_IF_ERR(result, "data_control_add_data_change_cb() failed.");
+}
diff --git a/lib-apps-common/src/Model/DataControlProvider.cpp b/lib-apps-common/src/Model/DataControlProvider.cpp
new file mode 100644 (file)
index 0000000..e7f6313
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Model/DataControlProvider.h"
+#include "Model/DataControlConsumer.h"
+
+using namespace Model;
+using namespace std::placeholders;
+
+DataControlProvider::DataControlProvider(DataControlConsumer &consumer)
+       : m_Consumer(consumer), m_ChangesPending(0)
+{
+}
+
+DataControlProvider::~DataControlProvider()
+{
+       m_Consumer.onDataItemChanged() -= this;
+}
+
+void DataControlProvider::startInit()
+{
+       m_Consumer.getDataItems(std::bind(&DataControlProvider::finishInit, this, _1));
+       m_Consumer.onDataItemChanged() += { std::bind(&DataControlProvider::onDataItemChanged, this, _1, _2), this };
+}
+
+void DataControlProvider::startUpdate()
+{
+       for (auto &&change : m_Changes) {
+               if (change.type == DATA_CONTROL_DATA_CHANGE_SQL_DELETE) {
+                       applyChange(change, nullptr);
+                       continue;
+               }
+
+               ++m_ChangesPending;
+               m_Consumer.getDataItem(change.id, [this, change](DataList dataList) {
+                       applyChange(change, dataList.front());
+                       if (!--m_ChangesPending) {
+                               finishUpdate();
+                       }
+               });
+       }
+
+       m_Changes.clear();
+       if (!m_ChangesPending) {
+               finishUpdate();
+       }
+}
+
+void DataControlProvider::applyChange(ChangeInfo change, ::Model::DataItem *newItem)
+{
+       switch (change.type) {
+               case DATA_CONTROL_DATA_CHANGE_SQL_INSERT:
+                       insertDataItem(newItem);
+                       break;
+               case DATA_CONTROL_DATA_CHANGE_SQL_UPDATE:
+                       if (auto item = findDataItem(change.id)) {
+                               item->update(newItem);
+                       }
+                       delete newItem;
+                       break;
+               case DATA_CONTROL_DATA_CHANGE_SQL_DELETE:
+                       if (auto item = findDataItem(change.id)) {
+                               deleteDataItem(*item);
+                       }
+                       break;
+               default:
+                       break;
+       }
+}
+
+void DataControlProvider::onDataItemChanged(int id, data_control_data_change_type_e type)
+{
+       m_Changes.push_back({ id, type });
+       update();
+}
index 402fbb898fa2ac713409ecb03d55d42f90d2c75a..b9622f0e49e1da701cfc9f2915ef04a5408420eb 100644 (file)
 using namespace Model;
 
 DataItem::DataItem()
-       : m_IsStandalone(false), m_Changes(0), m_ChangeType(ChangeNone)
+       : m_Id(0), m_IsStandalone(false), m_Changes(0), m_ChangeType(ChangeNone)
 {
 }
 
 DataItem::DataItem(const DataItem &that)
        : DataItem()
 {
+       m_Id = that.m_Id;
+}
+
+DataItem &DataItem::operator=(const DataItem &that)
+{
+       if (this != &that) {
+               m_Id = that.m_Id;
+               m_Changes = 0;
+               m_ChangeType = ChangeNone;
+               setStandalone(false);
+       }
+
+       return *this;
+}
+
+int DataItem::getId() const
+{
+       return m_Id;
 }
 
 void DataItem::update(void *data)
@@ -61,6 +79,11 @@ DataItem::DeleteCallback &DataItem::onDeleted()
        return m_OnDeleted;
 }
 
+void DataItem::setId(int id)
+{
+       m_Id = id;
+}
+
 void DataItem::setChanged(ChangeType changeType, int changes)
 {
        if (m_ChangeType == ChangeNone) {
index 54b73e5e5d3d2ae842644d32732028d19eb588f9..b48c17701da84f76b4c906e4a656f58f03af0558 100644 (file)
@@ -76,6 +76,16 @@ void DataProvider::destroy()
        }
 }
 
+DataItem *DataProvider::findDataItem(int id)
+{
+       for (auto &&item : getDataList()) {
+               if (item->getId() == id) {
+                       return item;
+               }
+       }
+       return nullptr;
+}
+
 void DataProvider::setUpdateEnabled(bool isEnabled)
 {
        m_IsUpdateEnabled = isEnabled;
diff --git a/lib-apps-common/src/Utils/Bundle.cpp b/lib-apps-common/src/Utils/Bundle.cpp
new file mode 100644 (file)
index 0000000..242bf46
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Utils/Bundle.h"
+
+using namespace Utils;
+
+Bundle::Bundle()
+       : m_Bundle(nullptr), m_IsOwner(true)
+{
+       m_Bundle = bundle_create();
+}
+
+Bundle::Bundle(bundle *bundle)
+       : m_Bundle(bundle), m_IsOwner(false)
+{
+}
+
+Bundle::Bundle(Bundle &&that)
+       : m_Bundle(that.m_Bundle), m_IsOwner(that.m_IsOwner)
+{
+       that.m_IsOwner = false;
+}
+
+Bundle::~Bundle()
+{
+       if (m_IsOwner) {
+               bundle_free(m_Bundle);
+       }
+}
+
+Bundle &Bundle::operator=(Bundle &&that)
+{
+       if (m_IsOwner) {
+               bundle_free(m_Bundle);
+       }
+       m_Bundle = that.m_Bundle;
+       m_IsOwner = that.m_IsOwner;
+       that.m_IsOwner = false;
+       return *this;
+}
+
+bundle *Bundle::getBundle() const
+{
+       return m_Bundle;
+}
+
+int Bundle::getInt(const char *key) const
+{
+       int *value = nullptr;
+       bundle_get_byte(m_Bundle, key, (void **) &value, nullptr);
+       return value ? *value : 0;
+}
+
+const char *Bundle::getStr(const char *key) const
+{
+       char *str = nullptr;
+       bundle_get_str(m_Bundle, key, &str);
+       return str;
+}
+
+Bundle &Bundle::addInt(const char *key, int value)
+{
+       bundle_add_byte(m_Bundle, key, &value, sizeof(value));
+       return *this;
+}
+
+Bundle &Bundle::addStr(const char *key, const char *str)
+{
+       bundle_add_str(m_Bundle, key, str);
+       return *this;
+}
index 8c62520122b90a693ed371d0a0763c443d3bf4cd..e0561a28a3c96f1ec99b5494ef42857fb834c5e7 100644 (file)
@@ -43,11 +43,6 @@ namespace Common
                        Alarm(const Alarm &that);
                        Alarm &operator=(const Alarm &that);
 
-                       /**
-                        * @return Database ID.
-                        */
-                       int getId() const;
-
                        /**
                         * @return Alarm time and date.
                         */
@@ -125,7 +120,6 @@ namespace Common
                        void onDataCallbackAdded(data_control_h provider,
                                        data_control_error_e result, int callbackId);
 
-                       int m_Id;
                        tm m_Date;
                        int m_Repeat;
                        bool m_IsEnabled;
index 282a0db5993aa774cf9ef8e01defb1aed562d578..cf023dee149d6521d803d543dee280839babc854 100644 (file)
@@ -30,9 +30,8 @@
 using namespace Common::Model;
 
 Alarm::Alarm()
-       : m_Id(0), m_Repeat(0), m_IsEnabled(true),
-         m_SnoozeDate{0}, m_SnoozeCount(0), m_IsSnoozed(false),
-         m_ChangeCallbackId(0)
+       : m_Repeat(0), m_IsEnabled(true), m_SnoozeDate{0},
+         m_SnoozeCount(0), m_IsSnoozed(false), m_ChangeCallbackId(0)
 {
        setTime(DEFAULT_TIME);
 }
@@ -50,7 +49,7 @@ Alarm::Alarm(const Alarm &that)
 Alarm &Alarm::operator=(const Alarm &that)
 {
        if (this != &that) {
-               m_Id = that.m_Id;
+               setId(that.getId());
                m_Date = that.m_Date;
                m_Repeat = that.m_Repeat;
                m_IsEnabled = that.m_IsEnabled;
@@ -63,11 +62,6 @@ Alarm &Alarm::operator=(const Alarm &that)
        return *this;
 }
 
-int Alarm::getId() const
-{
-       return m_Id;
-}
-
 const tm &Alarm::getDate() const
 {
        return m_Date;
index b65aff60f8a82bef9bfb5fa0bbda37f5e33ea231..12655d3c771147fb5f45d7770cdbf6d054052819 100644 (file)
@@ -53,7 +53,7 @@ Alarm *AlarmBuilder::createAlarm(result_set_cursor cursor)
                data_control_sql_get_int_data(cursor, i, &value);
 
                if (strcmp(name, COLUMN_ID) == 0) {
-                       alarm->m_Id = value;
+                       alarm->setId(value);
                } else if (strcmp(name, COLUMN_DATE) == 0) {
                        alarm->m_Date = *localtime((time_t *) &value);
                } else if (strcmp(name, COLUMN_REPEAT) == 0) {