Merge lp:~feng-kylin/unity8/OpenUrlInIndicator into lp:unity8

Proposed by handsome_feng
Status: Rejected
Rejected by: Albert Astals Cid
Proposed branch: lp:~feng-kylin/unity8/OpenUrlInIndicator
Merge into: lp:unity8
Diff against target: 274 lines (+244/-1)
2 files modified
qml/Panel/Indicators/MessageMenuItemFactory.qml (+33/-1)
qml/Panel/Indicators/ba-linkify.js (+211/-0)
To merge this branch: bzr merge lp:~feng-kylin/unity8/OpenUrlInIndicator
Reviewer Review Type Date Requested Status
Nick Dedekind (community) Needs Fixing
Albert Astals Cid (community) Needs Information
Renato Araujo Oliveira Filho Pending
Review via email: mp+258836@code.launchpad.net

Commit message

use the same code in messaging-app to implement the function that open url in text viewed in the message indicator.

Description of the change

Use the same code in messaging-app to implement the function that open url in text viewed in the message indicator.

To post a comment you must log in.
Revision history for this message
Albert Astals Cid (aacid) wrote :

If the messaging-app is already doing this shouldn't it just feed the "transformed" text to us instead of us transforming it again?

review: Needs Information
Revision history for this message
Nick Dedekind (nick-dedekind) wrote :

> If the messaging-app is already doing this shouldn't it just feed the
> "transformed" text to us instead of us transforming it again?

Yeah, no chance here.
Either we need to put this directly in telephony when we receive the message, or it needs to be a function in the Ubuntu.Telephony[.Message] qml plugin.

review: Needs Fixing
Revision history for this message
Albert Astals Cid (aacid) wrote :

Feng?

Revision history for this message
handsome_feng (feng-kylin) wrote :

> Feng?

uhs,So I need to move this code into lp:telephony-service?

Revision history for this message
Albert Astals Cid (aacid) wrote :

What Nick says is that whatever text we get in the UI should be correct already, if lp:telephony-service is the thing feeding those messages, yes, the code should be there so we get the text correctly marked up for display already.

Unmerged revisions

1773. By handsome_feng

