Skip to content

Commit

Permalink
TIMOB-9390: Blackberry: implement rest of String.formatXXX methods (o…
Browse files Browse the repository at this point in the history
…ther than format)

Reviewers: HarutM, JP

ChangeLog:
- Added new TiDateObject class.
- Added new message constants.
- Implemented formatDate, formatTime, formatCurrency, formatDecimal methods

Test Cases:
- Using sample tibbtest project create var with the value of Date
- Use formatDate(date, [format]), formatTime(date, [format]) in app.js
- Make sure string correctly formatted.
- Try the same with previously changed language.
- Create var with number value.
- Use formatCurrency(number) in app.js
- Make sure string correctly formatted.
- Try the same with previously changed language.
- Create var with number value.
- Use formatDecimal(number, [locale], [pattern]) in app.js
- Make sure string correctly formatted.
- Try the same with previously changed language.

NOTE: locale & pattern not yet implemented for formatDecimal
  • Loading branch information
unknown authored and unknown committed Jun 11, 2012
1 parent 3c9409d commit 99130f8
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 8 deletions.
8 changes: 8 additions & 0 deletions blackberry/tibb/TiConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ enum N_TEXT_ALIGNMENT
TEXT_ALIGNMENT_RIGHT
};

// Locale format constants
enum N_DATE_FORMAT_TYPE
{
DATE_FORMAT_LONG,
DATE_FORMAT_MEDIUM,
DATE_FORMAT_SHORT
};

}

}
Expand Down
34 changes: 34 additions & 0 deletions blackberry/tibb/TiDateObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#include "TiDateObject.h"

TiDateObject::TiDateObject(NativeObjectFactory* objectFactory)
: TiObject("Date")
{
objectFactory_ = objectFactory;
}

TiDateObject::~TiDateObject()
{
}

void TiDateObject::addObjectToParent(TiObject* parent, NativeObjectFactory* objectFactory)
{
TiDateObject* obj = new TiDateObject(objectFactory);
parent->addMember(obj);
obj->release();
}

void TiDateObject::onCreateStaticMembers()
{
}

bool TiDateObject::canAddMembers() const
{
return false;
}
37 changes: 37 additions & 0 deletions blackberry/tibb/TiDateObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#ifndef TIDATEOBJECT_H_
#define TIDATEOBJECT_H_

#include "TiObject.h"

/*
* TiDateObject
*/
class TiDateObject : public TiObject
{
public:
static void addObjectToParent(TiObject* parent, NativeObjectFactory* objectFactory);

protected:
virtual ~TiDateObject();
virtual void onCreateStaticMembers();
virtual bool canAddMembers() const;

private:
explicit TiDateObject();
explicit TiDateObject(NativeObjectFactory* objectFactory);

/* Not copiable; Not assignable */
TiDateObject(const TiDateObject& date);
TiDateObject& operator=(const TiDateObject& date);

NativeObjectFactory* objectFactory_;
};

