| 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.12 |
| 18 |
import QtContacts 5.0 |
| 19 |
|
| 20 |
import Lomiri.Components 1.3 |
| 21 |
import Lomiri.Components.Popups 1.3 |
| 22 |
import Lomiri.Contacts 0.1 |
| 23 |
import Lomiri.AddressBook.Base 0.1 |
| 24 |
|
| 25 |
ContactDetailBase { |
| 26 |
id: root |
| 27 |
|
| 28 |
readonly property alias busy: activityIndicator.running |
| 29 |
readonly property string defaultAvatar: "image://theme/contact" |
| 30 |
property string temporaryAvatar: "" |
| 31 |
property string temporaryAvatarId: "" |
| 32 |
|
| 33 |
function isEmpty() { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
function save() { |
| 38 |
const newAvatarUrl = avatarImage.source == root.defaultAvatar ? "" : avatarImage.source; |
| 39 |
if (root.detail && (root.detail.imageUrl === newAvatarUrl)) { |
| 40 |
return false |
| 41 |
} else { |
| 42 |
// create the avatar detail |
| 43 |
if (!root.detail) { |
| 44 |
root.detail = root.contact.avatar |
| 45 |
} |
| 46 |
|
| 47 |
root.detail.imageUrl = newAvatarUrl |
| 48 |
return true |
| 49 |
} |
| 50 |
return false |
| 51 |
} |
| 52 |
|
| 53 |
function getAvatar(avatarDetail) |
| 54 |
{ |
| 55 |
// use this verbose mode to avoid problems with binding loops |
| 56 |
var avatarUrl = root.defaultAvatar |
| 57 |
|
| 58 |
if (avatarDetail) { |
| 59 |
var avatarValue = avatarDetail.value(Avatar.ImageUrl) |
| 60 |
if (avatarValue && (avatarValue != "")) { |
| 61 |
avatarUrl = avatarValue |
| 62 |
} |
| 63 |
} |
| 64 |
return avatarUrl |
| 65 |
} |
| 66 |
|
| 67 |
detail: contact ? contact.detail(ContactDetail.Avatar) : null |
| 68 |
implicitHeight: units.gu(8) |
| 69 |
implicitWidth: units.gu(8) |
| 70 |
highlightOnFocus: false |
| 71 |
|
| 72 |
LomiriShape { |
| 73 |
id: avatar |
| 74 |
|
| 75 |
radius: "medium" |
| 76 |
anchors.fill: parent |
| 77 |
source: avatarImage |
| 78 |
sourceFillMode: LomiriShape.PreserveAspectCrop |
| 79 |
|
| 80 |
Image { |
| 81 |
id: avatarImage |
| 82 |
objectName: "avatarImage" |
| 83 |
|
| 84 |
asynchronous: true |
| 85 |
source: root.getAvatar(root.detail) |
| 86 |
anchors.centerIn: visible ? avatar : undefined |
| 87 |
height: units.gu(3) |
| 88 |
width: units.gu(3) |
| 89 |
visible: false |
| 90 |
autoTransform: true |
| 91 |
sourceSize { |
| 92 |
width: avatar.width |
| 93 |
height: avatar.height |
| 94 |
} |
| 95 |
|
| 96 |
// When updating the avatar using the content picker the temporary file returned |
| 97 |
// can contain the same name as the previous one and if the cache is enabled this |
| 98 |
// will cause the image to not be updated |
| 99 |
cache: false |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
LomiriShape { |
| 104 |
id: addShape |
| 105 |
|
| 106 |
height: units.gu(3) |
| 107 |
width: units.gu(3) |
| 108 |
anchors.right: avatar.right |
| 109 |
anchors.bottom: avatar.bottom |
| 110 |
anchors.margins: units.gu(0.5) |
| 111 |
backgroundColor: theme.palette.normal.background |
| 112 |
|
| 113 |
Image { |
| 114 |
id: addImage |
| 115 |
objectName: "addImage" |
| 116 |
|
| 117 |
asynchronous: true |
| 118 |
source: "image://theme/add" |
| 119 |
anchors.centerIn: addShape |
| 120 |
|
| 121 |
height: units.gu(2) |
| 122 |
width: units.gu(2) |
| 123 |
visible: true |
| 124 |
sourceSize { |
| 125 |
width: units.gu(2) |
| 126 |
height:units.gu(2) |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
ActivityIndicator { |
| 134 |
id: activityIndicator |
| 135 |
|
| 136 |
anchors.centerIn: avatar |
| 137 |
running: (avatarImport.importDialog != null) || (root.temporaryAvatarId != "") |
| 138 |
visible: running |
| 139 |
} |
| 140 |
|
| 141 |
AvatarImport { |
| 142 |
id: avatarImport |
| 143 |
|
| 144 |
onAvatarReceived: { |
| 145 |
Contacts.removeFile(root.temporaryAvatar) |
| 146 |
|
| 147 |
// remove the previous image, this is nessary to make sure that the new image |
| 148 |
// get updated otherwise if the new image has the same name the image will not |
| 149 |
// be updated |
| 150 |
avatarImage.source = "" |
| 151 |
// copy and resize image |
| 152 |
root.temporaryAvatarId = Contacts.copyImage(avatarUrl, null); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
Connections { |
| 157 |
target: Contacts |
| 158 |
function onImageCopyDone(id, fileName) { |
| 159 |
if (root.temporaryAvatarId === id) { |
| 160 |
root.temporaryAvatar = fileName |
| 161 |
avatarImage.source = fileName |
| 162 |
root.temporaryAvatarId = "" |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
MouseArea { |
| 168 |
anchors.fill: parent |
| 169 |
onClicked: { |
| 170 |
// make sure the OSK disappear |
| 171 |
root.forceActiveFocus() |
| 172 |
|
| 173 |
if (avatarImage.source != root.defaultAvatar) { |
| 174 |
var dialog = PopupUtils.open(dialogCmp, addShape) |
| 175 |
dialog.removeRequested.connect(function() { |
| 176 |
Contacts.removeFile(root.temporaryAvatar) |
| 177 |
avatarImage.source = root.defaultAvatar |
| 178 |
PopupUtils.close(dialog) |
| 179 |
}); |
| 180 |
dialog.importRequested.connect(function() { |
| 181 |
avatarImport.requestNewAvatar() |
| 182 |
PopupUtils.close(dialog) |
| 183 |
}); |
| 184 |
} else { |
| 185 |
avatarImport.requestNewAvatar() |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
Component { |
| 192 |
id: dialogCmp |
| 193 |
|
| 194 |
Popover { |
| 195 |
id: popover |
| 196 |
|
| 197 |
signal removeRequested() |
| 198 |
signal importRequested() |
| 199 |
|
| 200 |
Column { |
| 201 |
id: containerLayout |
| 202 |
anchors { |
| 203 |
left: parent.left |
| 204 |
top: parent.top |
| 205 |
right: parent.right |
| 206 |
} |
| 207 |
|
| 208 |
ListItem { |
| 209 |
ListItemLayout { |
| 210 |
id: somethingLayout |
| 211 |
title.text: i18n.tr("Remove image") |
| 212 |
} |
| 213 |
onClicked: popover.removeRequested() |
| 214 |
} |
| 215 |
ListItem { |
| 216 |
ListItemLayout { |
| 217 |
id: somethingElseLayout |
| 218 |
title.text: i18n.tr("Import image") |
| 219 |
} |
| 220 |
onClicked: popover.importRequested() |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
Component.onDestruction: { |
| 227 |
root.temporaryAvatar = "" |
| 228 |
} |
| 229 |
} |