| 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 |
import Lomiri.Components 1.3 |
| 20 |
import Lomiri.Components.ListItems 1.3 as ListItem |
| 21 |
import Lomiri.Contacts 0.1 |
| 22 |
|
| 23 |
import "ContactList.js" as Sections |
| 24 |
|
| 25 |
/*! |
| 26 |
\qmltype ContactSimpleListView |
| 27 |
\inqmlmodule Lomiri.Contacts 0.1 |
| 28 |
\ingroup ubuntu |
| 29 |
\brief The ContactSimpleListView provides a simple contact list view |
| 30 |
|
| 31 |
The ContactSimpleListView provide a easy way to show the contact list view |
| 32 |
with all default visuals defined by Ubuntu system. |
| 33 |
|
| 34 |
Example: |
| 35 |
\qml |
| 36 |
import Lomiri.Contacts 0.1 |
| 37 |
|
| 38 |
ContactSimpleListView { |
| 39 |
anchors.fill: parent |
| 40 |
onContactClicked: console.debug("Contact ID:" + contactId) |
| 41 |
} |
| 42 |
\endqml |
| 43 |
*/ |
| 44 |
|
| 45 |
MultipleSelectionListView { |
| 46 |
id: contactListView |
| 47 |
|
| 48 |
/*! |
| 49 |
\qmlproperty bool showAvatar |
| 50 |
|
| 51 |
This property holds if the contact avatar will appear on the list or not. |
| 52 |
By default this is set to true. |
| 53 |
*/ |
| 54 |
property bool showAvatar: true |
| 55 |
|
| 56 |
/*! |
| 57 |
\qmlproperty list<SortOrder> sortOrders |
| 58 |
|
| 59 |
This property holds a list of sort orders used by the contacts model. |
| 60 |
\sa SortOrder |
| 61 |
*/ |
| 62 |
property list<SortOrder> sortOrders : [ |
| 63 |
SortOrder { |
| 64 |
detail: ContactDetail.Tag |
| 65 |
field: Tag.Tag |
| 66 |
direction: Qt.AscendingOrder |
| 67 |
blankPolicy: SortOrder.BlanksLast |
| 68 |
caseSensitivity: Qt.CaseInsensitive |
| 69 |
}, |
| 70 |
// empty tags will be sorted by display Label |
| 71 |
SortOrder { |
| 72 |
detail: ContactDetail.DisplayLabel |
| 73 |
field: DisplayLabel.Label |
| 74 |
direction: Qt.AscendingOrder |
| 75 |
blankPolicy: SortOrder.BlanksLast |
| 76 |
caseSensitivity: Qt.CaseInsensitive |
| 77 |
} |
| 78 |
] |
| 79 |
/*! |
| 80 |
\qmlproperty FetchHint fetchHint |
| 81 |
|
| 82 |
This property holds the fetch hint instance used by the contact model. |
| 83 |
|
| 84 |
\sa FetchHint |
| 85 |
*/ |
| 86 |
property var fetchHint : FetchHint { |
| 87 |
detailTypesHint: { |
| 88 |
var hints = [ ContactDetail.Tag, // sections |
| 89 |
ContactDetail.DisplayLabel // label |
| 90 |
] |
| 91 |
|
| 92 |
if (contactListView.showAvatar) { |
| 93 |
hints.push(ContactDetail.Avatar) |
| 94 |
} |
| 95 |
return hints |
| 96 |
} |
| 97 |
} |
| 98 |
/*! |
| 99 |
\qmlproperty bool multiSelectionEnabled |
| 100 |
|
| 101 |
This property holds if the multi selection mode is enabled or not |
| 102 |
By default this is set to false |
| 103 |
*/ |
| 104 |
property bool multiSelectionEnabled: false |
| 105 |
/*! |
| 106 |
\qmlproperty string defaultAvatarImage |
| 107 |
|
| 108 |
This property holds the default image url to be used when the current contact does |
| 109 |
not contains a photo |
| 110 |
*/ |
| 111 |
property string defaultAvatarImageUrl: "image://theme/contact" |
| 112 |
/*! |
| 113 |
\qmlproperty bool loading |
| 114 |
|
| 115 |
This property holds when the model still loading new contacts |
| 116 |
*/ |
| 117 |
readonly property bool loading: busyIndicator.busy |
| 118 |
/*! |
| 119 |
\qmlproperty bool showSections |
| 120 |
|
| 121 |
This property holds if the listview will show or not the section headers |
| 122 |
By default this is set to true |
| 123 |
*/ |
| 124 |
property bool showSections: true |
| 125 |
|
| 126 |
/*! |
| 127 |
\qmlproperty string manager |
| 128 |
|
| 129 |
This property holds the manager uri of the contact backend engine. |
| 130 |
By default this is set to "org.nemomobile.contacts.sqlite" |
| 131 |
*/ |
| 132 |
property string manager: ContactManager.manager() |
| 133 |
|
| 134 |
/*! |
| 135 |
\qmlproperty Action leftSideAction |
| 136 |
|
| 137 |
This property holds the available actions when swipe the contact item from left to right |
| 138 |
*/ |
| 139 |
property Action leftSideAction |
| 140 |
|
| 141 |
/*! |
| 142 |
\qmlproperty list<Action> rightSideActions |
| 143 |
|
| 144 |
This property holds the available actions when swipe the contact item from right to left |
| 145 |
*/ |
| 146 |
property list<Action> rightSideActions |
| 147 |
|
| 148 |
/*! |
| 149 |
\qmlproperty highlightSelected |
| 150 |
|
| 151 |
This property holds if the current contact should be highlighted or not |
| 152 |
*/ |
| 153 |
property bool highlightSelected: false |
| 154 |
|
| 155 |
/* internal */ |
| 156 |
property var _currentSwipedItem: null |
| 157 |
|
| 158 |
/*! |
| 159 |
This handler is called when any error occurs in the contact model |
| 160 |
*/ |
| 161 |
signal error(string message) |
| 162 |
/*! |
| 163 |
This handler is called when any contact in the list receives a click |
| 164 |
*/ |
| 165 |
signal contactClicked(QtObject contact) |
| 166 |
/*! |
| 167 |
This handler is called when the contact delegate disapear (height === 0) caused by the function call makeDisappear |
| 168 |
*/ |
| 169 |
signal contactDisappeared(QtObject contact) |
| 170 |
/*! |
| 171 |
Retrieve the contact index inside of the list based on contact id or contact name if the id is empty |
| 172 |
*/ |
| 173 |
function getIndex(contact) |
| 174 |
{ |
| 175 |
var contacts = listModel.contacts |
| 176 |
var contactId = null |
| 177 |
var firstName |
| 178 |
var middleName |
| 179 |
var lastName |
| 180 |
|
| 181 |
if (contact.contactId !== "qtcontacts:::") { |
| 182 |
contactId = contact.contactId |
| 183 |
} else { |
| 184 |
firstName = contact.name.firstName |
| 185 |
middleName = contact.name.middleName |
| 186 |
lastName = contact.name.lastName |
| 187 |
} |
| 188 |
|
| 189 |
for (var i = 0, count = contacts.length; i < count; i++) { |
| 190 |
var c = contacts[i] |
| 191 |
if (contactId && (c.contactId === contactId)) { |
| 192 |
return i |
| 193 |
} else if ((c.name.firstName === firstName) && |
| 194 |
(c.name.middleName === middleName) && |
| 195 |
(c.name.lastName === lastName)) { |
| 196 |
return i |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return -1 |
| 201 |
} |
| 202 |
|
| 203 |
/*! |
| 204 |
Scroll the list to requested contact if the contact exists in the list |
| 205 |
*/ |
| 206 |
function positionViewAtContact(contact) |
| 207 |
{ |
| 208 |
currentIndex = getIndex(contact) |
| 209 |
positionViewAtIndex(currentIndex, ListView.Center) |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
/*! |
| 214 |
Scroll the list to requested contact if the contact exists in the list |
| 215 |
*/ |
| 216 |
function positionViewAtContactId(contactId) |
| 217 |
{ |
| 218 |
var contacts = listModel.contacts |
| 219 |
|
| 220 |
for (var i = 0, count = contacts.length; i < count; i++) { |
| 221 |
var c = contacts[i] |
| 222 |
if (c.contactId === contactId) { |
| 223 |
currentIndex = i |
| 224 |
positionViewAtIndex(i, ListView.Center) |
| 225 |
return |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
function getSectionText(index) { |
| 231 |
return Sections.sectionValueForContact(listModel.contacts[index]) |
| 232 |
} |
| 233 |
|
| 234 |
/*! |
| 235 |
private |
| 236 |
Fetch contact and emit contact clicked signal |
| 237 |
*/ |
| 238 |
function _fetchContact(index, contact) |
| 239 |
{ |
| 240 |
if (contact) { |
| 241 |
contactFetch.fetchContact(contact.contactId) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
function _updateSwipeState(item) |
| 246 |
{ |
| 247 |
if (item.swipping) { |
| 248 |
return |
| 249 |
} |
| 250 |
|
| 251 |
if (item.swipeState !== "Normal") { |
| 252 |
if (contactListView._currentSwipedItem !== item) { |
| 253 |
if (contactListView._currentSwipedItem) { |
| 254 |
contactListView._currentSwipedItem.resetSwipe() |
| 255 |
} |
| 256 |
contactListView._currentSwipedItem = item |
| 257 |
} |
| 258 |
} else if (item.swipeState !== "Normal" && contactListView._currentSwipedItem === item) { |
| 259 |
contactListView._currentSwipedItem = null |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
highlightFollowsCurrentItem: true |
| 264 |
section { |
| 265 |
property: showSections ? "contact.displayLabel.label" : "" |
| 266 |
criteria: ViewSection.FirstCharacter |
| 267 |
labelPositioning: ViewSection.InlineLabels |
| 268 |
delegate: SectionDelegate { |
| 269 |
anchors { |
| 270 |
left: parent.left |
| 271 |
right: parent.right |
| 272 |
margins: units.gu(2) |
| 273 |
} |
| 274 |
text: section != "" ? section.toUpperCase() : "#" |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
onCountChanged: { |
| 279 |
busyIndicator.ping() |
| 280 |
dirtyModel.restart() |
| 281 |
} |
| 282 |
|
| 283 |
listDelegate: ContactDelegate { |
| 284 |
id: contactDelegate |
| 285 |
|
| 286 |
property var removalAnimation |
| 287 |
|
| 288 |
function remove() |
| 289 |
{ |
| 290 |
removalAnimation.start() |
| 291 |
} |
| 292 |
|
| 293 |
flicking: contactListView.flicking |
| 294 |
width: parent.width |
| 295 |
selected: (contactListView.multiSelectionEnabled && contactListView.isSelected(contactDelegate)) |
| 296 |
|| (!contactListView.isInSelectionMode && contactListView.highlightSelected && (contactListView.currentIndex == index)) |
| 297 |
selectionMode: contactListView.isInSelectionMode |
| 298 |
defaultAvatarUrl: contactListView.defaultAvatarImageUrl |
| 299 |
isCurrentItem: ListView.isCurrentItem |
| 300 |
|
| 301 |
// actions |
| 302 |
leftSideAction: contactListView.leftSideAction |
| 303 |
rightSideActions: contactListView.rightSideActions |
| 304 |
|
| 305 |
ListView.onRemove: SequentialAnimation { |
| 306 |
PropertyAction { target: contactDelegate; property: "ListView.delayRemove"; value: true } |
| 307 |
NumberAnimation { target: contactDelegate; property: "height"; to: 0; duration: 250; easing.type: Easing.InOutQuad } |
| 308 |
PropertyAction { target: contactDelegate; property: "ListView.delayRemove"; value: false } |
| 309 |
} |
| 310 |
|
| 311 |
// used by swipe to delete |
| 312 |
removalAnimation: SequentialAnimation { |
| 313 |
alwaysRunToEnd: true |
| 314 |
|
| 315 |
PropertyAction { |
| 316 |
target: contactDelegate |
| 317 |
property: "ListView.delayRemove" |
| 318 |
value: true |
| 319 |
} |
| 320 |
LomiriNumberAnimation { |
| 321 |
target: contactDelegate |
| 322 |
property: "height" |
| 323 |
to: 1 |
| 324 |
} |
| 325 |
PropertyAction { |
| 326 |
target: contactDelegate |
| 327 |
property: "ListView.delayRemove" |
| 328 |
value: false |
| 329 |
} |
| 330 |
ScriptAction { |
| 331 |
script: contactListView.listModel.removeContact(contact.contactId) |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
onClicked: { |
| 336 |
if (contactListView.isInSelectionMode) { |
| 337 |
if (!contactListView.selectItem(contactDelegate)) { |
| 338 |
contactListView.deselectItem(contactDelegate) |
| 339 |
} |
| 340 |
} else { |
| 341 |
contactListView.currentIndex = index |
| 342 |
contactListView._fetchContact(index, contact) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
onPressAndHold: { |
| 347 |
if (contactListView.multiSelectionEnabled) { |
| 348 |
contactListView.currentIndex = -1 |
| 349 |
contactListView.startSelection() |
| 350 |
contactListView.selectItem(contactDelegate) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
onSwippingChanged: contactListView._updateSwipeState(contactDelegate) |
| 355 |
onSwipeStateChanged: contactListView._updateSwipeState(contactDelegate) |
| 356 |
} |
| 357 |
|
| 358 |
ContactFetch { |
| 359 |
id: contactFetch |
| 360 |
|
| 361 |
model: root.listModel |
| 362 |
onContactFetched: contactListView.contactClicked(contact) |
| 363 |
} |
| 364 |
// This is a workaround to make sure the spinner will disappear if the model is empty |
| 365 |
// FIXME: implement a model property to say if the model still busy or not |
| 366 |
Item { |
| 367 |
id: busyIndicator |
| 368 |
|
| 369 |
property bool busy: timer.running || priv.currentOperation !== -1 |
| 370 |
|
| 371 |
function ping() |
| 372 |
{ |
| 373 |
timer.restart() |
| 374 |
} |
| 375 |
|
| 376 |
visible: busy |
| 377 |
anchors.fill: parent |
| 378 |
|
| 379 |
Timer { |
| 380 |
id: timer |
| 381 |
|
| 382 |
interval: 3000 |
| 383 |
running: true |
| 384 |
repeat: false |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
Timer { |
| 389 |
id: dirtyModel |
| 390 |
|
| 391 |
interval: 1000 |
| 392 |
running: false |
| 393 |
repeat: false |
| 394 |
onTriggered: Sections.initSectionData(contactListView) |
| 395 |
} |
| 396 |
|
| 397 |
QtObject { |
| 398 |
id: priv |
| 399 |
|
| 400 |
property int currentOperation: -1 |
| 401 |
property int pendingTargetIndex: 0 |
| 402 |
property variant pendingTargetMode: null |
| 403 |
} |
| 404 |
} |