Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2023 Ubports Foundation
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.0
18 import QtQuick.Layouts 1.12
19 import Lomiri.Components 1.3
20 import Lomiri.Components.Popups 1.3
21 import Lomiri.Content 1.3
22 import QtMultimedia 5.6
23 import Lomiri.Contacts 0.1
24
25 Page {
26 id: root
27 objectName: "contactSoundsPage"
28
29 property var activeTransfer
30 property string currentSoundFile: ""
31 property alias currentRingtoneType: ringToneModel.ringToneType
32
33 onCurrentSoundFileChanged: soundSelected(currentSoundFile)
34
35 signal soundSelected(string file)
36 signal closeRequested()
37
38 function requestFileDelete(path) {
39 var dialog = PopupUtils.open(deleteConfirm, root)
40 dialog.canceled.connect(function() {
41 PopupUtils.close(dialog)
42 });
43 dialog.accepted.connect(function() {
44 ringToneModel.removeFile(path)
45 PopupUtils.close(dialog)
46 });
47 }
48
49 function onImportedFile(uri) {
50 const newFile = uri.toString().replace("file:///", "/")
51 ringToneModel.addFile(newFile);
52
53 root.currentSoundFile = newFile
54 soundList.currentIndex = ringToneModel.getIndex(root.currentSoundFile);
55 soundList.positionViewAtIndex(soundList.currentIndex, ListView.Center)
56 soundEffect.play()
57 }
58
59 header: PageHeader {
60 title: i18n.dtr("lomiri-addressbook-app", "Select a custom sound")
61 leadingActionBar {
62 actions: [
63 Action {
64 text: i18n.tr("back")
65 iconName: "back"
66 onTriggered: {
67 root.closeRequested()
68 }
69 }
70 ]
71 }
72
73 trailingActionBar {
74 actions: [
75 Action {
76 text: i18n.tr("import")
77 iconName: "import"
78 onTriggered: {
79 PopupUtils.open(pickerDialog, null)
80 }
81 }
82 ]
83 }
84 }
85
86 Audio {
87 id: soundEffect
88 audioRole: MediaPlayer.NotificationRole
89 source: currentSoundFile
90 }
91
92 RingtoneModel {
93 id: ringToneModel
94 }
95
96 Rectangle {
97 anchors.fill: parent
98 color: theme.palette.normal.background
99 }
100
101 Label {
102 anchors.centerIn: parent
103 text: i18n.tr("No sounds found")
104 visible: soundList.count === 0
105 }
106
107 ColumnLayout {
108 id: column
109 anchors {
110 top: header.bottom
111 left: parent.left
112 right: parent.right
113 bottom: stopItem.top
114
115 }
116 ListView {
117 id: soundList
118 Layout.fillHeight: true
119 Layout.fillWidth: true
120 currentIndex: -1
121 model: ringToneModel
122 onCountChanged: {
123 if (count >0) {
124 soundList.currentIndex = ringToneModel.getIndex(root.currentSoundFile);
125 soundList.positionViewAtIndex(soundList.currentIndex, ListView.Center)
126 }
127 }
128
129 clip: true
130 section {
131 property: "readOnly"
132 criteria: ViewSection.FullString
133 labelPositioning: ViewSection.InlineLabels
134 delegate: Rectangle {
135 anchors {
136 left: parent.left
137 right: parent.right
138 margins: units.gu(2)
139 }
140
141 color: Theme.palette.normal.background
142 height: units.gu(6)
143 Label {
144 id: title
145
146 anchors.fill: parent
147 verticalAlignment: Text.AlignVCenter
148 height: units.gu(3)
149 font.bold: true
150 text: section == "true" ? i18n.tr("System sounds:") : i18n.tr("Custom sounds:")
151 }
152 }
153 }
154
155 delegate: ListItem {
156 id: soundItem
157 height: layout.height + (divider.visible ? divider.height : 0)
158 selected: soundList.currentIndex === index
159 swipeEnabled: !readOnly
160 leadingActions: ListItemActions {
161 actions: [
162 Action {
163 iconName: "delete"
164 onTriggered: root.requestFileDelete(path)
165 }
166 ]
167 }
168 ListItemLayout {
169 id: layout
170 title.text: name;
171
172 Icon {
173 name: "tick"
174 visible: soundItem.selected
175 SlotsLayout.position: SlotsLayout.Trailing;
176 width: units.gu(2)
177 }
178 }
179 onClicked: {
180 soundList.currentIndex = index
181 root.currentSoundFile = path
182 soundEffect.play()
183 }
184 }
185 }
186 }
187
188 ListItem {
189 id: stopItem
190 anchors.bottom: parent.bottom
191 enabled: soundEffect.playbackState == Audio.PlayingState
192 visible: enabled
193 AbstractButton {
194 id: stopButton
195 anchors.verticalCenter: parent.verticalCenter
196 anchors.horizontalCenter: parent.horizontalCenter
197 focus: false
198 width: height
199 height: units.gu(4)
200 visible: stopItem.enabled
201
202 Rectangle {
203 anchors.fill: parent
204 radius: width * 0.5
205 border.color: theme.palette.normal.raisedText
206 border.width: 1
207 }
208
209 Rectangle {
210 width: parent.height * 0.4
211 height: width
212 smooth: true
213 anchors {
214 verticalCenter: parent.verticalCenter
215 horizontalCenter: parent.horizontalCenter
216 }
217 color: theme.palette.normal.raisedText
218 }
219 onClicked: soundEffect.stop()
220 }
221
222 Rectangle {
223 anchors.fill: parent
224 z: parent.z - 1
225 visible: stopItem.visible
226 color: Theme.palette.normal.background
227 }
228 }
229
230 Component {
231 id: pickerDialog
232
233 Item {
234 id: picker
235
236 property var activeTransfer: null
237 signal avatarReceived(string avatarUrl)
238 signal destruction()
239
240 anchors.fill: parent
241
242 function hide() {
243 picker.visible = false;
244 }
245
246 function show() {
247 picker.visible = true;
248 }
249
250 ContentTransferHint {
251 anchors.fill: parent
252 activeTransfer: picker.activeTransfer
253 }
254
255 ContentPeerPicker {
256 id: peerPicker
257
258 anchors.fill: parent
259 handler: ContentHandler.Source
260 contentType: ContentType.Music
261 showTitle: true
262
263 onPeerSelected: {
264 peer.selectionType = ContentTransfer.Single
265 picker.activeTransfer = peer.request()
266 }
267
268 onCancelPressed: PopupUtils.close(picker);
269 }
270
271 Connections {
272 id: contentHubConnection
273
274 property var allowedExtensions :["wav","ogg","mp3"]
275
276 target: picker.activeTransfer
277
278 function onStateChanged() {
279 if (activeTransfer.state === ContentTransfer.Charged) {
280 if (activeTransfer.items.length > 0) {
281
282 var customSoundPath = ringToneModel.soundPath(currentRingtoneType)
283
284 var item = activeTransfer.items[0];
285 const isMusicFile = allowedExtensions.some((ext) => item.url.toString().endsWith(ext));
286 if (!isMusicFile) {
287 PopupUtils.open(unsupportedFile, root)
288 activeTransfer.finalize()
289 return;
290 }
291
292 var toneUri;
293 if (item.move(customSoundPath)) {
294 toneUri = item.url;
295 } else {
296 PopupUtils.open(importError, root)
297 activeTransfer.finalize()
298 return;
299 }
300
301 console.log("moved file to:", toneUri)
302 PopupUtils.close(picker)
303
304 root.onImportedFile(toneUri);
305 }
306 activeTransfer.finalize()
307 }
308 }
309 }
310 }
311 }
312
313 Component {
314 id: deleteConfirm
315
316 Dialog {
317 id: dialog
318
319 property int threadCount: 0
320
321 title: i18n.tr("Delete file")
322 text: i18n.tr("Are you sure you want to delete this file ?")
323
324 signal accepted()
325 signal canceled()
326
327 Column {
328 anchors.left: parent.left
329 anchors.right: parent.right
330 spacing: units.gu(2)
331 Row {
332 anchors.horizontalCenter: parent.horizontalCenter
333 spacing: units.gu(4)
334 Button {
335 text: i18n.tr("Cancel")
336 onClicked: dialog.canceled()
337 }
338 Button {
339 text: i18n.tr("Delete")
340 color: theme.palette.normal.negative
341 onClicked: dialog.accepted()
342 }
343 }
344 }
345 }
346 }
347
348 Component {
349 id: unsupportedFile
350 Dialog {
351 id: dialog
352
353 title: i18n.tr("Unsupported file")
354
355 text: i18n.tr("Sorry, only ogg, mp3 and wav file format is supported")
356
357 Column {
358 anchors.left: parent.left
359 anchors.right: parent.right
360 spacing: units.gu(2)
361 Row {
362 anchors.horizontalCenter: parent.horizontalCenter
363 spacing: units.gu(4)
364 Button {
365 text: i18n.tr("ok")
366 onClicked: dialog.hide()
367 }
368 }
369 }
370 }
371 }
372
373 Component {
374 id: importError
375 Dialog {
376 id: dialog
377
378 title: i18n.tr("Import error")
379
380 text: i18n.tr("Sorry, an error occurred while importing")
381
382 Column {
383 anchors.left: parent.left
384 anchors.right: parent.right
385 spacing: units.gu(2)
386 Row {
387 anchors.horizontalCenter: parent.horizontalCenter
388 spacing: units.gu(4)
389 Button {
390 text: i18n.tr("ok")
391 onClicked: dialog.hide()
392 }
393 }
394 }
395 }
396 }
397
398 Component.onDestruction: {
399 if (soundEffect.playbackState === Audio.PlayingState) {
400 soundEffect.stop()
401 }
402 }
403 }
404