--- /dev/null
+/*
+ * 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 COMMON_MODEL_ALARM_PROVIDER_H
+#define COMMON_MODEL_ALARM_PROVIDER_H
+
+#include "Model/DataProvider.h"
+#include <data_control_noti.h>
+
+namespace Common
+{
+ namespace Model
+ {
+ class Alarm;
+ class AlarmConsumer;
+ class EXPORT_API AlarmProvider : public ::Model::DataProvider
+ {
+ public:
+ AlarmProvider();
+ virtual ~AlarmProvider() override;
+
+ private:
+ struct ChangeInfo
+ {
+ int id;
+ data_control_data_change_type_e type;
+ };
+
+ virtual void startInit() override;
+ virtual void startUpdate() override;
+
+ Alarm *findAlarm(int id);
+ void applyChange(ChangeInfo change, ::Model::DataItem *newItem);
+
+ 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);
+
+ AlarmConsumer &m_Consumer;
+ int m_ChangeCallbackId;
+
+ std::vector<ChangeInfo> m_Changes;
+ int m_ChangesPending;
+ };
+ }
+}
+
+#endif /* COMMON_MODEL_ALARM_PROVIDER_H */
--- /dev/null
+/*
+ * 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 "Common/Model/AlarmProvider.h"
+#include "Common/Model/AlarmConsumer.h"
+#include "Common/Model/AlarmDb.h"
+#include "Common/Model/Alarm.h"
+#include "Common/Bundle.h"
+
+#include "Utils/Callback.h"
+#include "Utils/Logger.h"
+
+using namespace Common;
+using namespace Common::Model;
+using namespace std::placeholders;
+
+AlarmProvider::AlarmProvider()
+ : m_Consumer(AlarmConsumer::getInstance()),
+ m_ChangeCallbackId(0), m_ChangesPending(0)
+{
+}
+
+AlarmProvider::~AlarmProvider()
+{
+ data_control_remove_data_change_cb(m_Consumer.getProvider(),
+ m_ChangeCallbackId);
+}
+
+void AlarmProvider::startInit()
+{
+ m_Consumer.getAlarms(std::bind(&AlarmProvider::finishInit, this, _1));
+ int err = data_control_add_data_change_cb(m_Consumer.getProvider(),
+ makeCallbackWithLastParam(&AlarmProvider::onDataChanged), this,
+ makeCallbackWithLastParam(&AlarmProvider::onDataCallbackAdded), this,
+ &m_ChangeCallbackId);
+ WARN_IF_ERR(err, "data_control_add_data_change_cb() failed.");
+}
+
+void AlarmProvider::startUpdate()
+{
+ for (auto &&change : m_Changes) {
+ if (change.type == DATA_CONTROL_DATA_CHANGE_SQL_DELETE) {
+ applyChange(change, nullptr);
+ continue;
+ }
+
+ ++m_ChangesPending;
+ m_Consumer.getAlarm(change.id, [this, change](DataList dataList) {
+ applyChange(change, dataList.front());
+ if (!--m_ChangesPending) {
+ finishUpdate();
+ }
+ });
+ }
+ m_Changes.clear();
+}
+
+Alarm *AlarmProvider::findAlarm(int id)
+{
+ for (auto &&dataItem : getDataList()) {
+ auto alarm = static_cast<Alarm *>(dataItem);
+ if (alarm->getId() == id) {
+ return alarm;
+ }
+ }
+ return nullptr;
+}
+
+void AlarmProvider::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 alarm = findAlarm(change.id)) {
+ alarm->update(newItem);
+ }
+ delete newItem;
+ break;
+ case DATA_CONTROL_DATA_CHANGE_SQL_DELETE:
+ if (auto alarm = findAlarm(change.id)) {
+ deleteDataItem(*alarm);
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+void AlarmProvider::onDataChanged(data_control_h provider,
+ data_control_data_change_type_e type, bundle *data)
+{
+ m_Changes.push_back({ Bundle(data).getInt(COLUMN_ID), type });
+ update();
+}
+
+void AlarmProvider::onDataCallbackAdded(data_control_h provider,
+ data_control_error_e result, int callbackId)
+{
+ WARN_IF_ERR(result, "data_control_add_data_change_cb() failed.");
+}