Unity 8
here-terms.qml
1 /*
2  * Copyright (C) 2014,2015 Canonical, Ltd.
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 AccountsService 0.1
19 import Qt.labs.folderlistmodel 2.1
20 import Ubuntu.Components 1.3
21 import Ubuntu.Web 0.2
22 import ".." as LocalComponents
23 
24 LocalComponents.Page {
25  objectName: "hereTermsPage"
26 
27  title: i18n.tr("Terms & Conditions")
28  customBack: true
29 
30  FolderListModel {
31  id: termsModel
32  folder: AccountsService.hereLicensePath
33  nameFilters: ["*.html"]
34  showDirs: false
35  showOnlyReadable: true
36  onCountChanged: loadFileContent()
37  }
38 
39  function makeFileName(lang, country) {
40  return lang + "_" + country + ".html"
41  }
42 
43  function defaultCountryForLanguage(lang) {
44  if (lang === "da") return "DK"
45  if (lang === "en") return "US"
46  if (lang === "ko") return "KR"
47  if (lang === "zh") return "CN"
48  return lang.toUpperCase()
49  }
50 
51  function determineFileName() {
52  var codes = i18n.language.split(".")[0].split("_")
53  var defaultCountry = defaultCountryForLanguage(codes[0])
54  if (codes.count === 1)
55  codes = [codes[0], defaultCountry]
56  var perfectMatch = makeFileName(codes[0], codes[1])
57  var nearMatch = makeFileName(codes[0], defaultCountry)
58  var nearMatchExists = false
59 
60  for (var i = 0; i < termsModel.count; i++) {
61  var fileName = termsModel.get(i, "fileName")
62  if (fileName == perfectMatch) {
63  return perfectMatch
64  } else if (fileName == nearMatch) {
65  nearMatchExists = true
66  }
67  }
68 
69  if (nearMatchExists) {
70  return nearMatch
71  } else {
72  return makeFileName("en", "US")
73  }
74  }
75 
76  function loadFileContent() {
77  var xhr = new XMLHttpRequest
78  xhr.open("GET", AccountsService.hereLicensePath + "/" + determineFileName())
79  xhr.onreadystatechange = function() {
80  if (xhr.readyState == XMLHttpRequest.DONE) {
81  termsLabel.text = xhr.responseText
82  }
83  }
84  xhr.send()
85  }
86 
87  onBackClicked: {
88  if (webview.visible) {
89  termsLabel.visible = true
90  } else {
91  pageStack.prev()
92  }
93  }
94 
95  Column {
96  id: column
97  anchors.fill: content
98 
99  Label {
100  id: termsLabel
101  objectName: "termsLabel"
102  anchors.left: parent.left
103  anchors.right: parent.right
104  wrapMode: Text.Wrap
105  linkColor: theme.palette.normal.foregroundText
106  onLinkActivated: {
107  webview.url = link
108  termsLabel.visible = false
109  }
110  }
111 
112  WebView {
113  id: webview
114  objectName: "webview"
115  anchors.left: parent.left
116  anchors.right: parent.right
117  height: parent.height
118  visible: !termsLabel.visible
119  }
120  }
121 }