| 1 |
/* |
| 2 |
* Copyright (C) 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 |
| 19 |
|
| 20 |
import Lomiri.Components 1.3 |
| 21 |
import Lomiri.Components.ListItems 1.3 |
| 22 |
|
| 23 |
import Lomiri.Contacts 0.1 |
| 24 |
import Lomiri.AddressBook.Base 0.1 |
| 25 |
|
| 26 |
Empty { |
| 27 |
id: root |
| 28 |
|
| 29 |
signal changed() |
| 30 |
|
| 31 |
function update() |
| 32 |
{ |
| 33 |
sourceModel.update() |
| 34 |
} |
| 35 |
|
| 36 |
function save() |
| 37 |
{ |
| 38 |
var changed = false |
| 39 |
var selectedSource = getSelectedSource() |
| 40 |
if (!selectedSource) { |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
var details = selectedSource.details(ContactDetail.ExtendedDetail) |
| 45 |
for(var d in details) { |
| 46 |
if (details[d].name === "IS-PRIMARY") { |
| 47 |
if (!details[d].data) { |
| 48 |
details[d].data = true |
| 49 |
changed = true |
| 50 |
break |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
if (changed) { |
| 55 |
sourceModel.saveContact(selectedSource) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
function getSelectedSource() { |
| 60 |
if (sources.model.count <= 0) |
| 61 |
return -1 |
| 62 |
|
| 63 |
return sources.model.get(sources.selectedIndex).source |
| 64 |
} |
| 65 |
|
| 66 |
height: sources.currentlyExpanded ? |
| 67 |
sources.containerHeight + units.gu(6) + label.height : |
| 68 |
sources.itemHeight + units.gu(6) + label.height |
| 69 |
|
| 70 |
ContactModel { |
| 71 |
id: sourceModel |
| 72 |
|
| 73 |
manager: ContactManager.manager() |
| 74 |
filter: DetailFilter { |
| 75 |
detail: ContactDetail.Type |
| 76 |
field: Type.TypeField |
| 77 |
value: Type.Group |
| 78 |
matchFlags: DetailFilter.MatchExactly |
| 79 |
} |
| 80 |
autoUpdate: false |
| 81 |
onContactsChanged: { |
| 82 |
if (contacts.length > 0) { |
| 83 |
writableSources.reload() |
| 84 |
root.changed() |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
ListModel { |
| 90 |
id: writableSources |
| 91 |
|
| 92 |
function getSourceMetaData(contact) { |
| 93 |
var metaData = {'read-only' : false, |
| 94 |
'account-provider': '', |
| 95 |
'account-id': 0, |
| 96 |
'is-primary': false} |
| 97 |
|
| 98 |
var details = contact.details(ContactDetail.ExtendedDetail) |
| 99 |
for(var d in details) { |
| 100 |
if (details[d].name === "READ-ONLY") { |
| 101 |
metaData['read-only'] = details[d].data |
| 102 |
} else if (details[d].name === "PROVIDER") { |
| 103 |
metaData['account-provider'] = details[d].data |
| 104 |
} else if (details[d].name === "APPLICATION-ID") { |
| 105 |
metaData['account-id'] = details[d].data |
| 106 |
} else if (details[d].name === "IS-PRIMARY") { |
| 107 |
metaData['is-primary'] = details[d].data |
| 108 |
} |
| 109 |
} |
| 110 |
return metaData |
| 111 |
} |
| 112 |
|
| 113 |
function reload() { |
| 114 |
sources._notify = false |
| 115 |
|
| 116 |
clear() |
| 117 |
// filter out read-only sources |
| 118 |
var contacts = sourceModel.contacts |
| 119 |
if (contacts.length === 0) { |
| 120 |
return |
| 121 |
} |
| 122 |
|
| 123 |
var data = [] |
| 124 |
for(var i in contacts) { |
| 125 |
var sourceMetaData = getSourceMetaData(contacts[i]) |
| 126 |
if (!sourceMetaData['readOnly']) { |
| 127 |
data.push({'sourceId': contacts[i].guid.guid, |
| 128 |
'sourceName': contacts[i].displayLabel.label, |
| 129 |
'accountId': sourceMetaData['account-id'], |
| 130 |
'accountProvider': sourceMetaData['account-provider'], |
| 131 |
'readOnly': sourceMetaData['read-only'], |
| 132 |
'isPrimary': sourceMetaData['is-primary'], |
| 133 |
'source': contacts[i] |
| 134 |
}) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
data.sort(function(a, b) { |
| 139 |
var valA = a.accountId |
| 140 |
var valB = b.accountId |
| 141 |
if (a.accountId == b.accountId) { |
| 142 |
valA = a.sourceName |
| 143 |
valB = b.sourceName |
| 144 |
} |
| 145 |
|
| 146 |
if (valA == valB) { |
| 147 |
return 0 |
| 148 |
} else if (valA < valB) { |
| 149 |
return -1 |
| 150 |
} else { |
| 151 |
return 1 |
| 152 |
} |
| 153 |
}) |
| 154 |
|
| 155 |
sources.selectedIndex = 0 |
| 156 |
var primaryIndex = 0 |
| 157 |
for (var i in data) { |
| 158 |
if (data[i].isPrimary) { |
| 159 |
primaryIndex = i |
| 160 |
} |
| 161 |
append(data[i]) |
| 162 |
} |
| 163 |
|
| 164 |
// select primary account |
| 165 |
sources.selectedIndex = primaryIndex |
| 166 |
sources._notify = true |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
Label { |
| 171 |
id: label |
| 172 |
|
| 173 |
text: i18n.tr("Default address book") |
| 174 |
anchors { |
| 175 |
left: parent.left |
| 176 |
top: parent.top |
| 177 |
right: parent.right |
| 178 |
margins: units.gu(2) |
| 179 |
} |
| 180 |
height: units.gu(4) |
| 181 |
} |
| 182 |
|
| 183 |
OptionSelector { |
| 184 |
id: sources |
| 185 |
property bool _notify: true |
| 186 |
|
| 187 |
model: writableSources |
| 188 |
anchors { |
| 189 |
left: parent.left |
| 190 |
leftMargin: units.gu(2) |
| 191 |
top: label.bottom |
| 192 |
right: parent.right |
| 193 |
rightMargin: units.gu(2) |
| 194 |
bottom: parent.bottom |
| 195 |
bottomMargin: units.gu(2) |
| 196 |
} |
| 197 |
|
| 198 |
delegate: OptionSelectorDelegate { |
| 199 |
text: { |
| 200 |
if ((sourceId != "system-address-book") && (accountProvider == "")) { |
| 201 |
return i18n.dtr("lomiri-addressbook-app", "Personal - %1").arg(sourceName) |
| 202 |
} else { |
| 203 |
return sourceName |
| 204 |
} |
| 205 |
} |
| 206 |
height: units.gu(4) |
| 207 |
} |
| 208 |
|
| 209 |
containerHeight: sources.model && sources.model.count > 4 ? itemHeight * 4 : sources.model ? itemHeight * sources.model.count : 0 |
| 210 |
onSelectedIndexChanged: { |
| 211 |
if (_notify && selectedIndex >= 0) { |
| 212 |
root.changed() |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|