Merge lp:~renatofilho/telephony-service/phonenumber-format into lp:telephony-service

Proposed by Renato Araujo Oliveira Filho
Status: Merged
Approved by: Gustavo Pichorim Boiko
Approved revision: 850
Merged at revision: 850
Proposed branch: lp:~renatofilho/telephony-service/phonenumber-format
Merge into: lp:telephony-service
Diff against target: 267 lines (+198/-9)
6 files modified
Ubuntu/Telephony/PhoneNumber/CMakeLists.txt (+2/-0)
Ubuntu/Telephony/PhoneNumber/phonenumber.cpp (+10/-0)
Ubuntu/Telephony/PhoneNumber/phoneutils.cpp (+84/-0)
Ubuntu/Telephony/PhoneNumber/phoneutils.h (+45/-0)
Ubuntu/Telephony/tests/CMakeLists.txt (+13/-9)
Ubuntu/Telephony/tests/tst_PhoneNumberPhoneUtils.qml (+44/-0)
To merge this branch: bzr merge lp:~renatofilho/telephony-service/phonenumber-format
Reviewer Review Type Date Requested Status
Gustavo Pichorim Boiko (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+226541@code.launchpad.net

Commit message

Implemented PhoneNumber.format.

PhoneNumber.format can be used to format any string into a phone number formatted value.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
849. By Renato Araujo Oliveira Filho

Added missing license header.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
850. By Renato Araujo Oliveira Filho

[PhoneNumber] Does not try format phone numbers starting with "*" or "#"

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Renato Araujo Oliveira Filho (renatofilho) wrote :

Are there any related MPs required for this MP to build/function as expected? NO

Is your branch in sync with latest trunk? YES

Did you perform an exploratory manual test run of your code change and any related functionality on device or emulator? YES

Did you successfully run all tests found in your component's Test Plan on device or emulator? YES

If you changed the UI, was the change specified/approved by design? NO UI CHANGE

If you changed the packaging (debian), did you add a core-dev as a reviewer to this MP? NO PACKAGE CHANGE

Revision history for this message
Gustavo Pichorim Boiko (boiko) wrote :

Did you perform an exploratory manual test run of the code change and any related functionality on device or emulator?
Yes

Did CI run pass? If not, please explain why.
Yes

Have you checked that submitter has accurately filled out the submitter checklist and has taken no shortcut?
Yes

Code looks good and works as expected!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Ubuntu/Telephony/PhoneNumber/CMakeLists.txt'
2--- Ubuntu/Telephony/PhoneNumber/CMakeLists.txt 2014-07-04 17:16:59 +0000
3+++ Ubuntu/Telephony/PhoneNumber/CMakeLists.txt 2014-07-12 13:26:42 +0000
4@@ -1,6 +1,8 @@
5 set(PHONENUMBER_SRCS
6 phonenumber.h
7 phonenumber.cpp
8+ phoneutils.h
9+ phoneutils.cpp
10 asyoutypeformatter.h
11 asyoutypeformatter.cpp
12 )
13
14=== modified file 'Ubuntu/Telephony/PhoneNumber/phonenumber.cpp'
15--- Ubuntu/Telephony/PhoneNumber/phonenumber.cpp 2014-04-23 20:55:13 +0000
16+++ Ubuntu/Telephony/PhoneNumber/phonenumber.cpp 2014-07-12 13:26:42 +0000
17@@ -22,10 +22,19 @@
18
19 #include "phonenumber.h"
20 #include "asyoutypeformatter.h"
21+#include "phoneutils.h"
22
23 #include <QQmlEngine>
24 #include <qqml.h>
25
26+
27+static QObject *phoneUtilsProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
28+{
29+ Q_UNUSED(engine)
30+ Q_UNUSED(scriptEngine)
31+ return new PhoneUtils;
32+}
33+
34 void PhoneNumber::initializeEngine(QQmlEngine *engine, const char *uri)
35 {
36 Q_UNUSED(engine);
37@@ -36,4 +45,5 @@
38 {
39 // @uri Telephony.PhoneNumber
40 qmlRegisterType<AsYouTypeFormatter>(uri, 0, 1, "AsYouTypeFormatter");
41+ qmlRegisterSingletonType<PhoneUtils>(uri, 0, 1, "PhoneUtils", phoneUtilsProvider);
42 }
43
44=== added file 'Ubuntu/Telephony/PhoneNumber/phoneutils.cpp'
45--- Ubuntu/Telephony/PhoneNumber/phoneutils.cpp 1970-01-01 00:00:00 +0000
46+++ Ubuntu/Telephony/PhoneNumber/phoneutils.cpp 2014-07-12 13:26:42 +0000
47@@ -0,0 +1,84 @@
48+/*
49+ * Copyright (C) 2014 Canonical, Ltd.
50+ *
51+ * Authors:
52+ * Renato Araujo Oliveira Filho <renato.filho@canonical.com>
53+ *
54+ * This file is part of telephony-service.
55+ *
56+ * telephony-service is free software; you can redistribute it and/or modify
57+ * it under the terms of the GNU General Public License as published by
58+ * the Free Software Foundation; version 3.
59+ *
60+ * telephony-service is distributed in the hope that it will be useful,
61+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
62+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63+ * GNU General Public License for more details.
64+ *
65+ * You should have received a copy of the GNU General Public License
66+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
67+ */
68+
69+#include "phoneutils.h"
70+
71+#include <QtCore/QDebug>
72+#include <phonenumbers/phonenumberutil.h>
73+
74+
75+PhoneUtils::PhoneUtils(QObject *parent)
76+ : QObject(parent)
77+{
78+}
79+
80+PhoneUtils::~PhoneUtils()
81+{
82+}
83+
84+QString PhoneUtils::format(const QString &phoneNumber, const QString &defaultRegion, PhoneUtils::PhoneNumberFormat format)
85+{
86+ std::string formattedNumber;
87+ i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat pNFormat;
88+ if (format == PhoneUtils::Auto) {
89+ // skip if it is a special number or a command
90+ if (phoneNumber.startsWith("#") || phoneNumber.startsWith("*")) {
91+ return phoneNumber;
92+ } else if (phoneNumber.startsWith("+")) {
93+ pNFormat = i18n::phonenumbers::PhoneNumberUtil::INTERNATIONAL;
94+ } else {
95+ pNFormat = i18n::phonenumbers::PhoneNumberUtil::NATIONAL;
96+ }
97+ } else {
98+ pNFormat = static_cast<i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat>(format);
99+ }
100+
101+
102+ i18n::phonenumbers::PhoneNumberUtil *phonenumberUtil = i18n::phonenumbers::PhoneNumberUtil::GetInstance();
103+
104+ i18n::phonenumbers::PhoneNumber number;
105+ i18n::phonenumbers::PhoneNumberUtil::ErrorType error;
106+ error = phonenumberUtil->Parse(phoneNumber.toStdString(), defaultRegion.toStdString(), &number);
107+
108+ switch(error) {
109+ case i18n::phonenumbers::PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR:
110+ qWarning() << "Invalid coutry code for:" << phoneNumber;
111+ return "";
112+ case i18n::phonenumbers::PhoneNumberUtil::NOT_A_NUMBER:
113+ qWarning() << "The phone number is not a valid number:" << phoneNumber;
114+ return "";
115+ case i18n::phonenumbers::PhoneNumberUtil::TOO_SHORT_AFTER_IDD:
116+ case i18n::phonenumbers::PhoneNumberUtil::TOO_SHORT_NSN:
117+ case i18n::phonenumbers::PhoneNumberUtil::TOO_LONG_NSN:
118+ qWarning() << "Invalid phone number" << phoneNumber;
119+ return "";
120+ default:
121+ break;
122+ }
123+
124+
125+ phonenumberUtil->Format(number,
126+ pNFormat,
127+ &formattedNumber);
128+ return QString::fromStdString(formattedNumber);
129+}
130+
131+
132
133=== added file 'Ubuntu/Telephony/PhoneNumber/phoneutils.h'
134--- Ubuntu/Telephony/PhoneNumber/phoneutils.h 1970-01-01 00:00:00 +0000
135+++ Ubuntu/Telephony/PhoneNumber/phoneutils.h 2014-07-12 13:26:42 +0000
136@@ -0,0 +1,45 @@
137+/*
138+ * Copyright (C) 2014 Canonical, Ltd.
139+ *
140+ * Authors:
141+ * Renato Araujo Oliveira Filho <renato.filho@canonical.com>
142+ *
143+ * This file is part of telephony-service.
144+ *
145+ * telephony-service is free software; you can redistribute it and/or modify
146+ * it under the terms of the GNU General Public License as published by
147+ * the Free Software Foundation; version 3.
148+ *
149+ * telephony-service is distributed in the hope that it will be useful,
150+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
151+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
152+ * GNU General Public License for more details.
153+ *
154+ * You should have received a copy of the GNU General Public License
155+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
156+ */
157+
158+#ifndef TELEPHONY_PHONEUTILS_H
159+#define TELEPHONY_PHONEUTILS_H
160+
161+#include <QtCore/QObject>
162+
163+class PhoneUtils : public QObject
164+{
165+ Q_OBJECT
166+public:
167+ enum PhoneNumberFormat {
168+ E164 = 0,
169+ International,
170+ National,
171+ RFC3966,
172+ Auto
173+ };
174+
175+ PhoneUtils(QObject *parent = 0);
176+ ~PhoneUtils();
177+
178+ Q_INVOKABLE QString format(const QString &phoneNumber, const QString &defaultRegion, PhoneNumberFormat format = Auto);
179+};
180+
181+#endif
182
183=== modified file 'Ubuntu/Telephony/tests/CMakeLists.txt'
184--- Ubuntu/Telephony/tests/CMakeLists.txt 2014-07-04 17:16:59 +0000
185+++ Ubuntu/Telephony/tests/CMakeLists.txt 2014-07-12 13:26:42 +0000
186@@ -14,19 +14,23 @@
187 endforeach(test)
188 endmacro(generate_tests)
189
190+macro(DECLARE_QML_TEST TST_NAME TST_QML_FILE)
191+ add_test(NAME ${TST_NAME}
192+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
193+ COMMAND xvfb-run -a -s "-screen 0 1024x768x24" qmltestrunner -import ${CMAKE_BINARY_DIR} -input ${CMAKE_CURRENT_SOURCE_DIR}/${TST_QML_FILE}
194+ )
195+endmacro()
196+
197
198 generate_tests(
199 ContactWatcherTest
200 )
201
202+declare_qml_test("context_properties" tst_contextProperties.qml)
203+declare_qml_test("phonenumber_field" tst_PhoneNumberField.qml)
204+declare_qml_test("phonenumber_input" tst_PhoneNumberInput.qml)
205+declare_qml_test("phonenumber_utils" tst_PhoneNumberPhoneUtils.qml)
206+
207+# make the files visible on qtcreator
208 file(GLOB QML_TESTS *.qml *.js)
209-
210-# make the files visible on qtcreator
211 add_custom_target(telephonyservice_QMLTESTS ALL SOURCES ${QML_TESTS})
212-
213-# FIXME: those tests are disabled because qmltestrunner is crashing on armhf.
214-# They should be re-enabled once the problem is found and fixed.
215-add_test(NAME qmltestrunner
216- WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
217- COMMAND xvfb-run -a -s "-screen 0 1024x768x24" qmltestrunner -import ${CMAKE_BINARY_DIR} -input ${CMAKE_CURRENT_SOURCE_DIR}
218-)
219
220=== added file 'Ubuntu/Telephony/tests/tst_PhoneNumberPhoneUtils.qml'
221--- Ubuntu/Telephony/tests/tst_PhoneNumberPhoneUtils.qml 1970-01-01 00:00:00 +0000
222+++ Ubuntu/Telephony/tests/tst_PhoneNumberPhoneUtils.qml 2014-07-12 13:26:42 +0000
223@@ -0,0 +1,44 @@
224+/*
225+ * Copyright (C) 2014 Canonical, Ltd.
226+ *
227+ * This file is part of telephony-service.
228+ *
229+ * telephony-service is free software; you can redistribute it and/or modify
230+ * it under the terms of the GNU General Public License as published by
231+ * the Free Software Foundation; version 3.
232+ *
233+ * telephony-service is distributed in the hope that it will be useful,
234+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
235+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
236+ * GNU General Public License for more details.
237+ *
238+ * You should have received a copy of the GNU General Public License
239+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
240+ */
241+
242+import QtQuick 2.0
243+import QtTest 1.0
244+import Ubuntu.Telephony.PhoneNumber 0.1 as PhoneNumber
245+
246+TestCase {
247+ id: phoneNumberPhoneUtils
248+ name: "phoneNumberPhoneUtils"
249+
250+ function test_formatPhone_data()
251+ {
252+ var data = [];
253+ data.push({input: "6681800", expectedOutput: "668-1800"}) // Local number
254+ data.push({input: "7327572923", expectedOutput: "(732) 757-2923"}) // Coutry number
255+ data.push({input: "+558187042155", expectedOutput: "+55 81 8704-2155"}) // International number
256+ data.push({input: "55555555555", expectedOutput: "55555555555"}) // Ivalid number
257+ data.push({input: "*144", expectedOutput: "*144"}) // Special number
258+ data.push({input: "#123#", expectedOutput: "#123#"}) // Operators command
259+ return data
260+ }
261+
262+ function test_formatPhone(data)
263+ {
264+ var formatted = PhoneNumber.PhoneUtils.format(data.input, "US")
265+ compare(formatted, data.expectedOutput)
266+ }
267+}

Subscribers

People subscribed via source and target branches