summaryrefslogtreecommitdiff
path: root/mobile_src/Alarm/AlarmConverter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mobile_src/Alarm/AlarmConverter.cpp')
-rw-r--r--mobile_src/Alarm/AlarmConverter.cpp256
1 files changed, 256 insertions, 0 deletions
diff --git a/mobile_src/Alarm/AlarmConverter.cpp b/mobile_src/Alarm/AlarmConverter.cpp
new file mode 100644
index 0000000..53c93b4
--- /dev/null
+++ b/mobile_src/Alarm/AlarmConverter.cpp
@@ -0,0 +1,256 @@
+//
+// Tizen Web Device API
+// 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.
+//
+
+#include <vector>
+#include <app.h>
+#include <time.h>
+#include <CommonsJavaScript/Converter.h>
+#include <CommonsJavaScript/Validator.h>
+#include <CommonsJavaScript/JSUtils.h>
+#include <CommonsJavaScript/JSDOMExceptionFactory.h>
+#include "AlarmConverter.h"
+#include "JSAlarmAbsolute.h"
+#include "AlarmAbsolute.h"
+#include "JSAlarmRelative.h"
+#include "AlarmRelative.h"
+#include <Logger.h>
+
+namespace DeviceAPI {
+namespace Alarm {
+
+using namespace WrtDeviceApis::Commons;
+using namespace WrtDeviceApis::CommonsJavaScript;
+
+AlarmConverter::AlarmConverter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
+{
+
+}
+
+AlarmConverter::~AlarmConverter()
+{
+
+}
+
+int AlarmConverter::toNativeAlarmValue(std::vector<std::string> daysOfTheWeek)
+{
+ int nativeValue = 0;
+
+ for( unsigned int i=0; i<daysOfTheWeek.size(); i++ )
+ {
+ if( daysOfTheWeek[i]=="SU" )
+ nativeValue = nativeValue | ALARM_WEEK_FLAG_SUNDAY;
+ else if( daysOfTheWeek[i]=="MO" )
+ nativeValue = nativeValue | ALARM_WEEK_FLAG_MONDAY ;
+ else if( daysOfTheWeek[i]=="TU" )
+ nativeValue = nativeValue | ALARM_WEEK_FLAG_TUESDAY ;
+ else if( daysOfTheWeek[i]=="WE" )
+ nativeValue = nativeValue | ALARM_WEEK_FLAG_WEDNESDAY;
+ else if( daysOfTheWeek[i]=="TH" )
+ nativeValue = nativeValue | ALARM_WEEK_FLAG_THURSDAY ;
+ else if( daysOfTheWeek[i]=="FR" )
+ nativeValue = nativeValue | ALARM_WEEK_FLAG_FRIDAY ;
+ else if( daysOfTheWeek[i]=="SA" )
+ nativeValue = nativeValue | ALARM_WEEK_FLAG_SATURDAY ;
+ }
+ return nativeValue;
+}
+
+std::vector<std::string> AlarmConverter::convertFlagToDaysOfTheWeek(int byDayValue)
+{
+ std::vector<std::string> daysOfTheWeek;
+
+ if(byDayValue & ALARM_WEEK_FLAG_SUNDAY)
+ daysOfTheWeek.push_back("SU");
+ if(byDayValue & ALARM_WEEK_FLAG_MONDAY)
+ daysOfTheWeek.push_back("MO");
+ if(byDayValue & ALARM_WEEK_FLAG_TUESDAY)
+ daysOfTheWeek.push_back("TU");
+ if(byDayValue & ALARM_WEEK_FLAG_WEDNESDAY)
+ daysOfTheWeek.push_back("WE");
+ if(byDayValue & ALARM_WEEK_FLAG_THURSDAY)
+ daysOfTheWeek.push_back("TH");
+ if(byDayValue & ALARM_WEEK_FLAG_FRIDAY)
+ daysOfTheWeek.push_back("FR");
+ if(byDayValue & ALARM_WEEK_FLAG_SATURDAY)
+ daysOfTheWeek.push_back("SA");
+
+ return daysOfTheWeek;
+}
+
+service_h AlarmConverter::toService(std::string id)
+{
+ service_h service;
+ service_create(&service);
+
+ service_set_operation(service, SERVICE_OPERATION_DEFAULT);
+ service_set_package(service, id.c_str());
+ return service;
+}
+service_h AlarmConverter::toService(std::string id, std::string page)
+{
+ service_h service;
+ service_create(&service);
+
+ service_set_operation(service, SERVICE_OPERATION_DEFAULT);
+ service_set_package(service, id.c_str());
+ return service;
+}
+
+bool AlarmConverter::toAlarmAbsolutePtr(int id, service_h handle, AlarmAbsolutePtr privateData)
+{
+ char* dateString;
+ char* alarmType;
+ struct tm date;
+ memset(&date, 0, sizeof(tm));
+ int error = ALARM_ERROR_NONE;
+
+ error = service_get_extra_data(handle, ALARM_ALSOLUTE_DATE_KEY, &dateString);
+
+ LoggerI("Date Strng = " << dateString);
+ if(error != SERVICE_ERROR_NONE)
+ {
+ LoggerE("Fail to get AlarmDelay");
+ return false;
+ }
+ sscanf(dateString, "%d %d %d %d %d %d", &date.tm_year, &date.tm_mon,
+ &date.tm_mday, &date.tm_hour, &date.tm_min, &date.tm_sec);
+ mktime(&date);
+
+ LoggerI("Converter AlarmAbsolute Date = " << " Sec: " << date.tm_sec << ", Min: "<< date.tm_min
+ << ", Hour: " << date.tm_hour << ", Day: " << date.tm_mday << ", MON: " << date.tm_mon
+ << ", Year: " << date.tm_year);
+
+ service_get_extra_data(handle, ALARM_ALSOLUTE_RECURRENCE_TYPE_KEY, &alarmType);
+
+ if(!strcmp(alarmType, ALARM_ALSOLUTE_RECURRENCE_TYPE_INTERVAL)) {
+ int interval = 0;
+ alarm_get_scheduled_period(id, &interval);
+ LoggerI("interval type alarm: "<<interval);
+ privateData->setInterval(interval);
+ } else if(!strcmp(alarmType, ALARM_ALSOLUTE_RECURRENCE_TYPE_BYDAYVALUE)) {
+ int byDayValue =0;
+ error = alarm_get_scheduled_recurrence_week_flag(id, &byDayValue);
+ LoggerI("daysOfWeek type alarm: "<<byDayValue<<", error: "<<error);
+ if(error==ALARM_ERROR_NONE && byDayValue>0) {
+ std::vector<std::string> result;
+ result = convertFlagToDaysOfTheWeek(byDayValue);
+ privateData->setByDayRecurrence(result);
+ } else {
+ LoggerE("Can't get the recurrence week flag.");
+ }
+ }
+
+ privateData->setId(id);
+ privateData->setDate(date);
+
+ return true;
+}
+
+bool AlarmConverter::toAlarmRelativePtr(int id, service_h handle, AlarmRelativePtr privateData)
+{
+ int interval = 0;
+ char* delayString;
+ int delay;
+
+ int error = ALARM_ERROR_NONE;
+ error = alarm_get_scheduled_period(id, &interval);
+ if(error != ALARM_ERROR_NONE) {
+ interval = 0;
+ }
+
+ error = service_get_extra_data(handle, ALARM_RELATIVE_DELAY_KEY, &delayString);
+ if(error != SERVICE_ERROR_NONE)
+ {
+ LoggerE("Fail to get AlarmDelay");
+ return false;
+ }
+ delay = atoi(delayString);
+ free(delayString);
+
+ privateData->setId(id);
+ privateData->setDelay(delay);
+ privateData->setPeriod(interval);
+
+ return true;
+}
+
+int AlarmConverter::toNativeInterval(std::string freq, std::string interval)
+{
+ int freqInSecond = 0;
+ int intervalValue = toInt(interval);
+
+ if (!freq.compare(ALARM_PROPERTY_MINUTELY_RECURRENCE))
+ freqInSecond = 60;
+ else if (!freq.compare(ALARM_PROPERTY_HOURLY_RECURRENCE))
+ freqInSecond = 3600;
+ else if (!freq.compare(ALARM_PROPERTY_DAILY_RECURRENCE))
+ freqInSecond = 3600*24;
+ else if (!freq.compare(ALARM_PROPERTY_WEEKLY_RECURRENCE))
+ freqInSecond = 3600 * 24 * 7;
+
+ return freqInSecond * intervalValue;
+}
+
+bool AlarmConverter::toAlarmService(service_h service, DeviceAPI::Application::ApplicationControlPtr appservice)
+ {
+ if (appservice->getOperation().compare("") != 0) {
+ service_set_operation(service, appservice->getOperation().c_str() );
+ } else {
+ LoggerD("Error. operation is madatory field. cannot be null");
+ return false;
+ }
+
+ if (appservice->getUri().compare("") != 0) {
+ service_set_uri(service, appservice->getUri().c_str() );
+ }
+
+ if (appservice->getMime().compare("") != 0) {
+ service_set_mime(service, appservice->getMime().c_str() );
+ }
+
+ std::vector<DeviceAPI::Application::ApplicationControlDataPtr> appControlDataArray = appservice->getAppControlDataArray();
+
+ if (!appControlDataArray.empty()) {
+ std::string key;
+ const char** arr = NULL;
+
+ for (size_t i = 0; i < appControlDataArray.size(); ++i) {
+ key = appControlDataArray.at(i)->getKey();
+ if (key.empty()) {
+ return false;
+ }
+ std::vector<std::string> valueArray = appControlDataArray.at(i)->getValue();
+ size_t size = valueArray.size();
+
+ arr = (const char**)calloc(sizeof(char*), size);
+
+ for (size_t j = 0; j < size; j++) {
+ arr[j] = valueArray.at(j).c_str();
+ }
+
+ service_add_extra_data_array(service, key.c_str(), arr, size);
+
+ if (arr)
+ free(arr);
+ }
+ }
+ return true;
+}
+
+}
+}
+