| 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 |
property var contactModel: null |
| 30 |
|
| •31 |
signal changed() |
| 32 |
|
| 33 |
function save() |
| 34 |
{ |
| 35 |
var changed = false |
| 36 |
var selectedSource = getSelectedSource() |
| 37 |
if (!selectedSource) { |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
var details = selectedSource.details(ContactDetail.ExtendedDetail) |
| 42 |
for(var d in details) { |
| 43 |
if (details[d].name === "IS-PRIMARY") { |
| 44 |
if (!details[d].data) { |
| 45 |
details[d].data = true |
| 46 |
changed = true |
| 47 |
break |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
if (changed) { |
| 52 |
sourceModel.saveContact(selectedSource) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
function getSelectedSource() { |
| 57 |
if (sources.model.count <= 0) |
| 58 |
return -1 |
| 59 |
|
| 60 |
return sources.model.get(sources.selectedIndex).source |
| 61 |
} |
| 62 |
|
| 63 |
height: sources.currentlyExpanded ? |
| 64 |
sources.containerHeight + units.gu(6) + label.height : |
| 65 |
sources.itemHeight + units.gu(6) + label.height |
| 66 |
|
| 67 |
ListModel { |
| 68 |
id: writableSources |
| 69 |
|
| •70 |
function populate() { |
| 71 |
clear() |
| 72 |
|
| •73 |
var collections = contactModel.collections; |
| 74 |
for(var i = 0 ; i < collections.length ; ++i) { |
| 75 |
var col = collections[i] |
| 76 |
const isReadOnly = col.extendedMetaData("ReadOnly") === true |
| 77 |
const isAggregate = col.collectionId === Contacts.aggregateCollectionId |
| 78 |
const accountId = col.extendedMetaData("AccountId") |
| 79 |
console.log(col.name, isReadOnly, isAggregate, accountId) |
| 80 |
if (!isReadOnly && !isAggregate) { |
| 81 |
var item = { |
| 82 |
'collectionId': col.collectionId, |
| 83 |
'name': col.name, |
| 84 |
'description': col.description, |
| 85 |
'isLocal': col.extendedMetaData("AccountId") === 0 |
| 86 |
} |
| 87 |
append(item) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
Label { |
| 94 |
id: label |
| 95 |
|
| 96 |
text: i18n.tr("Default address book") |
| 97 |
anchors { |
| 98 |
left: parent.left |
| 99 |
top: parent.top |
| 100 |
right: parent.right |
| 101 |
margins: units.gu(2) |
| 102 |
} |
| 103 |
height: units.gu(4) |
| 104 |
} |
| 105 |
|
| 106 |
OptionSelector { |
| 107 |
id: sources |
| 108 |
property bool _notify: true |
| 109 |
|
| 110 |
model: writableSources |
| 111 |
|
| 112 |
delegate: OptionSelectorDelegate { |
| 113 |
text: { |
| •114 |
if (isLocal) { |
| 115 |
return i18n.dtr("lomiri-addressbook-app", "Personal - %1").arg(name) |
| 116 |
} else { |
| •117 |
return name |
| 118 |
} |
| 119 |
} |
| •120 |
subText: description |
| 121 |
height: units.gu(4) |
| 122 |
} |
| 123 |
|
| 124 |
containerHeight: sources.model && sources.model.count > 4 ? itemHeight * 4 : sources.model ? itemHeight * sources.model.count : 0 |
| •125 |
|
| 126 |
onDelegateClicked: { |
| 127 |
const ab = model.get(index) |
| 128 |
console.log('changed default address book to', ab.name) |
| 129 |
Contacts.defaultCollectionId = ab.collectionId |
| 130 |
} |
| 131 |
|
| 132 |
Component.onCompleted: { |
| 133 |
writableSources.populate() |
| 134 |
for( var i = 0; i < writableSources.count; i++ ) { |
| 135 |
console.log(writableSources.get(i).collectionId, Contacts.defaultCollectionId) |
| 136 |
if (writableSources.get(i).collectionId === Contacts.defaultCollectionId) { |
| 137 |
selectedIndex = i |
| 138 |
break |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|