TizenRefApp-7917 Implement Alarm model class 65/111665/1
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Mon, 23 Jan 2017 09:23:07 +0000 (11:23 +0200)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Mon, 23 Jan 2017 09:23:07 +0000 (11:23 +0200)
Change-Id: I2dcba52cd6e3b99ef3e3fdedab8ed1e93210292a
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
lib-common/inc/Common/Model/Alarm.h [new file with mode: 0644]
lib-common/src/Common/Model/Alarm.cpp [new file with mode: 0644]
lib-common/src/dummy.cpp [deleted file]

diff --git a/lib-common/inc/Common/Model/Alarm.h b/lib-common/inc/Common/Model/Alarm.h
new file mode 100644 (file)
index 0000000..72b610c
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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 */
diff --git a/lib-common/src/Common/Model/Alarm.cpp b/lib-common/src/Common/Model/Alarm.cpp
new file mode 100644 (file)
index 0000000..ba9d6d3
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * 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(&currentTime);
+       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;
+}
diff --git a/lib-common/src/dummy.cpp b/lib-common/src/dummy.cpp
deleted file mode 100644 (file)
index ee2eacf..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * 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;
-}