AlarmProviderFile backend implementation
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Fri, 5 Aug 2016 14:32:31 +0000 (16:32 +0200)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Thu, 25 Aug 2016 09:40:04 +0000 (11:40 +0200)
AlarmProviderFile uses binary files to store Alarm objects.

Change-Id: Id14ee2c5b2b01b1f0f104fb354ea719f94af6d7d

23 files changed:
common/.cproject
common/inc/Common/Defines.h [new file with mode: 0644]
common/inc/Internal/AlarmProviderEet.h [deleted file]
common/inc/Internal/AlarmProviderFile.h [new file with mode: 0644]
common/inc/Internal/AlarmProviderStub.h
common/inc/Model/Alarm.h
common/inc/Model/AlarmProvider.h
common/inc/Model/WeekFlags.h
common/inc/Utils/BinaryFileReader.h [new file with mode: 0644]
common/inc/Utils/BinaryFileWriter.h [new file with mode: 0644]
common/inc/Utils/Reader.h [new file with mode: 0644]
common/inc/Utils/Serialization.h [new file with mode: 0644]
common/inc/Utils/Writer.h [new file with mode: 0644]
common/src/Model/Alarm.cpp
common/src/Model/AlarmProvider.cpp
common/src/Model/AlarmProviderEet.cpp [deleted file]
common/src/Model/AlarmProviderFile.cpp [new file with mode: 0644]
common/src/Model/AlarmProviderStub.cpp
common/src/Model/WeekFlags.cpp
common/src/Utils/BinaryFileReader.cpp [new file with mode: 0644]
common/src/Utils/BinaryFileWriter.cpp [new file with mode: 0644]
common/src/Utils/Serialization.cpp [new file with mode: 0644]
common/src/Utils/Utils.cpp

index 542b9d8..18f5093 100644 (file)
                </scannerConfigBuildInfo>
        </storageModule>
        <storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
+       <storageModule moduleId="refreshScope"/>
 </cproject>
