Debian Codegraph @

src/imports/Lomiri/Contacts/ContactDetailPhoneNumberTypeModel.qml

raw
1 /*
2 * Copyright (C) 2012-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 QtContacts 5.0 as QtContacts
19
20 ListModel {
21 id: typeModel
22
23 signal loaded()
24
25 function getTypeIndex(detail) {
26 var contexts = detail.contexts
27 var subTypes = detail.subTypes
28
29 if (contexts.indexOf(QtContacts.ContactDetail.ContextHome) > -1) {
30 if (subTypes && subTypes.indexOf(QtContacts.PhoneNumber.Mobile) > -1) {
31 return 2
32 } else {
33 return 0
34 }
35 } else if (contexts.indexOf(QtContacts.ContactDetail.ContextWork) > -1) {
36 if (subTypes && subTypes.indexOf(QtContacts.PhoneNumber.Mobile)> -1) {
37 return 3
38 } else {
39 return 1
40 }
41 } else if (contexts.indexOf(QtContacts.ContactDetail.ContextOther) > -1) {
42 return 4
43 } else {
44 // phone without context and voice type is other
45 if (subTypes && subTypes.indexOf(QtContacts.PhoneNumber.Voice) > -1) {
46 return 4
47 } else if (subTypes && subTypes.indexOf(QtContacts.PhoneNumber.Mobile) > -1) {
48 return 2
49 } else {
50 return 2 // Default value is "Mobile"
51 }
52 }
53 }
54
55 function compareList(listA, listB) {
56 if (!listA && !listB) {
57 return true
58 }
59
60 if (!listA || !listB) {
61 return false
62 }
63
64 if (listA.length !== listB.length) {
65 return false
66 }
67 for(var i=0; i < listA.length; i++) {
68 if (listA[i] !== listB[i]) {
69 return false
70 }
71 }
72 return true
73 }
74
75 function updateDetail(detail, index) {
76 var modelData = get(index)
77 if (!modelData) {
78 return false
79 }
80
81 var newSubTypes = []
82 var newContext = []
83
84 if (modelData.context !== -1) {
85 newContext.push(modelData.context)
86 }
87 newSubTypes.push(modelData.subType)
88
89 var changed = false
90 if (!compareList(newContext, detail.contexts)) {
91 detail.contexts = newContext
92 changed = true
93 }
94
95 if (!compareList(newSubTypes, detail.subTypes)) {
96 detail.subTypes = newSubTypes
97 changed = true
98 }
99 return changed
100 }
101
102 Component.onCompleted: {
103 append({"value": "Home",
104 // TRANSLATORS: This refers to home landline phone label
105 "label": i18n.dtr("lomiri-addressbook-app", "Home"), "icon": "",
106 "context": QtContacts.ContactDetail.ContextHome, "subType": QtContacts.PhoneNumber.Voice })
107 append({"value": "Work",
108 // TRANSLATORS: This refers to landline work phone label
109 "label": i18n.dtr("lomiri-addressbook-app", "Work"), "icon": "",
110 "context": QtContacts.ContactDetail.ContextWork, "subType": QtContacts.PhoneNumber.Voice })
111 append({"value": "Mobile",
112 // TRANSLATORS: This refers to mobile/cellphone phone label
113 "label": i18n.dtr("lomiri-addressbook-app", "Mobile"), "icon": "",
114 "context": -1, "subType": QtContacts.PhoneNumber.Mobile })
115 append({"value": "Mobile-Work",
116 // TRANSLATORS: This refers to mobile/cellphone work phone label
117 "label": i18n.dtr("lomiri-addressbook-app", "Work Mobile"), "icon": "",
118 "context": QtContacts.ContactDetail.ContextWork, "subType": QtContacts.PhoneNumber.Mobile })
119 append({"value": "Other",
120 // TRANSLATORS: This refers to any other phone label
121 "label": i18n.dtr("lomiri-addressbook-app", "Other"), "icon": "",
122 "context": QtContacts.ContactDetail.ContextOther, "subType": QtContacts.PhoneNumber.Voice })
123 loaded()
124 }
125 }