#endif /* TIDATEOBJECT_H_ */
1 change: 1 addition & 0 deletions blackberry/tibb/TiMessageStrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ TIMESSAGESTRINGS_CONST_DEF(char*, Expected_argument_of_type_double, "Expected ar
TIMESSAGESTRINGS_CONST_DEF(char*, Expected_argument_of_type_integer, "Expected argument of type integer");
TIMESSAGESTRINGS_CONST_DEF(char*, Expected_argument_of_type_object_or_external, "Expected argument of type object or external");
TIMESSAGESTRINGS_CONST_DEF(char*, Expected_argument_of_type_string, "Expected argument of type string");
TIMESSAGESTRINGS_CONST_DEF(char*, Expected_argument_of_type_date, "Expected argument of type date");
TIMESSAGESTRINGS_CONST_DEF(char*, Expected_argument_of_type_unsigned_integer, "Expected argument of type unsigned integer");
TIMESSAGESTRINGS_CONST_DEF(char*, INTERNAL__An_error_occurred_while_parsing_the_format_string, "INTERNAL: An error occurred while parsing the format string");

Expand Down
2 changes: 2 additions & 0 deletions blackberry/tibb/TiRootObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "TiRootObject.h"

#include "TiDateObject.h"
#include "TiGenericFunctionObject.h"
#include "TiJSONObject.h"
#include "TiMessageStrings.h"
Expand Down Expand Up @@ -37,6 +38,7 @@ void TiRootObject::onCreateStaticMembers()
addMember(ti);
addMember(ti, "Ti");

TiDateObject::addObjectToParent(this, objectFactory_);
TiStringObject::addObjectToParent(this, objectFactory_);
TiJSONObject::addObjectToParent(this, objectFactory_);
TiGenericFunctionObject::addGenericFunctionToParent(this, "L", this, _L); // TODO: use the same object as Ti.Locale.getString
Expand Down
148 changes: 140 additions & 8 deletions blackberry/tibb/TiStringObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include "TiGenericFunctionObject.h"
#include "TiMessageStrings.h"
#include <QDate>
#include <QDateTime>
#include <QLocale>
#include <QRegExp>
#include <QString>
#include <QTextStream>
Expand All @@ -20,6 +23,9 @@ static QString formatDouble(QString s, Local<Value> arg);
static QString formatString(QString s, Local<Value> arg);
static QString formatPointer(QString s, Local<Value> arg);

static const char* DATE_FORMAT_SHORT = "short";
static const char* DATE_FORMAT_MEDIUM = "medium";
static const char* DATE_FORMAT_LONG = "long";

TiStringObject::TiStringObject(NativeObjectFactory* objectFactory)
: TiObject("String")
Expand Down Expand Up @@ -139,26 +145,152 @@ Handle<Value> TiStringObject::_format(void* userContext, TiObject* caller, const

Handle<Value> TiStringObject::_formatCurrency(void* userContext, TiObject* caller, const Arguments& args)
{
// TODO: Implement
return Undefined();
if (args.Length() < 1 || (!args[0]->IsNumber() && !args[0]->IsNumberObject()))
{
ThrowException(String::New(Ti::Msg::Expected_argument_of_type_integer));
return Undefined();
}

Handle<Number> num = Handle<Number>::Cast(args[0]);
QString strRes = QLocale().toCurrencyString(num->Value());

Handle<String> result = String::New(strRes.toUtf8());
return (result);
}

Handle<Value> TiStringObject::_formatDate(void* userContext, TiObject* caller, const Arguments& args)
{
// TODO: Implement
return Undefined();
if (args.Length() < 1 || !args[0]->IsDate() || !args[0]->IsObject())
{
ThrowException(String::New(Ti::Msg::Expected_argument_of_type_date));
return Undefined();
}

Local<Value> value = args[0];
unsigned int year = 0, month = 0, day = 0;

Local<Object> obj = Object::Cast(*value);
// Get year property
Local<Value> getYear_prop = (Object::Cast(*value)->Get(String::New("getFullYear")));
if (getYear_prop->IsFunction())
{
Local<Function> getYear_func = Function::Cast(*getYear_prop);
Local<Value> yearValue = getYear_func->Call(obj, 0, NULL);
year = yearValue->NumberValue();
}
// Get month property
Local<Value> getMonth_prop = (Object::Cast(*value)->Get(String::New("getMonth")));
if (getMonth_prop->IsFunction())
{
Local<Function> getMonth_func = Function::Cast(*getMonth_prop);
Local<Value> monthValue = getMonth_func->Call(obj, 0, NULL);
month = monthValue->NumberValue();
}
// Get day property
Local<Value> getDay_prop = (Object::Cast(*value)->Get(String::New("getDate")));
if (getDay_prop->IsFunction())
{
Local<Function> getDay_func = Function::Cast(*getDay_prop);
Local<Value> dayValue = getDay_func->Call(obj, 0, NULL);
day = dayValue->NumberValue();
}

// Defaults to NarrowFormat
QLocale::FormatType fType = QLocale::NarrowFormat;

// Try to parse optional format argument
if (args.Length() == 2 && args[1]->IsString())
{
const String::Utf8Value utf8(args[1]);
QString strFormat = QString::fromUtf8(*utf8);
if (strFormat.compare(DATE_FORMAT_MEDIUM) == 0)
{
fType = QLocale::ShortFormat;
}
else if (strFormat.compare(DATE_FORMAT_LONG) == 0)
{
fType = QLocale::LongFormat;
}
else
{
fType = QLocale::NarrowFormat;
}
}

// Adding +1 to month, since it starting from 0
QDate date(year, month + 1, day);
QString strRes = QLocale().toString(date, fType);

Handle<String> result = String::New(strRes.toUtf8());
return (result);
}

Handle<Value> TiStringObject::_formatDecimal(void* userContext, TiObject* caller, const Arguments& args)
{
// TODO: Implement
return Undefined();
if (args.Length() < 1 || (!args[0]->IsNumber() && !args[0]->IsNumberObject()))
{
ThrowException(String::New(Ti::Msg::Expected_argument_of_type_integer));
return Undefined();
}

Handle<Number> num = Handle<Number>::Cast(args[0]);
QString strRes = QLocale().toString(num->Value());

// TODO: parse optional parameters: locale & pattern
// See: http://docs.appcelerator.com/titanium/2.0/index.html#!/api/Global.String

Handle<String> result = String::New(strRes.toUtf8());
return (result);
}

Handle<Value> TiStringObject::_formatTime(void* userContext, TiObject* caller, const Arguments& args)
{
// TODO: Implement
return Undefined();
if (args.Length() < 1 || !args[0]->IsDate() || !args[0]->IsObject())
{
ThrowException(String::New(Ti::Msg::Expected_argument_of_type_date));
return Undefined();
}

Local<Value> value = args[0];
uint64_t msecs = 0;

Local<Object> obj = Object::Cast(*value);
// Get time property
Local<Value> getTime_prop = (Object::Cast(*value)->Get(String::New("getTime")));
if (getTime_prop->IsFunction())
{
Local<Function> getTime_func = Function::Cast(*getTime_prop);
Local<Value> timeValue = getTime_func->Call(obj, 0, NULL);
msecs = timeValue->NumberValue();
}

// Defaults to NarrowFormat
QLocale::FormatType fType = QLocale::NarrowFormat;

// Try to parse optional format argument
if (args.Length() == 2 && args[1]->IsString())
{
const String::Utf8Value utf8(args[1]);
QString strFormat = QString::fromUtf8(*utf8);
if (strFormat.compare(DATE_FORMAT_MEDIUM) == 0)
{
fType = QLocale::ShortFormat;
}
else if (strFormat.compare(DATE_FORMAT_LONG) == 0)
{
fType = QLocale::LongFormat;
}
else
{
fType = QLocale::NarrowFormat;
}
}

QDateTime time = QDateTime::fromMSecsSinceEpoch(msecs);
QString strRes = QLocale().toString(time, fType);

Handle<String> result = String::New(strRes.toUtf8());
return (result);
}


Expand Down

0 comments on commit 99130f8

Please sign in to comment.