Debian Codegraph @

src/imports/Lomiri/AddressBook/ContactEditor/ContactDetailAvatarEditor.qml

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