From b71157ca328586ebe82beee4206d61854220c305 Mon Sep 17 00:00:00 2001 From: Dongjin Choi Date: Wed, 10 Jul 2013 16:58:45 +0900 Subject: [PATCH] Update change log and spec for wrt-plugins-tizen_0.4.52 [model] REDWOOD [binary_type] PDA [customer] OPEN [Issue] intensive test. [Problem] simple absolute path was allowed for resolve api. [Cause] no check. [Solution] deny simple absolute path without file:// scheme. [Bluetooth] Fix bug in getting onconnect of BluetoothHealthApplication [Bluetooth] Use system_info_get_platform_bool() [Application] exception should not be thrown for get/set properties [Issue#] N/A [Problem] The returned value has to be the biggest possible unit without losing the precision. But its unit is smaller one of two TimeDurations. [Cause] It changed to smaller unit before calculating the differce and the result was returned. [Solution] The result to calculate the difference will be changed to the biggest possible unit without losing the precision. [Issue] N/A [Problem] loadMessageAttachment fail after loadMessageBody [Cause] message is not set for attachments [Solution] message set for attachments [team] WebAPI [request] N/A [horizontal_expansion] N/A UnitTC passed. --- packaging/wrt-plugins-tizen.spec | 2 +- src/Application/JSApplication.cpp | 52 ----------------------- src/Application/JSApplication.h | 28 +----------- src/Application/JSApplicationCert.cpp | 11 +++-- src/Application/JSApplicationContext.cpp | 12 +++--- src/Application/JSApplicationControl.cpp | 14 +++--- src/Application/JSApplicationControlData.cpp | 25 ++++++----- src/Application/JSApplicationInformation.cpp | 13 +++--- src/Application/JSApplicationMetaData.cpp | 11 +++-- src/Application/JSRequestedApplicationControl.cpp | 9 ++-- src/Bluetooth/JSBluetoothAdapter.cpp | 2 +- src/Bluetooth/JSBluetoothHealthApplication.cpp | 2 +- src/Filesystem/FilesystemUtils.cpp | 29 ++++++++++++- src/Filesystem/FilesystemUtils.h | 1 + src/Filesystem/JSFilesystemManager.cpp | 11 +++-- src/Filesystem/Manager.cpp | 3 +- src/Filesystem/Node.cpp | 12 +++++- src/Messaging/MailSync.cpp | 17 +++++++- src/TimeUtil/JSTimeDuration.cpp | 3 +- src/TimeUtil/TimeUtilConverter.cpp | 37 ++++++++++++++++ src/TimeUtil/TimeUtilConverter.h | 2 +- 21 files changed, 152 insertions(+), 144 deletions(-) diff --git a/packaging/wrt-plugins-tizen.spec b/packaging/wrt-plugins-tizen.spec index 1600340..0ed41ce 100755 --- a/packaging/wrt-plugins-tizen.spec +++ b/packaging/wrt-plugins-tizen.spec @@ -1,6 +1,6 @@ Name: wrt-plugins-tizen Summary: JavaScript plugins for WebRuntime -Version: 0.4.51 +Version: 0.4.52 Release: 0 Group: Development/Libraries License: Apache License, Version 2.0 diff --git a/src/Application/JSApplication.cpp b/src/Application/JSApplication.cpp index f96dd9b..46a7f90 100644 --- a/src/Application/JSApplication.cpp +++ b/src/Application/JSApplication.cpp @@ -68,14 +68,6 @@ JSClassDefinition JSApplication::m_classInfo = { NULL, //ConvertToType }; -#if 0 -JSStaticValue JSApplication::m_property[] = { - { TIZEN_APPLICATION_APP_INFO, getProperty, NULL, kJSPropertyAttributeReadOnly }, - { TIZEN_APPLICATION_APP_CONTEXT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly }, - { 0, 0, 0, 0 } -}; -#endif - JSStaticFunction JSApplication::m_function[] = { { APPLICATION_FUNCTION_API_EXIT, JSApplication::exit, kJSPropertyAttributeNone }, { APPLICATION_FUNCTION_API_HIDE, JSApplication::hide, kJSPropertyAttributeNone }, @@ -120,50 +112,6 @@ JSValueRef JSApplication::makeObject(JSContextRef ctx, const ApplicationPtr valu return target; } -#if 0 - -bool JSApplication::isObjectOfClass(JSContextRef context, JSValueRef value) -{ - return JSValueIsObjectOfClass(context, value, getClassRef()); -} - -ApplicationPtr JSApplication::getPrivData(JSObjectRef object) -{ - JSApplicationPriv *priv = static_cast(JSObjectGetPrivate(object)); - if (!priv) { - ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null"); - } - ApplicationPtr result = priv->getObject(); - if (!result) { - ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null"); - } - return result; -} - - -JSValueRef JSApplication::getProperty(JSContextRef context, - JSObjectRef object, - JSStringRef propertyName, - JSValueRef* exception) -{ - Try { - ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context); - ApplicationPtr privateData = getPrivData(object); - - if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_APP_INFO)) { - return converter->toJSValueRefFromApplicationInformation(privateData->getAppInfo()); - } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_APP_CONTEXT_ID)) { - return converter->toJSValueRef(privateData->getContextId()); - } - } Catch(WrtDeviceApis::Commons::Exception) { - LoggerE("Exception: " << _rethrown_exception.GetMessage()); - JSWebAPIErrorFactory::postException(context, exception,JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid value error"); - } - - return NULL; -} -#endif - JSValueRef JSApplication::exit(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, diff --git a/src/Application/JSApplication.h b/src/Application/JSApplication.h index 6ad4619..1df90b9 100644 --- a/src/Application/JSApplication.h +++ b/src/Application/JSApplication.h @@ -41,27 +41,9 @@ public: */ static JSClassRef getClassRef(); -/* - static JSValueRef createJSObject(JSContextRef context, - const std::string &name, - const std::string &package, - const std::string &iconPath, - const std::string &version, - const bool &show, - const std::vector &categories, - const time_t &installDate, - const long &installSize); - - static bool isObjectOfClass(JSContextRef context, JSValueRef value); -*/ - - static JSValueRef makeObject(JSContextRef ctx, const ApplicationPtr value); -/* - static ApplicationInformationPtr - getApplicationInformation(JSContextRef context, JSValueRef value); -*/ + /** * hide the application based on application context. */ @@ -113,15 +95,7 @@ private: static JSStaticFunction m_function[]; static JSClassRef m_classRef; -#if 0 - - static ApplicationPtr getPrivData(JSObjectRef object); - static JSValueRef getProperty(JSContextRef context, - JSObjectRef object, - JSStringRef propertyName, - JSValueRef* exception); -#endif }; } diff --git a/src/Application/JSApplicationCert.cpp b/src/Application/JSApplicationCert.cpp index 7153000..7b9f2c4 100644 --- a/src/Application/JSApplicationCert.cpp +++ b/src/Application/JSApplicationCert.cpp @@ -118,14 +118,13 @@ JSValueRef JSApplicationCert::getProperty(JSContextRef context, } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CERT_VALUE)) { return converter.toJSValueRef(privateData->getValue()); } - } catch (const BasePlatformException &err) { - return JSWebAPIErrorFactory::postException(context, exception, err); - } catch (...) { - DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured"); - return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + LoggerE("Exception occured while get property"); + return JSValueMakeUndefined(context); } - return JSValueMakeUndefined(context); + /* do not return undefined object to find method */ + return NULL; } } diff --git a/src/Application/JSApplicationContext.cpp b/src/Application/JSApplicationContext.cpp index 1bc2408..8802dbf 100644 --- a/src/Application/JSApplicationContext.cpp +++ b/src/Application/JSApplicationContext.cpp @@ -150,15 +150,13 @@ JSValueRef JSApplicationContext::getProperty(JSContextRef context, } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTEXT_ID)) { return converter.toJSValueRef(privateData->getContextId()); } - - } catch (const BasePlatformException &err) { - return JSWebAPIErrorFactory::postException(context, exception, err); - } catch (...) { - DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured"); - return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + LoggerE("Exception occured while get property"); + return JSValueMakeUndefined(context); } - return JSValueMakeUndefined(context); + /* do not return undefined object to find method */ + return NULL; } diff --git a/src/Application/JSApplicationControl.cpp b/src/Application/JSApplicationControl.cpp index 803a5e4..16766d0 100644 --- a/src/Application/JSApplicationControl.cpp +++ b/src/Application/JSApplicationControl.cpp @@ -224,12 +224,11 @@ JSValueRef JSApplicationControl::getProperty(JSContextRef context, JSObjectRef o return converter->toJSValueRef(appsvc->getAppControlDataArray()); } - } catch (const BasePlatformException &err) { - return JSWebAPIErrorFactory::postException(context, exception, err); } catch (...) { - DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured"); - return JSWebAPIErrorFactory::postException(context, exception, err); + LoggerE("Exception occured while get property"); + return JSValueMakeUndefined(context); } + /* do not return undefined object to find method */ return NULL; } @@ -257,11 +256,8 @@ bool JSApplicationControl::setProperty(JSContextRef context, JSObjectRef object, return true; } - } catch (const BasePlatformException &err) { - return JSWebAPIErrorFactory::postException(context, exception, err); - } catch (...) { - DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured"); - return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + LoggerE("Exception occured while set property"); } return false; diff --git a/src/Application/JSApplicationControlData.cpp b/src/Application/JSApplicationControlData.cpp index a6eb790..186c288 100644 --- a/src/Application/JSApplicationControlData.cpp +++ b/src/Application/JSApplicationControlData.cpp @@ -170,38 +170,37 @@ JSValueRef JSApplicationControlData::getProperty(JSContextRef context, } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_VALUE)) { return converter.toJSValueRef(privateData->getValue()); } - } catch (const BasePlatformException &err) { - return JSWebAPIErrorFactory::postException(context, exception, err); - } catch (...) { - DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured"); - return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + LoggerE("Exception occured while get property"); + return JSValueMakeUndefined(context); } - return JSValueMakeUndefined(context); + /* do not return undefined object to find method */ + return NULL; } + bool JSApplicationControlData::setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception) { + const JSValueRef arguments[1] = {value}; + ArgumentValidator validator(context, 1, arguments); + try { - WrtDeviceApis::CommonsJavaScript::Converter converter(context); ApplicationControlDataPtr privateData = getPrivData(object); if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_KEY)) { - privateData->setKey(converter.toString(value)); + privateData->setKey(JSUtil::JSValueToString(context, value)); return true; } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_VALUE)) { - privateData->setValue(converter.toVectorOfStrings(value)); + privateData->setValue(JSUtil::JSArrayToStringVector(context, validator.toArrayObject(0))); return true; } } catch (const BasePlatformException &err) { - return JSWebAPIErrorFactory::postException(context, exception, err); - } catch (...) { - DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured"); - return JSWebAPIErrorFactory::postException(context, exception, err); + LoggerE("Setting property is failed." << err.getMessage()); } return false; diff --git a/src/Application/JSApplicationInformation.cpp b/src/Application/JSApplicationInformation.cpp index 97eb882..5031880 100644 --- a/src/Application/JSApplicationInformation.cpp +++ b/src/Application/JSApplicationInformation.cpp @@ -191,14 +191,13 @@ JSValueRef JSApplicationInformation::getProperty(JSContextRef context, return converter.toJSValueRefLong(privateData->getInstallSize()); } } - } catch (const BasePlatformException &err) { - return JSWebAPIErrorFactory::postException(context, exception, err); - } catch (...) { - DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured"); - return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + LoggerE("Exception occured while get property"); + return JSValueMakeUndefined(context); } - - return JSValueMakeUndefined(context); + + /* do not return undefined object to find method */ + return NULL; } } diff --git a/src/Application/JSApplicationMetaData.cpp b/src/Application/JSApplicationMetaData.cpp index 8abeb0e..67df56a 100644 --- a/src/Application/JSApplicationMetaData.cpp +++ b/src/Application/JSApplicationMetaData.cpp @@ -128,14 +128,13 @@ JSValueRef JSApplicationMetaData::getProperty(JSContextRef context, return converter.toJSValueRef(value); } - } catch (const BasePlatformException &err) { - return JSWebAPIErrorFactory::postException(context, exception, err); - } catch (...) { - DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured"); - return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + LoggerE("Exception occured while get property"); + return JSValueMakeUndefined(context); } - return JSValueMakeUndefined(context); + /* do not return undefined object to find method */ + return NULL; } } diff --git a/src/Application/JSRequestedApplicationControl.cpp b/src/Application/JSRequestedApplicationControl.cpp index f0bb9d2..54e0370 100644 --- a/src/Application/JSRequestedApplicationControl.cpp +++ b/src/Application/JSRequestedApplicationControl.cpp @@ -155,7 +155,8 @@ JSValueRef JSRequestedApplicationControl::getProperty(JSContextRef context, JSOb { JSRequestedApplicationControlPriv *priv = static_cast(JSObjectGetPrivate(object)); if (!priv) { - return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mistmatch error."); + LoggerE("Exception occured while get property"); + return JSValueMakeUndefined(context); } try { @@ -167,11 +168,9 @@ JSValueRef JSRequestedApplicationControl::getProperty(JSContextRef context, JSOb } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALLER_APP_ID)) { return converter->toJSValueRef(providerMgr->getCallerAppId()); } - } catch (const BasePlatformException &err) { - LoggerW("Getting property is failed. %s", err.getMessage().c_str()); } catch (...) { - DeviceAPI::Common::UnknownException err("Unknown Error in RequestedApplicationControl.replyFailure()."); - LoggerW("Getting property is failed. %s", err.getMessage().c_str()); + LoggerE("Exception occured while get property"); + return JSValueMakeUndefined(context); } /* do not return undefined object to find method */ diff --git a/src/Bluetooth/JSBluetoothAdapter.cpp b/src/Bluetooth/JSBluetoothAdapter.cpp index 91810e5..22badc7 100644 --- a/src/Bluetooth/JSBluetoothAdapter.cpp +++ b/src/Bluetooth/JSBluetoothAdapter.cpp @@ -203,7 +203,7 @@ JSValueRef JSBluetoothAdapter::setPowered(JSContextRef context, try { // Check whether BT is supported or not bool supported = false; - if(system_info_get_value_bool(SYSTEM_INFO_KEY_BLUETOOTH_SUPPORTED, &supported) != SYSTEM_INFO_ERROR_NONE) { + if(system_info_get_platform_bool("tizen.org/feature/network.bluetooth", &supported) != SYSTEM_INFO_ERROR_NONE) { LoggerW("Can't check BT is supported or not"); } diff --git a/src/Bluetooth/JSBluetoothHealthApplication.cpp b/src/Bluetooth/JSBluetoothHealthApplication.cpp index 580e34b..14ced18 100644 --- a/src/Bluetooth/JSBluetoothHealthApplication.cpp +++ b/src/Bluetooth/JSBluetoothHealthApplication.cpp @@ -130,7 +130,7 @@ JSValueRef JSBluetoothHealthApplication::getProperty(JSContextRef context, } else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) { LoggerD("get onconnect"); - return JSUtil::toJSValueRef(context, priv->mApp->getOnConnect()); + return priv->mApp->getOnConnect(context); } /* else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_IS_REGISTERED)) { diff --git a/src/Filesystem/FilesystemUtils.cpp b/src/Filesystem/FilesystemUtils.cpp index e351ff3..8ac3232 100644 --- a/src/Filesystem/FilesystemUtils.cpp +++ b/src/Filesystem/FilesystemUtils.cpp @@ -114,7 +114,7 @@ IPathPtr DLL_EXPORT fromVirtualPath(JSContextRef context, RootToPathMapIterator it = rootToPath.find(root); if (it == rootToPath.end()) { - //ThrowMsg(Commons::NotFoundException, "Location not found."); +// ThrowMsg(Commons::NotFoundException, "Location not found."); LoggerD("Allow non virtual root path " << arg); return IPath::create(arg); } @@ -155,6 +155,33 @@ std::string DLL_EXPORT toVirtualPath(JSContextRef context, const std::string& ar // ThrowMsg(Commons::ConversionException, "Path doesn't contain a valid location type."); } +bool DLL_EXPORT isVirtualPath(const std::string& path) { + std::string root; + std::string::size_type separatorPosition = path.find(IPath::getSeparator()); + + if (separatorPosition != std::string::npos) { + root = path.substr(0, separatorPosition); + } else { + root = path; + } + + int widgetId = WrtAccessSingleton::Instance().getWidgetId(); + WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId); + + RootToPathMap rootToPath = getRootToPathMap(); + rootToPath["wgt-package"] = widgetDB->getWidgetInstallationPath(); + rootToPath["wgt-private"] = widgetDB->getWidgetPersistentStoragePath(); + rootToPath["wgt-private-tmp"] = widgetDB->getWidgetTemporaryStoragePath(); + RootToPathMapIterator it = rootToPath.find(root); + + + if (it == rootToPath.end()) { + return false; + } + else { + return true; + } +} bool DLL_EXPORT isUriPath(const std::string& path) { const char* uriPrefix = "file://"; const char* stringFromPath = path.c_str(); diff --git a/src/Filesystem/FilesystemUtils.h b/src/Filesystem/FilesystemUtils.h index 1e50a16..4eb9e0d 100755 --- a/src/Filesystem/FilesystemUtils.h +++ b/src/Filesystem/FilesystemUtils.h @@ -33,6 +33,7 @@ std::string toVirtualPath(JSContextRef context, const std::string& arg); bool isPathValid(const std::string& path); bool isUriPath(const std::string& path); +bool isVirtualPath(const std::string& path); void toUTF8String(std::string fromEncoding, const char* from, const size_t fromLength, std::string &outputString); } diff --git a/src/Filesystem/JSFilesystemManager.cpp b/src/Filesystem/JSFilesystemManager.cpp index fbaa582..b6c0cef 100644 --- a/src/Filesystem/JSFilesystemManager.cpp +++ b/src/Filesystem/JSFilesystemManager.cpp @@ -538,9 +538,14 @@ JSValueRef JSFilesystemManager::resolve(JSContextRef context, cbm->setObject(thisObject); IPathPtr path; - std::string virtualPath; - path = Utils::fromVirtualPath(globalContext, converter->toString(reserveArguments[0])); - virtualPath = converter->toString(reserveArguments[0]); + std::string virtualPath = converter->toString(reserveArguments[0]); + + if (!Utils::isVirtualPath(virtualPath) && !Utils::isUriPath(virtualPath)) { + ThrowMsg(Commons::NotFoundException, "Location not found."); + } + + path = Utils::fromVirtualPath(globalContext, virtualPath); + LoggerD("virtualPath:[" << virtualPath << "]"); int permissions = PERM_READ | PERM_WRITE; std::string perm = "rw"; diff --git a/src/Filesystem/Manager.cpp b/src/Filesystem/Manager.cpp index 1518351..692efeb 100644 --- a/src/Filesystem/Manager.cpp +++ b/src/Filesystem/Manager.cpp @@ -53,7 +53,8 @@ namespace Filesystem { Manager::Locations Manager::m_locations; Manager::RootList Manager::m_rootlist; Manager::SubRootList Manager::m_subrootlist; -const std::size_t Manager::m_maxPathLength = 256; +const std::size_t Manager::m_maxPathLength = PATH_MAX; + NodeList Manager::m_openedNodes; std::vector Manager::m_watchers; diff --git a/src/Filesystem/Node.cpp b/src/Filesystem/Node.cpp index 30b07cd..08c602d 100644 --- a/src/Filesystem/Node.cpp +++ b/src/Filesystem/Node.cpp @@ -428,7 +428,17 @@ bool Node::exists(const IPathPtr& path) struct stat info; memset(&info, 0, sizeof(struct stat)); int status = lstat(path->getFullPath().c_str(), &info); - if ((status == 0) || ((status != 0) && (errno != ENOENT))) { + + if (status == 0) + { + return true; + } + else if (errno == ENAMETOOLONG) + { + ThrowMsg(Commons::PlatformException, "file name is too long"); + } + else if (errno != ENOENT) + { return true; } return false; diff --git a/src/Messaging/MailSync.cpp b/src/Messaging/MailSync.cpp index b716596..20236a4 100644 --- a/src/Messaging/MailSync.cpp +++ b/src/Messaging/MailSync.cpp @@ -604,11 +604,26 @@ void MailSync::OnEventReceived(const DBus::MessageEvent& event) EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver(); if (mail && requestReceiver) { EventMessagingServicePtr event = mail->getMessagingServiceEvent(); -// EventMessagingServicePtr event = messagingService->getEventFromHandle(handle); if ( status == NOTI_DOWNLOAD_BODY_FINISH ) { event->m_message->readAllData(); + + std::vector attachments = mail->getAttachments(); + std::vector inlineAttachments = mail->getInlineAttachments(); + + for (unsigned int idx = 0; idx < attachments.size() ; idx++ ) + { + LoggerD("set Attachment ID = " << attachments[idx]->getAttachmentID()); + attachments[idx]->setMessage(event->m_message); + + } + for (unsigned int idx = 0; idx < inlineAttachments.size() ; idx++ ) + { + LoggerD("set inline Attachment ID = " << inlineAttachments[idx]->getAttachmentID()); + inlineAttachments[idx]->setMessage(event->m_message); + } + requestReceiver->ManualAnswer(event); m_SyncRequests.erase( handle ); } diff --git a/src/TimeUtil/JSTimeDuration.cpp b/src/TimeUtil/JSTimeDuration.cpp index 26adf36..9e836eb 100755 --- a/src/TimeUtil/JSTimeDuration.cpp +++ b/src/TimeUtil/JSTimeDuration.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "JSTimeDuration.h" #include "TimeUtilConverter.h" @@ -289,7 +290,7 @@ JSValueRef JSTimeDuration::diffTimeDuration(JSContextRef context, JSObjectRef th } case DIFFERENCE: default: - return converter.makeDurationObject(diff); + return converter.makeDurationObject(converter.optimizedTimeDuration(diff)); } } diff --git a/src/TimeUtil/TimeUtilConverter.cpp b/src/TimeUtil/TimeUtilConverter.cpp index fb931ae..df96235 100755 --- a/src/TimeUtil/TimeUtilConverter.cpp +++ b/src/TimeUtil/TimeUtilConverter.cpp @@ -226,6 +226,43 @@ long long TimeUtilConverter::convertDurationLength(DurationProperties duration, return result; } +DurationProperties TimeUtilConverter::optimizedTimeDuration(DurationProperties origin) { + DurationProperties result; + result.unit = origin.unit; + result.length = origin.length; + + switch(origin.unit) { + case MSECS_UNIT: + if (result.length % 1000) + return result; + result.unit = SECONDS_UNIT; + result.length /= 1000; + // intentional fall-through + + case SECONDS_UNIT: + if (result.length % 60) + return result; + result.unit = MINUTES_UNIT; + result.length /= 60; + // intentional fall-through + + case MINUTES_UNIT: + if (result.length % 60) + return result; + result.unit = HOURS_UNIT; + result.length /= 60; + // intentional fall-through + + case HOURS_UNIT: + if (result.length % 24) + return result; + result.unit = DAYS_UNIT; + result.length /= 24; + // intentional fall-through + } + return result; +} + double TimeUtilConverter::getTimeInMilliseconds(JSValueRef arg) { if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) || !JSValueIsObjectOfClass(m_context, arg, JSTZDate::getClassRef())) { ThrowMsg(Commons::ConversionException, diff --git a/src/TimeUtil/TimeUtilConverter.h b/src/TimeUtil/TimeUtilConverter.h index 3962903..e29d558 100755 --- a/src/TimeUtil/TimeUtilConverter.h +++ b/src/TimeUtil/TimeUtilConverter.h @@ -63,7 +63,7 @@ class TimeUtilConverter : public Converter tm toTZDateTime(JSValueRef arg); tm toTZDateTime(JSObjectRef arg); JSObjectRef toJSValueRefTZDate(const double milliseconds, const std::string &timezone); - + DurationProperties optimizedTimeDuration(DurationProperties origin); }; } -- 2.7.4