| 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 |
| 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 |
ContactDetailBase { |
| 27 |
id: root |
| 28 |
|
| 29 |
property alias active: sourceModel.autoUpdate |
| 30 |
property bool isNewContact: contact && contact.contactId === "qtcontacts:::" |
| 31 |
property real myHeight: label.height + units.gu(6) + (sources.currentlyExpanded ? sources.containerHeight : |
| 32 |
sources.itemHeight) |
| 33 |
|
| 34 |
signal changed() |
| 35 |
|
| 36 |
function save() { |
| 37 |
// only changes the target sync for new contacts |
| 38 |
if (!isNewContact) { |
| 39 |
return; |
| 40 |
} |
| 41 |
var activeSource = getSelectedSource() |
| 42 |
if (!activeSource) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if (!root.detail) { |
| 47 |
root.detail = root.contact.syncTarget |
| 48 |
} |
| 49 |
root.detail.syncTarget = activeSource |
| 50 |
} |
| 51 |
|
| 52 |
function getSelectedSource() { |
| 53 |
if (sources.model.count <= 0) |
| 54 |
return -1 |
| 55 |
|
| 56 |
var selectedSourceId = sources.model.get(sources.selectedIndex).sourceId |
| 57 |
if (selectedSourceId) { |
| 58 |
return selectedSourceId |
| 59 |
} else { |
| 60 |
return -1 |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
function contactIsReadOnly(contact) { |
| 65 |
var sources = sourceModel.contacts |
| 66 |
var contactSyncTarget = contact.syncTarget.value(SyncTarget.SyncTarget + 1) |
| 67 |
|
| 68 |
for (var i = 0; i < writableSources.count; i++) { |
| 69 |
var source = writableSources.get(i) |
| 70 |
if (source.sourceId === contactSyncTarget) { |
| 71 |
return false |
| 72 |
} |
| 73 |
} |
| 74 |
return true |
| 75 |
} |
| 76 |
|
| 77 |
detail: root.contact ? contact.detail(ContactDetail.SyncTarget) : null |
| 78 |
implicitHeight: root.isNewContact && sources.model && (sources.model.count > 1) ? myHeight : 0 |
| 79 |
visible: height > 0 |
| 80 |
|
| 81 |
ContactModel { |
| 82 |
id: sourceModel |
| 83 |
|
| 84 |
manager: ContactManager.manager() |
| 85 |
filter: DetailFilter { |
| 86 |
detail: ContactDetail.Type |
| 87 |
field: Type.TypeField |
| 88 |
value: Type.Group |
| 89 |
matchFlags: DetailFilter.MatchExactly |
| 90 |
} |
| 91 |
autoUpdate: false |
| 92 |
onContactsChanged: { |
| 93 |
if (contacts.length > 0) { |
| 94 |
writableSources.reload() |
| 95 |
root.changed() |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
ListModel { |
| 101 |
id: writableSources |
| 102 |
|
| 103 |
function getSourceMetaData(contact) { |
| 104 |
var metaData = {'read-only' : false, |
| 105 |
'account-provider': '', |
| 106 |
'account-id': 0, |
| 107 |
'is-primary': false} |
| 108 |
|
| 109 |
var details = contact.details(ContactDetail.ExtendedDetail) |
| 110 |
for(var d in details) { |
| 111 |
if (details[d].name === "READ-ONLY") { |
| 112 |
metaData['read-only'] = details[d].data |
| 113 |
} else if (details[d].name === "PROVIDER") { |
| 114 |
metaData['account-provider'] = details[d].data |
| 115 |
} else if (details[d].name === "APPLICATION-ID") { |
| 116 |
metaData['account-id'] = details[d].data |
| 117 |
} else if (details[d].name === "IS-PRIMARY") { |
| 118 |
metaData['is-primary'] = details[d].data |
| 119 |
} |
| 120 |
} |
| 121 |
return metaData |
| 122 |
} |
| 123 |
|
| 124 |
function reload() { |
| 125 |
clear() |
| 126 |
|
| 127 |
// filter out read-only sources |
| 128 |
var contacts = sourceModel.contacts |
| 129 |
if (contacts.length === 0) { |
| 130 |
return |
| 131 |
} |
| 132 |
|
| 133 |
var data = [] |
| 134 |
for(var i in contacts) { |
| 135 |
var sourceMetaData = getSourceMetaData(contacts[i]) |
| 136 |
if (!sourceMetaData['read-only']) { |
| 137 |
data.push({'sourceId': contacts[i].guid.guid, |
| 138 |
'sourceName': contacts[i].displayLabel.label, |
| 139 |
'accountId': sourceMetaData['account-id'], |
| 140 |
'accountProvider': sourceMetaData['account-provider'], |
| 141 |
'readOnly': sourceMetaData['read-only'], |
| 142 |
'isPrimary': sourceMetaData['is-primary'] |
| 143 |
}) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
data.sort(function(a, b) { |
| 148 |
var valA = a.accountId |
| 149 |
var valB = b.accountId |
| 150 |
if (a.accountId == b.accountId) { |
| 151 |
valA = a.sourceName |
| 152 |
valB = b.sourceName |
| 153 |
} |
| 154 |
|
| 155 |
if (valA == valB) { |
| 156 |
return 0 |
| 157 |
} else if (valA < valB) { |
| 158 |
return -1 |
| 159 |
} else { |
| 160 |
return 1 |
| 161 |
} |
| 162 |
}) |
| 163 |
|
| 164 |
var primaryIndex = 0 |
| 165 |
for (var i in data) { |
| 166 |
if (data[i].isPrimary) { |
| 167 |
primaryIndex = i |
| 168 |
} |
| 169 |
append(data[i]) |
| 170 |
} |
| 171 |
|
| 172 |
// select primary account |
| 173 |
sources.selectedIndex = primaryIndex |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
ContactDetailTitle { |
| 178 |
id: label |
| 179 |
title: i18n.dtr("lomiri-addressbook-app", "Addressbook") |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
OptionSelector { |
| 184 |
id: sources |
| 185 |
|
| 186 |
model: writableSources |
| 187 |
anchors { |
| 188 |
left: parent.left |
| 189 |
leftMargin: units.gu(2) |
| 190 |
top: label.bottom |
| 191 |
topMargin: units.gu(2) |
| 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 |
} |
| 211 |
|
| 212 |
onActiveChanged: { |
| 213 |
if (active) { |
| 214 |
sourceModel.update() |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// In case of sources changed we need to update the model |
| 219 |
Connections { |
| 220 |
target: application |
| 221 |
function onSourcesChanged() { sourceModel.update() } |
| 222 |
} |
| 223 |
} |
| 224 |
|