summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksandr Sapozhnik <a.sapozhnik@samsung.com>2017-03-13 16:45:17 +0200
committerGerrit Code Review <gerrit@review.vlan103.tizen.org>2017-03-14 04:40:50 -0700
commite2ffaea56b3a448d95e12269eaf752685af0884f (patch)
treefa03d5f1b3de355af470638298c65ed234d9b038
parent2b944f5ee5bbabe20ee1f25691e59258f66bb02a (diff)
downloadalarm-e2ffaea56b3a448d95e12269eaf752685af0884f.tar.gz
alarm-e2ffaea56b3a448d95e12269eaf752685af0884f.tar.bz2
alarm-e2ffaea56b3a448d95e12269eaf752685af0884f.zip
Update lib-apps-common
Change-Id: If21739d86bed8d4bc59773385b702da8bcd715f8 Signed-off-by: Aleksandr Sapozhnik <a.sapozhnik@samsung.com>
-rw-r--r--lib-apps-common/inc/Model/DataControlConsumer.h166
-rw-r--r--lib-apps-common/inc/Model/DataControlProvider.h59
-rw-r--r--lib-apps-common/inc/Model/DataItem.h17
-rw-r--r--lib-apps-common/inc/Model/DataProvider.h7
-rw-r--r--lib-apps-common/inc/Utils/Bundle.h88
-rw-r--r--lib-apps-common/inc/Utils/Logger.h2
-rw-r--r--lib-apps-common/src/Model/DataControlConsumer.cpp188
-rw-r--r--lib-apps-common/src/Model/DataControlProvider.cpp88
-rw-r--r--lib-apps-common/src/Model/DataItem.cpp25
-rw-r--r--lib-apps-common/src/Model/DataProvider.cpp10
-rw-r--r--lib-apps-common/src/Utils/Bundle.cpp85
-rw-r--r--lib-common/inc/Common/Model/Alarm.h6
-rw-r--r--lib-common/src/Common/Model/Alarm.cpp12
-rw-r--r--lib-common/src/Common/Model/AlarmBuilder.cpp2
14 files changed, 735 insertions, 20 deletions
diff --git a/lib-apps-common/inc/Model/DataControlConsumer.h b/lib-apps-common/inc/Model/DataControlConsumer.h
new file mode 100644
index 0000000..3cb0b24
--- /dev/null
+++ b/lib-apps-common/inc/Model/DataControlConsumer.h
@@ -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
index 0000000..387b3dd
--- /dev/null
+++ b/lib-apps-common/inc/Model/DataControlProvider.h
@@ -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 */
diff --git a/lib-apps-common/inc/Model/DataItem.h b/lib-apps-common/inc/Model/DataItem.h
index e410d3e..ff70af6 100644
--- a/lib-apps-common/inc/Model/DataItem.h
+++ b/lib-apps-common/inc/Model/DataItem.h
@@ -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);
@@ -80,6 +86,12 @@ namespace Model
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
* @param[in] changes Which item data was updated if type is ChangeUpdate
@@ -103,6 +115,7 @@ namespace Model
friend class DataProvider;
void finishUpdate();
+ int m_Id;
bool m_IsStandalone;
int m_Changes;
ChangeType m_ChangeType;
diff --git a/lib-apps-common/inc/Model/DataProvider.h b/lib-apps-common/inc/Model/DataProvider.h
index 6c4422f..101023b 100644
--- a/lib-apps-common/inc/Model/DataProvider.h
+++ b/lib-apps-common/inc/Model/DataProvider.h
@@ -74,6 +74,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
index 0000000..630f889
--- /dev/null
+++ b/lib-apps-common/inc/Utils/Bundle.h
@@ -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 */
diff --git a/lib-apps-common/inc/Utils/Logger.h b/lib-apps-common/inc/Utils/Logger.h
index 97ebe42..8922e62 100644
--- a/lib-apps-common/inc/Utils/Logger.h
+++ b/lib-apps-common/inc/Utils/Logger.h
@@ -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
index 0000000..c733d88
--- /dev/null
+++ b/lib-apps-common/src/Model/DataControlConsumer.cpp
@@ -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
index 0000000..e7f6313
--- /dev/null
+++ b/lib-apps-common/src/Model/DataControlProvider.cpp
@@ -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();
+}
diff --git a/lib-apps-common/src/Model/DataItem.cpp b/lib-apps-common/src/Model/DataItem.cpp
index 402fbb8..b9622f0 100644
--- a/lib-apps-common/src/Model/DataItem.cpp
+++ b/lib-apps-common/src/Model/DataItem.cpp
@@ -19,13 +19,31 @@
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) {
diff --git a/lib-apps-common/src/Model/DataProvider.cpp b/lib-apps-common/src/Model/DataProvider.cpp
index 54b73e5..b48c177 100644
--- a/lib-apps-common/src/Model/DataProvider.cpp
+++ b/lib-apps-common/src/Model/DataProvider.cpp
@@ -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
index 0000000..242bf46
--- /dev/null
+++ b/lib-apps-common/src/Utils/Bundle.cpp
@@ -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;
+}
diff --git a/lib-common/inc/Common/Model/Alarm.h b/lib-common/inc/Common/Model/Alarm.h
index 8c62520..e0561a2 100644
--- a/lib-common/inc/Common/Model/Alarm.h
+++ b/lib-common/inc/Common/Model/Alarm.h
@@ -44,11 +44,6 @@ namespace Common
Alarm &operator=(const Alarm &that);
/**
- * @return Database ID.
- */
- int getId() const;
-
- /**
* @return Alarm time and date.
*/
const tm &getDate() const;
@@ -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;
diff --git a/lib-common/src/Common/Model/Alarm.cpp b/lib-common/src/Common/Model/Alarm.cpp
index 282a0db..cf023de 100644
--- a/lib-common/src/Common/Model/Alarm.cpp
+++ b/lib-common/src/Common/Model/Alarm.cpp
@@ -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;
diff --git a/lib-common/src/Common/Model/AlarmBuilder.cpp b/lib-common/src/Common/Model/AlarmBuilder.cpp
index b65aff6..12655d3 100644
--- a/lib-common/src/Common/Model/AlarmBuilder.cpp
+++ b/lib-common/src/Common/Model/AlarmBuilder.cpp
@@ -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) {