diff --git a/common/inc/Common/Defines.h b/common/inc/Common/Defines.h
new file mode 100644 (file)
index 0000000..0973165
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef _CLOCK_COMMON_DEFINES_H_
+#define _CLOCK_COMMON_DEFINES_H_
+
+#define CLOCK_EXPORT_API __attribute__((__visibility__("default")))
+#define PACKAGE "org.tizen.clock"
+
+#endif
diff --git a/common/inc/Internal/AlarmProviderEet.h b/common/inc/Internal/AlarmProviderEet.h
deleted file mode 100644 (file)
index 6336c7c..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef _INTERNAL_ALARM_PROVIDER_EET_H_
-#define _INTERNAL_ALARM_PROVIDER_EET_H_
-
-#include "Model/AlarmProvider.h"
-
-namespace internal {
-       namespace model {
-               class AlarmProviderEet : public common::model::AlarmProvider
-               {
-                       public:
-                               std::vector<std::reference_wrapper<common::model::Alarm>> GetAlarms();
-                               void Sync(common::model::Alarm& alarm);
-                               void SyncAll();
-                               void Add(common::model::Alarm& alarm);
-                               void Remove(common::model::Alarm& alarm);
-                               AlarmProviderEet();
-               };
-       } /* model */
-} /* internal */
-
-#endif
diff --git a/common/inc/Internal/AlarmProviderFile.h b/common/inc/Internal/AlarmProviderFile.h
new file mode 100644 (file)
index 0000000..a3c9e20
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef _INTERNAL_ALARM_PROVIDER_FILE_H_
+#define _INTERNAL_ALARM_PROVIDER_FILE_H_
+
+#include <vector>
+#include <list>
+
+#include "Model/AlarmProvider.h"
+
+namespace internal {
+       namespace model {
+               class AlarmProviderFile : public common::model::AlarmProvider
+               {
+                       public:
+                               std::vector<std::reference_wrapper<common::model::Alarm>> GetAlarms();
+                               common::model::Alarm* GetAlarmWithId(common::model::AlarmId id);
+                               void Add(common::model::Alarm& alarm);
+                               void Remove(common::model::Alarm& alarm);
+                               AlarmProviderFile(common::utils::IReader &r);
+                               ~AlarmProviderFile();
+                               void Serialize(common::utils::IWriter &w) const;
+                       private:
+                               std::list<common::model::Alarm> alarms;
+                               static const int db_schema_version;
+               };
+       } /* model */
+} /* internal */
+
+#endif
index 36160c0..4beec80 100644 (file)
@@ -9,11 +9,11 @@ namespace internal {
                {
                        public:
                                std::vector<std::reference_wrapper<common::model::Alarm>> GetAlarms();
-                               void Sync(common::model::Alarm& alarm);
-                               void SyncAll();
+                               common::model::Alarm* GetAlarmWithId(common::model::AlarmId id);
                                void Add(common::model::Alarm& alarm);
                                void Remove(common::model::Alarm& alarm);
-                               AlarmProviderStub();
+                               AlarmProviderStub(common::utils::IReader &r);
+                               void Serialize(common::utils::IWriter &w) const {}
                        private:
                                std::vector<common::model::Alarm> alarms;
                };
index 8ee97e4..0503fed 100644 (file)
@@ -4,43 +4,83 @@
 #include <string>
 #include <time.h>
 
+#include "Common/Defines.h"
 #include "Model/WeekFlags.h"
+#include "Utils/Serialization.h"
 
 namespace common {
        namespace model {
-               enum AlarmType {
+               typedef int AlarmId;
+               enum class AlarmType {
+                       SOUND_ONLY,
+                       VIBRATION,
+                       VIBRATION_AND_SOUND
                };
-               class Alarm {
+               class CLOCK_EXPORT_API Time : public utils::ISerializable {
+                       public:
+                               unsigned int Hour, Min, Sec;
+                               Time() : Hour(0), Min(0), Sec(0) {}
+                               Time(unsigned int h, unsigned int m, unsigned int s) : Hour(h), Min(m), Sec(s) {}
+                               Time(utils::IReader &);
+                               void Serialize(utils::IWriter &w) const;
+                               inline bool operator==(const Time &a) { return (a.Hour == Hour ) &&
+                                       (a.Min == Min) && (a.Sec == Sec); }
+               };
+               void Serialize(utils::IWriter &w, AlarmType type);
+               void Deserialize(utils::IReader &w, AlarmType &type);
+               class CLOCK_EXPORT_API Alarm : public utils::ISerializable {
                        public:
-                               Alarm(int alarm_id);
                                Alarm();
+                               Alarm(utils::IReader &);
                                void Activate();
+                               bool IsActivated() const;
                                void Deactivate();
                                void Snooze();
                                void EnableSnooze();
                                void DisableSnooze();
-                               bool SnoozeEnabled();
-                               std::string GetName();
+                               bool IsSnoozeEnabled();
+                               void SetSnoozeInterval(unsigned int seconds);
+                               unsigned int GetSnoozeInterval() const;
+                               void SetSnoozeMaxAttemps(unsigned int attemps);
+                               unsigned int GetSnoozeMaxAttempts() const;
+                               std::string GetName() const;
                                void SetName(std::string name);
-                               time_t GetTime();
-                               void SetTime(time_t time);
-                               std::string GetMelody();
+                               Time GetTime() const;
+                               void SetTime(Time time);
+                               std::string GetMelody() const;
                                void SetMelody(std::string path);
-                               std::string GetVibration();
-                               void SetVibration(std::string pattern);
-                               WeekFlags GetWeekFlags();
+                               AlarmType GetType() const;
+                               void SetType(AlarmType pattern);
+                               WeekFlags GetWeekFlags() const;
                                void SetWeekFlags(WeekFlags flags);
+                               void Serialize(utils::IWriter &w) const;
+                               unsigned int GetVolume() const;
+                               void SetVolume(unsigned int volume);
+
+                               /** Implement guideline requirement. Two alarms are considered
+                                * same if they have same time and name */
+                               inline bool operator==(const Alarm &a) { return (time == a.time) && (a.name == name); }
+                               inline bool operator==(const AlarmId id) { return this->alarm_id == id; }
                        private:
                                int alarm_id;
                                std::string name;
-                               std::string melody;
-                               std::string vibration;
+                               struct {
+                                       std::string melody;
+                                       unsigned int volume;
+                               } sound;
+                               AlarmType type_;
                                WeekFlags flags;
                                bool activated;
                                bool snooze_enabled;
-                               time_t time;
+                               struct {
+                                       int alarm_id;
+                                       unsigned int interval;
+                                       unsigned int attempt;
+                                       unsigned int attempts_max;
+                               } snooze;
+                               Time time;
                };
        } /* model */
-} /* clock */
+} /* common */
 
 #endif
index 0b0d551..7468100 100644 (file)
@@ -1,27 +1,46 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 _CLOCK_ALARM_PROVIDER_H_
 #define _CLOCK_ALARM_PROVIDER_H_
 
+#include "Common/Defines.h"
 #include "Model/Alarm.h"
+#include "Utils/Serialization.h"
 
 #include <vector>
 #include <functional>
 
+
 namespace common {
        namespace model {
-               class AlarmProvider {
+               class CLOCK_EXPORT_API AlarmProvider : public utils::ISerializable {
                        public:
-                               static AlarmProvider& GetInstance();
+                               static AlarmProvider *GetInstance();
+                               static void Sync();
 
                                virtual std::vector<std::reference_wrapper<Alarm>> GetAlarms() = 0;
-                               virtual void Sync(Alarm& alarm) = 0;
-                               virtual void SyncAll() = 0;
+                               virtual Alarm *GetAlarmWithId(AlarmId id) = 0;
                                virtual void Add(Alarm& alarm) = 0;
                                virtual void Remove(Alarm& alarm) = 0;
                        protected:
-                               AlarmProvider();
-                               virtual ~AlarmProvider();
+                               AlarmProvider() {}
+                               virtual ~AlarmProvider() {}
                };
        } /* model */
-} /* clock */
+} /* common */
 
 #endif
index 9d20b00..b020703 100644 (file)
@@ -1,30 +1,60 @@
-#ifndef _CLOCK_ALARM_WEEK_FLAGS_H
-#define _CLOCK_ALARM_WEEK_FLAGS_H
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 _CLOCK_WEEK_FLAGS_H
+#define _CLOCK_WEEK_FLAGS_H
+
+#include <Common/Defines.h>
+#include <Utils/Serialization.h>
+
+#include <app_alarm.h>
 
 namespace common {
        namespace model {
-               enum WeekDay {
-                       MONDAY   = (1 << 0),
-                       TUESDAY  = (1 << 1),
-                       WENSDAY  = (1 << 2),
-                       THURSDAY = (1 << 3),
-                       FRIDAY   = (1 << 4),
-                       SATURDAY = (1 << 5),
-                       SUNDAY   = (1 << 6),
-                       ALL_WEEK = (MONDAY | TUESDAY | WENSDAY | THURSDAY | FRIDAY | SATURDAY | SUNDAY),
-                       WEEKEND  = (SATURDAY | SUNDAY),
+               enum class WeekDay : int {
+                       MONDAY    = ALARM_WEEK_FLAG_MONDAY,
+                       TUESDAY   = ALARM_WEEK_FLAG_TUESDAY,
+                       WEDNESDAY = ALARM_WEEK_FLAG_WEDNESDAY,
+                       THURSDAY  = ALARM_WEEK_FLAG_THURSDAY,
+                       FRIDAY    = ALARM_WEEK_FLAG_FRIDAY,
+                       SATURDAY  = ALARM_WEEK_FLAG_SATURDAY,
+                       SUNDAY    = ALARM_WEEK_FLAG_SUNDAY,
+                       ALL_WEEK  = (MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY | SATURDAY | SUNDAY),
+                       WEEKEND   = (SATURDAY | SUNDAY),
                };
-               class WeekFlags {
+               inline constexpr WeekDay
+               operator|(WeekDay a, WeekDay b)
+               {
+                       return static_cast<WeekDay>(static_cast<int>(a) | static_cast<int>(b));
+               }
+               class CLOCK_EXPORT_API WeekFlags : public utils::ISerializable {
                        public:
                                WeekFlags();
-                               WeekFlags(int bitmask);
+                               WeekFlags(utils::IReader &r);
+                               WeekFlags(WeekDay day);
                                void AddDay(WeekDay day);
                                void RemoveDay(WeekDay day);
                                bool IsOn(WeekDay day);
+                               void Serialize(utils::IWriter &w) const;
+                               int GetBitMask() const;
+                               inline bool operator==(const WeekFlags &a) { return a.raw_flags == raw_flags; }
                        private:
                                int raw_flags;
                };
        } /* model */
-} /* clock */
+} /* common */
 
 #endif
diff --git a/common/inc/Utils/BinaryFileReader.h b/common/inc/Utils/BinaryFileReader.h
new file mode 100644 (file)
index 0000000..0582fcb
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 _CLOCK_COMMON_BINARY_FILE_READER_H_
+#define _CLOCK_COMMON_BINARY_FILE_READER_H_
+
+#include "Utils/Reader.h"
+#include "Utils/Utils.h"
+#include <vector>
+
+namespace common {
+       namespace utils {
+               class BinaryFileReader : public IReader {
+                       public:
+                               BinaryFileReader(Utils::AppSubdirectory dir, const char *filename);
+                               ~BinaryFileReader() {}
+                               void Read(void *buffer, size_t size);
+                       private:
+                               std::vector<unsigned char> buffer;
+                               unsigned int offset;
+               };
+       } /* utils */
+} /* common */
+
+#endif
diff --git a/common/inc/Utils/BinaryFileWriter.h b/common/inc/Utils/BinaryFileWriter.h
new file mode 100644 (file)
index 0000000..e09c73d
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 _CLOCK_COMMON_BINARY_FILE_WRITER_H_
+#define _CLOCK_COMMON_BINARY_FILE_WRITER_H_
+
+#include "Utils/Writer.h"
+#include "Utils/Utils.h"
+
+#include <fstream>
+
+namespace common {
+       namespace utils {
+               class BinaryFileWriter : public IWriter {
+                       public:
+                               BinaryFileWriter(Utils::AppSubdirectory dir, const char *filename);
+                               ~BinaryFileWriter();
+                               void Write(const void *buffer, size_t size);
+                               void Flush();
+                       private:
+                               std::ofstream file;
+               };
+       } /* utils */
+} /* common */
+
+#endif
diff --git a/common/inc/Utils/Reader.h b/common/inc/Utils/Reader.h
new file mode 100644 (file)
index 0000000..ba65b90
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 _CLOCK_COMMON_READER_H_
+#define _CLOCK_COMMON_READER_H_
+
+#include <cstdlib>
+
+namespace common {
+       namespace utils {
+               class IReader {
+                       public:
+                               virtual void Read(void *buffer, size_t size) = 0;
+                               virtual ~IReader() {}
+               };
+       } /* utils */
+} /* common */
+#endif
diff --git a/common/inc/Utils/Serialization.h b/common/inc/Utils/Serialization.h
new file mode 100644 (file)
index 0000000..911e404
--- /dev/null
@@ -0,0 +1,110 @@
+#ifndef _CLOCK_COMMON_SERIALIZATION_H_
+#define _CLOCK_COMMON_SERIALIZATION_H_
+
+#include <Utils/Reader.h>
+#include <Utils/Writer.h>
+
+#include <list>
+#include <string>
+
+namespace common {
+       namespace utils {
+               class ISerializable {
+                       public:
+                               ISerializable() {}
+                               ISerializable(IReader &reader) {}
+                               virtual void Serialize(IWriter &writer) const = 0;
+                               virtual ~ISerializable() {}
+               };
+
+               // serialization/deserialization for basic types
+               // ISerializable
+               inline void Serialize(IWriter &w, const ISerializable &o) {
+                       o.Serialize(w);
+               }
+               template<typename T>
+               inline void Deserialize(IReader &r, T &obj) {
+                       obj = T(r);
+               }
+
+               // int
+               inline void Serialize(IWriter &w, const int v) {
+                       w.Write(&v, sizeof(v));
+               }
+               inline void Deserialize(IReader &r, int *v) {
+                       r.Read(v, sizeof(v));
+               }
+               inline void Deserialize(IReader &r, int &v) {
+                       r.Read(&v, sizeof(v));
+               }
+
+               // unsigned int
+               inline void Serialize(IWriter &w, const unsigned int v) {
+                       w.Write(&v, sizeof(v));
+               }
+               inline void Deserialize(IReader &r, unsigned int *v) {
+                       r.Read(v, sizeof(v));
+               }
+               inline void Deserialize(IReader &r, unsigned int &v) {
+                       r.Read(&v, sizeof(v));
+               }
+
+               // bool
+               inline void Serialize(IWriter &w, const bool v) {
+                       w.Write(&v, sizeof(v));
+               }
+               inline void Deserialize(IReader &r, bool *v) {
+                       r.Read(v, sizeof(v));
+               }
+               inline void Deserialize(IReader &r, bool &v) {
+                       r.Read(&v, sizeof(v));
+               }
+
+               // time_t
+               inline void Serialize(IWriter &w, const time_t v) {
+                       w.Write(&v, sizeof(v));
+               }
+               inline void Deserialize(IReader &r, time_t *v) {
+                       r.Read(v, sizeof(v));
+               }
+               inline void Deserialize(IReader &r, time_t &v) {
+                       r.Read(&v, sizeof(v));
+               }
+
+               // serialization for std::string
+               inline void Serialize(IWriter &w, const std::string &v) {
+                       int len = v.size();
+                       w.Write(&len, sizeof(len));
+                       w.Write(v.c_str(), len);
+               }
+               inline void Deserialize(IReader &r, std::string &v) {
+                       int len;
+                       r.Read(&len, sizeof(len));
+                       char *buf = new char[len + 1];
+                       r.Read(buf, len);
+                       buf[len] = '\0';
+                       v = std::string(buf);
+                       delete buf;
+               };
+
+               // serialization for std::list
+               template <typename T>
+               inline void Serialize(IWriter &w, const std::list<T> &list) {
+                       int len = list.size();
+                       w.Write(&len, sizeof(len));
+                       for (auto it = list.begin(); it != list.end(); ++it) {
+                               Serialize(w, *it);
+                       }
+               }
+               template <typename T>
+               inline void Deserialize(IReader &r, std::list<T> &list) {
+                       int len;
+                       r.Read(&len, sizeof(len));
+                       for (int i = 0; i < len; i++) {
+                               T obj(r);
+                               list.push_back(std::move(obj));
+                       }
+               }
+       } /* utils */
+} /* common */
+#endif
diff --git a/common/inc/Utils/Writer.h b/common/inc/Utils/Writer.h
new file mode 100644 (file)
index 0000000..da38d02
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 _CLOCK_COMMON_WRITER_H_
+#define _CLOCK_COMMON_WRITER_H_
+
+#include <cstdlib>
+
+namespace common {
+       namespace utils {
+               class IWriter {
+                       public:
+                               virtual void Write(const void *buffer, size_t size) = 0;
+                               virtual ~IWriter() {}
+               };
+       } /* utils */
+} /* common */
+#endif
index 11c75ad..754727a 100644 (file)
 #include <app_alarm.h>
 #include "Model/Alarm.h"
+#include "Common/Defines.h"
 
 using namespace common::model;
+using namespace common::utils;
 
-
-Alarm::Alarm(int alarm_id)
+static app_control_h AppControlCreate()
 {
-       alarm_id = alarm_id;
-       activated = true;
+       app_control_h ctrl;
+
+       int err = app_control_create(&ctrl);
+       if (err != APP_CONTROL_ERROR_NONE)
+               return NULL;
+
+       err = app_control_set_app_id(ctrl, PACKAGE);
+       if (err != APP_CONTROL_ERROR_NONE) {
+               app_control_destroy(ctrl);
+               return NULL;
+       }
+
+       return ctrl;
 }
 
-Alarm::Alarm()
+Alarm::Alarm() :
+       flags(WeekDay::ALL_WEEK)
 {
+       snooze.attempts_max = 3;
        activated = false;
+       snooze_enabled = false;
+       alarm_id = -1;
 }
 
 void Alarm::Activate()
 {
-       if (!activated) {
+       struct tm tmtime = {0, };
+
+       if (activated)
+               return;
+
+       app_control_h control = AppControlCreate();
+       if (!control) {
+               // log error
+               return;
        }
+
+       tmtime.tm_hour = time.Hour;
+       tmtime.tm_min= time.Min;
+       tmtime.tm_sec= time.Sec;
+
+       int err = alarm_schedule_with_recurrence_week_flag(
+                       control,
+                       &tmtime,
+                       flags.GetBitMask(),
+                       &alarm_id);
+       if (err != ALARM_ERROR_NONE) {
+               activated = true;
+       } else {
+               // log error
+       }
+
+       app_control_destroy(control);
 }
 
 void Alarm::Deactivate()
 {
-       if (activated) {
+       if (!activated)
+               return;
+
+       int err = alarm_cancel(alarm_id);
+       if (err != ALARM_ERROR_NONE) {
        }
+       err = alarm_cancel(snooze.alarm_id);
+       if (err != ALARM_ERROR_NONE) {
+       }
+
+       activated = false;
 }
 
 void Alarm::Snooze()
 {
+       if (!activated || !snooze_enabled)
+               return;
+
+       if (snooze.attempt >= snooze.attempts_max)
+               return;
+
+       app_control_h control = AppControlCreate();
+       if (!control) return;
+
+       int err = alarm_schedule_once_after_delay(control, snooze.interval, &snooze.alarm_id);
+       if (err == ALARM_ERROR_NONE) {
+               snooze.attempt++;
+       } else {
+               // log error
+       }
+
+       app_control_destroy(control);
 }
 
 void Alarm::EnableSnooze()
 {
+       snooze_enabled = true;
 }
 
 void Alarm::DisableSnooze()
 {
+       snooze_enabled = false;
 }
 
-bool Alarm::SnoozeEnabled()
+bool Alarm::IsSnoozeEnabled()
 {
        return snooze_enabled;
 }
 
-std::string Alarm::GetName()
+void Alarm::SetSnoozeInterval(unsigned int seconds)
+{
+       snooze.interval = seconds;
+}
+
+unsigned int Alarm::GetSnoozeInterval() const
+{
+       return snooze.interval;
+}
+
+void Alarm::SetSnoozeMaxAttemps(unsigned int attemps)
+{
+       snooze.attempts_max = attemps;
+}
+
+unsigned int Alarm::GetSnoozeMaxAttempts() const
+{
+       return snooze.attempts_max;
+}
+
+std::string Alarm::GetName() const
 {
        return name;
 }
@@ -54,39 +143,133 @@ void Alarm::SetName(std::string nm)
        name = nm;
 }
 
-time_t Alarm::GetTime()
+Time Alarm::GetTime() const
 {
        return time;
 }
 
-void Alarm::SetTime(time_t tm)
+void Alarm::SetTime(Time tm)
 {
+       if (time == tm)
+               return;
+
        time = tm;
+
+       if (activated) {
+               Deactivate();
+               Activate();
+       }
 }
 
-std::string Alarm::GetMelody()
+std::string Alarm::GetMelody() const
 {
-       return melody;
+       return sound.melody;
 }
 
 void Alarm::SetMelody(std::string path)
 {
+       sound.melody = path;
 }
 
-std::string Alarm::GetVibration()
+AlarmType Alarm::GetType() const
 {
-       return vibration;
+       return type_;
 }
 
-void Alarm::SetVibration(std::string pattern)
+void Alarm::SetType(AlarmType type)
 {
+       type_ = type;
 }
 
-WeekFlags Alarm::GetWeekFlags()
+WeekFlags Alarm::GetWeekFlags() const
 {
        return flags;
 }
 
 void Alarm::SetWeekFlags(WeekFlags flags)
 {
+       if (this->flags == flags)
+               return;
+
+       this->flags = flags;
+
+       if (activated) {
+               Deactivate();
+               Activate();
+       }
+}
+
+bool Alarm::IsActivated() const
+{
+       return activated;
+}
+
+void common::model::Serialize(IWriter &w, AlarmType type)
+{
+       common::utils::Serialize(w, static_cast<int>(type));
+}
+
+void common::model::Deserialize(IReader &r, AlarmType &type)
+{
+       int tmp;
+       utils::Deserialize(r, tmp);
+       type = static_cast<AlarmType>(tmp);
+}
+
+Alarm::Alarm(IReader &r)
+{
+       utils::Deserialize(r, alarm_id);
+       utils::Deserialize(r, snooze.alarm_id);
+       utils::Deserialize(r, snooze.interval);
+       utils::Deserialize(r, snooze.attempt);
+       utils::Deserialize(r, snooze.attempts_max);
+       utils::Deserialize(r, name);
+       utils::Deserialize(r, sound.melody);
+       utils::Deserialize(r, sound.volume);
+       common::model::Deserialize(r, type_);
+       utils::Deserialize(r, flags);
+       utils::Deserialize(r, activated);
+       utils::Deserialize(r, snooze_enabled);
+       utils::Deserialize(r, time);
+}
+
+void Alarm::Serialize(utils::IWriter &w) const
+{
+       utils::Serialize(w, alarm_id);
+       utils::Serialize(w, snooze.alarm_id);
+       utils::Serialize(w, snooze.interval);
+       utils::Serialize(w, snooze.attempt);
+       utils::Serialize(w, snooze.attempts_max);
+       utils::Serialize(w, name);
+       utils::Serialize(w, sound.melody);
+       utils::Serialize(w, sound.volume);
+       common::model::Serialize(w, type_);
+       utils::Serialize(w, flags);
+       utils::Serialize(w, activated);
+       utils::Serialize(w, snooze_enabled);
+       utils::Serialize(w, time);
+}
+
+unsigned int Alarm::GetVolume() const
+{
+       return sound.volume;
+}
+
+void Alarm::SetVolume(unsigned int volume)
+{
+       sound.volume = volume;
+}
+
+Time::Time(utils::IReader &r)
+{
+       utils::Deserialize(r, Hour);
+       utils::Deserialize(r, Min);
+       utils::Deserialize(r, Sec);
+}
+
+void Time::Serialize(utils::IWriter &w) const
+{
+       utils::Serialize(w, Hour);
+       utils::Serialize(w, Min);
+       utils::Serialize(w, Sec);
 }
index f5677ef..762df5b 100644 (file)
@@ -1,20 +1,30 @@
 #include "Model/AlarmProvider.h"
-#include "Internal/AlarmProviderStub.h"
+#include "Internal/AlarmProviderFile.h"
+#include "Utils/BinaryFileReader.h"
+#include "Utils/BinaryFileWriter.h"
+#include "Utils/Utils.h"
+
+static const char DATABASE_FILE[] = "alarmsdb.cfg";
 
 using namespace common::model;
+using namespace common::utils;
 using namespace internal::model;
 
-
-AlarmProvider::AlarmProvider()
+AlarmProvider *AlarmProvider::GetInstance()
 {
+       static AlarmProvider *instance;
+       if (!instance) {
+               BinaryFileReader reader(Utils::APP_DIR_DATA, DATABASE_FILE);
+               instance = new AlarmProviderFile(reader);
+       }
+       return instance;
 }
 
-AlarmProvider::~AlarmProvider()
+void AlarmProvider::Sync()
 {
-}
+       AlarmProvider *instance = GetInstance();
 
-AlarmProvider &AlarmProvider::GetInstance()
-{
-       static AlarmProviderStub instance;
-       return instance;
+       BinaryFileWriter writer(Utils::APP_DIR_DATA, DATABASE_FILE);
+       instance->Serialize(writer);
+       writer.Flush();
 }
diff --git a/common/src/Model/AlarmProviderEet.cpp b/common/src/Model/AlarmProviderEet.cpp
deleted file mode 100644 (file)
index 3236da8..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "Internal/AlarmProviderEet.h"
-
-#include <Eet.h>
-
-using namespace internal::model;
-
-AlarmProviderEet::AlarmProviderEet()
-{
-}
-
-std::vector<std::reference_wrapper<common::model::Alarm>> AlarmProviderEet::GetAlarms()
-{
-}
-
-void AlarmProviderEet::Sync(common::model::Alarm& alarm)
-{
-}
-
-void AlarmProviderEet::SyncAll()
-{
-}
-
-void AlarmProviderEet::Add(common::model::Alarm& alarm)
-{
-}
-
-void AlarmProviderEet::Remove(common::model::Alarm& alarm)
-{
-}
diff --git a/common/src/Model/AlarmProviderFile.cpp b/common/src/Model/AlarmProviderFile.cpp
new file mode 100644 (file)
index 0000000..9a3f114
--- /dev/null
@@ -0,0 +1,60 @@
+#include "Internal/AlarmProviderFile.h"
+
+#include <Ecore_File.h>
+#include <cstdlib>
+#include <algorithm>
+
+using namespace internal::model;
+using namespace common::model;
+using namespace common::utils;
+
+const int AlarmProviderFile::db_schema_version = 1;
+
+
+void AlarmProviderFile::Add(common::model::Alarm& alarm)
+{
+       auto it = std::find(alarms.begin(), alarms.end(), alarm);
+
+       if (it == alarms.end()) {
+               alarms.push_back(alarm);
+       }
+
+       //FIXME emit AlarmAddedEvent
+}
+
+void AlarmProviderFile::Remove(common::model::Alarm& alarm)
+{
+       auto it = std::find(alarms.begin(), alarms.end(), alarm);
+
+       if (it != alarms.end()) {
+               alarms.erase(it);
+       }
+
+       //FIXME emit AlarmRemovedEvent
+}
+
+AlarmProviderFile::AlarmProviderFile(IReader &r)
+{
+       common::utils::Deserialize(r, alarms);
+}
+
+AlarmProviderFile::~AlarmProviderFile()
+{
+}
+
+std::vector<std::reference_wrapper<common::model::Alarm>> AlarmProviderFile::GetAlarms()
+{
+       return std::vector<std::reference_wrapper<common::model::Alarm>>(
+                       alarms.begin(),
+                       alarms.end());
+}
+
+Alarm* AlarmProviderFile::GetAlarmWithId(AlarmId id)
+{
+       return nullptr;
+}
+
+void AlarmProviderFile::Serialize(common::utils::IWriter &w) const
+{
+       common::utils::Serialize(w, alarms);
+}
index a715bc4..105cd75 100644 (file)
@@ -2,43 +2,45 @@
 #include "Model/Alarm.h"
 #include "Utils/Time.h"
 
+#include <algorithm>
+
 using namespace internal::model;
 using namespace common::model;
 using namespace common::utils;
 
-AlarmProviderStub::AlarmProviderStub()
+AlarmProviderStub::AlarmProviderStub(IReader &r)
 {
        Alarm a;
 
        a.SetName("Wake up lazy bastard");
-       a.SetTime(MakeTime(28, 7, 2016, 7, 0));
-       a.SetWeekFlags(WeekFlags(ALL_WEEK));
+       a.SetTime(Time(7, 0, 0));
+       a.SetWeekFlags(WeekFlags(WeekDay::ALL_WEEK));
        a.EnableSnooze();
        a.Activate();
        alarms.push_back(a);
 
        a.SetName("Mechanic");
-       a.SetTime(MakeTime(28, 7, 2016, 8, 30));
+       a.SetTime(Time(8, 30, 0));
        a.DisableSnooze();
        a.Deactivate();
        alarms.push_back(a);
 
        a.SetName("Job Appointment");
-       a.SetTime(MakeTime(11, 8, 2016, 14, 0));
+       a.SetTime(Time(20, 14, 0));
        a.DisableSnooze();
        a.Activate();
        alarms.push_back(a);
 
        a.SetName("Visit grandma");
-       a.SetTime(MakeTime(9, 7, 2016, 18, 32));
-       a.SetWeekFlags(WeekFlags(MONDAY | WENSDAY | FRIDAY));
+       a.SetTime(Time(16, 18, 32));
+       a.SetWeekFlags(WeekFlags(WeekDay::MONDAY | WeekDay::WEDNESDAY | WeekDay::FRIDAY));
        a.EnableSnooze();
        a.Activate();
        alarms.push_back(a);
 
        a.SetName("Take a pill!");
-       a.SetTime(MakeTime(28, 7, 2016, 22, 0));
-       a.SetWeekFlags(WeekFlags(ALL_WEEK));
+       a.SetTime(Time(1, 22, 0));
+       a.SetWeekFlags(WeekFlags(WeekDay::ALL_WEEK));
        a.EnableSnooze();
        a.Activate();
        alarms.push_back(a);
@@ -49,14 +51,6 @@ std::vector<std::reference_wrapper<Alarm>> AlarmProviderStub::GetAlarms()
        return std::vector<std::reference_wrapper<Alarm>>(alarms.begin(), alarms.end());
 }
 
-void AlarmProviderStub::Sync(Alarm& alarm)
-{
-}
-
-void AlarmProviderStub::SyncAll()
-{
-}
-
 void AlarmProviderStub::Add(Alarm& alarm)
 {
        alarms.push_back(alarm);
@@ -72,3 +66,13 @@ void AlarmProviderStub::Remove(Alarm& alarm)
        }
 }
 
+Alarm* AlarmProviderStub::GetAlarmWithId(AlarmId id)
+{
+       auto it = std::find(alarms.begin(), alarms.end(), id);
+
+       if (it != alarms.end()) {
+               return &(*it);
+       }
+       return nullptr;
+}
+
index 3844c61..eb24013 100644 (file)
@@ -7,18 +7,33 @@ WeekFlags::WeekFlags() {
 };
 
 void WeekFlags::AddDay(WeekDay day) {
-       raw_flags |= day;
+       raw_flags |= static_cast<int>(day);
 };
 
 void WeekFlags::RemoveDay(WeekDay day) {
-       raw_flags &= ~day;
+       raw_flags &= ~static_cast<int>(day);
 };
 
 bool WeekFlags::IsOn(WeekDay day) {
-       return raw_flags & day;
+       return raw_flags & static_cast<int>(day);
 };
 
-WeekFlags::WeekFlags(int mask)
+WeekFlags::WeekFlags(WeekDay mask)
 {
-       raw_flags = mask;
+       raw_flags = static_cast<int>(mask);
+}
+
+void WeekFlags::Serialize(utils::IWriter &w) const
+{
+       utils::Serialize(w, raw_flags);
+}
+
+WeekFlags::WeekFlags(utils::IReader &r)
+{
+       utils::Deserialize(r, raw_flags);
+}
+
+int WeekFlags::GetBitMask() const
+{
+       return raw_flags;
 }
diff --git a/common/src/Utils/BinaryFileReader.cpp b/common/src/Utils/BinaryFileReader.cpp
new file mode 100644 (file)
index 0000000..4d53a90
--- /dev/null
@@ -0,0 +1,34 @@
+#include "Utils/BinaryFileReader.h"
+
+#include <fstream>
+#include <cstdlib>
+#include <cstring>
+
+using namespace common::utils;
+using namespace std;
+
+BinaryFileReader::BinaryFileReader(Utils::AppSubdirectory dir, const char *filename)
+{
+       ifstream file(Utils::GetAppResourcePath(dir, filename));
+       offset = 0;
+
+       if (!file.good()) {
+               return;
+       }
+
+       file.seekg(0, ios::end);
+       unsigned int size = file.tellg();
+       file.seekg(0, ios::beg);
+
+       buffer.reserve(size);
+       file.read((char*)&buffer[0], size);
+}
+
+void BinaryFileReader::Read(void *buffer, size_t size)
+{
+       if (offset + size > this->buffer.capacity())
+               return;
+
+       memcpy(buffer, &(this->buffer[offset]), size);
+       offset += size;
+}
diff --git a/common/src/Utils/BinaryFileWriter.cpp b/common/src/Utils/BinaryFileWriter.cpp
new file mode 100644 (file)
index 0000000..c31eb95
--- /dev/null
@@ -0,0 +1,28 @@
+#include "Utils/BinaryFileWriter.h"
+
+#include <fstream>
+
+using namespace common::utils;
+using namespace std;
+
+BinaryFileWriter::BinaryFileWriter(Utils::AppSubdirectory dir, const char *filename) :
+       file(Utils::GetAppResourcePath(dir, filename), ios::binary)
+{
+       if (!file.good())
+               abort();
+}
+
+BinaryFileWriter::~BinaryFileWriter()
+{
+}
+
+void BinaryFileWriter::Write(const void *buffer, size_t size)
+{
+       file.write((const char*)buffer, size);
+}
+
+void BinaryFileWriter::Flush()
+{
+       file.flush();
+       file.seekp(0, ios::beg);
+}
diff --git a/common/src/Utils/Serialization.cpp b/common/src/Utils/Serialization.cpp
new file mode 100644 (file)
index 0000000..e69de29
index a1f3758..1b92772 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Utils.h"
+#include "Utils/Utils.h"
 
 #include <dlog.h>
 #include <app.h>