// // Copyright (c) 2012 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 FScl_CalTodoImpl.cpp * @brief This is the implementation for _CalTodoImpl class. * * This file contains definitions of @e CalTodoImpl class. */ #include #include #include #include #include #include #include #include #include #include #include #include "FScl_CalendarbookDbConnector.h" #include "FScl_CalendarbookImpl.h" #include "FScl_CalTodoImpl.h" #include "FScl_RecordImpl.h" using namespace Tizen::App; using namespace Tizen::Base; using namespace Tizen::Base::Collection; namespace Tizen { namespace Social { static const double _MIN_LATITUDE = -90.0; static const double _MAX_LATITUDE = 90.0; static const double _MIN_LONGITUDE = -180.0; static const double _MAX_LONGITUDE = 180.0; static const long long _CALENDAR_TODO_NO_START_DATE = -0x7fffffffffffffffLL; static const long long _CALENDAR_TODO_NO_DUE_DATE = 0x7fffffffffffffffLL; _CalTodoImpl::_CalTodoImpl(void) : __reminderListUpdated(false) { calendar_record_h todoHandle = null; result r = _CalendarbookDbConnector::Connect(); SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM)); int errorCode = calendar_record_create(_calendar_todo._uri, &todoHandle); SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()) { DateTime tmpDateTime; // Set start date to current date Tizen::System::SystemTime::GetCurrentTime(tmpDateTime); if (IsFailed(GetLastResult())) { tmpDateTime = _CalendarbookImpl::GetMinDateTime(); ClearLastResult(); } tmpDateTime.SetValue(tmpDateTime.GetYear(), tmpDateTime.GetMonth(), tmpDateTime.GetDay(), 0, 0, 0); calendar_time_s convertedCalendarTime; convertedCalendarTime.type = CALENDAR_TIME_UTIME; convertedCalendarTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(tmpDateTime); calendar_record_set_caltime(todoHandle, _calendar_todo.start_time, convertedCalendarTime); calendar_record_set_caltime(todoHandle, _calendar_todo.due_time, convertedCalendarTime); } else { calendar_time_s convertedStartTime; convertedStartTime.type = CALENDAR_TIME_UTIME; convertedStartTime.time.utime = _CALENDAR_TODO_NO_START_DATE; calendar_time_s convertedDueTime; convertedDueTime.type = CALENDAR_TIME_UTIME; convertedDueTime.time.utime = _CALENDAR_TODO_NO_DUE_DATE; calendar_record_set_caltime(todoHandle, _calendar_todo.start_time, convertedStartTime); calendar_record_set_caltime(todoHandle, _calendar_todo.due_time, convertedDueTime); } // default value calendar_record_set_int(todoHandle, _calendar_todo.todo_status, CALENDAR_TODO_STATUS_NONE); calendar_record_set_int(todoHandle, _calendar_todo.priority, CALENDAR_TODO_PRIORITY_NORMAL); calendar_record_set_int(todoHandle, _calendar_todo.sensitivity, CALENDAR_SENSITIVITY_PUBLIC); calendar_record_set_int(todoHandle, _calendar_todo.calendar_book_id, INVALID_RECORD_ID); __todoRecord.ResetHandle(todoHandle); r = __reminderList.Construct(); SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); } _CalTodoImpl::_CalTodoImpl(const _CalTodoImpl& rhs) : __reminderListUpdated(false) { calendar_record_h todoHandle = null; result r = _CalendarbookDbConnector::Connect(); SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM)); int errorCode = calendar_record_clone(rhs.__todoRecord.GetHandle(), &todoHandle); SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); __todoRecord.ResetHandle(todoHandle); r = __reminderList.Construct(); SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); } _CalTodoImpl::~_CalTodoImpl(void) { __reminderList.RemoveAll(true); result r = _CalendarbookDbConnector::Disconnect(); SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM)); } _CalTodoImpl& _CalTodoImpl::operator =(const _CalTodoImpl& rhs) { if (this == &rhs) { return *this; } __reminderList.RemoveAll(true); __reminderListUpdated = false; calendar_record_h todoHandle = null; int errorCode = calendar_record_clone(rhs.__todoRecord.GetHandle(), &todoHandle); SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); __todoRecord.ResetHandle(todoHandle); return *this; } String _CalTodoImpl::GetSubject(void) const { char* pSubject = null; calendar_record_get_str_p(__todoRecord.GetHandle(), _calendar_todo.summary, &pSubject); return String(pSubject); } String _CalTodoImpl::GetDescription(void) const { char* pDescription = null; calendar_record_get_str_p(__todoRecord.GetHandle(), _calendar_todo.description, &pDescription); return String(pDescription); } DateTime _CalTodoImpl::GetStartDate(void) const { ClearLastResult(); calendar_time_s tmpCalendarTime; calendar_record_get_caltime(__todoRecord.GetHandle(), _calendar_todo.start_time, &tmpCalendarTime); if (tmpCalendarTime.time.utime == _CALENDAR_TODO_NO_START_DATE) { SetLastResult(E_DATA_NOT_FOUND); return DateTime::GetMinValue(); } DateTime tmpDateTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(tmpCalendarTime.time.utime); return tmpDateTime; } DateTime _CalTodoImpl::GetDueDate(void) const { ClearLastResult(); calendar_time_s tmpCalendarTime; calendar_record_get_caltime(__todoRecord.GetHandle(), _calendar_todo.due_time, &tmpCalendarTime); if (tmpCalendarTime.time.utime == _CALENDAR_TODO_NO_DUE_DATE) { SetLastResult(E_DATA_NOT_FOUND); return DateTime::GetMinValue(); } DateTime tmpDateTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(tmpCalendarTime.time.utime); return tmpDateTime; } TodoPriority _CalTodoImpl::GetPriority(void) const { int srcPriority = CALENDAR_EVENT_PRIORITY_LOW; TodoPriority priority = TODO_PRIORITY_NORMAL; calendar_record_get_int(__todoRecord.GetHandle(), _calendar_todo.priority, &srcPriority); switch (srcPriority) { case CALENDAR_TODO_PRIORITY_LOW: priority = TODO_PRIORITY_LOW; break; case CALENDAR_TODO_PRIORITY_NONE: // fall through case CALENDAR_TODO_PRIORITY_NORMAL: priority = TODO_PRIORITY_NORMAL; break; case CALENDAR_TODO_PRIORITY_HIGH: priority = TODO_PRIORITY_HIGH; break; default: SysLog(NID_SCL, "The priority value is invalid. priority = %d", srcPriority); priority = TODO_PRIORITY_NORMAL; break; } return priority; } TodoStatus _CalTodoImpl::GetStatus(void) const { int srcStatus = CALENDAR_EVENT_STATUS_NONE; TodoStatus status = TODO_STATUS_NONE; calendar_record_get_int(__todoRecord.GetHandle(), _calendar_todo.todo_status, &srcStatus); switch (srcStatus) { case CALENDAR_TODO_STATUS_NONE: status = TODO_STATUS_NONE; break; case CALENDAR_TODO_STATUS_NEEDS_ACTION: status = TODO_STATUS_NEEDS_ACTION; break; case CALENDAR_TODO_STATUS_COMPLETED: status = TODO_STATUS_COMPLETED; break; case CALENDAR_TODO_STATUS_IN_PROCESS: status = TODO_STATUS_IN_PROCESS; break; case CALENDAR_TODO_STATUS_CANCELED: status = TODO_STATUS_CANCELLED; break; default: SysLog(NID_SCL, "The status value is invalid. status = %d", srcStatus); status = TODO_STATUS_NONE; break; } return status; } RecordSensitivity _CalTodoImpl::GetSensitivity(void) const { int srcSensitivity = CALENDAR_SENSITIVITY_PUBLIC; RecordSensitivity sensitivity = SENSITIVITY_PUBLIC; calendar_record_get_int(__todoRecord.GetHandle(), _calendar_todo.sensitivity, &srcSensitivity); switch (srcSensitivity) { case CALENDAR_SENSITIVITY_PUBLIC: sensitivity = SENSITIVITY_PUBLIC; break; case CALENDAR_SENSITIVITY_PRIVATE: sensitivity = SENSITIVITY_PRIVATE; break; case CALENDAR_SENSITIVITY_CONFIDENTIAL: sensitivity = SENSITIVITY_CONFIDENTIAL; break; default: SysLog(NID_SCL, "The sensitivity value is invalid. sensitivity = %d", srcSensitivity); sensitivity = SENSITIVITY_PUBLIC; break; } return sensitivity; } DateTime _CalTodoImpl::GetLastRevisedTime(void) const { long long lastModifiedTime = 0; calendar_record_get_lli(__todoRecord.GetHandle(), _calendar_todo.last_modified_time, &lastModifiedTime); return _CalendarbookUtil::ConvertEpochTimeToDateTime(lastModifiedTime); } result _CalTodoImpl::SetSubject(const String& subject) { if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()) { SysTryReturnResult(NID_SCL, subject.GetLength() <= MAX_TODO_SUBJECT_LENGTH, E_INVALID_ARG, "Invalid argument is used. The length of the subject exceeds MAX_TODO_SUBJECT_LENGTH."); } std::unique_ptr pConvertedSubject(_StringConverter::CopyToCharArrayN(subject)); SysTryReturnResult(NID_SCL, pConvertedSubject != null, E_OUT_OF_MEMORY, "Memory allocation failed."); int errorCode = calendar_record_set_str(__todoRecord.GetHandle(), _calendar_todo.summary, pConvertedSubject.get()); SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed."); return E_SUCCESS; } result _CalTodoImpl::SetDescription(const String& description) { if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()) { SysTryReturnResult(NID_SCL, description.GetLength() <= MAX_TODO_DESCRIPTION_LENGTH, E_INVALID_ARG, "Invalid argument is used. The length of the description exceeds MAX_TODO_SUBJECT_LENGTH."); } std::unique_ptr pConvertedDescription(_StringConverter::CopyToCharArrayN(description)); SysTryReturnResult(NID_SCL, pConvertedDescription != null, E_OUT_OF_MEMORY, "Memory allocation failed."); int errorCode = calendar_record_set_str(__todoRecord.GetHandle(), _calendar_todo.description, pConvertedDescription.get()); SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed."); return E_SUCCESS; } result _CalTodoImpl::SetStartAndDueDate(const DateTime& startDate, const DateTime& dueDate) { SysTryReturnResult(NID_SCL, _CalendarbookUtil::CheckValidDateTime(startDate), E_INVALID_ARG, "Invalid argument is used. The start date is invalid."); SysTryReturnResult(NID_SCL, _CalendarbookUtil::CheckValidDateTime(dueDate), E_INVALID_ARG, "Invalid argument is used. The end date is invalid."); SysTryReturnResult(NID_SCL, startDate <= dueDate, E_INVALID_ARG, "Invalid argument is used. The end date is earlier than the start date."); calendar_time_s convertedStartTime; calendar_time_s convertedDueTime; convertedStartTime.type = CALENDAR_TIME_UTIME; convertedStartTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(startDate); convertedDueTime.type = CALENDAR_TIME_UTIME; convertedDueTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(dueDate); calendar_record_set_caltime(__todoRecord.GetHandle(), _calendar_todo.start_time, convertedStartTime); calendar_record_set_caltime(__todoRecord.GetHandle(), _calendar_todo.due_time, convertedDueTime); return E_SUCCESS; } result _CalTodoImpl::SetStartDate(const DateTime& startDate) { calendar_time_s convertedStartTime; convertedStartTime.type = CALENDAR_TIME_UTIME; if (startDate == DateTime::GetMinValue()) { convertedStartTime.time.utime = _CALENDAR_TODO_NO_START_DATE; } else if (startDate < _CalendarbookImpl::GetMinDateTime() || startDate > _CalendarbookImpl::GetMaxDateTime()) { SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The startDate is invalid.", GetErrorMessage(E_INVALID_ARG)); return E_INVALID_ARG; } else { calendar_time_s convertedDueTime; calendar_record_get_caltime(__todoRecord.GetHandle(), _calendar_todo.due_time, &convertedDueTime); convertedStartTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(startDate); if (convertedStartTime.time.utime > convertedDueTime.time.utime) { convertedDueTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(DateTime::GetMinValue()); calendar_record_set_caltime(__todoRecord.GetHandle(), _calendar_todo.due_time, convertedDueTime); } } calendar_record_set_caltime(__todoRecord.GetHandle(), _calendar_todo.start_time, convertedStartTime); return E_SUCCESS; } result _CalTodoImpl::SetDueDate(const DateTime& dueDate) { calendar_time_s convertedDueTime; convertedDueTime.type = CALENDAR_TIME_UTIME; if (dueDate == DateTime::GetMinValue()) { convertedDueTime.time.utime = _CALENDAR_TODO_NO_DUE_DATE; } else if (dueDate < _CalendarbookImpl::GetMinDateTime() || dueDate > _CalendarbookImpl::GetMaxDateTime()) { SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The dueDate is invalid.", GetErrorMessage(E_INVALID_ARG)); return E_INVALID_ARG; } else { calendar_time_s convertedStartTime; calendar_record_get_caltime(__todoRecord.GetHandle(), _calendar_todo.start_time, &convertedStartTime); convertedDueTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(dueDate); if (convertedStartTime.time.utime > convertedDueTime.time.utime) { convertedStartTime.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(DateTime::GetMinValue()); calendar_record_set_caltime(__todoRecord.GetHandle(), _calendar_todo.start_time, convertedStartTime); } } calendar_record_set_caltime(__todoRecord.GetHandle(), _calendar_todo.due_time, convertedDueTime); return E_SUCCESS; } void _CalTodoImpl::SetPriority(TodoPriority priority) { calendar_record_set_int(__todoRecord.GetHandle(), _calendar_todo.priority, _CalendarbookUtil::ConvertTodoPriorityToCSTodoPriority(priority)); } void _CalTodoImpl::SetStatus(TodoStatus status) { calendar_record_set_int(__todoRecord.GetHandle(), _calendar_todo.todo_status, _CalendarbookUtil::ConvertTodoStatusToCSTodoStatus(status)); } void _CalTodoImpl::SetSensitivity(RecordSensitivity sensitivity) { calendar_record_set_int(__todoRecord.GetHandle(), _calendar_todo.sensitivity, _CalendarbookUtil::ConvertSensitivityToCSSensitivity(sensitivity)); } void _CalTodoImpl::SetLocation(const String& location) { std::unique_ptr pConvertedLocation(_StringConverter::CopyToCharArrayN(location)); SysTryReturnVoidResult(NID_SCL, pConvertedLocation != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); int errorCode = calendar_record_set_str(__todoRecord.GetHandle(), _calendar_todo.location, pConvertedLocation.get()); SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); } String _CalTodoImpl::GetLocation(void) const { char* pLocation = null; calendar_record_get_str_p(__todoRecord.GetHandle(), _calendar_todo.location, &pLocation); return String(pLocation); } result _CalTodoImpl::SetCoordinates(double latitude, double longitude) { SysTryReturnResult(NID_SCL, latitude >= _MIN_LATITUDE && latitude <= _MAX_LATITUDE, E_INVALID_ARG, "Invalid argument is used. The latitude is out of range."); SysTryReturnResult(NID_SCL, longitude >= _MIN_LONGITUDE && longitude <= _MAX_LONGITUDE, E_INVALID_ARG, "Invalid argument is used. The longitude is out of range."); calendar_record_set_double(__todoRecord.GetHandle(), _calendar_todo.latitude, latitude); calendar_record_set_double(__todoRecord.GetHandle(), _calendar_todo.longitude, longitude); return E_SUCCESS; } void _CalTodoImpl::GetCoordinates(double& latitude, double& longitude) const { calendar_record_get_double(__todoRecord.GetHandle(), _calendar_todo.latitude, &latitude); calendar_record_get_double(__todoRecord.GetHandle(), _calendar_todo.longitude, &longitude); } result _CalTodoImpl::AddReminder(const Reminder& reminder) { calendar_record_h tmpAlarmHandle = null; std::unique_ptr pTmpAlarmTone(_StringConverter::CopyToCharArrayN(reminder.GetSoundFile())); SysTryReturnResult(NID_SCL, pTmpAlarmTone != null, E_OUT_OF_MEMORY, "Memory allocation failed."); int errorCode = calendar_record_create(_calendar_alarm._uri, &tmpAlarmHandle); SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed."); int convertedTimeUnit = 0; ReminderTimeUnit timeUnit = reminder.GetTimeUnit(); switch (timeUnit) { case REMINDER_TIME_UNIT_MINUTE: convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_MINUTE; break; case REMINDER_TIME_UNIT_HOUR: convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_HOUR; break; case REMINDER_TIME_UNIT_DAY: convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_DAY; break; case REMINDER_TIME_UNIT_WEEK: convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_WEEK; break; case REMINDER_TIME_UNIT_NONE: convertedTimeUnit = CALENDAR_ALARM_TIME_UNIT_SPECIFIC; break; default: break; } calendar_record_set_int(tmpAlarmHandle, _calendar_alarm.tick_unit, convertedTimeUnit); calendar_record_set_int(tmpAlarmHandle, _calendar_alarm.tick, reminder.GetTimeOffset()); errorCode = calendar_record_set_str(tmpAlarmHandle, _calendar_alarm.tone, pTmpAlarmTone.get()); SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed."); if (convertedTimeUnit == CALENDAR_ALARM_TIME_UNIT_SPECIFIC) { calendar_record_set_lli(tmpAlarmHandle, _calendar_alarm.time, _CalendarbookUtil::ConvertDateTimeToEpochTime(reminder.GetAbsoluteTime())); } errorCode = calendar_record_add_child_record(__todoRecord.GetHandle(), _calendar_todo.calendar_alarm, tmpAlarmHandle); SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed."); __reminderList.RemoveAll(true); __reminderListUpdated = false; return E_SUCCESS; } result _CalTodoImpl::RemoveReminderAt(int index) { calendar_record_h tmpAlarmHandle = null; calendar_record_get_child_record_at_p(__todoRecord.GetHandle(), _calendar_todo.calendar_alarm, index, &tmpAlarmHandle); SysTryReturnResult(NID_SCL, tmpAlarmHandle != null, E_OUT_OF_RANGE, "The index is either equal to or greater than the number of reminders or less than 0."); calendar_record_remove_child_record(__todoRecord.GetHandle(), _calendar_todo.calendar_alarm, tmpAlarmHandle); __reminderList.RemoveAll(true); __reminderListUpdated = false; return E_SUCCESS; } const IList& _CalTodoImpl::GetAllReminders(void) const { ClearLastResult(); if (__reminderListUpdated == false) { result r = _CalendarbookUtil::ConvertTodoAlarmsToReminderList(__todoRecord.GetHandle(), __reminderList); SysTryReturn(NID_SCL, r == E_SUCCESS, __reminderList, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); __reminderListUpdated = true; } return __reminderList; } RecordId _CalTodoImpl::GetCalendarId(void) const { int srcCalendarbookId = 0; calendar_record_get_int(__todoRecord.GetHandle(), _calendar_todo.calendar_book_id, &srcCalendarbookId); return srcCalendarbookId; } void _CalTodoImpl::SetRecordHandle(calendar_record_h todoHandle) { __todoRecord.ResetHandle(todoHandle); } calendar_record_h _CalTodoImpl::GetRecordHandle(void) const { return __todoRecord.GetHandle(); } CalTodo* _CalTodoImpl::CreateDefaultInstanceN(void) { return new (std::nothrow) CalTodo(); } _CalTodoImpl* _CalTodoImpl::GetInstance(CalTodo& todo) { return todo.__pCalTodoImpl; } const _CalTodoImpl* _CalTodoImpl::GetInstance(const CalTodo& todo) { return todo.__pCalTodoImpl; } }} // Tizen::Social