--- /dev/null
+//
+// Open Service Platform
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// 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.
+//
+
+/**
+ * @file CalendarConditionHandler.cpp
+ * @brief This is the declaration file of the _CalendarConditionHandler class.
+ *
+ */
+
+#include <new>
+#include <unique_ptr.h>
+#include <FBaseInteger.h>
+#include <FBaseLongLong.h>
+#include <FBaseDateTime.h>
+#include <FBaseString.h>
+#include <FBaseColHashMap.h>
+#include <FBaseColAllElementsDeleter.h>
+
+#include "CalendarConditionHandler.h"
+
+using namespace std;
+
+using namespace Tizen::App;
+using namespace Tizen::Base;
+using namespace Tizen::Base::Collection;
+
+static const char* REMINDER_CB_KEY_ID = "id";
+static const char* REMINDER_CB_KEY_TYPE = "type";
+static const char* REMINDER_CB_KEY_TIME = "time";
+static const char* REMINDER_CB_KEY_TICK = "tick";
+static const char* REMINDER_CB_KEY_UNIT = "unit";
+
+static const char* REMINDER_CB_VALUE_TYPE_EVENT = "1";
+static const char* REMINDER_CB_VALUE_TYPE_TODO = "2";
+
+static const wchar_t* REMINDER_ARG_KEY_RECORD_ID = L"recordid";
+static const wchar_t* REMINDER_ARG_KEY_TYPE = L"recordtype";
+static const wchar_t* REMINDER_ARG_KEY_TIME = L"time";
+static const wchar_t* REMINDER_ARG_KEY_OFFSET = L"offset";
+static const wchar_t* REMINDER_ARG_KEY_UNIT = L"unit";
+
+static const wchar_t* REMINDER_ARG_VALUE_TYPE_EVENT = L"event";
+static const wchar_t* REMINDER_ARG_VALUE_TYPE_TODO = L"todo";
+
+static const wchar_t* REMINDER_ARG_VALUE_UNIT_MINUTE = L"minute";
+static const wchar_t* REMINDER_ARG_VALUE_UNIT_HOUR = L"hour";
+static const wchar_t* REMINDER_ARG_VALUE_UNIT_DAY = L"day";
+static const wchar_t* REMINDER_ARG_VALUE_UNIT_WEEK = L"week";
+static const wchar_t* REMINDER_ARG_VALUE_UNIT_NONE = L"none";
+
+static const int MSEC_UNIT = 1000;
+static const long long EPOCH_YEAR_INTERVAL_SECONDS = 62135596800;
+
+_CalendarConditionHandler* pCalendarConditionHandler = null;
+int RefCalendarConditionHandler = 0;
+
+_CalendarConditionHandler*
+_CalendarConditionHandler::GetInstance()
+{
+ if(pCalendarConditionHandler == null)
+ {
+ pCalendarConditionHandler = new (std::nothrow) _CalendarConditionHandler;
+ }
+ RefCalendarConditionHandler ++;
+ return pCalendarConditionHandler;
+}
+
+void
+_CalendarConditionHandler::ReleaseInstance()
+{
+ if(RefCalendarConditionHandler == 0)
+ {
+ return;
+ }
+
+ RefCalendarConditionHandler --;
+ if(RefCalendarConditionHandler == 0)
+ {
+ delete pCalendarConditionHandler;
+ pCalendarConditionHandler = null;
+ }
+}
+
+_CalendarConditionHandler::_CalendarConditionHandler()
+{
+}
+
+_CalendarConditionHandler::~_CalendarConditionHandler()
+{
+}
+
+result
+_CalendarConditionHandler::Register( _AppLaunchCondition& appLaunchCondition)
+{
+ SysLog(NID_APP, "Register(calendar reminder) started. condition : %S", appLaunchCondition.GetConditionString().GetPointer());
+
+ int errorCode = 0;
+
+ // set user data
+ appLaunchCondition.SetUserData(this);
+
+ errorCode = calendar_connect();
+ SysTryReturnResult(NID_APP, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
+
+ std::unique_ptr<String> pAppId(new (std::nothrow) String(appLaunchCondition.GetAppId()));
+ SysTryCatch(NID_APP, pAppId != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
+
+ errorCode = calendar_reminder_add_cb(ReminderCallback, &appLaunchCondition);
+ SysTryCatch(NID_APP, errorCode == CALENDAR_ERROR_NONE, , E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
+
+ return E_SUCCESS;
+
+CATCH:
+
+ errorCode = calendar_disconnect();
+
+ return E_SYSTEM;
+}
+
+
+result
+_CalendarConditionHandler::Unregister( _AppLaunchCondition& appLaunchCondition)
+{
+ SysLog(NID_APP, "Unregister(calendar reminder) started");
+
+ int errorCode = 0;
+
+ errorCode = calendar_reminder_remove_cb(ReminderCallback, null);
+ SysTryReturnResult(NID_APP, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
+
+ SysLog(NID_APP, "Callback is successfully removed. (%S)", appLaunchCondition.GetAppId().GetPointer());
+
+ errorCode = calendar_disconnect();
+
+ return E_SUCCESS;
+}
+
+void
+_CalendarConditionHandler::ReminderCallback(bundle* pBundle, void* pUserData)
+{
+ _AppLaunchCondition* pAppLaunchCondition = static_cast<_AppLaunchCondition*>(pUserData);
+ SysTryReturnVoidResult(NID_APP, pAppLaunchCondition != null, E_INVALID_STATE, "[E_INVALID_STATE] pUserData should NOT be null.");
+
+ _CalendarConditionHandler* pHandler = dynamic_cast<_CalendarConditionHandler*>(pAppLaunchCondition->GetUserData());
+ SysTryReturnVoidResult(NID_APP, pHandler != null, E_INVALID_STATE, "user data is null");
+
+ SysTryReturnVoidResult(NID_APP, pBundle != null, E_INVALID_STATE, "[E_INVALID_STATE] pBundle should NOT be null.");
+
+ std::unique_ptr<HashMap, AllElementsDeleter> pResultMap(new (std::nothrow) HashMap());
+ SysTryReturnVoidResult(NID_APP, pResultMap != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
+
+ result r = pResultMap->Construct();
+ SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
+
+ // get values
+ const char* pStrRecordId = bundle_get_val(pBundle, REMINDER_CB_KEY_ID);
+ SysTryReturnVoidResult(NID_APP, pStrRecordId != null, E_INVALID_STATE, "[E_INVALID_STATE] id should NOT be null.");
+
+ const char* pStrType = bundle_get_val(pBundle, REMINDER_CB_KEY_TYPE);
+ SysTryReturnVoidResult(NID_APP, pStrType != null, E_INVALID_STATE, "[E_INVALID_STATE] type should NOT be null.");
+
+ const char* pStrTime = bundle_get_val(pBundle, REMINDER_CB_KEY_TIME);
+ SysTryReturnVoidResult(NID_APP, pStrTime != null, E_INVALID_STATE, "[E_INVALID_STATE] time should NOT be null.");
+
+ const char* pStrTick = bundle_get_val(pBundle, REMINDER_CB_KEY_TICK);
+ SysTryReturnVoidResult(NID_APP, pStrTick != null, E_INVALID_STATE, "[E_INVALID_STATE] tick should NOT be null.");
+
+ const char* pStrUnit = bundle_get_val(pBundle, REMINDER_CB_KEY_UNIT);
+ SysTryReturnVoidResult(NID_APP, pStrUnit != null, E_INVALID_STATE, "[E_INVALID_STATE] unit should NOT be null.");
+
+ SysLog(NID_APP, "Reminder callback is called. id : %s, type : %s, time : %s, tick : %s, unit : %s", pStrRecordId, pStrType, pStrTime, pStrTick, pStrUnit);
+
+ // convert
+ std::unique_ptr<String> pRecordType;
+ if (strcmp(pStrType, REMINDER_CB_VALUE_TYPE_EVENT) == 0)
+ {
+ pRecordType.reset(new (std::nothrow) String(REMINDER_ARG_VALUE_TYPE_EVENT));
+ }
+ else if (strcmp(pStrType, REMINDER_CB_VALUE_TYPE_TODO) == 0)
+ {
+ pRecordType.reset(new (std::nothrow) String(REMINDER_ARG_VALUE_TYPE_TODO));
+ }
+
+ int unit = 0;
+ r = Integer::Parse(pStrUnit, unit);
+ int offset = 0;
+ r = Integer::Parse(pStrTick, offset);
+
+ std::unique_ptr<String> pReminderUnit;
+ switch (unit)
+ {
+ case CALENDAR_ALARM_NONE:
+ pReminderUnit.reset(new (std::nothrow) String(REMINDER_ARG_VALUE_UNIT_NONE));
+ offset = 0;
+ break;
+ case CALENDAR_ALARM_TIME_UNIT_SPECIFIC:
+ pReminderUnit.reset(new (std::nothrow) String(REMINDER_ARG_VALUE_UNIT_NONE));
+ offset = 0;
+ break;
+ case CALENDAR_ALARM_TIME_UNIT_MINUTE:
+ pReminderUnit.reset(new (std::nothrow) String(REMINDER_ARG_VALUE_UNIT_MINUTE));
+ break;
+ case CALENDAR_ALARM_TIME_UNIT_HOUR:
+ pReminderUnit.reset(new (std::nothrow) String(REMINDER_ARG_VALUE_UNIT_HOUR));
+ break;
+ case CALENDAR_ALARM_TIME_UNIT_DAY:
+ pReminderUnit.reset(new (std::nothrow) String(REMINDER_ARG_VALUE_UNIT_DAY));
+ break;
+ case CALENDAR_ALARM_TIME_UNIT_WEEK:
+ pReminderUnit.reset(new (std::nothrow) String(REMINDER_ARG_VALUE_UNIT_WEEK));
+ break;
+ default:
+ SysLogException(NID_APP, E_INVALID_STATE, "[E_INVALID_STATE] unit should NOT be null.");
+ return;
+ }
+
+ std::unique_ptr<String> pReminderOffset(new (std::nothrow) String());
+ pReminderOffset->Append(offset);
+
+ long long time = 0;
+ r = LongLong::Parse(pStrTime, time);
+ std::unique_ptr<String> pTime(new (std::nothrow) String(ConvertEpochTimeToDateTime(time).ToString()));
+
+ r = pResultMap->Add(new String(REMINDER_ARG_KEY_RECORD_ID), new String(pStrRecordId));
+ SysTryReturnVoidResult(NID_APP, r == E_SUCCESS, r, "[%s] Failed to add id to the hash map.", GetErrorMessage(r));
+
+ r = pResultMap->Add(new String(REMINDER_ARG_KEY_TYPE), pRecordType.release());
+ SysTryReturnVoidResult(NID_APP, r == E_SUCCESS, r, "[%s] Failed to add type to the hash map.", GetErrorMessage(r));
+
+ r = pResultMap->Add(new String(REMINDER_ARG_KEY_OFFSET), pReminderOffset.release());
+ SysTryReturnVoidResult(NID_APP, r == E_SUCCESS, r, "[%s] Failed to add offset to the hash map.", GetErrorMessage(r));
+
+ r = pResultMap->Add(new String(REMINDER_ARG_KEY_UNIT), pReminderUnit.release());
+ SysTryReturnVoidResult(NID_APP, r == E_SUCCESS, r, "[%s] Failed to add unit to the hash map.", GetErrorMessage(r));
+
+ r = pResultMap->Add(new String(REMINDER_ARG_KEY_TIME), pTime.release());
+ SysTryReturnVoidResult(NID_APP, r == E_SUCCESS, r, "[%s] Failed to add time to the hash map.", GetErrorMessage(r));
+
+ r = pHandler->Fire(*pAppLaunchCondition, pResultMap.release());
+ SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
+}
+
+DateTime
+_CalendarConditionHandler::ConvertEpochTimeToDateTime(long long int dateTime)
+{
+ DateTime tmpDateTime;
+ tmpDateTime.SetValue(TimeSpan((dateTime + EPOCH_YEAR_INTERVAL_SECONDS) * MSEC_UNIT));
+ return tmpDateTime;
+}
+
+//////////////////////////////////////////////////////////////
+// All plugins must provide both a creation and a destruction function
+/////////////////////////////////////////////////////////////
+extern "C"
+{
+_OSP_EXPORT_ _AppLaunchConditionHandlerBase*
+CreatePlugin(void)
+{
+ return _CalendarConditionHandler::GetInstance();
+}
+
+_OSP_EXPORT_ void
+DestroyPlugin(_AppLaunchConditionHandlerBase* p)
+{
+ _CalendarConditionHandler::ReleaseInstance();
+}
+
+}//extern "C"