use the same code in messaging-app to implement the function that open url in text viewed in the message indicator

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'qml/Panel/Indicators/MessageMenuItemFactory.qml'
2--- qml/Panel/Indicators/MessageMenuItemFactory.qml 2015-03-13 10:07:57 +0000
3+++ qml/Panel/Indicators/MessageMenuItemFactory.qml 2015-05-12 07:27:56 +0000
4@@ -23,6 +23,9 @@
5 import Ubuntu.Settings.Menus 0.1 as Menus
6 import QMenuModel 0.1 as QMenuModel
7 import Utils 0.1 as Utils
8+import Ubuntu.Telephony.PhoneNumber 0.1 as PhoneNumber
9+
10+import "ba-linkify.js" as BaLinkify
11
12 Loader {
13 id: messageFactoryItem
14@@ -92,6 +95,35 @@
15 return defaultValue;
16 }
17
18+ function getCountryCode() {
19+ var localeName = Qt.locale().name
20+ return localeName.substr(localeName.length - 2, 2)
21+ }
22+
23+ function formatTelSchemeWith(phoneNumber) {
24+ return '<a href="tel:///' + phoneNumber + '">' + phoneNumber + '</a>'
25+ }
26+
27+ function parseText(text) {
28+ // remove html tags
29+ text = text.replace(/</g,'&lt;').replace(/>/g,'<tt>&gt;</tt>');
30+ // replace line breaks
31+ text = text.replace(/(\n)+/g, '<br />');
32+ // check for links
33+ var htmlText = BaLinkify.linkify(text);
34+ if (htmlText !== text) {
35+ return htmlText
36+ }
37+
38+ // linkify phone numbers if no web links were found
39+ var phoneNumbers = PhoneNumber.PhoneUtils.matchInText(text, getCountryCode())
40+ for (var i = 0; i < phoneNumbers.length; ++i) {
41+ var currentNumber = phoneNumbers[i]
42+ text = text.replace(currentNumber, formatTelSchemeWith(currentNumber))
43+ }
44+ return text
45+ }
46+
47 Component {
48 id: simpleMessage
49
50@@ -144,7 +176,7 @@
51 // text
52 title: menuData && menuData.label || ""
53 time: timeFormatter.timeString
54- body: getExtendedProperty(extendedData, "xCanonicalText", "")
55+ body: parseText(getExtendedProperty(extendedData, "xCanonicalText", ""))
56 replyButtonText: getExtendedProperty(replyActionDescription, "label", "Send")
57 replyHintText: i18n.ctr("Label: Hint in message indicator line edit", "Reply")
58 // icons
59
60=== added file 'qml/Panel/Indicators/ba-linkify.js'
61--- qml/Panel/Indicators/ba-linkify.js 1970-01-01 00:00:00 +0000
62+++ qml/Panel/Indicators/ba-linkify.js 2015-05-12 07:27:56 +0000
63@@ -0,0 +1,211 @@
64+/*!
65+ * JavaScript Linkify - v0.3 - 6/27/2009
66+ * http://benalman.com/projects/javascript-linkify/
67+ *
68+ * Copyright (c) 2009 "Cowboy" Ben Alman
69+ * Dual licensed under the MIT and GPL licenses.
70+ * http://benalman.com/about/license/
71+ *
72+ * Some regexps adapted from http://userscripts.org/scripts/review/7122
73+ */
74+
75+// Script: JavaScript Linkify: Process links in text!
76+//
77+// *Version: 0.3, Last updated: 6/27/2009*
78+//
79+// Project Home - http://benalman.com/projects/javascript-linkify/
80+// GitHub - http://github.com/cowboy/javascript-linkify/
81+// Source - http://github.com/cowboy/javascript-linkify/raw/master/ba-linkify.js
82+// (Minified) - http://github.com/cowboy/javascript-linkify/raw/master/ba-linkify.min.js (2.8kb)
83+//
84+// About: License
85+//
86+// Copyright (c) 2009 "Cowboy" Ben Alman,
87+// Dual licensed under the MIT and GPL licenses.
88+// http://benalman.com/about/license/
89+//
90+// About: Examples
91+//
92+// This working example, complete with fully commented code, illustrates one way
93+// in which this code can be used.
94+//
95+// Linkify - http://benalman.com/code/projects/javascript-linkify/examples/linkify/
96+//
97+// About: Support and Testing
98+//
99+// Information about what browsers this code has been tested in.
100+//
101+// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.7, Safari 3-4, Chrome, Opera 9.6-10.
102+//
103+// About: Release History
104+//
105+// 0.3 - (6/27/2009) Initial release
106+
107+// Function: linkify
108+//
109+// Turn text into linkified html.
110+//
111+// Usage:
112+//
113+// > var html = linkify( text [, options ] );
114+//
115+// Arguments:
116+//
117+// text - (String) Non-HTML text containing links to be parsed.
118+// options - (Object) An optional object containing linkify parse options.
119+//
120+// Options:
121+//
122+// callback (Function) - If specified, this will be called once for each link-
123+// or non-link-chunk with two arguments, text and href. If the chunk is
124+// non-link, href will be omitted. If unspecified, the default linkification
125+// callback is used.
126+// punct_regexp (RegExp) - A RegExp that will be used to trim trailing
127+// punctuation from links, instead of the default. If set to null, trailing
128+// punctuation will not be trimmed.
129+//
130+// Returns:
131+//
132+// (String) An HTML string containing links.
133+
134+var SCHEME = "[a-z\\d.-]+://",
135+IPV4 = "(?:(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.){3}(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])",
136+HOSTNAME = "(?:(?:[^\\s!@#$%^&*()_=+[\\]{}\\\\|;:'\",.<>/?]+)\\.)+",
137+TLD = "(?:ac|ad|aero|ae|af|ag|ai|al|am|an|ao|aq|arpa|ar|asia|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|biz|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|cat|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|coop|com|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|info|int|in|io|iq|ir|is|it|je|jm|jobs|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mobi|mo|mp|mq|mr|ms|mt|museum|mu|mv|mw|mx|my|mz|name|na|nc|net|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pro|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|travel|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--80akhbyknj4f|xn--9t4b11yi5a|xn--deba0ad|xn--g6w251d|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--jxalpdlp|xn--kgbechtv|xn--zckzah|ye|yt|yu|za|zm|zw)",
138+HOST_OR_IP = "(?:" + HOSTNAME + TLD + "|" + IPV4 + ")",
139+PATH = "(?:[;/][^#?<>\\s]*)?",
140+QUERY_FRAG = "(?:\\?[^#<>\\s]*)?(?:#[^<>\\s]*)?",
141+URI1 = "\\b" + SCHEME + "[^<>\\s]+",
142+URI2 = "\\b" + HOST_OR_IP + PATH + QUERY_FRAG + "(?!\\w)",
143+
144+MAILTO = "mailto:",
145+EMAIL = "(?:" + MAILTO + ")?[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@" + HOST_OR_IP + QUERY_FRAG + "(?!\\w)",
146+
147+URI_RE = new RegExp( "(?:" + URI1 + "|" + URI2 + "|" + EMAIL + ")", "ig" ),
148+SCHEME_RE = new RegExp( "^" + SCHEME, "i" ),
149+
150+quotes = {
151+ "'": "`",
152+ '>': '<',
153+ ')': '(',
154+ ']': '[',
155+ '}': '{',
156+ '»': '«',
157+ '›': '‹'
158+},
159+
160+default_options = {
161+ callback: function( text, href ) {
162+ return href ? '<a href="' + href + '" title="' + href + '">' + text + '</a>' : text;
163+ },
164+ punct_regexp: /(?:[!?.,:;'"]|(?:&|&amp;)(?:lt|gt|quot|apos|raquo|laquo|rsaquo|lsaquo);)$/
165+};
166+
167+function linkify( txt, options ) {
168+ options = options || {};
169+
170+ // Temp variables.
171+ var arr,
172+ i,
173+ link,
174+ href,
175+
176+ // Output HTML.
177+ html = '',
178+
179+ // Store text / link parts, in order, for re-combination.
180+ parts = [],
181+
182+ // Used for keeping track of indices in the text.
183+ idx_prev,
184+ idx_last,
185+ idx,
186+ link_last,
187+
188+ // Used for trimming trailing punctuation and quotes from links.
189+ matches_begin,
190+ matches_end,
191+ quote_begin,
192+ quote_end;
193+
194+ // Initialize options.
195+ for ( i in default_options ) {
196+ if ( options[ i ] === undefined ) {
197+ options[ i ] = default_options[ i ];
198+ }
199+ }
200+
201+ // Find links.
202+ while ( arr = URI_RE.exec( txt ) ) {
203+
204+ link = arr[0];
205+ idx_last = URI_RE.lastIndex;
206+ idx = idx_last - link.length;
207+
208+ // Not a link if preceded by certain characters.
209+ if ( /[\/:]/.test( txt.charAt( idx - 1 ) ) ) {
210+ continue;
211+ }
212+
213+ // Trim trailing punctuation.
214+ do {
215+ // If no changes are made, we don't want to loop forever!
216+ link_last = link;
217+
218+ quote_end = link.substr( -1 )
219+ quote_begin = quotes[ quote_end ];
220+
221+ // Ending quote character?
222+ if ( quote_begin ) {
223+ matches_begin = link.match( new RegExp( '\\' + quote_begin + '(?!$)', 'g' ) );
224+ matches_end = link.match( new RegExp( '\\' + quote_end, 'g' ) );
225+
226+ // If quotes are unbalanced, remove trailing quote character.
227+ if ( ( matches_begin ? matches_begin.length : 0 ) < ( matches_end ? matches_end.length : 0 ) ) {
228+ link = link.substr( 0, link.length - 1 );
229+ idx_last--;
230+ }
231+ }
232+
233+ // Ending non-quote punctuation character?
234+ if ( options.punct_regexp ) {
235+ link = link.replace( options.punct_regexp, function(a){
236+ idx_last -= a.length;
237+ return '';
238+ });
239+ }
240+ } while ( link.length && link !== link_last );
241+
242+ href = link;
243+
244+ // Add appropriate protocol to naked links.
245+ if ( !SCHEME_RE.test( href ) ) {
246+ href = ( href.indexOf( '@' ) !== -1 ? ( !href.indexOf( MAILTO ) ? '' : MAILTO )
247+ : !href.indexOf( 'irc.' ) ? 'irc://'
248+ : !href.indexOf( 'ftp.' ) ? 'ftp://'
249+ : 'http://' )
250+ + href;
251+ }
252+
253+ // Push preceding non-link text onto the array.
254+ if ( idx_prev != idx ) {
255+ parts.push([ txt.slice( idx_prev, idx ) ]);
256+ idx_prev = idx_last;
257+ }
258+
259+ // Push massaged link onto the array
260+ parts.push([ link, href ]);
261+ };
262+
263+ // Push remaining non-link text onto the array.
264+ parts.push([ txt.substr( idx_prev ) ]);
265+
266+ // Process the array items.
267+ for ( i = 0; i < parts.length; i++ ) {
268+ html += options.callback.apply( this, parts[i] );
269+ }
270+
271+ // In case of catastrophic failure, return the original text;
272+ return html || txt;
273+};
274+

Subscribers

People subscribed via source and target branches