80
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: formatterqtnative
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#ifdef RD_JAVA_UI_QT
|
|
20 |
#include <qcoreapplication.h>
|
|
21 |
#include <qlibraryinfo.h>
|
|
22 |
#include <qlocale.h>
|
|
23 |
#include <qtranslator.h>
|
|
24 |
#ifdef __SYMBIAN32__
|
|
25 |
#include <hbparameterlengthlimiter.h>
|
|
26 |
#include <hbstringutil.h>
|
|
27 |
#else // __SYMBIAN32__
|
|
28 |
// If HbParameterLengthLimiter is not available, define it as empty macro.
|
|
29 |
#define HbParameterLengthLimiter(text) (text)
|
|
30 |
#endif // __SYMBIAN32__
|
|
31 |
#endif // RD_JAVA_UI_QT
|
|
32 |
|
|
33 |
#include "com_nokia_mj_impl_utils_FormatterQt.h"
|
|
34 |
#include "com_nokia_mj_impl_utils_ResourceLoader.h"
|
|
35 |
#include "com_nokia_mj_impl_utils_ResourceLoaderQt.h"
|
|
36 |
#include "javajniutils.h"
|
|
37 |
#include "logger.h"
|
|
38 |
|
|
39 |
#ifdef __SYMBIAN32__
|
|
40 |
#include "s60commonutils.h"
|
|
41 |
#else // __SYMBIAN32__
|
|
42 |
#define KErrNone (0)
|
|
43 |
#define KErrNotFound (-1)
|
|
44 |
#endif // __SYMBIAN32__
|
|
45 |
|
|
46 |
using namespace java::util;
|
|
47 |
|
|
48 |
JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_utils_ResourceLoader__1getLocaleIdQt
|
|
49 |
(JNIEnv *env, jclass)
|
|
50 |
{
|
|
51 |
#ifdef RD_JAVA_UI_QT
|
|
52 |
QString localeName = QLocale::system().name();
|
|
53 |
jstring loc = env->NewString(localeName.utf16(), localeName.size());
|
|
54 |
return loc;
|
|
55 |
#else // RD_JAVA_UI_QT
|
|
56 |
(void)env; // just to suppress a warning
|
|
57 |
return NULL;
|
|
58 |
#endif // RD_JAVA_UI_QT
|
|
59 |
}
|
|
60 |
|
|
61 |
JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_utils_ResourceLoaderQt__1createTranslator
|
|
62 |
(JNIEnv *aEnv, jclass, jstring aResourceName)
|
|
63 |
{
|
|
64 |
#ifdef RD_JAVA_UI_QT
|
|
65 |
QString resourceName = QString::fromStdWString(
|
|
66 |
JniUtils::jstringToWstring(aEnv, aResourceName)) +
|
|
67 |
"_" + QLocale::system().name();
|
|
68 |
|
|
69 |
if (qApp == 0)
|
|
70 |
{
|
|
71 |
ELOG1(EUtils,
|
|
72 |
"ResourceLoaderQt__1createTranslator: QApplication "
|
|
73 |
"does not exist. Loading failed for %S",
|
|
74 |
resourceName.toStdWString().c_str());
|
|
75 |
return KErrNotFound;
|
|
76 |
}
|
|
77 |
|
|
78 |
QTranslator* translator = new QTranslator();
|
|
79 |
bool translatorLoaded = translator->load(
|
|
80 |
resourceName, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
81 |
if (!translatorLoaded)
|
|
82 |
{
|
|
83 |
translatorLoaded = translator->load(
|
|
84 |
resourceName,
|
|
85 |
"C:" + QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
86 |
}
|
|
87 |
if (!translatorLoaded)
|
|
88 |
{
|
|
89 |
translatorLoaded = translator->load(
|
|
90 |
resourceName,
|
|
91 |
"Z:" + QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
92 |
}
|
|
93 |
if (translatorLoaded)
|
|
94 |
{
|
|
95 |
qApp->installTranslator(translator);
|
|
96 |
}
|
|
97 |
else
|
|
98 |
{
|
|
99 |
ILOG1(EUtils, "__1createTranslator: loading resource %S failed",
|
|
100 |
resourceName.toStdWString().c_str());
|
|
101 |
delete translator;
|
|
102 |
return KErrNotFound;
|
|
103 |
}
|
|
104 |
// Return handle to translator.
|
|
105 |
return reinterpret_cast<unsigned int>(translator)>>2;
|
|
106 |
#else // RD_JAVA_UI_QT
|
|
107 |
(void)aEnv; // just to suppress a warning
|
|
108 |
(void)aResourceName; // just to suppress a warning
|
|
109 |
return KErrNone;
|
|
110 |
#endif // RD_JAVA_UI_QT
|
|
111 |
}
|
|
112 |
|
|
113 |
JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_utils_ResourceLoaderQt__1deleteTranslator
|
|
114 |
(JNIEnv *, jclass, jint aHandle)
|
|
115 |
{
|
|
116 |
#ifdef RD_JAVA_UI_QT
|
|
117 |
QTranslator *pTranslator = reinterpret_cast<QTranslator*>(aHandle<<2);
|
|
118 |
QCoreApplication::removeTranslator(pTranslator);
|
|
119 |
delete pTranslator;
|
|
120 |
return KErrNone;
|
|
121 |
#else // RD_JAVA_UI_QT
|
|
122 |
(void)aHandle; // just to suppress a warning
|
|
123 |
return KErrNone;
|
|
124 |
#endif // RD_JAVA_UI_QT
|
|
125 |
}
|
|
126 |
|
|
127 |
JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_utils_ResourceLoaderQt__1translate
|
|
128 |
(JNIEnv *aEnv, jclass, jint aHandle, jstring aId, jint aN)
|
|
129 |
{
|
|
130 |
#ifdef RD_JAVA_UI_QT
|
|
131 |
(void)aHandle; // just to suppress a warning
|
|
132 |
QString id = QString::fromStdWString(JniUtils::jstringToWstring(aEnv, aId));
|
|
133 |
QString result = QString(qtTrId(id.toUtf8().constData(), aN));
|
|
134 |
return aEnv->NewString(result.utf16(), result.size());
|
|
135 |
#else // RD_JAVA_UI_QT
|
|
136 |
(void)aEnv; // just to suppress a warning
|
|
137 |
(void)aHandle; // just to suppress a warning
|
|
138 |
(void)aId; // just to suppress a warning
|
|
139 |
(void)aN; // just to suppress a warning
|
|
140 |
return aId;
|
|
141 |
#endif // RD_JAVA_UI_QT
|
|
142 |
}
|
|
143 |
|
|
144 |
#ifdef RD_JAVA_UI_QT
|
|
145 |
QString FormatTextArg(JNIEnv *aEnv, QString aText, jobjectArray aArgs, int aI)
|
|
146 |
{
|
|
147 |
// Check the type of aArgs[aI] and call correct aText.arg() method.
|
|
148 |
jobject obj = aEnv->GetObjectArrayElement(aArgs, aI);
|
|
149 |
jclass clazz = aEnv->FindClass("java/lang/String");
|
|
150 |
if (aEnv->IsInstanceOf(obj, clazz))
|
|
151 |
{
|
|
152 |
return HbParameterLengthLimiter(aText).arg(
|
|
153 |
QString::fromStdWString(
|
|
154 |
JniUtils::jstringToWstring(aEnv, (jstring)obj)));
|
|
155 |
}
|
|
156 |
clazz = aEnv->FindClass("java/lang/Integer");
|
|
157 |
if (aEnv->IsInstanceOf(obj, clazz))
|
|
158 |
{
|
|
159 |
jmethodID method = aEnv->GetMethodID(clazz, "intValue", "()I");
|
|
160 |
return HbParameterLengthLimiter(aText).arg(
|
|
161 |
aEnv->CallIntMethod(obj, method));
|
|
162 |
}
|
|
163 |
clazz = aEnv->FindClass("java/lang/Character");
|
|
164 |
if (aEnv->IsInstanceOf(obj, clazz))
|
|
165 |
{
|
|
166 |
jmethodID method = aEnv->GetMethodID(clazz, "charValue", "()C");
|
|
167 |
return HbParameterLengthLimiter(aText).arg(
|
|
168 |
(char)aEnv->CallCharMethod(obj, method));
|
|
169 |
}
|
|
170 |
clazz = aEnv->FindClass("java/util/Calendar");
|
|
171 |
if (aEnv->IsInstanceOf(obj, clazz))
|
|
172 |
{
|
|
173 |
jmethodID method = aEnv->GetMethodID(clazz, "get", "(I)I");
|
|
174 |
int year = aEnv->CallIntMethod(obj, method, 1); // Calendar.YEAR
|
|
175 |
int month = aEnv->CallIntMethod(obj, method, 2) + 1; // Calendar.MONTH
|
|
176 |
int day = aEnv->CallIntMethod(obj, method, 5); // Calendar.DAY_OF_MONTH
|
|
177 |
QDate date(year, month, day);
|
|
178 |
QLocale locale;
|
|
179 |
return HbParameterLengthLimiter(aText).arg(
|
|
180 |
locale.toString(date, QLocale::ShortFormat));
|
|
181 |
}
|
|
182 |
return aText;
|
|
183 |
}
|
|
184 |
#endif // RD_JAVA_UI_QT
|
|
185 |
|
|
186 |
JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_utils_FormatterQt__1formatParameters
|
|
187 |
(JNIEnv *aEnv, jclass, jstring aText, jobjectArray aArgs)
|
|
188 |
{
|
|
189 |
#ifdef RD_JAVA_UI_QT
|
|
190 |
std::wstring wsText = JniUtils::jstringToWstring(aEnv, aText);
|
|
191 |
QString text = QString::fromStdWString(wsText);
|
|
192 |
int argsLen = aEnv->GetArrayLength(aArgs);
|
|
193 |
for (int i = 0; i < argsLen; i++)
|
|
194 |
{
|
|
195 |
text = FormatTextArg(aEnv, text, aArgs, i);
|
|
196 |
}
|
|
197 |
return aEnv->NewString(text.utf16(), text.size());
|
|
198 |
#else // RD_JAVA_UI_QT
|
|
199 |
(void)aEnv; // just to suppress a warning
|
|
200 |
(void)aText; // just to suppress a warning
|
|
201 |
(void)aArgs; // just to suppress a warning
|
|
202 |
return aText;
|
|
203 |
#endif // RD_JAVA_UI_QT
|
|
204 |
}
|
|
205 |
|
|
206 |
JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_utils_FormatterQt__1formatDigits
|
|
207 |
(JNIEnv * aEnv, jclass, jstring aText)
|
|
208 |
{
|
|
209 |
jstring ret = aText;
|
|
210 |
#if defined(RD_JAVA_UI_QT) && defined(__SYMBIAN32__)
|
|
211 |
QString text = QString::fromStdWString(
|
|
212 |
JniUtils::jstringToWstring(aEnv, aText));
|
|
213 |
text = HbStringUtil::convertDigits(text);
|
|
214 |
ret = aEnv->NewString(text.utf16(), text.size());
|
|
215 |
#else // RD_JAVA_UI_QT && __SYMBIAN32__
|
|
216 |
(void)aEnv; // just to suppress a warning
|
|
217 |
#endif // RD_JAVA_UI_QT && __SYMBIAN32__
|
|
218 |
return ret;
|
|
219 |
}
|