Debian Codegraph @

src/imports/Lomiri/AddressBook/ContactView/ContactDetailNoteView.qml

raw
1 /*
2 * Copyright (C) 2022 UBports Foundation.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.3
19 import QtContacts 5.0 as QtContacts
20 import Lomiri.Telephony.PhoneNumber 0.1
21
22 import Lomiri.AddressBook.Base 0.1
23
24 ContactDetailBase {
25 id: root
26
27 property string note : detail ? detail.note : ""
28
29 detailType: QtContacts.ContactDetail.Note
30
31 visible: implicitHeight > 0
32 implicitHeight:(note.length > 0) ? contents.implicitHeight : 0
33
34 function escapeHTML(text) {
35 return text.replace(/[\t\n&><"' ]/g, function(m) {
36 switch (m) {
37 case '&':
38 return '&amp;';
39 case '<':
40 return '&lt;';
41 case '>':
42 return '&gt;';
43 case ' ':
44 return '&nbsp;';
45 case '"':
46 return '&quot;';
47 case '\n':
48 return '<br>';
49 case '\t':
50 return '&nbsp;&nbsp;&nbsp;&nbsp;';
51 default:
52 return '&#039;';
53 }
54 });
55 }
56
57 function getCountryCode() {
58 var localeName = Qt.locale().name
59 return localeName.substr(localeName.length - 2, 2)
60 }
61
62 function parseText(text){
63 if (!text) return ""
64
65 const linkRegex = /([\w+]+\:\/\/)?([\w\d-]+\.)*[\w-]+[\.\:]\w+([\/\?\=\&\&amp;\#\.]?[\w-]+)*\/?/gm
66 const region = getCountryCode()
67
68 let lines = []
69 text.split(/\n/).forEach( (line) => {
70 if (line.length > 0) {
71
72 line = escapeHTML(line)
73
74 // links
75 line = line.replace(linkRegex, function(url) {
76 var hyperlink = url;
77 if (hyperlink.startsWith('www')) {
78 hyperlink = 'https://' + hyperlink;
79 }
80
81 return '<a href="' + hyperlink + '">' + url + '</a>';
82 })
83
84 // phone numbers
85 PhoneUtils.matchInText(line, region).forEach((phoneNumber) => {
86 line = line.replace(phoneNumber, '<a href="tel:///' + phoneNumber + '">' + phoneNumber + '</a>');
87 });
88 }
89
90 lines.push(line)
91 })
92
93 return lines.join('<br>')
94 }
95
96 Column {
97 id: contents
98 spacing: units.gu(1)
99 width: parent.width
100
101 ContactDetailTitle {
102 title: i18n.dtr("lomiri-addressbook-app", "Note")
103 }
104
105 Rectangle {
106
107 anchors {
108 left: parent.left
109 right: parent.right
110 leftMargin: units.gu(2)
111 rightMargin: units.gu(2)
112 }
113 height: childrenRect.height
114 color: theme.palette.normal.background
115
116 Text {
117 id: label
118 anchors.left: parent.left
119 height: implicitHeight
120 font.weight: Font.Light
121 font.family: "Ubuntu"
122 text: root.parseText(root.note)
123 color: theme.palette.normal.baseText
124 // It needs to be Text.StyledText to use linkColor: https://api-docs.ubports.com/sdk/apps/qml/QtQuick/Text.html#sdk-qtquick-text-linkcolor
125 textFormat: Text.StyledText
126 wrapMode: Text.Wrap
127 onLinkActivated: Qt.openUrlExternally(link)
128 }
129 }
130 }
131 }