--- /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_H
+#define COMMON_MODEL_ALARM_H
+
+#include "Model/DataItem.h"
+#include <ctime>
+
+namespace Common
+{
+ namespace Model
+ {
+ class EXPORT_API Alarm : public ::Model::DataItem
+ {
+ public:
+ /**
+ * @brief Describes alarm changes during update.
+ */
+ enum ChangedInfo
+ {
+ ChangedDate = 1 << 0,
+ ChangedRepeat = 1 << 1,
+ ChangedEnabled = 1 << 2
+ };
+
+ Alarm();
+
+ /**
+ * @return Database ID.
+ */
+ int getId() const;
+
+ /**
+ * @return Alarm time and date.
+ */
+ const tm &getDate() const;
+
+ /**
+ * @return Weekly repeat mask.
+ */
+ int getRepeat() const;
+
+ /**
+ * @return Whether alarm is enabled.
+ */
+ bool isEnabled() const;
+
+ /**
+ * @brief Set alarm time.
+ * @param[in] hour Hours
+ * @param[in] min Minutes
+ */
+ void setTime(int hour, int min);
+
+ /**
+ * @brief Set weekly repeat.
+ * @param[in] value Repeat mask
+ */
+ void setRepeat(int value);
+
+ /**
+ * @brief Set alarm enabled state.
+ * @param[in] isEnabled Whether alarm is enabled
+ */
+ void setEnabled(bool isEnabled);
+
+ private:
+ virtual int onUpdate(void *data) override;
+
+ int m_Id;
+ tm m_Date;
+ int m_Repeat;
+ bool m_IsEnabled;
+ };
+ }
+}
+
+#endif /* COMMON_MODEL_ALARM_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/Alarm.h"
+
+#define DEFAULT_TIME 6, 0
+#define DAY_COUNT 7
+#define DAY_SECONDS 60 * 60 * 24
+
+using namespace Common::Model;
+
+Alarm::Alarm()
+ : m_Id(0), m_Repeat(0), m_IsEnabled(true)
+{
+ setTime(DEFAULT_TIME);
+}
+
+int Alarm::getId() const
+{
+ return m_Id;
+}
+
+const tm &Alarm::getDate() const
+{
+ return m_Date;
+}
+
+int Alarm::getRepeat() const
+{
+ return m_Repeat;
+}
+
+bool Alarm::isEnabled() const
+{
+ return m_IsEnabled;
+}
+
+void Alarm::setTime(int hour, int min)
+{
+ time_t currentTime = time(nullptr);
+ m_Date = *localtime(¤tTime);
+ m_Date.tm_hour = hour;
+ m_Date.tm_min = min;
+ m_Date.tm_sec = 0;
+
+ /* Adjust date to tomorrow if necessary */
+ time_t alarmTime = mktime(&m_Date);
+ if (alarmTime <= currentTime) {
+ alarmTime += DAY_SECONDS;
+ m_Date = *localtime(&alarmTime);
+ }
+
+ if (m_Repeat) {
+ int wday = m_Date.tm_wday;
+ /* Find closest future weekday that fits repeat mask */
+ while (!(m_Repeat & (1 << (wday % DAY_COUNT)))) {
+ ++wday;
+ }
+ alarmTime += (wday - m_Date.tm_wday) * DAY_SECONDS;
+ m_Date = *localtime(&alarmTime);
+ }
+}
+
+void Alarm::setRepeat(int value)
+{
+ m_Repeat = value;
+ setTime(m_Date.tm_hour, m_Date.tm_min);
+}
+
+void Alarm::setEnabled(bool isEnabled)
+{
+ m_IsEnabled = isEnabled;
+ if (m_IsEnabled) {
+ setTime(m_Date.tm_hour, m_Date.tm_min);
+ }
+}
+
+int Alarm::onUpdate(void *data)
+{
+ int changes = 0;
+ Alarm &that = *(Alarm *) data;
+
+ if (mktime(&that.m_Date) != mktime(&m_Date)) {
+ changes |= ChangedDate;
+ }
+ if (that.m_Repeat != m_Repeat) {
+ changes |= ChangedRepeat;
+ }
+ if (that.m_IsEnabled != m_IsEnabled) {
+ changes |= ChangedEnabled;
+ }
+
+ *this = std::move(that);
+ return changes;
+}
+++ /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.
- */
-
-int dummy()
-{
- return 0;
-}